Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / LabelClassRenderingProperties.java @ 20905

History | View | Annotate | Download (11.4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.symbology.gui.styling;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.Font;
48
import java.awt.GridLayout;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.util.ArrayList;
52

    
53
import javax.swing.BorderFactory;
54
import javax.swing.JLabel;
55
import javax.swing.JOptionPane;
56
import javax.swing.JPanel;
57
import javax.swing.JTextField;
58

    
59
import org.gvsig.gui.beans.AcceptCancelPanel;
60
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
61
import org.gvsig.gui.beans.swing.JButton;
62

    
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.messages.NotificationManager;
65
import com.iver.andami.ui.mdiManager.IWindow;
66
import com.iver.andami.ui.mdiManager.WindowInfo;
67
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
68
import com.iver.cit.gvsig.fmap.core.FShape;
69
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
70
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
71
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
72
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass;
73
import com.iver.cit.gvsig.gui.styling.SelectorFilter;
74
import com.iver.cit.gvsig.gui.styling.StyleSelector;
75
import com.iver.cit.gvsig.gui.styling.SymbolPreviewer;
76
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
77
import com.iver.cit.gvsig.gui.utils.FontChooser;
78
import com.iver.cit.gvsig.project.documents.view.legend.gui.ISymbolSelector;
79
import com.iver.utiles.swing.JComboBox;
80

    
81
/**
82
 * 
83
 * LabelClassRenderingProperties.java
84
 *
85
 * 
86
 * @author jaume dominguez faus - jaume.dominguez@iver.es Feb 29, 2008
87
 *
88
 */
89
public class LabelClassRenderingProperties extends GridBagLayoutPanel implements ActionListener{
90
        private SymbolPreviewer previewer;
91
        private JComboBox cmbExpressions;
92
        private JButton btnFont;
93
        private JButton btnSymbol;
94
        private JButton btnLabelStyles;
95
        private JButton btnEditExpression;
96
        private LabelClass lc;
97
        private ArrayList<String> expressions;
98
//        private ILabelingMethod method;
99
        private String[] fieldNames;
100
        private boolean noEvent = false;
101
        
102
        private class ExprEditorPanel extends JPanel implements IWindow {
103
                private static final long serialVersionUID = -3867224882591879900L;
104
                private ActionListener action = new ActionListener() {
105
                        public void actionPerformed(ActionEvent e) {
106
                                if ("OK".equals(e.getActionCommand())) {
107
                                        if (validateExpr())
108
                                                PluginServices.getMDIManager().closeWindow(
109
                                                                ExprEditorPanel.this);
110
                                        else
111
                                                JOptionPane.showMessageDialog(ExprEditorPanel.this,
112
                                                                PluginServices.getText(this, "syntax_error"),
113
                                                                PluginServices.getText(this, "error"),
114
                                                                JOptionPane.ERROR_MESSAGE);
115
                                } else {
116
                                        getTxtExpression().setText(lastExtression);
117
                                        PluginServices.getMDIManager().closeWindow(
118
                                                        ExprEditorPanel.this);
119
                                }
120
                        }
121
                };
122
                private JTextField txtExpression;
123
                private String lastExtression;
124

    
125
                public ExprEditorPanel(String currentExpression) {
126
                        super();
127
                        initialize();
128
                        lastExtression = currentExpression;
129
                        getTxtExpression().setText(currentExpression);
130

    
131
                }
132

    
133
                private void initialize() {
134
                        GridBagLayoutPanel content = new GridBagLayoutPanel();
135
                        content.addComponent(new JLabel(" "+PluginServices.getText(this, "label_expression_help")));
136
                        content.addComponent(
137
                                        " "+PluginServices.getText(this, "expression")+":",
138
                                        getTxtExpression());
139
                        setLayout(new BorderLayout(5, 5));
140

    
141
                        add(content, BorderLayout.CENTER);
142
                        add(new AcceptCancelPanel(action, action), BorderLayout.SOUTH);
143
                        setSize(new Dimension(300, 80));
144
                }
145

    
146
                private JTextField getTxtExpression() {
147
                        if (txtExpression == null) {
148
                                txtExpression = new JTextField(25);
149
                        }
150
                        return txtExpression;
151
                }
152

    
153
                public String getExpression() {
154
                        return getTxtExpression().getText();
155
                }
156

    
157
                public WindowInfo getWindowInfo() {
158
                        WindowInfo viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
159
                        viewInfo.setWidth(getWidth());
160
                        viewInfo.setHeight(getHeight());
161
                        viewInfo.setTitle(PluginServices.getText(this,"label_expression_editor"));
162
                        return viewInfo;
163
                }
164

    
165
                private boolean validateExpr() {
166
                        // TODO : implement it
167
                        return true;
168
                }
169
        }
170
        
171
        public LabelClassRenderingProperties() {
172
                initialize();
173
        }
174
        
175
        private void initialize() {
176
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
177
                aux.setBorder(BorderFactory.createTitledBorder(
178
                                null, PluginServices.getText(this, "text_string")));
179
                aux.add(new JLabel(PluginServices.getText(this, "expression")+":"));
180
                aux.add(getCmbExpressions());
181
                aux.add(getBtnEditExpression());
182
                aux.setPreferredSize(new Dimension(612, 60));
183
                addComponent(aux);
184

    
185

    
186
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
187
                aux.setBorder(BorderFactory.createTitledBorder(
188
                                null, PluginServices.getText(this, "text_symbol")));
189
                aux.add(getSymbolPreviewer());
190
                
191
                JPanel aux2 = new JPanel();
192
                aux2.add(getBtnFont());
193
                aux2.add(getBtnSymbol());
194
                aux2.add(getBtnLabelStyles());
195

    
196
                aux2.setLayout(new GridLayout(aux2.getComponentCount(), 1));
197
                aux.add(aux2);
198
                aux.setPreferredSize(new Dimension(612, 100));
199
                addComponent(aux);
200
        }
201
        
202
        
203
        public void setFieldNames(String[] fieldNames) {
204
                this.fieldNames = fieldNames;
205
        }
206

    
207
        private void refreshCmbExpressions() {
208

    
209
                ArrayList<String> exp = getExpressions();
210

    
211
                noEvent = true;
212
                if ( lc != null && 
213
                                lc.getLabelExpression()!=null && 
214
                                !lc.getLabelExpression().equals("") && 
215
                                !exp.contains(lc.getLabelExpression())) {
216
                        exp.add(0, lc.getLabelExpression());
217
                }
218

    
219
                getCmbExpressions().removeAllItems();
220
                for (int i = 0; i < exp.size(); i++) {
221
                        getCmbExpressions().addItem(exp.get(i));
222
                }
223
                if (lc != null) {
224
                        getCmbExpressions().setSelectedItem(lc.getLabelExpression());
225
                } else {
226
                        getCmbExpressions().setSelectedItem(exp.get(0));
227
                }
228
                noEvent  = false;
229
        }
230
        
231
        private JButton getBtnEditExpression() {
232
                if (btnEditExpression == null) {
233
                        btnEditExpression = new JButton(
234
                                        PluginServices.getText(this, "edit_expression")+"...");
235
                        btnEditExpression.setName("BTNEDITEXPRESSION");
236
                        btnEditExpression.addActionListener(this);
237
                }
238
                return btnEditExpression;
239
        }
240

    
241
        private JButton getBtnSymbol() {
242
                if (btnSymbol == null) {
243
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
244
                        btnSymbol.setName("BTNSYMBOL");
245
                        btnSymbol.addActionListener(this);
246
                }
247

    
248
                return btnSymbol;
249
        }
250
        
251
        private ArrayList<String> getExpressions() {
252
                if (expressions == null) {
253
                        expressions = new ArrayList<String>();
254
                        try {
255
                                for (int i = 0; i < fieldNames.length; i++) {
256
                                        expressions.add("["+fieldNames[i]+"];");
257
                                }
258
                        } catch (Exception e) {
259
                                NotificationManager.addError(
260
                                                PluginServices.getText(
261
                                                                this, "could_not_retreive_layer_field_names"), e);
262
                        }
263
                }
264
                return expressions;
265
        }
266
        private JComboBox getCmbExpressions() {
267
                if (cmbExpressions == null) {
268
                        cmbExpressions = new JComboBox();
269
                        cmbExpressions.setPreferredSize(new Dimension(150, 20));
270
                        cmbExpressions.setName("CMBEXPRESSIONS");
271
                        cmbExpressions.addActionListener(this);
272
                }
273
                return cmbExpressions;
274

    
275
        }
276

    
277
        private JButton getBtnFont() {
278
                if (btnFont == null) {
279
                        btnFont = new JButton(
280
                                        PluginServices.getText(this,"fuente")+"...");
281
                        btnFont.setName("BTNFONT");
282
                        btnFont.addActionListener(this);
283
                }
284
                return btnFont;
285
        }
286
        
287
        private SymbolPreviewer getSymbolPreviewer() {
288
                if (previewer == null) {
289
                        previewer = new SymbolPreviewer();
290
                        previewer.setPreferredSize(new Dimension(420, 65));
291
                }
292
                return previewer;
293
        }
294
        
295
        
296
        public void setModel(LabelClass lc) {
297
                this.lc = lc;
298
                refreshCmbExpressions();
299
                refreshTextSymbolPreviewer();
300
        }
301
        
302
        public void actionPerformed(ActionEvent e) {
303
                if (noEvent) return;
304
                Component c = (Component) e.getSource();
305
                if (c.equals(btnEditExpression)) {
306
                        ExprEditorPanel ep = new ExprEditorPanel((String) getCmbExpressions().
307
                                        getSelectedItem());
308
                        PluginServices.getMDIManager().addWindow( ep );
309
                        getCmbExpressions().addItem(ep.getExpression());
310
                        getCmbExpressions().setSelectedItem(ep.getExpression());
311
                        lc.setLabelExpression(ep.getExpression());
312
                } else if (c.equals(btnFont)) {
313
                        Font labelFont = lc.getTextSymbol().getFont();
314
                        Font newFont = FontChooser.showDialog(PluginServices.getText(this, "font"), labelFont);
315
                        if (newFont != null) {
316
                                lc.getTextSymbol().setFont(newFont);
317
                        }
318
                        refreshTextSymbolPreviewer();
319
                } else if (c.equals(btnLabelStyles)) {
320
                        // here open symbol selector
321

    
322
                        StyleSelector stySel = new StyleSelector(
323
                                        lc.getLabelStyle(),
324
                                        FShape.TEXT );
325
                        stySel.setUnit(lc.getUnit());
326
                        stySel.setReferenceSystem(lc.getReferenceSystem());
327
                        PluginServices.getMDIManager().addWindow(stySel);
328
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
329
                        if (sty != null) {
330
                                // gather the style and apply to the class
331
                                lc.setLabelStyle(sty);
332
                                lc.setUnit(stySel.getUnit());
333
                                lc.setReferenceSystem(stySel.getReferenceSystem());
334

    
335
                        }
336
                } else if (c.equals(cmbExpressions)) {
337
                        lc.setLabelExpression((String) cmbExpressions.getSelectedItem());
338
                } if (c.equals(btnSymbol)) {
339
                        
340
                        // here open symbol selector
341

    
342
                        ISymbolSelector symSel = SymbolSelector.createSymbolSelector(lc.getTextSymbol(),
343
                                        FShape.TEXT,
344
                                        new SelectorFilter() {
345
                                                public boolean accepts(Object obj) {
346
                                                        if (obj instanceof ISymbol) {
347
                                                                ISymbol sym = (ISymbol) obj;
348
                                                                return sym.getSymbolType() == FShape.TEXT;
349
                                                        }
350
                                                        return false;
351
                                                }
352
                                        });
353
                        
354
                        PluginServices.getMDIManager().addWindow(symSel);
355
                        ISymbol sym = (ISymbol) symSel.getSelectedObject();
356
                        if (sym != null) {
357
                                // gather the symbol and apply to the class
358
                                if (sym instanceof CartographicSupport) {
359
                                        CartographicSupport csSty = (CartographicSupport) sym;
360
                                        lc.setUnit(csSty.getUnit());
361
                                        lc.setReferenceSystem(csSty.getReferenceSystem());
362
                                }
363
                                lc.setTextSymbol((ITextSymbol) sym);
364
                                
365
                        }
366
                        
367
                        refreshTextSymbolPreviewer();
368
                } 
369
        }
370

    
371
        private JButton getBtnLabelStyles() {
372
                if (btnLabelStyles == null) {
373
                        btnLabelStyles = new JButton(
374
                                        PluginServices.getText(this, "label_styles")+"...");
375
                        btnLabelStyles.setName("BTNLABELSTYLES");
376
                        btnLabelStyles.addActionListener(this);
377
                }
378
                return btnLabelStyles;
379
        }
380

    
381
        private void refreshTextSymbolPreviewer() {
382
                ITextSymbol textSym = lc.getTextSymbol();
383
                textSym.setText(PluginServices.getText(this, "GeneralLabeling.sample_text"));
384
                getSymbolPreviewer().setSymbol(textSym);
385
        
386
        }
387

    
388
}