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 @ 37610

History | View | Annotate | Download (7.76 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) {
75
                super();
76
                this.defaultDownloadURL = defaultDownloadURL;
77
                swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator
78
                                .getSwingInstallerManager();
79
                initComponents();
80
                initListeners();
81
                // Standard installation mode is selected by default
82
                standardRadioButton.setSelected(true);
83
        }
84

    
85
        private void initListeners() {
86
                standardRadioButton.addItemListener(this);
87
                fileRadioButton.addItemListener(this);
88
                urlRadioButton.addItemListener(this);
89
                Object obj = selectFileText.getComponent(0);
90
                if ((obj != null) && (obj instanceof JTextField)) {
91
                        ((JTextField) obj).getDocument().addDocumentListener(this);
92
                }
93
        }
94

    
95
        private void initComponents() {
96
                java.awt.GridBagConstraints gridBagConstraints;
97

    
98
                northPanel = new JPanel();
99
                standardRadioButton = new JRadioButton();
100
                fileRadioButton = new JRadioButton();
101
                urlRadioButton = new JRadioButton();
102
                selectFileText = new FileTextField();
103
                selectFileText.setFileFilter(new FileFilter() {
104

    
105
                        private String packageExt = swingInstallerManager
106
                                        .getInstallerManager().getDefaultPackageFileExtension();
107
                        private String packageSetExt = swingInstallerManager
108
                                        .getInstallerManager().getDefaultPackageSetFileExtension();
109
                        private String indexSetExt = swingInstallerManager
110
                                        .getInstallerManager().getDefaultIndexSetFileExtension();
111

    
112
                        @Override
113
                        public String getDescription() {
114
                                return "_gvSIG_packages_and_packages_and_index_sets" + " (*."
115
                                                + packageExt + ", *." + packageSetExt + ", *."
116
                                                + indexSetExt + ")";
117
                        }
118

    
119
                        @Override
120
                        public boolean accept(File file) {
121
                                if (file.isFile()) {
122
                                        String name = file.getName().toLowerCase();
123
                                        return name.endsWith(packageExt)
124
                                                        || name.endsWith(packageSetExt)
125
                                                        || name.endsWith(indexSetExt);
126
                                }
127
                                return true;
128
                        }
129

    
130
                        @Override
131
                        public String getDefaultExtension() {
132
                                return packageSetExt;
133
                        }
134
                });
135

    
136
                downloadURL = new JComboBox();
137
                downloadURL.setEditable(true);
138
                if (defaultDownloadURL != null) {
139
                        for (int i = 0; i < defaultDownloadURL.size(); i++) {
140
                                downloadURL.addItem(defaultDownloadURL.get(i));
141
                        }
142
                }
143

    
144
                buttonGroup = new ButtonGroup();
145

    
146
                buttonGroup.add(standardRadioButton);
147
                buttonGroup.add(fileRadioButton);
148
                buttonGroup.add(urlRadioButton);
149

    
150
                setLayout(new BorderLayout());
151

    
152
                northPanel.setLayout(new GridBagLayout());
153

    
154
                standardRadioButton.setText(swingInstallerManager
155
                                .getText("_standard_installation"));
156
                gridBagConstraints = new GridBagConstraints();
157
                gridBagConstraints.anchor = GridBagConstraints.WEST;
158
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
159
                northPanel.add(standardRadioButton, gridBagConstraints);
160

    
161
                fileRadioButton.setText(swingInstallerManager
162
                                .getText("_installation_from_file"));
163
                gridBagConstraints = new GridBagConstraints();
164
                gridBagConstraints.gridx = 0;
165
                gridBagConstraints.gridy = 1;
166
                gridBagConstraints.anchor = GridBagConstraints.WEST;
167
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
168
                northPanel.add(fileRadioButton, gridBagConstraints);
169
                gridBagConstraints = new GridBagConstraints();
170
                gridBagConstraints.gridx = 0;
171
                gridBagConstraints.gridy = 2;
172
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
173
                gridBagConstraints.weightx = 1.0;
174
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
175
                northPanel.add(selectFileText, gridBagConstraints);
176

    
177
                urlRadioButton.setText(swingInstallerManager
178
                                .getText("_installation_from_url"));
179
                gridBagConstraints = new GridBagConstraints();
180
                gridBagConstraints.gridx = 0;
181
                gridBagConstraints.gridy = 3;
182
                gridBagConstraints.anchor = GridBagConstraints.WEST;
183
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
184
                northPanel.add(urlRadioButton, gridBagConstraints);
185
                gridBagConstraints = new GridBagConstraints();
186
                gridBagConstraints.gridx = 0;
187
                gridBagConstraints.gridy = 4;
188
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
189
                gridBagConstraints.weightx = 1.0;
190
                gridBagConstraints.insets = new Insets(2, 20, 2, 2);
191
                northPanel.add(downloadURL, gridBagConstraints);
192

    
193
                add(northPanel, java.awt.BorderLayout.NORTH);
194
        }
195

    
196
        public void itemStateChanged(ItemEvent e) {
197
                selectFileText.setEnabled(fileRadioButton.isSelected());
198
                checkNextButtonEnabled();
199
        }
200

    
201
        protected File getSelectedFile() {
202
                return selectFileText.getSelectedFile();
203
        }
204

    
205
        protected URL getSelectedURL() throws MalformedURLException {
206
                if (downloadURL.getSelectedItem() instanceof URL) {
207
                        return (URL) downloadURL.getSelectedItem();
208
                }
209
                return new URL(downloadURL.getSelectedItem().toString());
210

    
211
        }
212

    
213
        protected boolean isStandardSelected() {
214
                return standardRadioButton.isSelected();
215
        }
216

    
217
        protected boolean isFileSelected() {
218
                return fileRadioButton.isSelected();
219
        }
220

    
221
        protected boolean isURLSelected() {
222
                return urlRadioButton.isSelected();
223
        }
224

    
225
        protected boolean isNextButtonEnabled() {
226
                if (isStandardSelected()) {
227
                        return true;
228
                }
229
                if (isFileSelected()) {
230
                        File file = selectFileText.getSelectedFile();
231
                        if (file == null) {
232
                                return false;
233
                        }
234
                        return file.exists();
235
                }
236
                if (isURLSelected()) {
237
                        try {
238
                                getSelectedURL();
239
                                return true;
240
                        } catch (MalformedURLException e) {
241
                                return false;
242
                        }
243
                }
244
                return false;
245
        }
246

    
247
        protected void checkNextButtonEnabled() {
248

    
249
        }
250

    
251
        public void changedUpdate(DocumentEvent e) {
252
                checkNextButtonEnabled();
253
        }
254

    
255
        public void insertUpdate(DocumentEvent e) {
256
                checkNextButtonEnabled();
257
        }
258

    
259
        public void removeUpdate(DocumentEvent e) {
260
                checkNextButtonEnabled();
261
        }
262

    
263
}