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 @ 43126
History | View | Annotate | Download (10.3 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 43126 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40560 | jjdelcerro | *
|
11 | 43126 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40560 | jjdelcerro | *
|
16 | 43126 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40560 | jjdelcerro | *
|
20 | 43126 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2010 {Prodevelop} {Task}
|
||
26 | */
|
||
27 | package org.gvsig.installer.swing.impl.execution.panel; |
||
28 | |||
29 | import java.awt.BorderLayout; |
||
30 | import java.awt.GridBagConstraints; |
||
31 | import java.awt.GridBagLayout; |
||
32 | import java.awt.Insets; |
||
33 | import java.awt.event.ItemEvent; |
||
34 | import java.awt.event.ItemListener; |
||
35 | import java.io.File; |
||
36 | import java.net.MalformedURLException; |
||
37 | import java.net.URL; |
||
38 | import java.util.List; |
||
39 | |||
40 | import javax.swing.ButtonGroup; |
||
41 | import javax.swing.JComboBox; |
||
42 | import javax.swing.JPanel; |
||
43 | import javax.swing.JRadioButton; |
||
44 | import javax.swing.JTextField; |
||
45 | import javax.swing.event.DocumentEvent; |
||
46 | import javax.swing.event.DocumentListener; |
||
47 | 43126 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
48 | 40435 | jjdelcerro | |
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 | 40581 | jjdelcerro | import org.gvsig.installer.swing.api.SwingInstallerManager.UrlAndLabel; |
53 | 40435 | jjdelcerro | import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager; |
54 | 43126 | jjdelcerro | import org.slf4j.Logger; |
55 | import org.slf4j.LoggerFactory; |
||
56 | 40435 | jjdelcerro | |
57 | /**
|
||
58 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodrá</a>
|
||
59 | */
|
||
60 | public class SelectBundlesPanel extends JPanel implements ItemListener, |
||
61 | 43126 | jjdelcerro | DocumentListener {
|
62 | 40435 | jjdelcerro | |
63 | 43126 | jjdelcerro | /**
|
64 | *
|
||
65 | 40435 | jjdelcerro | */
|
66 | 43126 | jjdelcerro | protected static final Logger LOG = LoggerFactory.getLogger(SelectBundlesPanel.class); |
67 | private static final long serialVersionUID = -6580729307001414868L; |
||
68 | protected DefaultSwingInstallerManager swingInstallerManager = null; |
||
69 | private JRadioButton fileRadioButton; |
||
70 | private ButtonGroup buttonGroup; |
||
71 | private JPanel northPanel; |
||
72 | private FileTextField selectFileText;
|
||
73 | private JRadioButton standardRadioButton; |
||
74 | private JComboBox downloadURL; |
||
75 | private JRadioButton urlRadioButton; |
||
76 | private final List<UrlAndLabel> defaultDownloadURL; |
||
77 | 40435 | jjdelcerro | |
78 | 43126 | jjdelcerro | public SelectBundlesPanel(List<UrlAndLabel> defaultDownloadURL2, File installFolder) { |
79 | super();
|
||
80 | this.defaultDownloadURL = defaultDownloadURL2;
|
||
81 | swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator |
||
82 | .getSwingInstallerManager(); |
||
83 | initComponents(); |
||
84 | initListeners(); |
||
85 | 40435 | jjdelcerro | |
86 | 43126 | jjdelcerro | String[] files = installFolder.list(); |
87 | 40435 | jjdelcerro | |
88 | 43126 | jjdelcerro | if (files != null && files.length > 0) { |
89 | standardRadioButton.setSelected(true);
|
||
90 | urlRadioButton.setSelected(false);
|
||
91 | } else {
|
||
92 | urlRadioButton.setSelected(true);
|
||
93 | standardRadioButton.setEnabled(false);
|
||
94 | } |
||
95 | } |
||
96 | 40435 | jjdelcerro | |
97 | 43126 | jjdelcerro | private void initListeners() { |
98 | standardRadioButton.addItemListener(this);
|
||
99 | fileRadioButton.addItemListener(this);
|
||
100 | urlRadioButton.addItemListener(this);
|
||
101 | Object obj = selectFileText.getComponent(0); |
||
102 | if ((obj != null) && (obj instanceof JTextField)) { |
||
103 | ((JTextField) obj).getDocument().addDocumentListener(this); |
||
104 | } |
||
105 | } |
||
106 | 40435 | jjdelcerro | |
107 | 43126 | jjdelcerro | private void initComponents() { |
108 | java.awt.GridBagConstraints gridBagConstraints; |
||
109 | 40435 | jjdelcerro | |
110 | 43126 | jjdelcerro | northPanel = new JPanel(); |
111 | standardRadioButton = new JRadioButton(); |
||
112 | fileRadioButton = new JRadioButton(); |
||
113 | urlRadioButton = new JRadioButton(); |
||
114 | selectFileText = new FileTextField();
|
||
115 | selectFileText.setFileFilter(new FileFilter() { |
||
116 | 40435 | jjdelcerro | |
117 | 43126 | jjdelcerro | private String packageExt = swingInstallerManager |
118 | .getInstallerManager().getDefaultPackageFileExtension(); |
||
119 | private String packageSetExt = swingInstallerManager |
||
120 | .getInstallerManager().getDefaultPackageSetFileExtension(); |
||
121 | private String indexSetExt = swingInstallerManager |
||
122 | .getInstallerManager().getDefaultIndexSetFileExtension(); |
||
123 | 40435 | jjdelcerro | |
124 | 43126 | jjdelcerro | @Override
|
125 | public String getDescription() { |
||
126 | 40435 | jjdelcerro | |
127 | 43126 | jjdelcerro | return swingInstallerManager.getText(
|
128 | "_gvSIG_packages_and_packages_and_index_sets")
|
||
129 | + " (*." + packageExt + ", *." + packageSetExt |
||
130 | + ", *." + indexSetExt + ")"; |
||
131 | } |
||
132 | 40435 | jjdelcerro | |
133 | 43126 | jjdelcerro | @Override
|
134 | public boolean accept(File file) { |
||
135 | if (file.isFile()) {
|
||
136 | String name = file.getName().toLowerCase();
|
||
137 | return name.endsWith(packageExt)
|
||
138 | || name.endsWith(packageSetExt) |
||
139 | || name.endsWith(indexSetExt); |
||
140 | } |
||
141 | return true; |
||
142 | } |
||
143 | 40435 | jjdelcerro | |
144 | 43126 | jjdelcerro | @Override
|
145 | public String getDefaultExtension() { |
||
146 | return packageSetExt;
|
||
147 | } |
||
148 | }); |
||
149 | 40435 | jjdelcerro | |
150 | 43126 | jjdelcerro | downloadURL = new JComboBox(); |
151 | downloadURL.setEditable(true);
|
||
152 | if (defaultDownloadURL != null) { |
||
153 | for (int i = 0; i < defaultDownloadURL.size(); i++) { |
||
154 | downloadURL.addItem(defaultDownloadURL.get(i)); |
||
155 | } |
||
156 | } |
||
157 | 40435 | jjdelcerro | |
158 | 43126 | jjdelcerro | buttonGroup = new ButtonGroup(); |
159 | 40435 | jjdelcerro | |
160 | 43126 | jjdelcerro | buttonGroup.add(standardRadioButton); |
161 | buttonGroup.add(fileRadioButton); |
||
162 | buttonGroup.add(urlRadioButton); |
||
163 | 40435 | jjdelcerro | |
164 | 43126 | jjdelcerro | setLayout(new BorderLayout()); |
165 | 40435 | jjdelcerro | |
166 | 43126 | jjdelcerro | northPanel.setLayout(new GridBagLayout()); |
167 | 40435 | jjdelcerro | |
168 | 43126 | jjdelcerro | standardRadioButton.setText(swingInstallerManager |
169 | .getText("_standard_installation")
|
||
170 | + " ("
|
||
171 | + swingInstallerManager.getText("_install_addons_in_gvsig_standard_dist")
|
||
172 | + ")");
|
||
173 | 40435 | jjdelcerro | |
174 | 43126 | jjdelcerro | gridBagConstraints = new GridBagConstraints(); |
175 | gridBagConstraints.anchor = GridBagConstraints.WEST;
|
||
176 | gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
||
177 | northPanel.add(standardRadioButton, gridBagConstraints); |
||
178 | 40435 | jjdelcerro | |
179 | 43126 | jjdelcerro | fileRadioButton.setText(swingInstallerManager |
180 | .getText("_installation_from_file")
|
||
181 | + " ("
|
||
182 | + swingInstallerManager.getText("_install_addons_in_gvspki_or_gvspks_file")
|
||
183 | + ")");
|
||
184 | 40435 | jjdelcerro | |
185 | 43126 | jjdelcerro | gridBagConstraints = new GridBagConstraints(); |
186 | gridBagConstraints.gridx = 0;
|
||
187 | gridBagConstraints.gridy = 1;
|
||
188 | gridBagConstraints.anchor = GridBagConstraints.WEST;
|
||
189 | gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
||
190 | northPanel.add(fileRadioButton, gridBagConstraints); |
||
191 | gridBagConstraints = new GridBagConstraints(); |
||
192 | gridBagConstraints.gridx = 0;
|
||
193 | gridBagConstraints.gridy = 2;
|
||
194 | gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
||
195 | gridBagConstraints.weightx = 1.0;
|
||
196 | gridBagConstraints.insets = new Insets(2, 20, 2, 2); |
||
197 | northPanel.add(selectFileText, gridBagConstraints); |
||
198 | 40435 | jjdelcerro | |
199 | 43126 | jjdelcerro | urlRadioButton.setText(swingInstallerManager |
200 | .getText("_installation_from_url")
|
||
201 | + " ("
|
||
202 | + swingInstallerManager.getText("_install_addons_from_remote_repo")
|
||
203 | + ")");
|
||
204 | 40435 | jjdelcerro | |
205 | 43126 | jjdelcerro | gridBagConstraints = new GridBagConstraints(); |
206 | gridBagConstraints.gridx = 0;
|
||
207 | gridBagConstraints.gridy = 3;
|
||
208 | gridBagConstraints.anchor = GridBagConstraints.WEST;
|
||
209 | gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
||
210 | northPanel.add(urlRadioButton, gridBagConstraints); |
||
211 | gridBagConstraints = new GridBagConstraints(); |
||
212 | gridBagConstraints.gridx = 0;
|
||
213 | gridBagConstraints.gridy = 4;
|
||
214 | gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
||
215 | gridBagConstraints.weightx = 1.0;
|
||
216 | gridBagConstraints.insets = new Insets(2, 20, 2, 2); |
||
217 | northPanel.add(downloadURL, gridBagConstraints); |
||
218 | 40435 | jjdelcerro | |
219 | 43126 | jjdelcerro | add(northPanel, java.awt.BorderLayout.NORTH); |
220 | } |
||
221 | 40435 | jjdelcerro | |
222 | 43126 | jjdelcerro | public void itemStateChanged(ItemEvent e) { |
223 | selectFileText.setEnabled(fileRadioButton.isSelected()); |
||
224 | downloadURL.setEnabled(urlRadioButton.isSelected()); |
||
225 | downloadURL.setEditable(urlRadioButton.isSelected()); |
||
226 | checkNextButtonEnabled(); |
||
227 | } |
||
228 | 40435 | jjdelcerro | |
229 | 43126 | jjdelcerro | public File getSelectedFile() { |
230 | return selectFileText.getSelectedFile();
|
||
231 | } |
||
232 | 40435 | jjdelcerro | |
233 | 43126 | jjdelcerro | public URL getSelectedURL() { |
234 | |||
235 | Object value = downloadURL.getSelectedItem();
|
||
236 | if (value instanceof UrlAndLabel) { |
||
237 | // clicked fron the combo
|
||
238 | return ((UrlAndLabel) value).getURL();
|
||
239 | } |
||
240 | // String entered in the text field
|
||
241 | if( value == null ) { |
||
242 | return null; |
||
243 | } |
||
244 | if( StringUtils.isEmpty(value.toString()) ) {
|
||
245 | return null; |
||
246 | } |
||
247 | try {
|
||
248 | return new URL(value.toString()); |
||
249 | } catch(Exception ex) { |
||
250 | LOG.warn("The entered url are not valid");
|
||
251 | return null; |
||
252 | } |
||
253 | } |
||
254 | 40435 | jjdelcerro | |
255 | 43126 | jjdelcerro | public boolean isStandardSelected() { |
256 | return standardRadioButton.isSelected();
|
||
257 | } |
||
258 | 40435 | jjdelcerro | |
259 | 43126 | jjdelcerro | public boolean isFileSelected() { |
260 | return fileRadioButton.isSelected();
|
||
261 | } |
||
262 | 40435 | jjdelcerro | |
263 | 43126 | jjdelcerro | public boolean isURLSelected() { |
264 | return urlRadioButton.isSelected();
|
||
265 | } |
||
266 | 40435 | jjdelcerro | |
267 | 43126 | jjdelcerro | protected boolean isNextButtonEnabled() { |
268 | if (isStandardSelected()) {
|
||
269 | return true; |
||
270 | } |
||
271 | if (isFileSelected()) {
|
||
272 | File file = selectFileText.getSelectedFile();
|
||
273 | if (file == null) { |
||
274 | return false; |
||
275 | } |
||
276 | return file.exists();
|
||
277 | } |
||
278 | if (isURLSelected()) {
|
||
279 | return getSelectedURL() != null; |
||
280 | } |
||
281 | return false; |
||
282 | } |
||
283 | 40435 | jjdelcerro | |
284 | 43126 | jjdelcerro | protected void checkNextButtonEnabled() { |
285 | 40435 | jjdelcerro | |
286 | 43126 | jjdelcerro | } |
287 | 40435 | jjdelcerro | |
288 | 43126 | jjdelcerro | @Override
|
289 | public void changedUpdate(DocumentEvent e) { |
||
290 | checkNextButtonEnabled(); |
||
291 | } |
||
292 | |||
293 | @Override
|
||
294 | public void insertUpdate(DocumentEvent e) { |
||
295 | checkNextButtonEnabled(); |
||
296 | } |
||
297 | |||
298 | @Override
|
||
299 | public void removeUpdate(DocumentEvent e) { |
||
300 | checkNextButtonEnabled(); |
||
301 | } |
||
302 | |||
303 | 40435 | jjdelcerro | } |