Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.spi / src / main / java / org / gvsig / tools / swing / spi / AbstractJDynFieldComponent.java @ 306

History | View | Annotate | Download (6.96 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.tools.swing.spi;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Component;
26
import java.util.ArrayList;
27
import java.util.List;
28

    
29
import javax.swing.BorderFactory;
30
import javax.swing.Box;
31
import javax.swing.BoxLayout;
32
import javax.swing.JButton;
33
import javax.swing.JPanel;
34

    
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
38
import org.gvsig.tools.service.Manager;
39
import org.gvsig.tools.service.ServiceException;
40
import org.gvsig.tools.swing.api.ToolsSwingLocator;
41
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager;
42
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
43
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
44
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
45

    
46
/**
47
 * Component which is able to render a {@link DynObject} attribute value.
48
 * 
49
 * @author 2010- C?sar Ordi?ana - gvSIG team
50
 */
51
public abstract class AbstractJDynFieldComponent extends AbstractJDynField
52
    implements JDynFieldComponent {
53

    
54
    private static final long serialVersionUID = 7833217085675382320L;
55

    
56
    private final DynObjectSwingManager manager;
57

    
58
    private final List<ValueChangedListener> listeners;
59

    
60
    /**
61
     * Constructor.
62
     * 
63
     * @param dynField
64
     *            the field of the {@link DynObject} to render
65
     * @param dynObject
66
     *            the {@link DynObject} with the attribute to render
67
     */
68
        public AbstractJDynFieldComponent(ValueField parent)
69
        throws ServiceException {
70
                super(parent);
71
        listeners = new ArrayList<ValueChangedListener>();
72
        // this.dynObject = dynObject.getDynObject();
73
        manager = ToolsSwingLocator.getDynObjectSwingManager();
74
        this.init();
75
    }
76

    
77
    /*
78
     * (non-Javadoc)
79
     * 
80
     * @seeorg.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#
81
     * addValueChangedListener
82
     * (org.gvsig.tools.swing.api.dynobject.ValueChangedListener)
83
     */
84
    @Override
85
    public void addValueChangedListener(ValueChangedListener listener) {
86
        // If it was the first item, we need to
87
        // initialize Component listeners
88
        // add listener to the dynfield set of listeners
89
        listeners.add(listener);
90
        // set supplementary listeners.
91
        this.setJDynFieldComponentListeners();
92
    }
93

    
94
    protected JPanel createBoxLayout(Component component1,
95
        Component component2, Integer marginLeft, Integer marginRight,
96
        JPanel container) {
97
        return this.createBoxRowPanel(container, component1, component2,
98
            marginLeft, marginRight);
99
    }
100

    
101
    /*
102
     * (non-Javadoc)
103
     * 
104
     * @see
105
     * 
106
     * 
107
     * 
108
     * org.gvsig.tools.swing.api.usability.UsabilitySwingManager#createBoxRowPanel
109
     * (javax.swing.JPanel, java.awt.Component, java.awt.Component,
110
     * java.lang.Integer, java.lang.Integer)
111
     */
112
    public JPanel createBoxRowPanel(JPanel panel, Component labelComponent,
113
        Component fieldLabel, Integer marginLeft, Integer marginRight) {
114

    
115
        // Create a panel that uses GridLayout.
116
        JPanel pane = new JPanel();
117
        pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));
118
        pane.add(labelComponent);
119
        if (marginLeft != null) {
120
            pane.add(Box.createHorizontalStrut(marginLeft));
121
        }
122
        // pane.add(new JSeparator(SwingConstants.VERTICAL));
123
        if (marginRight != null) {
124
            pane.add(Box.createHorizontalStrut(marginRight));
125
        }
126
        pane.add(fieldLabel, BorderLayout.NORTH);
127
        // if (buttonComponent!=null)
128
        // pane.add(buttonComponent);
129
        pane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
130
        return pane;
131
    }
132

    
133
    protected JButton createButton(String text) {
134
        return ToolsSwingLocator.getUsabilitySwingManager().createJToolButton(
135
            this.translate(text));
136
    }
137

    
138
    /*
139
     * (non-Javadoc)
140
     * 
141
     * @seeorg.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#
142
     * raiseValueChangedEvent()
143
     */
144
    @Override
145
    public void fireValueChangedEvent() {
146
        for (ValueChangedListener listener : listeners) {
147
            listener.handleValueChanged(this);
148
        }
149
    }
150

    
151
    public Object getDefaultValue() {
152
        return this.getDynField().getDefaultValue();
153
    }
154

    
155
    protected String getFieldName() {
156
        return this.getDynField().getName();
157
    }
158

    
159
    // /**
160
    // * Returns the value of the {@link DynObject}'s field to manage.
161
    // *
162
    // * @return the field's value
163
    // */
164
    // protected Object getFieldValue(ValueField fieldValue) {
165
    // return fieldValue.getFieldValue();
166
    // }
167

    
168
    /*
169
     * (non-Javadoc)
170
     * 
171
     * @see org.gvsig.tools.service.Service#getManager()
172
     */
173
    public Manager getManager() {
174
        return manager;
175
    }
176

    
177
    /**
178
     * @return
179
     */
180
    public abstract Object getValue();
181

    
182
    public boolean isMandatory() {
183
        return this.getDynField().isMandatory();
184
    }
185

    
186
    /**
187
     * @return
188
     */
189
    protected boolean isReadOnly() {
190
        return this.getDynField().isReadOnly();
191
    }
192

    
193
    /*
194
     * (non-Javadoc)
195
     * 
196
     * @see
197
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent#isValid()
198
     */
199
    public boolean isValid() {
200
        // Object value = getValue();
201
        // if ((value==null)||(value.equals("")))
202
        // if (isMandatory())
203
        // return false;
204
        // else if (value==null)
205
        // return true;
206
        return this.validate(this.getValue());
207
    }
208

    
209
    /**
210
     * Stores the edited data into the DynObject field.
211
     */
212
    public void saveStatus() {
213
        this.setFieldValue(this.getValue());
214
    }
215

    
216
    /**
217
         * 
218
         */
219
    @Override
220
    protected abstract void setJDynFieldComponentListeners();
221

    
222
    protected String translate(String text) {
223
        return ToolsLocator.getI18nManager().getTranslation(text);
224
    }
225

    
226
    /**
227
     * @param value
228
     */
229
    private boolean validate(Object value) {
230
        try {
231
            this.getDynField().validate(value);
232
            return true;
233
        } catch (DynFieldValidateException e) {
234
            return false;
235
        }
236
    }
237

    
238
}