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 / layerproperties / DefaultLabeling.java @ 40911
History | View | Annotate | Download (10.4 KB)
1 |
/**
|
---|---|
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 |
package org.gvsig.labeling.gui.layerproperties; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.FlowLayout; |
28 |
import java.awt.IllegalComponentStateException; |
29 |
import java.awt.Rectangle; |
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.awt.event.ItemEvent; |
33 |
import java.awt.event.ItemListener; |
34 |
import java.beans.PropertyChangeEvent; |
35 |
|
36 |
import javax.swing.JButton; |
37 |
import javax.swing.JCheckBox; |
38 |
import javax.swing.JPanel; |
39 |
import javax.swing.JSplitPane; |
40 |
|
41 |
import org.gvsig.andami.messages.NotificationManager; |
42 |
import org.gvsig.app.ApplicationLocator; |
43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.exception.ReadException; |
45 |
import org.gvsig.fmap.dal.feature.Feature; |
46 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
47 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
48 |
import org.gvsig.fmap.dal.feature.FeatureType; |
49 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
50 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod; |
51 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy; |
52 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
53 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints; |
54 |
import org.gvsig.gui.beans.swing.JBlank; |
55 |
import org.gvsig.i18n.Messages; |
56 |
import org.gvsig.labeling.gui.styling.LayerPreview; |
57 |
import org.gvsig.labeling.label.GeneralLabelingStrategy; |
58 |
import org.gvsig.labeling.lang.LabelClassUtils; |
59 |
import org.gvsig.symbology.SymbologyLocator; |
60 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.DefaultLabelingMethod; |
61 |
import org.gvsig.tools.dispose.DisposableIterator; |
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
|
66 |
public class DefaultLabeling extends AbstractLabelingMethodPanel implements ActionListener { |
67 |
|
68 |
private static final Logger logger = |
69 |
LoggerFactory.getLogger(DefaultLabeling.class); |
70 |
|
71 |
private static final long serialVersionUID = 7100208944546698724L; |
72 |
private ILabelClass defaultLabel;
|
73 |
private IPlacementConstraints placementConstraints;
|
74 |
private LayerPreview layerPrev;
|
75 |
private JCheckBox enableLayerPrev; |
76 |
private LabelClassPreview labelPrev;
|
77 |
private JButton btnProperties; |
78 |
|
79 |
private FeatureAttributeDescriptor[] fieldDescs; |
80 |
|
81 |
private boolean allowOverlap; |
82 |
private IZoomConstraints zoomConstraints;
|
83 |
private LabelClassProperties lcProp;
|
84 |
|
85 |
public Class<? extends ILabelingMethod> getLabelingMethodClass() { |
86 |
return DefaultLabelingMethod.class;
|
87 |
} |
88 |
|
89 |
|
90 |
public String getName() { |
91 |
return Messages.getText("label_features_in_the_same_way") + "."; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void fillPanel(ILabelingMethod method, FeatureType fty) { |
96 |
try {
|
97 |
if (enableLayerPrev.isSelected()) {
|
98 |
layerPrev.setLayer(layer); |
99 |
} else {
|
100 |
layerPrev.setLayer(null);
|
101 |
} |
102 |
|
103 |
fieldDescs = fty.getAttributeDescriptors(); |
104 |
|
105 |
ILabelingStrategy labeling = layer.getLabelingStrategy(); |
106 |
if (!(labeling instanceof GeneralLabelingStrategy)) { |
107 |
labeling = new GeneralLabelingStrategy();
|
108 |
labeling.setLayer(layer); |
109 |
layer.setLabelingStrategy(labeling); |
110 |
} |
111 |
|
112 |
} catch (Exception e) { |
113 |
NotificationManager.addWarning(e.getMessage(), e); |
114 |
} |
115 |
|
116 |
ILabelClass lc = null;
|
117 |
if (method.getLabelClasses() != null && method.getLabelClasses().length > 0) { |
118 |
lc = method.getLabelClasses()[0];
|
119 |
} else {
|
120 |
lc = SymbologyLocator.getSymbologyManager().createDefaultLabel(); |
121 |
} |
122 |
setLabel(lc); |
123 |
getLcProp(); |
124 |
} |
125 |
|
126 |
private JButton getBtnProperties(){ |
127 |
if (btnProperties == null){ |
128 |
btnProperties = new JButton(Messages.getText("properties")); |
129 |
btnProperties.addActionListener(this);
|
130 |
} |
131 |
return btnProperties;
|
132 |
} |
133 |
|
134 |
private LabelClassPreview getLabelPrev(){
|
135 |
if (labelPrev == null){ |
136 |
labelPrev = new LabelClassPreview();
|
137 |
} |
138 |
return labelPrev;
|
139 |
} |
140 |
|
141 |
private JCheckBox getEnableLayerPreview(){ |
142 |
if(enableLayerPrev == null){ |
143 |
enableLayerPrev = new JCheckBox( |
144 |
Messages.getText("Enable_layer_preview"));
|
145 |
enableLayerPrev.addItemListener(new ItemListener() { |
146 |
|
147 |
public void itemStateChanged(ItemEvent e) { |
148 |
try {
|
149 |
if (e.getStateChange()==ItemEvent.SELECTED) { |
150 |
layerPrev.setLayer(layer); |
151 |
} |
152 |
else if (e.getStateChange()==ItemEvent.DESELECTED) { |
153 |
layerPrev.setLayer(null);
|
154 |
} |
155 |
} catch (ReadException e1) {
|
156 |
logger.error("While setting changing layer in preview", e1);
|
157 |
} |
158 |
} |
159 |
}); |
160 |
} |
161 |
return enableLayerPrev;
|
162 |
} |
163 |
|
164 |
private LayerPreview getLayerPreview(){
|
165 |
if (layerPrev == null){ |
166 |
layerPrev = new LayerPreview();
|
167 |
} |
168 |
return layerPrev;
|
169 |
} |
170 |
@Override
|
171 |
protected void initializePanel() { |
172 |
setLayout(new BorderLayout()); |
173 |
JSplitPane scrl = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); |
174 |
scrl.add(getLayerPreview(), JSplitPane.LEFT);
|
175 |
|
176 |
labelPrev = getLabelPrev(); //new LabelClassPreview();
|
177 |
JPanel aux = new JPanel(new BorderLayout()); |
178 |
aux.add(new JBlank(10, 10), BorderLayout.NORTH ); |
179 |
aux.add(new JBlank(10, 10), BorderLayout.WEST ); |
180 |
aux.add(labelPrev, BorderLayout.CENTER);
|
181 |
aux.add(new JBlank(10, 10), BorderLayout.EAST ); |
182 |
JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10)); |
183 |
btnProperties = getBtnProperties(); |
184 |
aux2.add(btnProperties); |
185 |
// btnProperties.addActionListener(this);
|
186 |
aux.add(aux2, BorderLayout.SOUTH);
|
187 |
scrl.add(aux, JSplitPane.RIGHT);
|
188 |
add(scrl, BorderLayout.CENTER);
|
189 |
scrl.setDividerLocation(500);
|
190 |
|
191 |
getEnableLayerPreview().setSelected(false);
|
192 |
add(enableLayerPrev, BorderLayout.SOUTH);
|
193 |
} |
194 |
|
195 |
private LabelClassProperties getLcProp(){
|
196 |
if(lcProp == null) { |
197 |
|
198 |
int n = this.fieldDescs.length; |
199 |
String[] fnames = new String[n]; |
200 |
int[] ftypes = new int[n]; |
201 |
for (int i=0; i<n; i++) { |
202 |
fnames[i] = fieldDescs[i].getName(); |
203 |
ftypes[i] = fieldDescs[i].getType(); |
204 |
} |
205 |
lcProp = new LabelClassProperties(this.layer.getFeatureStore()); |
206 |
lcProp.setLabelClass(method.getLabelClasses()[0]);
|
207 |
} |
208 |
return lcProp;
|
209 |
|
210 |
} |
211 |
|
212 |
public void actionPerformed(ActionEvent e) { |
213 |
if (e.getSource().equals(getBtnProperties())) {
|
214 |
// LabelClassProperties lcProp = new LabelClassProperties(fieldNames, fieldTypes);
|
215 |
// lcProp.setLabelClass(method.getLabelClasses()[0]);
|
216 |
// PluginServices.getMDIManager().addWindow(lcProp);
|
217 |
// setLabel(lcProp.getLabelClass());
|
218 |
LabelClassProperties lcProp = getLcProp(); |
219 |
ILabelClass lc = defaultLabel; |
220 |
lcProp.setLabelClass(lc); |
221 |
boolean eval = false; |
222 |
while (!eval){
|
223 |
|
224 |
ApplicationLocator.getManager().getUIManager().addWindow(lcProp); |
225 |
if(!lcProp.isAccepted()){ break; }; |
226 |
lc = lcProp.getLabelClass(); |
227 |
eval = checkValidSQL(lc); |
228 |
} |
229 |
setLabel(lc); |
230 |
} |
231 |
} |
232 |
|
233 |
private boolean checkValidSQL(ILabelClass lc){ |
234 |
|
235 |
String sqlQuery = lc.getSQLQuery();
|
236 |
if (sqlQuery != null && sqlQuery.trim().length() > 0) { |
237 |
return LabelClassUtils.validExpression(
|
238 |
sqlQuery, layer.getFeatureStore(), true);
|
239 |
} else {
|
240 |
return true; |
241 |
} |
242 |
|
243 |
} |
244 |
|
245 |
private void setLabel(ILabelClass labelClass) { |
246 |
|
247 |
defaultLabel = labelClass; |
248 |
// defaultLabel = LabelingFactory.createLabelClassFromXML(labelClass.getXMLEntity());
|
249 |
labelPrev.setLabelClass(defaultLabel); |
250 |
method = newMethodForThePreview(defaultLabel); |
251 |
|
252 |
updatePreview(); |
253 |
|
254 |
} |
255 |
|
256 |
protected ILabelingMethod newMethodForThePreview(ILabelClass defaultLabel) {
|
257 |
|
258 |
ILabelingMethod resp = |
259 |
SymbologyLocator.getSymbologyManager().createDefaultLabelingMethod(); |
260 |
resp.addLabelClass(defaultLabel); |
261 |
return resp;
|
262 |
} |
263 |
|
264 |
private void updatePreview() { |
265 |
|
266 |
ILabelingStrategy stra = layer.getLabelingStrategy(); |
267 |
if (method == null){ |
268 |
stra.setLabelingMethod(newMethodForThePreview(defaultLabel)); |
269 |
} else {
|
270 |
stra.setLabelingMethod(method); |
271 |
} |
272 |
// s.setPlacementConstraints(placementConstraints);
|
273 |
// s.setAllowOverlapping(allowOverlap);
|
274 |
// s.setZoomConstraints(zoomConstraints);
|
275 |
|
276 |
layer.setIsLabeled(true);
|
277 |
|
278 |
DisposableIterator diter = null;
|
279 |
FeatureSelection fsel = null;
|
280 |
|
281 |
try {
|
282 |
diter = layer.getFeatureStore().getFeatureSet().fastIterator(); |
283 |
fsel = layer.getFeatureStore().getFeatureSelection(); |
284 |
fsel.deselectAll(); |
285 |
} catch (DataException de) {
|
286 |
logger.error("While getting store selection", de);
|
287 |
return;
|
288 |
} |
289 |
|
290 |
long count = 0; |
291 |
Feature feat = null;
|
292 |
while (diter.hasNext() && count < 200) { |
293 |
feat = (Feature) diter.next(); |
294 |
if (count % 4 == 0) { |
295 |
fsel.select(feat); |
296 |
} |
297 |
count++; |
298 |
} |
299 |
diter.dispose(); |
300 |
|
301 |
try {
|
302 |
Rectangle r = layerPrev.getBounds();
|
303 |
r.setLocation(layerPrev.getLocationOnScreen()); |
304 |
layerPrev.paintImmediately(r); |
305 |
layerPrev.doLayout(); |
306 |
} catch (IllegalComponentStateException ex) { |
307 |
// this happens when the component is not showing in the
|
308 |
// screen. If that is the case, then we don't need to do
|
309 |
// anything.
|
310 |
} |
311 |
|
312 |
} |
313 |
|
314 |
public void propertyChange(PropertyChangeEvent evt) { |
315 |
|
316 |
String prop = evt.getPropertyName();
|
317 |
ILabelingStrategy strat = layer.getLabelingStrategy(); |
318 |
|
319 |
if (AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS.equals(prop)) {
|
320 |
placementConstraints = (IPlacementConstraints) evt.getNewValue(); |
321 |
strat.setPlacementConstraints(placementConstraints); |
322 |
} else if ((strat instanceof GeneralLabelingStrategy) && |
323 |
AbstractLabelingMethodPanel.ALLOW_OVERLAP.equals(prop)) { |
324 |
allowOverlap = (Boolean) evt.getNewValue();
|
325 |
((GeneralLabelingStrategy) strat).setAllowOverlapping(allowOverlap); |
326 |
} else if (AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS.equals(prop)) { |
327 |
zoomConstraints = (IZoomConstraints) evt.getNewValue(); |
328 |
strat.setZoomConstraints(zoomConstraints); |
329 |
} |
330 |
|
331 |
updatePreview(); |
332 |
} |
333 |
|
334 |
public void setEnabled(boolean enabled) { |
335 |
super.setEnabled(enabled);
|
336 |
if (layerPrev!=null) { |
337 |
layerPrev.setEnabled(enabled); |
338 |
}; |
339 |
if (labelPrev!=null) { |
340 |
labelPrev.setEnabled(enabled); |
341 |
}; |
342 |
if (btnProperties!=null) { |
343 |
btnProperties.setEnabled(enabled); |
344 |
}; |
345 |
} |
346 |
|
347 |
|
348 |
|
349 |
|
350 |
} |