Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extDalTransform / src / org / gvsig / app / daltransform / gui / impl / LoadLayerWizardPanel.java @ 31496

History | View | Annotate | Download (10.5 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.impl;
29

    
30
import java.awt.event.ItemEvent;
31
import java.awt.event.ItemListener;
32
import java.util.List;
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 org.gvsig.andami.PluginServices;
43
import org.gvsig.andami.ui.mdiManager.IWindow;
44
import org.gvsig.app.daltransform.gui.DataTransformGui;
45
import org.gvsig.app.project.ProjectManager;
46
import org.gvsig.app.project.documents.Document;
47
import org.gvsig.app.project.documents.view.BaseViewDocument;
48
import org.gvsig.app.project.documents.view.ViewManager;
49
import org.gvsig.app.project.documents.view.gui.IView;
50
import org.gvsig.fmap.dal.DataTypes;
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
53
import org.gvsig.fmap.dal.feature.FeatureStore;
54
import org.gvsig.fmap.dal.feature.FeatureStoreTransform;
55
import org.gvsig.fmap.dal.feature.FeatureType;
56
import org.gvsig.fmap.mapcontext.MapContext;
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59

    
60

    
61
/**
62
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
63
 */
64
public class LoadLayerWizardPanel extends AbstractDataTransformWizardPanel implements ItemListener{
65
        /**
66
         * 
67
         */
68
        private static final long serialVersionUID = -7893735055404404591L;
69
        
70
        private static final Logger logger = LoggerFactory.getLogger(LoadLayerWizardPanel.class);
71
        private JCheckBox loadLayerCb = null;
72
        private JScrollPane messageScroll = null;
73
        private JTextArea messageTextArea = null;
74
        private JPanel northPanel = null;
75
        private JLabel selectViewLabel;
76
        private JList selectViewList;
77
        private JScrollPane selectViewScroll;
78
    private JPanel centerPanel;
79
        private FeatureStoreTransform transform = null;
80
//        private boolean hasViews = false;
81

    
82
        /**
83
         * @param wizardComponents
84
         */
85
        public LoadLayerWizardPanel() {
86
                super();        
87
                initComponents();
88
                initLabels();        
89
                addViews();
90
                loadLayerCb.addItemListener(this);
91
                itemStateChanged(null);                
92
        }
93

    
94
        private void initLabels(){
95
                loadLayerCb.setText(PluginServices.getText(this, "transform_load_layer_query"));
96
                selectViewLabel.setText(PluginServices.getText(this, "transform_view_to_load"));
97
        }
98

    
99
        private void initComponents() {
100
        java.awt.GridBagConstraints gridBagConstraints;
101

    
102
        northPanel = new javax.swing.JPanel();
103
        messageScroll = new javax.swing.JScrollPane();
104
        messageTextArea = new javax.swing.JTextArea();
105
        loadLayerCb = new javax.swing.JCheckBox();
106
        selectViewLabel = new javax.swing.JLabel();
107
        centerPanel = new javax.swing.JPanel();
108
        selectViewScroll = new javax.swing.JScrollPane();
109
        selectViewList = new javax.swing.JList();
110
        selectViewList.setModel(new DefaultListModel());
111

    
112
        setLayout(new java.awt.BorderLayout());
113

    
114
        northPanel.setLayout(new java.awt.GridBagLayout());
115

    
116
        messageScroll.setBorder(null);
117

    
118
        messageTextArea.setColumns(20);
119
        messageTextArea.setEditable(false);
120
        messageTextArea.setLineWrap(true);
121
        messageTextArea.setRows(5);
122
        messageTextArea.setBorder(null);
123
        messageScroll.setViewportView(messageTextArea);
124

    
125
        gridBagConstraints = new java.awt.GridBagConstraints();
126
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127
        gridBagConstraints.weightx = 1.0;
128
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 5, 2);
129
        northPanel.add(messageScroll, gridBagConstraints);
130

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

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

    
151
        add(northPanel, java.awt.BorderLayout.NORTH);
152

    
153
        centerPanel.setLayout(new java.awt.GridBagLayout());
154

    
155
        selectViewScroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
156
        selectViewScroll.setPreferredSize(null);
157

    
158
        selectViewScroll.setViewportView(selectViewList);
159

    
160
        gridBagConstraints = new java.awt.GridBagConstraints();
161
        gridBagConstraints.gridx = 0;
162
        gridBagConstraints.gridy = 0;
163
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
164
        gridBagConstraints.weightx = 1.0;
165
        gridBagConstraints.weighty = 1.0;
166
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 2);
167
        centerPanel.add(selectViewScroll, gridBagConstraints);
168

    
169
        add(centerPanel, java.awt.BorderLayout.CENTER);
170
        }
171

    
172
        /*
173
         * (non-Javadoc)
174
         * @see org.gvsig.app.daltransform.impl.AbstractDataTransformWizardPanel#updatePanel()
175
         */
176
        public void updatePanel() {
177
                //Gets the selected transformation
178
                DataTransformGui featureTransformGui = getDataTransformWizard().getDataTransformGui();
179
                
180
                //Gets the selected FeatureStore
181
                FeatureStore featureStore = getFeatureStore();
182
                
183
                //Apply the transformation
184
                try {
185
                        transform = featureTransformGui.createFeatureStoreTransform(featureStore);
186

    
187
                        if (getDataTransformWizard().isFeatureStoreLayer()){
188
                                updateGuiForLayer(transform);
189
                        }else{
190
                                updateGuiForTable(transform);
191
                        }
192
                } catch (DataException e) {
193
                        logger.error("Error creating the transformation", e);
194
                }                
195
        }
196

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

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

    
238
        /**
239
         * Add the project views
240
         */
241
        private void addViews(){
242
                selectViewList.removeAll();
243
                
244
                List<Document> views = ProjectManager.getInstance().getCurrentProject().getDocuments(ViewManager.TYPENAME);
245
                for (Document view : views) {
246
                        ((DefaultListModel)selectViewList.getModel()).addElement(view);
247
                }
248
//                ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
249
//                ArrayList<ProjectDocument> projects = ext.getProject().getDocumentsByType(ProjectViewFactory.registerName);
250
//                for (int i=0 ; i<projects.size() ; i++){
251
//                        ProjectViewBase view = (ProjectViewBase)projects.get(i);
252
//                        ((DefaultListModel)selectViewList.getModel()).addElement(view);
253
//                }
254
//                if (selectViewList.getModel().getSize() > 0){
255
//                        hasViews = true;
256
//                }
257
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
258
                if (window instanceof IView){
259
                        selectViewList.setSelectedValue(window, true);
260
                }else{
261
                        selectViewList.setSelectedIndex(0);
262
                }
263
        }
264

    
265
        /**
266
         * Update the form when the transform has been applied in
267
         * to a layer
268
         * @param transform
269
         * @throws DataException
270
         */
271
        private void updateGuiForLayer(FeatureStoreTransform transform){
272
                messageTextArea.setText(PluginServices.getText(this, "transform_layer"));
273
                setLoadLayerVisible(false);
274
        }
275

    
276
        /**
277
         * @return if the layer has to be loaded 
278
         */
279
        public boolean isLayerLoaded(){
280
                if (!loadLayerCb.isVisible()){
281
                        return false;
282
                }
283
                return loadLayerCb.isSelected();
284
        }        
285

    
286
        /**
287
         * @return the transform
288
         */
289
        public FeatureStoreTransform getTransform() {
290
                return transform;
291
        }
292
        
293
        /**
294
         * @return The mapcontext
295
         */
296
        public MapContext getMapContext(){
297
                Object obj = selectViewList.getSelectedValue();
298
                if (obj != null){
299
                        return ((BaseViewDocument)obj).getMapContext();
300
                }
301
                return null;
302
        }
303

    
304
        /* (non-Javadoc)
305
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
306
         */
307
        public void itemStateChanged(ItemEvent arg0) {
308
                boolean isSelected = loadLayerCb.isSelected();
309
                selectViewLabel.setEnabled(isSelected);
310
                selectViewList.setEnabled(isSelected);
311
        }
312

    
313
        /* (non-Javadoc)
314
         * @see org.gvsig.app.daltransform.gui.FeatureTransformWizard#getPanelTitle()
315
         */
316
        public String getPanelTitle() {
317
                return PluginServices.getText(this, "transform_apply");
318
        }
319
}
320