Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolLayerManager.java @ 22477

History | View | Annotate | Download (14.1 KB)

1 15672 jdominguez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2 9745 jaume
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23 15672 jdominguez
 *   Av. Blasco Ib??ez, 50
24 9745 jaume
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46 13729 jaume
* Revision 1.8  2007-09-17 09:21:45  jaume
47
* refactored SymboSelector (added support for multishapedsymbol)
48
*
49
* Revision 1.7  2007/08/09 10:39:04  jaume
50 12994 jaume
* first round of found bugs fixed
51
*
52
* Revision 1.6  2007/06/29 13:07:33  jaume
53 12427 jaume
* +PictureLineSymbol
54
*
55
* Revision 1.5  2007/05/31 09:36:22  jaume
56 11297 jaume
* *** empty log message ***
57
*
58 11919 jaume
* Revision 1.4  2007/04/20 07:54:38  jaume
59
* *** empty log message ***
60
*
61 11297 jaume
* Revision 1.3  2007/03/09 11:25:00  jaume
62 10679 jaume
* Advanced symbology (start committing)
63 10626 caballero
*
64 10679 jaume
* Revision 1.1.2.4  2007/02/21 07:35:14  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.3  2007/02/08 15:45:37  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1.2.2  2007/02/08 15:43:04  jaume
71
* some bug fixes in the editor and removed unnecessary imports
72
*
73
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
74
* *** empty log message ***
75
*
76 10626 caballero
* Revision 1.1  2007/01/16 11:52:11  jaume
77 9745 jaume
* *** empty log message ***
78
*
79
* Revision 1.7  2007/01/10 17:05:05  jaume
80
* moved to FMap and gvSIG
81
*
82
* Revision 1.6  2006/11/08 10:56:47  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.5  2006/11/07 08:52:30  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.4  2006/11/06 17:08:45  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.3  2006/11/06 16:06:52  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.2  2006/11/06 07:33:54  jaume
95
* javadoc, source style
96
*
97
* Revision 1.1  2006/11/02 17:19:28  jaume
98
* *** empty log message ***
99
*
100
*
101
*/
102
package com.iver.cit.gvsig.gui.styling;
103
104
import java.awt.BorderLayout;
105
import java.awt.Color;
106
import java.awt.Component;
107
import java.awt.Dimension;
108
import java.awt.FlowLayout;
109
import java.awt.LayoutManager;
110
import java.awt.event.ActionEvent;
111
import java.awt.event.ActionListener;
112
import java.awt.event.MouseAdapter;
113
import java.awt.event.MouseEvent;
114
import java.util.ArrayList;
115
116
import javax.swing.AbstractListModel;
117
import javax.swing.BorderFactory;
118
import javax.swing.DefaultListCellRenderer;
119
import javax.swing.ImageIcon;
120 11297 jaume
import javax.swing.JCheckBox;
121 9745 jaume
import javax.swing.JList;
122
import javax.swing.JPanel;
123
import javax.swing.JScrollPane;
124
import javax.swing.event.ListDataListener;
125
import javax.swing.event.ListSelectionEvent;
126
import javax.swing.event.ListSelectionListener;
127
128 21299 vcaballero
import org.gvsig.fmap.geom.Geometry;
129 20994 jmvivo
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
130
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
131
import org.gvsig.fmap.mapcontext.rendering.symbols.MultiLayerFillSymbol;
132
import org.gvsig.fmap.mapcontext.rendering.symbols.MultiLayerLineSymbol;
133
import org.gvsig.fmap.mapcontext.rendering.symbols.MultiLayerMarkerSymbol;
134
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
135 9745 jaume
import org.gvsig.gui.beans.swing.JButton;
136
137
import com.iver.andami.PluginServices;
138 13729 jaume
import com.iver.cit.gvsig.project.documents.view.legend.gui.ISymbolSelector;
139 9745 jaume
import com.iver.utiles.XMLEntity;
140
141
/**
142
 * A component linked to a SymbolEditor that shows the layers
143
 *
144
 * @author jaume
145
 *
146
 */
147
public class SymbolLayerManager extends JPanel implements ActionListener{
148 12427 jaume
   private static final long serialVersionUID = -1939951243066481691L;
149
        private static final Dimension LIST_CELL_SIZE = new Dimension(150,37);
150 10679 jaume
        private SymbolEditor owner;
151 9745 jaume
    private JList jListLayers;
152
    private ISymbol activeLayer;
153 10679 jaume
    private IMultiLayerSymbol symbol;
154 9745 jaume
    private JButton btnAddLayer = null;
155
    private JPanel pnlButtons = null;
156
    private JButton btnRemoveLayer = null;
157
    private JButton btnMoveUpLayer = null;
158
    private JButton btnMoveDownLayer = null;
159
    private final Dimension btnDimension = new Dimension(24, 24);
160
    private JScrollPane scroll;
161
    private Color cellSelectedBGColor = Color.BLUE;
162
163
164
    public SymbolLayerManager(SymbolEditor owner) {
165
        this.owner = owner;
166 14736 jdominguez
        this.symbol = (IMultiLayerSymbol) owner.getSymbol();
167
        if (symbol.getLayerCount()==0) {
168
                int shapeType = -1;
169
                if (symbol instanceof MultiLayerMarkerSymbol) {
170 21299 vcaballero
                        shapeType = Geometry.TYPES.POINT;
171 14736 jdominguez
                } else if (symbol instanceof MultiLayerLineSymbol) {
172 21299 vcaballero
                        shapeType = Geometry.TYPES.CURVE;
173 14736 jdominguez
                        } else if (symbol instanceof MultiLayerFillSymbol) {
174 21299 vcaballero
                        shapeType = Geometry.TYPES.SURFACE;
175
                        }
176 14821 jmvivo
177 14736 jdominguez
                if (shapeType != -1)
178
                        symbol.addLayer(SymbologyFactory.
179 16086 jdominguez
                                        createDefaultSymbolByShapeType(shapeType));
180 14821 jmvivo
                   }
181 11919 jaume
        initialize();
182 14821 jmvivo
183 9745 jaume
    }
184
185
    private void initialize() {
186
        this.setLayout(new BorderLayout());
187
        this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
188
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
189 14736 jdominguez
        getJListLayers().setSelectedIndex(0);
190 9745 jaume
    }
191
192
    private JScrollPane getJScrollPane() {
193
        if (scroll ==  null) {
194
            scroll = new JScrollPane();
195 11297 jaume
            scroll.setPreferredSize(new Dimension(150, 160));
196 10679 jaume
            scroll.setViewportView(getJListLayers());
197 9745 jaume
        }
198
        return scroll;
199
    }
200
201
    private JList getJListLayers() {
202
        if (jListLayers == null) {
203
            jListLayers = new JList();
204
            jListLayers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
205
            jListLayers.setLayoutOrientation(JList.HORIZONTAL_WRAP);
206
207
            jListLayers.setVisibleRowCount(-1);
208
            jListLayers.addListSelectionListener(new ListSelectionListener() {
209
                public void valueChanged(ListSelectionEvent e) {
210
                    setLayer((ISymbol) jListLayers.getSelectedValue());
211
                }
212
            });
213 10679 jaume
            jListLayers.setModel(new SymbolLayerListModel());
214 9745 jaume
            jListLayers.setCellRenderer(new DefaultListCellRenderer() {
215 12427 jaume
                      private static final long serialVersionUID = -2551357351239544039L;
216
217
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
218 11297 jaume
                        return new SymbolLayerComponent((ISymbol) value, index, isSelected);
219 9745 jaume
                }
220
            });
221
            jListLayers.addMouseListener(new MouseAdapter() {
222
                                public void mouseClicked(MouseEvent e) {
223
                                        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
224 13729 jaume
                                                ISymbolSelector symSel = SymbolSelector.createSymbolSelector(activeLayer, owner.getShapeType());
225 9745 jaume
                                                PluginServices.getMDIManager().addWindow(symSel);
226 10679 jaume
                                                updateSymbol((ISymbol) symSel.getSelectedObject());
227
                                                setLayer((ISymbol) symSel.getSelectedObject());
228 9745 jaume
                                        }
229
230
                                }
231
            });
232
        }
233
        return jListLayers;
234
    }
235
236
    private void updateSymbol(ISymbol layer) {
237
            if (layer != null) {
238
                    int index = getJListLayers().getSelectedIndex();
239 10679 jaume
                    symbol.setLayer(index, layer);
240 9745 jaume
                    owner.refresh();
241 10679 jaume
                    validate();
242 9745 jaume
            }
243
    }
244
245
    private void setLayer(ISymbol symbol) {
246
            if (symbol!=null) {
247
                    activeLayer = symbol;
248 10679 jaume
                    owner.setOptionsPageFor(symbol);
249 9745 jaume
            }
250
    }
251
252
    private JButton getBtnAddLayer() {
253
        if (btnAddLayer == null) {
254 14821 jmvivo
                btnAddLayer = new JButton(PluginServices.getIconTheme().get("add-layer-icono"));
255 9745 jaume
            btnAddLayer.setActionCommand("ADD");
256
            btnAddLayer.setSize(btnDimension);
257
            btnAddLayer.setPreferredSize(btnDimension);
258
            btnAddLayer.addActionListener(this);
259
        }
260
        return btnAddLayer;
261
    }
262
263
    private JPanel getPnlButtons() {
264
        if (pnlButtons == null) {
265
            pnlButtons = new JPanel();
266
            pnlButtons.add(getBtnAddLayer(), null);
267
            pnlButtons.add(getBtnRemoveLayer(), null);
268
            pnlButtons.add(getBtnMoveUpLayer(), null);
269
            pnlButtons.add(getBtnMoveDown(), null);
270
        }
271
        return pnlButtons;
272
    }
273
274
    private JButton getBtnRemoveLayer() {
275
        if (btnRemoveLayer == null) {
276 14821 jmvivo
            btnRemoveLayer = new JButton(PluginServices.getIconTheme().get("delete-icono"));
277 9745 jaume
            btnRemoveLayer.setActionCommand("REMOVE");
278
            btnRemoveLayer.setSize(btnDimension);
279
            btnRemoveLayer.setPreferredSize(btnDimension);
280
            btnRemoveLayer.addActionListener(this);
281
        }
282
        return btnRemoveLayer;
283
    }
284
285
    private JButton getBtnMoveUpLayer() {
286
        if (btnMoveUpLayer == null) {
287 14821 jmvivo
            btnMoveUpLayer = new JButton(PluginServices.getIconTheme().get("arrow-up-icono"));
288 9745 jaume
            btnMoveUpLayer.setActionCommand("MOVE_UP");
289
            btnMoveUpLayer.setSize(btnDimension);
290
            btnMoveUpLayer.setPreferredSize(btnDimension);
291
            btnMoveUpLayer.addActionListener(this);
292
        }
293
        return btnMoveUpLayer;
294
    }
295
296
    private JButton getBtnMoveDown() {
297
        if (btnMoveDownLayer == null) {
298 14821 jmvivo
            btnMoveDownLayer = new JButton(PluginServices.getIconTheme().get("arrow-down-icono"));
299 9745 jaume
            btnMoveDownLayer.setActionCommand("MOVE_DOWN");
300
            btnMoveDownLayer.setSize(btnDimension);
301
            btnMoveDownLayer.setPreferredSize(btnDimension);
302
            btnMoveDownLayer.addActionListener(this);
303
        }
304
        return btnMoveDownLayer;
305
    }
306
307
    public void actionPerformed(ActionEvent e) {
308
        String command = e.getActionCommand();
309
310 10679 jaume
        int jlistIndex = getJListLayers().getSelectedIndex();
311
        int index = symbol.getLayerCount() - 1 - jlistIndex;
312
313
        if (command.equals("ADD")) {
314
                symbol.addLayer(owner.getNewLayer());
315
            index = symbol.getLayerCount();
316 9745 jaume
        } else if (command.equals("REMOVE")) {
317 14736 jdominguez
                int layerCount = symbol.getLayerCount();
318
                if (layerCount >1 && index < layerCount)
319 12994 jaume
                        symbol.removeLayer(symbol.getLayer(index));
320 9745 jaume
        } else if (command.equals("MOVE_UP")) {
321 10679 jaume
            if (index < symbol.getLayerCount()-1) {
322
                    symbol.swapLayers(index, index+1);
323
                jlistIndex--;
324 9745 jaume
            }
325
        } else if (command.equals("MOVE_DOWN")) {
326 10679 jaume
            if (index > 0) {
327
                    symbol.swapLayers(index, index-1);
328
                jlistIndex++;
329 9745 jaume
            }
330
        } else if (command.equals("LOCK")) {
331
            try {
332 10679 jaume
                XMLEntity layerXML = symbol.getLayer(index).getXMLEntity();
333
                boolean locked = !layerXML.contains("locked") || !layerXML.getBooleanProperty("locked");
334 9745 jaume
                layerXML.putProperty("locked", locked);
335
            } catch (Exception ex) {
336
                // Index out of bounds or so, we don't care
337
            }
338
        }
339
        owner.refresh();
340 10679 jaume
        validate();
341
        getJListLayers().setSelectedIndex(jlistIndex);
342 9745 jaume
343
    }
344
345 10679 jaume
    private class SymbolLayerListModel extends AbstractListModel {
346 12427 jaume
            private static final long serialVersionUID = 4197818146836802855L;
347
                private ArrayList listeners = new ArrayList();
348 9745 jaume
349 10679 jaume
        public int getSize() {
350
            return symbol.getLayerCount();
351
        }
352 9745 jaume
353 10679 jaume
        public Object getElementAt(int index) {
354
                return symbol.getLayer(symbol.getLayerCount()-1-index);
355
        }
356 9745 jaume
357 10679 jaume
        public void addListDataListener(ListDataListener l) {
358
                listeners.add(l);
359
        }
360 9745 jaume
361 10679 jaume
        public void removeListDataListener(ListDataListener l) {
362
                listeners.remove(l);
363
        }
364 9745 jaume
365
    }
366
367 10679 jaume
        public ISymbol getSelectedLayer() {
368
                return (ISymbol) getJListLayers().getSelectedValue();
369
        }
370 9745 jaume
371 10679 jaume
        public int getSelectedLayerIndex() {
372
                return getJListLayers().getSelectedIndex();
373
        }
374
375
        public void validate() { // patched to force the list to be painted when it starts empty
376
                jListLayers = null;
377
                getJScrollPane().setViewportView(getJListLayers());
378
                super.validate();
379
        }
380 11297 jaume
381
        private class SymbolLayerComponent extends JPanel {
382 12427 jaume
                private static final long serialVersionUID = -3706313315505454031L;
383
384 11297 jaume
                public SymbolLayerComponent(ISymbol sym, int index, boolean isSelected) {
385
                         LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 3, 3);
386
             setLayout(layout);
387
             Color bgColor = (isSelected) ? cellSelectedBGColor
388
                      : Color.WHITE;
389
             setBackground(bgColor);
390
             JCheckBox chkVisible = new JCheckBox("", symbol.getLayer(index).isShapeVisible());
391
             chkVisible.setBackground(bgColor);
392
             add(chkVisible);
393
             SymbolPreviewer sp = new SymbolPreviewer();
394
             sp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
395
             sp.setAlignmentX(Component.LEFT_ALIGNMENT);
396
             sp.setPreferredSize(new Dimension(80, 30));
397
             sp.setSize(new Dimension(80, 30));
398
             sp.setSymbol(sym);
399
             sp.setBackground(Color.WHITE);
400
             add(sp);
401
             XMLEntity xml = sym.getXMLEntity();
402
             String source;
403
             if (xml.contains("locked"))
404
                     source = xml.getBooleanProperty("locked") ?
405
                                     "images/locked.png" :
406
                                             "images/unlocked.png";
407
             else
408
                     source = "images/unlocked.png";
409
410
             ImageIcon icon = new ImageIcon(getClass().
411
                             getClassLoader().getResource(source));
412
             JButton btnLock = new JButton(icon);
413
             btnLock.setSize(btnDimension);
414
             btnLock.setPreferredSize(btnDimension);
415
             btnLock.setActionCommand("LOCK");
416
             btnLock.setBackground(bgColor);
417
             btnLock.setAlignmentX(Component.CENTER_ALIGNMENT);
418
             add(btnLock);
419
             setPreferredSize(LIST_CELL_SIZE);
420
421
                }
422
        }
423 9745 jaume
}
424 10679 jaume