Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / TablesListItem.java @ 22373

History | View | Annotate | Download (9.17 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.prodevelop.cit.gvsig.vectorialdb.wizard;
44

    
45
import java.util.ArrayList;
46
import java.util.Iterator;
47

    
48
import javax.swing.JCheckBox;
49

    
50
import org.cresques.cts.IProjection;
51
import org.gvsig.fmap.data.ReadException;
52
import org.gvsig.fmap.data.feature.db.DBAttributeDescriptor;
53
import org.gvsig.fmap.data.feature.db.DBExplorer;
54
import org.gvsig.fmap.data.feature.db.DBParameters;
55
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
56
import org.gvsig.fmap.data.feature.FeatureType;
57
import org.gvsig.fmap.mapcontrol.MapControl;
58

    
59
import com.iver.andami.messages.NotificationManager;
60
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
61
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
62

    
63

    
64
/**
65
 * Utility class that represents a table list item as a selectable check box.
66
 *
67
 * @author jldominguez
68
 *
69
 */
70
public class TablesListItem extends JCheckBox {
71
    private String tableName = "";
72
    private UserSelectedFieldsPanel selectedFieldsPanel = null;
73
    private UserTableSettingsPanel tableSettingsPanel = null;
74
//    private FeatureStore featureStore = null;
75
//    private IConnection conn = null;
76
    private MapControl mc;
77
    private WizardVectorialDB parent = null;
78
    private boolean activated = false;
79
    private CRSSelectPanel jPanelProj;
80
        private DBParameters parameters;
81
        private DBExplorer explorer;
82

    
83
    public TablesListItem(DBExplorer explorer,DBParameters parameters,MapControl _mc, WizardVectorialDB _parent) {
84
        tableName = parameters.getTableName();
85
        setText(tableName);
86
        this.parameters = parameters;
87
        this.explorer=explorer;
88
//        conn = _conn;
89
        mc = _mc;
90
        parent = _parent;
91
    }
92

    
93
    public void activate() {
94
        activated = true;
95
        selectedFieldsPanel.loadValues();
96
        tableSettingsPanel.loadValues();
97
    }
98

    
99
    public boolean isActivated() {
100
        return activated;
101
    }
102

    
103
    /**
104
     * Tells whether this item prevents the wizard from being in a valid final state.
105
     * @return whether this item prevents the wizard from being in a valid final state.
106
     */
107
    public boolean disturbsWizardValidity() {
108
        if (isSelected()) {
109
            return (!hasValidValues());
110
        }
111
        else {
112
            return false;
113
        }
114
    }
115

    
116
    private boolean hasValidValues() {
117
        return tableSettingsPanel.hasValidValues();
118
    }
119

    
120
    public String toString() {
121
        return tableName;
122
    }
123

    
124
    public String getTableName() {
125
        return tableName;
126
    }
127

    
128
    public void setEnabledPanels(boolean b) {
129
        selectedFieldsPanel.enableControls(b);
130
        tableSettingsPanel.enableControls(b);
131
    }
132

    
133
    public UserSelectedFieldsPanel getUserSelectedFieldsPanel() {
134
        if (selectedFieldsPanel == null) {
135
                FeatureType ft=null;
136
                        try {
137
                                ft = explorer.getFeatureTypes(parameters)[0];
138
                        } catch (ReadException e) {
139
                                // TODO Auto-generated catch block
140
                                e.printStackTrace();
141
                        }
142
                FeatureAttributeDescriptor[] allf=(FeatureAttributeDescriptor[])ft.toArray(new FeatureAttributeDescriptor[0]);
143
//            FeatureAttributeDescriptor[] allf = driver.getAllFields(conn, tableName);
144
//            String[] allt = driver.getAllFieldTypeNames(conn, tableName);
145
            selectedFieldsPanel = new UserSelectedFieldsPanel(allf, true,
146
                    parent);
147
        }
148

    
149
        return selectedFieldsPanel;
150
    }
151

    
152
    public UserTableSettingsPanel getUserTableSettingsPanel(String espView) {
153
        if (tableSettingsPanel == null) {
154

    
155
                String[] ids = new String[0];
156
//                try {
157
                FeatureType ft=null;
158

    
159
                        try {
160
                                ft = explorer.getFeatureTypes(parameters)[0];
161
                        } catch (ReadException e) {
162
                                // TODO Auto-generated catch block
163
                                e.printStackTrace();
164
                        }
165

    
166

    
167
                       ids = ft.getFields();
168
//                } catch (DBException se) {
169
//                        String msg = PluginServices.getText(this, "id_not_available") + " " + tableName
170
//                        + ":\n" + se.getMessage();
171
//                        String title = PluginServices.getText(this, "id_error");
172
//                        JOptionPane.showMessageDialog(parent, msg, title, JOptionPane.ERROR_MESSAGE);
173
//                        setSelected(false);
174
//                }
175

    
176
                String[] geos = new String[0];
177
                ArrayList auxGeo = new ArrayList();
178
                        ArrayList auxId = new ArrayList();
179
                Iterator iter = ft.iterator();
180
                        while (iter.hasNext()) {
181
                                DBAttributeDescriptor dbattr = (DBAttributeDescriptor) iter
182
                                                .next();
183
                                if (dbattr.getDataType() == dbattr.TYPE_GEOMETRY) {
184
                                        auxGeo.add(dbattr.getName());
185
                                }
186
                                // if (dbattr.isPrimaryKey()) {
187
                                        auxId.add(dbattr.getName());
188
                                // }
189

    
190
                        }
191

    
192
//                try {
193
                        geos = (String[]) auxGeo.toArray(new String[0]);
194
                        ids = (String[]) auxId.toArray(new String[0]);
195
//                } catch (DBException se) {
196
//                        String msg = PluginServices.getText(this, "geo_field_not_available")
197
//                        + ":\n" + PluginServices.getText(this, se.getMessage()) + ": " + tableName;
198
//                        String title = PluginServices.getText(this, "geo_field_error");
199
//                        JOptionPane.showMessageDialog(parent, msg, title, JOptionPane.ERROR_MESSAGE);
200
//                        setSelected(false);
201
//                }
202

    
203
            int ids_size = ids.length;
204
            FieldComboItem[] ids_ci = new FieldComboItem[ids_size];
205

    
206
            for (int i = 0; i < ids_size; i++) {
207
                                ids_ci[i] = new FieldComboItem(ids[i]);
208
                        }
209

    
210
            int geos_size = geos.length;
211
            FieldComboItem[] geos_ci = new FieldComboItem[geos_size];
212

    
213
            for (int i = 0; i < geos_size; i++) {
214
                                geos_ci[i] = new FieldComboItem(geos[i]);
215
                        }
216

    
217
            tableSettingsPanel = new UserTableSettingsPanel(ids_ci, geos_ci,
218
                              tableName, mc, true, parent, projectionPanel(espView,ids_ci,geos_ci));
219
        }
220

    
221
        return tableSettingsPanel;
222
    }
223
    public CRSSelectPanel projectionPanel(String espView,
224
                        FieldComboItem[] ids_ci, FieldComboItem[] geos_ci) {
225
                IProjection proj = AddLayerDialog.getLastProjection();
226
//                if (driver instanceof ICanReproject) {
227
                        try {
228
//                                DBFeatureType lyrDef = new DBFeatureType();
229
////                                lyrDef.setName(tableName);
230
//                                String[] tokens = tableName.split("\\u002E", 2);
231
//                                String schema = null;
232
//                                String tableName;
233
//                                if (tokens.length > 1) {
234
//                                        schema = tokens[0];
235
//                                        tableName = tokens[1];
236
//                                } else {
237
//                                        tableName = tokens[0];
238
//                                }
239
//                                lyrDef.setSchema(schema);
240
//                                lyrDef.setTableName(tableName);
241
//                                String fidField = ids_ci[0].toString();
242
//                                String geomField = geos_ci[0].toString();
243
//                                lyrDef.setFieldGeometry(geomField);
244
//                                lyrDef.setFieldID(fidField);
245
//                                String strEPSG = espView;
246
//                                lyrDef.setSRID_EPSG(strEPSG);
247
//                                ((ICanReproject) driver).setDestProjection(strEPSG);
248
//
249
//                                proj = CRSFactory.getCRS("EPSG:"
250
//                                                + ((ICanReproject) driver).getSourceProjection(conn,
251
//                                                                lyrDef));
252
                                ArrayList list=new ArrayList(1);
253
                                list.add(espView);
254
                                FeatureType ft=null;
255
                                try {
256
                                        ft = explorer.getFeatureTypes(parameters)[0];
257
                                } catch (ReadException e) {
258
                                        // TODO Auto-generated catch block
259
                                        e.printStackTrace();
260
                                }
261
                                ft.setAllSRS(list);
262
                        } catch (Exception e) {
263
                                NotificationManager.addInfo("Incorrect projection", e);
264
                        }
265
//                }
266
                return getJPanelProj(proj);
267
        }
268
    private CRSSelectPanel getJPanelProj(IProjection proj) {
269
//                if (jPanelProj == null) {
270
//                        IProjection proj = CRSFactory.getCRS("EPSG:23030");
271
//                        if (PluginServices.getMainFrame() != null) {
272
//                                proj = FOpenDialog.getLastProjection();
273
//                        }
274
                        jPanelProj = CRSSelectPanel.getPanel(proj);
275

    
276
                        jPanelProj.setTransPanelActive(true);
277
                        jPanelProj.setBounds(new java.awt.Rectangle(210, 22, 280, 32));
278
                        jPanelProj.setPreferredSize(new java.awt.Dimension(448,35));
279
                        jPanelProj.addActionListener(new java.awt.event.ActionListener() {
280
                                public void actionPerformed(java.awt.event.ActionEvent e) {
281
                                if (jPanelProj.isOkPressed()) {
282
                                        AddLayerDialog.setLastProjection(jPanelProj.getCurProj());
283
                                }
284
                                }
285
                        });
286

    
287
//                }
288

    
289
                return jPanelProj;
290
        }
291
    public DBParameters getParameters(){
292
            return parameters;
293
    }
294
}