Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolLayerManager.java @ 14725

History | View | Annotate | Download (13.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
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
 *   Av. Blasco Ib??ez, 50
24
 *   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: SymbolLayerManager.java 13729 2007-09-17 09:21:45Z jaume $
45
* $Log$
46
* 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
* first round of found bugs fixed
51
*
52
* Revision 1.6  2007/06/29 13:07:33  jaume
53
* +PictureLineSymbol
54
*
55
* Revision 1.5  2007/05/31 09:36:22  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4  2007/04/20 07:54:38  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.3  2007/03/09 11:25:00  jaume
62
* Advanced symbology (start committing)
63
*
64
* 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
* Revision 1.1  2007/01/16 11:52:11  jaume
77
* *** 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
import javax.swing.JCheckBox;
121
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
import org.gvsig.gui.beans.swing.JButton;
129

    
130
import com.iver.andami.PluginServices;
131
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
132
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
133
import com.iver.cit.gvsig.project.documents.view.legend.gui.ISymbolSelector;
134
import com.iver.utiles.XMLEntity;
135

    
136
/**
137
 * A component linked to a SymbolEditor that shows the layers
138
 *
139
 * @author jaume
140
 *
141
 */
142
public class SymbolLayerManager extends JPanel implements ActionListener{
143
   private static final long serialVersionUID = -1939951243066481691L;
144
        private static final Dimension LIST_CELL_SIZE = new Dimension(150,37);
145
        private SymbolEditor owner;
146
    private JList jListLayers;
147
    private ISymbol activeLayer;
148
    private IMultiLayerSymbol symbol;
149
    private JButton btnAddLayer = null;
150
    private JPanel pnlButtons = null;
151
    private JButton btnRemoveLayer = null;
152
    private JButton btnMoveUpLayer = null;
153
    private JButton btnMoveDownLayer = null;
154
    private final Dimension btnDimension = new Dimension(24, 24);
155
    private JScrollPane scroll;
156
    private Color cellSelectedBGColor = Color.BLUE;
157

    
158

    
159
    public SymbolLayerManager(SymbolEditor owner) {
160
        this.owner = owner;
161
        initialize();
162
        this.symbol = (IMultiLayerSymbol) owner.getSymbol();
163

    
164
    }
165

    
166
    private void initialize() {
167
        this.setLayout(new BorderLayout());
168
        this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
169
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
170
    }
171

    
172
    private JScrollPane getJScrollPane() {
173
        if (scroll ==  null) {
174
            scroll = new JScrollPane();
175
            scroll.setPreferredSize(new Dimension(150, 160));
176
            scroll.setViewportView(getJListLayers());
177
        }
178
        return scroll;
179
    }
180

    
181
    private JList getJListLayers() {
182
        if (jListLayers == null) {
183
            jListLayers = new JList();
184
            jListLayers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
185
            jListLayers.setLayoutOrientation(JList.HORIZONTAL_WRAP);
186

    
187
            jListLayers.setVisibleRowCount(-1);
188
            jListLayers.addListSelectionListener(new ListSelectionListener() {
189
                public void valueChanged(ListSelectionEvent e) {
190
                    setLayer((ISymbol) jListLayers.getSelectedValue());
191
                }
192
            });
193
            jListLayers.setModel(new SymbolLayerListModel());
194
            jListLayers.setCellRenderer(new DefaultListCellRenderer() {
195
                      private static final long serialVersionUID = -2551357351239544039L;
196

    
197
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
198
                        return new SymbolLayerComponent((ISymbol) value, index, isSelected);
199
                }
200
            });
201
            jListLayers.addMouseListener(new MouseAdapter() {
202
                                public void mouseClicked(MouseEvent e) {
203
                                        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
204
                                                ISymbolSelector symSel = SymbolSelector.createSymbolSelector(activeLayer, owner.getShapeType());
205
                                                PluginServices.getMDIManager().addWindow(symSel);
206
                                                updateSymbol((ISymbol) symSel.getSelectedObject());
207
                                                setLayer((ISymbol) symSel.getSelectedObject());
208
                                        }
209

    
210
                                }
211
            });
212
        }
213
        return jListLayers;
214
    }
215

    
216
    private void updateSymbol(ISymbol layer) {
217
            if (layer != null) {
218
                    int index = getJListLayers().getSelectedIndex();
219
                    symbol.setLayer(index, layer);
220
                    owner.refresh();
221
                    validate();
222
            }
223
    }
224

    
225
    private void setLayer(ISymbol symbol) {
226
            if (symbol!=null) {
227
                    activeLayer = symbol;
228
                    owner.setOptionsPageFor(symbol);
229
            }
230
    }
231

    
232
    private JButton getBtnAddLayer() {
233
        if (btnAddLayer == null) {
234
            btnAddLayer = new JButton(new ImageIcon(getClass().
235
                    getClassLoader().getResource("images/add-layer.png")));
236
            btnAddLayer.setActionCommand("ADD");
237
            btnAddLayer.setSize(btnDimension);
238
            btnAddLayer.setPreferredSize(btnDimension);
239
            btnAddLayer.addActionListener(this);
240
        }
241
        return btnAddLayer;
242
    }
243

    
244
    private JPanel getPnlButtons() {
245
        if (pnlButtons == null) {
246
            pnlButtons = new JPanel();
247
            pnlButtons.add(getBtnAddLayer(), null);
248
            pnlButtons.add(getBtnRemoveLayer(), null);
249
            pnlButtons.add(getBtnMoveUpLayer(), null);
250
            pnlButtons.add(getBtnMoveDown(), null);
251
        }
252
        return pnlButtons;
253
    }
254

    
255
    private JButton getBtnRemoveLayer() {
256
        if (btnRemoveLayer == null) {
257
            btnRemoveLayer = new JButton(new ImageIcon(getClass().
258
                    getClassLoader().getResource("images/delete.png")));
259
            btnRemoveLayer.setActionCommand("REMOVE");
260
            btnRemoveLayer.setSize(btnDimension);
261
            btnRemoveLayer.setPreferredSize(btnDimension);
262
            btnRemoveLayer.addActionListener(this);
263
        }
264
        return btnRemoveLayer;
265
    }
266

    
267
    private JButton getBtnMoveUpLayer() {
268
        if (btnMoveUpLayer == null) {
269
            btnMoveUpLayer = new JButton(new ImageIcon(getClass().
270
                    getClassLoader().getResource("images/up-arrow.png")));
271
            btnMoveUpLayer.setActionCommand("MOVE_UP");
272
            btnMoveUpLayer.setSize(btnDimension);
273
            btnMoveUpLayer.setPreferredSize(btnDimension);
274
            btnMoveUpLayer.addActionListener(this);
275
        }
276
        return btnMoveUpLayer;
277
    }
278

    
279
    private JButton getBtnMoveDown() {
280
        if (btnMoveDownLayer == null) {
281
            btnMoveDownLayer = new JButton(new ImageIcon(getClass().
282
                    getClassLoader().getResource("images/down-arrow.png")));
283
            btnMoveDownLayer.setActionCommand("MOVE_DOWN");
284
            btnMoveDownLayer.setSize(btnDimension);
285
            btnMoveDownLayer.setPreferredSize(btnDimension);
286
            btnMoveDownLayer.addActionListener(this);
287
        }
288
        return btnMoveDownLayer;
289
    }
290

    
291
    public void actionPerformed(ActionEvent e) {
292
        String command = e.getActionCommand();
293

    
294
        int jlistIndex = getJListLayers().getSelectedIndex();
295
        int index = symbol.getLayerCount() - 1 - jlistIndex;
296

    
297
        if (command.equals("ADD")) {
298
                symbol.addLayer(owner.getNewLayer());
299
            index = symbol.getLayerCount();
300
        } else if (command.equals("REMOVE")) {
301
                if (index < symbol.getLayerCount())
302
                        symbol.removeLayer(symbol.getLayer(index));
303
        } else if (command.equals("MOVE_UP")) {
304
            if (index < symbol.getLayerCount()-1) {
305
                    symbol.swapLayers(index, index+1);
306
                jlistIndex--;
307
            }
308
        } else if (command.equals("MOVE_DOWN")) {
309
            if (index > 0) {
310
                    symbol.swapLayers(index, index-1);
311
                jlistIndex++;
312
            }
313
        } else if (command.equals("LOCK")) {
314
            try {
315
                XMLEntity layerXML = symbol.getLayer(index).getXMLEntity();
316
                boolean locked = !layerXML.contains("locked") || !layerXML.getBooleanProperty("locked");
317
                layerXML.putProperty("locked", locked);
318
            } catch (Exception ex) {
319
                // Index out of bounds or so, we don't care
320
            }
321
        }
322
        owner.refresh();
323
        validate();
324
        getJListLayers().setSelectedIndex(jlistIndex);
325

    
326
    }
327

    
328
    private class SymbolLayerListModel extends AbstractListModel {
329
            private static final long serialVersionUID = 4197818146836802855L;
330
                private ArrayList listeners = new ArrayList();
331

    
332
        public int getSize() {
333
            return symbol.getLayerCount();
334
        }
335

    
336
        public Object getElementAt(int index) {
337
                return symbol.getLayer(symbol.getLayerCount()-1-index);
338
        }
339

    
340
        public void addListDataListener(ListDataListener l) {
341
                listeners.add(l);
342
        }
343

    
344
        public void removeListDataListener(ListDataListener l) {
345
                listeners.remove(l);
346
        }
347

    
348
    }
349

    
350
        public ISymbol getSelectedLayer() {
351
                return (ISymbol) getJListLayers().getSelectedValue();
352
        }
353

    
354
        public int getSelectedLayerIndex() {
355
                return getJListLayers().getSelectedIndex();
356
        }
357

    
358
        public void validate() { // patched to force the list to be painted when it starts empty
359
                jListLayers = null;
360
                getJScrollPane().setViewportView(getJListLayers());
361
                super.validate();
362
        }
363

    
364
        private class SymbolLayerComponent extends JPanel {
365
                private static final long serialVersionUID = -3706313315505454031L;
366

    
367
                public SymbolLayerComponent(ISymbol sym, int index, boolean isSelected) {
368
                         LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 3, 3);
369
             setLayout(layout);
370
             Color bgColor = (isSelected) ? cellSelectedBGColor
371
                      : Color.WHITE;
372
             setBackground(bgColor);
373
             JCheckBox chkVisible = new JCheckBox("", symbol.getLayer(index).isShapeVisible());
374
             chkVisible.setBackground(bgColor);
375
             add(chkVisible);
376
             SymbolPreviewer sp = new SymbolPreviewer();
377
             sp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
378
             sp.setAlignmentX(Component.LEFT_ALIGNMENT);
379
             sp.setPreferredSize(new Dimension(80, 30));
380
             sp.setSize(new Dimension(80, 30));
381
             sp.setSymbol(sym);
382
             sp.setBackground(Color.WHITE);
383
             add(sp);
384
             XMLEntity xml = sym.getXMLEntity();
385
             String source;
386
             if (xml.contains("locked"))
387
                     source = xml.getBooleanProperty("locked") ?
388
                                     "images/locked.png" :
389
                                             "images/unlocked.png";
390
             else
391
                     source = "images/unlocked.png";
392

    
393
             ImageIcon icon = new ImageIcon(getClass().
394
                             getClassLoader().getResource(source));
395
             JButton btnLock = new JButton(icon);
396
             btnLock.setSize(btnDimension);
397
             btnLock.setPreferredSize(btnDimension);
398
             btnLock.setActionCommand("LOCK");
399
             btnLock.setBackground(bgColor);
400
             btnLock.setAlignmentX(Component.CENTER_ALIGNMENT);
401
             add(btnLock);
402
             setPreferredSize(LIST_CELL_SIZE);
403

    
404
                }
405
        }
406
}
407

    
408