Revision 11073 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/core/styles/SimpleLabelStyle.java

View differences:

SimpleLabelStyle.java
43 43
*
44 44
* $Id$
45 45
* $Log$
46
* Revision 1.7  2007-04-04 15:42:03  jaume
46
* Revision 1.8  2007-04-05 16:07:14  jaume
47
* Styled labeling stuff
48
*
49
* Revision 1.7  2007/04/04 15:42:03  jaume
47 50
* *** empty log message ***
48 51
*
49 52
* Revision 1.6  2007/04/04 15:41:05  jaume
......
72 75
package com.iver.cit.gvsig.fmap.core.styles;
73 76

  
74 77
import java.awt.Color;
75
import java.awt.Dimension;
76 78
import java.awt.Graphics2D;
77
import java.awt.Point;
78 79
import java.awt.Rectangle;
79
import java.awt.geom.AffineTransform;
80 80
import java.awt.geom.Point2D;
81 81
import java.awt.geom.Rectangle2D;
82
import java.util.ArrayList;
82 83

  
83 84
import com.iver.cit.gvsig.fmap.Messages;
85
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
86
import com.iver.cit.gvsig.fmap.core.FShape;
87
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
84 88
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
89
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
90
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
85 91
import com.iver.utiles.XMLEntity;
86 92

  
87 93

  
88 94
/**
89 95
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
90 96
 */
91
public class SimpleLabelStyle extends SVGStyle implements ILabelStyle{
97
public class SimpleLabelStyle extends SVGStyle implements ILabelStyle {
92 98
	private String text;
93 99
	private Point2D markerPoint = new Point2D.Double();
100
	// conertir a?? a Rectangle2D[] ja que pot arribar a gastar-se masivament
101
	// en el pintat
102
	private ArrayList textFieldAreas = new ArrayList();
94 103

  
95 104
	public int getFieldCount() {
96 105
		return 1;
......
123 132
		xml.putProperty("className", getClassName());
124 133
		xml.putProperty("desc", getDescription());
125 134
		xml.putProperty("text", text);
135
		xml.putProperty("markerPointX", markerPoint.getX());
136
		xml.putProperty("markerPointY", markerPoint.getY());
137

  
138
		int size = getFieldCount();
139
		String[] minx = new String[size];
140
		String[] miny = new String[size];
141
		String[] widths = new String[size];
142
		String[] heights = new String[size];
143

  
144
		Rectangle2D[] rects = getTextBounds();
145
		for (int i = 0; i < rects.length; i++) {
146
			minx[i] = String.valueOf(rects[i].getMinX());
147
			miny[i] = String.valueOf(rects[i].getMinY());
148
			widths[i] = String.valueOf(rects[i].getHeight());
149
			heights[i] = String.valueOf(rects[i].getWidth());
150
		}
151

  
152
		xml.putProperty("minXArray", minx);
153
		xml.putProperty("minYArray", miny);
154
		xml.putProperty("widthArray", widths);
155
		xml.putProperty("heightArray", heights);
126 156
		return xml;
127 157
	}
128 158

  
......
130 160
		super.setXMLEntity(xml);
131 161
		setDescription(xml.getStringProperty("desc"));
132 162
		text = xml.getStringProperty("text");
163
		double x = xml.getDoubleProperty("markerPointX");
164
		double y = xml.getDoubleProperty("markerPointY");
165

  
166
		double[] minx = xml.getDoubleArrayProperty("minXArray");
167
		double[] miny = xml.getDoubleArrayProperty("minYArray");
168
		double[] widths = xml.getDoubleArrayProperty("widthArray");
169
		double[] heights = xml.getDoubleArrayProperty("heightArray");
170
		for (int i = 0; i < minx.length; i++) {
171
			addTextFieldArea(new Rectangle2D.Double(minx[i], miny[i], widths[i], heights[i]));
172
		}
173
		markerPoint.setLocation(x, y);
133 174
	}
134 175

  
135
	public Rectangle[] getTextBounds() {
136
		return new Rectangle[] {new Rectangle(0,0,300,-60)};
176
	public Rectangle2D[] getTextBounds() {
177
		return (Rectangle2D[]) textFieldAreas.toArray(new Rectangle2D[textFieldAreas.size()]);
137 178
	}
138 179

  
139 180
	public void drawInsideRectangle(Graphics2D g, Rectangle r) {
......
166 207
		double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
167 208
		double ratioViewPort = r.getWidth() / r.getWidth();
168 209

  
169
		int x;
170
		int y;
171
		if (ratioViewPort > ratioLabel) {
172
			// size is defined by the viewport height
173
			y = (int) (r.height*markerPoint.getY());
174
			x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height));
175
		} else {
176
			// size is defined by the viewport width
177
			x = (int) (r.width * markerPoint.getX());
178
			y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel));
210
		// draw the pointer
211
		{
212
			int x;
213
			int y;
214
			if (ratioViewPort > ratioLabel) {
215
				// size is defined by the viewport height
216
				y = (int) (r.height*markerPoint.getY());
217
				x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height));
218
			} else {
219
				// size is defined by the viewport width
220
				x = (int) (r.width * markerPoint.getX());
221
				y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel));
222
			}
223
			y = r.height - y;
224

  
225
			int size = 7;
226
			g.setColor(Color.ORANGE.darker());
227
			g.fillOval(x, y, size, size);
228
			g.setColor(Color.BLACK.brighter());
229
			g.drawString(Messages.getString("labeled_point"), x + size + 10, y + size);
230
			g.setColor(Color.BLACK);
231
			g.drawLine(x-size, (int) (y+(size*0.5)), x+2*size-1, (int) (y+(size*0.5)));
232
			g.drawLine((int) (x+(size*0.5)), y-size, (int) (x+(size*0.5)), y+2*size-1);
179 233
		}
180
		y = r.height - y;
181 234

  
182
		int size = 3;
183
		g.setColor(Color.RED);
184
		g.fillOval(x, y, size, size);
185
		g.drawString(Messages.getString("labeled_point"), x + size + 5, y);
235
		// draw the text fields
236
		if (textFieldAreas.size() > 0) {
237
			SimpleFillSymbol sym = new SimpleFillSymbol();
238
			Color c = Color.YELLOW;
239

  
240
			sym.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
241
			SimpleLineSymbol outline = new SimpleLineSymbol();
242
			c = Color.BLACK;
243
			outline.setLineColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
244
			sym.setOutline(outline);
245
			for (int i = 0; i < textFieldAreas.size(); i++) {
246
				Rectangle2D textFieldArea = (Rectangle2D) textFieldAreas.get(i);
247

  
248
				int x = (int) (r.width * textFieldArea.getX());
249
				int y = (int) (r.height * textFieldArea.getY());
250
				int width = (int) (r.width * textFieldArea.getWidth());
251
				int height = (int) (r.width * textFieldArea.getHeight());
252
				Rectangle aux = new Rectangle(x, y, width, height);
253
				FShape shp = new FPolygon2D(new GeneralPathX(aux));
254
				sym.draw(g, null, shp);
255
				g.setColor(Color.BLACK);
256
				g.drawString(String.valueOf(i), x+5, y + 10);
257
			}
258
		}
186 259
	}
260
	public void setTextFieldArea(int index, Rectangle2D rect) {
261
		// patch, please remove it
262
		if (textFieldAreas.size()==0)
263
			textFieldAreas.add(new Rectangle2D.Double());
264
		// end patch
265
		textFieldAreas.set(index, rect);
266
	}
187 267

  
268
	public void addTextFieldArea(Rectangle2D rect) {
269
		textFieldAreas.add(rect);
270
	}
271

  
272
	public void deleteTextFieldArea(int index) {
273
		textFieldAreas.remove(index);
274
	}
275

  
188 276
}

Also available in: Unified diff