root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / JSymbolPreviewButton.java @ 26821
History | View | Annotate | Download (6.47 KB)
1 | 10679 | 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 | 13729 | jaume | * Revision 1.4 2007-09-17 09:21:45 jaume
|
47 | * refactored SymboSelector (added support for multishapedsymbol)
|
||
48 | *
|
||
49 | * Revision 1.3 2007/08/09 10:39:04 jaume
|
||
50 | 12994 | jaume | * first round of found bugs fixed
|
51 | *
|
||
52 | * Revision 1.2 2007/03/09 11:25:00 jaume
|
||
53 | 10679 | jaume | * Advanced symbology (start committing)
|
54 | *
|
||
55 | * Revision 1.1.2.4 2007/02/21 07:35:14 jaume
|
||
56 | * *** empty log message ***
|
||
57 | *
|
||
58 | * Revision 1.1.2.3 2007/02/14 10:00:45 jaume
|
||
59 | * *** empty log message ***
|
||
60 | *
|
||
61 | * Revision 1.1.2.2 2007/02/14 09:59:17 jaume
|
||
62 | * *** empty log message ***
|
||
63 | *
|
||
64 | * Revision 1.1.2.1 2007/02/13 16:19:19 jaume
|
||
65 | * graduated symbol legends (start commiting)
|
||
66 | *
|
||
67 | *
|
||
68 | */
|
||
69 | package com.iver.cit.gvsig.project.documents.view.legend.gui; |
||
70 | |||
71 | import java.awt.BasicStroke; |
||
72 | import java.awt.Color; |
||
73 | 12994 | jaume | import java.awt.Dimension; |
74 | 10679 | jaume | import java.awt.Graphics; |
75 | import java.awt.Graphics2D; |
||
76 | import java.awt.Rectangle; |
||
77 | import java.awt.event.ActionEvent; |
||
78 | import java.awt.event.ActionListener; |
||
79 | import java.awt.event.MouseEvent; |
||
80 | import java.awt.event.MouseListener; |
||
81 | import java.awt.geom.Rectangle2D; |
||
82 | import java.util.ArrayList; |
||
83 | |||
84 | import javax.swing.BorderFactory; |
||
85 | import javax.swing.JComponent; |
||
86 | |||
87 | 20994 | jmvivo | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
88 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
||
89 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory; |
||
90 | |||
91 | 10679 | jaume | import com.iver.andami.PluginServices; |
92 | import com.iver.cit.gvsig.gui.styling.SymbolSelector; |
||
93 | /**
|
||
94 | * Just a Button that shows an ISymbol instead a text.
|
||
95 | *
|
||
96 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
97 | *
|
||
98 | */
|
||
99 | public class JSymbolPreviewButton extends JComponent implements MouseListener { |
||
100 | 20779 | vcaballero | private static final long serialVersionUID = -7878718124556977288L; |
101 | 10679 | jaume | private ISymbol prev;
|
102 | private boolean pressed; |
||
103 | private int shapeType; |
||
104 | 20779 | vcaballero | private ArrayList<ActionListener> listeners; // @jve:decl-index=0: |
105 | 10679 | jaume | private ActionEvent event; |
106 | private String actionCommand; |
||
107 | |||
108 | /**
|
||
109 | * @return the actionCommand
|
||
110 | */
|
||
111 | public String getActionCommand() { |
||
112 | return actionCommand;
|
||
113 | } |
||
114 | |||
115 | |||
116 | |||
117 | /**
|
||
118 | * @param actionCommand the actionCommand to set
|
||
119 | */
|
||
120 | public void setActionCommand(String actionCommand) { |
||
121 | this.actionCommand = actionCommand;
|
||
122 | } |
||
123 | |||
124 | |||
125 | |||
126 | public JSymbolPreviewButton(int shapeType) { |
||
127 | 14273 | jdominguez | this(null, shapeType); |
128 | } |
||
129 | |||
130 | public JSymbolPreviewButton(ISymbol sym,
|
||
131 | int shapeType) {
|
||
132 | 10679 | jaume | super();
|
133 | addMouseListener(this);
|
||
134 | mouseExited(null);
|
||
135 | 12994 | jaume | setPreferredSize(new Dimension(150, 20)); |
136 | 10679 | jaume | this.shapeType = shapeType;
|
137 | 14273 | jdominguez | this.prev = sym;
|
138 | } |
||
139 | 10679 | jaume | |
140 | |||
141 | 14273 | jdominguez | |
142 | 10679 | jaume | public void paint(Graphics g) { |
143 | Rectangle bounds = getBounds();
|
||
144 | Graphics2D g2 = (Graphics2D) g; |
||
145 | if (g2 != null) { |
||
146 | g2.setStroke(new BasicStroke(2)); |
||
147 | 20779 | vcaballero | g2.setColor(pressed && isEnabled() ? Color.GRAY : Color.WHITE); |
148 | 10679 | jaume | g2.drawLine(0, 0, (int) bounds.getWidth(), 0); |
149 | g2.drawLine(0, 0, 0, (int) bounds.getHeight()); |
||
150 | 20779 | vcaballero | g2.setColor(pressed && isEnabled() ? Color.WHITE : Color.GRAY); |
151 | 10679 | jaume | g2.drawLine(2, (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight()); |
152 | g2.drawLine((int) bounds.getWidth(), 0, (int) bounds.getWidth(), (int) bounds.getHeight()); |
||
153 | |||
154 | Rectangle2D r = new Rectangle2D.Double(3, 3, bounds.getWidth()-3, bounds.getHeight()-6); |
||
155 | |||
156 | if (prev!=null) { |
||
157 | 18623 | jdominguez | try {
|
158 | prev.drawInsideRectangle(g2, g2.getTransform(), r.getBounds()); |
||
159 | } catch (SymbolDrawingException e) {
|
||
160 | if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
|
||
161 | try {
|
||
162 | SymbologyFactory.getWarningSymbol(SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, |
||
163 | prev.getDescription(), |
||
164 | SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS |
||
165 | ).drawInsideRectangle(g2, g2.getTransform(), r.getBounds()); |
||
166 | } catch (SymbolDrawingException e1) {
|
||
167 | // IMPOSSIBLE TO REACH THIS
|
||
168 | } |
||
169 | } else {
|
||
170 | // should be unreachable code
|
||
171 | throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch")); |
||
172 | } |
||
173 | } |
||
174 | 10679 | jaume | } |
175 | } |
||
176 | } |
||
177 | |||
178 | public void setSymbol(ISymbol symbol) { |
||
179 | this.prev = symbol;
|
||
180 | paintImmediately(getBounds()); |
||
181 | } |
||
182 | |||
183 | public void mouseClicked(MouseEvent e) { |
||
184 | if (e.getButton() == MouseEvent.BUTTON1) { |
||
185 | 13729 | jaume | ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(prev, shapeType); |
186 | 10679 | jaume | PluginServices.getMDIManager().addWindow(sSelect); |
187 | setSymbol((ISymbol) sSelect.getSelectedObject()); |
||
188 | fireActionListeners(); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | private void fireActionListeners() { |
||
193 | 20779 | vcaballero | if (!isEnabled()) return; |
194 | 10679 | jaume | if (listeners != null) { |
195 | if (event == null) { |
||
196 | event = new ActionEvent(this, 0, actionCommand); |
||
197 | } |
||
198 | for (int i = 0; i < listeners.size(); i++) { |
||
199 | ((ActionListener) listeners.get(i)).actionPerformed(
|
||
200 | event); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | } |
||
205 | |||
206 | |||
207 | |||
208 | public void mouseEntered(MouseEvent e) {} |
||
209 | |||
210 | public void mouseExited(MouseEvent e) { |
||
211 | setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
|
||
212 | pressed = false;
|
||
213 | paintImmediately(getBounds()); |
||
214 | } |
||
215 | |||
216 | public void mousePressed(MouseEvent e) { |
||
217 | |||
218 | setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
|
||
219 | pressed = true;
|
||
220 | paintImmediately(getBounds()); |
||
221 | } |
||
222 | |||
223 | public void mouseReleased(MouseEvent e) { |
||
224 | if (pressed)
|
||
225 | mouseClicked(e); |
||
226 | mouseExited(e); |
||
227 | } |
||
228 | |||
229 | |||
230 | |||
231 | public ISymbol getSymbol() {
|
||
232 | return prev;
|
||
233 | } |
||
234 | |||
235 | |||
236 | |||
237 | public void addActionListener(ActionListener l) { |
||
238 | if (listeners == null) |
||
239 | 20779 | vcaballero | listeners = new ArrayList<ActionListener>(); |
240 | 10679 | jaume | listeners.add(l); |
241 | } |
||
242 | |||
243 | |||
244 | |||
245 | public void setShapeType(int shapeType) { |
||
246 | this.shapeType = shapeType;
|
||
247 | } |
||
248 | |||
249 | |||
250 | } |