Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolPreviewer.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: SymbolPreviewer.java 28368 2009-05-04 15:30:35Z vcaballero $
45
 * $Log: SymbolPreviewer.java,v $
46
 * Revision 1.6  2007/08/16 06:54:35  jvidal
47
 * javadoc updated
48
 *
49
 * Revision 1.5  2007/08/07 11:41:15  jvidal
50
 * javadoc
51
 *
52
 * Revision 1.4  2007/04/05 16:08:34  jaume
53
 * Styled labeling stuff
54
 *
55
 * Revision 1.3  2007/04/04 16:01:14  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.2  2007/03/09 11:25:00  jaume
59
 * Advanced symbology (start committing)
60
 *
61
 * Revision 1.1.2.3  2007/02/21 07:35:14  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.1.2.2  2007/02/08 15:43:05  jaume
65
 * some bug fixes in the editor and removed unnecessary imports
66
 *
67
 * Revision 1.1.2.1  2007/01/26 13:49:03  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.4  2007/01/16 11:52:11  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.8  2007/01/10 17:05:05  jaume
74
 * moved to FMap and gvSIG
75
 *
76
 * Revision 1.7  2006/10/30 19:30:35  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.6  2006/10/29 23:53:49  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.5  2006/10/29 21:45:12  jaume
83
 * centers in x the "none selected message"
84
 *
85
 * Revision 1.4  2006/10/29 21:43:34  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.3  2006/10/29 21:40:29  jaume
89
 * centers in x the "none selected message"
90
 *
91
 * Revision 1.2  2006/10/26 07:46:58  jaume
92
 * *** empty log message ***
93
 *
94
 * Revision 1.1  2006/10/25 10:50:41  jaume
95
 * movement of classes and gui stuff
96
 *
97
 * Revision 1.2  2006/10/24 19:54:55  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.1  2006/10/24 16:31:12  jaume
101
 * *** empty log message ***
102
 *
103
 *
104
 */
105
package com.iver.cit.gvsig.gui.styling;
106

    
107
import java.awt.Color;
108
import java.awt.Font;
109
import java.awt.FontMetrics;
110
import java.awt.Graphics;
111
import java.awt.Graphics2D;
112
import java.awt.Rectangle;
113
import java.awt.RenderingHints;
114
import java.awt.event.MouseListener;
115
import java.awt.event.MouseMotionListener;
116
import java.awt.geom.AffineTransform;
117

    
118
import javax.swing.JPanel;
119

    
120
import com.iver.andami.PluginServices;
121
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
122
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
123
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
124

    
125
/**
126
 * SymbolPreviewer creates a JPanel used for the user to watch the preview of
127
 * a symbol.It has an square form with a white background and the symbol
128
 * is inserted in the middle.
129
 * @author jaume dominguez faus - jaume.dominguez@iver.es
130
 *
131
 */
132
public class SymbolPreviewer extends JPanel {
133
        private int hGap = 5, vGap = 5;
134
        private ISymbol symbol;
135
        private EditorTool prevTool;
136
        /**
137
         * constructor method
138
         *
139
         */
140
        public SymbolPreviewer() {
141
                super(true);
142
                setBackground(Color.WHITE);
143
        }
144
        /**
145
         * Returns the symbol printed inside
146
         * @return symbol
147
         */
148
        public ISymbol getSymbol() {
149
                return symbol;
150
        }
151
        /**
152
         * Establishes the symbol to be showed in the symbolpreviewer panel
153
         * @param symbol
154
         */
155
        public void setSymbol(ISymbol symbol) {
156
                this.symbol = symbol;
157
                repaint();
158
        }
159
        /**
160
         * Draws the symbol in the middle of the pane in order to create a preview
161
         * of the final one.
162
         */
163
        public void paint(Graphics g) {
164
                super.paint(g);
165
                Graphics2D g2 = (Graphics2D) g;
166
                RenderingHints old = g2.getRenderingHints();
167
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
168
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
169
                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
170
                
171
                g2.translate(hGap, vGap);
172
                Rectangle r = getBounds();
173
                r = new Rectangle(0, 0, (int) (r.getWidth()-(hGap*2)), (int) (r.getHeight()-(vGap*2)));
174

    
175
                if (symbol != null) {
176
                        try {
177
                                symbol.drawInsideRectangle(g2, new AffineTransform(), r, null);
178
                        } catch (SymbolDrawingException e) {
179
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
180
                                        try {
181
                                                SymbologyFactory.getWarningSymbol(
182
                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
183
                                                                "",
184
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS).
185
                                                        drawInsideRectangle(g2, null, r, null);
186
                                        } catch (SymbolDrawingException e1) {
187
                                                // IMPOSSIBLE TO REACH THIS
188
                                        }
189
                                } else {
190
                                        // should be unreachable code
191
                                        throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
192
                                }
193
                        }
194
                } else {
195
                        String noneSelected = "["+PluginServices.getText(this, "preview_not_available")+"]";
196
                        FontMetrics fm = g2.getFontMetrics();
197
                        int lineWidth = fm.stringWidth(noneSelected);
198
                        float scale = (float) r.getWidth() / lineWidth;
199
                        Font f = g2.getFont();
200
                        float fontSize = f.getSize()*scale;
201
                        g2.setFont(        f.deriveFont( fontSize ) );
202

    
203
                        g2.drawString(noneSelected,         (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
204
                }
205
                g2.setRenderingHints(old);
206
        }
207
        
208
        /**
209
         * Sets the EditorTool for the pane.
210
         *
211
         * @param l,EditorTool
212
         */
213
        public EditorTool setEditorTool(EditorTool tool) {
214
                EditorTool previous = prevTool;
215
                
216
                MouseListener[] ml = getMouseListeners();
217
                for (int i = 0; i < ml.length; i++) {
218
                        super.removeMouseListener(ml[i]);
219
                }
220
                MouseMotionListener[] mml = getMouseMotionListeners();
221
                for (int i = 0; i < mml.length; i++) {
222
                        super.removeMouseMotionListener(mml[i]);
223
                }
224

    
225
                if (tool!= null) {
226
                        super.addMouseListener(tool);
227
                        setCursor(tool.getCursor());
228
                        super.addMouseMotionListener(tool);
229
                }
230
                prevTool = tool;
231
                return previous;
232
        }
233

    
234
}