Statistics
| Revision:

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

History | View | Annotate | Download (7.93 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 com.hardcode.driverManager.Driver;
46
import com.iver.andami.PluginServices;
47
import com.iver.andami.messages.NotificationManager;
48
import com.iver.cit.gvsig.fmap.MapControl;
49
import com.iver.cit.gvsig.fmap.core.ICanReproject;
50
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
51
import com.iver.cit.gvsig.fmap.drivers.DBException;
52
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
53
import com.iver.cit.gvsig.fmap.drivers.IConnection;
54
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
55
import com.iver.cit.gvsig.gui.panels.CRSSelectPanel;
56
import com.iver.cit.gvsig.project.documents.gui.FOpenDialog;
57

    
58
import javax.swing.JCheckBox;
59
import javax.swing.JOptionPane;
60

    
61
import org.cresques.cts.IProjection;
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 IVectorialDatabaseDriver driver = 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

    
81
    public TablesListItem(String name, IVectorialDatabaseDriver drv, IConnection _conn,
82
        MapControl _mc, WizardVectorialDB _parent) {
83
        tableName = name;
84
        setText(name);
85
        driver = drv;
86
        conn = _conn;
87
        mc = _mc;
88
        parent = _parent;
89
    }
90

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

    
97
    public boolean isActivated() {
98
        return activated;
99
    }
100

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

    
114
    private boolean hasValidValues() {
115
        return tableSettingsPanel.hasValidValues();
116
    }
117

    
118
    public String toString() {
119
        return tableName;
120
    }
121

    
122
    public String getTableName() {
123
        return tableName;
124
    }
125

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

    
131
    public UserSelectedFieldsPanel getUserSelectedFieldsPanel()
132
        throws DBException {
133
        if (selectedFieldsPanel == null) {
134
            String[] allf = driver.getAllFields(conn, tableName);
135
            String[] allt = driver.getAllFieldTypeNames(conn, tableName);
136
            selectedFieldsPanel = new UserSelectedFieldsPanel(allf, allt, true,
137
                    parent);
138
        }
139

    
140
        return selectedFieldsPanel;
141
    }
142

    
143
    public UserTableSettingsPanel getUserTableSettingsPanel(String espView)
144
        throws DBException {
145
        if (tableSettingsPanel == null) {
146

    
147
                String[] ids = new String[0];
148
                try {
149
                        ids = driver.getIdFieldsCandidates(conn, tableName);
150
                } catch (DBException se) {
151
                        String msg = PluginServices.getText(this, "id_not_available") + " " + tableName
152
                        + ":\n" + se.getMessage();
153
                        String title = PluginServices.getText(this, "id_error");
154
                        JOptionPane.showMessageDialog(parent, msg, title, JOptionPane.ERROR_MESSAGE);
155
                        setSelected(false);
156
                }
157

    
158
                String[] geos = new String[0];
159
                try {
160
                        geos = driver.getGeometryFieldsCandidates(conn, tableName);
161
                } catch (DBException se) {
162
                        String msg = PluginServices.getText(this, "geo_field_not_available")
163
                        + ":\n" + PluginServices.getText(this, se.getMessage()) + ": " + tableName;
164
                        String title = PluginServices.getText(this, "geo_field_error");
165
                        JOptionPane.showMessageDialog(parent, msg, title, JOptionPane.ERROR_MESSAGE);
166
                        setSelected(false);
167
                }
168

    
169
            int ids_size = ids.length;
170
            FieldComboItem[] ids_ci = new FieldComboItem[ids_size];
171

    
172
            for (int i = 0; i < ids_size; i++)
173
                ids_ci[i] = new FieldComboItem(ids[i]);
174

    
175
            int geos_size = geos.length;
176
            FieldComboItem[] geos_ci = new FieldComboItem[geos_size];
177

    
178
            for (int i = 0; i < geos_size; i++)
179
                geos_ci[i] = new FieldComboItem(geos[i]);
180

    
181
            tableSettingsPanel = new UserTableSettingsPanel(ids_ci, geos_ci,
182
                    tableName, mc, true, parent, projectionPanel(espView,ids_ci,geos_ci));
183
        }
184

    
185
        return tableSettingsPanel;
186
    }
187
    public CRSSelectPanel projectionPanel(String espView,
188
                        FieldComboItem[] ids_ci, FieldComboItem[] geos_ci) {
189
                IProjection proj = FOpenDialog.getLastProjection();
190
                if (driver instanceof ICanReproject) {
191
                        try {
192
                                DBLayerDefinition lyrDef = new DBLayerDefinition();
193
                                lyrDef.setName(tableName);
194
                                String[] tokens = tableName.split("\\u002E", 2);
195
                                String schema = null;
196
                                String tableName;
197
                                if (tokens.length > 1) {
198
                                        schema = tokens[0];
199
                                        tableName = tokens[1];
200
                                } else {
201
                                        tableName = tokens[0];
202
                                }
203
                                lyrDef.setSchema(schema);
204
                                lyrDef.setTableName(tableName);
205
                                String fidField = ids_ci[0].toString();
206
                                String geomField = geos_ci[0].toString();
207
                                lyrDef.setFieldGeometry(geomField);
208
                                lyrDef.setFieldID(fidField);
209
                                String strEPSG = espView;
210
                                lyrDef.setSRID_EPSG(strEPSG);
211
                                ((ICanReproject) driver).setDestProjection(strEPSG);
212

    
213
                                proj = CRSFactory.getCRS("EPSG:"
214
                                                + ((ICanReproject) driver).getSourceProjection(conn,
215
                                                                lyrDef));
216
                        } catch (Exception e) {
217
                                NotificationManager.addInfo("Incorrect projection", e);
218
                        }
219
                }
220
                return getJPanelProj(proj);
221
        }
222
    private CRSSelectPanel getJPanelProj(IProjection proj) {
223
//                if (jPanelProj == null) {
224
//                        IProjection proj = CRSFactory.getCRS("EPSG:23030");
225
//                        if (PluginServices.getMainFrame() != null) {
226
//                                proj = FOpenDialog.getLastProjection();
227
//                        }
228
                        jPanelProj = CRSSelectPanel.getPanel(proj);
229

    
230
                        jPanelProj.setTransPanelActive(true);
231
                        jPanelProj.setBounds(new java.awt.Rectangle(230, 22, 260, 32));
232
                        jPanelProj.setPreferredSize(new java.awt.Dimension(448,35));
233
                        jPanelProj.addActionListener(new java.awt.event.ActionListener() {
234
                                public void actionPerformed(java.awt.event.ActionEvent e) {
235
                                if (jPanelProj.isOkPressed()) {
236
                                        FOpenDialog.setLastProjection(jPanelProj.getCurProj());
237
                                }
238
                                }
239
                        });
240

    
241
//                }
242

    
243
                return jPanelProj;
244
        }
245
//    public IConnection getConnection() {
246
//            return conn;
247
//    }
248
}