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 @ 41190

History | View | Annotate | Download (7.42 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
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
107

    
108

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

    
167
                g2.translate(hGap, vGap);
168
                Rectangle r = getBounds();
169
        r = new Rectangle(0, 0,
170
            (int) (r.getWidth()-(hGap*2)), (int) (r.getHeight()-(vGap*2)));
171
                
172
                Double forcesize = this.forceSize(symbol);
173
                if (forcesize != null && !useAllRoom) {
174
            long difx = Math.round(0.5 * (r.getWidth() - forcesize.doubleValue()));
175
            long dify = Math.round(0.5 * (r.getHeight() - forcesize.doubleValue()));
176
                    
177
                    r = new Rectangle((int) difx, (int) dify,
178
                    (int) forcesize.doubleValue(),
179
                    (int) forcesize.doubleValue());
180
                }
181

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

    
211
                        g2.drawString(noneSelected,         (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
212
                }
213
                g2.setRenderingHints(old);
214
        }
215

    
216
        /**
217
         * Sets the EditorTool for the pane.
218
         *
219
         * @param l,EditorTool
220
         */
221
        public EditorTool setEditorTool(EditorTool tool) {
222
                EditorTool previous = prevTool;
223

    
224
                MouseListener[] ml = getMouseListeners();
225
                for (int i = 0; i < ml.length; i++) {
226
                        super.removeMouseListener(ml[i]);
227
                }
228
                MouseMotionListener[] mml = getMouseMotionListeners();
229
                for (int i = 0; i < mml.length; i++) {
230
                        super.removeMouseMotionListener(mml[i]);
231
                }
232

    
233
                if (tool!= null) {
234
                        super.addMouseListener(tool);
235
                        setCursor(tool.getCursor());
236
                        super.addMouseMotionListener(tool);
237
                }
238
                prevTool = tool;
239
                return previous;
240
        }
241
        
242
        private Double forceSize(ISymbol sym) {
243
            
244
            if (sym == null) {
245
                return null;
246
            }
247
            
248
            if (!(sym instanceof IMarkerSymbol)) {
249
                return null;
250
            }
251
            
252
            IMarkerSymbol msym = (IMarkerSymbol) sym;
253
            if (msym.getUnit() == -1) {
254
                /*
255
                 * Pixels
256
                 */
257
                return new Double(msym.getSize());
258
            } else {
259
                return null;
260
            }
261
            
262
        }
263

    
264
}