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 / StylePreviewer.java @ 40560

History | View | Annotate | Download (5.17 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: StylePreviewer.java 30892 2009-09-21 12:02:19Z cordinyana $
27
* $Log$
28
* Revision 1.6  2007-08-16 06:54:35  jvidal
29
* javadoc updated
30
*
31
* Revision 1.5  2007/08/13 11:33:03  jvidal
32
* javadoc
33
*
34
* Revision 1.4  2007/07/30 12:56:04  jaume
35
* organize imports, java 5 code downgraded to 1.4 and added PictureFillSymbol
36
*
37
* Revision 1.3  2007/05/08 15:44:07  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.2  2007/04/04 16:01:14  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.2  2007/03/09 11:25:00  jaume
44
* Advanced symbology (start committing)
45
*
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/08 15:43:04  jaume
50
* some bug fixes in the editor and removed unnecessary imports
51
*
52
* Revision 1.1.2.2  2007/01/30 18:10:10  jaume
53
* start commiting labeling stuff
54
*
55
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
56
* *** empty log message ***
57
*
58
*
59
*/
60
package org.gvsig.app.gui.styling;
61

    
62
import java.awt.Color;
63
import java.awt.Font;
64
import java.awt.FontMetrics;
65
import java.awt.Graphics;
66
import java.awt.Graphics2D;
67
import java.awt.Rectangle;
68

    
69
import org.gvsig.fmap.mapcontext.MapContextLocator;
70
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
71
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
72
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
73
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
74
import org.gvsig.i18n.Messages;
75

    
76

    
77
/**
78
* Defines the properties of the symbols that are showed in the
79
* SymbolPreviewer panel.Also the user has methods to set this attributes.
80
*
81
* @author jaume dominguez faus - jaume.dominguez@iver.es
82
*
83
*/
84
public class StylePreviewer extends SymbolPreviewer{
85
        /**
86
         * 
87
         */
88
        private static final long serialVersionUID = -1690697926703748637L;
89
        private int hGap = 5, vGap = 5;
90
        private IStyle style;
91
        private boolean showOutline;
92

    
93
        /**
94
         * Constructor method
95
         *
96
         */
97
        public StylePreviewer() {
98
                super();
99
                setBackground(Color.WHITE);
100
        }
101
        /**
102
         * Obtains the style of the symbol showed in the SymbolPreviewer panel
103
         * @return style,IStyle
104
         */
105
        public IStyle getStyle() {
106
                return style;
107
        }
108

    
109
        public ISymbol getSymbol() {
110
                throw new Error(Messages.getText("undefined_for_StylePreviewer_use")
111
                                +" getStyle() "+
112
                                Messages.getText("instead") );
113

    
114
        }
115

    
116
        public void setSymbol(ISymbol symbol) {
117
                throw new Error(Messages.getText("undefined_for_StylePreviewer_use")
118
                                +" setStyle(IStyle) "+
119
                                Messages.getText("instead") );
120

    
121
        }
122
        /**
123
         * Defines the style of the symbol showed in the SymbolPreviewer panel
124
         * @param style,IStyle
125
         */
126

    
127
        public void setStyle(IStyle style) {
128
                this.style = style;
129
//                repaint();
130

    
131
        }
132

    
133
        public void paint(Graphics g) {
134
                Graphics2D g2 = (Graphics2D) g;
135
                Rectangle r = getBounds();
136
                g2.translate(hGap, vGap);
137
                r = new Rectangle(0, 0, (int) (r.getWidth()-(hGap*2)), (int) (r.getHeight()-(vGap*2)));
138

    
139
                if (style != null) {
140
                        try {
141
                                if (showOutline) {
142
                                        // this is a trick for the editor
143
                                        style.drawOutline(g2, r);
144

    
145
                                } else {
146
                                        style.drawInsideRectangle(g2, r);
147
                                }
148
                        } catch (SymbolDrawingException e) {
149
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
150
                                        try {
151
                                                IWarningSymbol warning =
152
                                                                (IWarningSymbol) MapContextLocator.getSymbolManager()
153
                                                                                .getWarningSymbol(
154
                                                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
155
                                                                                                "",
156
                                                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
157
                                                warning.drawInsideRectangle(g2, null, r,null);
158
                                        } catch (SymbolDrawingException e1) {
159
                                                // IMPOSSIBLE TO REACH THIS
160
                                        }
161
                                } else {
162
                                        // should be unreachable code
163
                                        throw new Error(Messages.getText("symbol_shapetype_mismatch"));
164
                                }
165
                        }
166
                } else {
167
                        String noneSelected = "["+Messages.getText("preview_not_available")+"]";
168
                        FontMetrics fm = g2.getFontMetrics();
169
                        int lineWidth = fm.stringWidth(noneSelected);
170
                        float scale = (float) r.getWidth() / lineWidth;
171
                        Font f = g2.getFont();
172
                        float fontSize = f.getSize()*scale;
173
                        g2.setFont(        f.deriveFont( fontSize ) );
174

    
175
                        g2.drawString(noneSelected,         (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
176
                }
177
        }
178

    
179
        /**
180
         * Allows to choose between paint styles Outline or the style itself.
181
         * @param b
182
         */
183
        public void setShowOutline(boolean b) {
184
                this.showOutline = b;
185
        }
186
}