Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / daltransform / gui / SelectDataStoreWizard.java @ 28833

History | View | Annotate | Download (6.17 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {Iver T.I.}   {Task}
26
 */
27

    
28
package org.gvsig.app.daltransform.gui;
29

    
30
import java.util.ArrayList;
31

    
32
import javax.swing.DefaultListModel;
33
import javax.swing.JList;
34
import javax.swing.JScrollPane;
35

    
36
import org.gvsig.fmap.dal.exception.ReadException;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.mapcontext.layers.FLayer;
39
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
40
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
41
import org.gvsig.project.document.table.FeatureTableDocument;
42
import org.gvsig.project.document.table.FeatureTableDocumentFactory;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
import com.iver.andami.PluginServices;
47
import com.iver.andami.ui.mdiManager.IWindow;
48
import com.iver.cit.gvsig.ProjectExtension;
49
import com.iver.cit.gvsig.project.documents.ProjectDocument;
50
import com.iver.cit.gvsig.project.documents.view.gui.IView;
51

    
52
/**
53
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
54
 */
55
public class SelectDataStoreWizard extends FeatureTransformWizard{
56
        private static final long serialVersionUID = -1841990357325903449L;
57
        private static final Logger logger = LoggerFactory.getLogger(SelectDataStoreWizard.class);
58
        private JList dataStoreList;
59
        private JScrollPane dataStoreScrollPane;
60

    
61
        /**
62
         * @param wizardComponents
63
         */
64
        public SelectDataStoreWizard(FeatureTransformWizardModel featureTransformWizardModel) {
65
                super(featureTransformWizardModel);
66
                initComponents();
67
                initLabels();
68
                addDataStores();
69
        }
70

    
71
        private void initLabels(){
72
                setBorder(javax.swing.BorderFactory.createTitledBorder(PluginServices.getText(this, "transform_datastore_selection")));
73
        }
74

    
75
        private void initComponents() {
76
                java.awt.GridBagConstraints gridBagConstraints;
77

    
78
                dataStoreScrollPane = new javax.swing.JScrollPane();
79
                dataStoreList = new javax.swing.JList();
80

    
81
                setLayout(new java.awt.GridBagLayout());
82

    
83
                dataStoreScrollPane.setViewportView(dataStoreList);
84

    
85
                dataStoreList.setModel(new DefaultListModel());
86

    
87
                gridBagConstraints = new java.awt.GridBagConstraints();
88
                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
89
                gridBagConstraints.weightx = 1.0;
90
                gridBagConstraints.weighty = 1.0;
91
                gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
92
                add(dataStoreScrollPane, gridBagConstraints);
93
        }
94

    
95
        /**
96
         * Adding the objects
97
         */
98
        private void addDataStores(){
99
                //Add all the tables
100
                ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
101
                ArrayList<ProjectDocument> tables = ext.getProject().getDocumentsByType(FeatureTableDocumentFactory.registerName);
102
                for (int i=0 ; i<tables.size() ; i++){
103
                        FeatureTableDocument table = (FeatureTableDocument)tables.get(i);
104
                        ((DefaultListModel)dataStoreList.getModel()).addElement(                                        
105
                                        new FeatureStoreCombo(table.getName(),
106
                                                        table.getStore(),
107
                                                        false));
108
                }
109
                //Add the layers from the current view
110
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
111
                if (window instanceof IView){
112
                        IView view = (IView)window;
113
                        LayersIterator it = new LayersIterator(
114
                                        view.getMapControl().getMapContext().getLayers());
115
                        while(it.hasNext()){
116
                                FLayer layer = it.nextLayer();
117
                                if (layer instanceof FLyrVect){
118
                                        try {
119
                                                FLyrVect layerVect = (FLyrVect)layer;
120
                                                FeatureStore featureStore = layerVect.getFeatureStore();
121
                                                boolean found = false;
122
                                                for (int i=0 ; i<tables.size() ; i++){
123
                                                        FeatureTableDocument table = (FeatureTableDocument)tables.get(i);
124
                                                        if (table.getStore().equals(featureStore)) {
125
                                                                found = true;
126
                                                        }                                                        
127
                                                }
128
                                                if (!found){
129
                                                        ((DefaultListModel)dataStoreList.getModel()).addElement(
130
                                                                        new FeatureStoreCombo(layerVect.getName(),
131
                                                                                        featureStore,
132
                                                                                        true));
133
                                                }
134
                                        } catch (ReadException e) {
135
                                                logger.error("It is not possible to read the FeatureStore", e);
136
                                        }
137
                                }
138
                        }
139
                }        
140
        }
141

    
142
        /* (non-Javadoc)
143
         * @see jwizardcomponent.JWizardPanel#update()
144
         */        
145
        public void update() {
146
                if (dataStoreList.getModel().getSize() > 0){
147
                        dataStoreList.setSelectedIndex(0);
148
                }else{
149
                        getWizardComponents().getNextButton().setEnabled(false);
150
                }
151
        }
152

    
153

    
154
        /**
155
         * @return the selected feature store
156
         */
157
        public FeatureStore getSelectedFeatureStore(){
158
                Object obj = dataStoreList.getSelectedValue();
159
                if (obj != null){
160
                        return ((FeatureStoreCombo)obj).getFeatureStore();
161
                }
162
                return null;
163
        }
164

    
165
        /**
166
         * @return the selected feature store
167
         */
168
        public boolean isSelectedFeatureStoreLoaded(){
169
                Object obj = dataStoreList.getSelectedValue();
170
                if (obj != null){
171
                        return ((FeatureStoreCombo)obj).isLoaded();
172
                }
173
                return false;
174
        }
175

    
176
        /**
177
         * Used to fill the combo
178
         * @author jpiera
179
         */
180
        private class FeatureStoreCombo{
181
                private FeatureStore featureStore = null;
182
                private String name = null;
183
                private boolean isLoaded = false;
184

    
185
                public FeatureStoreCombo(String name, FeatureStore featureStore, boolean isLoaded) {
186
                        super();
187
                        this.name = name;
188
                        this.featureStore = featureStore;
189
                        this.isLoaded = isLoaded;
190
                }
191

    
192
                /**
193
                 * @return the isLoaded
194
                 */
195
                public boolean isLoaded() {
196
                        return isLoaded;
197
                }
198

    
199
                /**
200
                 * @return the featureStore
201
                 */
202
                public FeatureStore getFeatureStore() {
203
                        return featureStore;
204
                }
205

    
206
                /* (non-Javadoc)
207
                 * @see java.lang.Object#toString()
208
                 */                
209
                public String toString() {                        
210
                        return name;
211
                }                
212
        }
213
}
214