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

History | View | Annotate | Download (7.48 KB)

1
package org.gvsig.labeling.placements;
2

    
3
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
4
import org.gvsig.tools.ToolsLocator;
5
import org.gvsig.tools.dynobject.DynStruct;
6
import org.gvsig.tools.persistence.PersistenceManager;
7
import org.gvsig.tools.persistence.PersistentState;
8
import org.gvsig.tools.persistence.exception.PersistenceException;
9

    
10

    
11
/**
12
 * @author  jaume dominguez faus - jaume.dominguez@iver.es
13
 */
14
public abstract class AbstractPlacementConstraints implements
15
Cloneable, IPlacementConstraints {
16
        
17
        public static final String PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME =
18
                        "PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME";
19
        
20
        // CAUTION, THIS IS A CLONEABLE OBJECT, DON'T FORGET TO 
21
        // UPDATE clone() METHOD WHEN ADDING FIELDS
22
        private int duplicateLabelsMode = ONE_LABEL_PER_FEATURE_PART; // default duplicate treatment
23
        private int placementMode;
24
        private boolean belowTheLine;
25
        private boolean aboveTheLine;
26
        private boolean onTheLine;
27
        private boolean pageOriented;
28
        private int locationAlongLine;
29
        private boolean fitInsidePolygon;
30
        // CAUTION, THIS IS A CLONEABLE OBJECT, DON'T FORGET TO 
31
        // UPDATE clone() METHOD WHEN ADDING FIELDS
32
        
33
        
34
        
35
        public void setDuplicateLabelsMode(int mode) {
36
                if (mode != REMOVE_DUPLICATE_LABELS &&
37
                        mode != ONE_LABEL_PER_FEATURE &&
38
                        mode != ONE_LABEL_PER_FEATURE_PART)
39
                        throw new IllegalArgumentException(
40
                                        "Only REMOVE_DUPLICATE_LABELS, " +
41
                                        "ONE_LABEL_PER_FEATURE " +
42
                                        "or ONE_LABEL_PER_FEATURE_PARTS allowed");
43
                this.duplicateLabelsMode = mode;
44
        }
45

    
46
        public void setPlacementMode(int mode) {
47
                if (this instanceof PointPlacementConstraints) {
48
                        if (mode != OFFSET_HORIZONTALY_AROUND_THE_POINT &&
49
                                mode != ON_TOP_OF_THE_POINT &&
50
                                mode != AT_SPECIFIED_ANGLE &&
51
                                mode != AT_ANGLE_SPECIFIED_BY_A_FIELD)
52
                                throw new IllegalArgumentException(
53
                                                "Only OFFSET_HORIZONTALY_AROUND_THE_POINT, " +
54
                                                "ON_TOP_OF_THE_POINT, " +
55
                                                "AT_SPECIFIED_ANGLE " +
56
                                                "or AT_ANGLE_SPECIFIED_BY_A_FIELD allowed for points: "+ mode);
57
                }
58

    
59
                if (this instanceof PolygonPlacementConstraints) {
60
                        if (mode != HORIZONTAL &&
61
                                mode != PARALLEL )
62
                                        throw new IllegalArgumentException(
63
                                                        "Only HORIZONTAL, " +
64
                                                        "or PARALLEL allowed for polygons: "+ mode);
65
                }
66

    
67
                if (this instanceof LinePlacementConstraints) {
68
                                        if (mode != HORIZONTAL &&
69
                                mode != PARALLEL &&
70
                                mode != FOLLOWING_LINE &&
71
                                mode != PERPENDICULAR)
72
                                throw new IllegalArgumentException(
73
                                                "Only HORIZONTAL, PARALLEL," +
74
                                                "FOLLOWING_LINE, or PERPENDICULAR allowed for lines: "+ mode);
75
                }
76
                this.placementMode = mode;
77
        }
78

    
79
        public boolean isHorizontal() {
80
                return placementMode == HORIZONTAL;
81
        }
82

    
83
        public boolean isPerpendicular() {
84
                return placementMode  == PERPENDICULAR;
85
        }
86

    
87
        public boolean isFollowingLine() {
88
                return placementMode  == FOLLOWING_LINE;
89
        }
90

    
91
        public boolean isParallel() {
92
                return placementMode == PARALLEL;
93
        }
94

    
95
        public int getDuplicateLabelsMode() {
96
                return duplicateLabelsMode;
97
        }
98

    
99
        /**
100
         * Tells if the place mode selected is to put the label over the <b>POINT</b>
101
         * @return boolean
102
         */
103
        public boolean isOnTopOfThePoint() {
104
                return placementMode == ON_TOP_OF_THE_POINT;
105
        }
106

    
107
        public boolean isBelowTheLine() {
108
                return belowTheLine;
109
        }
110

    
111
        public boolean isAboveTheLine() {
112
                return aboveTheLine;
113
        }
114

    
115
        public boolean isOnTheLine() {
116
                return onTheLine;
117
        }
118

    
119
        public boolean isPageOriented() {
120
                return pageOriented;
121
        }
122

    
123
        public void setOnTheLine(boolean b) {
124
                this.onTheLine = b;
125
        }
126

    
127
        public void setPageOriented(boolean b) {
128
                this.pageOriented = b;
129
        }
130

    
131
        public void setBelowTheLine(boolean b) {
132
                this.belowTheLine = b;
133
        }
134

    
135
        public void setAboveTheLine(boolean b) {
136
                this.aboveTheLine = b;
137
        }
138

    
139
        public boolean isAtTheEndOfLine() {
140
                return locationAlongLine == AT_THE_END_OF_THE_LINE;
141
        }
142

    
143
        public boolean isAtTheBeginingOfLine() {
144
                return locationAlongLine == AT_THE_BEGINING_OF_THE_LINE;
145
        }
146

    
147
        public boolean isInTheMiddleOfLine() {
148
                return locationAlongLine == AT_THE_MIDDLE_OF_THE_LINE;
149
        }
150
        
151
        public boolean isAtBestOfLine() {
152
                return locationAlongLine == AT_BEST_OF_LINE;
153
        }
154

    
155
        public boolean isAroundThePoint() {
156
                return placementMode == OFFSET_HORIZONTALY_AROUND_THE_POINT;
157
        }
158

    
159
        public boolean isFitInsidePolygon() {
160
                return fitInsidePolygon;
161
        }
162

    
163
        public void setFitInsidePolygon(boolean b) {
164
                fitInsidePolygon = b;
165
        }
166

    
167
        public void setLocationAlongTheLine(int location) {
168
                if (location != IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE
169
                        && location != IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE
170
                        && location != IPlacementConstraints.AT_THE_END_OF_THE_LINE
171
                        && location != IPlacementConstraints.AT_BEST_OF_LINE) {
172
                        throw new IllegalArgumentException("Only IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE, " +
173
                                        "IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE, or " +
174
                                        "IPlacementConstraints.AT_THE_END_OF_THE_LINE, or " +
175
                                        "IPlacementConstraints.AT_BEST_OF_LINE values are allowed" );
176
                }
177
                this.locationAlongLine = location;
178
        }
179
        
180
        
181
        
182
        public void saveToState(PersistentState state) throws PersistenceException {
183

    
184
                state.set("duplicateLabelsMode", getDuplicateLabelsMode());
185
                state.set("placementMode", placementMode);
186
                state.set("locationAlongLine", locationAlongLine);
187
                
188
                state.set("belowTheLine", isBelowTheLine());
189
                state.set("aboveTheLine", isAboveTheLine());
190
                state.set("onTheLine", isOnTheLine());
191
                state.set("pageOriented", isPageOriented());
192
                state.set("fitInsidePolygon", isFitInsidePolygon());
193
        }
194

    
195
        public void loadFromState(PersistentState state) throws PersistenceException {
196
                
197
                setDuplicateLabelsMode(state.getInt("duplicateLabelsMode"));
198
                setPlacementMode(state.getInt("placementMode"));
199
                locationAlongLine = state.getInt("locationAlongLine");
200
                
201
                setBelowTheLine(state.getBoolean("belowTheLine"));
202
                setAboveTheLine(state.getBoolean("aboveTheLine"));
203
                setOnTheLine(state.getBoolean("onTheLine"));
204
                setPageOriented(state.getBoolean("pageOriented"));
205
                setFitInsidePolygon(state.getBoolean("fitInsidePolygon"));
206
        }
207

    
208
        public static void registerPersistent() {
209
                
210
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
211
                if( manager.getDefinition(PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME) == null ) {
212
                        DynStruct definition = manager.addDefinition(
213
                                        AbstractPlacementConstraints.class,
214
                                        PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME,
215
                                        PLACEMENT_CONSTRAINTS_PERSISTENCE_NAME +" Persistence definition",
216
                                        null, 
217
                                        null);
218
                        definition.addDynFieldBoolean("belowTheLine").setMandatory(true);
219
                        definition.addDynFieldBoolean("aboveTheLine").setMandatory(true);
220
                        definition.addDynFieldBoolean("onTheLine").setMandatory(true);
221
                        definition.addDynFieldBoolean("pageOriented").setMandatory(true);
222
                        definition.addDynFieldBoolean("fitInsidePolygon").setMandatory(true);
223
                        
224
                        definition.addDynFieldInt("duplicateLabelsMode").setMandatory(true);
225
                        definition.addDynFieldInt("placementMode").setMandatory(true);
226
                        definition.addDynFieldInt("locationAlongLine").setMandatory(true);
227
                }                
228
        }
229
        
230

    
231
        public Object clone() throws CloneNotSupportedException {
232
                try {
233
                        AbstractPlacementConstraints clone = getClass().newInstance();
234
                        clone.aboveTheLine        = this.aboveTheLine;
235
                        clone.belowTheLine        = this.belowTheLine;
236
                        clone.duplicateLabelsMode = this.duplicateLabelsMode;
237
                        clone.fitInsidePolygon    = this.fitInsidePolygon;
238
                        clone.locationAlongLine   = this.locationAlongLine;
239
                        clone.onTheLine           = this.onTheLine;
240
                        clone.pageOriented        = this.pageOriented;
241
                        clone.placementMode       = this.placementMode;
242
                        return clone;
243
                } catch (Exception e) {
244
                        throw new CloneNotSupportedException(e.getMessage());
245
                }
246
        }
247
}