Revision 43126 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

View differences:

SelectBundlesPanel.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 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.
6
 * 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 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.
11
 * 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 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.
16
 * 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.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
/*
25 24
 * AUTHORS (In addition to CIT):
26 25
 * 2010 {Prodevelop}   {Task}
27 26
 */
28

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

  
31 29
import java.awt.BorderLayout;
......
46 44
import javax.swing.JTextField;
47 45
import javax.swing.event.DocumentEvent;
48 46
import javax.swing.event.DocumentListener;
47
import org.apache.commons.lang3.StringUtils;
49 48

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

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

  
62
	/**
63
     * 
63
    /**
64
     *
64 65
     */
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;
66
    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;
75 77

  
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!=null && files.length >0){
87
			standardRadioButton.setSelected(true);
88
			urlRadioButton.setSelected(false);	
89
		} else {
90
			urlRadioButton.setSelected(true);	
91
			standardRadioButton.setEnabled(false);
92
		}
93
	}
78
    public SelectBundlesPanel(List<UrlAndLabel> defaultDownloadURL2, File installFolder) {
79
        super();
80
        this.defaultDownloadURL = defaultDownloadURL2;
81
        swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator
82
                .getSwingInstallerManager();
83
        initComponents();
84
        initListeners();
94 85

  
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
	}
86
        String[] files = installFolder.list();
104 87

  
105
	private void initComponents() {
106
		java.awt.GridBagConstraints gridBagConstraints;
88
        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
    }
107 96

  
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() {
97
    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
    }
114 106

  
115
			private String packageExt = swingInstallerManager
116
					.getInstallerManager().getDefaultPackageFileExtension();
117
			private String packageSetExt = swingInstallerManager
118
					.getInstallerManager().getDefaultPackageSetFileExtension();
119
			private String indexSetExt = swingInstallerManager
120
					.getInstallerManager().getDefaultIndexSetFileExtension();
107
    private void initComponents() {
108
        java.awt.GridBagConstraints gridBagConstraints;
121 109

  
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
			}
110
        northPanel = new JPanel();
111
        standardRadioButton = new JRadioButton();
112
        fileRadioButton = new JRadioButton();
113
        urlRadioButton = new JRadioButton();
114
        selectFileText = new FileTextField();
115
        selectFileText.setFileFilter(new FileFilter() {
130 116

  
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
			}
117
            private String packageExt = swingInstallerManager
118
                    .getInstallerManager().getDefaultPackageFileExtension();
119
            private String packageSetExt = swingInstallerManager
120
                    .getInstallerManager().getDefaultPackageSetFileExtension();
121
            private String indexSetExt = swingInstallerManager
122
                    .getInstallerManager().getDefaultIndexSetFileExtension();
141 123

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

  
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
		}
127
                return swingInstallerManager.getText(
128
                        "_gvSIG_packages_and_packages_and_index_sets")
129
                        + " (*." + packageExt + ", *." + packageSetExt
130
                        + ", *." + indexSetExt + ")";
131
            }
155 132

  
156
		buttonGroup = new ButtonGroup();
133
            @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
            }
157 143

  
158
		buttonGroup.add(standardRadioButton);
159
		buttonGroup.add(fileRadioButton);
160
		buttonGroup.add(urlRadioButton);
144
            @Override
145
            public String getDefaultExtension() {
146
                return packageSetExt;
147
            }
148
        });
161 149

  
162
		setLayout(new BorderLayout());
150
        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
        }
163 157

  
164
		northPanel.setLayout(new GridBagLayout());
158
        buttonGroup = new ButtonGroup();
165 159

  
166
		standardRadioButton.setText(swingInstallerManager
167
				.getText("_standard_installation") +
168
				" (" +
169
				swingInstallerManager.getText("_install_addons_in_gvsig_standard_dist") +
170
				")");
160
        buttonGroup.add(standardRadioButton);
161
        buttonGroup.add(fileRadioButton);
162
        buttonGroup.add(urlRadioButton);
171 163

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

  
177
		fileRadioButton.setText(swingInstallerManager
178
				.getText("_installation_from_file") +
179
		             " (" +
180
		             swingInstallerManager.getText("_install_addons_in_gvspki_or_gvspks_file") +
181
		             ")");
166
        northPanel.setLayout(new GridBagLayout());
182 167

  
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);
168
        standardRadioButton.setText(swingInstallerManager
169
                .getText("_standard_installation")
170
                + " ("
171
                + swingInstallerManager.getText("_install_addons_in_gvsig_standard_dist")
172
                + ")");
196 173

  
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);
174
        gridBagConstraints = new GridBagConstraints();
175
        gridBagConstraints.anchor = GridBagConstraints.WEST;
176
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
177
        northPanel.add(standardRadioButton, gridBagConstraints);
216 178

  
217
		add(northPanel, java.awt.BorderLayout.NORTH);
218
	}
179
        fileRadioButton.setText(swingInstallerManager
180
                .getText("_installation_from_file")
181
                + " ("
182
                + swingInstallerManager.getText("_install_addons_in_gvspki_or_gvspks_file")
183
                + ")");
219 184

  
220
	public void itemStateChanged(ItemEvent e) {
221
		selectFileText.setEnabled(fileRadioButton.isSelected());
222
		downloadURL.setEnabled(urlRadioButton.isSelected());
223
		downloadURL.setEditable(urlRadioButton.isSelected());
224
		checkNextButtonEnabled();
225
	}
185
        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);
226 198

  
227
	protected File getSelectedFile() {
228
		return selectFileText.getSelectedFile();
229
	}
199
        urlRadioButton.setText(swingInstallerManager
200
                .getText("_installation_from_url")
201
                + " ("
202
                + swingInstallerManager.getText("_install_addons_from_remote_repo")
203
                + ")");
230 204

  
231
	protected URL getSelectedURL() throws MalformedURLException {
232
		Object value = downloadURL.getSelectedItem();
233
		if( value instanceof UrlAndLabel) {
234
			// clicked fron the combo
235
			return ((UrlAndLabel)value).getURL();
236
		} else {
237
			// String entered in the text field
238
			return new URL(value.toString());
239
		}
205
        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);
240 218

  
241
	}
219
        add(northPanel, java.awt.BorderLayout.NORTH);
220
    }
242 221

  
243
	protected boolean isStandardSelected() {
244
		return standardRadioButton.isSelected();
245
	}
222
    public void itemStateChanged(ItemEvent e) {
223
        selectFileText.setEnabled(fileRadioButton.isSelected());
224
        downloadURL.setEnabled(urlRadioButton.isSelected());
225
        downloadURL.setEditable(urlRadioButton.isSelected());
226
        checkNextButtonEnabled();
227
    }
246 228

  
247
	protected boolean isFileSelected() {
248
		return fileRadioButton.isSelected();
249
	}
229
    public File getSelectedFile() {
230
        return selectFileText.getSelectedFile();
231
    }
250 232

  
251
	protected boolean isURLSelected() {
252
		return urlRadioButton.isSelected();
253
	}
233
    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 254

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

  
277
	protected void checkNextButtonEnabled() {
259
    public boolean isFileSelected() {
260
        return fileRadioButton.isSelected();
261
    }
278 262

  
279
	}
263
    public boolean isURLSelected() {
264
        return urlRadioButton.isSelected();
265
    }
280 266

  
281
	public void changedUpdate(DocumentEvent e) {
282
		checkNextButtonEnabled();
283
	}
267
    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
    }
284 283

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

  
289
	public void removeUpdate(DocumentEvent e) {
290
		checkNextButtonEnabled();
291
	}
286
    }
292 287

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

  
293 303
}

Also available in: Unified diff