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 / gui / styling / SymbolPreviewer.java @ 40560

History | View | Annotate | Download (6.35 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: SymbolPreviewer.java 30892 2009-09-21 12:02:19Z cordinyana $
27
 * $Log: SymbolPreviewer.java,v $
28
 * Revision 1.6  2007/08/16 06:54:35  jvidal
29
 * javadoc updated
30
 *
31
 * Revision 1.5  2007/08/07 11:41:15  jvidal
32
 * javadoc
33
 *
34
 * Revision 1.4  2007/04/05 16:08:34  jaume
35
 * Styled labeling stuff
36
 *
37
 * Revision 1.3  2007/04/04 16:01:14  jaume
38
 * *** empty log message ***
39
 *
40
 * Revision 1.2  2007/03/09 11:25:00  jaume
41
 * Advanced symbology (start committing)
42
 *
43
 * Revision 1.1.2.3  2007/02/21 07:35:14  jaume
44
 * *** empty log message ***
45
 *
46
 * Revision 1.1.2.2  2007/02/08 15:43:05  jaume
47
 * some bug fixes in the editor and removed unnecessary imports
48
 *
49
 * Revision 1.1.2.1  2007/01/26 13:49:03  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.4  2007/01/16 11:52:11  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.8  2007/01/10 17:05:05  jaume
56
 * moved to FMap and gvSIG
57
 *
58
 * Revision 1.7  2006/10/30 19:30:35  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.6  2006/10/29 23:53:49  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.5  2006/10/29 21:45:12  jaume
65
 * centers in x the "none selected message"
66
 *
67
 * Revision 1.4  2006/10/29 21:43:34  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.3  2006/10/29 21:40:29  jaume
71
 * centers in x the "none selected message"
72
 *
73
 * Revision 1.2  2006/10/26 07:46:58  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.1  2006/10/25 10:50:41  jaume
77
 * movement of classes and gui stuff
78
 *
79
 * Revision 1.2  2006/10/24 19:54:55  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1  2006/10/24 16:31:12  jaume
83
 * *** empty log message ***
84
 *
85
 *
86
 */
87
package org.gvsig.app.gui.styling;
88

    
89
import java.awt.Color;
90
import java.awt.Font;
91
import java.awt.FontMetrics;
92
import java.awt.Graphics;
93
import java.awt.Graphics2D;
94
import java.awt.Rectangle;
95
import java.awt.RenderingHints;
96
import java.awt.event.MouseListener;
97
import java.awt.event.MouseMotionListener;
98
import java.awt.geom.AffineTransform;
99

    
100
import javax.swing.JPanel;
101

    
102
import org.gvsig.fmap.mapcontext.MapContextLocator;
103
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
104
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
105
import org.gvsig.i18n.Messages;
106

    
107

    
108
/**
109
 * SymbolPreviewer creates a JPanel used for the user to watch the preview of
110
 * a symbol.It has an square form with a white background and the symbol
111
 * is inserted in the middle.
112
 * @author jaume dominguez faus - jaume.dominguez@iver.es
113
 *
114
 */
115
public class SymbolPreviewer extends JPanel {
116
        /**
117
         * 
118
         */
119
        private static final long serialVersionUID = 8095930506630873031L;
120
        private int hGap = 5, vGap = 5;
121
        private ISymbol symbol;
122
        private EditorTool prevTool;
123
        /**
124
         * constructor method
125
         *
126
         */
127
        public SymbolPreviewer() {
128
                super(true);
129
                setBackground(Color.WHITE);
130
        }
131
        /**
132
         * Returns the symbol printed inside
133
         * @return symbol
134
         */
135
        public ISymbol getSymbol() {
136
                return symbol;
137
        }
138
        /**
139
         * Establishes the symbol to be showed in the symbolpreviewer panel
140
         * @param symbol
141
         */
142
        public void setSymbol(ISymbol symbol) {
143
                this.symbol = symbol;
144
                repaint();
145
        }
146
        /**
147
         * Draws the symbol in the middle of the pane in order to create a preview
148
         * of the final one.
149
         */
150
        public void paint(Graphics g) {
151
                super.paint(g);
152
                Graphics2D g2 = (Graphics2D) g;
153
                RenderingHints old = g2.getRenderingHints();
154
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
155
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
156
                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
157

    
158
                g2.translate(hGap, vGap);
159
                Rectangle r = getBounds();
160
                r = new Rectangle(0, 0, (int) (r.getWidth()-(hGap*2)), (int) (r.getHeight()-(vGap*2)));
161

    
162
                if (symbol != null) {
163
                        try {
164
                                symbol.drawInsideRectangle(g2, new AffineTransform(), r,null);
165
                        } catch (SymbolDrawingException e) {
166
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
167
                                        try {
168
                                                MapContextLocator.getSymbolManager()
169
                                                                .getWarningSymbol(
170
                                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
171
                                                                                "",
172
                                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
173
                                                                .drawInsideRectangle(g2, null, r, null);
174
                                        } catch (SymbolDrawingException e1) {
175
                                                // IMPOSSIBLE TO REACH THIS
176
                                        }
177
                                } else {
178
                                        // should be unreachable code
179
                                        throw new Error(Messages.getText("symbol_shapetype_mismatch"));
180
                                }
181
                        }
182
                } else {
183
                        String noneSelected = "["+Messages.getText("preview_not_available")+"]";
184
                        FontMetrics fm = g2.getFontMetrics();
185
                        int lineWidth = fm.stringWidth(noneSelected);
186
                        float scale = (float) r.getWidth() / lineWidth;
187
                        Font f = g2.getFont();
188
                        float fontSize = f.getSize()*scale;
189
                        g2.setFont(        f.deriveFont( fontSize ) );
190

    
191
                        g2.drawString(noneSelected,         (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
192
                }
193
                g2.setRenderingHints(old);
194
        }
195

    
196
        /**
197
         * Sets the EditorTool for the pane.
198
         *
199
         * @param l,EditorTool
200
         */
201
        public EditorTool setEditorTool(EditorTool tool) {
202
                EditorTool previous = prevTool;
203

    
204
                MouseListener[] ml = getMouseListeners();
205
                for (int i = 0; i < ml.length; i++) {
206
                        super.removeMouseListener(ml[i]);
207
                }
208
                MouseMotionListener[] mml = getMouseMotionListeners();
209
                for (int i = 0; i < mml.length; i++) {
210
                        super.removeMouseMotionListener(mml[i]);
211
                }
212

    
213
                if (tool!= null) {
214
                        super.addMouseListener(tool);
215
                        setCursor(tool.getCursor());
216
                        super.addMouseMotionListener(tool);
217
                }
218
                prevTool = tool;
219
                return previous;
220
        }
221

    
222
}