Statistics
| Revision:

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

History | View | Annotate | Download (5.07 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 10437 2007-02-21 07:35:14Z jaume $
45
* $Log$
46
* Revision 1.1.2.4  2007-02-21 07:35:14  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.3  2007/02/14 10:00:45  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.2  2007/02/14 09:59:17  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.1  2007/02/13 16:19:19  jaume
56
* graduated symbol legends (start commiting)
57
*
58
*
59
*/
60
package com.iver.cit.gvsig.project.documents.view.legend.gui;
61

    
62
import java.awt.BasicStroke;
63
import java.awt.Color;
64
import java.awt.Graphics;
65
import java.awt.Graphics2D;
66
import java.awt.Rectangle;
67
import java.awt.event.ActionEvent;
68
import java.awt.event.ActionListener;
69
import java.awt.event.MouseEvent;
70
import java.awt.event.MouseListener;
71
import java.awt.geom.Rectangle2D;
72
import java.util.ArrayList;
73

    
74
import javax.swing.BorderFactory;
75
import javax.swing.JComponent;
76

    
77
import com.iver.andami.PluginServices;
78
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
79
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
80
/**
81
 * Just a Button that shows an ISymbol instead a text.
82
 *
83
 * @author jaume dominguez faus - jaume.dominguez@iver.es
84
 *
85
 */
86
public class JSymbolPreviewButton extends JComponent implements MouseListener {
87
        private ISymbol prev;
88
        private boolean pressed;
89
        private int shapeType;
90
        private ArrayList listeners;  //  @jve:decl-index=0:
91
        private ActionEvent event;
92
        private String actionCommand;
93

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

    
101

    
102

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

    
110

    
111

    
112
        public JSymbolPreviewButton(int shapeType) {
113
                super();
114
                addMouseListener(this);
115
                mouseExited(null);
116
                this.shapeType = shapeType;
117
        }
118

    
119
        
120

    
121
        public void paint(Graphics g) {
122
                Rectangle bounds = getBounds();
123
                Graphics2D g2 = (Graphics2D) g;
124
                if (g2 != null) {
125
                        g2.setStroke(new BasicStroke(2));
126
                        g2.setColor(pressed ? Color.GRAY : Color.WHITE);
127
                        g2.drawLine(0, 0, (int) bounds.getWidth(), 0);
128
                        g2.drawLine(0, 0, 0, (int) bounds.getHeight());
129
                        g2.setColor(pressed ? Color.WHITE : Color.GRAY);
130
                        g2.drawLine(2, (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight());
131
                        g2.drawLine((int) bounds.getWidth(), 0, (int) bounds.getWidth(), (int) bounds.getHeight());
132

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

    
135
                        if (prev!=null) {
136
                                prev.drawInsideRectangle(g2, g2.getTransform(), r.getBounds());
137
                        }
138
                }
139
        }
140

    
141
        public void setSymbol(ISymbol symbol) {
142
                this.prev = symbol;
143
                paintImmediately(getBounds());
144
        }
145

    
146
        public void mouseClicked(MouseEvent e) {
147
                if (e.getButton() == MouseEvent.BUTTON1) {
148
                        SymbolSelector sSelect = new SymbolSelector(prev, shapeType);
149
                        PluginServices.getMDIManager().addWindow(sSelect);
150
                        setSymbol(sSelect.getSymbol());
151
                        fireActionListeners();
152
                }
153
        }
154

    
155
        private void fireActionListeners() {
156
                
157
                if (listeners != null) {
158
                        if (event == null) {
159
                                event = new ActionEvent(this, 0, actionCommand);
160
                        }
161
                        for (int i = 0; i < listeners.size(); i++) {
162
                                ((ActionListener) listeners.get(i)).actionPerformed(
163
                                                event);
164
                        }
165
                }
166
                        
167
        }
168

    
169

    
170

    
171
        public void mouseEntered(MouseEvent e) {}
172

    
173
        public void mouseExited(MouseEvent e) {
174
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
175
                pressed = false;
176
                paintImmediately(getBounds());
177
        }
178

    
179
        public void mousePressed(MouseEvent e) {
180

    
181
                setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
182
                pressed = true;
183
                paintImmediately(getBounds());
184
        }
185

    
186
        public void mouseReleased(MouseEvent e) {
187
                if (pressed)
188
                        mouseClicked(e);
189
                mouseExited(e);
190
        }
191

    
192

    
193

    
194
        public ISymbol getSymbol() {
195
                return prev;
196
        }
197

    
198

    
199

    
200
        public void addActionListener(ActionListener l) {
201
                if (listeners == null)
202
                        listeners = new ArrayList();
203
                listeners.add(l);
204
        }
205

    
206

    
207

    
208
        public void setShapeType(int shapeType) {
209
                this.shapeType = shapeType;
210
        }
211

    
212

    
213
}