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 / placements / AbstractPlacementConstraints.java @ 43510
History | View | Annotate | Download (8.35 KB)
1 | 43510 | jjdelcerro | /**
|
---|---|---|---|
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 | 40911 | jldominguez | package org.gvsig.labeling.placements; |
25 | |||
26 | import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
||
27 | import org.gvsig.tools.ToolsLocator; |
||
28 | import org.gvsig.tools.dynobject.DynStruct; |
||
29 | import org.gvsig.tools.persistence.PersistenceManager; |
||
30 | import org.gvsig.tools.persistence.PersistentState; |
||
31 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
32 | |||
33 | |||
34 | public abstract class AbstractPlacementConstraints implements |
||
35 | Cloneable, IPlacementConstraints {
|
||
36 | |||
37 | public static final String PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME = |
||
38 | "PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME";
|
||
39 | |||
40 | // CAUTION, THIS IS A CLONEABLE OBJECT, DON'T FORGET TO
|
||
41 | // UPDATE clone() METHOD WHEN ADDING FIELDS
|
||
42 | private int duplicateLabelsMode = ONE_LABEL_PER_FEATURE_PART; // default duplicate treatment |
||
43 | private int placementMode; |
||
44 | private boolean belowTheLine; |
||
45 | private boolean aboveTheLine; |
||
46 | private boolean onTheLine; |
||
47 | private boolean pageOriented; |
||
48 | private int locationAlongLine; |
||
49 | private boolean fitInsidePolygon; |
||
50 | // CAUTION, THIS IS A CLONEABLE OBJECT, DON'T FORGET TO
|
||
51 | // UPDATE clone() METHOD WHEN ADDING FIELDS
|
||
52 | |||
53 | |||
54 | |||
55 | public void setDuplicateLabelsMode(int mode) { |
||
56 | if (mode != REMOVE_DUPLICATE_LABELS &&
|
||
57 | mode != ONE_LABEL_PER_FEATURE && |
||
58 | mode != ONE_LABEL_PER_FEATURE_PART) |
||
59 | throw new IllegalArgumentException( |
||
60 | "Only REMOVE_DUPLICATE_LABELS, " +
|
||
61 | "ONE_LABEL_PER_FEATURE " +
|
||
62 | "or ONE_LABEL_PER_FEATURE_PARTS allowed");
|
||
63 | this.duplicateLabelsMode = mode;
|
||
64 | } |
||
65 | |||
66 | public void setPlacementMode(int mode) { |
||
67 | if (this instanceof PointPlacementConstraints) { |
||
68 | if (mode != OFFSET_HORIZONTALY_AROUND_THE_POINT &&
|
||
69 | mode != ON_TOP_OF_THE_POINT && |
||
70 | mode != AT_SPECIFIED_ANGLE && |
||
71 | mode != AT_ANGLE_SPECIFIED_BY_A_FIELD) |
||
72 | throw new IllegalArgumentException( |
||
73 | "Only OFFSET_HORIZONTALY_AROUND_THE_POINT, " +
|
||
74 | "ON_TOP_OF_THE_POINT, " +
|
||
75 | "AT_SPECIFIED_ANGLE " +
|
||
76 | "or AT_ANGLE_SPECIFIED_BY_A_FIELD allowed for points: "+ mode);
|
||
77 | } |
||
78 | |||
79 | if (this instanceof PolygonPlacementConstraints) { |
||
80 | if (mode != HORIZONTAL &&
|
||
81 | mode != PARALLEL ) |
||
82 | throw new IllegalArgumentException( |
||
83 | "Only HORIZONTAL, " +
|
||
84 | "or PARALLEL allowed for polygons: "+ mode);
|
||
85 | } |
||
86 | |||
87 | if (this instanceof LinePlacementConstraints) { |
||
88 | if (mode != HORIZONTAL &&
|
||
89 | mode != PARALLEL && |
||
90 | mode != FOLLOWING_LINE && |
||
91 | mode != PERPENDICULAR) |
||
92 | throw new IllegalArgumentException( |
||
93 | "Only HORIZONTAL, PARALLEL," +
|
||
94 | "FOLLOWING_LINE, or PERPENDICULAR allowed for lines: "+ mode);
|
||
95 | } |
||
96 | this.placementMode = mode;
|
||
97 | } |
||
98 | |||
99 | public boolean isHorizontal() { |
||
100 | return placementMode == HORIZONTAL;
|
||
101 | } |
||
102 | |||
103 | public boolean isPerpendicular() { |
||
104 | return placementMode == PERPENDICULAR;
|
||
105 | } |
||
106 | |||
107 | public boolean isFollowingLine() { |
||
108 | return placementMode == FOLLOWING_LINE;
|
||
109 | } |
||
110 | |||
111 | public boolean isParallel() { |
||
112 | return placementMode == PARALLEL;
|
||
113 | } |
||
114 | |||
115 | public int getDuplicateLabelsMode() { |
||
116 | return duplicateLabelsMode;
|
||
117 | } |
||
118 | |||
119 | /**
|
||
120 | * Tells if the place mode selected is to put the label over the <b>POINT</b>
|
||
121 | * @return boolean
|
||
122 | */
|
||
123 | public boolean isOnTopOfThePoint() { |
||
124 | return placementMode == ON_TOP_OF_THE_POINT;
|
||
125 | } |
||
126 | |||
127 | public boolean isBelowTheLine() { |
||
128 | return belowTheLine;
|
||
129 | } |
||
130 | |||
131 | public boolean isAboveTheLine() { |
||
132 | return aboveTheLine;
|
||
133 | } |
||
134 | |||
135 | public boolean isOnTheLine() { |
||
136 | return onTheLine;
|
||
137 | } |
||
138 | |||
139 | public boolean isPageOriented() { |
||
140 | return pageOriented;
|
||
141 | } |
||
142 | |||
143 | public void setOnTheLine(boolean b) { |
||
144 | this.onTheLine = b;
|
||
145 | } |
||
146 | |||
147 | public void setPageOriented(boolean b) { |
||
148 | this.pageOriented = b;
|
||
149 | } |
||
150 | |||
151 | public void setBelowTheLine(boolean b) { |
||
152 | this.belowTheLine = b;
|
||
153 | } |
||
154 | |||
155 | public void setAboveTheLine(boolean b) { |
||
156 | this.aboveTheLine = b;
|
||
157 | } |
||
158 | |||
159 | public boolean isAtTheEndOfLine() { |
||
160 | return locationAlongLine == AT_THE_END_OF_THE_LINE;
|
||
161 | } |
||
162 | |||
163 | public boolean isAtTheBeginingOfLine() { |
||
164 | return locationAlongLine == AT_THE_BEGINING_OF_THE_LINE;
|
||
165 | } |
||
166 | |||
167 | public boolean isInTheMiddleOfLine() { |
||
168 | return locationAlongLine == AT_THE_MIDDLE_OF_THE_LINE;
|
||
169 | } |
||
170 | |||
171 | public boolean isAtBestOfLine() { |
||
172 | return locationAlongLine == AT_BEST_OF_LINE;
|
||
173 | } |
||
174 | |||
175 | public boolean isAroundThePoint() { |
||
176 | return placementMode == OFFSET_HORIZONTALY_AROUND_THE_POINT;
|
||
177 | } |
||
178 | |||
179 | public boolean isFitInsidePolygon() { |
||
180 | return fitInsidePolygon;
|
||
181 | } |
||
182 | |||
183 | public void setFitInsidePolygon(boolean b) { |
||
184 | fitInsidePolygon = b; |
||
185 | } |
||
186 | |||
187 | public void setLocationAlongTheLine(int location) { |
||
188 | if (location != IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE
|
||
189 | && location != IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE |
||
190 | && location != IPlacementConstraints.AT_THE_END_OF_THE_LINE |
||
191 | && location != IPlacementConstraints.AT_BEST_OF_LINE) { |
||
192 | throw new IllegalArgumentException("Only IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE, " + |
||
193 | "IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE, or " +
|
||
194 | "IPlacementConstraints.AT_THE_END_OF_THE_LINE, or " +
|
||
195 | "IPlacementConstraints.AT_BEST_OF_LINE values are allowed" );
|
||
196 | } |
||
197 | this.locationAlongLine = location;
|
||
198 | } |
||
199 | |||
200 | |||
201 | |||
202 | public void saveToState(PersistentState state) throws PersistenceException { |
||
203 | |||
204 | state.set("duplicateLabelsMode", getDuplicateLabelsMode());
|
||
205 | state.set("placementMode", placementMode);
|
||
206 | state.set("locationAlongLine", locationAlongLine);
|
||
207 | |||
208 | state.set("belowTheLine", isBelowTheLine());
|
||
209 | state.set("aboveTheLine", isAboveTheLine());
|
||
210 | state.set("onTheLine", isOnTheLine());
|
||
211 | state.set("pageOriented", isPageOriented());
|
||
212 | state.set("fitInsidePolygon", isFitInsidePolygon());
|
||
213 | } |
||
214 | |||
215 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
216 | |||
217 | setDuplicateLabelsMode(state.getInt("duplicateLabelsMode"));
|
||
218 | setPlacementMode(state.getInt("placementMode"));
|
||
219 | locationAlongLine = state.getInt("locationAlongLine");
|
||
220 | |||
221 | setBelowTheLine(state.getBoolean("belowTheLine"));
|
||
222 | setAboveTheLine(state.getBoolean("aboveTheLine"));
|
||
223 | setOnTheLine(state.getBoolean("onTheLine"));
|
||
224 | setPageOriented(state.getBoolean("pageOriented"));
|
||
225 | setFitInsidePolygon(state.getBoolean("fitInsidePolygon"));
|
||
226 | } |
||
227 | |||
228 | public static void registerPersistent() { |
||
229 | |||
230 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
231 | if( manager.getDefinition(PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME) == null ) { |
||
232 | DynStruct definition = manager.addDefinition( |
||
233 | AbstractPlacementConstraints.class, |
||
234 | PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME, |
||
235 | PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME +" Persistence definition",
|
||
236 | null,
|
||
237 | null);
|
||
238 | definition.addDynFieldBoolean("belowTheLine").setMandatory(true); |
||
239 | definition.addDynFieldBoolean("aboveTheLine").setMandatory(true); |
||
240 | definition.addDynFieldBoolean("onTheLine").setMandatory(true); |
||
241 | definition.addDynFieldBoolean("pageOriented").setMandatory(true); |
||
242 | definition.addDynFieldBoolean("fitInsidePolygon").setMandatory(true); |
||
243 | |||
244 | definition.addDynFieldInt("duplicateLabelsMode").setMandatory(true); |
||
245 | definition.addDynFieldInt("placementMode").setMandatory(true); |
||
246 | definition.addDynFieldInt("locationAlongLine").setMandatory(true); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | |||
251 | public Object clone() throws CloneNotSupportedException { |
||
252 | try {
|
||
253 | AbstractPlacementConstraints clone = getClass().newInstance(); |
||
254 | clone.aboveTheLine = this.aboveTheLine;
|
||
255 | clone.belowTheLine = this.belowTheLine;
|
||
256 | clone.duplicateLabelsMode = this.duplicateLabelsMode;
|
||
257 | clone.fitInsidePolygon = this.fitInsidePolygon;
|
||
258 | clone.locationAlongLine = this.locationAlongLine;
|
||
259 | clone.onTheLine = this.onTheLine;
|
||
260 | clone.pageOriented = this.pageOriented;
|
||
261 | clone.placementMode = this.placementMode;
|
||
262 | return clone;
|
||
263 | } catch (Exception e) { |
||
264 | throw new CloneNotSupportedException(e.getMessage()); |
||
265 | } |
||
266 | } |
||
267 | } |