Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / UserTableSettingsPanel.java @ 39298

History | View | Annotate | Download (11.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package org.gvsig.geodb.vectorialdb.wizard;
44

    
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.awt.event.KeyEvent;
48
import java.awt.event.KeyListener;
49
import java.text.DecimalFormat;
50
import java.text.DecimalFormatSymbols;
51
import java.util.ArrayList;
52
import java.util.List;
53

    
54
import javax.swing.ImageIcon;
55
import javax.swing.JButton;
56
import javax.swing.JCheckBox;
57
import javax.swing.JComboBox;
58
import javax.swing.JLabel;
59
import javax.swing.JPanel;
60
import javax.swing.JScrollPane;
61
import javax.swing.JTextArea;
62
import javax.swing.JTextField;
63

    
64
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
66

    
67
import org.gvsig.andami.IconThemeHelper;
68
import org.gvsig.andami.PluginServices;
69
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
70
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
71
import org.gvsig.i18n.Messages;
72
import org.gvsig.tools.service.ServiceException;
73

    
74

    
75

    
76
/**
77
 * Utility class that holds a single table settings controls.
78
 *
79
 * @author jldominguez
80
 *
81
 */
82
public class UserTableSettingsPanel extends JPanel implements ActionListener,
83
    KeyListener {
84

    
85
    private static final long serialVersionUID = -9205704982299200912L;
86

    
87
    private static Logger LOG = LoggerFactory
88
                        .getLogger(UserTableSettingsPanel.class);
89
    
90
    private FieldComboItem[] ids;
91
    private String initLayerName = "";
92
    private JComboBox idComboBox = null;
93
    private JTextArea sqlTextArea = null;
94
    private JLabel idLabel = null;
95
    private JLabel sqlLabel = null;
96
    private JCheckBox activateSQLCheckBox = null;
97
    protected JLabel tableNamejLabel = null;
98
    private JTextField layerNameTextField = null;
99
    private JScrollPane sqlTextAreaScroll = null;
100
    protected WizardDB parent = null;
101
        private JButton advancedSettingsButton;
102
        protected DBStoreParameters parameters;
103

    
104
    protected UserTableSettingsPanel() {
105
                super();
106
        }
107

    
108
        protected void setInitValues(FieldComboItem[] idComboItems,
109
                        String initialLayerName, boolean empty, WizardDB _p,DBStoreParameters parameters) {
110
        parent = _p;
111

    
112
        initLayerName = initialLayerName;
113
                ids = idComboItems;
114
                this.parameters = parameters;
115

    
116
        }
117

    
118
    public UserTableSettingsPanel(FieldComboItem[] idComboItems,
119
        String initialLayerName, boolean empty, WizardDB _p,
120
                        DBStoreParameters parameters) {
121
                setInitValues(idComboItems, initialLayerName, empty, _p, parameters);
122
        initialize(empty);
123
    }
124

    
125
    public boolean hasValidValues() {
126
        if (!combosHaveItems()) {
127
            return false;
128
        }
129

    
130
        if ((activateSQLCheckBox.isSelected()) &&
131
                (getSqlTextArea().getText().trim().length() == 0)) {
132
            return false;
133
        }
134

    
135
        if (getLayerNameTextField().getText().trim().length() == 0) {
136
            return false;
137
        }
138

    
139
        return true;
140
    }
141

    
142
    protected void initialize(boolean _empty) {
143
        tableNamejLabel = new JLabel();
144
        tableNamejLabel.setText(PluginServices.getText(this, "name"));
145
        tableNamejLabel.setSize(new java.awt.Dimension(86, 21));
146
        tableNamejLabel.setLocation(new java.awt.Point(8, 25));
147
        sqlLabel = new JLabel();
148
        // sqlLabel.setBounds(new java.awt.Rectangle(30, 90, 116, 21));
149
                sqlLabel.setBounds(new java.awt.Rectangle(30, 55, 116, 21));
150
        sqlLabel.setText(PluginServices.getText(this, "sql_restriction"));
151
        idLabel = new JLabel();
152
        // idLabel.setBounds(new java.awt.Rectangle(5, 55, 86, 21));
153
                idLabel.setBounds(new java.awt.Rectangle(260, 25, 86, 21));
154
        idLabel.setText(PluginServices.getText(this, "id_field"));
155

    
156
        setLayout(null);
157
        setBounds(new java.awt.Rectangle(3, 225, 501, 246));
158
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
159
                PluginServices.getText(this, "specify_table_settings"),
160
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
161
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
162
        add(getIdComboBox(), null);
163
        add(getSqlTextAreaScroll(), null);
164
        add(idLabel, null);
165
        add(sqlLabel, null);
166
        add(getActivateSQLCheckBox(), null);
167
        add(getLayerNameTextField(), null);
168
        add(tableNamejLabel, null);
169
        add(getAdvancedButton(), null);
170

    
171
        loadValues(_empty);
172
    }
173

    
174
    public void enableAlphaControls(boolean enable) {
175
        getIdComboBox().setEnabled(enable);
176
        getLayerNameTextField().setEnabled(enable);
177
        getSqlTextArea().setEnabled(enable);
178
        getActivateSQLCheckBox().setEnabled(enable);
179
        getAdvancedButton().setEnabled(enable);
180
    }
181

    
182
    public void enableSpatialControls(boolean enable) {
183

    
184
    }
185

    
186
    public void loadValues() {
187
        loadValues(false);
188
    }
189

    
190
    protected void loadValues(boolean is_empty) {
191
        if (is_empty) {
192
            enableAlphaControls(false);
193
            enableSpatialControls(false);
194
            getLayerNameTextField().setText("");
195
            getActivateSQLCheckBox().setSelected(false);
196
        }
197
        else {
198
            enableAlphaControls(true);
199
                        enableSpatialControls(true);
200
                        getActivateSQLCheckBox().setEnabled(true);
201
            getIdComboBox().removeAllItems();
202

    
203
            for (int i = 0; i < ids.length; i++) {
204
                getIdComboBox().addItem(ids[i]);
205
            }
206

    
207

    
208
            getLayerNameTextField().setText(initLayerName);
209

    
210
        }
211
    }
212

    
213
    private JComboBox getIdComboBox() {
214
        if (idComboBox == null) {
215
            idComboBox = new JComboBox();
216

    
217
            // idComboBox.setBounds(new java.awt.Rectangle(105, 55, 126, 21));
218
                        idComboBox.setBounds(new java.awt.Rectangle(351, 25, 134, 21));
219
        }
220

    
221
        return idComboBox;
222
    }
223

    
224
    private JButton getAdvancedButton() {
225
            if (advancedSettingsButton == null) {
226
                advancedSettingsButton = new org.gvsig.gui.beans.swing.JButton();
227
                int w = 31;
228
                advancedSettingsButton.setBounds(8, 126-w, w, w);
229
                
230
                ImageIcon ii = IconThemeHelper.getImageIcon("geodb-advanced-properties");
231
                // ImageIcon ii = IconThemeHelper.getImageIcon("geodb-connection-add");
232
                
233
                advancedSettingsButton.setIcon(ii);
234
                       // geodb-get-view-extent
235
                advancedSettingsButton.setToolTipText(
236
                    Messages.getText("_Advanced_properties"));
237
                advancedSettingsButton.addActionListener(this);
238
                }
239
                return advancedSettingsButton;
240
        }
241

    
242
    private JCheckBox getActivateSQLCheckBox() {
243
        if (activateSQLCheckBox == null) {
244
            activateSQLCheckBox = new JCheckBox();
245
            activateSQLCheckBox.addActionListener(this);
246
            //activateSQLCheckBox
247
                                        //.setBounds(new java.awt.Rectangle(5, 90, 21, 21));
248
                        activateSQLCheckBox
249
                                        .setBounds(new java.awt.Rectangle(5, 55, 21, 21));
250
        }
251

    
252
        return activateSQLCheckBox;
253
    }
254

    
255
    private JTextField getLayerNameTextField() {
256
        if (layerNameTextField == null) {
257
            layerNameTextField = new JTextField();
258
            layerNameTextField.setSize(new java.awt.Dimension(118, 21));
259
                        // layerNameTextField.setLocation(new java.awt.Point(105, 25));
260
                        layerNameTextField.setLocation(new java.awt.Point(120, 25));
261
            layerNameTextField.addKeyListener(this);
262
        }
263

    
264
        return layerNameTextField;
265
    }
266

    
267
    private JTextArea getSqlTextArea() {
268
        if (sqlTextArea == null) {
269
            sqlTextArea = new JTextArea();
270
            sqlTextArea.setLineWrap(true);
271
            sqlTextArea.setWrapStyleWord(true);
272
            sqlTextArea.addKeyListener(this);
273

    
274
            // sqlTextArea.setBounds(new java.awt.Rectangle(160,90,326,41));
275
        }
276

    
277
        return sqlTextArea;
278
    }
279

    
280
    private JScrollPane getSqlTextAreaScroll() {
281
        if (sqlTextAreaScroll == null) {
282
            sqlTextAreaScroll = new JScrollPane();
283
            // sqlTextAreaScroll.setBounds(new java.awt.Rectangle(160, 90, 326,
284
                        // 41));
285
                        sqlTextAreaScroll
286
                                        .setBounds(new java.awt.Rectangle(120, 55, 372, 53));
287
            sqlTextAreaScroll.setViewportView(getSqlTextArea());
288
            sqlTextAreaScroll.updateUI();
289
        }
290

    
291
        return sqlTextAreaScroll;
292
    }
293

    
294
    public void actionPerformed(ActionEvent e) {
295
        Object src = e.getSource();
296

    
297
        if (src == activateSQLCheckBox) {
298
            enableSQLSettings(activateSQLCheckBox.isSelected());
299
            parent.checkFinishable();
300
        } else if (src == getAdvancedButton()) {
301
                List<String> toHide = new ArrayList<String>();
302
                        toHide.add(DBStoreParameters.PASSWORD_PARAMTER_NAME);
303
                        toHide.add(DBStoreParameters.HOST_PARAMTER_NAME);
304
                        toHide.add(DBStoreParameters.PORT_PARAMTER_NAME);
305
                        toHide.add(DBStoreParameters.USER_PARAMTER_NAME);
306
                        toHide.add(DBStoreParameters.DBNAME_PARAMTER_NAME);
307
                        toHide.add(DBStoreParameters.DEFAULTGEOMETRY_PARAMTER_NAME);
308
                        toHide.add(DBStoreParameters.PKFIELDS_PARAMTER_NAME);
309
                        toHide.add(DBStoreParameters.FIELDS_PARAMTER_NAME);
310
                        toHide.add(DBStoreParameters.BASEFILTER_PARAMTER_NAME);
311
                        toHide.add(DBStoreParameters.WORKINGAREA_PARAMTER_NAME);
312
                        toHide.add(DBStoreParameters.TABLE_PARAMTER_NAME);
313
                        toHide.add(DBStoreParameters.CRS_PARAMTER_NAME);
314
                        try {
315
                                parameters.validate();
316
                        } catch (Exception ex) {
317
                                // ignore... only for fill default values
318
                        }
319
                        try {
320
                DynObjectEditor editor = new DynObjectEditor(parameters);
321
                editor.editObject(true);
322
                } catch (ServiceException ex) {
323
                    LOG.error(
324
                        "Error creating a Swing component for the DynObject: "
325
                            + parameters, ex);
326
            }
327

    
328

    
329
                        return;
330

    
331
        }
332

    
333
    }
334

    
335

    
336
    private void enableSQLSettings(boolean b) {
337
        sqlTextArea.setEnabled(b);
338
    }
339

    
340
    protected String getFormattedDouble(double d) {
341
        DecimalFormat df = new DecimalFormat("#.###");
342
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
343
        dfs.setDecimalSeparator('.');
344
        df.setDecimalFormatSymbols(dfs);
345
        return df.format(d);
346
    }
347

    
348
    public void keyPressed(KeyEvent e) {
349
    }
350

    
351
    public void keyReleased(KeyEvent e) {
352
        parent.checkFinishable();
353
    }
354

    
355
    public void keyTyped(KeyEvent e) {
356
    }
357

    
358

    
359
    public String getUserLayerName() {
360
        return getLayerNameTextField().getText();
361
    }
362

    
363
    public boolean combosHaveItems() {
364
        if (getIdComboBox().getItemCount() == 0) {
365
            return false;
366
        }
367

    
368
        return true;
369
    }
370

    
371
    public void repaint() {
372
        super.repaint();
373
        getIdComboBox().updateUI();
374
    }
375

    
376
    public String getIdFieldName() {
377
        return getIdComboBox().getSelectedItem().toString();
378
    }
379

    
380
    public String getWhereClause() {
381
        return getSqlTextArea().getText();
382
    }
383

    
384
    public boolean isSqlActive() {
385
        return getActivateSQLCheckBox().isSelected();
386
    }
387
}