Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / gvsig / gui / styling / SymbolLayerManager.java @ 8537

History | View | Annotate | Download (13.8 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 8537 2006-11-06 16:06:52Z jaume $
45
* $Log$
46
* Revision 1.3  2006-11-06 16:06:52  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2006/11/06 07:33:54  jaume
50
* javadoc, source style
51
*
52
* Revision 1.1  2006/11/02 17:19:28  jaume
53
* *** empty log message ***
54
*
55
*
56
*/
57
package com.iver.cit.gvsig.gvsig.gui.styling;
58

    
59
import java.awt.BorderLayout;
60
import java.awt.Color;
61
import java.awt.Component;
62
import java.awt.Dimension;
63
import java.awt.FlowLayout;
64
import java.awt.Font;
65
import java.awt.LayoutManager;
66
import java.awt.event.ActionEvent;
67
import java.awt.event.ActionListener;
68
import java.awt.event.MouseAdapter;
69
import java.awt.event.MouseEvent;
70
import java.util.ArrayList;
71

    
72
import javax.swing.AbstractListModel;
73
import javax.swing.BorderFactory;
74
import javax.swing.DefaultListCellRenderer;
75
import javax.swing.ImageIcon;
76
import javax.swing.JList;
77
import javax.swing.JPanel;
78
import javax.swing.JScrollPane;
79
import javax.swing.event.ListDataListener;
80
import javax.swing.event.ListSelectionEvent;
81
import javax.swing.event.ListSelectionListener;
82

    
83
import org.gvsig.gui.beans.swing.JButton;
84

    
85
import com.iver.andami.PluginServices;
86
import com.iver.cit.gvsig.fmap.core.FShape;
87
import com.iver.cit.gvsig.fmap.core.ISymbol;
88
import com.iver.cit.gvsig.fmap.core.symbols.CharacterMarkerSymbol;
89
import com.iver.cit.gvsig.fmap.core.symbols.SymbolFactory;
90
import com.iver.utiles.XMLEntity;
91

    
92
/**
93
 * A component linked to a SymbolEditor that shows the layers
94
 *
95
 * @author jaume
96
 *
97
 */
98
public class SymbolLayerManager extends JPanel implements ActionListener{
99
    private SymbolEditor owner;
100
    private JList jListLayers;
101
    private ISymbol activeLayer;
102
    private ISymbol symbol;
103
    private JButton btnAddLayer = null;
104
    private JPanel pnlButtons = null;
105
    private JButton btnRemoveLayer = null;
106
    private JButton btnMoveUpLayer = null;
107
    private JButton btnMoveDownLayer = null;
108
    private final Dimension btnDimension = new Dimension(24, 24);
109
    private JScrollPane scroll;
110
    private Color cellSelectedBGColor = Color.BLUE;
111

    
112

    
113
    public SymbolLayerManager(SymbolEditor owner) {
114
        this.owner = owner;
115
        this.symbol = owner.getSymbol();
116
        initialize();
117
    }
118

    
119
    private void initialize() {
120
        this.setLayout(new BorderLayout());
121
        this.setSize(new java.awt.Dimension(155,106));
122
        this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
123
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
124
    }
125

    
126
    private JScrollPane getJScrollPane() {
127
        if (scroll ==  null) {
128
            scroll = new JScrollPane();
129
            scroll.setViewportView(getJListLayers());
130
            scroll.setPreferredSize(new Dimension(120, 160));
131
        }
132
        return scroll;
133
    }
134

    
135
    /**
136
     * This method initializes jList
137
     *
138
     * @return javax.swing.JList
139
     */
140
    private JList getJListLayers() {
141
        if (jListLayers == null) {
142
            jListLayers = new JList();
143
            jListLayers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
144
            jListLayers.setLayoutOrientation(JList.HORIZONTAL_WRAP);
145

    
146
            jListLayers.setVisibleRowCount(-1);
147
            jListLayers.addListSelectionListener(new ListSelectionListener() {
148
                public void valueChanged(ListSelectionEvent e) {
149
                    setLayer((ISymbol) jListLayers.getSelectedValue());
150
                }
151
            });
152
            jListLayers.setModel(new SymbolLayerListModel(symbol));
153
            jListLayers.setCellRenderer(new DefaultListCellRenderer() {
154

    
155
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
156
                    return getListCell(list, value, index, isSelected, cellHasFocus);
157
                }
158

    
159
            });
160

    
161
            jListLayers.addMouseListener(new MouseAdapter() {
162
                                public void mouseClicked(MouseEvent e) {
163
                                        if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
164
                                                SymbolSelector symSel = new SymbolSelector(activeLayer, owner.getShapeType());
165
                                                PluginServices.getMDIManager().addWindow(symSel);
166
                                                updateSymbol(symSel.getSymbol());
167
                                                setLayer(symSel.getSymbol());
168
                                        }
169

    
170
                                }
171
            });
172
        }
173
        return jListLayers;
174
    }
175

    
176
    public Component getListCell(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
177
            ISymbol sym = (ISymbol) value;
178
        JPanel pnl = new JPanel();
179
        LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 3, 3);
180
        pnl.setLayout(layout);
181
        Color bgColor = (isSelected) ? cellSelectedBGColor
182
                 : Color.WHITE;
183
        pnl.setBackground(bgColor);
184
        SymbolPreview sp = new SymbolPreview();
185
        sp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
186
        sp.setAlignmentX(Component.LEFT_ALIGNMENT);
187
        sp.setPreferredSize(new Dimension(80, 30));
188
        sp.setSize(new Dimension(80, 30));
189
        sp.setSymbol(sym);
190
        sp.setBackground(Color.WHITE);
191
        pnl.add(sp);
192

    
193
        XMLEntity xml = sym.getXMLEntity();
194
        String source;
195
        if (xml.contains("locked"))
196
            source = xml.getBooleanProperty("locked") ?
197
                    "images/locked.png" :
198
                    "images/unlocked.png";
199
         else
200
            source = "images/unlocked.png";
201

    
202
        ImageIcon icon = new ImageIcon(getClass().
203
                getClassLoader().getResource(source));
204
        JButton btnLock = new JButton(icon);
205
        btnLock.setSize(btnDimension);
206
        btnLock.setPreferredSize(btnDimension);
207
        btnLock.setActionCommand("LOCK");
208
        btnLock.setBackground(bgColor);
209
        btnLock.setAlignmentX(Component.CENTER_ALIGNMENT);
210
        pnl.add(btnLock);
211
        pnl.setPreferredSize(new Dimension(150,37));
212
        return pnl;
213
    }
214

    
215
    /**
216
     * replaces current selected layer with this symbol
217
     * @param layer
218
     */
219
    private void updateSymbol(ISymbol layer) {
220
            if (layer != null) {
221
                    int index = getJListLayers().getSelectedIndex();
222
                    XMLEntity xml = symbol.getXMLEntity();
223
                    ArrayList layers = new ArrayList() ;
224
                    for (int i = 0; i < xml.getChildrenCount(); i++) {
225
                            if (i == index) {
226
                                    layers.add(layer.getXMLEntity());
227
                            } else {
228
                                    layers.add(xml.getChild(i));
229
                            }
230
                    }
231
                    xml.removeAllChildren();
232
                    for (int i = 0; i < layers.size(); i++) {
233
                            xml.addChild((XMLEntity) layers.get(i));
234
                    }
235
                    symbol.setXMLEntity(xml);
236
                    owner.refresh();
237
                    getJListLayers().repaint();
238
            }
239
    }
240

    
241
    private void setLayer(ISymbol symbol) {
242
            if (symbol!=null) {
243
                    activeLayer = symbol;
244

    
245
            }
246
    }
247

    
248
    /**
249
     * This method initializes jButton
250
     *
251
     * @return javax.swing.JButton
252
     */
253
    private JButton getBtnAddLayer() {
254
        if (btnAddLayer == null) {
255
            btnAddLayer = new JButton(new ImageIcon(getClass().
256
                    getClassLoader().getResource("images/add-layer.png")));
257
            btnAddLayer.setActionCommand("ADD");
258
            btnAddLayer.setSize(btnDimension);
259
            btnAddLayer.setPreferredSize(btnDimension);
260
            btnAddLayer.addActionListener(this);
261
        }
262
        return btnAddLayer;
263
    }
264

    
265
    /**
266
     * This method initializes pnlButtons
267
     *
268
     * @return javax.swing.JPanel
269
     */
270
    private JPanel getPnlButtons() {
271
        if (pnlButtons == null) {
272
            pnlButtons = new JPanel();
273
            pnlButtons.add(getBtnAddLayer(), null);
274
            pnlButtons.add(getBtnRemoveLayer(), null);
275
            pnlButtons.add(getBtnMoveUpLayer(), null);
276
            pnlButtons.add(getBtnMoveDown(), null);
277
        }
278
        return pnlButtons;
279
    }
280

    
281
    /**
282
     * This method initializes jButton1
283
     *
284
     * @return javax.swing.JButton
285
     */
286
    private JButton getBtnRemoveLayer() {
287
        if (btnRemoveLayer == null) {
288
            btnRemoveLayer = new JButton(new ImageIcon(getClass().
289
                    getClassLoader().getResource("images/delete.png")));
290
            btnRemoveLayer.setActionCommand("REMOVE");
291
            btnRemoveLayer.setSize(btnDimension);
292
            btnRemoveLayer.setPreferredSize(btnDimension);
293
            btnRemoveLayer.addActionListener(this);
294
        }
295
        return btnRemoveLayer;
296
    }
297

    
298
    /**
299
     * This method initializes jButton2
300
     *
301
     * @return javax.swing.JButton
302
     */
303
    private JButton getBtnMoveUpLayer() {
304
        if (btnMoveUpLayer == null) {
305
            btnMoveUpLayer = new JButton(new ImageIcon(getClass().
306
                    getClassLoader().getResource("images/up-arrow.png")));
307
            btnMoveUpLayer.setActionCommand("MOVE_UP");
308
            btnMoveUpLayer.setSize(btnDimension);
309
            btnMoveUpLayer.setPreferredSize(btnDimension);
310
            btnMoveUpLayer.addActionListener(this);
311
        }
312
        return btnMoveUpLayer;
313
    }
314

    
315
    /**
316
     * This method initializes jButton3
317
     *
318
     * @return javax.swing.JButton
319
     */
320
    private JButton getBtnMoveDown() {
321
        if (btnMoveDownLayer == null) {
322
            btnMoveDownLayer = new JButton(new ImageIcon(getClass().
323
                    getClassLoader().getResource("images/down-arrow.png")));
324
            btnMoveDownLayer.setActionCommand("MOVE_DOWN");
325
            btnMoveDownLayer.setSize(btnDimension);
326
            btnMoveDownLayer.setPreferredSize(btnDimension);
327
            btnMoveDownLayer.addActionListener(this);
328
        }
329
        return btnMoveDownLayer;
330
    }
331

    
332
    public void actionPerformed(ActionEvent e) {
333
        String command = e.getActionCommand();
334
        XMLEntity xml = symbol.getXMLEntity();
335
        int index = getJListLayers().getSelectedIndex();
336
        if (command.equals("ADD")) {
337
            xml.addChild(new CharacterMarkerSymbol(new Font("Arial", Font.PLAIN, 10), (int) 'a', Color.BLACK).getXMLEntity());
338
            index = xml.getChildrenCount()-1;
339
        } else if (command.equals("REMOVE")) {
340
                try {
341
                        xml.removeChild(getJListLayers().getSelectedIndex());
342
                } catch (Exception ex) {
343
                        // Index out of bounds or so, we don't care
344
                }
345
        } else if (command.equals("MOVE_UP")) {
346
            if (index>0) {
347
                ArrayList layers = new ArrayList();
348
                for (int i = 0; i < xml.getChildrenCount(); i++) {
349
                    if (i == index)
350
                        layers.add(i-1, xml.getChild(i));
351
                    else
352
                        layers.add(xml.getChild(i));
353
                }
354
                xml.removeAllChildren();
355
                for (int i = 0; i < layers.size(); i++) {
356
                    xml.addChild((XMLEntity) layers.get(i));
357
                }
358
                index --;
359
            }
360
        } else if (command.equals("MOVE_DOWN")) {
361
            if (index < xml.getChildrenCount()-1) {
362
                ArrayList layers = new ArrayList();
363
                for (int i = 0; i < xml.getChildrenCount(); i++) {
364
                    if (i == index) {
365
                        layers.add(i, xml.getChild(i+1));
366
                        layers.add(i+1, xml.getChild(i));
367
                        i++;
368
                    }
369
                    else
370
                        layers.add(xml.getChild(i));
371
                }
372
                xml.removeAllChildren();
373
                for (int i = 0; i < layers.size(); i++) {
374
                    xml.addChild((XMLEntity) layers.get(i));
375
                }
376
                index++;
377
            }
378
        } else if (command.equals("LOCK")) {
379
            try {
380
                XMLEntity layerXML = xml.getChild(index);
381
                boolean locked = !xml.contains("locked") || !xml.getBooleanProperty("locked");
382
                layerXML.putProperty("locked", locked);
383
            } catch (Exception ex) {
384
                // Index out of bounds or so, we don't care
385
            }
386
        }
387
        symbol.setXMLEntity(xml);
388
        getJListLayers().setSelectedIndex(index);
389
        owner.refresh();
390
        getJListLayers().repaint();
391
    }
392

    
393
    public int getSelectedLayerIndex() {
394
        /*int i = getJListLayers().getSelectedIndex();
395
        if (i==-1 && symbol.getXMLEntity().getChildrenCount()==0)
396
            i = 0;
397
        return i;*/
398
        return getJListLayers().getSelectedIndex();
399
    }
400
}
401

    
402
class SymbolLayerListModel extends AbstractListModel {
403

    
404
    private ISymbol symbol;
405
        private ArrayList listeners = new ArrayList();
406

    
407
    public SymbolLayerListModel(ISymbol symbol) {
408
        this.symbol = symbol;
409
    }
410

    
411
    public int getSize() {
412
        return symbol.getXMLEntity().getChildrenCount();
413
    }
414

    
415
    public Object getElementAt(int index) {
416
        XMLEntity xml = symbol.getXMLEntity();
417
        return SymbolFactory.createFromXML(xml.getChild(index),
418
                        PluginServices.getText(this, "layer")+index);
419
    }
420

    
421
    public void addListDataListener(ListDataListener l) {
422
            listeners.add(l);
423
    }
424

    
425
    public void removeListDataListener(ListDataListener l) {
426
            listeners.remove(l);
427
    }
428

    
429
}