Statistics
| Revision:

svn-gvsig-desktop / 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 @ 37489

History | View | Annotate | Download (8.69 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 =
78
            (DefaultSwingInstallerManager) SwingInstallerLocator
79
                .getSwingInstallerManager();
80
        initComponents();
81
        initListeners();
82
        // Standard installation mode is selected by default
83
        standardRadioButton.setSelected(true);
84
    }
85

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

    
97
    private void initComponents() {
98
        java.awt.GridBagConstraints gridBagConstraints;
99

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

    
107
            private String packageExt =
108
                swingInstallerManager.getInstallerManager()
109
                    .getDefaultPackageFileExtension();
110
            private String packageSetExt =
111
                swingInstallerManager.getInstallerManager()
112
                    .getDefaultPackageSetFileExtension();
113

    
114
            @Override
115
            public String getDescription() {
116
                return "gvSIG package and package sets (*." + packageExt
117
                    + ", *." + packageSetExt + ")";
118
            }
119

    
120
            @Override
121
            public boolean accept(File file) {
122
                if (file.isFile()) {
123
                    String name = file.getName().toLowerCase();
124
                    return name.endsWith(packageExt)
125
                        || name.endsWith(packageSetExt);
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
        buttonGroup = new ButtonGroup();
144

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

    
149
        setLayout(new BorderLayout());
150

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

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

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

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

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

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

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

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

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

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

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

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

    
246
    protected void checkNextButtonEnabled() {
247

    
248
    }
249

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

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

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

    
262
}