Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / gui / CSVSeparatorOptionsPanel.java @ 36443

History | View | Annotate | Download (9.98 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
package org.gvsig.app.project.documents.table.gui;
23

    
24
import java.awt.GridBagConstraints;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.awt.event.ItemEvent;
28
import java.awt.event.ItemListener;
29

    
30
import javax.swing.ButtonGroup;
31
import javax.swing.JButton;
32
import javax.swing.JPanel;
33
import javax.swing.JRadioButton;
34
import javax.swing.JTextField;
35

    
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.andami.ui.mdiManager.WindowInfo;
39

    
40
/**
41
 * Class to create csv file at disk with the statistics group generated from a
42
 * table with an Specifically separator
43
 * 
44
 * Basic separator "," -> csv basic file
45
 * Excel separator ";" -> csv file to work in gvSIG documents
46
 * 
47
 * Optionally separator -> the user can enter a character or a string
48
 * 
49
 * 
50
 * @author ?ngel Fraile Gri??n e-mail: angel.fraile@iver.es
51
 * 
52
 */
53

    
54
public class CSVSeparatorOptionsPanel extends JPanel implements IWindow,
55
    ItemListener, ActionListener {
56

    
57
    private static final long serialVersionUID = 1L;
58

    
59
    public static final String commaChar = ",";
60
    public static final String semicolonChar = ";";
61
    private JButton buttonAccept;
62
    private JButton buttonCancel;
63
    private JRadioButton commaOption;
64
    private JRadioButton semicolonOption;
65
    private JRadioButton otherSymbol;
66
    private JTextField symbol;
67
    private String separator = null;
68

    
69
    public CSVSeparatorOptionsPanel() {
70

    
71
        setName("Separation options");
72
        // Initialize component
73
        inicialize();
74

    
75
    }
76

    
77
    /**
78
     * Creating a basic option separator panel
79
     * 
80
     */
81
    private void inicialize() {
82

    
83
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
84

    
85
        setLayout(new java.awt.GridBagLayout());
86

    
87
        gridBagConstraints = new java.awt.GridBagConstraints();
88
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
89
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 5);
90
        add(getJCheckBoxPointComma(), gridBagConstraints);
91

    
92
        gridBagConstraints = new java.awt.GridBagConstraints();
93
        gridBagConstraints.gridwidth = 2;
94
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
95
        gridBagConstraints.weightx = 1.0;
96
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 10);
97
        add(getJCheckBoxComma(), gridBagConstraints);
98

    
99
        gridBagConstraints = new java.awt.GridBagConstraints();
100
        gridBagConstraints.gridx = 0;
101
        gridBagConstraints.gridy = 1;
102
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
103
        gridBagConstraints.weighty = 1.0;
104
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
105
        add(getJCheckBoxOtherSymbol(), gridBagConstraints);
106

    
107
        gridBagConstraints = new java.awt.GridBagConstraints();
108
        gridBagConstraints.gridx = 1;
109
        gridBagConstraints.gridy = 1;
110
        gridBagConstraints.gridwidth = 2;
111
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
112
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
113
        gridBagConstraints.weightx = 1.0;
114
        gridBagConstraints.weighty = 1.0;
115
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
116
        add(getTextFieldSymbol(), gridBagConstraints);
117

    
118
        gridBagConstraints = new java.awt.GridBagConstraints();
119
        gridBagConstraints.gridx = 1;
120
        gridBagConstraints.gridy = 2;
121
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
122
        gridBagConstraints.weightx = 1.0;
123
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
124
        add(getAcceptButton(), gridBagConstraints);
125

    
126
        gridBagConstraints = new java.awt.GridBagConstraints();
127
        gridBagConstraints.gridx = 2;
128
        gridBagConstraints.gridy = 2;
129
        add(getCancelButton(), gridBagConstraints);
130

    
131
        ButtonGroup group = new ButtonGroup();
132
        group.add(commaOption);
133
        group.add(semicolonOption);
134
        group.add(otherSymbol);
135

    
136
    }
137

    
138
    /**
139
     * Selection option comma as the separator
140
     * 
141
     * 
142
     * @return JRadioButton
143
     */
144

    
145
    private JRadioButton getJCheckBoxComma() {
146
        if (commaOption == null) {
147
            commaOption =
148
                new JRadioButton(PluginServices.getText(this, "comma"), false);
149
            commaOption.setEnabled(true);
150
            commaOption.setToolTipText(PluginServices.getText(this,
151
                "sepatator_info_coma"));
152
        }
153
        return commaOption;
154
    }
155

    
156
    /**
157
     * Selection option semicolon as the separator
158
     * 
159
     * @return JRadioButton
160
     */
161
    private JRadioButton getJCheckBoxPointComma() {
162
        if (semicolonOption == null) {
163
            semicolonOption =
164
                new JRadioButton(PluginServices.getText(this, "semicolon"),
165
                    true);
166
            semicolonOption.setEnabled(true);
167
            semicolonOption.setToolTipText(PluginServices.getText(this,
168
                "sepatator_info_semicolon"));
169
        }
170
        return semicolonOption;
171
    }
172

    
173
    /**
174
     * The user can enter the separator
175
     * 
176
     * @return JRadioButton
177
     */
178
    private JRadioButton getJCheckBoxOtherSymbol() {
179
        if (otherSymbol == null) {
180
            otherSymbol =
181
                new JRadioButton(PluginServices.getText(this, "otro_simbolo"),
182
                    false);
183
            otherSymbol.setEnabled(true);
184
            otherSymbol.addItemListener(this);
185
            otherSymbol.setToolTipText(PluginServices.getText(this,
186
                "sepatator_info_other"));
187
        }
188
        return otherSymbol;
189
    }
190

    
191
    /**
192
     * Return textfield where the user can enter a separator (char or string)
193
     * 
194
     * @return TextField.
195
     */
196
    private JTextField getTextFieldSymbol() {
197
        if (symbol == null) {
198
            symbol = new JTextField(null, 4);
199
            symbol.setHorizontalAlignment(JTextField.RIGHT);
200
            symbol.setEnabled(otherSymbol.isSelected());
201
            symbol.setToolTipText(PluginServices
202
                .getText(this, "sepatator_info"));
203
        }
204
        return symbol;
205
    }
206

    
207
    /**
208
     * Method that generates the button to accept selected separator
209
     * 
210
     * @return JButton
211
     */
212
    private JButton getAcceptButton() {
213
        if (buttonAccept == null) {
214
            buttonAccept = new JButton();
215
            buttonAccept.setText(PluginServices.getText(this, "Aceptar"));
216
            buttonAccept.addActionListener(this);
217
            buttonAccept
218
                .setToolTipText(PluginServices.getText(this, "Aceptar"));
219
        }
220
        return buttonAccept;
221
    }
222

    
223
    /**
224
     * Method that generates the button to cancel csv file creation
225
     * 
226
     * @return JButton
227
     */
228
    private JButton getCancelButton() {
229
        if (buttonCancel == null) {
230
            buttonCancel = new JButton();
231
            buttonCancel.setText(PluginServices.getText(this, "Cancelar"));
232
            buttonCancel.addActionListener(this);
233
            buttonCancel.setToolTipText(PluginServices
234
                .getText(this, "Cancelar"));
235
        }
236
        return buttonCancel;
237
    }
238

    
239
    /**
240
     * Window properties
241
     * 
242
     * @see org.gvsig.andami.ui.mdiManager.IWindow#getWindowInfo()
243
     */
244
    public WindowInfo getWindowInfo() {
245
        WindowInfo m_viewinfo =
246
            new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
247
        m_viewinfo
248
            .setTitle(PluginServices.getText(this, "opciones_separacion"));
249
        m_viewinfo.setHeight(100);
250
        m_viewinfo.setWidth(300);
251
        return m_viewinfo;
252
    }
253

    
254
    /**
255
     * Getting the window profile
256
     */
257
    // public Object getWindowProfile() {
258
    // return WindowInfo.DIALOG_PROFILE;
259
    // }
260

    
261
    /**
262
     * JRadioButton to enter the separator listener
263
     */
264
    public void itemStateChanged(ItemEvent e) {
265
        symbol.setEnabled(otherSymbol.isSelected());
266
    }
267

    
268
    /**
269
     * JButtons Accept and Cancel listener
270
     */
271
    public void actionPerformed(ActionEvent event) {
272
        if (event.getSource() == this.buttonAccept) {
273
            if (commaOption.isSelected()) {
274
                separator = commaChar;
275

    
276
            } else
277
                if (semicolonOption.isSelected()) {
278
                    separator = semicolonChar;
279
                } else
280
                    if (otherSymbol.isSelected()) {
281
                        separator = symbol.getText();
282

    
283
                    }
284
            PluginServices.getMDIManager().closeWindow(this);
285

    
286
        }
287
        if (event.getSource() == this.buttonCancel) {
288
            separator = null;
289
            PluginServices.getMDIManager().closeWindow(this);
290
            return;
291
        }
292

    
293
    }
294

    
295
    /**
296
     * 
297
     * @return separator
298
     *         - Chosen as the separator character or separator string
299
     */
300
    public String getSeparator() {
301
        return separator;
302
    }
303

    
304
    public Object getWindowProfile() {
305
        return WindowInfo.DIALOG_PROFILE;
306
    }
307

    
308
}