Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / UserTableSettingsPanel.java @ 10954

History | View | Annotate | Download (15.7 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 com.iver.cit.gvsig.sde.gui.sdewizard2;
44

    
45
import com.iver.andami.PluginServices;
46

    
47
import com.iver.cit.gvsig.fmap.MapControl;
48

    
49
import org.apache.log4j.Logger;
50

    
51
import org.gvsig.gui.beans.swing.JButton;
52

    
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55
import java.awt.event.KeyEvent;
56
import java.awt.event.KeyListener;
57
import java.awt.geom.Rectangle2D;
58

    
59
import java.text.DecimalFormat;
60
import java.text.DecimalFormatSymbols;
61

    
62
import javax.swing.JCheckBox;
63
import javax.swing.JComboBox;
64
import javax.swing.JLabel;
65
import javax.swing.JPanel;
66
import javax.swing.JScrollPane;
67
import javax.swing.JTextArea;
68
import javax.swing.JTextField;
69

    
70

    
71
/**
72
 * Utility class that holds a single table settings controls.
73
 *
74
 * @author jldominguez
75
 *
76
 */
77
public class UserTableSettingsPanel extends JPanel implements ActionListener,
78
    KeyListener {
79
    private static Logger logger = Logger.getLogger(UserTableSettingsPanel.class.getName());
80
    private FieldComboItem[] ids;
81
    private FieldComboItem[] geos;
82
    private String initLayerName = "";
83
    private JComboBox idComboBox = null;
84
    private JComboBox geomComboBox = null;
85
    private JTextArea sqlTextArea = null;
86
    private JLabel idLabel = null;
87
    private JLabel geomLabel = null;
88
    private JLabel sqlLabel = null;
89
    private JLabel waLabel = null;
90
    private JLabel topLabel = null;
91
    private JTextField topTextField = null;
92
    private JTextField bottomTextField = null;
93
    private JTextField rightTextField = null;
94
    private JTextField leftTextField = null;
95
    private JLabel bottomLabel = null;
96
    private JLabel rightLabel = null;
97
    private JLabel leftLabel = null;
98
    private JButton getviewButton = null;
99
    private JCheckBox activateWACheckBox = null;
100
    private JCheckBox activateSQLCheckBox = null;
101
    private JLabel tableNamejLabel = null;
102
    private JTextField layerNameTextField = null;
103
    private JScrollPane sqlTextAreaScroll = null;
104
    private MapControl mControl = null;
105
    private WizardSDE parent = null;
106

    
107
    public UserTableSettingsPanel(FieldComboItem[] idComboItems,
108
        FieldComboItem[] geoComboItems, String initialLayerName,
109
        MapControl mapc, boolean empty, WizardSDE _p) {
110
        parent = _p;
111

    
112
        mControl = mapc;
113
        initLayerName = initialLayerName;
114
        ids = idComboItems;
115
        geos = geoComboItems;
116
        initialize(empty);
117
    }
118

    
119
    public boolean hasValidValues() {
120
        if (!combosHaveItems()) {
121
            return false;
122
        }
123

    
124
        if ((activateWACheckBox.isSelected()) && (getWorkingArea() == null)) {
125
            return false;
126
        }
127

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

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

    
137
        return true;
138
    }
139

    
140
    private void initialize(boolean _empty) {
141
        tableNamejLabel = new JLabel();
142
        tableNamejLabel.setBounds(new java.awt.Rectangle(15, 25, 221, 21));
143
        tableNamejLabel.setText(PluginServices.getText(this, "layer_name"));
144
        leftLabel = new JLabel();
145
        leftLabel.setBounds(new java.awt.Rectangle(375, 175, 111, 16));
146
        leftLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
147
        leftLabel.setText(PluginServices.getText(this, "xmin"));
148
        rightLabel = new JLabel();
149
        rightLabel.setBounds(new java.awt.Rectangle(260, 175, 111, 16));
150
        rightLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
151
        rightLabel.setText(PluginServices.getText(this, "xmax"));
152
        bottomLabel = new JLabel();
153
        bottomLabel.setBounds(new java.awt.Rectangle(130, 175, 111, 16));
154
        bottomLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
155
        bottomLabel.setText(PluginServices.getText(this, "ymin"));
156
        topLabel = new JLabel();
157
        topLabel.setBounds(new java.awt.Rectangle(15, 175, 111, 16));
158
        topLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 10));
159
        topLabel.setText(PluginServices.getText(this, "ymax"));
160
        waLabel = new JLabel();
161
        waLabel.setBounds(new java.awt.Rectangle(40, 145, 131, 21));
162
        waLabel.setText(PluginServices.getText(this, "working_area"));
163
        sqlLabel = new JLabel();
164
        sqlLabel.setBounds(new java.awt.Rectangle(40, 90, 116, 21));
165
        sqlLabel.setText(PluginServices.getText(this, "sql_restriction"));
166
        geomLabel = new JLabel();
167
        geomLabel.setBounds(new java.awt.Rectangle(240, 55, 111, 21));
168
        geomLabel.setText(PluginServices.getText(this, "geo_field"));
169
        idLabel = new JLabel();
170
        idLabel.setBounds(new java.awt.Rectangle(15, 55, 86, 21));
171
        idLabel.setText(PluginServices.getText(this, "id_field"));
172

    
173
        setLayout(null);
174
        setBounds(new java.awt.Rectangle(5, 250, 501, 221));
175
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
176
                PluginServices.getText(this, "specify_table_settings"),
177
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
178
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
179
        add(getIdComboBox(), null);
180
        add(getGeomComboBox(), null);
181
        add(getSqlTextAreaScroll(), null);
182
        add(idLabel, null);
183
        add(geomLabel, null);
184
        add(sqlLabel, null);
185
        add(waLabel, null);
186
        add(topLabel, null);
187
        add(getTopTextField(), null);
188
        add(getBottomTextField(), null);
189
        add(getRightTextField(), null);
190
        add(getLeftTextField(), null);
191
        add(bottomLabel, null);
192
        add(rightLabel, null);
193
        add(leftLabel, null);
194
        add(getGetviewButton(), null);
195
        add(getActivateWACheckBox(), null);
196
        add(getActivateSQLCheckBox(), null);
197
        add(getLayerNameTextField(), null);
198
        add(tableNamejLabel, null);
199

    
200
        loadValues(_empty);
201
    }
202

    
203
    public void enableControls(boolean enable) {
204
        getIdComboBox().setEnabled(enable);
205
        getGeomComboBox().setEnabled(enable);
206
        getLayerNameTextField().setEnabled(enable);
207
        getActivateSQLCheckBox().setEnabled(enable);
208
        getSqlTextArea().setEnabled(enable);
209

    
210
        getActivateWACheckBox().setEnabled(enable);
211

    
212
        boolean there_is_view = ((mControl != null) &&
213
            (mControl.getViewPort().getAdjustedExtent() != null));
214

    
215
        getGetviewButton().setEnabled(enable && there_is_view);
216
        getTopTextField().setEnabled(enable);
217
        getBottomTextField().setEnabled(enable);
218
        getRightTextField().setEnabled(enable);
219
        getLeftTextField().setEnabled(enable);
220
    }
221

    
222
    public void loadValues() {
223
        loadValues(false);
224
    }
225

    
226
    private void loadValues(boolean is_empty) {
227
        if (is_empty) {
228
            enableControls(false);
229
            getLayerNameTextField().setText("");
230
            getActivateSQLCheckBox().setSelected(false);
231
            getActivateWACheckBox().setSelected(false);
232
        }
233
        else {
234
            getIdComboBox().removeAllItems();
235

    
236
            for (int i = 0; i < ids.length; i++) {
237
                getIdComboBox().addItem(ids[i]);
238
            }
239

    
240
            getGeomComboBox().removeAllItems();
241

    
242
            for (int i = 0; i < geos.length; i++) {
243
                getGeomComboBox().addItem(geos[i]);
244
            }
245

    
246
            getLayerNameTextField().setText(initLayerName);
247
        }
248
    }
249

    
250
    private JComboBox getIdComboBox() {
251
        if (idComboBox == null) {
252
            idComboBox = new JComboBox();
253

    
254
            idComboBox.setBounds(new java.awt.Rectangle(105, 55, 126, 21));
255
        }
256

    
257
        return idComboBox;
258
    }
259

    
260
    private JComboBox getGeomComboBox() {
261
        if (geomComboBox == null) {
262
            geomComboBox = new JComboBox();
263
            geomComboBox.setBounds(new java.awt.Rectangle(355, 55, 131, 21));
264
        }
265

    
266
        return geomComboBox;
267
    }
268

    
269
    private JTextField getTopTextField() {
270
        if (topTextField == null) {
271
            topTextField = new JTextField();
272
            topTextField.addKeyListener(this);
273
            topTextField.setBounds(new java.awt.Rectangle(15, 190, 111, 21));
274
        }
275

    
276
        return topTextField;
277
    }
278

    
279
    private JTextField getBottomTextField() {
280
        if (bottomTextField == null) {
281
            bottomTextField = new JTextField();
282
            bottomTextField.addKeyListener(this);
283
            bottomTextField.setBounds(new java.awt.Rectangle(130, 190, 111, 21));
284
        }
285

    
286
        return bottomTextField;
287
    }
288

    
289
    private JTextField getRightTextField() {
290
        if (rightTextField == null) {
291
            rightTextField = new JTextField();
292
            rightTextField.addKeyListener(this);
293
            rightTextField.setBounds(new java.awt.Rectangle(260, 190, 111, 21));
294
        }
295

    
296
        return rightTextField;
297
    }
298

    
299
    private JTextField getLeftTextField() {
300
        if (leftTextField == null) {
301
            leftTextField = new JTextField();
302
            leftTextField.addKeyListener(this);
303
            leftTextField.setBounds(new java.awt.Rectangle(375, 190, 111, 21));
304
        }
305

    
306
        return leftTextField;
307
    }
308

    
309
    private JButton getGetviewButton() {
310
        if (getviewButton == null) {
311
            getviewButton = new JButton();
312
            getviewButton.addActionListener(this);
313
            getviewButton.setBounds(new java.awt.Rectangle(195, 145, 111, 26));
314
            getviewButton.setForeground(java.awt.Color.black);
315
            getviewButton.setText(PluginServices.getText(this, "get_view"));
316
        }
317

    
318
        return getviewButton;
319
    }
320

    
321
    private JCheckBox getActivateWACheckBox() {
322
        if (activateWACheckBox == null) {
323
            activateWACheckBox = new JCheckBox();
324
            activateWACheckBox.addActionListener(this);
325
            activateWACheckBox.setBounds(new java.awt.Rectangle(15, 145, 21, 21));
326
        }
327

    
328
        return activateWACheckBox;
329
    }
330

    
331
    private JCheckBox getActivateSQLCheckBox() {
332
        if (activateSQLCheckBox == null) {
333
            activateSQLCheckBox = new JCheckBox();
334
            activateSQLCheckBox.addActionListener(this);
335
            activateSQLCheckBox.setBounds(new java.awt.Rectangle(15, 90, 21, 21));
336
        }
337

    
338
        return activateSQLCheckBox;
339
    }
340

    
341
    private JTextField getLayerNameTextField() {
342
        if (layerNameTextField == null) {
343
            layerNameTextField = new JTextField();
344
            layerNameTextField.setBounds(new java.awt.Rectangle(240, 25, 246, 21));
345
            layerNameTextField.addKeyListener(this);
346
        }
347

    
348
        return layerNameTextField;
349
    }
350

    
351
    private JTextArea getSqlTextArea() {
352
        if (sqlTextArea == null) {
353
            sqlTextArea = new JTextArea();
354
            sqlTextArea.setLineWrap(true);
355
            sqlTextArea.setWrapStyleWord(true);
356
            sqlTextArea.addKeyListener(this);
357

    
358
            // sqlTextArea.setBounds(new java.awt.Rectangle(160,90,326,41));
359
        }
360

    
361
        return sqlTextArea;
362
    }
363

    
364
    private JScrollPane getSqlTextAreaScroll() {
365
        if (sqlTextAreaScroll == null) {
366
            sqlTextAreaScroll = new JScrollPane();
367
            sqlTextAreaScroll.setBounds(new java.awt.Rectangle(160, 90, 326, 41));
368
            sqlTextAreaScroll.setViewportView(getSqlTextArea());
369
            sqlTextAreaScroll.updateUI();
370
        }
371

    
372
        return sqlTextAreaScroll;
373
    }
374

    
375
    public void actionPerformed(ActionEvent e) {
376
        Object src = e.getSource();
377

    
378
        if (src == getviewButton) {
379
            getViewIntoFourBounds();
380
            parent.checkFinishable();
381
        }
382

    
383
        if (src == activateSQLCheckBox) {
384
            enableSQLSettings(activateSQLCheckBox.isSelected());
385
            parent.checkFinishable();
386
        }
387

    
388
        if (src == activateWACheckBox) {
389
            enableWASettings(activateWACheckBox.isSelected());
390
            parent.checkFinishable();
391
        }
392
    }
393

    
394
    private void enableWASettings(boolean b) {
395
        getviewButton.setEnabled(b &&
396
            (mControl.getViewPort().getAdjustedExtent() != null));
397
        rightTextField.setEnabled(b);
398
        leftTextField.setEnabled(b);
399
        topTextField.setEnabled(b);
400
        bottomTextField.setEnabled(b);
401
    }
402

    
403
    private void enableSQLSettings(boolean b) {
404
        sqlTextArea.setEnabled(b);
405
    }
406

    
407
    private void getViewIntoFourBounds() {
408
        Rectangle2D rect = mControl.getViewPort().getAdjustedExtent();
409
        topTextField.setText(getFormattedDouble(rect.getMaxY()));
410
        bottomTextField.setText(getFormattedDouble(rect.getMinY()));
411
        rightTextField.setText(getFormattedDouble(rect.getMaxX()));
412
        leftTextField.setText(getFormattedDouble(rect.getMinX()));
413
    }
414

    
415
    private String getFormattedDouble(double d) {
416
        DecimalFormat df = new DecimalFormat("#.###");
417
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
418
        dfs.setDecimalSeparator('.');
419
        df.setDecimalFormatSymbols(dfs);
420
        return df.format(d);
421
    }
422

    
423
    public void keyPressed(KeyEvent e) {
424
    }
425

    
426
    public void keyReleased(KeyEvent e) {
427
        parent.checkFinishable();
428
    }
429

    
430
    public void keyTyped(KeyEvent e) {
431
    }
432

    
433
    public Rectangle2D getWorkingArea() {
434
        if (!activateWACheckBox.isSelected()) {
435
            return null;
436
        }
437

    
438
        double maxx;
439
        double maxy;
440
        double minx;
441
        double miny;
442

    
443
        try {
444
            maxx = Double.parseDouble(rightTextField.getText());
445
            miny = Double.parseDouble(bottomTextField.getText());
446
            minx = Double.parseDouble(leftTextField.getText());
447
            maxy = Double.parseDouble(topTextField.getText());
448
        }
449
        catch (NumberFormatException nfe) {
450
            logger.error("Not valid value: " + nfe.getMessage());
451

    
452
            return null;
453
        }
454

    
455
        return new Rectangle2D.Double(minx, miny, maxx - minx, maxy - miny);
456
    }
457

    
458
    public String getUserLayerName() {
459
        return getLayerNameTextField().getText();
460
    }
461

    
462
    public boolean combosHaveItems() {
463
        if (getIdComboBox().getItemCount() == 0) {
464
            return false;
465
        }
466

    
467
        if (getGeomComboBox().getItemCount() == 0) {
468
            return false;
469
        }
470

    
471
        return true;
472
    }
473

    
474
    public void repaint() {
475
        super.repaint();
476
        getIdComboBox().updateUI();
477
        getGeomComboBox().updateUI();
478
    }
479

    
480
    public String getIdFieldName() {
481
        return getIdComboBox().getSelectedItem().toString();
482
    }
483

    
484
    public String getGeoFieldName() {
485
        return getGeomComboBox().getSelectedItem().toString();
486
    }
487

    
488
    public String getWhereClause() {
489
        return getSqlTextArea().getText();
490
    }
491

    
492
    public boolean isSqlActive() {
493
        return getActivateSQLCheckBox().isSelected();
494
    }
495
}