Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolLayerManager.java @ 47476

History | View | Annotate | Download (14.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: SymbolLayerManager.java 31067 2009-10-01 16:11:25Z cordinyana $
27
* $Log$
28
* Revision 1.8  2007-09-17 09:21:45  jaume
29
* refactored SymboSelector (added support for multishapedsymbol)
30
*
31
* Revision 1.7  2007/08/09 10:39:04  jaume
32
* first round of found bugs fixed
33
*
34
* Revision 1.6  2007/06/29 13:07:33  jaume
35
* +PictureLineSymbol
36
*
37
* Revision 1.5  2007/05/31 09:36:22  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.4  2007/04/20 07:54:38  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.3  2007/03/09 11:25:00  jaume
44
* Advanced symbology (start committing)
45
*
46
* Revision 1.1.2.4  2007/02/21 07:35:14  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.3  2007/02/08 15:45:37  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.2  2007/02/08 15:43:04  jaume
53
* some bug fixes in the editor and removed unnecessary imports
54
*
55
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.1  2007/01/16 11:52:11  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.7  2007/01/10 17:05:05  jaume
62
* moved to FMap and gvSIG
63
*
64
* Revision 1.6  2006/11/08 10:56:47  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.5  2006/11/07 08:52:30  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.4  2006/11/06 17:08:45  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.3  2006/11/06 16:06:52  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.2  2006/11/06 07:33:54  jaume
77
* javadoc, source style
78
*
79
* Revision 1.1  2006/11/02 17:19:28  jaume
80
* *** empty log message ***
81
*
82
*
83
*/
84
package org.gvsig.app.gui.styling;
85

    
86
import java.awt.BorderLayout;
87
import java.awt.Color;
88
import java.awt.Component;
89
import java.awt.Dimension;
90
import java.awt.FlowLayout;
91
import java.awt.LayoutManager;
92
import java.awt.event.ActionEvent;
93
import java.awt.event.ActionListener;
94
import java.awt.event.MouseAdapter;
95
import java.awt.event.MouseEvent;
96
import java.util.ArrayList;
97

    
98
import javax.swing.AbstractListModel;
99
import javax.swing.BorderFactory;
100
import javax.swing.DefaultListCellRenderer;
101
import javax.swing.ImageIcon;
102
import javax.swing.JCheckBox;
103
import javax.swing.JList;
104
import javax.swing.JPanel;
105
import javax.swing.JScrollPane;
106
import javax.swing.event.ListDataListener;
107
import javax.swing.event.ListSelectionEvent;
108
import javax.swing.event.ListSelectionListener;
109

    
110
import org.gvsig.andami.IconThemeHelper;
111
import org.gvsig.andami.PluginServices;
112
import org.gvsig.app.project.documents.view.legend.gui.ISymbolSelector;
113
import org.gvsig.fmap.geom.Geometry;
114
import org.gvsig.fmap.mapcontext.MapContextLocator;
115
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
116
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
117
import org.gvsig.gui.beans.swing.JButton;
118
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
119
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
120
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
121

    
122

    
123
/**
124
 * A component linked to a SymbolEditor that shows the layers
125
 *
126
 * @author jaume
127
 *
128
 */
129
public class SymbolLayerManager extends JPanel implements ActionListener{
130
   private static final long serialVersionUID = -1939951243066481691L;
131
        private static final Dimension LIST_CELL_SIZE = new Dimension(150,37);
132
        private SymbolEditor owner;
133
    private JList jListLayers;
134
    private ISymbol activeLayer;
135
    private IMultiLayerSymbol symbol;
136
    private JButton btnAddLayer = null;
137
    private JPanel pnlButtons = null;
138
    private JButton btnRemoveLayer = null;
139
    private JButton btnMoveUpLayer = null;
140
    private JButton btnMoveDownLayer = null;
141
    private final Dimension btnDimension = new Dimension(24, 24);
142
    private JScrollPane scroll;
143
    private Color cellSelectedBGColor = Color.BLUE;
144

    
145

    
146
    public SymbolLayerManager(SymbolEditor owner) {
147
        this.owner = owner;
148
        this.symbol = (IMultiLayerSymbol) owner.getSymbol();
149
        if (symbol.getLayerCount()==0) {
150
                int shapeType = -1;
151
                        if (symbol instanceof IMarkerSymbol) {
152
                        shapeType = Geometry.TYPES.POINT;
153
                        } else if (symbol instanceof ILineSymbol) {
154
                        shapeType = Geometry.TYPES.CURVE;
155
                        } else if (symbol instanceof IFillSymbol) {
156
                        shapeType = Geometry.TYPES.SURFACE;
157
                        }
158

    
159
                if (shapeType != -1) {
160
                                symbol.addLayer(MapContextLocator.getSymbolManager().createSymbol(shapeType));
161
            }
162
                   }
163
        initialize();
164

    
165
    }
166

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

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

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

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

    
199
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
200
                        return new SymbolLayerComponent((ISymbol) value, index, isSelected);
201
                }
202
            });
203
            jListLayers.addMouseListener(new MouseAdapter() {
204
                                public void mouseClicked(MouseEvent e) {
205
                                        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
206
                                                ISymbolSelector symSel = SymbolSelector.createSymbolSelector(activeLayer, owner.getShapeType());
207
                                                PluginServices.getMDIManager().addWindow(symSel);
208
                                                
209
                                                ISymbol new_sym = (ISymbol) symSel.getSelectedObject();
210
                                                /*
211
                                                 * new_sym can be null (should not be a problem)
212
                                                 */
213
                                                updateSymbol(new_sym);
214
                                                setLayer(new_sym);
215
                                        }
216

    
217
                                }
218
            });
219
        }
220
        return jListLayers;
221
    }
222

    
223
    private void updateSymbol(ISymbol layer) {
224
            if (layer != null) {
225
                JList layers = getJListLayers();
226
                    int index = layers.getSelectedIndex();
227
                int size = layers.getModel().getSize();
228
                
229
                    symbol.setLayer(size - index - 1, layer);
230
                    owner.refresh();
231
                    validate();
232
            }
233
    }
234

    
235
    private void setLayer(ISymbol symbol) {
236
            if (symbol!=null) {
237
                    activeLayer = symbol;
238
                    owner.setOptionsPageFor(symbol);
239
            }
240
    }
241

    
242
    private JButton getBtnAddLayer() {
243
        if (btnAddLayer == null) {
244
                btnAddLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-add"));
245
            btnAddLayer.setActionCommand("ADD");
246
            btnAddLayer.setSize(btnDimension);
247
            btnAddLayer.setPreferredSize(btnDimension);
248
            btnAddLayer.addActionListener(this);
249
        }
250
        return btnAddLayer;
251
    }
252

    
253
    private JPanel getPnlButtons() {
254
        if (pnlButtons == null) {
255
            pnlButtons = new JPanel();
256
            pnlButtons.add(getBtnAddLayer(), null);
257
            pnlButtons.add(getBtnRemoveLayer(), null);
258
            pnlButtons.add(getBtnMoveUpLayer(), null);
259
            pnlButtons.add(getBtnMoveDown(), null);
260
        }
261
        return pnlButtons;
262
    }
263

    
264
    private JButton getBtnRemoveLayer() {
265
        if (btnRemoveLayer == null) {
266
            btnRemoveLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-remove"));
267
            btnRemoveLayer.setActionCommand("REMOVE");
268
            btnRemoveLayer.setSize(btnDimension);
269
            btnRemoveLayer.setPreferredSize(btnDimension);
270
            btnRemoveLayer.addActionListener(this);
271
        }
272
        return btnRemoveLayer;
273
    }
274

    
275
    private JButton getBtnMoveUpLayer() {
276
        if (btnMoveUpLayer == null) {
277
            btnMoveUpLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-move-up"));
278
            btnMoveUpLayer.setActionCommand("MOVE_UP");
279
            btnMoveUpLayer.setSize(btnDimension);
280
            btnMoveUpLayer.setPreferredSize(btnDimension);
281
            btnMoveUpLayer.addActionListener(this);
282
        }
283
        return btnMoveUpLayer;
284
    }
285

    
286
    private JButton getBtnMoveDown() {
287
        if (btnMoveDownLayer == null) {
288
            btnMoveDownLayer = new JButton(PluginServices.getIconTheme().get("symbol-layer-move-down"));
289
            btnMoveDownLayer.setActionCommand("MOVE_DOWN");
290
            btnMoveDownLayer.setSize(btnDimension);
291
            btnMoveDownLayer.setPreferredSize(btnDimension);
292
            btnMoveDownLayer.addActionListener(this);
293
        }
294
        return btnMoveDownLayer;
295
    }
296

    
297
    public void actionPerformed(ActionEvent e) {
298
        String command = e.getActionCommand();
299

    
300
        int jlistIndex = getJListLayers().getSelectedIndex();
301
        int index = symbol.getLayerCount() - 1 - jlistIndex;
302

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

    
334
    }
335

    
336
    private class SymbolLayerListModel extends AbstractListModel {
337
            private static final long serialVersionUID = 4197818146836802855L;
338
                private ArrayList listeners = new ArrayList();
339

    
340
        public int getSize() {
341
            return symbol.getLayerCount();
342
        }
343

    
344
        public Object getElementAt(int index) {
345
                return symbol.getLayer(symbol.getLayerCount()-1-index);
346
        }
347

    
348
        public void addListDataListener(ListDataListener l) {
349
                listeners.add(l);
350
        }
351

    
352
        public void removeListDataListener(ListDataListener l) {
353
                listeners.remove(l);
354
        }
355

    
356
    }
357

    
358
        public ISymbol getSelectedLayer() {
359
                return (ISymbol) getJListLayers().getSelectedValue();
360
        }
361

    
362
        public int getSelectedLayerIndex() {
363
                return getJListLayers().getSelectedIndex();
364
        }
365

    
366
        public void validate() { // patched to force the list to be painted when it starts empty
367
                jListLayers = null;
368
                getJScrollPane().setViewportView(getJListLayers());
369
                super.validate();
370
        }
371

    
372
        private class SymbolLayerComponent extends JPanel {
373
                private static final long serialVersionUID = -3706313315505454031L;
374

    
375
                public SymbolLayerComponent(ISymbol sym, int index, boolean isSelected) {
376
                         LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 3, 3);
377
             setLayout(layout);
378
             Color bgColor = (isSelected) ? cellSelectedBGColor
379
                      : Color.WHITE;
380
             setBackground(bgColor);
381
             
382
             JCheckBox chkVisible = new JCheckBox("", false);
383
             ISymbol symbolLayer = symbol.getLayer(index);
384
             if( symbolLayer!=null ) {
385
                chkVisible.setSelected(symbolLayer.isShapeVisible());
386
             }
387
             chkVisible.setBackground(bgColor);
388
             add(chkVisible);
389
             SymbolPreviewer sp = new SymbolPreviewer(owner.getSampleFeature());
390
             sp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
391
             sp.setAlignmentX(Component.LEFT_ALIGNMENT);
392
             sp.setPreferredSize(new Dimension(80, 30));
393
             sp.setSize(new Dimension(80, 30));
394
             sp.setSymbol(sym);
395
             sp.setBackground(Color.WHITE);
396
             add(sp);
397
             
398
             // TODO: which is the meaning of a "locked" symbol, and where is defined??
399
//             XMLEntity xml=null;
400
////XMLENTITY-UPDATED        
401
////                        try {
402
////                                xml = sym.getXMLEntity();
403
////                        } catch (XMLException e) {
404
////                                NotificationManager.addWarning("Symbol layer", e);
405
////                        }
406
//             String source;
407
//             if (xml.contains("locked"))
408
//                     source = xml.getBooleanProperty("locked") ?
409
//                                     "images/locked.png" :
410
//                                             "images/unlocked.png";
411
//             else
412
//                     source = "images/unlocked.png";
413

    
414
             
415
             JButton btnLock = new JButton(IconThemeHelper.getImageIcon("symbol-layer-unlock"));
416
             btnLock.setSize(btnDimension);
417
             btnLock.setPreferredSize(btnDimension);
418
             btnLock.setActionCommand("LOCK");
419
             btnLock.setBackground(bgColor);
420
             btnLock.setAlignmentX(Component.CENTER_ALIGNMENT);
421
             add(btnLock);
422
             setPreferredSize(LIST_CELL_SIZE);
423

    
424
                }
425
        }
426
}
427

    
428