Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / extensions / org.gvsig.installer / org.gvsig.installer / org.gvsig.installer.swing / org.gvsig.installer.swing.impl / src / main / java / org / gvsig / installer / swing / impl / execution / panel / SelectBundlesPanel.java @ 38358

History | View | Annotate | Download (7.88 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
                gridBagConstraints = new GridBagConstraints();
164
                gridBagConstraints.anchor = GridBagConstraints.WEST;
165
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
166
                northPanel.add(standardRadioButton, gridBagConstraints);
167

    
168
                fileRadioButton.setText(swingInstallerManager
169
                                .getText("_installation_from_file"));
170
                gridBagConstraints = new GridBagConstraints();
171
                gridBagConstraints.gridx = 0;
172
                gridBagConstraints.gridy = 1;
173
                gridBagConstraints.anchor = GridBagConstraints.WEST;
174
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
175
                northPanel.add(fileRadioButton, gridBagConstraints);
176
                gridBagConstraints = new GridBagConstraints();
177
                gridBagConstraints.gridx = 0;
178
                gridBagConstraints.gridy = 2;
179
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
180
                gridBagConstraints.weightx = 1.0;
181
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
182
                northPanel.add(selectFileText, gridBagConstraints);
183

    
184
                urlRadioButton.setText(swingInstallerManager
185
                                .getText("_installation_from_url"));
186
                gridBagConstraints = new GridBagConstraints();
187
                gridBagConstraints.gridx = 0;
188
                gridBagConstraints.gridy = 3;
189
                gridBagConstraints.anchor = GridBagConstraints.WEST;
190
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
191
                northPanel.add(urlRadioButton, gridBagConstraints);
192
                gridBagConstraints = new GridBagConstraints();
193
                gridBagConstraints.gridx = 0;
194
                gridBagConstraints.gridy = 4;
195
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
196
                gridBagConstraints.weightx = 1.0;
197
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
198
                northPanel.add(downloadURL, gridBagConstraints);
199

    
200
                add(northPanel, java.awt.BorderLayout.NORTH);
201
        }
202

    
203
        public void itemStateChanged(ItemEvent e) {
204
                selectFileText.setEnabled(fileRadioButton.isSelected());
205
                checkNextButtonEnabled();
206
        }
207

    
208
        protected File getSelectedFile() {
209
                return selectFileText.getSelectedFile();
210
        }
211

    
212
        protected URL getSelectedURL() throws MalformedURLException {
213
                if (downloadURL.getSelectedItem() instanceof URL) {
214
                        return (URL) downloadURL.getSelectedItem();
215
                }
216
                return new URL(downloadURL.getSelectedItem().toString());
217

    
218
        }
219

    
220
        protected boolean isStandardSelected() {
221
                return standardRadioButton.isSelected();
222
        }
223

    
224
        protected boolean isFileSelected() {
225
                return fileRadioButton.isSelected();
226
        }
227

    
228
        protected boolean isURLSelected() {
229
                return urlRadioButton.isSelected();
230
        }
231

    
232
        protected boolean isNextButtonEnabled() {
233
                if (isStandardSelected()) {
234
                        return true;
235
                }
236
                if (isFileSelected()) {
237
                        File file = selectFileText.getSelectedFile();
238
                        if (file == null) {
239
                                return false;
240
                        }
241
                        return file.exists();
242
                }
243
                if (isURLSelected()) {
244
                        try {
245
                                getSelectedURL();
246
                                return true;
247
                        } catch (MalformedURLException e) {
248
                                return false;
249
                        }
250
                }
251
                return false;
252
        }
253

    
254
        protected void checkNextButtonEnabled() {
255

    
256
        }
257

    
258
        public void changedUpdate(DocumentEvent e) {
259
                checkNextButtonEnabled();
260
        }
261

    
262
        public void insertUpdate(DocumentEvent e) {
263
                checkNextButtonEnabled();
264
        }
265

    
266
        public void removeUpdate(DocumentEvent e) {
267
                checkNextButtonEnabled();
268
        }
269

    
270
}