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

History | View | Annotate | Download (7.89 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
import org.gvsig.fmap.dal.feature.Feature;
102

    
103
import org.gvsig.fmap.mapcontext.MapContextLocator;
104
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
105
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
106
import org.gvsig.i18n.Messages;
107
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
108
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
110

    
111

    
112
/**
113
 * SymbolPreviewer creates a JPanel used for the user to watch the preview of
114
 * a symbol.It has an square form with a white background and the symbol
115
 * is inserted in the middle.
116
 * @author jaume dominguez faus - jaume.dominguez@iver.es
117
 *
118
 */
119
public class SymbolPreviewer extends JPanel {
120
    private static final Logger LOGGER = LoggerFactory.getLogger(SymbolPreviewer.class);
121
        /**
122
         * 
123
         */
124
        private static final long serialVersionUID = 8095930506630873031L;
125
        private int hGap = 3, vGap = 3;
126
        private ISymbol symbol;
127
        private EditorTool prevTool;
128
        private boolean useAllRoom = false; 
129
    private Feature sampleFeature;
130
        /**
131
         * constructor method
132
         *
133
         */
134
        public SymbolPreviewer() {
135
                super(true);
136
                setBackground(Color.WHITE);
137
        }
138

    
139
             public SymbolPreviewer(Feature sampleFeature) {
140
                this();
141
                this.sampleFeature = sampleFeature;
142
                
143
        }
144

    
145
    public SymbolPreviewer(boolean all_room) {
146
        super(true);
147
        setBackground(Color.WHITE);
148
        useAllRoom = all_room;
149
    }
150
        
151
        /**
152
         * Returns the symbol printed inside
153
         * @return symbol
154
         */
155
        public ISymbol getSymbol() {
156
                return symbol;
157
        }
158
        /**
159
         * Establishes the symbol to be showed in the symbolpreviewer panel
160
         * @param symbol
161
         */
162
        public void setSymbol(ISymbol symbol) {
163
                this.symbol = symbol;
164
                repaint();
165
        }
166
        /**
167
         * Draws the symbol in the middle of the pane in order to create a preview
168
         * of the final one.
169
         */
170
        public void paint(Graphics g) {
171
            try {
172
                super.paint(g);
173
                Graphics2D g2 = (Graphics2D) g;
174
                RenderingHints old = g2.getRenderingHints();
175
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
176
                g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
177
                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
178

    
179
                g2.translate(hGap, vGap);
180
                Rectangle r = getBounds();
181
        r = new Rectangle(0, 0,
182
            (int) (r.getWidth()-(hGap*2)), (int) (r.getHeight()-(vGap*2)));
183
                
184
                Double forcesize = this.forceSize(symbol);
185
                if (forcesize != null && !useAllRoom) {
186
            long difx = Math.round(0.5 * (r.getWidth() - forcesize.doubleValue()));
187
            long dify = Math.round(0.5 * (r.getHeight() - forcesize.doubleValue()));
188
                    
189
                    r = new Rectangle((int) difx, (int) dify,
190
                    (int) forcesize.doubleValue(),
191
                    (int) forcesize.doubleValue());
192
                }
193

    
194
                if (symbol != null) {
195
                        try {
196
                                symbol.drawInsideRectangle(g2, new AffineTransform(), r,null);
197
                        } catch (SymbolDrawingException e) {
198
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
199
                                        try {
200
                                                MapContextLocator.getSymbolManager()
201
                                                                .getWarningSymbol(
202
                                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
203
                                                                                "",
204
                                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
205
                                                                .drawInsideRectangle(g2, null, r, null);
206
                                        } catch (SymbolDrawingException e1) {
207
                                                // IMPOSSIBLE TO REACH THIS
208
                                        }
209
                                } else {
210
                                        // should be unreachable code
211
                                        throw new Error(Messages.getText("symbol_shapetype_mismatch"));
212
                                }
213
                        }
214
                } else {
215
                        String noneSelected = "["+Messages.getText("preview_not_available")+"]";
216
                        FontMetrics fm = g2.getFontMetrics();
217
                        int lineWidth = fm.stringWidth(noneSelected);
218
                        float scale = (float) r.getWidth() / lineWidth;
219
                        Font f = g2.getFont();
220
                        float fontSize = f.getSize()*scale;
221
                        g2.setFont(        f.deriveFont( fontSize ) );
222

    
223
                        g2.drawString(noneSelected,         (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
224
                }
225
                g2.setRenderingHints(old);
226
            } catch (Throwable t) {
227
                LOGGER.warn("Can't preview symbol", t);
228
            }
229
        }
230

    
231
        /**
232
         * Sets the EditorTool for the pane.
233
         *
234
         * @param l,EditorTool
235
         */
236
        public EditorTool setEditorTool(EditorTool tool) {
237
                EditorTool previous = prevTool;
238

    
239
                MouseListener[] ml = getMouseListeners();
240
                for (int i = 0; i < ml.length; i++) {
241
                        super.removeMouseListener(ml[i]);
242
                }
243
                MouseMotionListener[] mml = getMouseMotionListeners();
244
                for (int i = 0; i < mml.length; i++) {
245
                        super.removeMouseMotionListener(mml[i]);
246
                }
247

    
248
                if (tool!= null) {
249
                        super.addMouseListener(tool);
250
                        setCursor(tool.getCursor());
251
                        super.addMouseMotionListener(tool);
252
                }
253
                prevTool = tool;
254
                return previous;
255
        }
256
        
257
        private Double forceSize(ISymbol sym) {
258
            
259
            if (sym == null) {
260
                return null;
261
            }
262
            
263
            if (!(sym instanceof IMarkerSymbol)) {
264
                return null;
265
            }
266
            
267
            IMarkerSymbol msym = (IMarkerSymbol) sym;
268
            if (msym.getUnit() == -1) {
269
                /*
270
                 * Pixels
271
                 */
272
                return new Double(msym.getSize());
273
            } else {
274
                return null;
275
            }
276
            
277
        }
278

    
279
}