Revision 42259 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/project/documents/view/legend/gui/AttrInTableLabeling.java

View differences:

AttrInTableLabeling.java
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 23
 */
24
package org.gvsig.app.project.documents.view.legend.gui;
25

  
24
package org.gvsig.app.project.documents.view.legend.gui;

25

  
26 26
import java.awt.*;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.util.ArrayList;
30
import java.util.Iterator;
27
import java.awt.event.ActionEvent;

28
import java.awt.event.ActionListener;

29
import java.util.ArrayList;

30
import java.util.Iterator;

31 31

  
32 32
import javax.swing.*;
33
import javax.swing.JButton;
33
import javax.swing.JButton;

34 34
import javax.swing.border.EmptyBorder;
35 35

  
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.messages.NotificationManager;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.gui.JComboBoxUnits;
40
import org.gvsig.app.gui.panels.ColorChooserPanel;
41
import org.gvsig.app.gui.styling.JComboBoxUnitsReferenceSystem;
42
import org.gvsig.app.gui.utils.FontChooser;
43
import org.gvsig.fmap.dal.DataTypes;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.FeatureType;
47
import org.gvsig.fmap.mapcontext.MapContextLocator;
48
import org.gvsig.fmap.mapcontext.layers.FLayer;
49
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
50
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
36
import org.gvsig.andami.PluginServices;

37
import org.gvsig.andami.messages.NotificationManager;

38
import org.gvsig.app.ApplicationLocator;

39
import org.gvsig.app.gui.JComboBoxUnits;

40
import org.gvsig.app.gui.panels.ColorChooserPanel;

41
import org.gvsig.app.gui.styling.JComboBoxUnitsReferenceSystem;

42
import org.gvsig.app.gui.utils.FontChooser;

43
import org.gvsig.fmap.dal.DataTypes;

44
import org.gvsig.fmap.dal.exception.DataException;

45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;

46
import org.gvsig.fmap.dal.feature.FeatureType;

47
import org.gvsig.fmap.mapcontext.MapContextLocator;

48
import org.gvsig.fmap.mapcontext.layers.FLayer;

49
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;

50
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;

51 51
import org.gvsig.gui.beans.swing.*;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;
53
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.IAttrInTableLabelingStrategy;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;

53
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.IAttrInTableLabelingStrategy;

54 54
import org.gvsig.tools.ToolsLocator;
55 55
import org.gvsig.tools.i18n.I18nManager;
56
import org.gvsig.utils.swing.JComboBox;
57

  
58

  
59
public class AttrInTableLabeling extends JPanel implements  ILabelingStrategyPanel{
60
	private static final long serialVersionUID = 8229927418031917075L;
61
	private static final String NO_FIELD_ITEM = "-- " +
62
			PluginServices.getText(LabelingManager.class, "none") + " --";
63
	private String[] fieldNames;
64
	private String[] numericFieldNames;
65
	private String[] integerFieldNames;
66

  
67
	private JRadioButton rdBtnFixedHeight;
68
	private JRadioButton rdBtnHeightField;
69
	private JRadioButton rdBtnFixedColor;
70
	private JRadioButton rdBtnColorField;
71
	private JComboBox cmbTextField;
72
	private JComboBox cmbHeightField;
73
	private JComboBox cmbRotationField;
74
	private JComboBoxUnits cmbUnits;
75
	private JComboBoxUnitsReferenceSystem cmbReferenceSystem;
76
	private JTextField txtHeightField;
77
	private FLyrVect layer;
78
	private JCheckBox chkItalic;
79

  
80
	private ColorChooserPanel colorChooser;
81
	private JComboBox cmbColorField;
82
	private Font labelFont;
56
import org.gvsig.utils.swing.JComboBox;
57

  
58
public class AttrInTableLabeling extends JPanel implements
59
        ILabelingStrategyPanel {
60

  
61
    private static final long serialVersionUID = 8229927418031917075L;
62

  
63
    private static final String NO_FIELD_ITEM = "-- "
64
            + PluginServices.getText(LabelingManager.class, "none") + " --";
65

  
66
    private String[] fieldNames;
67

  
68
    private String[] numericFieldNames;
69

  
70
    private String[] integerFieldNames;
71

  
72
    private JRadioButton rdBtnFixedHeight;
73

  
74
    private JRadioButton rdBtnHeightField;
75

  
76
    private JRadioButton rdBtnFixedColor;
77

  
78
    private JRadioButton rdBtnColorField;
79

  
80
    private JComboBox cmbTextField;
81

  
82
    private JComboBox cmbHeightField;
83

  
84
    private JComboBox cmbRotationField;
85

  
86
    private JComboBoxUnits cmbUnits;
87

  
88
    private JComboBoxUnitsReferenceSystem cmbReferenceSystem;
89

  
90
    private JTextField txtHeightField;
91

  
92
    private FLyrVect layer;
93

  
94
    private JCheckBox chkItalic;
95

  
96
    private ColorChooserPanel colorChooser;
97

  
98
    private JComboBox cmbColorField;
99

  
100
    private Font labelFont;
101

  
83 102
    private JCheckBox chkBold;
103

  
84 104
    private JComboBoxFonts cmbFontType;
105

  
85 106
    private I18nManager i18nManager = ToolsLocator.getI18nManager();
107

  
86 108
    private JPanel pnlAttributes;
87

  
88
	public AttrInTableLabeling() {
89
		labelFont =
90
				MapContextLocator.getSymbolManager()
91
						.getSymbolPreferences()
92
						.getDefaultSymbolFont();
93
		initialize();
94
	}
95

  
96
	private void initialize() {
97
		setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
98
		GridBagLayoutPanel panel = new GridBagLayoutPanel();
99
		Insets ins = new Insets(3,0,3,0);
100
		
101
		GridBagLayoutPanel aux = new GridBagLayoutPanel();
102
		aux.addComponent(PluginServices.getText(this, "_Field_to_use_in_label") + ":", getCmbTextField(),1,ins);
103
		aux.addComponent(i18nManager.getTranslation("font_type_XcolonX"),getCmbFontType(),1,ins);
104
		aux.addComponent(getRdBtnHeightField(), getCmbHeightField(),1,ins);
105
		aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField(),1,ins);
106
		aux.addComponent(i18nManager.getTranslation("attributes_XcolonX"),getPanelAttributes(),1,ins);
107
		aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField(),1,ins);
108
		aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits(),1,ins);
109
		aux.addComponent(PluginServices.getText(this,"reference_system"),getCmbReferenceSystem(),1,ins);
110
		panel.add(aux);
111
		aux = new GridBagLayoutPanel();
112
		GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
113
		ins = new Insets(5, 5, 5, 5);
114
		aux2.setBorder(BorderFactory.createTitledBorder(null,PluginServices.getText(this,"Color")));
115
		aux2.addComponent(getRdBtnFixedColor(),colorChooser = new ColorChooserPanel(true),1,ins);
116
		aux2.addComponent(getRdBtnColorField(),getCmbColorField(),1,ins);
117
		aux.addComponent(aux2);
118

  
119
		panel.add(new JBlank(20,20));
120
		panel.add(aux);
121

  
122

  
123
		add(panel);
124

  
125

  
126
		ButtonGroup group = new ButtonGroup();
127
		group.add(getRdBtnFixedHeight());
128
		group.add(getRdBtnHeightField());
129

  
130
		ButtonGroup colorGroup = new ButtonGroup();
131
		colorGroup.add(getRdBtnFixedColor());
132
		colorGroup.add(getRdBtnColorField());
133

  
134
//		getRdBtnHeightField().setEnabled(true);
135
	}
136

  
137
	private JPanel getPanelAttributes() {
138
	    if (pnlAttributes == null) {
109

  
110
    public AttrInTableLabeling() {
111
        labelFont = MapContextLocator.getSymbolManager().getSymbolPreferences()
112
                .getDefaultSymbolFont();
113
        initialize();
114
    }
115

  
116
    private void initialize() {
117
        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
118
        GridBagLayoutPanel panel = new GridBagLayoutPanel();
119
        Insets ins = new Insets(3, 0, 3, 0);
120

  
121
        GridBagLayoutPanel aux = new GridBagLayoutPanel();
122
        aux.addComponent(PluginServices.getText(this, "_Field_to_use_in_label")
123
                + ":", getCmbTextField(), GridBagConstraints.BOTH, ins);
124
        aux.addComponent(i18nManager.getTranslation("font_type_XcolonX"),
125
                getCmbFontType(), GridBagConstraints.BOTH, ins);
126
        aux.addComponent(getRdBtnHeightField(), getCmbHeightField(),
127
                GridBagConstraints.BOTH, ins);
128
        aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField(),
129
                GridBagConstraints.BOTH, ins);
130
        aux.addComponent(i18nManager.getTranslation("attributes_XcolonX"),
131
                getPanelAttributes(), GridBagConstraints.BOTH, ins);
132
        aux.addComponent(PluginServices.getText(this, "rotation_height") + ":",
133
                getCmbRotationField(), GridBagConstraints.BOTH, ins);
134
        aux.addComponent(PluginServices.getText(this, "units") + ":",
135
                getCmbUnits(), GridBagConstraints.BOTH, ins);
136
        aux.addComponent(PluginServices.getText(this, "reference_system"),
137
                getCmbReferenceSystem(), GridBagConstraints.BOTH, ins);
138
        panel.add(aux);
139
        aux = new GridBagLayoutPanel();
140
        GridBagLayoutPanel colorPanel = new GridBagLayoutPanel();
141
        ins = new Insets(0, 3, 3, 3);
142
        Insets ins2 = new Insets(3, 3, 0, 3);
143
        colorPanel.setBorder(BorderFactory.createTitledBorder(null,
144
                PluginServices.getText(this, "Color")));
145
        colorChooser = new ColorChooserPanel(true);
146
        colorPanel.addComponent(getRdBtnFixedColor(), colorChooser,
147
                GridBagConstraints.HORIZONTAL, ins2);
148
        colorPanel.addComponent(getRdBtnColorField(), getCmbColorField(),
149
                GridBagConstraints.BOTH, ins);
150
        
151
        aux.addComponent(colorPanel);
152

  
153
        panel.add(new JBlank(20, 0));
154
        panel.add(aux);
155

  
156
        add(panel);
157

  
158
        ButtonGroup group = new ButtonGroup();
159
        group.add(getRdBtnFixedHeight());
160
        group.add(getRdBtnHeightField());
161

  
162
        ButtonGroup colorGroup = new ButtonGroup();
163
        colorGroup.add(getRdBtnFixedColor());
164
        colorGroup.add(getRdBtnColorField());
165
    }
166

  
167
    private JPanel getPanelAttributes() {
168
        if (pnlAttributes == null) {
139 169
            pnlAttributes = new JPanel(new GridBagLayout());
140 170
            GridBagConstraints c = new GridBagConstraints();
141
            
171

  
142 172
            c.fill = GridBagConstraints.BOTH;
143 173
            c.anchor = GridBagConstraints.WEST;
144
            c.insets = new Insets(3,3,3,3);
174
            c.insets = new Insets(3, 3, 3, 3);
145 175
            c.weightx = 1;
146 176
            c.gridx = 0;
147
            c.gridy = 0;            
148
            pnlAttributes.add(getChkBoxBold(),c);
149
            
177
            c.gridy = 0;
178
            pnlAttributes.add(getChkBoxBold(), c);
179

  
150 180
            c.gridx = 1;
151
            pnlAttributes.add(getChkItalic(),c);
152
	    }
181
            pnlAttributes.add(getChkItalic(), c);
182
        }
153 183
        return pnlAttributes;
154 184
    }
155 185

  
......
176 206
        return cmbFontType;
177 207
    }
178 208

  
179
	private JRadioButton getRdBtnFixedHeight() {
180
		if (rdBtnFixedHeight == null) {
181
			rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
182
			rdBtnFixedHeight.setSelected(true);
183
			rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
184
		}
185

  
186
		return rdBtnFixedHeight;
187
	}
188

  
189
	private JRadioButton getRdBtnHeightField() {
190
		if (rdBtnHeightField == null) {
191
			rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
192
			rdBtnHeightField.setSelected(false);
193
			rdBtnHeightField.setName("RDHEIGHTFIELD");
194
		}
195

  
196
		return rdBtnHeightField;
197
	}
198

  
199
	private JRadioButton getRdBtnFixedColor() {
200
		if (rdBtnFixedColor == null) {
201
			rdBtnFixedColor = new JRadioButton(PluginServices.getText(this, "_Fixed_color") + ":");
202
			rdBtnFixedColor.setSelected(true);
203
			rdBtnFixedColor.setName("RDFIXEDCOLOR");
204
		}
205

  
206
		return rdBtnFixedColor;
207
	}
208

  
209
	private JRadioButton getRdBtnColorField() {
210
		if (rdBtnColorField == null) {
211
			rdBtnColorField = new JRadioButton(PluginServices.getText(this, "color_field") + ":");
212
			rdBtnColorField.setSelected(false);
213
			rdBtnColorField.setName("RDCOLORFIELD");
214
		}
215

  
216
		return rdBtnColorField;
217
	}
218

  
219
	private JComboBoxUnits getCmbUnits() {
220
		if (cmbUnits == null) {
221
			cmbUnits = new JComboBoxUnits(true);
222
			cmbUnits.setName("CMBUNITS");
223
		}
224

  
225
		return cmbUnits;
226
	}
227

  
228
	private JComboBoxUnitsReferenceSystem getCmbReferenceSystem(){
229
		if(cmbReferenceSystem == null){
230
			cmbReferenceSystem = new JComboBoxUnitsReferenceSystem();
231
			cmbReferenceSystem.setSelectedIndex(0);
232
			cmbReferenceSystem.setName("CMBREFSYST");
233
		}
234
		return cmbReferenceSystem;
235
	}
209
    private JRadioButton getRdBtnFixedHeight() {
210
        if (rdBtnFixedHeight == null) {
211
            rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this,
212
                    "fixed_height") + ":");
213
            rdBtnFixedHeight.setSelected(true);
214
            rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
215
        }
236 216

  
237
	private JComboBox getCmbColorField() {
238
		if (cmbColorField == null) {
239
			cmbColorField = new JComboBox();
240
			cmbColorField.setName("CMBCOLOR");
241
		}
242

  
243
		return cmbColorField;
244
	}
245

  
246
	private void refreshControls() {
247
		// When the attributes are in the table -----
248
		//      field with the text
249
		refreshCmbTextField();
250

  
251
		//      field with the rotation
252
		refreshCmbRotationField();
253

  
254
		//      field with the text height or the text size
255
		refreshTextHeight();
256

  
257
		//		the text size unit name
258
		refreshCmbUnits();
259

  
260
		refreshCmbRefSystem();
261
		//		the font for the text
262
		refreshFont();
263
		//		the color for the font
264
		refreshColorFont();
265
	}
266

  
267
	private JComboBox getCmbRotationField() {
268
		if (cmbRotationField == null) {
269
			cmbRotationField = new JComboBox();
270
			cmbRotationField.setPreferredSize(new Dimension(200, 20));
271
			cmbRotationField.setName("CMBROTATIONFIELD");
272
		}
273
		return cmbRotationField;
274
	}
275

  
276
	private JComboBox getCmbHeightField() {
277
		if (cmbHeightField == null) {
278
			cmbHeightField = new JComboBox();
279
			cmbHeightField.setPreferredSize(new Dimension(200, 20));
280
			cmbHeightField.setName("CMBHEIGHTFIELD");
281
		}
282
		return cmbHeightField;
283
	}
284

  
285
	private JComboBox getCmbTextField() {
286
		if (cmbTextField == null) {
287
			cmbTextField = new JComboBox();
288
			cmbTextField.setPreferredSize(new Dimension(200, 20));
289
			cmbTextField.setName("CMBTEXTFIELD");
290
		}
291
		return cmbTextField;
292
	}
293

  
294

  
295
	private JTextField getTxtHeightField() {
296
		if (txtHeightField == null) {
297
			txtHeightField = new JTextField(10);
298
			txtHeightField.setText("10");
299
			txtHeightField.setName("TXTHEIGHTFIELD");
300
		}
301

  
302
		return txtHeightField;
303
	}
304

  
305
	public void actionPerformed(ActionEvent e) {
306
		// TODO Auto-generated method stub
307
		throw new Error("Not yet implemented!");
308

  
309
	}
310
	private ColorChooserPanel getColorChooser() {
311
		if (colorChooser == null){
312
			colorChooser = new ColorChooserPanel(true);
313
		}
314
		return colorChooser;
315
	}
316

  
317
	public ILabelingStrategy getLabelingStrategy() {
318
		// user selected to define each label attributes from values
319
		// contained in the table for each feature row.
320

  
321
		double fixedSize;
322
		try {
323
			fixedSize = Double.parseDouble(getTxtHeightField().getText());
324
		} catch (Exception e) {
325
			fixedSize = 10;
326
		}
327
		AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
328
		strategy.setLayer(layer);
329

  
330
		if(getCmbHeightField().getItemCount() > 0 && !rdBtnFixedHeight.isSelected()) {
331
			strategy.setHeightField(
332
				(String) getCmbHeightField().getSelectedItem());
333
		}
334
		if(getCmbRotationField().getItemCount() > 0) {
335
			if(!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM)) {
336
				strategy.setRotationField(
337
						(String) getCmbRotationField().getSelectedItem());
338
			} else {
339
				strategy.setRotationField(null);
340
			}
341
		}
342

  
343
		if(getCmbTextField().getItemCount() > 0) {
344
			strategy.setTextField(
345
				(String) getCmbTextField().getSelectedItem());
346
		}
347

  
348
		strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
349
		strategy.setFixedSize(fixedSize);
350

  
351
		if(getCmbUnits().getItemCount() > 0) {
352
			strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
353
		}
354
		if(getCmbReferenceSystem().getItemCount() > 0) {
355
			strategy.setReferenceSystem(getCmbReferenceSystem().getSelectedIndex());
356
		}
357

  
358
		strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
359
		strategy.setFixedColor(getColorChooser().getColor());
360

  
361
		if(getCmbColorField().getItemCount() > 0 && !rdBtnFixedColor.isSelected()) {
362
			strategy.setColorField((String) getCmbColorField().getSelectedItem());
363
		}
364

  
365

  
366
		strategy.setFont(labelFont);
367
		return strategy;
368
	}
369

  
370
	public void setModel(FLayer layer, ILabelingStrategy str) {
371
		this.layer = (FLyrVect) layer;
372
		// to allow the labeling of non-FLyrVect layers
373
		if (layer instanceof FLyrVect) {
374
			FLyrVect lv = (FLyrVect) layer;
375
			try {
376
				FeatureType featureType = lv.getFeatureStore()
377
						.getDefaultFeatureType();
378
				fieldNames = new String[featureType.size()];
379
				Iterator<FeatureAttributeDescriptor> iterator = featureType
380
						.iterator();
381
				ArrayList<String> l = new ArrayList<String>();
382
				ArrayList<String> lColors = new ArrayList<String>();
383
				String name;
384
				FeatureAttributeDescriptor descriptor;
385
				while (iterator.hasNext()) {
386
					descriptor = iterator.next();
387

  
388
					name = descriptor.getName();
389
					fieldNames[descriptor.getIndex()] = name;
390
					switch (descriptor.getType()) {
391
//					case DataTypes.DECIMAL:
392
//					case DataTypes.NUMERIC:
393
					case DataTypes.FLOAT:
394
//					case DataTypes.REAL:
395
					case DataTypes.DOUBLE:
396
						l.add(name);
397
						break;
398
					case DataTypes.INT:
399
//					case DataTypes.SMALLINT:
400
//					case DataTypes.TINYINT:
401
					case DataTypes.LONG:
402
						lColors.add(name);
403
						l.add(name);
404
						break;
405
					}
406
				}
407
				numericFieldNames = l.toArray(new String[l.size()]);
408
				integerFieldNames = lColors.toArray(new String[lColors.size()]);
409
			} catch (DataException e) {
410
				NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
411
			}
412
			refreshControls();
413
		}
414
	}
415

  
416
	private void refreshColorFont(){
417

  
418
		getCmbColorField().removeAllItems();
419

  
420
		boolean enabled = integerFieldNames.length>0;
421
		getCmbColorField().setEnabled(enabled);
422
		getRdBtnColorField().setEnabled(enabled);
423

  
424
		if (!enabled) {
425
			getRdBtnFixedColor().setSelected(true);
426
		}
427

  
428
		for (int i = 0; i < integerFieldNames.length; i++) {
429
			getCmbColorField().addItem(integerFieldNames[i]);
430
		}
431

  
432
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
433
			IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
434
			getRdBtnFixedColor().setSelected(aux.usesFixedColor());
435
			getRdBtnColorField().setSelected(!aux.usesFixedColor());
436

  
437
			String item = aux.getColorField();
438
			getCmbColorField().setSelectedItem(item);
439
			getColorChooser().setColor(aux.getFixedColor());
440
		}
441
	}
442

  
443
	private void refreshFont(){
444

  
445
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
446
			IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
447
			labelFont = aux.getFont();
448
		}
449
	}
450

  
451
	private void refreshCmbUnits() {
452

  
453
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
454
			AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
455
			getCmbUnits().setSelectedUnitIndex(aux.getUnit());
456
		}
457
	}
458

  
459
	private void refreshCmbRefSystem() {
217
        return rdBtnFixedHeight;
218
    }
460 219

  
461
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
462
			AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
463
			getCmbReferenceSystem().setSelectedIndex(aux.getReferenceSystem());
464
		}
465
	}
466

  
467
	private void refreshTextHeight() {
468
		getCmbHeightField().removeAllItems();
469

  
470
		/*
471
		boolean enabled = numericFieldNames.length>0;
472
	    // getCmbHeightField().setEnabled(enabled);
473
	    // getRdBtnHeightField().setEnabled(enabled);
474

  
475
		if (!enabled) {
476
			getRdBtnFixedHeight().setSelected(true);
477
		}
478
		*/
479

  
480
		for (int i = 0; i < numericFieldNames.length; i++) {
481
			getCmbHeightField().addItem(numericFieldNames[i]);
482
		}
483

  
484
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
485
			IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
486
			getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
487
			getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
488
			getRdBtnHeightField().setSelected(!aux.usesFixedSize());
489

  
490
			String item = aux.getHeightField();
491
			getCmbHeightField().setSelectedItem(item);
492

  
493
		}
494

  
495
	}
496

  
497
	private void refreshCmbRotationField() {
498
		getCmbRotationField().removeAllItems();
499
		getCmbRotationField().addItem(NO_FIELD_ITEM);
500
		for (int i = 0; i < numericFieldNames.length; i++) {
501
			getCmbRotationField().addItem(numericFieldNames[i]);
502
		}
503

  
504
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
505
			IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
506
			String item = aux.getRotationField();
507
			getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
508
		}
509
	}
510

  
511
	private void refreshCmbTextField() {
512
		getCmbTextField().removeAllItems();
513
		for (int i = 0; i < fieldNames.length; i++) {
514
			getCmbTextField().addItem(fieldNames[i]);
515
		}
516

  
517
		if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
518
			IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
519
			String item = aux.getTextField();
520
			getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
521
		}
522
	}
523

  
524
	public String getLabelingStrategyName() {
525
		return PluginServices.getText(this, "label_attributes_defined_in_table");
526
	}
527

  
528
	public Class getLabelingStrategyClass() {
529
		return AttrInTableLabelingStrategy.class;
530
	}
531

  
532
	public void setEnabled(boolean enabled) {
533
		super.setEnabled(enabled);
534
//		getChooseFontBut().setEnabled(enabled);
535
		getCmbColorField().setEnabled(enabled);
536
		getCmbHeightField().setEnabled(enabled);
537
		getCmbReferenceSystem().setEnabled(enabled);
538
		getCmbRotationField().setEnabled(enabled);
539
		getCmbTextField().setEnabled(enabled);
540
		getCmbUnits().setEnabled(enabled);
541
		getColorChooser().setEnabled(enabled);
542
		getRdBtnColorField().setEnabled(enabled);
543
		getRdBtnFixedColor().setEnabled(enabled);
544
		getRdBtnFixedHeight().setEnabled(enabled);
545
		getRdBtnHeightField().setEnabled(enabled);
546
		getTxtHeightField().setEnabled(enabled);
547
		getCmbFontType().setEnabled(enabled);
548
		getChkBoxBold().setEnabled(enabled);
549
		getChkItalic().setEnabled(enabled);
550
	}
551
	
552
	
553
	
220
    private JRadioButton getRdBtnHeightField() {
221
        if (rdBtnHeightField == null) {
222
            rdBtnHeightField = new JRadioButton(PluginServices.getText(this,
223
                    "text_height_field") + ":");
224
            rdBtnHeightField.setSelected(false);
225
            rdBtnHeightField.setName("RDHEIGHTFIELD");
226
        }
554 227

  
228
        return rdBtnHeightField;
229
    }
230

  
231
    private JRadioButton getRdBtnFixedColor() {
232
        if (rdBtnFixedColor == null) {
233
            rdBtnFixedColor = new JRadioButton(PluginServices.getText(this,
234
                    "_Fixed_color") + ":");
235
            rdBtnFixedColor.setSelected(true);
236
            rdBtnFixedColor.setName("RDFIXEDCOLOR");
237
        }
238

  
239
        return rdBtnFixedColor;
240
    }
241

  
242
    private JRadioButton getRdBtnColorField() {
243
        if (rdBtnColorField == null) {
244
            rdBtnColorField = new JRadioButton(PluginServices.getText(this,
245
                    "color_field") + ":");
246
            rdBtnColorField.setSelected(false);
247
            rdBtnColorField.setName("RDCOLORFIELD");
248
        }
249

  
250
        return rdBtnColorField;
251
    }
252

  
253
    private JComboBoxUnits getCmbUnits() {
254
        if (cmbUnits == null) {
255
            cmbUnits = new JComboBoxUnits(true);
256
            cmbUnits.setName("CMBUNITS");
257
        }
258

  
259
        return cmbUnits;
260
    }
261

  
262
    private JComboBoxUnitsReferenceSystem getCmbReferenceSystem() {
263
        if (cmbReferenceSystem == null) {
264
            cmbReferenceSystem = new JComboBoxUnitsReferenceSystem();
265
            cmbReferenceSystem.setSelectedIndex(0);
266
            cmbReferenceSystem.setName("CMBREFSYST");
267
        }
268
        return cmbReferenceSystem;
269
    }
270

  
271
    private JComboBox getCmbColorField() {
272
        if (cmbColorField == null) {
273
            cmbColorField = new JComboBox();
274
            cmbColorField.setName("CMBCOLOR");
275
        }
276

  
277
        return cmbColorField;
278
    }
279

  
280
    private void refreshControls() {
281
        // When the attributes are in the table -----
282
        // field with the text
283
        refreshCmbTextField();
284

  
285
        // field with the rotation
286
        refreshCmbRotationField();
287

  
288
        // field with the text height or the text size
289
        refreshTextHeight();
290

  
291
        // the text size unit name
292
        refreshCmbUnits();
293

  
294
        refreshCmbRefSystem();
295
        // the font for the text
296
        refreshFont();
297
        // the color for the font
298
        refreshColorFont();
299
    }
300

  
301
    private JComboBox getCmbRotationField() {
302
        if (cmbRotationField == null) {
303
            cmbRotationField = new JComboBox();
304
            cmbRotationField.setPreferredSize(new Dimension(200, 20));
305
            cmbRotationField.setName("CMBROTATIONFIELD");
306
        }
307
        return cmbRotationField;
308
    }
309

  
310
    private JComboBox getCmbHeightField() {
311
        if (cmbHeightField == null) {
312
            cmbHeightField = new JComboBox();
313
            cmbHeightField.setPreferredSize(new Dimension(200, 20));
314
            cmbHeightField.setName("CMBHEIGHTFIELD");
315
        }
316
        return cmbHeightField;
317
    }
318

  
319
    private JComboBox getCmbTextField() {
320
        if (cmbTextField == null) {
321
            cmbTextField = new JComboBox();
322
            cmbTextField.setPreferredSize(new Dimension(200, 20));
323
            cmbTextField.setName("CMBTEXTFIELD");
324
        }
325
        return cmbTextField;
326
    }
327

  
328
    private JTextField getTxtHeightField() {
329
        if (txtHeightField == null) {
330
            txtHeightField = new JTextField(10);
331
            txtHeightField.setText("10");
332
            txtHeightField.setName("TXTHEIGHTFIELD");
333
        }
334

  
335
        return txtHeightField;
336
    }
337

  
338
    public void actionPerformed(ActionEvent e) {
339
        // TODO Auto-generated method stub
340
        throw new Error("Not yet implemented!");
341

  
342
    }
343

  
344
    private ColorChooserPanel getColorChooser() {
345
        if (colorChooser == null) {
346
            colorChooser = new ColorChooserPanel(true);
347
        }
348
        return colorChooser;
349
    }
350

  
351
    public ILabelingStrategy getLabelingStrategy() {
352
        // user selected to define each label attributes from values
353
        // contained in the table for each feature row.
354

  
355
        double fixedSize;
356
        try {
357
            fixedSize = Double.parseDouble(getTxtHeightField().getText());
358
        }
359
        catch (Exception e) {
360
            fixedSize = 10;
361
        }
362
        AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
363
        strategy.setLayer(layer);
364

  
365
        if (getCmbHeightField().getItemCount() > 0
366
                && !rdBtnFixedHeight.isSelected()) {
367
            strategy.setHeightField((String) getCmbHeightField()
368
                    .getSelectedItem());
369
        }
370
        if (getCmbRotationField().getItemCount() > 0) {
371
            if (!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM)) {
372
                strategy.setRotationField((String) getCmbRotationField()
373
                        .getSelectedItem());
374
            }
375
            else {
376
                strategy.setRotationField(null);
377
            }
378
        }
379

  
380
        if (getCmbTextField().getItemCount() > 0) {
381
            strategy.setTextField((String) getCmbTextField().getSelectedItem());
382
        }
383

  
384
        strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
385
        strategy.setFixedSize(fixedSize);
386

  
387
        if (getCmbUnits().getItemCount() > 0) {
388
            strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
389
        }
390
        if (getCmbReferenceSystem().getItemCount() > 0) {
391
            strategy.setReferenceSystem(getCmbReferenceSystem()
392
                    .getSelectedIndex());
393
        }
394

  
395
        strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
396
        strategy.setFixedColor(getColorChooser().getColor());
397

  
398
        if (getCmbColorField().getItemCount() > 0
399
                && !rdBtnFixedColor.isSelected()) {
400
            strategy.setColorField((String) getCmbColorField()
401
                    .getSelectedItem());
402
        }
403

  
404
        strategy.setFont(labelFont);
405
        return strategy;
406
    }
407

  
408
    public void setModel(FLayer layer, ILabelingStrategy str) {
409
        this.layer = (FLyrVect) layer;
410
        // to allow the labeling of non-FLyrVect layers
411
        if (layer instanceof FLyrVect) {
412
            FLyrVect lv = (FLyrVect) layer;
413
            try {
414
                FeatureType featureType = lv.getFeatureStore()
415
                        .getDefaultFeatureType();
416
                fieldNames = new String[featureType.size()];
417
                Iterator<FeatureAttributeDescriptor> iterator = featureType
418
                        .iterator();
419
                ArrayList<String> l = new ArrayList<String>();
420
                ArrayList<String> lColors = new ArrayList<String>();
421
                String name;
422
                FeatureAttributeDescriptor descriptor;
423
                while (iterator.hasNext()) {
424
                    descriptor = iterator.next();
425

  
426
                    name = descriptor.getName();
427
                    fieldNames[descriptor.getIndex()] = name;
428
                    switch (descriptor.getType()) {
429
                    // case DataTypes.DECIMAL:
430
                    // case DataTypes.NUMERIC:
431
                    case DataTypes.FLOAT:
432
                        // case DataTypes.REAL:
433
                    case DataTypes.DOUBLE:
434
                        l.add(name);
435
                        break;
436
                    case DataTypes.INT:
437
                        // case DataTypes.SMALLINT:
438
                        // case DataTypes.TINYINT:
439
                    case DataTypes.LONG:
440
                        lColors.add(name);
441
                        l.add(name);
442
                        break;
443
                    }
444
                }
445
                numericFieldNames = l.toArray(new String[l.size()]);
446
                integerFieldNames = lColors.toArray(new String[lColors.size()]);
447
            }
448
            catch (DataException e) {
449
                NotificationManager.addError(PluginServices.getText(this,
450
                        "accessing_file_structure"), e);
451
            }
452
            refreshControls();
453
        }
454
    }
455

  
456
    private void refreshColorFont() {
457

  
458
        getCmbColorField().removeAllItems();
459

  
460
        boolean enabled = integerFieldNames.length > 0;
461
        getCmbColorField().setEnabled(enabled);
462
        getRdBtnColorField().setEnabled(enabled);
463

  
464
        if (!enabled) {
465
            getRdBtnFixedColor().setSelected(true);
466
        }
467

  
468
        for (int i = 0; i < integerFieldNames.length; i++) {
469
            getCmbColorField().addItem(integerFieldNames[i]);
470
        }
471

  
472
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
473
            IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer
474
                    .getLabelingStrategy();
475
            getRdBtnFixedColor().setSelected(aux.usesFixedColor());
476
            getRdBtnColorField().setSelected(!aux.usesFixedColor());
477

  
478
            String item = aux.getColorField();
479
            getCmbColorField().setSelectedItem(item);
480
            getColorChooser().setColor(aux.getFixedColor());
481
        }
482
    }
483

  
484
    private void refreshFont() {
485

  
486
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
487
            IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer
488
                    .getLabelingStrategy();
489
            labelFont = aux.getFont();
490
        }
491
    }
492

  
493
    private void refreshCmbUnits() {
494

  
495
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
496
            AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer
497
                    .getLabelingStrategy();
498
            getCmbUnits().setSelectedUnitIndex(aux.getUnit());
499
        }
500
    }
501

  
502
    private void refreshCmbRefSystem() {
503

  
504
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
505
            AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer
506
                    .getLabelingStrategy();
507
            getCmbReferenceSystem().setSelectedIndex(aux.getReferenceSystem());
508
        }
509
    }
510

  
511
    private void refreshTextHeight() {
512
        getCmbHeightField().removeAllItems();
513

  
514
        /*
515
         * boolean enabled = numericFieldNames.length>0; //
516
         * getCmbHeightField().setEnabled(enabled); //
517
         * getRdBtnHeightField().setEnabled(enabled); if (!enabled) {
518
         * getRdBtnFixedHeight().setSelected(true); }
519
         */
520

  
521
        for (int i = 0; i < numericFieldNames.length; i++) {
522
            getCmbHeightField().addItem(numericFieldNames[i]);
523
        }
524

  
525
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
526
            IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer
527
                    .getLabelingStrategy();
528
            getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
529
            getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
530
            getRdBtnHeightField().setSelected(!aux.usesFixedSize());
531

  
532
            String item = aux.getHeightField();
533
            getCmbHeightField().setSelectedItem(item);
534

  
535
        }
536

  
537
    }
538

  
539
    private void refreshCmbRotationField() {
540
        getCmbRotationField().removeAllItems();
541
        getCmbRotationField().addItem(NO_FIELD_ITEM);
542
        for (int i = 0; i < numericFieldNames.length; i++) {
543
            getCmbRotationField().addItem(numericFieldNames[i]);
544
        }
545

  
546
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
547
            IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer
548
                    .getLabelingStrategy();
549
            String item = aux.getRotationField();
550
            getCmbRotationField().setSelectedItem(
551
                    item != null ? item : NO_FIELD_ITEM);
552
        }
553
    }
554

  
555
    private void refreshCmbTextField() {
556
        getCmbTextField().removeAllItems();
557
        for (int i = 0; i < fieldNames.length; i++) {
558
            getCmbTextField().addItem(fieldNames[i]);
559
        }
560

  
561
        if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
562
            IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer
563
                    .getLabelingStrategy();
564
            String item = aux.getTextField();
565
            getCmbTextField().setSelectedItem(
566
                    item != null ? item : NO_FIELD_ITEM);
567
        }
568
    }
569

  
570
    public String getLabelingStrategyName() {
571
        return PluginServices
572
                .getText(this, "label_attributes_defined_in_table");
573
    }
574

  
575
    public Class getLabelingStrategyClass() {
576
        return AttrInTableLabelingStrategy.class;
577
    }
578

  
579
    public void setEnabled(boolean enabled) {
580
        super.setEnabled(enabled);
581
        // getChooseFontBut().setEnabled(enabled);
582
        getCmbColorField().setEnabled(enabled);
583
        getCmbHeightField().setEnabled(enabled);
584
        getCmbReferenceSystem().setEnabled(enabled);
585
        getCmbRotationField().setEnabled(enabled);
586
        getCmbTextField().setEnabled(enabled);
587
        getCmbUnits().setEnabled(enabled);
588
        getColorChooser().setEnabled(enabled);
589
        getRdBtnColorField().setEnabled(enabled);
590
        getRdBtnFixedColor().setEnabled(enabled);
591
        getRdBtnFixedHeight().setEnabled(enabled);
592
        getRdBtnHeightField().setEnabled(enabled);
593
        getTxtHeightField().setEnabled(enabled);
594
        getCmbFontType().setEnabled(enabled);
595
        getChkBoxBold().setEnabled(enabled);
596
        getChkItalic().setEnabled(enabled);
597
    }
598

  
555 599
    /**
556 600
     * Returns the selected style.
557 601
     *
......
567 611
        }
568 612
        return result;
569 613
    }
570
    
571
    
614

  
572 615
    /**
573 616
     * Returns a Font object representing the selection in the panel.
574 617
     *
575 618
     * @return the font.
576 619
     */
577 620
    public Font getSelectedFont() {
578
        return new Font((String)getCmbFontType().getSelectedItem(), getSelectedStyle(), Integer.parseInt(getTxtHeightField().getText()));
579
    }
580
}
621
        return new Font((String) getCmbFontType().getSelectedItem(),
622
                getSelectedStyle(), Integer.parseInt(getTxtHeightField()
623
                        .getText()));
624
    }
625
}

Also available in: Unified diff