Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.swing / org.gvsig.installer.swing.impl / src / main / java / org / gvsig / installer / swing / impl / execution / panel / SelectBundlesPanel.java @ 41022

History | View | Annotate | Download (8.59 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {Prodevelop}   {Task}
27
 */
28

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

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

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

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

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

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

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

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

    
105
        private void initComponents() {
106
                java.awt.GridBagConstraints gridBagConstraints;
107

    
108
                northPanel = new JPanel();
109
                standardRadioButton = new JRadioButton();
110
                fileRadioButton = new JRadioButton();
111
                urlRadioButton = new JRadioButton();
112
                selectFileText = new FileTextField();
113
                selectFileText.setFileFilter(new FileFilter() {
114

    
115
                        private String packageExt = swingInstallerManager
116
                                        .getInstallerManager().getDefaultPackageFileExtension();
117
                        private String packageSetExt = swingInstallerManager
118
                                        .getInstallerManager().getDefaultPackageSetFileExtension();
119
                        private String indexSetExt = swingInstallerManager
120
                                        .getInstallerManager().getDefaultIndexSetFileExtension();
121

    
122
                        @Override
123
                        public String getDescription() {
124
                            
125
                                return swingInstallerManager.getText(
126
                                    "_gvSIG_packages_and_packages_and_index_sets")
127
                                    + " (*." + packageExt + ", *." + packageSetExt
128
                                    + ", *." + indexSetExt + ")";
129
                        }
130

    
131
                        @Override
132
                        public boolean accept(File file) {
133
                                if (file.isFile()) {
134
                                        String name = file.getName().toLowerCase();
135
                                        return name.endsWith(packageExt)
136
                                                        || name.endsWith(packageSetExt)
137
                                                        || name.endsWith(indexSetExt);
138
                                }
139
                                return true;
140
                        }
141

    
142
                        @Override
143
                        public String getDefaultExtension() {
144
                                return packageSetExt;
145
                        }
146
                });
147

    
148
                downloadURL = new JComboBox();
149
                downloadURL.setEditable(true);
150
                if (defaultDownloadURL != null) {
151
                        for (int i = 0; i < defaultDownloadURL.size(); i++) {
152
                                downloadURL.addItem(defaultDownloadURL.get(i));
153
                        }
154
                }
155

    
156
                buttonGroup = new ButtonGroup();
157

    
158
                buttonGroup.add(standardRadioButton);
159
                buttonGroup.add(fileRadioButton);
160
                buttonGroup.add(urlRadioButton);
161

    
162
                setLayout(new BorderLayout());
163

    
164
                northPanel.setLayout(new GridBagLayout());
165

    
166
                standardRadioButton.setText(swingInstallerManager
167
                                .getText("_standard_installation") +
168
                                " (" +
169
                                swingInstallerManager.getText("_install_addons_in_gvsig_standard_dist") +
170
                                ")");
171

    
172
                gridBagConstraints = new GridBagConstraints();
173
                gridBagConstraints.anchor = GridBagConstraints.WEST;
174
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
175
                northPanel.add(standardRadioButton, gridBagConstraints);
176

    
177
                fileRadioButton.setText(swingInstallerManager
178
                                .getText("_installation_from_file") +
179
                             " (" +
180
                             swingInstallerManager.getText("_install_addons_in_gvspki_or_gvspks_file") +
181
                             ")");
182

    
183
                gridBagConstraints = new GridBagConstraints();
184
                gridBagConstraints.gridx = 0;
185
                gridBagConstraints.gridy = 1;
186
                gridBagConstraints.anchor = GridBagConstraints.WEST;
187
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
188
                northPanel.add(fileRadioButton, gridBagConstraints);
189
                gridBagConstraints = new GridBagConstraints();
190
                gridBagConstraints.gridx = 0;
191
                gridBagConstraints.gridy = 2;
192
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
193
                gridBagConstraints.weightx = 1.0;
194
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
195
                northPanel.add(selectFileText, gridBagConstraints);
196

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

    
217
                add(northPanel, java.awt.BorderLayout.NORTH);
218
        }
219

    
220
        public void itemStateChanged(ItemEvent e) {
221
                selectFileText.setEnabled(fileRadioButton.isSelected());
222
                downloadURL.setEditable(urlRadioButton.isEnabled());
223
                checkNextButtonEnabled();
224
        }
225

    
226
        protected File getSelectedFile() {
227
                return selectFileText.getSelectedFile();
228
        }
229

    
230
        protected URL getSelectedURL() throws MalformedURLException {
231
                Object value = downloadURL.getSelectedItem();
232
                if( value instanceof UrlAndLabel) {
233
                        // clicked fron the combo
234
                        return ((UrlAndLabel)value).getURL();
235
                } else {
236
                        // String entered in the text field
237
                        return new URL(value.toString());
238
                }
239

    
240
        }
241

    
242
        protected boolean isStandardSelected() {
243
                return standardRadioButton.isSelected();
244
        }
245

    
246
        protected boolean isFileSelected() {
247
                return fileRadioButton.isSelected();
248
        }
249

    
250
        protected boolean isURLSelected() {
251
                return urlRadioButton.isSelected();
252
        }
253

    
254
        protected boolean isNextButtonEnabled() {
255
                if (isStandardSelected()) {
256
                        return true;
257
                }
258
                if (isFileSelected()) {
259
                        File file = selectFileText.getSelectedFile();
260
                        if (file == null) {
261
                                return false;
262
                        }
263
                        return file.exists();
264
                }
265
                if (isURLSelected()) {
266
                        try {
267
                                getSelectedURL();
268
                                return true;
269
                        } catch (MalformedURLException e) {
270
                                return false;
271
                        }
272
                }
273
                return false;
274
        }
275

    
276
        protected void checkNextButtonEnabled() {
277

    
278
        }
279

    
280
        public void changedUpdate(DocumentEvent e) {
281
                checkNextButtonEnabled();
282
        }
283

    
284
        public void insertUpdate(DocumentEvent e) {
285
                checkNextButtonEnabled();
286
        }
287

    
288
        public void removeUpdate(DocumentEvent e) {
289
                checkNextButtonEnabled();
290
        }
291

    
292
}