Statistics
| Revision:

root / 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 @ 34003

History | View | Annotate | Download (7.22 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.FileTextField;
48
import org.gvsig.installer.swing.api.SwingInstallerLocator;
49
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager;
50

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

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

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

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

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

    
96
        northPanel = new JPanel();
97
        standardRadioButton = new JRadioButton();
98
        fileRadioButton = new JRadioButton();
99
        urlRadioButton = new JRadioButton();
100
        selectFileText = new FileTextField();
101
        urlText = new JTextField();
102
        if (defaultDownloadURL != null) {
103
            urlText.setText(defaultDownloadURL.toString());
104
        }
105
        buttonGroup = new ButtonGroup();
106

    
107
        buttonGroup.add(standardRadioButton);
108
        buttonGroup.add(fileRadioButton);
109
        buttonGroup.add(urlRadioButton);
110

    
111
        setLayout(new BorderLayout());
112

    
113
        northPanel.setLayout(new GridBagLayout());
114

    
115
        standardRadioButton.setText(swingInstallerManager
116
            .getText("standard_installation"));
117
        gridBagConstraints = new GridBagConstraints();
118
        gridBagConstraints.anchor = GridBagConstraints.WEST;
119
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
120
        northPanel.add(standardRadioButton, gridBagConstraints);
121

    
122
        fileRadioButton.setText(swingInstallerManager
123
            .getText("installation_from_file"));
124
        gridBagConstraints = new GridBagConstraints();
125
        gridBagConstraints.gridx = 0;
126
        gridBagConstraints.gridy = 1;
127
        gridBagConstraints.anchor = GridBagConstraints.WEST;
128
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
129
        northPanel.add(fileRadioButton, gridBagConstraints);
130
        gridBagConstraints = new GridBagConstraints();
131
        gridBagConstraints.gridx = 0;
132
        gridBagConstraints.gridy = 2;
133
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
134
        gridBagConstraints.weightx = 1.0;
135
        gridBagConstraints.insets = new Insets(2, 20, 2, 2);
136
        northPanel.add(selectFileText, gridBagConstraints);
137

    
138
        urlRadioButton.setText(swingInstallerManager
139
            .getText("installation_from_url"));
140
        gridBagConstraints = new GridBagConstraints();
141
        gridBagConstraints.gridx = 0;
142
        gridBagConstraints.gridy = 3;
143
        gridBagConstraints.anchor = GridBagConstraints.WEST;
144
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
145
        northPanel.add(urlRadioButton, gridBagConstraints);
146
        gridBagConstraints = new GridBagConstraints();
147
        gridBagConstraints.gridx = 0;
148
        gridBagConstraints.gridy = 4;
149
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
150
        gridBagConstraints.weightx = 1.0;
151
        gridBagConstraints.insets = new Insets(2, 20, 2, 2);
152
        northPanel.add(urlText, gridBagConstraints);
153

    
154
        add(northPanel, java.awt.BorderLayout.NORTH);
155
    }
156

    
157
    public void itemStateChanged(ItemEvent e) {
158
        selectFileText.setEnabled(fileRadioButton.isSelected());
159
        checkNextButtonEnabled();
160
    }
161

    
162
    protected File getSelectedFile() {
163
        return selectFileText.getSelectedFile();
164
    }
165

    
166
    protected URL getSelectedURL() throws MalformedURLException {
167
        return new URL(urlText.getText());
168
    }
169

    
170
    protected boolean isStandardSelected() {
171
        return standardRadioButton.isSelected();
172
    }
173

    
174
    protected boolean isFileSelected() {
175
        return fileRadioButton.isSelected();
176
    }
177

    
178
    protected boolean isURLSelected() {
179
        return urlRadioButton.isSelected();
180
    }
181

    
182
    protected boolean isNextButtonEnabled() {
183
        if (isStandardSelected()) {
184
            return true;
185
        }
186
        if (isFileSelected()) {
187
            File file = selectFileText.getSelectedFile();
188
            if (file == null) {
189
                return false;
190
            }
191
            return file.exists();
192
        }
193
        if (isURLSelected()) {
194
            try {
195
                getSelectedURL();
196
                return true;
197
            } catch (MalformedURLException e) {
198
                return false;
199
            }
200
        }
201
        return false;
202
    }
203

    
204
    protected void checkNextButtonEnabled() {
205

    
206
    }
207

    
208
    public void changedUpdate(DocumentEvent e) {
209
        checkNextButtonEnabled();
210
    }
211

    
212
    public void insertUpdate(DocumentEvent e) {
213
        checkNextButtonEnabled();
214
    }
215

    
216
    public void removeUpdate(DocumentEvent e) {
217
        checkNextButtonEnabled();
218
    }
219

    
220
}