Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / layerproperties / LabelExpressionEditorPanel.java @ 29859

History | View | Annotate | Download (6.56 KB)

1
package org.gvsig.symbology.gui.layerproperties;
2

    
3
import java.awt.Component;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.Insets;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.MouseEvent;
10
import java.awt.event.MouseListener;
11

    
12
import javax.swing.BorderFactory;
13
import javax.swing.JList;
14
import javax.swing.JPanel;
15
import javax.swing.JScrollPane;
16
import javax.swing.JTextArea;
17
import javax.swing.ScrollPaneConstants;
18

    
19
import org.gvsig.gui.beans.swing.JButton;
20

    
21
import com.iver.andami.PluginServices;
22
import com.iver.andami.ui.mdiManager.IWindow;
23
import com.iver.andami.ui.mdiManager.WindowInfo;
24

    
25
public class LabelExpressionEditorPanel extends JPanel implements IWindow, ActionListener, MouseListener {
26
        private static final String CANCEL_ACTION = "CANCEL";
27
        private static final String ACCEPT_ACTION = "ACCEPT";
28
        private static final String ADDFIELD_ACTION = "ADD_FIELD";
29
        private String originalValue;
30
        private String value;
31
        private WindowInfo wInfo = null;
32
        private String[] fieldNames;
33
        private int[] fieldTypes;
34
        private JButton botCancel;
35
        private JButton botAccept;
36
        private JButton botAddField;
37
        private JScrollPane spExpression;
38
        private JTextArea txtExpression;
39
        private JScrollPane spFieldList;
40
        private JList lstFields;
41

    
42
        public LabelExpressionEditorPanel(String[] fieldNames, int[] fieldTypes) {
43
                super();
44
                this.fieldNames = fieldNames;
45
                this.fieldTypes = fieldTypes;
46
                initialize();
47
        }
48

    
49
        private void initialize() {
50
                this.setLayout(new GridBagLayout());
51

    
52
                GridBagConstraints baseConst = new GridBagConstraints();
53
                baseConst.ipadx = 2;
54
                baseConst.ipady = 2;
55
                baseConst.anchor = baseConst.CENTER;
56
                baseConst.insets = new Insets(2,2,2,2);
57

    
58
                GridBagConstraints con;
59

    
60
                con = (GridBagConstraints) baseConst.clone();
61

    
62
                con.gridx = 0;
63
                con.gridy = 0;
64
                con.gridheight = 3;
65
                con.gridwidth = 2;
66
                con.fill = con.BOTH;
67
                con.weightx = 1;
68
                con.weighty = 1;
69
                this.add(getFieldListComposition(), con);
70

    
71
                con = (GridBagConstraints) baseConst.clone();
72
                con.gridx = 2;
73
                con.gridy = 2;
74
                con.fill = con.NONE;
75
                this.add(getBotAddField(), con);
76

    
77
                con.gridx = 0;
78
                con.gridy = 3;
79
                con.gridheight = 4;
80
                con.gridwidth = 5;
81
                con.fill = con.BOTH;
82
                con.weightx = 1;
83
                con.weighty = 1;
84
                this.add(getExpressionComposition(), con);
85

    
86
                con = (GridBagConstraints) baseConst.clone();
87
                con.gridx = 3;
88
                con.gridy = 8;
89
                con.fill = con.NONE;
90
                this.add(getBotAccept(), con);
91

    
92
                con = (GridBagConstraints) baseConst.clone();
93
                con.gridx = 4;
94
                con.gridy = 8;
95
                con.fill = con.NONE;
96
                this.add(getBotCancel(), con);
97

    
98
        }
99

    
100
        private JButton getBotCancel() {
101
                if (botCancel == null) {
102
                        botCancel = new JButton(PluginServices.getText(this, "cancel"));
103
                        botCancel.setActionCommand(CANCEL_ACTION);
104
                        botCancel.addActionListener(this);
105
                }
106
                return botCancel;
107
        }
108

    
109
        private JButton getBotAccept() {
110
                if (botAccept == null) {
111
                        botAccept = new JButton(PluginServices.getText(this, "accept"));
112
                        botAccept.setActionCommand(ACCEPT_ACTION);
113
                        botAccept.addActionListener(this);
114
                }
115
                return botAccept;
116

    
117
        }
118

    
119
        private Component getExpressionComposition() {
120
                if (spExpression == null) {
121
                        spExpression = new JScrollPane();
122
                        spExpression.setViewportView(getTxtExpression());
123
                        spExpression.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
124
                        spExpression.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
125
                        spExpression
126
                                        .setBorder(BorderFactory.createTitledBorder(PluginServices
127
                                                        .getText(this, "expression")));
128
                }
129
                return spExpression;
130
        }
131

    
132
        private JTextArea getTxtExpression() {
133
                if (txtExpression == null){
134
                        txtExpression = new JTextArea();
135
                }
136
                return txtExpression;
137
        }
138

    
139
        public void setExpression(String expr){
140
                this.getTxtExpression().setText(expr);
141
        }
142

    
143
        public String getExpression(){
144
                return this.getTxtExpression().getText();
145
        }
146

    
147
        private JButton getBotAddField() {
148
                if (botAddField == null) {
149
                        botAddField = new JButton(PluginServices.getText(this, "add_field"));
150
                        botAddField.setActionCommand(ADDFIELD_ACTION);
151
                        botAddField.addActionListener(this);
152
                }
153
                return botAddField;
154

    
155
        }
156

    
157
        private Component getFieldListComposition() {
158
                if (spFieldList == null){
159
                        spFieldList = new JScrollPane();
160
                        spFieldList.setViewportView(getLstFields());
161
                        spFieldList.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
162
                        spFieldList.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
163
                        spFieldList
164
                                        .setBorder(BorderFactory.createTitledBorder(PluginServices
165
                                                        .getText(this, "fields")));
166

    
167
                }
168
                return spFieldList;
169
        }
170

    
171
        private JList getLstFields() {
172
                if (lstFields == null){
173
                        lstFields = new JList(this.fieldNames);
174
                        lstFields.addMouseListener(this);
175
                }
176
                return lstFields;
177
        }
178

    
179
        public WindowInfo getWindowInfo() {
180
                if (wInfo == null) {
181
                        wInfo = new WindowInfo(WindowInfo.RESIZABLE
182
                                        + WindowInfo.MODALDIALOG);
183
                        wInfo.setWidth(500);
184
                        wInfo.setHeight(400);
185
                        wInfo.setTitle(PluginServices.getText(this, "expression"));
186
                }
187
                return wInfo;
188
        }
189

    
190
        public Object getWindowProfile() {
191
                return WindowInfo.EDITOR_PROFILE;
192
        }
193

    
194
        public void setValue(String value) {
195
                this.originalValue = value;
196
                this.value = value;
197
                this.setExpression(value);
198
        }
199

    
200
        public String getValue() {
201
                return this.value;
202
        }
203

    
204
        public void actionPerformed(ActionEvent e) {
205
                doAction(e.getActionCommand());
206
        }
207

    
208
        private void doAction(String action){
209
                if (action.equals(ADDFIELD_ACTION)){
210
                        JTextArea txt = getTxtExpression();
211
                        StringBuffer strb = new StringBuffer();
212
                        String str = txt.getText();
213
                        int sini = txt.getSelectionStart();
214
                        int send = txt.getSelectionEnd();
215
                        if (sini > 0){
216
                                strb.append(str.substring(0, sini));
217
                                str = str.substring(sini);
218
                        }
219
                        strb.append('[');
220
                        strb.append((String)getLstFields().getSelectedValue());
221
                        strb.append(']');
222
                        send = send -sini;
223
                        if (send > 0){
224
                                str = str.substring(send);
225
                        }
226
                        strb.append(str);
227

    
228
                        getTxtExpression().setText(strb.toString());
229

    
230
                } else {
231
                        // Accept or cancel
232
                        if (action.equals(ACCEPT_ACTION)){
233
                                this.value = getExpression();
234
                        } else {
235
                                this.value = this.originalValue;
236
                        }
237
                        PluginServices.getMDIManager().closeWindow(this);
238
                }
239
        }
240

    
241
        public void mouseClicked(MouseEvent e) {
242
                if (e.getSource() == getLstFields()){
243
                        if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() > 1){
244
                                doAction(ADDFIELD_ACTION);
245
                        }
246
                }
247

    
248

    
249
        }
250

    
251
        public void mouseEntered(MouseEvent e) {
252
                // Do nothing
253

    
254
        }
255

    
256
        public void mouseExited(MouseEvent e) {
257
                // Do nothing
258
        }
259

    
260
        public void mousePressed(MouseEvent e) {
261
                // Do nothing
262

    
263
        }
264

    
265
        public void mouseReleased(MouseEvent e) {
266
                // Do nothing
267

    
268
        }
269
}