Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / CharacterMarker.java @ 11073

History | View | Annotate | Download (8.47 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: CharacterMarker.java 11073 2007-04-05 16:08:34Z jaume $
45
* $Log$
46
* Revision 1.5  2007-04-05 16:08:34  jaume
47
* Styled labeling stuff
48
*
49
* Revision 1.4  2007/04/04 16:01:13  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.3  2007/03/30 12:54:11  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.2  2007/03/09 11:25:00  jaume
56
* Advanced symbology (start committing)
57
*
58
* Revision 1.1.2.7  2007/02/21 07:35:14  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.1.2.6  2007/02/08 15:43:05  jaume
62
* some bug fixes in the editor and removed unnecessary imports
63
*
64
* Revision 1.1.2.5  2007/02/05 14:58:28  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.3  2007/02/04 16:57:22  jaume
68
* working the map of characters
69
*
70
* Revision 1.1.2.2  2007/02/02 16:21:32  jaume
71
* *** empty log message ***
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.3  2006/11/06 17:08:45  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2  2006/11/06 16:06:52  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.1  2006/10/31 16:16:34  jaume
86
* *** empty log message ***
87
*
88
*
89
*/
90
package com.iver.cit.gvsig.gui.styling;
91

    
92
import java.awt.BorderLayout;
93
import java.awt.Color;
94
import java.awt.Component;
95
import java.awt.Dimension;
96
import java.awt.Font;
97
import java.awt.GraphicsEnvironment;
98
import java.awt.event.ActionEvent;
99
import java.awt.event.ActionListener;
100
import java.util.ArrayList;
101

    
102
import javax.swing.BoxLayout;
103
import javax.swing.JComponent;
104
import javax.swing.JLabel;
105
import javax.swing.JList;
106
import javax.swing.JPanel;
107
import javax.swing.JScrollPane;
108
import javax.swing.ListCellRenderer;
109
import javax.swing.ListModel;
110
import javax.swing.event.ListDataListener;
111
import javax.swing.event.ListSelectionEvent;
112
import javax.swing.event.ListSelectionListener;
113

    
114
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
115

    
116
import com.iver.andami.PluginServices;
117
import com.iver.andami.messages.NotificationManager;
118
import com.iver.cit.gvsig.fmap.core.symbols.CharacterMarkerSymbol;
119
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
120
import com.iver.utiles.swing.JComboBox;
121

    
122
/**
123
 *
124
 * @author jaume dominguez faus - jaume.dominguez@iver.es
125
 *
126
 */
127
public class CharacterMarker extends AbstractTypeSymbolEditorPanel implements ActionListener {
128

    
129
        private ArrayList tabs = new ArrayList();
130
        private JList jListSymbols;
131
        private JScrollPane jScrollPane;
132
        private JComboBox cmbFonts;
133
        private CharacterMarkerSymbol layer;
134

    
135
        public CharacterMarker(SymbolEditor owner) {
136
                super(owner);
137
                initialize();
138
        }
139

    
140
        private void initialize() {
141
                JPanel myTab;
142
                {
143
                        myTab = new JPanel();
144
                        myTab.setName(PluginServices.getText(this, "character_marker"));
145
                        myTab.setLayout(new BorderLayout(15,15));
146

    
147
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
148
                        aux.addComponent(PluginServices.getText(this, "font")+":", getCmbFonts());
149

    
150
                        getJListSymbols().setModel(new CharacterListModel(new Font("ESRI Conservation", Font.PLAIN, 14)));
151
                        aux.addComponent(getJScrollPane());
152
                        myTab.add(aux);
153
                        tabs.add(myTab);
154
                }
155

    
156
        }
157

    
158
        private JComboBox getCmbFonts() {
159
                if (cmbFonts == null) {
160
//                         Font info is obtained from the current graphics environment.
161
                        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
162

    
163
//                        --- Get an array of font names (smaller than the number of fonts)
164
                        String[] fontNames = ge.getAvailableFontFamilyNames();
165
                        cmbFonts = new JComboBox(fontNames);
166
                        cmbFonts.addActionListener(this);
167
                }
168
                return cmbFonts;
169
        }
170

    
171
        private JScrollPane getJScrollPane() {
172
                if (jScrollPane == null) {
173
                        jScrollPane = new JScrollPane();
174
                        jScrollPane.setViewportView(getJListSymbols());
175
                        jScrollPane.setPreferredSize(new Dimension(300, 180));
176
                }
177
                return jScrollPane;
178
        }
179

    
180
        private JList getJListSymbols() {
181
                if (jListSymbols == null) {
182
                        jListSymbols = new JList();
183
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
184
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
185
                        jListSymbols.setVisibleRowCount(-1);
186
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
187
                                public void valueChanged(ListSelectionEvent e) {
188
                                        CharacterItem theChar = (CharacterItem) jListSymbols.getSelectedValue();
189
                                        layer = new CharacterMarkerSymbol(theChar.font, theChar.glyph, Color.BLACK);
190
                                }
191
                        });
192
                        ListCellRenderer renderer = new ListCellRenderer() {
193
                                private Color mySelectedBGColor = new Color(255,145,100,255);
194
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
195
                                        CharacterItem theChar = (CharacterItem) value;
196
                                        CharacterMarkerSymbol sym = new CharacterMarkerSymbol(theChar.font, theChar.glyph, Color.BLACK);
197
                                        JPanel pnl = new JPanel();
198
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
199
                                        pnl.setLayout(layout);
200
                                        Color bgColor = (isSelected) ? mySelectedBGColor
201
                                                        : getJListSymbols().getBackground();
202

    
203
                                        pnl.setBackground(bgColor);
204
                                        SymbolPreviewer sp = new SymbolPreviewer();
205
                                        sp.setAlignmentX(Component.CENTER_ALIGNMENT);
206
                                        int prevSize = 30;
207
                                        sp.setPreferredSize(new Dimension(prevSize, prevSize));
208
                                        sp.setSymbol(sym);
209
                                        sp.setBackground(bgColor);
210
                                        sym.setSize(prevSize*.8);
211
                                        pnl.add(sp);
212
                                        JLabel lbl = new JLabel(sym.getDescription());
213
                                        lbl.setBackground(bgColor);
214
                                        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
215
                                        pnl.add(lbl);
216

    
217
                                        return pnl;
218
                                }
219
                        };
220
                        jListSymbols.setCellRenderer(renderer);
221
                }
222
                return jListSymbols;
223
        }
224

    
225

    
226
        public String getName() {
227
                return PluginServices.getText(this, "character_marker_symbol");
228
        }
229

    
230
        public JPanel[] getTabs() {
231
                return (JPanel[]) tabs .toArray(new JPanel[0]);
232
        }
233

    
234
        public void refreshControls(ISymbol layer) {
235
                CharacterMarkerSymbol sym;
236
                try {
237
                        sym = (CharacterMarkerSymbol) layer;
238
                        // TODO finish it
239
                } catch (IndexOutOfBoundsException ioEx) {
240
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
241
                } catch (ClassCastException ccEx) {
242
                        NotificationManager.addWarning("Illegal casting from " +
243
                                        layer.getClassName() + " to " + getSymbolClass().
244
                                        getName() + ".", ccEx);
245

    
246
                }
247

    
248
        }
249

    
250
        public Class getSymbolClass() {
251
                return CharacterMarkerSymbol.class;
252
        }
253

    
254
        public ISymbol getLayer() {
255
                if (layer == null)
256
                        layer = new CharacterMarkerSymbol();
257
                return layer;
258
        }
259

    
260
        private class CharacterListModel implements ListModel {
261

    
262
                private Font font;
263
                private ArrayList listeners;
264

    
265
                public CharacterListModel(Font font) {
266
                        this.font = font;
267
                }
268

    
269
                public int getSize() {
270
                        return font.getNumGlyphs();
271
                }
272

    
273
                public Object getElementAt(int index) {
274
                        return new CharacterItem(font, index);
275
                }
276

    
277
                public void addListDataListener(ListDataListener l) {
278
                        if (listeners == null)
279
                                listeners = new ArrayList();
280
                        listeners.add(l);
281
                }
282

    
283
                public void removeListDataListener(ListDataListener l) {
284
                        if (listeners!=null)
285
                                listeners.remove(l);
286
                }
287
        }
288

    
289
        private class CharacterItem {
290
                int glyph;
291
                Font font;
292
                public CharacterItem(Font font, int glyph) {
293
                        this.font = font;
294
                        this.glyph = glyph;
295
                }
296

    
297
        }
298

    
299
        public void actionPerformed(ActionEvent e) {
300
                JComponent c = (JComponent) e.getSource();
301
                if (c.equals(getCmbFonts())) {
302
                        String fontName = (String) getCmbFonts().getSelectedItem();
303
                        getJListSymbols().setModel(
304
                                        new CharacterListModel(
305
                                                        new Font(fontName, Font.PLAIN, 10)));
306
                }
307
        }
308

    
309
        public EditorTool getEditorTool() {
310
                return null;
311
        }
312
}