Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / gui / LayerSelectionPanel.java @ 30840

History | View | Annotate | Download (7.63 KB)

1
/*
2
 * Created on 27-oct-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: LayerSelectionPanel.java 30840 2009-09-18 16:06:37Z fpenarrubia $
47
* $Log$
48
* Revision 1.4  2007-09-07 11:29:47  fjp
49
* Casi compila. Falta arreglar lo de FArrowSymbol y retocar el graphiclist de FMap.
50
*
51
* Revision 1.3  2006/11/08 19:32:44  azabala
52
* *** empty log message ***
53
*
54
* Revision 1.2  2006/11/03 19:39:29  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.1  2006/10/27 18:26:07  azabala
58
* *** empty log message ***
59
*
60
*
61
*/
62
package org.gvsig.graph.gui;
63

    
64
import java.awt.BorderLayout;
65
import java.awt.Container;
66
import java.awt.GridBagConstraints;
67
import java.awt.Window;
68
import java.awt.event.ActionEvent;
69
import java.awt.event.ActionListener;
70
import java.util.ArrayList;
71
import java.util.Arrays;
72

    
73
import javax.swing.DefaultComboBoxModel;
74
import javax.swing.JComboBox;
75
import javax.swing.JLabel;
76
import javax.swing.JPanel;
77

    
78
import org.gvsig.gui.beans.AcceptCancelPanel;
79
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
80

    
81
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
82
import com.iver.andami.PluginServices;
83
import com.iver.andami.ui.mdiManager.IWindow;
84
import com.iver.andami.ui.mdiManager.WindowInfo;
85
import com.iver.cit.gvsig.fmap.layers.FLayer;
86
import com.iver.cit.gvsig.fmap.layers.FLayers;
87
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
88

    
89
/**
90
 * Component that shows in a combobox all the TOC's layers, and allows
91
 * user to select one of them.
92
 * 
93
 * It optionally allows to filter layers for a given geometry type.
94
 * 
95
 * 
96
 * */
97
public class LayerSelectionPanel extends JPanel implements IWindow{
98

    
99
        private GridBagLayoutPanel contentPanel;
100
        
101
        FLayers layers = null;
102
        JComboBox layersComboBox = null;
103
        AcceptCancelPanel buttonPanel = null;
104
        
105
        
106
        private ActionListener okActionListener = new ActionListener(){
107
                public void actionPerformed(ActionEvent arg0) {
108
                        wasFinishPressed = true;
109
                        closeParent(LayerSelectionPanel.this);
110
                }
111
        };
112

    
113
        private ActionListener cancelActionListener = new ActionListener(){
114
                public void actionPerformed(ActionEvent arg0) {
115
                        wasFinishPressed = false;
116
                        closeParent(LayerSelectionPanel.this);
117
                }
118
        };
119
        String title = null;
120
        String introductoryText = null;
121
        
122
        boolean wasFinishPressed = false;
123
        
124
        /**
125
         * Optinal filter to show in the combo box layers of a given
126
         * geometry typ0e
127
         * */
128
        ArrayList<Integer> geometryType = new ArrayList<Integer>();
129
        
130
        /**
131
         * If it is not null, we'll filter layers to only show
132
         * types of this class
133
         * */
134
        Class layerType;
135
        private WindowInfo wi;
136
        
137
        
138
        
139
        public LayerSelectionPanel(FLayers tocLayers, 
140
                        String title, 
141
                        String introductoryText){
142
                this(tocLayers, title, introductoryText, (Class) null, -1);
143
                
144
        }
145
        
146
        public LayerSelectionPanel(FLayers layers, String title, 
147
                                String introductoryText, Class class1, int geometryType) {
148
                super();
149
                this.layers = layers;
150
                this.introductoryText = introductoryText;
151
                this.layerType = class1;
152
                this.geometryType.add(geometryType);
153
                initialize();
154
        }
155

    
156
        public void addGeometryTypeConstraint(int geometryType){
157
                if(layerType == null || !(layerType.isAssignableFrom(FLyrVect.class)))
158
                        return;
159
                this.geometryType.add(geometryType);
160
                updateLayerCombo();
161
        }
162
        
163
        public void setLayerTypeConstraint(Class layerClass){
164
                this.layerType = layerClass;
165
        }
166
                
167
        
168
        public FLayer getLayer() {
169
                FLayer solution = null;
170
                String selectedLayer = (String)layersComboBox.getSelectedItem();
171
                if(selectedLayer != null){
172
                        if(!(selectedLayer.trim().equals("")))
173
                                solution = layers.getLayer(selectedLayer);
174
                }
175
        
176
        return solution;
177
        }
178
        
179
        public void setFLayers(FLayers layers){
180
                this.layers = layers;
181
                updateLayerCombo();
182
        }
183
        
184
        public void initialize(){
185
                setLayout(new BorderLayout());
186
                contentPanel = new GridBagLayoutPanel();
187
                contentPanel.addComponent(new JLabel(introductoryText), GridBagConstraints.BOTH);
188
                JComboBox comboBox = getLayersComboBox();
189
                contentPanel.addComponent(PluginServices.getText(null, "Capas:"), comboBox);
190
                add(contentPanel, BorderLayout.NORTH);
191
                add(getButtonPanel(), BorderLayout.SOUTH);
192
                
193
        }
194
        
195
        AcceptCancelPanel getButtonPanel() {
196
                if (buttonPanel == null) {
197
                        buttonPanel = new AcceptCancelPanel();
198
                        buttonPanel.setOkButtonActionListener(okActionListener);
199
                        buttonPanel.setCancelButtonActionListener(cancelActionListener);
200
                }
201
                return buttonPanel;
202
        }
203
        
204
        
205
        void updateLayerCombo(){
206
                DefaultComboBoxModel defaultModel = 
207
                new DefaultComboBoxModel(getLayerNames());
208
        layersComboBox.setModel(defaultModel);
209
        }
210
        
211
        public boolean wasFinishPressed(){
212
                return wasFinishPressed;
213
        }
214
        
215
        protected FLayer[] getLayerForType(FLayers layers){
216
                FLayer[] solution = null;
217
                int numLayers = layers.getLayersCount();
218
                if(layers != null &&  numLayers > 0){
219
                        ArrayList list = new ArrayList();
220
                        for(int i = 0; i < numLayers; i++){
221
                                FLayer layer = layers.getLayer(i);
222
                                if(layerType != null){
223
                                        if(!layer.getClass().isAssignableFrom(layerType))
224
                                                continue;
225
                                        if(layer instanceof FLyrVect){
226
                                                FLyrVect lyrVect = (FLyrVect) layer;
227
                                                try {
228
                                                        for (int j=0; j < geometryType.size(); j++) {
229
                                                                if(lyrVect.getShapeType() == geometryType.get(j))
230
                                                                        list.add(layer);
231
                                                        }
232
                                                } catch (ReadDriverException e) {
233
                                                        continue;
234
                                                }
235
                                        }//if        
236
                                }//if
237
                                if(layer instanceof FLayers){
238
                                        list.addAll(Arrays.asList(getLayerForType((FLayers)layer)));
239
                                }
240
                        }//for
241
                        solution = new FLyrVect[list.size()];
242
                        list.toArray(solution);
243
                }//if
244
                return solution;
245
        }
246
        
247
        protected String[] getLayerNames() {
248
                String[] solution = null;
249
                ArrayList nameList = new ArrayList();
250
                FLayer[] layers = getLayerForType(this.layers);
251
                for(int i = 0; i < layers.length; i++){
252
                        nameList.add(layers[i].getName());
253
                }
254
                solution = new String[nameList.size()];
255
                nameList.toArray(solution);
256
                return solution;
257
        }
258
        
259
        private JComboBox getLayersComboBox() {
260
                if (layersComboBox == null) {
261
            layersComboBox = new JComboBox();
262
                }
263
                updateLayerCombo();
264
                return layersComboBox;
265
        }
266
        
267
        public static void closeParent(JPanel component){
268
                Container container = component.getParent();
269
                Container parentOfContainer = null;
270
                while(! (container instanceof Window)){
271
                        parentOfContainer = container.getParent();
272
                        container = parentOfContainer;
273
                }
274
                ((Window)container).dispose();
275
        }
276
        
277
        
278
        
279
        public WindowInfo getWindowInfo() {
280
                  if (wi==null) {
281
                    wi = new WindowInfo(WindowInfo.MODALDIALOG);
282
                    wi.setWidth(350);
283
                    wi.setHeight(120);
284
                    wi.setTitle(title);
285
                }
286
                return wi;
287
        }
288
        public Object getWindowProfile(){
289
                return WindowInfo.DIALOG_PROFILE;
290
        }
291

    
292
}  //  @jve:decl-index=0:visual-constraint="10,10"
293