Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / TablesListItem.java @ 43891

History | View | Annotate | Download (6.95 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geodb.vectorialdb.wizard;
25

    
26
import java.util.ArrayList;
27
import java.util.Iterator;
28

    
29
import javax.swing.JCheckBox;
30

    
31
import org.cresques.cts.IProjection;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.app.addlayer.AddLayerDialog;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.FeatureType;
40
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
41
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
42

    
43

    
44

    
45
/**
46
 * Utility class that represents a table list item as a selectable check box.
47
 *
48
 * @author jldominguez
49
 *
50
 */
51
public class TablesListItem extends JCheckBox implements Comparable<TablesListItem> {
52
    
53
    private static Logger logger = LoggerFactory.getLogger(TablesListItem.class);
54
    
55
    protected String tableName = "";
56
    protected String schemaName = "";
57
    
58
    private UserSelectedFieldsPanel selectedFieldsPanel = null;
59
    protected WizardDB parent = null;
60
    private boolean activated = false;
61
    // private CRSSelectPanel jPanelProj;
62
        protected DBStoreParameters parameters;
63
        protected UserTableSettingsPanel tableSettingsPanel = null;
64

    
65
    public TablesListItem(DBStoreParameters param,
66
                        WizardDB _parent) {
67
        
68
        tableName = param.getTable();
69
        schemaName = getSchema(param);
70
        
71
        setText(toString());
72
        this.parameters = param;
73
        parent = _parent;
74
    }
75
    
76

    
77

    
78
    /**
79
     * @param param
80
     * @return
81
     */
82
    private String getSchema(DBStoreParameters dynobj) {
83
        
84
        String resp = null;
85
        try {
86
            resp = (String) dynobj.getDynValue(
87
                JDBCConnectionParameters.SCHEMA_PARAMTER_NAME);
88
        } catch (Exception ex) {
89
            // logger.info("did not find schema in DB parameters.");
90
        }
91
        return resp;
92
    }
93

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

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

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

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

    
121
    public String toString() {
122
        if (schemaName == null || schemaName.length() == 0) {
123
            return tableName;
124
        } else {
125
            return schemaName + "." + tableName;
126
        }
127
        
128
    }
129

    
130
    public String getTableName() {
131
        return tableName;
132
    }
133

    
134
    public void setEnabledPanels(boolean b) {
135
        selectedFieldsPanel.enableControls(b);
136
        tableSettingsPanel.enableAlphaControls(b);
137
        tableSettingsPanel.enableSpatialControls(b);
138

    
139
    }
140

    
141
    public UserSelectedFieldsPanel getUserSelectedFieldsPanel() {
142
        if (selectedFieldsPanel == null) {
143
                FeatureType ft=null;
144
                        try {
145
                                ft = this.parent.getServerExplorer().getFeatureType(parameters);
146
                        } catch (DataException e) {
147
                                NotificationManager.addError(e);
148
                                return null;
149
                        }
150
                        ArrayList<FeatureAttributeDescriptor> attList = new ArrayList<FeatureAttributeDescriptor>();
151

    
152
                        Iterator<FeatureAttributeDescriptor> iter = ft.iterator();
153
                        while (iter.hasNext()) {
154
                                attList.add(iter.next());
155
                        }
156

    
157
                FeatureAttributeDescriptor[] allf = attList
158
                                        .toArray(new FeatureAttributeDescriptor[0]);
159

    
160

    
161
            selectedFieldsPanel = new UserSelectedFieldsPanel(allf, false,
162
                    parent);
163
        }
164

    
165
        return selectedFieldsPanel;
166
    }
167

    
168
    public UserTableSettingsPanel getUserTableSettingsPanel() {
169
                if (tableSettingsPanel == null) {
170

    
171
                        String[] ids = new String[0];
172
                        FeatureType ft = null;
173

    
174
                        try {
175
                                ft = this.parent.getServerExplorer().getFeatureType(parameters);
176
                        } catch (DataException e) {
177
                                NotificationManager.addError(e);
178
                                return null;
179
                        }
180

    
181
                        ArrayList auxAll = new ArrayList();
182
                        ArrayList auxId = new ArrayList();
183
                        Iterator iter = ft.iterator();
184
                        while (iter.hasNext()) {
185
                                FeatureAttributeDescriptor dbattr = (FeatureAttributeDescriptor) iter
186
                                                .next();
187
                                if (dbattr.isPrimaryKey()) {
188
                                        auxId.add(dbattr.getName());
189
                                }
190
                                auxAll.add(dbattr.getName());
191

    
192
                        }
193

    
194
                        if (auxId.size() > 0) {
195
                                StringBuilder strb = new StringBuilder();
196
                                strb.append('{');
197
                                for (int i = 0; i < auxId.size()-1; i++) {
198
                                        strb.append(auxId.get(i));
199
                                        strb.append(',');
200
                                }
201
                                strb.append(auxId.get(auxId.size() - 1));
202

    
203
                                strb.append('}');
204
                                if (auxId.size() == 1) {
205
                                        auxAll.remove(auxId.get(0));
206
                                }
207
                                auxAll.add(0, strb.toString());
208
                        }
209
                        ids = (String[]) auxAll.toArray(new String[auxAll.size()]);
210
                        int ids_size = ids.length;
211
                        FieldComboItem[] ids_ci = new FieldComboItem[ids_size];
212

    
213
                        for (int i = 0; i < ids_size; i++) {
214
                                ids_ci[i] = new FieldComboItem(ids[i]);
215
                        }
216

    
217

    
218

    
219
                        tableSettingsPanel = new UserTableSettingsPanel(ids_ci, tableName,
220
                                        true, parent, getParameters());
221
                }
222

    
223
                return tableSettingsPanel;
224
        }
225

    
226

    
227
//    public IProjection currentProjection(String espView,
228
//                        FieldComboItem[] ids_ci, FieldComboItem[] geos_ci) {
229
//                IProjection proj = AddLayerDialog.getLastProjection();
230
//                try {
231
//                        ArrayList list = new ArrayList(1);
232
//                        list.add(espView);
233
//                        FeatureType ft = null;
234
//                        try {
235
//                                ft = this.parent.getServerExplorer().getFeatureType(parameters);
236
//                        } catch (DataException e) {
237
//                                // TODO Auto-generated catch block
238
//                                e.printStackTrace();
239
//                        }
240
//                } catch (Exception e) {
241
//                        NotificationManager.addInfo("Incorrect projection", e);
242
//                }
243
//                return proj;
244
//        }
245

    
246
    public DBStoreParameters getParameters() {
247
            return parameters;
248
    }
249

    
250
    @Override
251
    public int compareTo(TablesListItem o) {
252
        return this.toString().compareToIgnoreCase(o.toString());
253
    }
254
}