Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / TablesListItem.java @ 39486

History | View | Annotate | Download (7.19 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 org.gvsig.geodb.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.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
import org.gvsig.andami.messages.NotificationManager;
55
import org.gvsig.app.addlayer.AddLayerDialog;
56
import org.gvsig.fmap.dal.exception.DataException;
57
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
58
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer;
60
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
61
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
62

    
63

    
64

    
65
/**
66
 * Utility class that represents a table list item as a selectable check box.
67
 *
68
 * @author jldominguez
69
 *
70
 */
71
public class TablesListItem extends JCheckBox {
72
    
73
    private static Logger logger = LoggerFactory.getLogger(TablesListItem.class);
74
    
75
    protected String tableName = "";
76
    protected String schemaName = "";
77
    
78
    private UserSelectedFieldsPanel selectedFieldsPanel = null;
79
    protected WizardDB parent = null;
80
    private boolean activated = false;
81
    // private CRSSelectPanel jPanelProj;
82
        protected DBStoreParameters parameters;
83
        protected DBServerExplorer explorer;
84
        protected UserTableSettingsPanel tableSettingsPanel = null;
85

    
86
    public TablesListItem(DBServerExplorer explorer,
87
                        DBStoreParameters param,
88
                        WizardDB _parent) {
89
        
90
        tableName = param.getTable();
91
        schemaName = getSchema(param);
92
        
93
        setText(toString());
94
        this.parameters = param;
95
        this.explorer=explorer;
96
        parent = _parent;
97
    }
98
    
99

    
100

    
101
    /**
102
     * @param param
103
     * @return
104
     */
105
    private String getSchema(DBStoreParameters dynobj) {
106
        
107
        String resp = null;
108
        try {
109
            resp = (String) dynobj.getDynValue(
110
                JDBCConnectionParameters.SCHEMA_PARAMTER_NAME);
111
        } catch (Exception ex) {
112
            // logger.info("did not find schema in DB parameters.");
113
        }
114
        return resp;
115
    }
116

    
117
    public void activate() {
118
        activated = true;
119
        selectedFieldsPanel.loadValues();
120
        tableSettingsPanel.loadValues();
121
    }
122

    
123
    public boolean isActivated() {
124
        return activated;
125
    }
126

    
127
    /**
128
     * Tells whether this item prevents the wizard from being in a valid final state.
129
     * @return whether this item prevents the wizard from being in a valid final state.
130
     */
131
    public boolean disturbsWizardValidity() {
132
        if (isSelected()) {
133
            return (!hasValidValues());
134
        }
135
        else {
136
            return false;
137
        }
138
    }
139

    
140
    private boolean hasValidValues() {
141
        return tableSettingsPanel.hasValidValues();
142
    }
143

    
144
    public String toString() {
145
        if (schemaName == null || schemaName.length() == 0) {
146
            return tableName;
147
        } else {
148
            return schemaName + "." + tableName;
149
        }
150
        
151
    }
152

    
153
    public String getTableName() {
154
        return tableName;
155
    }
156

    
157
    public void setEnabledPanels(boolean b) {
158
        selectedFieldsPanel.enableControls(b);
159
        tableSettingsPanel.enableAlphaControls(b);
160
        tableSettingsPanel.enableSpatialControls(b);
161

    
162
    }
163

    
164
    public UserSelectedFieldsPanel getUserSelectedFieldsPanel() {
165
        if (selectedFieldsPanel == null) {
166
                FeatureType ft=null;
167
                        try {
168
                                ft = explorer.getFeatureType(parameters);
169
                        } catch (DataException e) {
170
                                NotificationManager.addError(e);
171
                                return null;
172
                        }
173
                        ArrayList<FeatureAttributeDescriptor> attList = new ArrayList<FeatureAttributeDescriptor>();
174

    
175
                        Iterator<FeatureAttributeDescriptor> iter = ft.iterator();
176
                        while (iter.hasNext()) {
177
                                attList.add(iter.next());
178
                        }
179

    
180
                FeatureAttributeDescriptor[] allf = attList
181
                                        .toArray(new FeatureAttributeDescriptor[0]);
182

    
183

    
184
            selectedFieldsPanel = new UserSelectedFieldsPanel(allf, false,
185
                    parent);
186
        }
187

    
188
        return selectedFieldsPanel;
189
    }
190

    
191
    public UserTableSettingsPanel getUserTableSettingsPanel() {
192
                if (tableSettingsPanel == null) {
193

    
194
                        String[] ids = new String[0];
195
                        FeatureType ft = null;
196

    
197
                        try {
198
                                ft = explorer.getFeatureType(parameters);
199
                        } catch (DataException e) {
200
                                NotificationManager.addError(e);
201
                                return null;
202
                        }
203

    
204
                        ArrayList auxAll = new ArrayList();
205
                        ArrayList auxId = new ArrayList();
206
                        Iterator iter = ft.iterator();
207
                        while (iter.hasNext()) {
208
                                FeatureAttributeDescriptor dbattr = (FeatureAttributeDescriptor) iter
209
                                                .next();
210
                                if (dbattr.isPrimaryKey()) {
211
                                        auxId.add(dbattr.getName());
212
                                }
213
                                auxAll.add(dbattr.getName());
214

    
215
                        }
216

    
217
                        if (auxId.size() > 0) {
218
                                StringBuilder strb = new StringBuilder();
219
                                strb.append('{');
220
                                for (int i = 0; i < auxId.size()-1; i++) {
221
                                        strb.append(auxId.get(i));
222
                                        strb.append(',');
223
                                }
224
                                strb.append(auxId.get(auxId.size() - 1));
225

    
226
                                strb.append('}');
227
                                if (auxId.size() == 1) {
228
                                        auxAll.remove(auxId.get(0));
229
                                }
230
                                auxAll.add(0, strb.toString());
231
                        }
232
                        ids = (String[]) auxAll.toArray(new String[auxAll.size()]);
233
                        int ids_size = ids.length;
234
                        FieldComboItem[] ids_ci = new FieldComboItem[ids_size];
235

    
236
                        for (int i = 0; i < ids_size; i++) {
237
                                ids_ci[i] = new FieldComboItem(ids[i]);
238
                        }
239

    
240

    
241

    
242
                        tableSettingsPanel = new UserTableSettingsPanel(ids_ci, tableName,
243
                                        true, parent, getParameters());
244
                }
245

    
246
                return tableSettingsPanel;
247
        }
248

    
249

    
250
    public IProjection currentProjection(String espView,
251
                        FieldComboItem[] ids_ci, FieldComboItem[] geos_ci) {
252
                IProjection proj = AddLayerDialog.getLastProjection();
253
                try {
254
                        ArrayList list = new ArrayList(1);
255
                        list.add(espView);
256
                        FeatureType ft = null;
257
                        try {
258
                                ft = explorer.getFeatureType(parameters);
259
                        } catch (DataException e) {
260
                                // TODO Auto-generated catch block
261
                                e.printStackTrace();
262
                        }
263
                } catch (Exception e) {
264
                        NotificationManager.addInfo("Incorrect projection", e);
265
                }
266
                return proj;
267
        }
268

    
269
    public DBStoreParameters getParameters() {
270
            return parameters;
271
    }
272
}