Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.swing / org.gvsig.installer.swing.impl / src / main / java / org / gvsig / installer / swing / impl / execution / panel / SelectBundlesPanel.java @ 38422

History | View | Annotate | Download (8.24 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27

    
28
package org.gvsig.installer.swing.impl.execution.panel;
29

    
30
import java.awt.BorderLayout;
31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.awt.Insets;
34
import java.awt.event.ItemEvent;
35
import java.awt.event.ItemListener;
36
import java.io.File;
37
import java.net.MalformedURLException;
38
import java.net.URL;
39
import java.util.List;
40

    
41
import javax.swing.ButtonGroup;
42
import javax.swing.JComboBox;
43
import javax.swing.JPanel;
44
import javax.swing.JRadioButton;
45
import javax.swing.JTextField;
46
import javax.swing.event.DocumentEvent;
47
import javax.swing.event.DocumentListener;
48

    
49
import org.gvsig.gui.beans.openfile.FileFilter;
50
import org.gvsig.gui.beans.openfile.FileTextField;
51
import org.gvsig.installer.swing.api.SwingInstallerLocator;
52
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager;
53

    
54
/**
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
56
 */
57
public class SelectBundlesPanel extends JPanel implements ItemListener,
58
                DocumentListener {
59

    
60
        /**
61
     * 
62
     */
63
        private static final long serialVersionUID = -6580729307001414868L;
64
        protected DefaultSwingInstallerManager swingInstallerManager = null;
65
        private JRadioButton fileRadioButton;
66
        private ButtonGroup buttonGroup;
67
        private JPanel northPanel;
68
        private FileTextField selectFileText;
69
        private JRadioButton standardRadioButton;
70
        private JComboBox downloadURL;
71
        private JRadioButton urlRadioButton;
72
        private final List<URL> defaultDownloadURL;
73

    
74
        public SelectBundlesPanel(List<URL> defaultDownloadURL, File installFolder) {
75
                super();
76
                this.defaultDownloadURL = defaultDownloadURL;
77
                swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator
78
                                .getSwingInstallerManager();
79
                initComponents();
80
                initListeners();
81
                
82
                String[] files = installFolder.list();
83
                
84
                if (files.length >0){
85
                        standardRadioButton.setSelected(true);
86
                } else {
87
                        urlRadioButton.setSelected(true);        
88
                        standardRadioButton.setEnabled(false);
89
                }
90
        }
91

    
92
        private void initListeners() {
93
                standardRadioButton.addItemListener(this);
94
                fileRadioButton.addItemListener(this);
95
                urlRadioButton.addItemListener(this);
96
                Object obj = selectFileText.getComponent(0);
97
                if ((obj != null) && (obj instanceof JTextField)) {
98
                        ((JTextField) obj).getDocument().addDocumentListener(this);
99
                }
100
        }
101

    
102
        private void initComponents() {
103
                java.awt.GridBagConstraints gridBagConstraints;
104

    
105
                northPanel = new JPanel();
106
                standardRadioButton = new JRadioButton();
107
                fileRadioButton = new JRadioButton();
108
                urlRadioButton = new JRadioButton();
109
                selectFileText = new FileTextField();
110
                selectFileText.setFileFilter(new FileFilter() {
111

    
112
                        private String packageExt = swingInstallerManager
113
                                        .getInstallerManager().getDefaultPackageFileExtension();
114
                        private String packageSetExt = swingInstallerManager
115
                                        .getInstallerManager().getDefaultPackageSetFileExtension();
116
                        private String indexSetExt = swingInstallerManager
117
                                        .getInstallerManager().getDefaultIndexSetFileExtension();
118

    
119
                        @Override
120
                        public String getDescription() {
121
                                return "_gvSIG_packages_and_packages_and_index_sets" + " (*."
122
                                                + packageExt + ", *." + packageSetExt + ", *."
123
                                                + indexSetExt + ")";
124
                        }
125

    
126
                        @Override
127
                        public boolean accept(File file) {
128
                                if (file.isFile()) {
129
                                        String name = file.getName().toLowerCase();
130
                                        return name.endsWith(packageExt)
131
                                                        || name.endsWith(packageSetExt)
132
                                                        || name.endsWith(indexSetExt);
133
                                }
134
                                return true;
135
                        }
136

    
137
                        @Override
138
                        public String getDefaultExtension() {
139
                                return packageSetExt;
140
                        }
141
                });
142

    
143
                downloadURL = new JComboBox();
144
                downloadURL.setEditable(true);
145
                if (defaultDownloadURL != null) {
146
                        for (int i = 0; i < defaultDownloadURL.size(); i++) {
147
                                downloadURL.addItem(defaultDownloadURL.get(i));
148
                        }
149
                }
150

    
151
                buttonGroup = new ButtonGroup();
152

    
153
                buttonGroup.add(standardRadioButton);
154
                buttonGroup.add(fileRadioButton);
155
                buttonGroup.add(urlRadioButton);
156

    
157
                setLayout(new BorderLayout());
158

    
159
                northPanel.setLayout(new GridBagLayout());
160

    
161
                standardRadioButton.setText(swingInstallerManager
162
                                .getText("_standard_installation") +
163
                                " (" +
164
                                swingInstallerManager.getText("_install_addons_in_gvsig_standard_dist") +
165
                                ")");
166

    
167
                gridBagConstraints = new GridBagConstraints();
168
                gridBagConstraints.anchor = GridBagConstraints.WEST;
169
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
170
                northPanel.add(standardRadioButton, gridBagConstraints);
171

    
172
                fileRadioButton.setText(swingInstallerManager
173
                                .getText("_installation_from_file") +
174
                             " (" +
175
                             swingInstallerManager.getText("_install_addons_in_gvspki_or_gvspks_file") +
176
                             ")");
177

    
178
                gridBagConstraints = new GridBagConstraints();
179
                gridBagConstraints.gridx = 0;
180
                gridBagConstraints.gridy = 1;
181
                gridBagConstraints.anchor = GridBagConstraints.WEST;
182
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
183
                northPanel.add(fileRadioButton, gridBagConstraints);
184
                gridBagConstraints = new GridBagConstraints();
185
                gridBagConstraints.gridx = 0;
186
                gridBagConstraints.gridy = 2;
187
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
188
                gridBagConstraints.weightx = 1.0;
189
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
190
                northPanel.add(selectFileText, gridBagConstraints);
191

    
192
                urlRadioButton.setText(swingInstallerManager
193
                                .getText("_installation_from_url") +
194
                     " (" +
195
                     swingInstallerManager.getText("_install_addons_from_remote_repo") +
196
                     ")");
197
                
198
                gridBagConstraints = new GridBagConstraints();
199
                gridBagConstraints.gridx = 0;
200
                gridBagConstraints.gridy = 3;
201
                gridBagConstraints.anchor = GridBagConstraints.WEST;
202
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
203
                northPanel.add(urlRadioButton, gridBagConstraints);
204
                gridBagConstraints = new GridBagConstraints();
205
                gridBagConstraints.gridx = 0;
206
                gridBagConstraints.gridy = 4;
207
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
208
                gridBagConstraints.weightx = 1.0;
209
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
210
                northPanel.add(downloadURL, gridBagConstraints);
211

    
212
                add(northPanel, java.awt.BorderLayout.NORTH);
213
        }
214

    
215
        public void itemStateChanged(ItemEvent e) {
216
                selectFileText.setEnabled(fileRadioButton.isSelected());
217
                checkNextButtonEnabled();
218
        }
219

    
220
        protected File getSelectedFile() {
221
                return selectFileText.getSelectedFile();
222
        }
223

    
224
        protected URL getSelectedURL() throws MalformedURLException {
225
                if (downloadURL.getSelectedItem() instanceof URL) {
226
                        return (URL) downloadURL.getSelectedItem();
227
                }
228
                return new URL(downloadURL.getSelectedItem().toString());
229

    
230
        }
231

    
232
        protected boolean isStandardSelected() {
233
                return standardRadioButton.isSelected();
234
        }
235

    
236
        protected boolean isFileSelected() {
237
                return fileRadioButton.isSelected();
238
        }
239

    
240
        protected boolean isURLSelected() {
241
                return urlRadioButton.isSelected();
242
        }
243

    
244
        protected boolean isNextButtonEnabled() {
245
                if (isStandardSelected()) {
246
                        return true;
247
                }
248
                if (isFileSelected()) {
249
                        File file = selectFileText.getSelectedFile();
250
                        if (file == null) {
251
                                return false;
252
                        }
253
                        return file.exists();
254
                }
255
                if (isURLSelected()) {
256
                        try {
257
                                getSelectedURL();
258
                                return true;
259
                        } catch (MalformedURLException e) {
260
                                return false;
261
                        }
262
                }
263
                return false;
264
        }
265

    
266
        protected void checkNextButtonEnabled() {
267

    
268
        }
269

    
270
        public void changedUpdate(DocumentEvent e) {
271
                checkNextButtonEnabled();
272
        }
273

    
274
        public void insertUpdate(DocumentEvent e) {
275
                checkNextButtonEnabled();
276
        }
277

    
278
        public void removeUpdate(DocumentEvent e) {
279
                checkNextButtonEnabled();
280
        }
281

    
282
}