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