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 @ 42981
History | View | Annotate | Download (11.6 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.awt.geom.Rectangle2D; |
35 |
import java.beans.PropertyChangeEvent; |
36 |
|
37 |
import javax.swing.JButton; |
38 |
import javax.swing.JCheckBox; |
39 |
import javax.swing.JOptionPane; |
40 |
import javax.swing.JPanel; |
41 |
import javax.swing.JSplitPane; |
42 |
|
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
import org.gvsig.andami.messages.NotificationManager; |
47 |
import org.gvsig.app.ApplicationLocator; |
48 |
import org.gvsig.fmap.dal.exception.ReadException; |
49 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
50 |
import org.gvsig.fmap.dal.feature.FeatureType; |
51 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
52 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod; |
53 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy; |
54 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints; |
55 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints; |
56 |
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle; |
57 |
import org.gvsig.gui.beans.swing.JBlank; |
58 |
import org.gvsig.i18n.Messages; |
59 |
import org.gvsig.labeling.gui.styling.LayerPreview; |
60 |
import org.gvsig.labeling.label.GeneralLabelingStrategy; |
61 |
import org.gvsig.labeling.lang.LabelClassUtils; |
62 |
import org.gvsig.symbology.SymbologyLocator; |
63 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.DefaultLabelingMethod; |
64 |
|
65 |
public class DefaultLabeling extends AbstractLabelingMethodPanel implements |
66 |
ActionListener {
|
67 |
|
68 |
private static final Logger logger = LoggerFactory |
69 |
.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 |
private JSplitPane scrl; |
86 |
private JPanel aux; |
87 |
private JPanel aux2; |
88 |
|
89 |
|
90 |
public Class<? extends ILabelingMethod> getLabelingMethodClass() { |
91 |
return DefaultLabelingMethod.class;
|
92 |
} |
93 |
|
94 |
public String getName() { |
95 |
return Messages.getText("label_features_in_the_same_way") + "."; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void fillPanel(FeatureType fty) { |
100 |
try {
|
101 |
if (getEnableLayerPreview().isSelected()) {
|
102 |
layerPrev.setLayer(layer); |
103 |
} else {
|
104 |
layerPrev.setLayer(null);
|
105 |
} |
106 |
|
107 |
fieldDescs = fty.getAttributeDescriptors(); |
108 |
|
109 |
ILabelingStrategy labeling = layer.getLabelingStrategy(); |
110 |
if (!(labeling instanceof GeneralLabelingStrategy)) { |
111 |
labeling = new GeneralLabelingStrategy();
|
112 |
labeling.setLayer(layer); |
113 |
layer.setLabelingStrategy(labeling); |
114 |
} |
115 |
|
116 |
} catch (Exception e) { |
117 |
NotificationManager.addWarning(e.getMessage(), e); |
118 |
} |
119 |
|
120 |
ILabelClass lc = null;
|
121 |
if (method.getLabelClasses() != null |
122 |
&& method.getLabelClasses().length > 0) {
|
123 |
lc = method.getLabelClasses()[0];
|
124 |
} else {
|
125 |
lc = SymbologyLocator.getSymbologyManager().createDefaultLabel(); |
126 |
} |
127 |
setLabel(lc); |
128 |
getLcProp(); |
129 |
} |
130 |
|
131 |
private JButton getBtnProperties() { |
132 |
if (btnProperties == null) { |
133 |
btnProperties = new JButton(Messages.getText("properties")); |
134 |
btnProperties.addActionListener(this);
|
135 |
} |
136 |
return btnProperties;
|
137 |
} |
138 |
|
139 |
private LabelClassPreview getLabelPrev() {
|
140 |
if (labelPrev == null) { |
141 |
labelPrev = new LabelClassPreview();
|
142 |
} |
143 |
return labelPrev;
|
144 |
} |
145 |
|
146 |
private JCheckBox getEnableLayerPreview() { |
147 |
if (enableLayerPrev == null) { |
148 |
enableLayerPrev = new JCheckBox( |
149 |
Messages.getText("Enable_layer_preview"));
|
150 |
enableLayerPrev.addItemListener(new ItemListener() { |
151 |
|
152 |
public void itemStateChanged(ItemEvent e) { |
153 |
try {
|
154 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
155 |
layerPrev.setLayer(layer); |
156 |
updatePreview(); |
157 |
} else if (e.getStateChange() == ItemEvent.DESELECTED) { |
158 |
layerPrev.setLayer(null);
|
159 |
} |
160 |
} catch (ReadException e1) {
|
161 |
logger.error("While setting changing layer in preview",
|
162 |
e1); |
163 |
} |
164 |
} |
165 |
}); |
166 |
} |
167 |
return enableLayerPrev;
|
168 |
} |
169 |
|
170 |
private LayerPreview getLayerPreview() {
|
171 |
if (layerPrev == null) { |
172 |
layerPrev = new LayerPreview();
|
173 |
} |
174 |
return layerPrev;
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
protected void initializePanel() { |
179 |
setLayout(new BorderLayout()); |
180 |
add(getScrl(), BorderLayout.CENTER);
|
181 |
scrl.setDividerLocation(500);
|
182 |
getEnableLayerPreview().setSelected(false);
|
183 |
add(getEnableLayerPreview(), BorderLayout.SOUTH);
|
184 |
} |
185 |
|
186 |
private JSplitPane getScrl() { |
187 |
if (scrl == null) { |
188 |
scrl = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); |
189 |
} |
190 |
scrl.add(getLayerPreview(), JSplitPane.LEFT);
|
191 |
scrl.add(getAuxPanel(), JSplitPane.RIGHT);
|
192 |
add(scrl, BorderLayout.CENTER);
|
193 |
return scrl;
|
194 |
} |
195 |
|
196 |
JPanel getAuxPanel(){
|
197 |
if(aux == null){ |
198 |
aux = new JPanel(new BorderLayout()); |
199 |
aux.add(new JBlank(10, 10), BorderLayout.NORTH); |
200 |
aux.add(new JBlank(10, 10), BorderLayout.WEST); |
201 |
aux.add(getLabelPrev(), BorderLayout.CENTER);
|
202 |
aux.add(new JBlank(10, 10), BorderLayout.EAST); |
203 |
aux.add(getAux2Panel(), BorderLayout.SOUTH);
|
204 |
} |
205 |
return aux;
|
206 |
} |
207 |
|
208 |
JPanel getAux2Panel(){
|
209 |
if(aux2 == null){ |
210 |
aux2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10)); |
211 |
aux2.add(getBtnProperties()); |
212 |
} |
213 |
return aux2;
|
214 |
} |
215 |
|
216 |
private LabelClassProperties getLcProp() {
|
217 |
if (lcProp == null) { |
218 |
|
219 |
int n = this.fieldDescs.length; |
220 |
String[] fnames = new String[n]; |
221 |
int[] ftypes = new int[n]; |
222 |
for (int i = 0; i < n; i++) { |
223 |
fnames[i] = fieldDescs[i].getName(); |
224 |
ftypes[i] = fieldDescs[i].getType(); |
225 |
} |
226 |
lcProp = new LabelClassProperties(this.layer.getFeatureStore()); |
227 |
lcProp.setLabelClass(method.getLabelClasses()[0]);
|
228 |
} |
229 |
return lcProp;
|
230 |
|
231 |
} |
232 |
|
233 |
public void actionPerformed(ActionEvent e) { |
234 |
if (e.getSource().equals(getBtnProperties())) {
|
235 |
LabelClassProperties lcProp = getLcProp(); |
236 |
ILabelClass lc = defaultLabel; |
237 |
lcProp.setLabelClass(lc); |
238 |
boolean eval = false; |
239 |
while (!eval) {
|
240 |
|
241 |
ApplicationLocator.getManager().getUIManager() |
242 |
.addWindow(lcProp); |
243 |
if (!lcProp.isAccepted()) {
|
244 |
break;
|
245 |
} |
246 |
; |
247 |
lc = lcProp.getLabelClass(); |
248 |
eval = checkValidSQL(lc); |
249 |
if (eval) {
|
250 |
/*
|
251 |
* Show warning if number of expressions != number of
|
252 |
* rectangles
|
253 |
*/
|
254 |
checkRectangles(lc); |
255 |
} |
256 |
} |
257 |
setLabel(lc); |
258 |
} |
259 |
} |
260 |
|
261 |
/**
|
262 |
* Checks whether the number of expressions euqls the number of rectangles
|
263 |
* in the style
|
264 |
*
|
265 |
* @param lc
|
266 |
* @return
|
267 |
*/
|
268 |
private void checkRectangles(ILabelClass lc) { |
269 |
|
270 |
ILabelStyle lbl_sty = lc.getLabelStyle(); |
271 |
if (lbl_sty != null) { |
272 |
String[] lbl_exps = lc.getLabelExpressions(); |
273 |
Rectangle2D[] lbl_rects = lbl_sty.getTextBounds(); |
274 |
int empty = countEmpty(lbl_exps);
|
275 |
|
276 |
if (lbl_exps != null && lbl_rects != null |
277 |
&& (empty > 0 || (lbl_exps.length != lbl_rects.length))) {
|
278 |
|
279 |
String[] pars = new String[3]; |
280 |
pars[0] = Integer.toString(lbl_exps.length); |
281 |
pars[1] = Integer.toString(empty); |
282 |
pars[2] = Integer.toString(lbl_rects.length); |
283 |
|
284 |
JOptionPane
|
285 |
.showMessageDialog( |
286 |
ApplicationLocator.getManager() |
287 |
.getRootComponent(), |
288 |
Messages.getText( |
289 |
"_There_are_N_expressions_N_are_empty_and_N_rectangles_"
|
290 |
+ "Check_label_expressions_and_background_image_properties",
|
291 |
pars), Messages |
292 |
.getText("label_expression_editor"),
|
293 |
JOptionPane.WARNING_MESSAGE);
|
294 |
|
295 |
} |
296 |
} |
297 |
|
298 |
} |
299 |
|
300 |
private int countEmpty(String[] strs) { |
301 |
if (strs == null || strs.length == 0) { |
302 |
return 0; |
303 |
} else {
|
304 |
int resp = 0; |
305 |
for (int i = 0; i < strs.length; i++) { |
306 |
if (strs[i] == null || strs[i].trim().length() == 0) { |
307 |
resp++; |
308 |
} |
309 |
} |
310 |
return resp;
|
311 |
} |
312 |
} |
313 |
|
314 |
private boolean checkValidSQL(ILabelClass lc) { |
315 |
|
316 |
String sqlQuery = lc.getSQLQuery();
|
317 |
if (sqlQuery != null && sqlQuery.trim().length() > 0) { |
318 |
return LabelClassUtils.validExpression(sqlQuery,
|
319 |
layer.getFeatureStore(), true);
|
320 |
} else {
|
321 |
return true; |
322 |
} |
323 |
|
324 |
} |
325 |
|
326 |
private void setLabel(ILabelClass labelClass) { |
327 |
|
328 |
defaultLabel = labelClass; |
329 |
labelPrev.setLabelClass(defaultLabel); |
330 |
method = newMethodForThePreview(defaultLabel); |
331 |
|
332 |
updatePreview(); |
333 |
|
334 |
} |
335 |
|
336 |
protected ILabelingMethod newMethodForThePreview(ILabelClass defaultLabel) {
|
337 |
|
338 |
ILabelingMethod resp = SymbologyLocator.getSymbologyManager() |
339 |
.createDefaultLabelingMethod(); |
340 |
resp.addLabelClass(defaultLabel); |
341 |
return resp;
|
342 |
} |
343 |
|
344 |
private void updatePreview() { |
345 |
|
346 |
ILabelingStrategy stra = layer.getLabelingStrategy(); |
347 |
if (method == null) { |
348 |
stra.setLabelingMethod(newMethodForThePreview(defaultLabel)); |
349 |
} else {
|
350 |
stra.setLabelingMethod(method); |
351 |
} |
352 |
|
353 |
layer.setIsLabeled(true);
|
354 |
try {
|
355 |
Rectangle r = layerPrev.getBounds();
|
356 |
r.setLocation(layerPrev.getLocationOnScreen()); |
357 |
layerPrev.paintImmediately(r); |
358 |
layerPrev.doLayout(); |
359 |
} catch (IllegalComponentStateException ex) { |
360 |
// this happens when the component is not showing in the
|
361 |
// screen. If that is the case, then we don't need to do
|
362 |
// anything.
|
363 |
} |
364 |
} |
365 |
|
366 |
public void propertyChange(PropertyChangeEvent evt) { |
367 |
|
368 |
String prop = evt.getPropertyName();
|
369 |
ILabelingStrategy strat = layer.getLabelingStrategy(); |
370 |
|
371 |
if (AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS.equals(prop)) {
|
372 |
placementConstraints = (IPlacementConstraints) evt.getNewValue(); |
373 |
strat.setPlacementConstraints(placementConstraints); |
374 |
} else if ((strat instanceof GeneralLabelingStrategy) |
375 |
&& AbstractLabelingMethodPanel.ALLOW_OVERLAP.equals(prop)) { |
376 |
allowOverlap = (Boolean) evt.getNewValue();
|
377 |
((GeneralLabelingStrategy) strat).setAllowOverlapping(allowOverlap); |
378 |
} else if (AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS.equals(prop)) { |
379 |
zoomConstraints = (IZoomConstraints) evt.getNewValue(); |
380 |
strat.setZoomConstraints(zoomConstraints); |
381 |
} |
382 |
|
383 |
updatePreview(); |
384 |
} |
385 |
|
386 |
public void setEnabled(boolean enabled) { |
387 |
super.setEnabled(enabled);
|
388 |
if (layerPrev != null) { |
389 |
layerPrev.setEnabled(enabled); |
390 |
} |
391 |
; |
392 |
if (labelPrev != null) { |
393 |
labelPrev.setEnabled(enabled); |
394 |
} |
395 |
; |
396 |
if (btnProperties != null) { |
397 |
btnProperties.setEnabled(enabled); |
398 |
} |
399 |
; |
400 |
} |
401 |
|
402 |
} |