Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2021 / 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 @ 34107

History | View | Annotate | Download (8.27 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

    
40
import javax.swing.ButtonGroup;
41
import javax.swing.JPanel;
42
import javax.swing.JRadioButton;
43
import javax.swing.JTextField;
44
import javax.swing.event.DocumentEvent;
45
import javax.swing.event.DocumentListener;
46

    
47
import org.gvsig.gui.beans.openfile.FileFilter;
48
import org.gvsig.gui.beans.openfile.FileTextField;
49
import org.gvsig.installer.swing.api.SwingInstallerLocator;
50
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager;
51

    
52
/**
53
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
54
 */
55
public class SelectBundlesPanel extends JPanel implements ItemListener,
56
    DocumentListener {
57

    
58
    /**
59
     * 
60
     */
61
    private static final long serialVersionUID = -6580729307001414868L;
62
    protected DefaultSwingInstallerManager swingInstallerManager = null;
63
    private JRadioButton fileRadioButton;
64
    private ButtonGroup buttonGroup;
65
    private JPanel northPanel;
66
    private FileTextField selectFileText;
67
    private JRadioButton standardRadioButton;
68
    private JTextField urlText;
69
    private JRadioButton urlRadioButton;
70
    private final URL defaultDownloadURL;
71

    
72
    public SelectBundlesPanel(URL defaultDownloadURL) {
73
        super();
74
        this.defaultDownloadURL = defaultDownloadURL;
75
        swingInstallerManager =
76
            (DefaultSwingInstallerManager) SwingInstallerLocator
77
                .getSwingInstallerManager();
78
        initComponents();
79
        initListeners();
80
        standardRadioButton.setSelected(true);
81
    }
82

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

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

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

    
104
            private String packageExt = swingInstallerManager
105
                .getInstallerManager().getDefaultPackageFileExtension();
106
            private String packageSetExt = swingInstallerManager
107
                .getInstallerManager().getDefaultPackageSetFileExtension();
108

    
109
            @Override
110
            public String getDescription() {
111
                return "gvSIG package and package sets (*." + packageExt
112
                    + ", *." + packageSetExt + ")";
113
            }
114

    
115
            @Override
116
            public boolean accept(File file) {
117
                if (file.isFile()) {
118
                    String name = file.getName().toLowerCase();
119
                    return name.endsWith(packageExt)
120
                        || name.endsWith(packageSetExt);
121
                }
122
                return true;
123
            }
124

    
125
            @Override
126
            public String getDefaultExtension() {
127
                return packageSetExt;
128
            }
129
        });
130

    
131
        urlText = new JTextField();
132
        if (defaultDownloadURL != null) {
133
            urlText.setText(defaultDownloadURL.toString());
134
        }
135
        buttonGroup = new ButtonGroup();
136

    
137
        buttonGroup.add(standardRadioButton);
138
        buttonGroup.add(fileRadioButton);
139
        buttonGroup.add(urlRadioButton);
140

    
141
        setLayout(new BorderLayout());
142

    
143
        northPanel.setLayout(new GridBagLayout());
144

    
145
        standardRadioButton.setText(swingInstallerManager
146
            .getText("standard_installation"));
147
        gridBagConstraints = new GridBagConstraints();
148
        gridBagConstraints.anchor = GridBagConstraints.WEST;
149
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
150
        northPanel.add(standardRadioButton, gridBagConstraints);
151

    
152
        fileRadioButton.setText(swingInstallerManager
153
            .getText("installation_from_file"));
154
        gridBagConstraints = new GridBagConstraints();
155
        gridBagConstraints.gridx = 0;
156
        gridBagConstraints.gridy = 1;
157
        gridBagConstraints.anchor = GridBagConstraints.WEST;
158
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
159
        northPanel.add(fileRadioButton, gridBagConstraints);
160
        gridBagConstraints = new GridBagConstraints();
161
        gridBagConstraints.gridx = 0;
162
        gridBagConstraints.gridy = 2;
163
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
164
        gridBagConstraints.weightx = 1.0;
165
        gridBagConstraints.insets = new Insets(2, 20, 2, 2);
166
        northPanel.add(selectFileText, gridBagConstraints);
167

    
168
        urlRadioButton.setText(swingInstallerManager
169
            .getText("installation_from_url"));
170
        gridBagConstraints = new GridBagConstraints();
171
        gridBagConstraints.gridx = 0;
172
        gridBagConstraints.gridy = 3;
173
        gridBagConstraints.anchor = GridBagConstraints.WEST;
174
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
175
        northPanel.add(urlRadioButton, gridBagConstraints);
176
        gridBagConstraints = new GridBagConstraints();
177
        gridBagConstraints.gridx = 0;
178
        gridBagConstraints.gridy = 4;
179
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
180
        gridBagConstraints.weightx = 1.0;
181
        gridBagConstraints.insets = new Insets(2, 20, 2, 2);
182
        northPanel.add(urlText, gridBagConstraints);
183

    
184
        add(northPanel, java.awt.BorderLayout.NORTH);
185
    }
186

    
187
    public void itemStateChanged(ItemEvent e) {
188
        selectFileText.setEnabled(fileRadioButton.isSelected());
189
        checkNextButtonEnabled();
190
    }
191

    
192
    protected File getSelectedFile() {
193
        return selectFileText.getSelectedFile();
194
    }
195

    
196
    protected URL getSelectedURL() throws MalformedURLException {
197
        return new URL(urlText.getText());
198
    }
199

    
200
    protected boolean isStandardSelected() {
201
        return standardRadioButton.isSelected();
202
    }
203

    
204
    protected boolean isFileSelected() {
205
        return fileRadioButton.isSelected();
206
    }
207

    
208
    protected boolean isURLSelected() {
209
        return urlRadioButton.isSelected();
210
    }
211

    
212
    protected boolean isNextButtonEnabled() {
213
        if (isStandardSelected()) {
214
            return true;
215
        }
216
        if (isFileSelected()) {
217
            File file = selectFileText.getSelectedFile();
218
            if (file == null) {
219
                return false;
220
            }
221
            return file.exists();
222
        }
223
        if (isURLSelected()) {
224
            try {
225
                getSelectedURL();
226
                return true;
227
            } catch (MalformedURLException e) {
228
                return false;
229
            }
230
        }
231
        return false;
232
    }
233

    
234
    protected void checkNextButtonEnabled() {
235

    
236
    }
237

    
238
    public void changedUpdate(DocumentEvent e) {
239
        checkNextButtonEnabled();
240
    }
241

    
242
    public void insertUpdate(DocumentEvent e) {
243
        checkNextButtonEnabled();
244
    }
245

    
246
    public void removeUpdate(DocumentEvent e) {
247
        checkNextButtonEnabled();
248
    }
249

    
250
}