Statistics
| Revision:

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

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 static final long serialVersionUID = 2232555304188584038L;
91
        private SymbolPreviewer previewer;
92
        private JComboBox cmbExpressions;
93
        private JButton btnFont;
94
        private JButton btnSymbol;
95
        private JButton btnLabelStyles;
96
        private JButton btnEditExpression;
97
        private LabelClass lc;
98
        private ArrayList<String> expressions;
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
                public Object getWindowProfile() {
166
                        return WindowInfo.DIALOG_PROFILE;
167
                }
168

    
169
                private boolean validateExpr() {
170
                        // TODO : implement it
171
                        return true;
172
                }
173
        }
174

    
175
        public LabelClassRenderingProperties() {
176
                initialize();
177
        }
178

    
179
        private void initialize() {
180
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
181
                aux.setBorder(BorderFactory.createTitledBorder(
182
                                null, PluginServices.getText(this, "text_string")));
183
                aux.add(new JLabel(PluginServices.getText(this, "expression")+":"));
184
                aux.add(getCmbExpressions());
185
                aux.add(getBtnEditExpression());
186
                aux.setPreferredSize(new Dimension(612, 60));
187
                addComponent(aux);
188

    
189

    
190
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
191
                aux.setBorder(BorderFactory.createTitledBorder(
192
                                null, PluginServices.getText(this, "text_symbol")));
193
                aux.add(getSymbolPreviewer());
194

    
195
                JPanel aux2 = new JPanel();
196
                aux2.add(getBtnFont());
197
                aux2.add(getBtnSymbol());
198
                aux2.add(getBtnLabelStyles());
199

    
200
                aux2.setLayout(new GridLayout(aux2.getComponentCount(), 1));
201
                aux.add(aux2);
202
                aux.setPreferredSize(new Dimension(612, 100));
203
                addComponent(aux);
204
        }
205

    
206

    
207
        public void setFieldNames(String[] fieldNames) {
208
                this.fieldNames = fieldNames;
209
        }
210

    
211
        private void refreshCmbExpressions() {
212

    
213
                ArrayList<String> exp = getExpressions();
214

    
215
                String expr = lc.getStringLabelExpression();
216

    
217
                noEvent = true;
218
                if ( lc != null &&
219
                                expr !=null &&
220
                                !expr.equals("") &&
221
                                !exp.contains(expr)) {
222
                        exp.add(0, expr);
223
                }
224

    
225
                getCmbExpressions().removeAllItems();
226
                for (int i = 0; i < exp.size(); i++) {
227
                        getCmbExpressions().addItem(exp.get(i));
228
                }
229
                if (lc != null) {
230
                        getCmbExpressions().setSelectedItem(lc.getLabelExpressions());
231
                } else {
232
                        getCmbExpressions().setSelectedItem(exp.get(0));
233
                }
234
                noEvent  = false;
235
        }
236

    
237
        private JButton getBtnEditExpression() {
238
                if (btnEditExpression == null) {
239
                        btnEditExpression = new JButton(
240
                                        PluginServices.getText(this, "edit_expression")+"...");
241
                        btnEditExpression.setName("BTNEDITEXPRESSION");
242
                        btnEditExpression.addActionListener(this);
243
                }
244
                return btnEditExpression;
245
        }
246

    
247
        private JButton getBtnSymbol() {
248
                if (btnSymbol == null) {
249
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
250
                        btnSymbol.setName("BTNSYMBOL");
251
                        btnSymbol.addActionListener(this);
252
                }
253

    
254
                return btnSymbol;
255
        }
256

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

    
281
        }
282

    
283
        private JButton getBtnFont() {
284
                if (btnFont == null) {
285
                        btnFont = new JButton(
286
                                        PluginServices.getText(this,"fuente")+"...");
287
                        btnFont.setName("BTNFONT");
288
                        btnFont.addActionListener(this);
289
                }
290
                return btnFont;
291
        }
292

    
293
        private SymbolPreviewer getSymbolPreviewer() {
294
                if (previewer == null) {
295
                        previewer = new SymbolPreviewer();
296
                        previewer.setPreferredSize(new Dimension(420, 65));
297
                }
298
                return previewer;
299
        }
300

    
301

    
302
        public void setModel(LabelClass lc) {
303
                this.lc = lc;
304
                refreshCmbExpressions();
305
                refreshTextSymbolPreviewer();
306
        }
307

    
308
        public void actionPerformed(ActionEvent e) {
309
                if (noEvent) return;
310
                Component c = (Component) e.getSource();
311
                if (c.equals(btnEditExpression)) {
312
                        ExprEditorPanel ep = new ExprEditorPanel((String) getCmbExpressions().
313
                                        getSelectedItem());
314
                        PluginServices.getMDIManager().addWindow( ep );
315
                        getCmbExpressions().addItem(ep.getExpression());
316
                        getCmbExpressions().setSelectedItem(ep.getExpression());
317
//                        lc.setLabelExpression(ep.getExpression());
318
                } else if (c.equals(btnFont)) {
319
                        Font labelFont = lc.getTextSymbol().getFont();
320
                        Font newFont = FontChooser.showDialog(PluginServices.getText(this, "font"), labelFont);
321
                        if (newFont != null) {
322
                                lc.getTextSymbol().setFont(newFont);
323
                        }
324
                        refreshTextSymbolPreviewer();
325
                } else if (c.equals(btnLabelStyles)) {
326
                        // here open symbol selector
327

    
328
                        StyleSelector stySel = new StyleSelector(
329
                                        lc.getLabelStyle(),
330
                                        FShape.TEXT );
331
                        stySel.setUnit(lc.getUnit());
332
                        stySel.setReferenceSystem(lc.getReferenceSystem());
333
                        PluginServices.getMDIManager().addWindow(stySel);
334
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
335
                        if (sty != null) {
336
                                // gather the style and apply to the class
337
                                lc.setLabelStyle(sty);
338
                                lc.setUnit(stySel.getUnit());
339
                                lc.setReferenceSystem(stySel.getReferenceSystem());
340

    
341
                        }
342
                } else if (c.equals(cmbExpressions)) {
343
//                        lc.setLabelExpression((String) cmbExpressions.getSelectedItem());
344
                } if (c.equals(btnSymbol)) {
345

    
346
                        // here open symbol selector
347

    
348
                        ISymbolSelector symSel = SymbolSelector.createSymbolSelector(lc.getTextSymbol(),
349
                                        FShape.TEXT,
350
                                        new SelectorFilter() {
351
                                                public boolean accepts(Object obj) {
352
                                                        if (obj instanceof ISymbol) {
353
                                                                ISymbol sym = (ISymbol) obj;
354
                                                                return sym.getSymbolType() == FShape.TEXT;
355
                                                        }
356
                                                        return false;
357
                                                }
358
                                        });
359

    
360
                        PluginServices.getMDIManager().addWindow(symSel);
361
                        ISymbol sym = (ISymbol) symSel.getSelectedObject();
362
                        if (sym != null) {
363
                                // gather the symbol and apply to the class
364
                                if (sym instanceof CartographicSupport) {
365
                                        CartographicSupport csSty = (CartographicSupport) sym;
366
                                        lc.setUnit(csSty.getUnit());
367
                                        lc.setReferenceSystem(csSty.getReferenceSystem());
368
                                }
369
                                lc.setTextSymbol((ITextSymbol) sym);
370

    
371
                        }
372

    
373
                        refreshTextSymbolPreviewer();
374
                }
375
        }
376

    
377
        private JButton getBtnLabelStyles() {
378
                if (btnLabelStyles == null) {
379
                        btnLabelStyles = new JButton(
380
                                        PluginServices.getText(this, "label_styles")+"...");
381
                        btnLabelStyles.setName("BTNLABELSTYLES");
382
                        btnLabelStyles.addActionListener(this);
383
                }
384
                return btnLabelStyles;
385
        }
386

    
387
        private void refreshTextSymbolPreviewer() {
388
                ITextSymbol textSym = lc.getTextSymbol();
389
                textSym.setText(PluginServices.getText(this, "GeneralLabeling.sample_text"));
390
                getSymbolPreviewer().setSymbol(textSym);
391

    
392
        }
393

    
394
}