Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extTopology / src / org / gvsig / topology / ui / LayerSelectionPanel.java @ 24619

History | View | Annotate | Download (8.36 KB)

1
/*
2
 * Created on 10-abr-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
*/
49
package org.gvsig.topology.ui;
50

    
51
import java.awt.BorderLayout;
52
import java.awt.FlowLayout;
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55
import java.util.ArrayList;
56
import java.util.List;
57

    
58
import javax.swing.JDialog;
59
import javax.swing.JLabel;
60
import javax.swing.JPanel;
61
import javax.swing.JScrollPane;
62
import javax.swing.JTable;
63
import javax.swing.table.AbstractTableModel;
64
import javax.swing.table.TableModel;
65

    
66
import org.gvsig.gui.beans.AcceptCancelPanel;
67
import org.gvsig.gui.beans.swing.JButton;
68
import org.gvsig.topology.ui.util.GUIUtil;
69

    
70
import com.iver.andami.PluginServices;
71
import com.iver.andami.ui.mdiFrame.MDIFrame;
72
import com.iver.cit.gvsig.fmap.MapContext;
73
import com.iver.cit.gvsig.fmap.layers.FLayer;
74
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
75

    
76
/**
77
 * This panel manages the selection of layers in TOC.
78
 * Allows to add layers from the toc, remove, and shows a dialog
79
 * with toc layers.
80
 * @author Alvaro Zabala
81
 *
82
 */
83
public class LayerSelectionPanel extends JPanel {
84

    
85
        private static final long serialVersionUID = 6171285621323810490L;
86
        
87
        /**
88
         * text in the header of the component
89
         */
90
        private String title;
91
        /**
92
         * Component to show the available layers in a table
93
         */
94
        private JTable lyrsTable;
95
        
96
        /**
97
         * Layers selected in the component's table
98
         */
99
        private List<FLayer> layers;
100
        
101
        /**
102
         * MapContext of the active view
103
         */
104
        private MapContext mapContext;
105
        
106
        /**
107
         * Contains listeners interested in the addition or removing of layers
108
         * to this component.
109
         */
110
        private List<LayerSelectionListener> selectionListeners;
111
        
112
        private boolean readOnly;
113

    
114
        /**
115
         * Constructor.
116
         * 
117
         * @param layers List of layers to initialize the table content
118
         */
119
        public LayerSelectionPanel(List<FLayer> layers, MapContext mapContext){
120
                this(layers, mapContext, false);
121
        }
122
        
123
        public LayerSelectionPanel(List<FLayer> layers, MapContext mapContext, boolean readOnly){
124
                super();
125
                this.layers = layers;
126
                this.mapContext = mapContext;
127
                this.readOnly = readOnly;
128
                selectionListeners = new ArrayList<LayerSelectionListener>();
129
                initialize();
130
        }
131
        
132
        
133
        /**
134
         * Returns the list of layers in the component's table
135
         * @return
136
         */
137
        public List<FLayer> getLayers(){
138
                return layers;
139
        }
140
        
141
        public void addLayerSelectionListener(LayerSelectionListener selectionListener){
142
                selectionListeners.add(selectionListener);
143
        }
144
        
145
        private void fireLayerSelectionEvent(SelectedLayerEvent event){
146
                for(int i = 0; i < selectionListeners.size(); i++){
147
                        LayerSelectionListener listener = selectionListeners.get(i);
148
                        listener.selectionEvent(event);
149
                }
150
        }
151
        
152
        
153
        private void initialize(){
154
                setLayout(new BorderLayout());
155
                JLabel titleLbl = new JLabel(PluginServices.getText(null,title));
156
                add(titleLbl, BorderLayout.NORTH);
157

    
158
                TableModel dataModel = new AbstractTableModel() {
159
                        public int getColumnCount() {
160
                                return 1;
161
                        }
162

    
163
                        public int getRowCount() {
164
                                return layers.size();
165
                        }
166

    
167
                        public Object getValueAt(int row, int col) {
168
                                FLayer lyr = layers.get(row);
169
                                return lyr.getName();
170
                        }
171
                };
172
                lyrsTable = new JTable();
173
                lyrsTable.setModel(dataModel);
174
                lyrsTable.getColumnModel().getColumn(0).setHeaderValue(
175
                                PluginServices.getText(null, "Layers"));
176
                JScrollPane scrollTable = new JScrollPane(lyrsTable);
177
                add(scrollTable, BorderLayout.CENTER);
178
                
179
                if(! readOnly){
180
                        add(getButtonsPanel(), BorderLayout.SOUTH);
181
                }//if ! readOnly
182
                
183
        }
184
        
185
        /**
186
         * Creates JPanel with add layer, remove layer, remove all layers buttons.
187
         * @return
188
         */
189
        private JPanel getButtonsPanel(){
190
                JButton addButton = new JButton(PluginServices.getText(null, "Add_Lyr"));
191
                addButton.addActionListener(new ActionListener(){
192
                        public void actionPerformed(ActionEvent arg0) {
193
                                LayersInTocPanel lyrsInTocPanel = new LayersInTocPanel(mapContext.getLayers(), layers);
194
                                final JDialog dialog = new JDialog();
195
                                dialog.setLayout(new BorderLayout());
196
                                
197
                                dialog.getContentPane().add(lyrsInTocPanel, BorderLayout.CENTER);
198
                                
199
                                final LayersInTocPanel tempPanel = lyrsInTocPanel;
200
                                ActionListener okAction = new ActionListener(){
201
                                        public void actionPerformed(ActionEvent e) {
202
                                                List<FLyrVect> newLyrs = tempPanel.getSelectedLyrs();
203
                                                for(int i = 0; i < newLyrs.size(); i++){
204
                                                        FLyrVect lyr = newLyrs.get(i);
205
                                                        if(!layers.contains(lyr)){//FIXME La capa que se a?ada aqu? hay que quitarla de la FLayers a la que pertenezca
206
//                                                                layers.add(lyr);
207
                                                                SelectedLayerEvent event = new SelectedLayerEvent();
208
                                                                event.lyrOfEvent = lyr;
209
                                                                event.eventType = SelectedLayerEvent.ADDED_EVENT_TYPE;
210
                                                                fireLayerSelectionEvent(event);
211
                                                                
212
                                                        }//if
213
                                                }//for
214
                                                lyrsTable.revalidate();
215
                                                dialog.dispose();
216
                                        }};
217
                                ActionListener cancelAction = new ActionListener(){
218
                                        public void actionPerformed(ActionEvent e) {
219
                                                dialog.dispose();
220
                                        }};
221
                                
222
                                AcceptCancelPanel acceptCancelPnl = new AcceptCancelPanel(okAction, cancelAction);
223
                                dialog.getContentPane().add(acceptCancelPnl, BorderLayout.SOUTH);
224
                                dialog.setTitle(PluginServices.getText(this, "Capas_A_A?adir_A_La_Topologia"));
225
                                dialog.setModal(true);
226
                                dialog.pack();
227
                                GUIUtil.getInstance().centerDialog(dialog, (MDIFrame) PluginServices.getMainFrame());
228
                                dialog.setVisible(true);        
229
                        }});
230
                
231
                JButton removeButton = new JButton(PluginServices.getText(this, "Remove_Lyr"));
232
                removeButton.addActionListener(new ActionListener(){
233
                        public void actionPerformed(ActionEvent arg0) {
234
                                int selectedLyr = lyrsTable.getSelectedRow();
235
                                SelectedLayerEvent event = new SelectedLayerEvent();
236
                                event.lyrOfEvent = layers.get(selectedLyr);
237
                                event.eventType = SelectedLayerEvent.REMOVED_EVENT_TYPE;
238
                                fireLayerSelectionEvent(event);
239
//                                layers.remove(selectedLyr);
240
                                lyrsTable.revalidate();
241
                        }});
242
                
243
                JButton removeAllBtn = new JButton(PluginServices.getText(this, "Remove_All"));
244
                removeAllBtn.addActionListener(new ActionListener(){
245
                        public void actionPerformed(ActionEvent e) {
246
                                for(int i = 0; i < layers.size(); i++){
247
                                        FLayer lyr = layers.get(i);
248
                                        SelectedLayerEvent event = new SelectedLayerEvent();
249
                                        event.lyrOfEvent = lyr;
250
                                        event.eventType = SelectedLayerEvent.REMOVED_EVENT_TYPE;
251
                                        fireLayerSelectionEvent(event);
252
                                }
253
//                                layers.clear();
254
                                lyrsTable.revalidate();
255
                        }
256
                });
257

    
258
                
259
                JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
260
                buttonPanel.add(addButton);
261
                buttonPanel.add(removeButton);
262
                buttonPanel.add(removeAllBtn);
263
                return buttonPanel;
264
        }
265
        
266
        /**
267
         * Subclasses of this interface listen for events launched by this control
268
         * 
269
         * @author Alvaro Zabala
270
         *
271
         */
272
        public interface LayerSelectionListener{
273
                public void selectionEvent(SelectedLayerEvent event);
274
        }
275
        /**
276
         * Event launched when this control adds or removes a layer.
277
         * @author Alvaro Zabala
278
         */
279
        class SelectedLayerEvent{
280
                final static int ADDED_EVENT_TYPE = 0;
281
                final static int REMOVED_EVENT_TYPE = 1;
282
                
283
                FLayer lyrOfEvent;
284
                int eventType;
285
        }
286
        
287
        
288
}