Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.swing / org.gvsig.expressionevaluator.swing.impl / src / main / java / org / gvsig / expressionevaluator / swing / impl / dynformfield / JDynFormFieldExpression.java @ 45936

History | View | Annotate | Download (7.31 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.expressionevaluator.swing.impl.dynformfield;
24

    
25
import java.awt.event.FocusEvent;
26
import java.awt.event.FocusListener;
27
import javax.swing.JButton;
28
import javax.swing.JPanel;
29
import javax.swing.text.JTextComponent;
30
import org.apache.commons.lang3.StringUtils;
31
import org.gvsig.expressionevaluator.Expression;
32
import org.gvsig.expressionevaluator.ExpressionUtils;
33
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator;
34
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
35
import org.gvsig.expressionevaluator.swing.ExpressionPickerController;
36
import org.gvsig.tools.dynform.DynFormFieldDefinition;
37
import org.gvsig.tools.dynform.JDynFormField;
38
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
39
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
40
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
41

    
42
public class JDynFormFieldExpression extends AbstractJDynFormField implements JDynFormField, FocusListener {
43

    
44
    protected Expression assignedValue = null;
45
    protected Expression currentValue = null;
46
    protected JTextComponent jtext = null;
47
    protected JButton btnBuilder = null;
48
    protected JButton btnHistory = null;
49
    protected JButton btnBookmarks = null;
50
    protected boolean readonly = false;
51
    private ExpressionPickerController pickerController;
52

    
53
    public JDynFormFieldExpression(
54
            DynFormSPIManager serviceManager, 
55
            DynFormSPIManager.ComponentsFactory componentsFactory,
56
            JDynFormFieldFactory factory, 
57
            DynFormFieldDefinition definition,
58
            Object value
59
            ) {
60
            super(serviceManager, componentsFactory, factory, definition, value);
61
        this.assignedValue = (Expression) value;
62
    }
63

    
64
    @Override
65
    public void setReadOnly(boolean readonly) {
66
        super.setReadOnly(readonly);
67
        if (this.contents != null) {
68
            if (this.readonly) {
69
                this.pickerController.setEditable(false);
70
            } else {
71
                this.pickerController.setEditable(true);
72
            }
73
        }
74
    }
75

    
76
    @Override
77
    public Object getAssignedValue() {
78
        return this.assignedValue;
79
    }
80

    
81
    @Override
82
    public void initComponent() {
83
        this.contents = new JPanel();
84
            
85
        this.jtext = this.getComponentsFactory().getJTextField(this.getDefinition(), null);
86
        this.jtext.addFocusListener(this);
87
        this.btnBuilder = this.getComponentsFactory().getJButton(this.getDefinition(), null);
88
        this.btnHistory = this.getComponentsFactory().getJButton(this.getDefinition(), "History");
89
        this.btnBookmarks = this.getComponentsFactory().getJButton(this.getDefinition(), "Bookmarks");
90
        ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager();
91
        this.pickerController = expressionSwingManager.createExpressionPickerController(
92
                jtext, 
93
                btnBuilder, 
94
                btnHistory, 
95
                btnBookmarks
96
        );
97
        if (this.readonly) {
98
            this.pickerController.setEditable(false);
99
        } else {
100
            this.pickerController.setEditable(true);
101
        }
102
        if( !this.getComponentsFactory().containsJTextField(this.getDefinition(), null) ) {
103
            this.createLayout();
104
        }
105
        this.setValue(this.assignedValue);
106
    }
107

    
108
    private void createLayout() {
109
        java.awt.GridBagConstraints gridBagConstraints;
110

    
111
        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
112
//        layout.columnWidths = new int[] {0, 5, 0, 5, 0, 5, 0};
113
//        layout.rowHeights = new int[] {0};
114
        this.contents.setLayout(layout);
115

    
116
        gridBagConstraints = new java.awt.GridBagConstraints();
117
        gridBagConstraints.gridx = 0;
118
        gridBagConstraints.gridy = 0;
119
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
120
        gridBagConstraints.weightx = 0.5;
121
        this.contents.add(this.jtext, gridBagConstraints);
122

    
123
        if( this.btnBuilder!= null ) {
124
            gridBagConstraints = new java.awt.GridBagConstraints();
125
            gridBagConstraints.gridx = 2;
126
            gridBagConstraints.gridy = 0;
127
            this.contents.add(this.btnBuilder, gridBagConstraints);
128
        }
129

    
130
        if( this.btnHistory!= null ) {
131
            gridBagConstraints = new java.awt.GridBagConstraints();
132
            gridBagConstraints.gridx = 4;
133
            gridBagConstraints.gridy = 0;
134
            this.contents.add(this.btnHistory, gridBagConstraints);
135
        }
136

    
137
        if( this.btnBookmarks!= null ) {
138
            gridBagConstraints = new java.awt.GridBagConstraints();
139
            gridBagConstraints.gridx = 6;
140
            gridBagConstraints.gridy = 0;
141
            this.contents.add(this.btnBookmarks, gridBagConstraints);        
142
        }
143
    }
144
    
145
    private void fireFieldChangedEventIfChanged(Expression previous) {
146
        if (previous == null) {
147
            if (this.currentValue == null) {
148
                return;
149
            }
150
            this.fireFieldChangedEvent();
151
            return;
152
        }
153
        if (this.currentValue == null) {
154
            this.fireFieldChangedEvent();
155
            return;
156
        }
157
        if ( StringUtils.equalsIgnoreCase(previous.getPhrase(),this.currentValue.getPhrase())) {
158
            this.fireFieldChangedEvent();
159
        }
160
    }
161

    
162
    @Override
163
    public void setValue(Object value) {
164
        Expression previous = this.currentValue;
165
        if( value instanceof CharSequence ) {
166
            value = ExpressionUtils.createExpression(value.toString());
167
        }
168
        if (value != null && !(value instanceof Expression)) {
169
            LOGGER.info("setValue invoked with non File value (" + value.toString() + ").");
170
            return;
171
        }
172
        this.pickerController.set(null);
173

    
174
        this.assignedValue = (Expression) value;
175
        this.currentValue = this.assignedValue;
176
        this.fireFieldChangedEventIfChanged(previous);
177
    }
178

    
179
    @Override
180
    public Object getValue() {
181
        return this.pickerController.get();
182
    }
183

    
184
    @Override
185
    public boolean hasValidValue() {
186
        return true;
187
    }
188

    
189
    @Override
190
    public void focusGained(FocusEvent arg0) {
191
        fireFieldEnterEvent();
192
        this.problemIndicator().restore();
193
    }
194

    
195
    @Override
196
    public void focusLost(FocusEvent arg0) {
197
        fireFieldExitEvent();
198
    }
199

    
200
    @Override
201
    public void clear() {
202
        this.pickerController.set(null);
203
    }
204

    
205
}