Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / 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 @ 40560

History | View | Annotate | Download (6.53 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: JSymbolPreviewButton.java 31301 2009-10-16 06:56:13Z vcaballero $
27
* $Log$
28
* Revision 1.4  2007-09-17 09:21:45  jaume
29
* refactored SymboSelector (added support for multishapedsymbol)
30
*
31
* Revision 1.3  2007/08/09 10:39:04  jaume
32
* first round of found bugs fixed
33
*
34
* Revision 1.2  2007/03/09 11:25:00  jaume
35
* Advanced symbology (start committing)
36
*
37
* Revision 1.1.2.4  2007/02/21 07:35:14  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.1.2.3  2007/02/14 10:00:45  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.1.2.2  2007/02/14 09:59:17  jaume
44
* *** empty log message ***
45
*
46
* Revision 1.1.2.1  2007/02/13 16:19:19  jaume
47
* graduated symbol legends (start commiting)
48
*
49
*
50
*/
51
package org.gvsig.app.project.documents.view.legend.gui;
52

    
53
import java.awt.BasicStroke;
54
import java.awt.Color;
55
import java.awt.Dimension;
56
import java.awt.Graphics;
57
import java.awt.Graphics2D;
58
import java.awt.Rectangle;
59
import java.awt.event.ActionEvent;
60
import java.awt.event.ActionListener;
61
import java.awt.event.MouseEvent;
62
import java.awt.event.MouseListener;
63
import java.awt.geom.AffineTransform;
64
import java.awt.geom.Rectangle2D;
65
import java.util.ArrayList;
66

    
67
import javax.swing.BorderFactory;
68
import javax.swing.JComponent;
69

    
70
import org.gvsig.andami.PluginServices;
71
import org.gvsig.app.gui.styling.SymbolSelector;
72
import org.gvsig.fmap.mapcontext.MapContextLocator;
73
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
74
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
75
import org.gvsig.i18n.Messages;
76

    
77
/**
78
 * Just a Button that shows an ISymbol instead a text.
79
 *
80
 * @author jaume dominguez faus - jaume.dominguez@iver.es
81
 *
82
 */
83
public class JSymbolPreviewButton extends JComponent implements MouseListener {
84
        private static final long serialVersionUID = -7878718124556977288L;
85
        private ISymbol prev;
86
        private boolean pressed;
87
        private int shapeType;
88
        private ArrayList<ActionListener> listeners;  //  @jve:decl-index=0:
89
        private ActionEvent event;
90
        private String actionCommand;
91

    
92
        /**
93
         * @return the actionCommand
94
         */
95
        public String getActionCommand() {
96
                return actionCommand;
97
        }
98

    
99

    
100

    
101
        /**
102
         * @param actionCommand the actionCommand to set
103
         */
104
        public void setActionCommand(String actionCommand) {
105
                this.actionCommand = actionCommand;
106
        }
107

    
108

    
109

    
110
        public JSymbolPreviewButton(int shapeType) {
111
                this(null, shapeType);
112
        }
113

    
114
        public JSymbolPreviewButton(ISymbol sym,
115
                        int shapeType) {
116
                super();
117
                addMouseListener(this);
118
                mouseExited(null);
119
                setPreferredSize(new Dimension(150, 20));
120
                this.shapeType = shapeType;
121
                this.prev = sym;
122
   }
123

    
124

    
125

    
126
        public void paint(Graphics g) {
127
                Rectangle bounds = getBounds();
128
                Graphics2D g2 = (Graphics2D) g;
129
                if (g2 != null) {
130
                        g2.setStroke(new BasicStroke(2));
131
                        g2.setColor(pressed && isEnabled() ? Color.GRAY : Color.WHITE);
132
                        g2.drawLine(0, 0, (int) bounds.getWidth(), 0);
133
                        g2.drawLine(0, 0, 0, (int) bounds.getHeight());
134
                        g2.setColor(pressed && isEnabled() ? Color.WHITE : Color.GRAY);
135
                        g2.drawLine(2, (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight());
136
                        g2.drawLine((int) bounds.getWidth(), 0, (int) bounds.getWidth(), (int) bounds.getHeight());
137
                        
138
                        int _margin = 5;
139

    
140
                        Rectangle2D r = new Rectangle2D.Double(
141
                            _margin, _margin,
142
                            bounds.getWidth() - 2 * _margin,
143
                            bounds.getHeight() - 2 * _margin);
144

    
145
                        if (prev!=null) {
146
                                try {
147
                                        prev.drawInsideRectangle(g2,
148
                                            new AffineTransform(), r.getBounds(),null);
149
                                } catch (SymbolDrawingException e) {
150
                                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
151
                                                try {
152
                                                        MapContextLocator.getSymbolManager()
153
                                                                        .getWarningSymbol(
154
                                                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
155
                                                                                        prev.getDescription(),
156
                                                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
157
                                                                        .drawInsideRectangle(g2, g2.getTransform(),
158
                                                                                        r.getBounds(), null);
159
                                                } catch (SymbolDrawingException e1) {
160
                                                        // IMPOSSIBLE TO REACH THIS
161
                                                }
162
                                        } else {
163
                                                // should be unreachable code
164
                                                throw new Error(Messages.getText("symbol_shapetype_mismatch"));
165
                                        }
166
                                }
167
                        }
168
                }
169
        }
170

    
171
        public void setSymbol(ISymbol symbol) {
172
                this.prev = symbol;
173
                this.repaint();
174
                // paintImmediately(getBounds());
175
        }
176

    
177
        public void mouseClicked(MouseEvent e) {
178
                if (e.getButton() == MouseEvent.BUTTON1) {
179
                        ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(prev, shapeType);
180
                        PluginServices.getMDIManager().addWindow(sSelect);
181
                        if (sSelect.getSelectedObject()!=null){
182
                                setSymbol((ISymbol) sSelect.getSelectedObject());
183
                                fireActionListeners();
184
                        }
185
                }
186
        }
187

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

    
200
        }
201

    
202

    
203

    
204
        public void mouseEntered(MouseEvent e) {}
205

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

    
212
        public void mousePressed(MouseEvent e) {
213

    
214
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
215
                pressed = true;
216
                paintImmediately(getBounds());
217
        }
218

    
219
        public void mouseReleased(MouseEvent e) {
220
                if (pressed)
221
                        mouseClicked(e);
222
                mouseExited(e);
223
        }
224

    
225

    
226

    
227
        public ISymbol getSymbol() {
228
                return prev;
229
        }
230

    
231

    
232

    
233
        public void addActionListener(ActionListener l) {
234
                if (listeners == null)
235
                        listeners = new ArrayList<ActionListener>();
236
                listeners.add(l);
237
        }
238

    
239

    
240

    
241
        public void setShapeType(int shapeType) {
242
                this.shapeType = shapeType;
243
        }
244

    
245

    
246
}