Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2058 / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolLayerManager.java @ 39268

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.IconThemeHelper;
129
import org.gvsig.andami.PluginServices;
130
import org.gvsig.app.project.documents.view.legend.gui.ISymbolSelector;
131
import org.gvsig.fmap.geom.Geometry;
132
import org.gvsig.fmap.mapcontext.MapContextLocator;
133
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
134
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
135
import org.gvsig.gui.beans.swing.JButton;
136
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
137
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
138
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
139

    
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
   private static final long serialVersionUID = -1939951243066481691L;
149
        private static final Dimension LIST_CELL_SIZE = new Dimension(150,37);
150
        private SymbolEditor owner;
151
    private JList jListLayers;
152
    private ISymbol activeLayer;
153
    private IMultiLayerSymbol symbol;
154
    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
        this.symbol = (IMultiLayerSymbol) owner.getSymbol();
167
        if (symbol.getLayerCount()==0) {
168
                int shapeType = -1;
169
                        if (symbol instanceof IMarkerSymbol) {
170
                        shapeType = Geometry.TYPES.POINT;
171
                        } else if (symbol instanceof ILineSymbol) {
172
                        shapeType = Geometry.TYPES.CURVE;
173
                        } else if (symbol instanceof IFillSymbol) {
174
                        shapeType = Geometry.TYPES.SURFACE;
175
                        }
176

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

    
183
    }
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
        getJListLayers().setSelectedIndex(0);
190
    }
191

    
192
    private JScrollPane getJScrollPane() {
193
        if (scroll ==  null) {
194
            scroll = new JScrollPane();
195
            scroll.setPreferredSize(new Dimension(150, 160));
196
            scroll.setViewportView(getJListLayers());
197
        }
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
            jListLayers.setModel(new SymbolLayerListModel());
214
            jListLayers.setCellRenderer(new DefaultListCellRenderer() {
215
                      private static final long serialVersionUID = -2551357351239544039L;
216

    
217
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
218
                        return new SymbolLayerComponent((ISymbol) value, index, isSelected);
219
                }
220
            });
221
            jListLayers.addMouseListener(new MouseAdapter() {
222
                                public void mouseClicked(MouseEvent e) {
223
                                        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
224
                                                ISymbolSelector symSel = SymbolSelector.createSymbolSelector(activeLayer, owner.getShapeType());
225
                                                PluginServices.getMDIManager().addWindow(symSel);
226
                                                
227
                                                ISymbol new_sym = (ISymbol) symSel.getSelectedObject();
228
                                                /*
229
                                                 * new_sym can be null (should not be a problem)
230
                                                 */
231
                                                updateSymbol(new_sym);
232
                                                setLayer(new_sym);
233
                                        }
234

    
235
                                }
236
            });
237
        }
238
        return jListLayers;
239
    }
240

    
241
    private void updateSymbol(ISymbol layer) {
242
            if (layer != null) {
243
                    int index = getJListLayers().getSelectedIndex();
244
                    symbol.setLayer(index, layer);
245
                    owner.refresh();
246
                    validate();
247
            }
248
    }
249

    
250
    private void setLayer(ISymbol symbol) {
251
            if (symbol!=null) {
252
                    activeLayer = symbol;
253
                    owner.setOptionsPageFor(symbol);
254
            }
255
    }
256

    
257
    private JButton getBtnAddLayer() {
258
        if (btnAddLayer == null) {
259
                btnAddLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-add"));
260
            btnAddLayer.setActionCommand("ADD");
261
            btnAddLayer.setSize(btnDimension);
262
            btnAddLayer.setPreferredSize(btnDimension);
263
            btnAddLayer.addActionListener(this);
264
        }
265
        return btnAddLayer;
266
    }
267

    
268
    private JPanel getPnlButtons() {
269
        if (pnlButtons == null) {
270
            pnlButtons = new JPanel();
271
            pnlButtons.add(getBtnAddLayer(), null);
272
            pnlButtons.add(getBtnRemoveLayer(), null);
273
            pnlButtons.add(getBtnMoveUpLayer(), null);
274
            pnlButtons.add(getBtnMoveDown(), null);
275
        }
276
        return pnlButtons;
277
    }
278

    
279
    private JButton getBtnRemoveLayer() {
280
        if (btnRemoveLayer == null) {
281
            btnRemoveLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-remove"));
282
            btnRemoveLayer.setActionCommand("REMOVE");
283
            btnRemoveLayer.setSize(btnDimension);
284
            btnRemoveLayer.setPreferredSize(btnDimension);
285
            btnRemoveLayer.addActionListener(this);
286
        }
287
        return btnRemoveLayer;
288
    }
289

    
290
    private JButton getBtnMoveUpLayer() {
291
        if (btnMoveUpLayer == null) {
292
            btnMoveUpLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-move-up"));
293
            btnMoveUpLayer.setActionCommand("MOVE_UP");
294
            btnMoveUpLayer.setSize(btnDimension);
295
            btnMoveUpLayer.setPreferredSize(btnDimension);
296
            btnMoveUpLayer.addActionListener(this);
297
        }
298
        return btnMoveUpLayer;
299
    }
300

    
301
    private JButton getBtnMoveDown() {
302
        if (btnMoveDownLayer == null) {
303
            btnMoveDownLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-move-down"));
304
            btnMoveDownLayer.setActionCommand("MOVE_DOWN");
305
            btnMoveDownLayer.setSize(btnDimension);
306
            btnMoveDownLayer.setPreferredSize(btnDimension);
307
            btnMoveDownLayer.addActionListener(this);
308
        }
309
        return btnMoveDownLayer;
310
    }
311

    
312
    public void actionPerformed(ActionEvent e) {
313
        String command = e.getActionCommand();
314

    
315
        int jlistIndex = getJListLayers().getSelectedIndex();
316
        int index = symbol.getLayerCount() - 1 - jlistIndex;
317

    
318
        if (command.equals("ADD")) {
319
                symbol.addLayer(owner.getNewLayer());
320
            index = symbol.getLayerCount();
321
        } else if (command.equals("REMOVE")) {
322
                int layerCount = symbol.getLayerCount();
323
                if (layerCount >1 && index < layerCount)
324
                        symbol.removeLayer(symbol.getLayer(index));
325
        } else if (command.equals("MOVE_UP")) {
326
            if (index < symbol.getLayerCount()-1) {
327
                    symbol.swapLayers(index, index+1);
328
                jlistIndex--;
329
            }
330
        } else if (command.equals("MOVE_DOWN")) {
331
            if (index > 0) {
332
                    symbol.swapLayers(index, index-1);
333
                jlistIndex++;
334
            }
335
        } else if (command.equals("LOCK")) {
336
            try {
337
//XMLENTITY-UPDATED                   
338
//                XMLEntity layerXML = symbol.getLayer(index).getXMLEntity();
339
//                boolean locked = !layerXML.contains("locked") || !layerXML.getBooleanProperty("locked");
340
//                layerXML.putProperty("locked", locked);
341
            } catch (Exception ex) {
342
                // Index out of bounds or so, we don't care
343
            }
344
        }
345
        owner.refresh();
346
        validate();
347
        getJListLayers().setSelectedIndex(jlistIndex);
348

    
349
    }
350

    
351
    private class SymbolLayerListModel extends AbstractListModel {
352
            private static final long serialVersionUID = 4197818146836802855L;
353
                private ArrayList listeners = new ArrayList();
354

    
355
        public int getSize() {
356
            return symbol.getLayerCount();
357
        }
358

    
359
        public Object getElementAt(int index) {
360
                return symbol.getLayer(symbol.getLayerCount()-1-index);
361
        }
362

    
363
        public void addListDataListener(ListDataListener l) {
364
                listeners.add(l);
365
        }
366

    
367
        public void removeListDataListener(ListDataListener l) {
368
                listeners.remove(l);
369
        }
370

    
371
    }
372

    
373
        public ISymbol getSelectedLayer() {
374
                return (ISymbol) getJListLayers().getSelectedValue();
375
        }
376

    
377
        public int getSelectedLayerIndex() {
378
                return getJListLayers().getSelectedIndex();
379
        }
380

    
381
        public void validate() { // patched to force the list to be painted when it starts empty
382
                jListLayers = null;
383
                getJScrollPane().setViewportView(getJListLayers());
384
                super.validate();
385
        }
386

    
387
        private class SymbolLayerComponent extends JPanel {
388
                private static final long serialVersionUID = -3706313315505454031L;
389

    
390
                public SymbolLayerComponent(ISymbol sym, int index, boolean isSelected) {
391
                         LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 3, 3);
392
             setLayout(layout);
393
             Color bgColor = (isSelected) ? cellSelectedBGColor
394
                      : Color.WHITE;
395
             setBackground(bgColor);
396
             JCheckBox chkVisible = new JCheckBox("", symbol.getLayer(index).isShapeVisible());
397
             chkVisible.setBackground(bgColor);
398
             add(chkVisible);
399
             SymbolPreviewer sp = new SymbolPreviewer();
400
             sp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
401
             sp.setAlignmentX(Component.LEFT_ALIGNMENT);
402
             sp.setPreferredSize(new Dimension(80, 30));
403
             sp.setSize(new Dimension(80, 30));
404
             sp.setSymbol(sym);
405
             sp.setBackground(Color.WHITE);
406
             add(sp);
407
             
408
             // TODO: which is the meaning of a "locked" symbol, and where is defined??
409
//             XMLEntity xml=null;
410
////XMLENTITY-UPDATED        
411
////                        try {
412
////                                xml = sym.getXMLEntity();
413
////                        } catch (XMLException e) {
414
////                                NotificationManager.addWarning("Symbol layer", e);
415
////                        }
416
//             String source;
417
//             if (xml.contains("locked"))
418
//                     source = xml.getBooleanProperty("locked") ?
419
//                                     "images/locked.png" :
420
//                                             "images/unlocked.png";
421
//             else
422
//                     source = "images/unlocked.png";
423

    
424
             
425
             JButton btnLock = new JButton(IconThemeHelper.getImageIcon("symbol-layer-unlock"));
426
             btnLock.setSize(btnDimension);
427
             btnLock.setPreferredSize(btnDimension);
428
             btnLock.setActionCommand("LOCK");
429
             btnLock.setBackground(bgColor);
430
             btnLock.setAlignmentX(Component.CENTER_ALIGNMENT);
431
             add(btnLock);
432
             setPreferredSize(LIST_CELL_SIZE);
433

    
434
                }
435
        }
436
}
437

    
438