Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / project / documents / view / legend / gui / JSymbolPreviewButton.java @ 40334

History | View | Annotate | Download (6.69 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: JSymbolPreviewButton.java 31301 2009-10-16 06:56:13Z vcaballero $
45
* $Log$
46
* 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
* first round of found bugs fixed
51
*
52
* Revision 1.2  2007/03/09 11:25:00  jaume
53
* 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 org.gvsig.app.project.documents.view.legend.gui;
70

    
71
import java.awt.BasicStroke;
72
import java.awt.Color;
73
import java.awt.Dimension;
74
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.AffineTransform;
82
import java.awt.geom.Rectangle2D;
83
import java.util.ArrayList;
84

    
85
import javax.swing.BorderFactory;
86
import javax.swing.JComponent;
87

    
88
import org.gvsig.andami.PluginServices;
89
import org.gvsig.app.gui.styling.SymbolSelector;
90
import org.gvsig.fmap.mapcontext.MapContextLocator;
91
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
92
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
93
import org.gvsig.i18n.Messages;
94

    
95
/**
96
 * Just a Button that shows an ISymbol instead a text.
97
 *
98
 * @author jaume dominguez faus - jaume.dominguez@iver.es
99
 *
100
 */
101
public class JSymbolPreviewButton extends JComponent implements MouseListener {
102
        private static final long serialVersionUID = -7878718124556977288L;
103
        private ISymbol prev;
104
        private boolean pressed;
105
        private int shapeType;
106
        private ArrayList<ActionListener> listeners;  //  @jve:decl-index=0:
107
        private ActionEvent event;
108
        private String actionCommand;
109

    
110
        /**
111
         * @return the actionCommand
112
         */
113
        public String getActionCommand() {
114
                return actionCommand;
115
        }
116

    
117

    
118

    
119
        /**
120
         * @param actionCommand the actionCommand to set
121
         */
122
        public void setActionCommand(String actionCommand) {
123
                this.actionCommand = actionCommand;
124
        }
125

    
126

    
127

    
128
        public JSymbolPreviewButton(int shapeType) {
129
                this(null, shapeType);
130
        }
131

    
132
        public JSymbolPreviewButton(ISymbol sym,
133
                        int shapeType) {
134
                super();
135
                addMouseListener(this);
136
                mouseExited(null);
137
                setPreferredSize(new Dimension(150, 20));
138
                this.shapeType = shapeType;
139
                this.prev = sym;
140
   }
141

    
142

    
143

    
144
        public void paint(Graphics g) {
145
                Rectangle bounds = getBounds();
146
                Graphics2D g2 = (Graphics2D) g;
147
                if (g2 != null) {
148
                        g2.setStroke(new BasicStroke(2));
149
                        g2.setColor(pressed && isEnabled() ? Color.GRAY : Color.WHITE);
150
                        g2.drawLine(0, 0, (int) bounds.getWidth(), 0);
151
                        g2.drawLine(0, 0, 0, (int) bounds.getHeight());
152
                        g2.setColor(pressed && isEnabled() ? Color.WHITE : Color.GRAY);
153
                        g2.drawLine(2, (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight());
154
                        g2.drawLine((int) bounds.getWidth(), 0, (int) bounds.getWidth(), (int) bounds.getHeight());
155

    
156
                        Rectangle2D r = new Rectangle2D.Double(3, 3, bounds.getWidth()-3, bounds.getHeight()-6);
157

    
158
                        if (prev!=null) {
159
                                try {
160
                                        prev.drawInsideRectangle(g2,
161
                                            new AffineTransform(), r.getBounds(),null);
162
                                } catch (SymbolDrawingException e) {
163
                                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
164
                                                try {
165
                                                        MapContextLocator.getSymbolManager()
166
                                                                        .getWarningSymbol(
167
                                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
168
                                                                                        prev.getDescription(),
169
                                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
170
                                                                        .drawInsideRectangle(g2, g2.getTransform(),
171
                                                                                        r.getBounds(), null);
172
                                                } catch (SymbolDrawingException e1) {
173
                                                        // IMPOSSIBLE TO REACH THIS
174
                                                }
175
                                        } else {
176
                                                // should be unreachable code
177
                                                throw new Error(Messages.getText("symbol_shapetype_mismatch"));
178
                                        }
179
                                }
180
                        }
181
                }
182
        }
183

    
184
        public void setSymbol(ISymbol symbol) {
185
                this.prev = symbol;
186
                paintImmediately(getBounds());
187
        }
188

    
189
        public void mouseClicked(MouseEvent e) {
190
                if (e.getButton() == MouseEvent.BUTTON1) {
191
                        ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(prev, shapeType);
192
                        PluginServices.getMDIManager().addWindow(sSelect);
193
                        if (sSelect.getSelectedObject()!=null){
194
                                setSymbol((ISymbol) sSelect.getSelectedObject());
195
                                fireActionListeners();
196
                        }
197
                }
198
        }
199

    
200
        private void fireActionListeners() {
201
                if (!isEnabled()) return;
202
                if (listeners != null) {
203
                        if (event == null) {
204
                                event = new ActionEvent(this, 0, actionCommand);
205
                        }
206
                        for (int i = 0; i < listeners.size(); i++) {
207
                                ((ActionListener) listeners.get(i)).actionPerformed(
208
                                                event);
209
                        }
210
                }
211

    
212
        }
213

    
214

    
215

    
216
        public void mouseEntered(MouseEvent e) {}
217

    
218
        public void mouseExited(MouseEvent e) {
219
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
220
                pressed = false;
221
                paintImmediately(getBounds());
222
        }
223

    
224
        public void mousePressed(MouseEvent e) {
225

    
226
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
227
                pressed = true;
228
                paintImmediately(getBounds());
229
        }
230

    
231
        public void mouseReleased(MouseEvent e) {
232
                if (pressed)
233
                        mouseClicked(e);
234
                mouseExited(e);
235
        }
236

    
237

    
238

    
239
        public ISymbol getSymbol() {
240
                return prev;
241
        }
242

    
243

    
244

    
245
        public void addActionListener(ActionListener l) {
246
                if (listeners == null)
247
                        listeners = new ArrayList<ActionListener>();
248
                listeners.add(l);
249
        }
250

    
251

    
252

    
253
        public void setShapeType(int shapeType) {
254
                this.shapeType = shapeType;
255
        }
256

    
257

    
258
}