Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / gui / LayerSelectionPanel.java @ 8513

History | View | Annotate | Download (7.09 KB)

1 8410 azabala
/*
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$
47
* $Log$
48 8513 azabala
* Revision 1.2  2006-11-03 19:39:29  azabala
49 8410 azabala
* *** empty log message ***
50
*
51 8513 azabala
* Revision 1.1  2006/10/27 18:26:07  azabala
52
* *** empty log message ***
53 8410 azabala
*
54 8513 azabala
*
55 8410 azabala
*/
56
package com.iver.cit.gvsig.graph.gui;
57
58 8513 azabala
import java.awt.BorderLayout;
59 8410 azabala
import java.awt.Container;
60
import java.awt.GridBagConstraints;
61
import java.awt.Window;
62
import java.awt.event.ActionEvent;
63
import java.awt.event.ActionListener;
64
import java.util.ArrayList;
65
import java.util.Arrays;
66
67
import javax.swing.DefaultComboBoxModel;
68
import javax.swing.JComboBox;
69
import javax.swing.JLabel;
70
import javax.swing.JPanel;
71
import org.gvsig.gui.beans.AcceptCancelPanel;
72
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
73
74
import com.iver.andami.PluginServices;
75
import com.iver.andami.ui.mdiManager.IWindow;
76
import com.iver.andami.ui.mdiManager.WindowInfo;
77
import com.iver.cit.gvsig.fmap.DriverException;
78
import com.iver.cit.gvsig.fmap.layers.FLayer;
79
import com.iver.cit.gvsig.fmap.layers.FLayers;
80
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
81
82
/**
83
 * Component that shows in a combobox all the TOC's layers, and allows
84
 * user to select one of them.
85
 *
86
 * It optionally allows to filter layers for a given geometry type.
87
 *
88
 *
89
 * */
90 8513 azabala
public class LayerSelectionPanel extends JPanel implements IWindow{
91 8410 azabala
92 8513 azabala
        private GridBagLayoutPanel contentPanel;
93
94 8410 azabala
        FLayers layers = null;
95
        JComboBox layersComboBox = null;
96
        AcceptCancelPanel buttonPanel = null;
97
98
99
        private ActionListener okActionListener = new ActionListener(){
100
                public void actionPerformed(ActionEvent arg0) {
101
                        wasFinishPressed = true;
102
                        closeParent(LayerSelectionPanel.this);
103
                }
104
        };
105
106
        private ActionListener cancelActionListener = new ActionListener(){
107
                public void actionPerformed(ActionEvent arg0) {
108
                        wasFinishPressed = false;
109
                        closeParent(LayerSelectionPanel.this);
110
                }
111
        };
112
        String title = null;
113
        String introductoryText = null;
114
115 8513 azabala
        boolean wasFinishPressed = false;
116 8410 azabala
117
        /**
118
         * Optinal filter to show in the combo box layers of a given
119
         * geometry typ0e
120
         * */
121
        int geometryType = -1;
122
123
        /**
124
         * If it is not null, we'll filter layers to only show
125
         * types of this class
126
         * */
127
        Class layerType;
128
        private WindowInfo wi;
129
130
131
132
        public LayerSelectionPanel(FLayers tocLayers,
133
                        String title,
134
                        String introductoryText){
135 8513 azabala
                this(tocLayers, title, introductoryText, (Class) null, -1);
136
137
        }
138
139
        public LayerSelectionPanel(FLayers layers, String title,
140
                                String introductoryText, Class class1, int geometryType) {
141 8410 azabala
                super();
142 8513 azabala
                this.layers = layers;
143 8410 azabala
                this.introductoryText = introductoryText;
144 8513 azabala
                this.layerType = class1;
145
                this.geometryType = geometryType;
146 8410 azabala
                initialize();
147
        }
148 8513 azabala
149 8410 azabala
        public void setGeometryTypeConstraint(int geometryType){
150
                if(layerType == null || !(layerType.isAssignableFrom(FLyrVect.class)))
151
                        return;
152
                this.geometryType = geometryType;
153
        }
154
155
        public void setLayerTypeConstraint(Class layerClass){
156
                this.layerType = layerClass;
157
        }
158
159
160
        public FLayer getLayer() {
161
                FLayer solution = null;
162
                String selectedLayer = (String)layersComboBox.getSelectedItem();
163
        solution = layers.getLayer(selectedLayer);
164
        return solution;
165
        }
166
167
        public void setFLayers(FLayers layers){
168
                this.layers = layers;
169
                updateLayerCombo();
170
        }
171
172
        public void initialize(){
173 8513 azabala
                setLayout(new BorderLayout());
174
                contentPanel = new GridBagLayoutPanel();
175
                contentPanel.addComponent(new JLabel(introductoryText), GridBagConstraints.BOTH);;
176
                contentPanel.addComponent(PluginServices.getText(null, "Capas:"), getLayersComboBox());
177
                add(contentPanel, BorderLayout.NORTH);
178
                add(getButtonPanel(), BorderLayout.SOUTH);
179
180 8410 azabala
        }
181
182
        AcceptCancelPanel getButtonPanel() {
183
                if (buttonPanel == null) {
184
                        buttonPanel = new AcceptCancelPanel();
185
                        buttonPanel.setOkButtonActionListener(okActionListener);
186
                        buttonPanel.setCancelButtonActionListener(cancelActionListener);
187
                }
188
                return buttonPanel;
189
        }
190
191
192
        void updateLayerCombo(){
193
                DefaultComboBoxModel defaultModel =
194
                new DefaultComboBoxModel(getLayerNames());
195
        layersComboBox.setModel(defaultModel);
196
        }
197
198
        public boolean wasFinishPressed(){
199
                return wasFinishPressed;
200
        }
201
202
        protected FLayer[] getLayerForType(FLayers layers){
203
                FLayer[] solution = null;
204
                int numLayers = layers.getLayersCount();
205
                if(layers != null &&  numLayers > 0){
206
                        ArrayList list = new ArrayList();
207
                        for(int i = 0; i < numLayers; i++){
208
                                FLayer layer = layers.getLayer(i);
209
                                if(layerType != null){
210
                                        if(!layer.getClass().isAssignableFrom(layerType))
211
                                                continue;
212
                                        if(layer instanceof FLyrVect){
213
                                                try {
214
                                                        if(((FLyrVect)layer).getShapeType() != geometryType)
215
                                                                continue;
216 8513 azabala
                                                        list.add(layer);
217 8410 azabala
                                                } catch (DriverException e) {
218
                                                        continue;
219
                                                }
220
                                        }//if
221
                                }//if
222
                                if(layer instanceof FLayers){
223
                                        list.addAll(Arrays.asList(getLayerForType((FLayers)layer)));
224
                                }
225
                        }//for
226
                        solution = new FLyrVect[list.size()];
227
                        list.toArray(solution);
228
                }//if
229
                return solution;
230
        }
231
232
        protected String[] getLayerNames() {
233
                String[] solution = null;
234
                ArrayList nameList = new ArrayList();
235
                FLayer[] layers = getLayerForType(this.layers);
236
                for(int i = 0; i < layers.length; i++){
237
                        nameList.add(layers[i].getName());
238
                }
239
                solution = new String[nameList.size()];
240
                nameList.toArray(solution);
241
                return solution;
242
        }
243
244
        private JComboBox getLayersComboBox() {
245
                if (layersComboBox == null) {
246
            layersComboBox = new JComboBox();
247
                }
248
                updateLayerCombo();
249
                return layersComboBox;
250
        }
251
252
        public static void closeParent(JPanel component){
253
                Container container = component.getParent();
254
                Container parentOfContainer = null;
255
                while(! (container instanceof Window)){
256
                        parentOfContainer = container.getParent();
257
                        container = parentOfContainer;
258
                }
259
                ((Window)container).dispose();
260
        }
261
262
263
264
        public WindowInfo getWindowInfo() {
265
                  if (wi==null) {
266
                    wi = new WindowInfo(WindowInfo.MODALDIALOG);
267
                    wi.setWidth(450);
268
                    wi.setHeight(320);
269
                    wi.setTitle(title);
270
                }
271
                return wi;
272
        }
273
274
}  //  @jve:decl-index=0:visual-constraint="10,10"