Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / JSymbolPreviewButton.java @ 33740

History | View | Annotate | Download (6.55 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.Rectangle2D;
82
import java.util.ArrayList;
83

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

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

    
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
        private static final long serialVersionUID = -7878718124556977288L;
101
        private ISymbol prev;
102
        private boolean pressed;
103
        private int shapeType;
104
        private ArrayList<ActionListener> listeners;  //  @jve:decl-index=0:
105
        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
                this(null, shapeType);
128
        }
129

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

    
140

    
141

    
142
        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
                        g2.setColor(pressed && isEnabled() ? Color.GRAY : Color.WHITE);
148
                        g2.drawLine(0, 0, (int) bounds.getWidth(), 0);
149
                        g2.drawLine(0, 0, 0, (int) bounds.getHeight());
150
                        g2.setColor(pressed && isEnabled() ? Color.WHITE : Color.GRAY);
151
                        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
                                try {
158
                                        prev.drawInsideRectangle(g2, g2.getTransform(), r.getBounds(),null);
159
                                } catch (SymbolDrawingException e) {
160
                                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
161
                                                try {
162
                                                        MapContextLocator.getSymbolManager()
163
                                                                        .getWarningSymbol(
164
                                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
165
                                                                                        prev.getDescription(),
166
                                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
167
                                                                        .drawInsideRectangle(g2, g2.getTransform(),
168
                                                                                        r.getBounds(), null);
169
                                                } catch (SymbolDrawingException e1) {
170
                                                        // IMPOSSIBLE TO REACH THIS
171
                                                }
172
                                        } else {
173
                                                // should be unreachable code
174
                                                throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
175
                                        }
176
                                }
177
                        }
178
                }
179
        }
180

    
181
        public void setSymbol(ISymbol symbol) {
182
                this.prev = symbol;
183
                paintImmediately(getBounds());
184
        }
185

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

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

    
209
        }
210

    
211

    
212

    
213
        public void mouseEntered(MouseEvent e) {}
214

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

    
221
        public void mousePressed(MouseEvent e) {
222

    
223
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
224
                pressed = true;
225
                paintImmediately(getBounds());
226
        }
227

    
228
        public void mouseReleased(MouseEvent e) {
229
                if (pressed)
230
                        mouseClicked(e);
231
                mouseExited(e);
232
        }
233

    
234

    
235

    
236
        public ISymbol getSymbol() {
237
                return prev;
238
        }
239

    
240

    
241

    
242
        public void addActionListener(ActionListener l) {
243
                if (listeners == null)
244
                        listeners = new ArrayList<ActionListener>();
245
                listeners.add(l);
246
        }
247

    
248

    
249

    
250
        public void setShapeType(int shapeType) {
251
                this.shapeType = shapeType;
252
        }
253

    
254

    
255
}