Statistics
| Revision:

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

History | View | Annotate | Download (8.21 KB)

1 9745 jaume
/* 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$
45
* $Log$
46 10993 jaume
* Revision 1.3  2007-03-30 12:54:11  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2007/03/09 11:25:00  jaume
50 10679 jaume
* Advanced symbology (start committing)
51
*
52
* Revision 1.1.2.7  2007/02/21 07:35:14  jaume
53 9745 jaume
* *** empty log message ***
54
*
55 10679 jaume
* Revision 1.1.2.6  2007/02/08 15:43:05  jaume
56
* some bug fixes in the editor and removed unnecessary imports
57
*
58
* Revision 1.1.2.5  2007/02/05 14:58:28  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.1.2.3  2007/02/04 16:57:22  jaume
62
* working the map of characters
63
*
64
* Revision 1.1.2.2  2007/02/02 16:21:32  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1  2007/01/16 11:52:11  jaume
71
* *** empty log message ***
72
*
73 9745 jaume
* Revision 1.3  2006/11/06 17:08:45  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.2  2006/11/06 16:06:52  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.1  2006/10/31 16:16:34  jaume
80
* *** empty log message ***
81
*
82
*
83
*/
84
package com.iver.cit.gvsig.gui.styling;
85
86 10679 jaume
import java.awt.BorderLayout;
87
import java.awt.Color;
88
import java.awt.Component;
89
import java.awt.Dimension;
90
import java.awt.Font;
91
import java.awt.GraphicsEnvironment;
92
import java.awt.event.ActionEvent;
93
import java.awt.event.ActionListener;
94 9745 jaume
import java.util.ArrayList;
95
96 10679 jaume
import javax.swing.BoxLayout;
97
import javax.swing.JComponent;
98
import javax.swing.JLabel;
99
import javax.swing.JList;
100 9745 jaume
import javax.swing.JPanel;
101 10679 jaume
import javax.swing.JScrollPane;
102
import javax.swing.ListCellRenderer;
103
import javax.swing.ListModel;
104
import javax.swing.event.ListDataListener;
105
import javax.swing.event.ListSelectionEvent;
106
import javax.swing.event.ListSelectionListener;
107 9745 jaume
108 10679 jaume
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
109
110 9745 jaume
import com.iver.andami.PluginServices;
111 10679 jaume
import com.iver.andami.messages.NotificationManager;
112
import com.iver.cit.gvsig.fmap.core.symbols.CharacterMarkerSymbol;
113
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
114
import com.iver.utiles.swing.JComboBox;
115 9745 jaume
116 10679 jaume
public class CharacterMarker extends AbstractTypeSymbolEditorPanel implements ActionListener {
117 9745 jaume
118
        private ArrayList tabs = new ArrayList();
119 10679 jaume
        private JList jListSymbols;
120
        private JScrollPane jScrollPane;
121
        private JComboBox cmbFonts;
122
        private CharacterMarkerSymbol layer;
123 9745 jaume
124
        public CharacterMarker(SymbolEditor owner) {
125
                super(owner);
126
                initialize();
127
        }
128
129
        private void initialize() {
130
                JPanel myTab;
131
                {
132
                        myTab = new JPanel();
133
                        myTab.setName(PluginServices.getText(this, "character_marker"));
134 10679 jaume
                        myTab.setLayout(new BorderLayout(15,15));
135
136
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
137
                        aux.addComponent(PluginServices.getText(this, "font")+":", getCmbFonts());
138
139
                        getJListSymbols().setModel(new CharacterListModel(new Font("ESRI Conservation", Font.PLAIN, 14)));
140
                        aux.addComponent(getJScrollPane());
141
                        myTab.add(aux);
142 9745 jaume
                        tabs.add(myTab);
143
                }
144
145
        }
146
147 10679 jaume
        private JComboBox getCmbFonts() {
148
                if (cmbFonts == null) {
149
//                         Font info is obtained from the current graphics environment.
150
                        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
151
152
//                        --- Get an array of font names (smaller than the number of fonts)
153
                        String[] fontNames = ge.getAvailableFontFamilyNames();
154
                        cmbFonts = new JComboBox(fontNames);
155
                        cmbFonts.addActionListener(this);
156
                }
157
                return cmbFonts;
158 9745 jaume
        }
159
160 10679 jaume
        private JScrollPane getJScrollPane() {
161
                if (jScrollPane == null) {
162
                        jScrollPane = new JScrollPane();
163
                        jScrollPane.setViewportView(getJListSymbols());
164
                        jScrollPane.setPreferredSize(new Dimension(300, 180));
165
                }
166
                return jScrollPane;
167
        }
168
169
        private JList getJListSymbols() {
170
                if (jListSymbols == null) {
171
                        jListSymbols = new JList();
172
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
173
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
174
                        jListSymbols.setVisibleRowCount(-1);
175
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
176
                                public void valueChanged(ListSelectionEvent e) {
177
                                        CharacterItem theChar = (CharacterItem) jListSymbols.getSelectedValue();
178
                                        layer = new CharacterMarkerSymbol(theChar.font, theChar.glyph, Color.BLACK);
179
                                }
180
                        });
181
                        ListCellRenderer renderer = new ListCellRenderer() {
182
                                private Color mySelectedBGColor = new Color(255,145,100,255);
183
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
184
                                        CharacterItem theChar = (CharacterItem) value;
185
                                        CharacterMarkerSymbol sym = new CharacterMarkerSymbol(theChar.font, theChar.glyph, Color.BLACK);
186
                                        JPanel pnl = new JPanel();
187
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
188
                                        pnl.setLayout(layout);
189
                                        Color bgColor = (isSelected) ? mySelectedBGColor
190
                                                        : getJListSymbols().getBackground();
191
192
                                        pnl.setBackground(bgColor);
193
                                        SymbolPreviewer sp = new SymbolPreviewer();
194
                                        sp.setAlignmentX(Component.CENTER_ALIGNMENT);
195 10993 jaume
                                        int prevSize = 30;
196
                                        sp.setPreferredSize(new Dimension(prevSize, prevSize));
197 10679 jaume
                                        sp.setSymbol(sym);
198
                                        sp.setBackground(bgColor);
199 10993 jaume
                                        sym.setSize(prevSize*.8);
200 10679 jaume
                                        pnl.add(sp);
201
                                        JLabel lbl = new JLabel(sym.getDescription());
202
                                        lbl.setBackground(bgColor);
203
                                        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
204
                                        pnl.add(lbl);
205
206
                                        return pnl;
207
                                }
208
                        };
209
                        jListSymbols.setCellRenderer(renderer);
210
                }
211
                return jListSymbols;
212
        }
213
214
215 9745 jaume
        public String getName() {
216
                return PluginServices.getText(this, "character_marker_symbol");
217
        }
218
219
        public JPanel[] getTabs() {
220
                return (JPanel[]) tabs .toArray(new JPanel[0]);
221
        }
222
223 10679 jaume
        public void refreshControls(ISymbol layer) {
224
                CharacterMarkerSymbol sym;
225
                try {
226
                        sym = (CharacterMarkerSymbol) layer;
227
                        // TODO finish it
228
                } catch (IndexOutOfBoundsException ioEx) {
229
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
230
                } catch (ClassCastException ccEx) {
231
                        NotificationManager.addWarning("Illegal casting from " +
232
                                        layer.getClassName() + " to " + getSymbolClass().
233
                                        getName() + ".", ccEx);
234 9745 jaume
235 10679 jaume
                }
236
237 9745 jaume
        }
238
239
        public Class getSymbolClass() {
240 10679 jaume
                return CharacterMarkerSymbol.class;
241 9745 jaume
        }
242
243 10679 jaume
        public ISymbol getLayer() {
244
                if (layer == null)
245
                        layer = new CharacterMarkerSymbol();
246
                return layer;
247
        }
248
249
        private class CharacterListModel implements ListModel {
250
251
                private Font font;
252
                private ArrayList listeners;
253
254
                public CharacterListModel(Font font) {
255
                        this.font = font;
256
                }
257
258
                public int getSize() {
259
                        return font.getNumGlyphs();
260
                }
261
262
                public Object getElementAt(int index) {
263
                        return new CharacterItem(font, index);
264
                }
265
266
                public void addListDataListener(ListDataListener l) {
267
                        if (listeners == null)
268
                                listeners = new ArrayList();
269
                        listeners.add(l);
270
                }
271
272
                public void removeListDataListener(ListDataListener l) {
273
                        if (listeners!=null)
274
                                listeners.remove(l);
275
                }
276
        }
277
278
        private class CharacterItem {
279
                int glyph;
280
                Font font;
281
                public CharacterItem(Font font, int glyph) {
282
                        this.font = font;
283
                        this.glyph = glyph;
284
                }
285
286
        }
287
288
        public void actionPerformed(ActionEvent e) {
289
                JComponent c = (JComponent) e.getSource();
290
                if (c.equals(getCmbFonts())) {
291
                        String fontName = (String) getCmbFonts().getSelectedItem();
292
                        getJListSymbols().setModel(
293
                                        new CharacterListModel(
294
                                                        new Font(fontName, Font.PLAIN, 10)));
295
                }
296
        }
297
298 9745 jaume
}