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

History | View | Annotate | Download (10.3 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 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
 *
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
 *
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.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
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
import org.apache.commons.lang3.StringUtils;
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.api.SwingInstallerManager.UrlAndLabel;
53
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

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

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

    
78
    public SelectBundlesPanel(List<UrlAndLabel> defaultDownloadURL2, File installFolder) {
79
        super();
80
        this.defaultDownloadURL = defaultDownloadURL2;
81
        swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator
82
                .getSwingInstallerManager();
83
        initComponents();
84
        initListeners();
85

    
86
        String[] files = installFolder.list();
87

    
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
    }
96

    
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
    }
106

    
107
    private void initComponents() {
108
        java.awt.GridBagConstraints gridBagConstraints;
109

    
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() {
116

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

    
124
            @Override
125
            public String getDescription() {
126

    
127
                return swingInstallerManager.getText(
128
                        "_gvSIG_packages_and_packages_and_index_sets")
129
                        + " (*." + packageExt + ", *." + packageSetExt
130
                        + ", *." + indexSetExt + ")";
131
            }
132

    
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
            }
143

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

    
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
        }
157

    
158
        buttonGroup = new ButtonGroup();
159

    
160
        buttonGroup.add(standardRadioButton);
161
        buttonGroup.add(fileRadioButton);
162
        buttonGroup.add(urlRadioButton);
163

    
164
        setLayout(new BorderLayout());
165

    
166
        northPanel.setLayout(new GridBagLayout());
167

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

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

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

    
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);
198

    
199
        urlRadioButton.setText(swingInstallerManager
200
                .getText("_installation_from_url")
201
                + " ("
202
                + swingInstallerManager.getText("_install_addons_from_remote_repo")
203
                + ")");
204

    
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);
218

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

    
222
    public void itemStateChanged(ItemEvent e) {
223
        selectFileText.setEnabled(fileRadioButton.isSelected());
224
        downloadURL.setEnabled(urlRadioButton.isSelected());
225
        downloadURL.setEditable(urlRadioButton.isSelected());
226
        checkNextButtonEnabled();
227
    }
228

    
229
    public File getSelectedFile() {
230
        return selectFileText.getSelectedFile();
231
    }
232

    
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

    
255
    public boolean isStandardSelected() {
256
        return standardRadioButton.isSelected();
257
    }
258

    
259
    public boolean isFileSelected() {
260
        return fileRadioButton.isSelected();
261
    }
262

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

    
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
    }
283

    
284
    protected void checkNextButtonEnabled() {
285

    
286
    }
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

    
303
}