Statistics
| Revision:

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 / LabelStylePanTool.java @ 40911

History | View | Annotate | Download (8.17 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.Toolkit;
47
import java.awt.event.MouseEvent;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50

    
51
import javax.swing.AbstractButton;
52
import javax.swing.ImageIcon;
53
import javax.swing.JComponent;
54
import javax.swing.JToggleButton;
55

    
56
import org.gvsig.andami.IconThemeHelper;
57
import org.gvsig.app.gui.styling.EditorTool;
58
import org.gvsig.app.gui.styling.StyleEditor;
59
import org.gvsig.app.gui.styling.StylePreviewer;
60
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
61
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
62
import org.gvsig.i18n.Messages;
63

    
64
/**
65
 *
66
 * SimpleLabelStylePanTool.java
67
 *
68
 *
69
 * @author jaume dominguez faus - jaume.dominguez@iver.es Jan 7, 2008
70
 *
71
 */
72
public class LabelStylePanTool extends EditorTool {
73
        private int NO_SELECTION_ON_STYLE = -1;
74
        private int TEXT_FIELD_SELECTED = 0;
75
        private int MARKER_POINT_SELECTED = 1;
76

    
77
        private JToggleButton btnPan;
78
        private Cursor cursor;
79
        private Point pIni, pEnd;
80
        private ILabelStyle style;
81
        private int toolSelected = NO_SELECTION_ON_STYLE;
82
        private int textFieldSelected = NO_SELECTION_ON_STYLE;
83

    
84
        private Dimension bounds;
85
        private int ownerHgap,ownerVgap;
86

    
87
        public LabelStylePanTool(JComponent owner) {
88
                super(owner);
89
        }
90

    
91
        private void getBounds() {
92

    
93
                bounds = ((StyleEditor) owner).getStylePreviewer().getSize();
94
                ownerHgap =  ((StyleEditor) owner).getStylePreviewer().getHGap();
95
                ownerVgap = ((StyleEditor) owner).getStylePreviewer().getVGap();
96

    
97
        }
98

    
99
        public Cursor getCursor() {
100
                if (cursor == null) {
101
                        
102
                        ImageIcon cursorImage = IconThemeHelper.getImageIcon("cursor-pan");
103
                        cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage.getImage(),
104
                                        new Point(cursorImage.getIconWidth()/2,
105
                                                        cursorImage.getIconHeight()/2), //16, 16), "");
106
                                                        "");
107

    
108
                }
109
                return cursor;
110
        }
111

    
112
        public void mousePressed(MouseEvent e) {
113
                getBounds();
114
                Point2D marker = style.getMarkerPoint();
115
                pIni = new Point((int) (marker.getX()*(bounds.width-ownerHgap)),(int) (marker.getY()*(bounds.height-ownerVgap)));
116
                pEnd = e.getPoint();
117

    
118
                Point selectedPoint = new Point((int) (pEnd.getX()-ownerHgap),(int) (pEnd.getY()-ownerVgap));
119
                if((Math.abs(pIni.x - selectedPoint.x) < ownerHgap ) && (Math.abs(pIni.y - selectedPoint.y) < ownerVgap )){
120
                        toolSelected = MARKER_POINT_SELECTED;
121
                        return;
122
                }
123

    
124
                Point2D selPoint = screenPointToLabelPoint(pEnd);
125

    
126
                for (int i = style.getTextBounds().length - 1; i >= 0 ; i--) {
127
                        Rectangle2D rectangle = style.getTextBounds()[i];
128
                        if((selPoint.getX() >= rectangle.getMinX()) &&
129
                                        (selPoint.getX() <= rectangle.getMaxX()) &&
130
                                        (selPoint.getY() >= rectangle.getMinY())&&
131
                                        (selPoint.getY() <= rectangle.getMaxY())){
132
                                toolSelected = TEXT_FIELD_SELECTED;
133
                                textFieldSelected = i;
134
                                pIni = pEnd;
135
                                return;
136
                        }
137
                }
138
        }
139

    
140
        public void mouseReleased(MouseEvent e) {
141

    
142
                toolSelected = NO_SELECTION_ON_STYLE;
143
                textFieldSelected = NO_SELECTION_ON_STYLE;
144

    
145
        }
146

    
147
        public void mouseDragged(MouseEvent e) {
148
                pEnd = e.getPoint();
149
                if((pEnd.getX() > bounds.width - ownerHgap) || ( pEnd.getY() > bounds.getHeight() - ownerVgap) ||
150
                                (pEnd.getX() < ownerHgap) || ( pEnd.getY() < ownerVgap))
151
                        return;
152

    
153
                Point2D labelEndPoint = screenPointToLabelPoint(pEnd);
154

    
155
                if(toolSelected == TEXT_FIELD_SELECTED){
156
                        if(textFieldSelected != NO_SELECTION_ON_STYLE){
157

    
158
                                Rectangle2D rectangle = style.getTextBounds()[textFieldSelected];
159

    
160
                                Point2D labelIniPoint = screenPointToLabelPoint(pIni);
161
                                Point maxPoint = new Point((int)bounds.getWidth(), (int)bounds.getHeight());
162
                                Point2D labelMaxPoint = screenPointToLabelPoint(maxPoint);
163

    
164
                                Point2D labelZero = screenPointToLabelPoint(new Point(0,0));
165
                                double x = rectangle.getX() + labelEndPoint.getX()-labelIniPoint.getX();
166
                                if (x<labelZero.getX()){ x=labelZero.getX(); }
167
                                if ((x+rectangle.getWidth())>=labelMaxPoint.getX()) { x =  labelMaxPoint.getX()-rectangle.getWidth();}
168

    
169
                                double y = rectangle.getY() + labelEndPoint.getY()-labelIniPoint.getY();
170
                                if (y<labelZero.getY()){ y=labelZero.getY(); }
171
                                if ((y+rectangle.getHeight())>=labelMaxPoint.getY()) { y =  labelMaxPoint.getY()-rectangle.getHeight();}
172

    
173
                                rectangle.setRect(x,
174
                                                y,
175
                                                rectangle.getWidth(),
176
                                                rectangle.getHeight());
177
                                style.setTextFieldArea(textFieldSelected,rectangle);
178
                                pIni = pEnd;
179
                        }
180
                }
181
                else if (toolSelected == MARKER_POINT_SELECTED){
182
                        double xOffset = (pEnd.getX())/( bounds.getWidth()- ownerHgap );
183
                        double yOffset = (pEnd.getY())/( bounds.getHeight()- ownerVgap );
184

    
185
                        if(xOffset > 1) xOffset = 1;
186
                        if(xOffset < 0) xOffset = 0;
187
                        if(yOffset > 1) yOffset = 1;
188
                        if(yOffset < 0) yOffset = 0;
189

    
190
                        Point2D marker = style.getMarkerPoint();
191
                        marker.setLocation(xOffset, yOffset);
192
                }
193
                owner.repaint();
194
        }
195

    
196
        private Point2D screenPointToLabelPoint(Point pIni) {
197
                //FIXME: Esto es un parche, habr?a que cambiar la API de los estilos y simbolos
198
                //pero mientras tanto
199
                int minx = pIni.x;
200
                int miny = pIni.y;
201

    
202
                StylePreviewer sp = ((StyleEditor) owner).getStylePreviewer();
203
                Dimension bounds = sp.getSize();
204

    
205
                IStyle style = sp.getStyle();
206
                Dimension backgroundBounds = null;
207
                if (style instanceof ILabelStyle){
208
                        backgroundBounds = ((ILabelStyle)style).getSize();
209
                }
210
                Point2D p;
211
                if (backgroundBounds == null){
212
                        p = new Point2D.Double(
213
                                        pIni.x/(bounds.getWidth()-sp.getHGap()/2),
214
                                        pIni.y/(bounds.getHeight()-sp.getVGap()/2));
215
                } else {
216
                        double xOffset = 0;
217
                        double yOffset = 0;
218
                        double scale = 1;
219
                        if (backgroundBounds.getWidth()>backgroundBounds.getHeight()) {
220
                                scale = (bounds.getWidth()-sp.getHGap())/backgroundBounds.getWidth();
221
                                yOffset = 0.5*(bounds.getHeight()-sp.getVGap() - backgroundBounds.getHeight()*scale);
222
                        } else {
223
                                scale = (bounds.getHeight()-sp.getVGap())/backgroundBounds.getHeight();
224
                                xOffset = 0.5*(bounds.getWidth()-sp.getHGap() - backgroundBounds.getWidth()*scale);
225
                        }
226
                        p = new Point2D.Double(
227
                                        ((minx-(sp.getHGap()/2)-xOffset)/scale)/backgroundBounds.getWidth(),
228
                                        ((miny-(sp.getVGap()/2)-yOffset)/scale)/backgroundBounds.getHeight());
229
                }
230
                return p;
231
        }
232

    
233
        @Override
234
        public AbstractButton getButton() {
235
                return getBtnPan();
236
        }
237

    
238
        private JToggleButton getBtnPan() {
239
                if (btnPan == null) {
240
                        btnPan = new JToggleButton(
241
                                        IconThemeHelper.getImageIcon("edit-shift-component"));
242
                        btnPan.setToolTipText(
243
                                        Messages.getText("_Set_label_offset"));
244
                        btnPan.setPreferredSize(EditorTool.SMALL_BTN_SIZE);
245
                        btnPan.setSize(EditorTool.SMALL_BTN_SIZE);
246
                }
247

    
248
                return btnPan;
249
        }
250

    
251
        @Override
252
        public String getID() {
253
                return "1";
254
        }
255

    
256
        @Override
257
        public boolean isSuitableFor(Object obj) {
258
                return obj instanceof ILabelStyle;
259
        }
260

    
261
        @Override
262
        public void setModel(Object objectToBeEdited) {
263
                style = (ILabelStyle) objectToBeEdited;
264
        }
265
}