Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / editortools / LabelStyleNewTextFieldTool.java @ 25807

History | View | Annotate | Download (5.38 KB)

1 20768 jdominguez
/* 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
package org.gvsig.symbology.gui.styling.editortools;
42
43
import java.awt.Cursor;
44 22836 vcaballero
import java.awt.Dimension;
45 20768 jdominguez
import java.awt.Point;
46
import java.awt.Rectangle;
47
import java.awt.event.MouseEvent;
48
import java.awt.geom.Rectangle2D;
49
50
import javax.swing.AbstractButton;
51
import javax.swing.JComponent;
52
import javax.swing.JToggleButton;
53
54 25807 vcaballero
import org.gvsig.symbology.fmap.styles.SimpleLabelStyle;
55
56 20768 jdominguez
import com.iver.andami.PluginServices;
57
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
58 25807 vcaballero
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
59 20768 jdominguez
import com.iver.cit.gvsig.gui.styling.EditorTool;
60 22836 vcaballero
import com.iver.cit.gvsig.gui.styling.StyleEditor;
61 25807 vcaballero
import com.iver.cit.gvsig.gui.styling.StylePreviewer;
62 20768 jdominguez
63
public class LabelStyleNewTextFieldTool extends EditorTool {
64
        private Point pIni, pEnd;
65
        private final Cursor cursor = Cursor.getDefaultCursor();
66
        private ILabelStyle style;
67
        private JToggleButton btnNewTextArea;
68 22836 vcaballero
69 20768 jdominguez
        public LabelStyleNewTextFieldTool(JComponent targetEditor) {
70
                super(targetEditor);
71
                // TODO Auto-generated constructor stub
72
        }
73
        public Cursor getCursor() {
74
                return cursor;
75
        }
76
77
        public void mousePressed(MouseEvent e) {
78
                pIni = e.getPoint();
79
                pEnd = e.getPoint();
80
                Rectangle2D rect = screenPointsToLabelRect(pIni, pEnd);
81
                style.addTextFieldArea(rect);
82
        }
83 22836 vcaballero
84 20768 jdominguez
        public void mouseReleased(MouseEvent e) {
85
86
        }
87 22836 vcaballero
88 20768 jdominguez
        public void mouseDragged(MouseEvent e) {
89 22836 vcaballero
                Dimension bounds = ((StyleEditor) owner).getStylePreviewer().getSize();
90 20768 jdominguez
                pEnd = e.getPoint();
91 22836 vcaballero
92
                if (pEnd.getX() < 0)
93
                        pEnd.x = 0;
94
                if (pEnd.getX() > bounds.getWidth())
95
                        pEnd.x = (int) bounds.getWidth();
96
97
                if (pEnd.getY() < 0)
98
                        pEnd.y = 0;
99
                if (pEnd.getY() > bounds.getHeight())
100
                        pEnd.y = (int) bounds.getHeight();
101
102 20768 jdominguez
                Rectangle2D rect = screenPointsToLabelRect(pIni, pEnd);
103
                style.setTextFieldArea(style.getFieldCount()-1, rect);
104
                owner.repaint();
105
        }
106
107
        private Rectangle2D screenPointsToLabelRect(Point pIni, Point pEnd) {
108
                int minx = pIni.x;
109
                int miny = pIni.y;
110 22836 vcaballero
111 20768 jdominguez
                int width = pEnd.x-pIni.x;
112
                int height = pEnd.y - pIni.y;
113
                if (width < 0) {
114
                        minx += width;
115
                        width = -width;
116
                }
117
                if (height <0) {
118
                        miny += height;
119
                        height = -height;
120
                }
121
122 25807 vcaballero
                StylePreviewer sp = ((StyleEditor) owner).getStylePreviewer();
123
                Dimension bounds = sp.getSize();
124 20768 jdominguez
125 25807 vcaballero
                IStyle style = sp.getStyle();
126
                Dimension backgroundBounds = null;
127
                if (style instanceof SimpleLabelStyle){
128
                        backgroundBounds = ((SimpleLabelStyle)style).getSize();
129
                }
130
                //FIXME: Esto es un parche, habr?a que cambiar la API de los estilos y simbolos
131
                //pero mientras tanto
132
                Rectangle2D rect;
133
                if (backgroundBounds == null){
134
                        rect = new Rectangle2D.Double(
135
                                        minx/(bounds.getWidth()-sp.getHGap()/2), //OJO, aqu? pon?a cuatro 10's a pi?on fijo
136
                                        miny/(bounds.getHeight()-sp.getVGap()/2),
137
                                        width/(bounds.getWidth()-sp.getHGap()),
138
                                        height/(bounds.getHeight()-sp.getVGap())
139
                        );
140
                } else {
141 22836 vcaballero
142 25807 vcaballero
                        double xOffset = 0;
143
                        double yOffset = 0;
144
                        double scale = 1;
145
                        if (backgroundBounds.getWidth()>backgroundBounds.getHeight()) {
146
                                scale = (bounds.getWidth()-sp.getHGap())/backgroundBounds.getWidth();
147
                                yOffset = 0.5*(bounds.getHeight()-sp.getVGap() - backgroundBounds.getHeight()*scale);
148
                        } else {
149
                                scale = (bounds.getHeight()-sp.getVGap())/backgroundBounds.getHeight();
150
                                xOffset = 0.5*(bounds.getWidth()-sp.getHGap() - backgroundBounds.getWidth()*scale);
151
                        }
152
153
                        rect = new Rectangle2D.Double(
154
                                        ((minx-(sp.getHGap()/2)-xOffset)/scale)/backgroundBounds.getWidth(),
155
                                        ((miny-(sp.getVGap()/2)-yOffset)/scale)/backgroundBounds.getHeight(),
156
                                        (width/scale)/backgroundBounds.getWidth(),
157
                                        (height/scale)/backgroundBounds.getHeight()
158
                        );
159
                }
160
161 20768 jdominguez
                return rect;
162
        }
163
164
        public AbstractButton getButton() {
165
                return getBtnNewTextArea();
166
        }
167 22836 vcaballero
168 20768 jdominguez
        @Override
169
        public String getID() {
170
                return "2";
171
        }
172 22836 vcaballero
173 20768 jdominguez
        @Override
174
        public boolean isSuitableFor(Object obj) {
175
                return obj instanceof ILabelStyle;
176
        }
177 22836 vcaballero
178 20768 jdominguez
        @Override
179
        public void setModel(Object objectToBeEdited) {
180
                style = (ILabelStyle) objectToBeEdited;
181
        }
182
183
        private JToggleButton getBtnNewTextArea() {
184
                if (btnNewTextArea == null) {
185
                        btnNewTextArea = new JToggleButton(PluginServices.getIconTheme().get("add-text-icon"));
186
                        btnNewTextArea.setToolTipText(PluginServices.getText(this, "add_text_area"));
187
                        btnNewTextArea.setSize(EditorTool.SMALL_BTN_SIZE);
188
                }
189
                return btnNewTextArea;
190
        }
191
}