Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolLayerManager.java @ 34291

History | View | Annotate | Download (14.4 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 31067 2009-10-01 16:11:25Z cordinyana $
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 org.gvsig.app.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.andami.PluginServices;
129
import org.gvsig.app.project.documents.view.legend.gui.ISymbolSelector;
130
import org.gvsig.fmap.geom.Geometry;
131
import org.gvsig.fmap.mapcontext.MapContextLocator;
132
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
133
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
134
import org.gvsig.gui.beans.swing.JButton;
135
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
136
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
137
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
138

    
139

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

    
162

    
163
    public SymbolLayerManager(SymbolEditor owner) {
164
        this.owner = owner;
165
        this.symbol = (IMultiLayerSymbol) owner.getSymbol();
166
        if (symbol.getLayerCount()==0) {
167
                int shapeType = -1;
168
                        if (symbol instanceof IMarkerSymbol) {
169
                        shapeType = Geometry.TYPES.POINT;
170
                        } else if (symbol instanceof ILineSymbol) {
171
                        shapeType = Geometry.TYPES.CURVE;
172
                        } else if (symbol instanceof IFillSymbol) {
173
                        shapeType = Geometry.TYPES.SURFACE;
174
                        }
175

    
176
                if (shapeType != -1)
177
                                symbol.addLayer(MapContextLocator.getSymbolManager()
178
                                                .createSymbol(shapeType));
179
                   }
180
        initialize();
181

    
182
    }
183

    
184
    private void initialize() {
185
        this.setLayout(new BorderLayout());
186
        this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
187
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
188
        getJListLayers().setSelectedIndex(0);
189
    }
190

    
191
    private JScrollPane getJScrollPane() {
192
        if (scroll ==  null) {
193
            scroll = new JScrollPane();
194
            scroll.setPreferredSize(new Dimension(150, 160));
195
            scroll.setViewportView(getJListLayers());
196
        }
197
        return scroll;
198
    }
199

    
200
    private JList getJListLayers() {
201
        if (jListLayers == null) {
202
            jListLayers = new JList();
203
            jListLayers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
204
            jListLayers.setLayoutOrientation(JList.HORIZONTAL_WRAP);
205

    
206
            jListLayers.setVisibleRowCount(-1);
207
            jListLayers.addListSelectionListener(new ListSelectionListener() {
208
                public void valueChanged(ListSelectionEvent e) {
209
                    setLayer((ISymbol) jListLayers.getSelectedValue());
210
                }
211
            });
212
            jListLayers.setModel(new SymbolLayerListModel());
213
            jListLayers.setCellRenderer(new DefaultListCellRenderer() {
214
                      private static final long serialVersionUID = -2551357351239544039L;
215

    
216
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
217
                        return new SymbolLayerComponent((ISymbol) value, index, isSelected);
218
                }
219
            });
220
            jListLayers.addMouseListener(new MouseAdapter() {
221
                                public void mouseClicked(MouseEvent e) {
222
                                        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
223
                                                ISymbolSelector symSel = SymbolSelector.createSymbolSelector(activeLayer, owner.getShapeType());
224
                                                PluginServices.getMDIManager().addWindow(symSel);
225
                                                updateSymbol((ISymbol) symSel.getSelectedObject());
226
                                                setLayer((ISymbol) symSel.getSelectedObject());
227
                                        }
228

    
229
                                }
230
            });
231
        }
232
        return jListLayers;
233
    }
234

    
235
    private void updateSymbol(ISymbol layer) {
236
            if (layer != null) {
237
                    int index = getJListLayers().getSelectedIndex();
238
                    symbol.setLayer(index, layer);
239
                    owner.refresh();
240
                    validate();
241
            }
242
    }
243

    
244
    private void setLayer(ISymbol symbol) {
245
            if (symbol!=null) {
246
                    activeLayer = symbol;
247
                    owner.setOptionsPageFor(symbol);
248
            }
249
    }
250

    
251
    private JButton getBtnAddLayer() {
252
        if (btnAddLayer == null) {
253
                btnAddLayer = new JButton(PluginServices.getIconTheme().get("add-layer-icono"));
254
            btnAddLayer.setActionCommand("ADD");
255
            btnAddLayer.setSize(btnDimension);
256
            btnAddLayer.setPreferredSize(btnDimension);
257
            btnAddLayer.addActionListener(this);
258
        }
259
        return btnAddLayer;
260
    }
261

    
262
    private JPanel getPnlButtons() {
263
        if (pnlButtons == null) {
264
            pnlButtons = new JPanel();
265
            pnlButtons.add(getBtnAddLayer(), null);
266
            pnlButtons.add(getBtnRemoveLayer(), null);
267
            pnlButtons.add(getBtnMoveUpLayer(), null);
268
            pnlButtons.add(getBtnMoveDown(), null);
269
        }
270
        return pnlButtons;
271
    }
272

    
273
    private JButton getBtnRemoveLayer() {
274
        if (btnRemoveLayer == null) {
275
            btnRemoveLayer = new JButton(PluginServices.getIconTheme().get("delete-icono"));
276
            btnRemoveLayer.setActionCommand("REMOVE");
277
            btnRemoveLayer.setSize(btnDimension);
278
            btnRemoveLayer.setPreferredSize(btnDimension);
279
            btnRemoveLayer.addActionListener(this);
280
        }
281
        return btnRemoveLayer;
282
    }
283

    
284
    private JButton getBtnMoveUpLayer() {
285
        if (btnMoveUpLayer == null) {
286
            btnMoveUpLayer = new JButton(PluginServices.getIconTheme().get("arrow-up-icono"));
287
            btnMoveUpLayer.setActionCommand("MOVE_UP");
288
            btnMoveUpLayer.setSize(btnDimension);
289
            btnMoveUpLayer.setPreferredSize(btnDimension);
290
            btnMoveUpLayer.addActionListener(this);
291
        }
292
        return btnMoveUpLayer;
293
    }
294

    
295
    private JButton getBtnMoveDown() {
296
        if (btnMoveDownLayer == null) {
297
            btnMoveDownLayer = new JButton(PluginServices.getIconTheme().get("arrow-down-icono"));
298
            btnMoveDownLayer.setActionCommand("MOVE_DOWN");
299
            btnMoveDownLayer.setSize(btnDimension);
300
            btnMoveDownLayer.setPreferredSize(btnDimension);
301
            btnMoveDownLayer.addActionListener(this);
302
        }
303
        return btnMoveDownLayer;
304
    }
305

    
306
    public void actionPerformed(ActionEvent e) {
307
        String command = e.getActionCommand();
308

    
309
        int jlistIndex = getJListLayers().getSelectedIndex();
310
        int index = symbol.getLayerCount() - 1 - jlistIndex;
311

    
312
        if (command.equals("ADD")) {
313
                symbol.addLayer(owner.getNewLayer());
314
            index = symbol.getLayerCount();
315
        } else if (command.equals("REMOVE")) {
316
                int layerCount = symbol.getLayerCount();
317
                if (layerCount >1 && index < layerCount)
318
                        symbol.removeLayer(symbol.getLayer(index));
319
        } else if (command.equals("MOVE_UP")) {
320
            if (index < symbol.getLayerCount()-1) {
321
                    symbol.swapLayers(index, index+1);
322
                jlistIndex--;
323
            }
324
        } else if (command.equals("MOVE_DOWN")) {
325
            if (index > 0) {
326
                    symbol.swapLayers(index, index-1);
327
                jlistIndex++;
328
            }
329
        } else if (command.equals("LOCK")) {
330
            try {
331
//XMLENTITY-UPDATED                   
332
//                XMLEntity layerXML = symbol.getLayer(index).getXMLEntity();
333
//                boolean locked = !layerXML.contains("locked") || !layerXML.getBooleanProperty("locked");
334
//                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
        validate();
341
        getJListLayers().setSelectedIndex(jlistIndex);
342

    
343
    }
344

    
345
    private class SymbolLayerListModel extends AbstractListModel {
346
            private static final long serialVersionUID = 4197818146836802855L;
347
                private ArrayList listeners = new ArrayList();
348

    
349
        public int getSize() {
350
            return symbol.getLayerCount();
351
        }
352

    
353
        public Object getElementAt(int index) {
354
                return symbol.getLayer(symbol.getLayerCount()-1-index);
355
        }
356

    
357
        public void addListDataListener(ListDataListener l) {
358
                listeners.add(l);
359
        }
360

    
361
        public void removeListDataListener(ListDataListener l) {
362
                listeners.remove(l);
363
        }
364

    
365
    }
366

    
367
        public ISymbol getSelectedLayer() {
368
                return (ISymbol) getJListLayers().getSelectedValue();
369
        }
370

    
371
        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

    
381
        private class SymbolLayerComponent extends JPanel {
382
                private static final long serialVersionUID = -3706313315505454031L;
383

    
384
                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
             
402
             // TODO: which is the meaning of a "locked" symbol, and where is defined??
403
//             XMLEntity xml=null;
404
////XMLENTITY-UPDATED        
405
////                        try {
406
////                                xml = sym.getXMLEntity();
407
////                        } catch (XMLException e) {
408
////                                NotificationManager.addWarning("Symbol layer", e);
409
////                        }
410
//             String source;
411
//             if (xml.contains("locked"))
412
//                     source = xml.getBooleanProperty("locked") ?
413
//                                     "images/locked.png" :
414
//                                             "images/unlocked.png";
415
//             else
416
//                     source = "images/unlocked.png";
417
             String source = "images/unlocked.png";
418

    
419
             ImageIcon icon = new ImageIcon(getClass().
420
                             getClassLoader().getResource(source));
421
             JButton btnLock = new JButton(icon);
422
             btnLock.setSize(btnDimension);
423
             btnLock.setPreferredSize(btnDimension);
424
             btnLock.setActionCommand("LOCK");
425
             btnLock.setBackground(bgColor);
426
             btnLock.setAlignmentX(Component.CENTER_ALIGNMENT);
427
             add(btnLock);
428
             setPreferredSize(LIST_CELL_SIZE);
429

    
430
                }
431
        }
432
}
433

    
434