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 @ 43510

History | View | Annotate | Download (14.4 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
                    int index = getJListLayers().getSelectedIndex();
226
                    symbol.setLayer(index, layer);
227
                    owner.refresh();
228
                    validate();
229
            }
230
    }
231

    
232
    private void setLayer(ISymbol symbol) {
233
            if (symbol!=null) {
234
                    activeLayer = symbol;
235
                    owner.setOptionsPageFor(symbol);
236
            }
237
    }
238

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

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

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

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

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

    
294
    public void actionPerformed(ActionEvent e) {
295
        String command = e.getActionCommand();
296

    
297
        int jlistIndex = getJListLayers().getSelectedIndex();
298
        int index = symbol.getLayerCount() - 1 - jlistIndex;
299

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

    
331
    }
332

    
333
    private class SymbolLayerListModel extends AbstractListModel {
334
            private static final long serialVersionUID = 4197818146836802855L;
335
                private ArrayList listeners = new ArrayList();
336

    
337
        public int getSize() {
338
            return symbol.getLayerCount();
339
        }
340

    
341
        public Object getElementAt(int index) {
342
                return symbol.getLayer(symbol.getLayerCount()-1-index);
343
        }
344

    
345
        public void addListDataListener(ListDataListener l) {
346
                listeners.add(l);
347
        }
348

    
349
        public void removeListDataListener(ListDataListener l) {
350
                listeners.remove(l);
351
        }
352

    
353
    }
354

    
355
        public ISymbol getSelectedLayer() {
356
                return (ISymbol) getJListLayers().getSelectedValue();
357
        }
358

    
359
        public int getSelectedLayerIndex() {
360
                return getJListLayers().getSelectedIndex();
361
        }
362

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

    
369
        private class SymbolLayerComponent extends JPanel {
370
                private static final long serialVersionUID = -3706313315505454031L;
371

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

    
411
             
412
             JButton btnLock = new JButton(IconThemeHelper.getImageIcon("symbol-layer-unlock"));
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
}
424

    
425