Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / UserTableSettingsPanel.java @ 40557

History | View | Annotate | Download (11.5 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.geodb.vectorialdb.wizard;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.awt.event.KeyEvent;
29
import java.awt.event.KeyListener;
30
import java.text.DecimalFormat;
31
import java.text.DecimalFormatSymbols;
32
import java.util.ArrayList;
33
import java.util.List;
34

    
35
import javax.swing.ImageIcon;
36
import javax.swing.JButton;
37
import javax.swing.JCheckBox;
38
import javax.swing.JComboBox;
39
import javax.swing.JLabel;
40
import javax.swing.JPanel;
41
import javax.swing.JScrollPane;
42
import javax.swing.JTextArea;
43
import javax.swing.JTextField;
44

    
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
import org.gvsig.andami.IconThemeHelper;
49
import org.gvsig.andami.PluginServices;
50
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
51
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
52
import org.gvsig.i18n.Messages;
53
import org.gvsig.tools.service.ServiceException;
54

    
55

    
56

    
57
/**
58
 * Utility class that holds a single table settings controls.
59
 *
60
 * @author jldominguez
61
 *
62
 */
63
public class UserTableSettingsPanel extends JPanel implements ActionListener,
64
    KeyListener {
65

    
66
    private static final long serialVersionUID = -9205704982299200912L;
67

    
68
    private static Logger LOG = LoggerFactory
69
                        .getLogger(UserTableSettingsPanel.class);
70
    
71
    private FieldComboItem[] ids;
72
    private String initLayerName = "";
73
    private JComboBox idComboBox = null;
74
    private JTextArea sqlTextArea = null;
75
    private JLabel idLabel = null;
76
    private JLabel sqlLabel = null;
77
    private JCheckBox activateSQLCheckBox = null;
78
    protected JLabel tableNamejLabel = null;
79
    private JTextField layerNameTextField = null;
80
    private JScrollPane sqlTextAreaScroll = null;
81
    protected WizardDB parent = null;
82
        private JButton advancedSettingsButton;
83
        protected DBStoreParameters parameters;
84

    
85
    protected UserTableSettingsPanel() {
86
                super();
87
        }
88

    
89
        protected void setInitValues(FieldComboItem[] idComboItems,
90
                        String initialLayerName, boolean empty, WizardDB _p,DBStoreParameters parameters) {
91
        parent = _p;
92

    
93
        initLayerName = initialLayerName;
94
                ids = idComboItems;
95
                this.parameters = parameters;
96

    
97
        }
98

    
99
    public UserTableSettingsPanel(FieldComboItem[] idComboItems,
100
        String initialLayerName, boolean empty, WizardDB _p,
101
                        DBStoreParameters parameters) {
102
                setInitValues(idComboItems, initialLayerName, empty, _p, parameters);
103
        initialize(empty);
104
    }
105

    
106
    public boolean hasValidValues() {
107
        if (!combosHaveItems()) {
108
            return false;
109
        }
110

    
111
        if ((activateSQLCheckBox.isSelected()) &&
112
                (getSqlTextArea().getText().trim().length() == 0)) {
113
            return false;
114
        }
115

    
116
        if (getLayerNameTextField().getText().trim().length() == 0) {
117
            return false;
118
        }
119

    
120
        return true;
121
    }
122

    
123
    protected void initialize(boolean _empty) {
124
        tableNamejLabel = new JLabel();
125
        tableNamejLabel.setText(PluginServices.getText(this, "_Name"));
126
        tableNamejLabel.setSize(new java.awt.Dimension(86, 21));
127
        tableNamejLabel.setLocation(new java.awt.Point(8, 25));
128
        sqlLabel = new JLabel();
129
        // sqlLabel.setBounds(new java.awt.Rectangle(30, 90, 116, 21));
130
                sqlLabel.setBounds(new java.awt.Rectangle(30, 55, 116, 21));
131
        sqlLabel.setText(PluginServices.getText(this, "_SQL_restriction"));
132
        idLabel = new JLabel();
133
        // idLabel.setBounds(new java.awt.Rectangle(5, 55, 86, 21));
134
                idLabel.setBounds(new java.awt.Rectangle(260, 25, 86, 21));
135
        idLabel.setText(PluginServices.getText(this, "_Id_field"));
136

    
137
        setLayout(null);
138
        setBounds(new java.awt.Rectangle(3, 225, 501, 246));
139
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
140
                PluginServices.getText(this, "_Table_settings"),
141
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
142
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
143
        add(getIdComboBox(), null);
144
        add(getSqlTextAreaScroll(), null);
145
        add(idLabel, null);
146
        add(sqlLabel, null);
147
        add(getActivateSQLCheckBox(), null);
148
        add(getLayerNameTextField(), null);
149
        add(tableNamejLabel, null);
150
        add(getAdvancedButton(), null);
151

    
152
        loadValues(_empty);
153
    }
154

    
155
    public void enableAlphaControls(boolean enable) {
156
        getIdComboBox().setEnabled(enable);
157
        getLayerNameTextField().setEnabled(enable);
158
        getSqlTextArea().setEnabled(enable);
159
        getActivateSQLCheckBox().setEnabled(enable);
160
        getAdvancedButton().setEnabled(enable);
161
    }
162

    
163
    public void enableSpatialControls(boolean enable) {
164

    
165
    }
166

    
167
    public void loadValues() {
168
        loadValues(false);
169
    }
170

    
171
    protected void loadValues(boolean is_empty) {
172
        if (is_empty) {
173
            enableAlphaControls(false);
174
            enableSpatialControls(false);
175
            getLayerNameTextField().setText("");
176
            getActivateSQLCheckBox().setSelected(false);
177
        }
178
        else {
179
            enableAlphaControls(true);
180
                        enableSpatialControls(true);
181
                        getActivateSQLCheckBox().setEnabled(true);
182
            getIdComboBox().removeAllItems();
183

    
184
            for (int i = 0; i < ids.length; i++) {
185
                getIdComboBox().addItem(ids[i]);
186
            }
187

    
188

    
189
            getLayerNameTextField().setText(initLayerName);
190

    
191
        }
192
    }
193

    
194
    private JComboBox getIdComboBox() {
195
        if (idComboBox == null) {
196
            idComboBox = new JComboBox();
197

    
198
            // idComboBox.setBounds(new java.awt.Rectangle(105, 55, 126, 21));
199
                        idComboBox.setBounds(new java.awt.Rectangle(351, 25, 134, 21));
200
        }
201

    
202
        return idComboBox;
203
    }
204

    
205
    private JButton getAdvancedButton() {
206
            if (advancedSettingsButton == null) {
207
                advancedSettingsButton = new org.gvsig.gui.beans.swing.JButton();
208
                int w = 31;
209
                advancedSettingsButton.setBounds(8, 126-w, w, w);
210
                
211
                ImageIcon ii = IconThemeHelper.getImageIcon("geodb-advanced-properties");
212
                // ImageIcon ii = IconThemeHelper.getImageIcon("geodb-connection-add");
213
                
214
                advancedSettingsButton.setIcon(ii);
215
                       // geodb-get-view-extent
216
                advancedSettingsButton.setToolTipText(
217
                    Messages.getText("_Advanced_properties"));
218
                advancedSettingsButton.addActionListener(this);
219
                }
220
                return advancedSettingsButton;
221
        }
222

    
223
    private JCheckBox getActivateSQLCheckBox() {
224
        if (activateSQLCheckBox == null) {
225
            activateSQLCheckBox = new JCheckBox();
226
            activateSQLCheckBox.addActionListener(this);
227
            //activateSQLCheckBox
228
                                        //.setBounds(new java.awt.Rectangle(5, 90, 21, 21));
229
                        activateSQLCheckBox
230
                                        .setBounds(new java.awt.Rectangle(5, 55, 21, 21));
231
        }
232

    
233
        return activateSQLCheckBox;
234
    }
235

    
236
    private JTextField getLayerNameTextField() {
237
        if (layerNameTextField == null) {
238
            layerNameTextField = new JTextField();
239
            layerNameTextField.setSize(new java.awt.Dimension(118, 21));
240
                        // layerNameTextField.setLocation(new java.awt.Point(105, 25));
241
                        layerNameTextField.setLocation(new java.awt.Point(120, 25));
242
            layerNameTextField.addKeyListener(this);
243
        }
244

    
245
        return layerNameTextField;
246
    }
247

    
248
    private JTextArea getSqlTextArea() {
249
        if (sqlTextArea == null) {
250
            sqlTextArea = new JTextArea();
251
            sqlTextArea.setLineWrap(true);
252
            sqlTextArea.setWrapStyleWord(true);
253
            sqlTextArea.addKeyListener(this);
254

    
255
            // sqlTextArea.setBounds(new java.awt.Rectangle(160,90,326,41));
256
        }
257

    
258
        return sqlTextArea;
259
    }
260

    
261
    private JScrollPane getSqlTextAreaScroll() {
262
        if (sqlTextAreaScroll == null) {
263
            sqlTextAreaScroll = new JScrollPane();
264
            // sqlTextAreaScroll.setBounds(new java.awt.Rectangle(160, 90, 326,
265
                        // 41));
266
                        sqlTextAreaScroll
267
                                        .setBounds(new java.awt.Rectangle(120, 55, 372, 53));
268
            sqlTextAreaScroll.setViewportView(getSqlTextArea());
269
            sqlTextAreaScroll.updateUI();
270
        }
271

    
272
        return sqlTextAreaScroll;
273
    }
274

    
275
    public void actionPerformed(ActionEvent e) {
276
        Object src = e.getSource();
277

    
278
        if (src == activateSQLCheckBox) {
279
            enableSQLSettings(activateSQLCheckBox.isSelected());
280
            parent.checkFinishable();
281
        } else if (src == getAdvancedButton()) {
282
                List<String> toHide = new ArrayList<String>();
283
                        toHide.add(DBStoreParameters.PASSWORD_PARAMTER_NAME);
284
                        toHide.add(DBStoreParameters.HOST_PARAMTER_NAME);
285
                        toHide.add(DBStoreParameters.PORT_PARAMTER_NAME);
286
                        toHide.add(DBStoreParameters.USER_PARAMTER_NAME);
287
                        toHide.add(DBStoreParameters.DBNAME_PARAMTER_NAME);
288
                        toHide.add(DBStoreParameters.DEFAULTGEOMETRY_PARAMTER_NAME);
289
                        toHide.add(DBStoreParameters.PKFIELDS_PARAMTER_NAME);
290
                        toHide.add(DBStoreParameters.FIELDS_PARAMTER_NAME);
291
                        toHide.add(DBStoreParameters.BASEFILTER_PARAMTER_NAME);
292
                        toHide.add(DBStoreParameters.WORKINGAREA_PARAMTER_NAME);
293
                        toHide.add(DBStoreParameters.TABLE_PARAMTER_NAME);
294
                        toHide.add(DBStoreParameters.CRS_PARAMTER_NAME);
295
                        try {
296
                                parameters.validate();
297
                        } catch (Exception ex) {
298
                                // ignore... only for fill default values
299
                        }
300
                        try {
301
                DynObjectEditor editor = new DynObjectEditor(parameters);
302
                editor.editObject(true);
303
                } catch (ServiceException ex) {
304
                    LOG.error(
305
                        "Error creating a Swing component for the DynObject: "
306
                            + parameters, ex);
307
            }
308

    
309

    
310
                        return;
311

    
312
        }
313

    
314
    }
315

    
316

    
317
    private void enableSQLSettings(boolean b) {
318
        sqlTextArea.setEnabled(b);
319
    }
320

    
321
    protected String getFormattedDouble(double d) {
322
        DecimalFormat df = new DecimalFormat("#.###");
323
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
324
        dfs.setDecimalSeparator('.');
325
        df.setDecimalFormatSymbols(dfs);
326
        return df.format(d);
327
    }
328

    
329
    public void keyPressed(KeyEvent e) {
330
    }
331

    
332
    public void keyReleased(KeyEvent e) {
333
        parent.checkFinishable();
334
    }
335

    
336
    public void keyTyped(KeyEvent e) {
337
    }
338

    
339

    
340
    public String getUserLayerName() {
341
        return getLayerNameTextField().getText();
342
    }
343

    
344
    public boolean combosHaveItems() {
345
        if (getIdComboBox().getItemCount() == 0) {
346
            return false;
347
        }
348

    
349
        return true;
350
    }
351

    
352
    public void repaint() {
353
        super.repaint();
354
        getIdComboBox().updateUI();
355
    }
356

    
357
    public String getIdFieldName() {
358
        return getIdComboBox().getSelectedItem().toString();
359
    }
360

    
361
    public String getWhereClause() {
362
        return getSqlTextArea().getText();
363
    }
364

    
365
    public boolean isSqlActive() {
366
        return getActivateSQLCheckBox().isSelected();
367
    }
368
}