Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.daltransform.app.mainplugin / src / main / java / org / gvsig / daltransform / swing / impl / LoadLayerWizardPanel.java @ 40354

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.daltransform.swing.impl;
29

    
30
import java.awt.Font;
31
import java.awt.event.ItemEvent;
32
import java.awt.event.ItemListener;
33
import java.util.List;
34

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

    
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
import org.gvsig.andami.PluginServices;
46
import org.gvsig.andami.ui.mdiManager.IWindow;
47
import org.gvsig.app.project.ProjectManager;
48
import org.gvsig.app.project.documents.Document;
49
import org.gvsig.app.project.documents.view.BaseViewDocument;
50
import org.gvsig.app.project.documents.view.ViewManager;
51
import org.gvsig.app.project.documents.view.gui.IView;
52
import org.gvsig.daltransform.swing.DataTransformGui;
53
import org.gvsig.fmap.dal.DataTypes;
54
import org.gvsig.fmap.dal.exception.DataException;
55
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.dal.feature.FeatureStoreTransform;
58
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.fmap.mapcontext.MapContext;
60

    
61

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

    
81
        /**
82
         * @param wizardComponents
83
         */
84
        public LoadLayerWizardPanel() {
85
                super();        
86

    
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
        selectViewCombo = new JComboBox();
109

    
110
        setLayout(new java.awt.BorderLayout());
111

    
112
        northPanel.setLayout(new java.awt.GridBagLayout());
113

    
114
        messageScroll.setBorder(null);
115

    
116
        messageTextArea.setColumns(20);
117
        messageTextArea.setEditable(false);
118
        messageTextArea.setLineWrap(true);
119

    
120
        messageTextArea.setWrapStyleWord(true);
121
        messageTextArea.setBackground(northPanel.getBackground());
122
        Font fnt = messageTextArea.getFont();
123
        fnt = fnt.deriveFont(Font.PLAIN);
124
        messageTextArea.setFont(fnt);
125

    
126
        
127
        messageTextArea.setRows(5);
128
        messageTextArea.setBorder(null);
129
        messageScroll.setViewportView(messageTextArea);
130

    
131
        gridBagConstraints = new java.awt.GridBagConstraints();
132
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
133
        gridBagConstraints.gridx = 0;
134
        gridBagConstraints.gridy = 0;
135
        gridBagConstraints.weightx = 1.0;
136
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 5, 2);
137
        northPanel.add(messageScroll, gridBagConstraints);
138

    
139
        loadLayerCb.setText("jCheckBox1");
140
        gridBagConstraints = new java.awt.GridBagConstraints();
141
        gridBagConstraints.gridx = 0;
142
        gridBagConstraints.gridy = 1;
143
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
144
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
145
        gridBagConstraints.weightx = 1.0;
146
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 2);
147
        northPanel.add(loadLayerCb, gridBagConstraints);
148
        
149
        add(northPanel, java.awt.BorderLayout.NORTH);
150
        
151
        centerPanel.setLayout(new java.awt.GridBagLayout());
152

    
153
        selectViewLabel.setText("jLabel1");
154
        gridBagConstraints = new java.awt.GridBagConstraints();
155
        gridBagConstraints.gridx = 0;
156
        gridBagConstraints.gridy = 0;
157
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
158
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
159
        gridBagConstraints.weightx = 1.0;
160
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 5, 2);
161
        centerPanel.add(selectViewLabel, gridBagConstraints);
162

    
163
        gridBagConstraints = new java.awt.GridBagConstraints();
164
        gridBagConstraints.gridx = 0;
165
        gridBagConstraints.gridy = 1;
166
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
167
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
168
        gridBagConstraints.weightx = 1.0;
169
        gridBagConstraints.weighty = 1.0;
170
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 2);
171
        centerPanel.add(selectViewCombo, gridBagConstraints);
172

    
173
        add(centerPanel, java.awt.BorderLayout.CENTER);
174
        }
175

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

    
191
                        if (getDataTransformWizard().isFeatureStoreLayer()){
192
                                updateGuiForLayer(transform);
193
                        }else{
194
                                updateGuiForTable(transform);
195
                        }
196
                } catch (DataException e) {
197
                        logger.error("Error creating the transformation", e);
198
                }                
199
        }
200

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

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

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

    
266
    public boolean isNextButtonEnabled() {
267
        return true;
268
    }
269

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

    
282
        /**
283
         * @return if the layer has to be loaded 
284
         */
285
        public boolean isLayerLoaded(){
286
                if (!loadLayerCb.isVisible()){
287
                        return false;
288
                }
289
                return loadLayerCb.isSelected();
290
        }        
291

    
292
        /**
293
         * @return the transform
294
         */
295
        public FeatureStoreTransform getTransform() {
296
                return transform;
297
        }
298
        
299
        /**
300
         * @return The mapcontext
301
         */
302
        public MapContext getMapContext(){
303
                Object obj = selectViewCombo.getSelectedItem();
304
                if (obj != null){
305
                        return ((BaseViewDocument)obj).getMapContext();
306
                }
307
                return null;
308
        }
309

    
310
        /* (non-Javadoc)
311
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
312
         */
313
        public void itemStateChanged(ItemEvent arg0) {
314
                boolean isSelected = loadLayerCb.isSelected();
315
                selectViewLabel.setEnabled(isSelected);
316
                selectViewCombo.setEnabled(isSelected);
317
        }
318

    
319
        /* (non-Javadoc)
320
         * @see org.gvsig.app.daltransform.gui.FeatureTransformWizard#getPanelTitle()
321
         */
322
        public String getPanelTitle() {
323
                return PluginServices.getText(this, "transform_apply");
324
        }
325
        
326
}
327