Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.spi / src / main / java / org / gvsig / tools / dynform / spi / AbstractJDynFormSet.java @ 1405

History | View | Annotate | Download (10.6 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.tools.dynform.spi;
7

    
8
import java.util.ArrayList;
9
import java.util.List;
10
import javax.swing.Action;
11
import javax.swing.JComponent;
12
import javax.swing.JOptionPane;
13
import org.gvsig.tools.dataTypes.CoercionException;
14
import org.gvsig.tools.dataTypes.DataType;
15
import org.gvsig.tools.dispose.DisposableIterator;
16
import org.gvsig.tools.dynform.DynFormDefinition;
17
import org.gvsig.tools.dynform.JDynFormSet;
18
import static org.gvsig.tools.dynform.spi.AbstractJDynForm.getLayoutFromTags;
19
import org.gvsig.tools.dynform.spi.dynformfield.VisitableSet;
20
import org.gvsig.tools.dynobject.DynObject;
21
import org.gvsig.tools.dynobject.DynObjectSet;
22
import org.gvsig.tools.dynobject.Tags;
23
import org.gvsig.tools.exception.BaseException;
24
import org.gvsig.tools.service.Manager;
25
import org.gvsig.tools.service.Service;
26
import org.gvsig.tools.service.ServiceException;
27
import org.gvsig.tools.service.spi.ServiceManager;
28
import org.gvsig.tools.visitor.VisitCanceledException;
29
import org.gvsig.tools.visitor.Visitor;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
public abstract class AbstractJDynFormSet implements JDynFormSet, Service {
34

    
35
    private static final Logger logger = LoggerFactory.getLogger(AbstractJDynFormSet.class);
36

    
37
    protected DynFormSPIManager manager = null;
38
    protected DynFormDefinition definition = null;
39

    
40
    protected JComponent contents = null;
41

    
42
    protected List<DynObject> values = null;
43
    protected VisitableSet listeners = null;
44
    protected boolean readOnly = false;
45

    
46
    protected boolean autosave = true;
47

    
48
    protected int layoutMode = USE_PLAIN;
49

    
50
    protected int formWidth = -1;
51
    protected int formHeight = -1;
52

    
53
    protected List<ActionStore> actionsBuffer = new ArrayList<ActionStore>();
54

    
55
    private boolean useScrollBars = true;
56
    private boolean _allowUpdate;
57
    private boolean _allowDelete;
58
    private boolean _allowNew;
59
    private boolean _allowSearch;
60
    private boolean _allowClose;
61
    
62
    private boolean _isInNewState = false;
63

    
64
    public AbstractJDynFormSet(ServiceManager manager, DynFormDefinition definition) throws ServiceException {
65
        this.manager = (DynFormSPIManager) manager;
66
        this.definition = definition;
67
        this.listeners = new DefaultVisitableSet();
68
    }
69
    
70
    public void loadDefaultValueFromTags(Tags tags) {
71
        this.setLayoutMode(getLayoutFromTags(tags));
72
        
73
        this.formWidth = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, this.formWidth);
74
        this.formHeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, this.formHeight);
75
        this.readOnly = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY, this.readOnly);
76
        this.autosave = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_AUTOSAVE, this.autosave);
77
        this.useScrollBars = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_USESCROLLBARS, this.useScrollBars);
78
        this.setAllowNew( tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_NEW, this._allowNew));
79
        this.setAllowUpdate(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_UPDATE, this._allowNew));
80
        this.setAllowDelete(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_DELETE, this._allowNew));
81
        this.setAllowSearch(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_SEARCH, this._allowNew));
82
        this.setAllowClose(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_CLOSE, this._allowNew));
83
    }
84

    
85
    public boolean isInNewState() {
86
        return this._isInNewState;
87
    }
88
    
89
    protected void enterStateNew() {
90
        this._isInNewState = true;
91
    }
92
    
93
    protected void leaveTheNewState() {
94
        this._isInNewState = false;
95
    }
96
    
97
    public void addListener(JDynFormSetListener listener) {
98
        this.listeners.add(listener);
99
    }
100

    
101
    public void removeListener(JDynFormSetListener listener) {
102
        this.listeners.remove(listener);
103
    }
104

    
105
    protected void fireMessageEvent(final String message) {
106
        try {
107
            this.listeners.accept(new Visitor() {
108
                public void visit(Object listener) throws VisitCanceledException, BaseException {
109
                    ((JDynFormSetListener) listener).formMessage(message);
110
                }
111
            });
112

    
113
        } catch (Exception e) {
114
            logger.info("Error calling to the form message event.", e);
115
        }
116
    }
117

    
118
    protected void fireCloseEvent() {
119
        try {
120
            this.listeners.accept(new Visitor() {
121
                public void visit(Object listener) throws VisitCanceledException, BaseException {
122
                    ((JDynFormSetListener) listener).formClose();
123
                }
124
            });
125
        } catch (Exception e) {
126
            logger.info("Error calling to the form close event.", e);
127
        }
128
    }
129

    
130
    protected void fireFormMovedToEvent(final int position) {
131
        try {
132
            this.listeners.accept(new Visitor() {
133
                public void visit(Object listener) throws VisitCanceledException, BaseException {
134
                    ((JDynFormSetListener) listener).formMovedTo(position);
135
                }
136
            });
137
        } catch (Exception e) {
138
            logger.info("Error calling to the form moved to event.", e);
139
        }
140
    }
141

    
142
    public int getLayoutMode() {
143
        return this.layoutMode;
144
    }
145

    
146
    public void setLayoutMode(int layoutMode) {
147
        if ( layoutMode < 0 || layoutMode > USE_SEPARATORS ) {
148
            throw new IllegalArgumentException("layoutMode (" + layoutMode + ") out of range. Valid values are 0 .. " + USE_SEPARATORS + ".");
149
        }
150
        this.layoutMode = layoutMode;
151
    }
152

    
153
    public boolean isReadOnly() {
154
        return readOnly;
155
    }
156

    
157
    public void setReadOnly(boolean readOnly) {
158
        this.readOnly = readOnly;
159
    }
160

    
161
    public boolean isAutosave() {
162
        return this.autosave;
163
    }
164

    
165
    public void setAutosave(boolean autosave) {
166
        this.autosave = autosave;
167
    }
168

    
169
    protected int confirmDialog(final String message, final String title, final int optionType,
170
            final int messageType) {
171
        return JOptionPane.showConfirmDialog(
172
                this.contents, message, title, optionType, messageType);
173
    }
174

    
175
    public void setFormSize(int width, int height) {
176
        this.formHeight = height;
177
        this.formWidth = width;
178
    }
179

    
180
    public int countValues() {
181
        return this.values.size();
182
    }
183

    
184
    public void setValues(DynObjectSet values) throws ServiceException {
185
        List x = new ArrayList();
186
        DisposableIterator it;
187
        try {
188
            it = values.iterator();
189
        } catch (BaseException e) {
190
            logger.info("Uf! o se que hacer con este error, lo relanzo sin mas.", e);
191
            throw new RuntimeException(e);
192
        }
193
        while ( it.hasNext() ) {
194
            DynObject obj = (DynObject) it.next();
195
            if ( obj instanceof org.gvsig.tools.lang.Cloneable ) {
196
                try {
197
                    obj = (DynObject) ((org.gvsig.tools.lang.Cloneable) obj).clone();
198
                } catch (CloneNotSupportedException e) {
199
                    // Do nothing
200
                }
201
            }
202
            x.add(obj);
203
        }
204
        this.values = x;
205
    }
206

    
207
    public void setValues(List values) throws ServiceException {
208
//        this.values = new ArrayList<DynObject>();
209
//        this.values.addAll(values);
210
        this.values = values;
211
    }
212

    
213
    public DynObject get(int position) {
214
        return (DynObject) this.values.get(position);
215
    }
216

    
217
    public void addActionToPopupMenu(DataType tipo, String name, Action action) {
218
        try {
219
            this.actionsBuffer.add(new ActionStore(tipo, name, action));
220
        } catch (Exception ex) {
221
            String s = (tipo == null) ? "(null)" : tipo.getName();
222
            logger.warn("Can't add popup menu '" + name + "' to the fields of type '" + s + "' of the form.", ex);
223
        }
224
    }
225

    
226
    public void addSeparatorToPopupMenu(DataType tipo) {
227
        try {
228
            this.actionsBuffer.add(new ActionStore(tipo));
229
        } catch (Exception ex) {
230
            String s = (tipo == null) ? "(null)" : tipo.getName();
231
            logger.warn("Can't add separator to the popup menu to the fields of type '" + s + "' of the form.", ex);
232
        }
233

    
234
    }
235

    
236
    public void setUseScrollBars(boolean usesScrolls) {
237
        this.useScrollBars = usesScrolls;
238
    }
239

    
240
    public boolean getUseScrollBars() {
241
        return useScrollBars;
242
    }
243

    
244
    public Manager getManager() {
245
        return this.manager;
246
    }
247

    
248
    public DynFormSPIManager getServiceManager() {
249
        return this.manager;
250
    }
251

    
252
    public void message() {
253
        // Do nothing, ignore message handling
254
    }
255

    
256
    public void message(String msg) {
257
        // Do nothing, ignore message handling
258
    }
259

    
260
    public boolean allowUpdate() {
261
        return this._allowUpdate;
262
    }
263

    
264
    public boolean allowClose() {
265
        return this._allowClose;
266
    }
267

    
268
    public boolean allowDelete() {
269
        return this._allowDelete;
270
    }
271

    
272
    public boolean allowNew() {
273
        return this._allowNew ;
274
    }
275

    
276
    public boolean allowSearch() {
277
        return this._allowSearch;
278
    }
279

    
280
    public void setAllowUpdate(boolean allowUpdate) {
281
        this._allowUpdate = allowUpdate;
282
    }
283

    
284
    public void setAllowDelete(boolean allowDelete) {
285
        this._allowDelete = allowDelete;
286
    }
287

    
288
    public void setAllowNew(boolean allowNew) {
289
        this._allowNew = allowNew;
290
    }
291

    
292
    public void setAllowSearch(boolean allowSearch) {
293
        this._allowSearch = allowSearch;
294
    }
295

    
296
    public void setAllowClose(boolean allowClose) {
297
        this._allowClose = allowClose;
298
    }
299

    
300
    public void getFormValues(DynObject values) {
301
        throw new UnsupportedOperationException();
302
    }
303

    
304
    protected int getTagValueAsInt(String tagname, int defaultVaue) {
305
        if( definition.getTags().has(tagname) ) {
306
            try {
307
                int value = definition.getTags().getInt(tagname);
308
                return value;
309
            } catch (CoercionException ex) {
310
                logger.warn("Can't parse tag '"+tagname+"' as int for text field '"+definition.getName()+"'.",ex);
311
            }
312
        }
313
        return defaultVaue;
314
    }
315

    
316
    protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) {
317
        if( definition.getTags().has(tagname) ) {
318
            try {
319
                boolean value = definition.getTags().getBoolean(tagname);
320
                return value;
321
            } catch (CoercionException ex) {
322
                logger.warn("Can't parse tag '"+tagname+"' as boolean for text field '"+definition.getName()+"'.",ex);
323
            }
324
        }
325
        return defaultVaue;
326
    }
327
    
328
}