Statistics
| Revision:

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

History | View | Annotate | Download (11.7 KB)

1
/*
2
 * Created on 09-nov-2005
3
 *
4
 * gvSIG. Sistema de Informacin Geogrfica 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 Ibez, 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.JComboBox;
51
import javax.swing.JDialog;
52
import javax.swing.JLabel;
53
import javax.swing.JPanel;
54

    
55
import org.cresques.cts.IProjection;
56
import org.gvsig.gui.beans.AcceptCancelPanel;
57
import org.gvsig.gui.beans.swing.JButton;
58

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

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

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

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

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

    
150
    private String[] getTableNames() {
151
        String[] tableNames = new String[tableList.size()];
152
        for (int i=0;i<tableList.size();i++) {
153
            tableNames[i] = ((ProjectTable)tableList.get(i)).getName();
154
        }
155
        return tableNames;
156
    }
157
    private String[] getFieldNames() {
158
        String tableName = (String)tableComboBox.getSelectedItem();
159
        ProjectTable projectTable = getProjectTable(tableName);
160
        DataSource ds;
161
        ArrayList fieldName=new ArrayList();
162
        try {
163
            ds = projectTable.getModelo().getRecordset();
164
            for (int i = 0; i < ds.getFieldCount(); i++) {
165
                    // Podr?amos comprobar si es un campo num?rico pero entonces
166
                    // no va con el driver de csv => moraleja: cuando
167
                    // se cambia el driver de csv para que sepa de qu? campos
168
                    // hablamos, se habilita esta l?nea.
169
                    // if (ds.getFieldType(i)==Types.DOUBLE || ds.getFieldType(i)==Types.INTEGER || ds.getFieldType(i)==Types.VARCHAR)
170
                            fieldName.add(ds.getFieldName(i));
171
            }
172
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
173
            e.printStackTrace();
174
            NotificationManager.addError(e);
175
        } catch (DriverLoadException e) {
176
                        e.printStackTrace();
177
                }
178
        return (String[])fieldName.toArray(new String[0]);
179
    }
180

    
181
    private ProjectTable getProjectTable(String tableName) {
182
        ProjectTable projectTable = null;
183
        for (int i=0;i<tableList.size();i++) {
184
            if (((ProjectTable)tableList.get(i)).getName().equals(tableName)) {
185
                projectTable = (ProjectTable)tableList.get(i);
186
                break;
187
            }
188
        }
189
        return projectTable;
190
    }
191

    
192
    private void createNewLayerFromDataSource() {
193
        String tableName = (String)tableComboBox.getSelectedItem();
194
        ProjectTable projectTable = getProjectTable(tableName);
195
        try {
196
                DataSource ds = projectTable.getModelo().getRecordset();
197
                int xFieldIndex = 0;
198
                int yFieldIndex = 0;
199

    
200
            xFieldIndex = ds.getFieldIndexByName(getXFieldName());
201
            yFieldIndex = ds.getFieldIndexByName(getYFieldName());
202
                AddEventThemeDriver addEventThemeDriver = new AddEventThemeDriver();
203
                addEventThemeDriver.setData(ds, xFieldIndex, yFieldIndex);
204
                FLayer capa = null;
205
                try {
206
                    capa = LayerFactory.createLayer(tableName, addEventThemeDriver, mapContext.getProjection());
207

    
208
                } catch (DriverException e) {
209
                    // TODO Auto-generated catch block
210
                    e.printStackTrace();
211
                }
212
                capa.addLayerListener(new AddEventThemListener());
213
                projectTable.setAssociatedTable((AlphanumericData)capa);
214
                mapContext.getLayers().addLayer(capa);
215
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
216
            NotificationManager.addError(e1);
217
        } catch (DriverLoadException e) {
218
                NotificationManager.addError(e);
219
                }
220

    
221
    }
222

    
223
    private String getXFieldName() {
224
        return ((String)xComboBox.getSelectedItem());
225
    }
226

    
227
    private String getYFieldName() {
228
        return ((String)yComboBox.getSelectedItem());
229
    }
230

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

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

    
269
    /**
270
     * This method initializes xComboBox
271
     *
272
     * @return javax.swing.JComboBox
273
     */
274
    private JComboBox getXComboBox() {
275
            if (xComboBox == null) {
276
                    xComboBox = new JComboBox();
277
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
278
            xComboBox.setModel(defaultModel);
279
                    xComboBox.setBounds(92, 37, 290, 23);
280
            }
281
            return xComboBox;
282
    }
283

    
284
    /**
285
     * This method initializes yComboBox
286
     *
287
     * @return javax.swing.JComboBox
288
     */
289
    private JComboBox getYComboBox() {
290
            if (yComboBox == null) {
291
                    yComboBox = new JComboBox();
292
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
293
            yComboBox.setModel(defaultModel);
294
                    yComboBox.setBounds(92, 68, 290, 23);
295
            }
296
            return yComboBox;
297
    }
298

    
299
    /**
300
     * This method initializes okButton
301
     *
302
     * @return javax.swing.JButton
303
     */
304
    private AcceptCancelPanel getAcceptPanel() {
305
            if (acceptPanel == null) {
306
                    acceptPanel = new AcceptCancelPanel(
307
                                    new java.awt.event.ActionListener() {
308
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
309
                                                System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
310
                                createNewLayerFromDataSource();
311
                                // y sale.
312
                                if (PluginServices.getMainFrame() == null)
313
                                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
314
                                else
315
                                    PluginServices.getMDIManager().closeView(AddEventThemePanel.this);
316
                                        }
317
                                },
318
                                    new java.awt.event.ActionListener() {
319
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
320
                                if (PluginServices.getMainFrame() == null)
321
                                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
322
                                else
323
                                    PluginServices.getMDIManager().closeView(AddEventThemePanel.this);
324
                                        }
325
                                }
326
                                    );
327
                    acceptPanel.setBounds(0, 100, getWidth()-6, 30);
328
            }
329
            return acceptPanel;
330
    }
331
}  //  @jve:decl-index=0:visual-constraint="10,10"