Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / 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 @ 40558

History | View | Annotate | Download (10 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.table.gui;
25

    
26
import java.awt.GridBagConstraints;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.awt.event.ItemEvent;
30
import java.awt.event.ItemListener;
31

    
32
import javax.swing.ButtonGroup;
33
import javax.swing.JButton;
34
import javax.swing.JPanel;
35
import javax.swing.JRadioButton;
36
import javax.swing.JTextField;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.andami.ui.mdiManager.WindowInfo;
41

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

    
56
public class CSVSeparatorOptionsPanel extends JPanel implements IWindow,
57
    ItemListener, ActionListener {
58

    
59
    private static final long serialVersionUID = 1L;
60

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

    
71
    public CSVSeparatorOptionsPanel() {
72

    
73
        setName("Separation options");
74
        // Initialize component
75
        inicialize();
76

    
77
    }
78

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

    
85
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
86

    
87
        setLayout(new java.awt.GridBagLayout());
88

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

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

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

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

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

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

    
133
        ButtonGroup group = new ButtonGroup();
134
        group.add(commaOption);
135
        group.add(semicolonOption);
136
        group.add(otherSymbol);
137

    
138
    }
139

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

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

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

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

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

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

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

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

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

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

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

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

    
285
                    }
286
            PluginServices.getMDIManager().closeWindow(this);
287

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

    
295
    }
296

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

    
306
    public Object getWindowProfile() {
307
        return WindowInfo.DIALOG_PROFILE;
308
    }
309

    
310
}