Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / JSymbolPreviewButton.java @ 28368

History | View | Annotate | Download (6.45 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 28368 2009-05-04 15:30:35Z 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 com.iver.cit.gvsig.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 com.iver.andami.PluginServices;
88
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
89
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
90
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
91
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
92
/**
93
 * Just a Button that shows an ISymbol instead a text.
94
 *
95
 * @author jaume dominguez faus - jaume.dominguez@iver.es
96
 *
97
 */
98
public class JSymbolPreviewButton extends JComponent implements MouseListener {
99
        private static final long serialVersionUID = -7878718124556977288L;
100
        private ISymbol prev;
101
        private boolean pressed;
102
        private int shapeType;
103
        private ArrayList<ActionListener> listeners;  //  @jve:decl-index=0:
104
        private ActionEvent event;
105
        private String actionCommand;
106

    
107
        /**
108
         * @return the actionCommand
109
         */
110
        public String getActionCommand() {
111
                return actionCommand;
112
        }
113

    
114

    
115

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

    
123

    
124

    
125
        public JSymbolPreviewButton(int shapeType) {
126
                this(null, shapeType);
127
        }
128

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

    
139

    
140

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

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

    
155
                        if (prev!=null) {
156
                                try {
157
                                        prev.drawInsideRectangle(g2, g2.getTransform(), r.getBounds(), null);
158
                                } catch (SymbolDrawingException e) {
159
                                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
160
                                                try {
161
                                                        SymbologyFactory.getWarningSymbol(SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
162
                                                                        prev.getDescription(),
163
                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS
164
                                                                        ).drawInsideRectangle(g2, g2.getTransform(), r.getBounds(), null);
165
                                                } catch (SymbolDrawingException e1) {
166
                                                        // IMPOSSIBLE TO REACH THIS
167
                                                }
168
                                        } else {
169
                                                // should be unreachable code
170
                                                throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
171
                                        }
172
                                }
173
                        }
174
                }
175
        }
176

    
177
        public void setSymbol(ISymbol symbol) {
178
                this.prev = symbol;
179
                paintImmediately(getBounds());
180
        }
181

    
182
        public void mouseClicked(MouseEvent e) {
183
                if (e.getButton() == MouseEvent.BUTTON1) {
184
                        ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(prev, shapeType);
185
                        PluginServices.getMDIManager().addWindow(sSelect);
186
                        setSymbol((ISymbol) sSelect.getSelectedObject());
187
                        fireActionListeners();
188
                }
189
        }
190

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

    
205

    
206

    
207
        public void mouseEntered(MouseEvent e) {}
208

    
209
        public void mouseExited(MouseEvent e) {
210
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
211
                pressed = false;
212
                paintImmediately(getBounds());
213
        }
214

    
215
        public void mousePressed(MouseEvent e) {
216

    
217
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
218
                pressed = true;
219
                paintImmediately(getBounds());
220
        }
221

    
222
        public void mouseReleased(MouseEvent e) {
223
                if (pressed)
224
                        mouseClicked(e);
225
                mouseExited(e);
226
        }
227

    
228

    
229

    
230
        public ISymbol getSymbol() {
231
                return prev;
232
        }
233

    
234

    
235

    
236
        public void addActionListener(ActionListener l) {
237
                if (listeners == null)
238
                        listeners = new ArrayList<ActionListener>();
239
                listeners.add(l);
240
        }
241

    
242

    
243

    
244
        public void setShapeType(int shapeType) {
245
                this.shapeType = shapeType;
246
        }
247

    
248

    
249
}