Statistics
| Revision:

gvsig-raster / org.gvsig.raster.postgis / trunk / org.gvsig.raster.postgis / org.gvsig.raster.postgis.app / org.gvsig.raster.postgis.app.postgisrasterclient / src / main / java / org / gvsig / raster / postgis / app / postgisrasterclient / gui / TablesListItem.java @ 877

History | View | Annotate | Download (6.77 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.raster.postgis.app.postgisrasterclient.gui;
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.andami.messages.NotificationManager;
52
import org.gvsig.app.addlayer.AddLayerDialog;
53
import org.gvsig.app.gui.panels.CRSSelectPanel;
54
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
56
import org.gvsig.fmap.dal.feature.FeatureType;
57
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer;
58
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
59
import org.gvsig.geodb.vectorialdb.wizard.UserSelectedFieldsPanel;
60
import org.gvsig.geodb.vectorialdb.wizard.UserTableSettingsPanel;
61
import org.gvsig.geodb.vectorialdb.wizard.WizardDB;
62

    
63

    
64
/**
65
 * Utility class that represents a table list item as a selectable check box.
66
 */
67
@SuppressWarnings("serial")
68
public class TablesListItem extends JCheckBox {
69
    protected String                 tableName           = "";
70
    private UserSelectedFieldsPanel  selectedFieldsPanel = null;
71
    protected WizardDB               parent              = null;
72
    private boolean                  activated           = false;
73
        protected DBStoreParameters      parameters          = null;
74
        protected DBServerExplorer       explorer            = null;
75
        protected UserTableSettingsPanel tableSettingsPanel  = null;
76

    
77
    public TablesListItem(DBServerExplorer explorer,
78
                        DBStoreParameters param,
79
                        WizardDB _parent) {
80
        tableName = param.getTable();
81
        setText(tableName);
82
        this.parameters = param;
83
        this.explorer=explorer;
84
        parent = _parent;
85
    }
86

    
87
    public void activate() {
88
        activated = true;
89
        selectedFieldsPanel.loadValues();
90
        tableSettingsPanel.loadValues();
91
    }
92

    
93
    public boolean isActivated() {
94
        return activated;
95
    }
96

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

    
110
    private boolean hasValidValues() {
111
        return tableSettingsPanel.hasValidValues();
112
    }
113

    
114
    public String toString() {
115
        return tableName;
116
    }
117

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

    
122
    public void setEnabledPanels(boolean b) {
123
        selectedFieldsPanel.enableControls(b);
124
        tableSettingsPanel.enableAlphaControls(b);
125
        tableSettingsPanel.enableSpatialControls(b);
126

    
127
    }
128

    
129
    @SuppressWarnings("unchecked")
130
        public UserSelectedFieldsPanel getUserSelectedFieldsPanel() {
131
        if (selectedFieldsPanel == null) {
132
                FeatureType ft=null;
133
                        try {
134
                                ft = explorer.getFeatureType(parameters);
135
                        } catch (DataException e) {
136
                                NotificationManager.addError(e);
137
                                return null;
138
                        }
139
                        ArrayList<FeatureAttributeDescriptor> attList = new ArrayList<FeatureAttributeDescriptor>();
140

    
141
                        Iterator<FeatureAttributeDescriptor> iter = ft.iterator();
142
                        while (iter.hasNext()) {
143
                                attList.add(iter.next());
144
                        }
145

    
146
                FeatureAttributeDescriptor[] allf = attList
147
                                        .toArray(new FeatureAttributeDescriptor[0]);
148

    
149

    
150
            selectedFieldsPanel = new UserSelectedFieldsPanel(allf, false,
151
                    parent);
152
        }
153

    
154
        return selectedFieldsPanel;
155
    }
156

    
157
    @SuppressWarnings("unchecked")
158
        public UserTableSettingsPanel getUserTableSettingsPanel() {
159
                if (tableSettingsPanel == null) {
160

    
161
                        String[] ids = new String[0];
162
                        FeatureType ft = null;
163

    
164
                        try {
165
                                ft = explorer.getFeatureType(parameters);
166
                        } catch (DataException e) {
167
                                NotificationManager.addError(e);
168
                                return null;
169
                        }
170

    
171
                        ArrayList auxAll = new ArrayList();
172
                        ArrayList auxId = new ArrayList();
173
                        Iterator iter = ft.iterator();
174
                        while (iter.hasNext()) {
175
                                FeatureAttributeDescriptor dbattr = (FeatureAttributeDescriptor) iter
176
                                                .next();
177
                                if (dbattr.isPrimaryKey()) {
178
                                        auxId.add(dbattr.getName());
179
                                }
180
                                auxAll.add(dbattr.getName());
181

    
182
                        }
183

    
184
                        if (auxId.size() > 0) {
185
                                StringBuilder strb = new StringBuilder();
186
                                strb.append('{');
187
                                for (int i = 0; i < auxId.size()-1; i++) {
188
                                        strb.append(((FeatureAttributeDescriptor) auxId.get(i))
189
                                                        .getName());
190
                                        strb.append(',');
191
                                }
192
                                strb.append(auxId.get(auxId.size() - 1));
193

    
194
                                strb.append('}');
195
                                if (auxId.size() == 1) {
196
                                        auxAll.remove(auxId.get(0));
197
                                }
198
                                auxAll.add(0, strb.toString());
199
                        }
200
                        ids = (String[]) auxAll.toArray(new String[auxAll.size()]);
201
                        int ids_size = ids.length;
202
                        FieldComboItem[] ids_ci = new FieldComboItem[ids_size];
203

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

    
208

    
209

    
210
                        tableSettingsPanel = new UserTableSettingsPanel(ids_ci, tableName,
211
                                        true, parent, getParameters());
212
                }
213

    
214
                return tableSettingsPanel;
215
        }
216

    
217

    
218
    @SuppressWarnings("unchecked")
219
        public IProjection currentProjection(String espView,
220
                        FieldComboItem[] ids_ci, FieldComboItem[] geos_ci) {
221
                IProjection proj = AddLayerDialog.getLastProjection();
222
                try {
223
                        ArrayList list = new ArrayList(1);
224
                        list.add(espView);
225
                        try {
226
                                explorer.getFeatureType(parameters);
227
                        } catch (DataException e) {
228
                                // TODO Auto-generated catch block
229
                                e.printStackTrace();
230
                        }
231
                } catch (Exception e) {
232
                        NotificationManager.addInfo("Incorrect projection", e);
233
                }
234
                return proj;
235
        }
236

    
237
    public DBStoreParameters getParameters() {
238
            return parameters;
239
    }
240
}