Statistics
| Revision:

root / trunk / extensions / extAddEventTheme / src / com / iver / gvsig / addeventtheme / gui / AddEventThemePanel.java @ 5932

History | View | Annotate | Download (10.9 KB)

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

    
46
import java.sql.Types;
47
import java.util.ArrayList;
48

    
49
import javax.swing.DefaultComboBoxModel;
50
import javax.swing.JButton;
51
import javax.swing.JComboBox;
52
import javax.swing.JDialog;
53
import javax.swing.JLabel;
54
import javax.swing.JPanel;
55

    
56
import org.cresques.cts.IProjection;
57

    
58
import com.hardcode.driverManager.DriverLoadException;
59
import com.hardcode.gdbms.engine.data.DataSource;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.ui.mdiManager.View;
63
import com.iver.andami.ui.mdiManager.ViewInfo;
64
import com.iver.cit.gvsig.fmap.DriverException;
65
import com.iver.cit.gvsig.fmap.FMap;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
69
import com.iver.cit.gvsig.project.ProjectTable;
70
import com.iver.gvsig.addeventtheme.AddEventThemListener;
71
import com.iver.gvsig.addeventtheme.AddEventThemeDriver;
72

    
73
/**
74
 * The AddEventThemePanel class creates a JPanel where the
75
 * user can input the name of the gvSIG Table from that the
76
 * plugin will create the new point Layer, and the fields
77
 * corresponding with the point coordinates.
78
 *
79
 * @author jmorell
80
 */
81
public class AddEventThemePanel extends JPanel implements View {
82
    private static final long serialVersionUID = 1L;
83
    private ViewInfo viewInfo = null;
84
    private JLabel tableLabel = null;
85
    private JLabel xLabel = null;
86
    private JLabel yLabel = null;
87
    private JComboBox tableComboBox = null;
88
    private JComboBox xComboBox = null;
89
    private JComboBox yComboBox = null;
90
    private JButton okButton = null;
91
    private FMap mapContext;
92
    private ArrayList tableList;
93
    private String firstCoordinate;
94
    private String secondCoordinate;
95

    
96
    /**
97
     * This is the default constructor
98
     */
99
    public AddEventThemePanel(FMap mapContext, ArrayList tableList) {
100
        super();
101
        this.mapContext = mapContext;
102
        this.tableList = tableList;
103
        initializeCoordinates();
104
        initialize();
105
    }
106

    
107
    private void initializeCoordinates() {
108
        IProjection proj = mapContext.getProjection();
109
        String projName = proj.getAbrev();
110
        if (projName.equals("EPSG:23030") || projName.equals("EPSG:27492") || projName.equals("EPSG:42101") || projName.equals("EPSG:42304")) {
111
            firstCoordinate = "X";
112
            secondCoordinate = "Y";
113
        } else if (projName.equals("EPSG:4230") || projName.equals("EPSG:4326")) {
114
            firstCoordinate = "Lon";
115
            secondCoordinate = "Lat";
116
        } else {
117
            System.out.println("Proyecci?n: " + projName);
118
            System.out.println("Proyecci?n no soportada.");
119
        }
120
    }
121

    
122
    /**
123
     * This method initializes this
124
     *
125
     * @return void
126
     */
127
    private void initialize() {
128
        yLabel = new JLabel();
129
        yLabel.setBounds(6, 68, 36, 24);
130
        yLabel.setText(secondCoordinate + ":");
131
        xLabel = new JLabel();
132
        xLabel.setBounds(6, 37, 36, 24);
133
        xLabel.setText(firstCoordinate + ":");
134
        tableLabel = new JLabel();
135
        tableLabel.setBounds(6, 6, 36, 24);
136
        tableLabel.setText(PluginServices.getText(this,"Tabla") + ":");
137
        this.setLayout(null);
138
        this.setSize(221, 131);
139
        this.add(tableLabel, null);
140
        this.add(xLabel, null);
141
        this.add(yLabel, null);
142
        this.add(getTableComboBox(), null);
143
        this.add(getXComboBox(), null);
144
        this.add(getYComboBox(), null);
145
        this.add(getOkButton(), null);
146
    }
147

    
148
    private String[] getTableNames() {
149
        String[] tableNames = new String[tableList.size()];
150
        for (int i=0;i<tableList.size();i++) {
151
            tableNames[i] = ((ProjectTable)tableList.get(i)).getName();
152
        }
153
        return tableNames;
154
    }
155
    private String[] getFieldNames() {
156
        String tableName = (String)tableComboBox.getSelectedItem();
157
        ProjectTable projectTable = getProjectTable(tableName);
158
        DataSource ds;
159
        ArrayList fieldName=new ArrayList();
160
        try {
161
            ds = projectTable.getModelo().getRecordset();
162
            for (int i = 0; i < ds.getFieldCount(); i++) {
163
                    if (ds.getFieldType(i)==Types.DOUBLE || ds.getFieldType(i)==Types.INTEGER)
164
                            fieldName.add(ds.getFieldName(i));
165
            }
166
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
167
            e.printStackTrace();
168
            NotificationManager.addError(e);
169
        } catch (DriverLoadException e) {
170
                        e.printStackTrace();
171
                }
172
        return (String[])fieldName.toArray(new String[0]);
173
    }
174

    
175
    private ProjectTable getProjectTable(String tableName) {
176
        ProjectTable projectTable = null;
177
        for (int i=0;i<tableList.size();i++) {
178
            if (((ProjectTable)tableList.get(i)).getName().equals(tableName)) {
179
                projectTable = (ProjectTable)tableList.get(i);
180
                break;
181
            }
182
        }
183
        return projectTable;
184
    }
185

    
186
    private void createNewLayerFromDataSource() {
187
        String tableName = (String)tableComboBox.getSelectedItem();
188
        ProjectTable projectTable = getProjectTable(tableName);
189
        try {
190
                DataSource ds = projectTable.getModelo().getRecordset();
191
                int xFieldIndex = 0;
192
                int yFieldIndex = 0;
193

    
194
            xFieldIndex = ds.getFieldIndexByName(getXFieldName());
195
            yFieldIndex = ds.getFieldIndexByName(getYFieldName());
196
                AddEventThemeDriver addEventThemeDriver = new AddEventThemeDriver();
197
                addEventThemeDriver.setData(ds, xFieldIndex, yFieldIndex);
198
                FLayer capa = null;
199
                try {
200
                    capa = LayerFactory.createLayer(tableName, addEventThemeDriver, mapContext.getProjection());
201

    
202
                } catch (DriverException e) {
203
                    // TODO Auto-generated catch block
204
                    e.printStackTrace();
205
                }
206
                capa.addLayerListener(new AddEventThemListener());
207
                projectTable.setAssociatedTable((AlphanumericData)capa);
208
                mapContext.getLayers().addLayer(capa);
209
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
210
            NotificationManager.addError(e1);
211
        } catch (DriverLoadException e) {
212
                NotificationManager.addError(e);
213
                }
214

    
215
    }
216

    
217
    private String getXFieldName() {
218
        return ((String)xComboBox.getSelectedItem());
219
    }
220

    
221
    private String getYFieldName() {
222
        return ((String)yComboBox.getSelectedItem());
223
    }
224

    
225
    /* (non-Javadoc)
226
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
227
     */
228
    public ViewInfo getViewInfo() {
229
        // TODO Auto-generated method stub
230
        if (viewInfo == null) {
231
            viewInfo=new ViewInfo(ViewInfo.MODALDIALOG);
232
            viewInfo.setTitle(PluginServices.getText(this,"Anadir_capa_de_eventos"));
233
        }
234
        return viewInfo;
235
    }
236

    
237
    /**
238
     * This method initializes tableComboBox
239
     *
240
     * @return javax.swing.JComboBox
241
     */
242
    private JComboBox getTableComboBox() {
243
            if (tableComboBox == null) {
244
                    tableComboBox = new JComboBox();
245
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getTableNames());
246
            tableComboBox.setModel(defaultModel);
247
                    tableComboBox.setBounds(43, 6, 161, 24);
248
                    tableComboBox.addItemListener(new java.awt.event.ItemListener() {
249
                            public void itemStateChanged(java.awt.event.ItemEvent e) {
250
                                    System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
251
                    // Cambiar el estado del xComboBox
252
                    DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
253
                    xComboBox.setModel(defaultModel);
254
                    // Cambiar el estado del yComboBox
255
                    defaultModel = new DefaultComboBoxModel(getFieldNames());
256
                    yComboBox.setModel(defaultModel);
257
                            }
258
                    });
259
            }
260
            return tableComboBox;
261
    }
262

    
263
    /**
264
     * This method initializes xComboBox
265
     *
266
     * @return javax.swing.JComboBox
267
     */
268
    private JComboBox getXComboBox() {
269
            if (xComboBox == null) {
270
                    xComboBox = new JComboBox();
271
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
272
            xComboBox.setModel(defaultModel);
273
                    xComboBox.setBounds(43, 37, 161, 24);
274
            }
275
            return xComboBox;
276
    }
277

    
278
    /**
279
     * This method initializes yComboBox
280
     *
281
     * @return javax.swing.JComboBox
282
     */
283
    private JComboBox getYComboBox() {
284
            if (yComboBox == null) {
285
                    yComboBox = new JComboBox();
286
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
287
            yComboBox.setModel(defaultModel);
288
                    yComboBox.setBounds(43, 68, 161, 24);
289
            }
290
            return yComboBox;
291
    }
292

    
293
    /**
294
     * This method initializes okButton
295
     *
296
     * @return javax.swing.JButton
297
     */
298
    private JButton getOkButton() {
299
            if (okButton == null) {
300
                    okButton = new JButton();
301
                    okButton.setBounds(104, 100, 100, 24);
302
                    okButton.addActionListener(new java.awt.event.ActionListener() {
303
                            public void actionPerformed(java.awt.event.ActionEvent e) {
304
                                    System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
305
                    createNewLayerFromDataSource();
306
                    // y sale.
307
                    if (PluginServices.getMainFrame() == null)
308
                        ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
309
                    else
310
                        PluginServices.getMDIManager().closeView(AddEventThemePanel.this);
311
                            }
312
                    });
313
                    okButton.setText(PluginServices.getText(this,"Aceptar"));
314
            }
315
            return okButton;
316
    }
317
}  //  @jve:decl-index=0:visual-constraint="10,10"