Statistics
| Revision:

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

History | View | Annotate | Download (10.1 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.awt.event.ItemEvent;
31
import java.awt.event.ItemListener;
32
import java.util.ArrayList;
33

    
34
import javax.swing.DefaultListModel;
35
import javax.swing.JCheckBox;
36
import javax.swing.JLabel;
37
import javax.swing.JList;
38
import javax.swing.JPanel;
39
import javax.swing.JScrollPane;
40
import javax.swing.JTextArea;
41

    
42
import jwizardcomponent.JWizardComponents;
43

    
44
import org.gvsig.fmap.dal.DataTypes;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.dal.feature.FeatureStoreTransform;
49
import org.gvsig.fmap.dal.feature.FeatureType;
50
import org.gvsig.fmap.mapcontext.MapContext;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
import com.iver.andami.PluginServices;
55
import com.iver.andami.ui.mdiManager.IWindow;
56
import com.iver.cit.gvsig.ProjectExtension;
57
import com.iver.cit.gvsig.project.documents.ProjectDocument;
58
import com.iver.cit.gvsig.project.documents.view.ProjectViewBase;
59
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
60
import com.iver.cit.gvsig.project.documents.view.gui.IView;
61

    
62
/**
63
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
64
 */
65
public class LoadLayerWizard  extends FeatureTransformWizard implements ItemListener{
66
        private static final Logger logger = LoggerFactory.getLogger(LoadLayerWizard.class);
67
        private JCheckBox loadLayerCb = null;
68
        private JScrollPane messageScroll = null;
69
        private JTextArea messageTextArea = null;
70
        private JPanel northPanel = null;
71
        private JLabel selectViewLabel;
72
        private JList selectViewList;
73
        private JScrollPane selectViewScroll;
74
    private JPanel centerPanel;
75
        private FeatureStoreTransform transform = null;
76

    
77
        /**
78
         * @param wizardComponents
79
         */
80
        public LoadLayerWizard(FeatureTransformWizardModel featureTransformWizardModel) {
81
                super(featureTransformWizardModel);        
82
                initComponents();
83
                initLabels();        
84
                addViews();
85
                loadLayerCb.addItemListener(this);
86
                itemStateChanged(null);
87
        }
88

    
89
        private void initLabels(){
90
                loadLayerCb.setText(PluginServices.getText(this, "transform_load_layer_query"));
91
                selectViewLabel.setText(PluginServices.getText(this, "transform_view_to_load"));
92
                setBorder(javax.swing.BorderFactory.createTitledBorder(PluginServices.getText(this, "transform_apply")));
93
        }
94

    
95
        private void initComponents() {
96
        java.awt.GridBagConstraints gridBagConstraints;
97

    
98
        northPanel = new javax.swing.JPanel();
99
        messageScroll = new javax.swing.JScrollPane();
100
        messageTextArea = new javax.swing.JTextArea();
101
        loadLayerCb = new javax.swing.JCheckBox();
102
        selectViewLabel = new javax.swing.JLabel();
103
        centerPanel = new javax.swing.JPanel();
104
        selectViewScroll = new javax.swing.JScrollPane();
105
        selectViewList = new javax.swing.JList();
106
        selectViewList.setModel(new DefaultListModel());
107

    
108
        setLayout(new java.awt.BorderLayout());
109

    
110
        northPanel.setLayout(new java.awt.GridBagLayout());
111

    
112
        messageScroll.setBorder(null);
113

    
114
        messageTextArea.setColumns(20);
115
        messageTextArea.setEditable(false);
116
        messageTextArea.setLineWrap(true);
117
        messageTextArea.setRows(5);
118
        messageTextArea.setBorder(null);
119
        messageScroll.setViewportView(messageTextArea);
120

    
121
        gridBagConstraints = new java.awt.GridBagConstraints();
122
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
123
        gridBagConstraints.weightx = 1.0;
124
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 5, 2);
125
        northPanel.add(messageScroll, gridBagConstraints);
126

    
127
        loadLayerCb.setText("jCheckBox1");
128
        gridBagConstraints = new java.awt.GridBagConstraints();
129
        gridBagConstraints.gridx = 0;
130
        gridBagConstraints.gridy = 1;
131
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
132
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
133
        gridBagConstraints.weightx = 1.0;
134
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 2);
135
        northPanel.add(loadLayerCb, gridBagConstraints);
136

    
137
        selectViewLabel.setText("jLabel1");
138
        gridBagConstraints = new java.awt.GridBagConstraints();
139
        gridBagConstraints.gridx = 0;
140
        gridBagConstraints.gridy = 2;
141
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
142
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
143
        gridBagConstraints.weightx = 1.0;
144
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 5, 2);
145
        northPanel.add(selectViewLabel, gridBagConstraints);
146

    
147
        add(northPanel, java.awt.BorderLayout.NORTH);
148

    
149
        centerPanel.setLayout(new java.awt.GridBagLayout());
150

    
151
        selectViewScroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
152
        selectViewScroll.setPreferredSize(null);
153

    
154
        selectViewScroll.setViewportView(selectViewList);
155

    
156
        gridBagConstraints = new java.awt.GridBagConstraints();
157
        gridBagConstraints.gridx = 0;
158
        gridBagConstraints.gridy = 0;
159
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
160
        gridBagConstraints.weightx = 1.0;
161
        gridBagConstraints.weighty = 1.0;
162
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 2);
163
        centerPanel.add(selectViewScroll, gridBagConstraints);
164

    
165
        add(centerPanel, java.awt.BorderLayout.CENTER);
166
        }
167

    
168
        /* (non-Javadoc)
169
         * @see jwizardcomponent.JWizardPanel#update()
170
         */        
171
        public void update() {
172
                JWizardComponents components = getWizardComponents();
173
                //Gets the selected transformation
174
                FeatureTransformGui featureTransformGui = getSelectedFeatureTransformGui();
175
                
176
                //Gets the selected FeatureStore
177
                FeatureStore featureStore = getSelectedFeatureStore();
178
                
179
                //Apply the transformation
180
                try {
181
                        transform = featureTransformGui.createFeatureStoreTransform(featureStore);
182
                        if (isSelectedFeatureStoreLoaded()){
183
                                updateGuiForLayer(transform);
184
                        }else{
185
                                updateGuiForTable(transform);
186
                        }
187
                } catch (DataException e) {
188
                        logger.error("Error creating the transformation", e);
189
                }                
190
        }
191

    
192
        /**
193
         * Update the form when the transform has been applied in
194
         * to a table
195
         * @param transform
196
         * @throws DataException
197
         */
198
        private void updateGuiForTable(FeatureStoreTransform transform) throws DataException{
199
                FeatureType featureType = transform.getDefaultFeatureType();
200
                FeatureAttributeDescriptor[] featureAttributeDescriptors = featureType.getAttributeDescriptors();
201
                boolean hasGeometry = false;
202
                for (int i=0 ; i<featureAttributeDescriptors.length ; i++){
203
                        if (featureAttributeDescriptors[i].getDataType() == DataTypes.GEOMETRY){
204
                                hasGeometry = true;
205
                        }
206
                }
207
                if (hasGeometry){
208
                        if (selectViewList.getModel().getSize() == 0){
209
                                messageTextArea.setText(PluginServices.getText(this, "transform_layout_not_view_to_load"));
210
                                setLoadLayerVisible(false);
211
                        }else{
212
                                messageTextArea.setText(PluginServices.getText(this, "transform_layout_geometry"));
213
                                setLoadLayerVisible(true);
214
                        }
215
                }else{
216
                        messageTextArea.setText(PluginServices.getText(this, "transform_layout_no_geometry"));
217
                        setLoadLayerVisible(false);
218
                }                
219
        }
220

    
221
        /**
222
         * Set if it is possible or not lo load a layer
223
         * from the transformation
224
         * @param isVisible
225
         */
226
        private void setLoadLayerVisible(boolean isVisible){
227
                loadLayerCb.setVisible(isVisible);
228
                selectViewLabel.setVisible(isVisible);
229
                selectViewScroll.setVisible(isVisible);
230
                selectViewList.setVisible(isVisible);
231
        }
232

    
233
        /**
234
         * Add the project views
235
         */
236
        private void addViews(){
237
                selectViewList.removeAll();
238
                ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
239
                ArrayList<ProjectDocument> projects = ext.getProject().getDocumentsByType(ProjectViewFactory.registerName);
240
                for (int i=0 ; i<projects.size() ; i++){
241
                        ProjectViewBase view = (ProjectViewBase)projects.get(i);
242
                        ((DefaultListModel)selectViewList.getModel()).addElement(view);
243
                }
244
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
245
                if (window instanceof IView){
246
                        selectViewList.setSelectedValue(window, true);
247
                }else{
248
                        selectViewList.setSelectedIndex(0);
249
                }
250
        }
251

    
252
        /**
253
         * Update the form when the transform has been applied in
254
         * to a layer
255
         * @param transform
256
         * @throws DataException
257
         */
258
        private void updateGuiForLayer(FeatureStoreTransform transform){
259
                messageTextArea.setText(PluginServices.getText(this, "transform_layer"));
260
                setLoadLayerVisible(false);
261
        }
262

    
263
        /**
264
         * @return if the layer has to be loaded 
265
         */
266
        public boolean isLayerLoaded(){
267
                if (!loadLayerCb.isVisible()){
268
                        return false;
269
                }
270
                return loadLayerCb.isSelected();
271
        }        
272

    
273
        /**
274
         * @return the transform
275
         */
276
        public FeatureStoreTransform getTransform() {
277
                return transform;
278
        }
279
        
280
        /**
281
         * @return The mapcontext
282
         */
283
        public MapContext getMapContext(){
284
                Object obj = selectViewList.getSelectedValue();
285
                if (obj != null){
286
                        return ((ProjectViewBase)obj).getMapContext();
287
                }
288
                return null;
289
        }
290

    
291
        /* (non-Javadoc)
292
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
293
         */
294
        public void itemStateChanged(ItemEvent arg0) {
295
                boolean isSelected = loadLayerCb.isSelected();
296
                selectViewLabel.setEnabled(isSelected);
297
                selectViewList.setEnabled(isSelected);
298
        }
299
}
300