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 / dynformset / AbstractJDynFormSet.java @ 2039

History | View | Annotate | Download (11.4 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.dynformset;
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.JDynForm;
18
import org.gvsig.tools.dynform.JDynForm.DynFormContext;
19
import static org.gvsig.tools.dynform.JDynForm.USE_PLAIN;
20
import static org.gvsig.tools.dynform.JDynForm.USE_SEPARATORS;
21
import org.gvsig.tools.dynform.JDynFormSet;
22
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
23
import static org.gvsig.tools.dynform.spi.dynform.AbstractJDynForm.getLayoutFromTags;
24
import org.gvsig.tools.dynform.spi.dynformfield.VisitableSet;
25
import org.gvsig.tools.dynobject.DynObject;
26
import org.gvsig.tools.dynobject.DynObjectSet;
27
import org.gvsig.tools.dynobject.Tags;
28
import org.gvsig.tools.exception.BaseException;
29
import org.gvsig.tools.service.ServiceException;
30
import org.gvsig.tools.visitor.VisitCanceledException;
31
import org.gvsig.tools.visitor.Visitor;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
public abstract class AbstractJDynFormSet implements JDynFormSet {
36

    
37
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractJDynFormSet.class);
38

    
39
    private final DynFormSPIManager manager;
40
    private final JDynFormSetFactory factory;
41
    private final DynFormDefinition definition;
42

    
43
    protected JComponent contents = null;
44

    
45
    protected List<DynObject> values = null;
46
    protected VisitableSet listeners = null;
47
    protected boolean readOnly = false;
48

    
49
    protected boolean autosave = true;
50

    
51
    protected int layoutMode = USE_PLAIN;
52

    
53
    protected int formWidth = -1;
54
    protected int formHeight = -1;
55

    
56
    protected List<ActionStore> actionsBuffer = new ArrayList<>();
57

    
58
    private boolean useScrollBars = true;
59
    private boolean _allowUpdate;
60
    private boolean _allowDelete;
61
    private boolean _allowNew;
62
    private boolean _allowSearch;
63
    private boolean _allowClose;
64
    
65
    private boolean _isInNewState = false;
66
    private final JDynForm.DynFormContext context;
67
    private final Tags contextTags;
68

    
69
    public AbstractJDynFormSet(
70
            DynFormSPIManager manager, 
71
            JDynFormSetFactory factory,
72
            JDynForm.DynFormContext context,
73
            DynFormDefinition definition,
74
            Tags contextTags
75
        ) {
76
        this.manager = manager;
77
        this.factory = factory;
78
        this.context = context;
79
        this.definition = definition;
80
        this.contextTags = contextTags;
81
        this.listeners = new DefaultVisitableSet();
82
    }
83
    
84
    @Override
85
    public JDynForm getForm() {
86
        return null;
87
    }
88

    
89
    @Override
90
    public void fireEvent(String action, Object value) {
91
    }
92
     
93
    protected JDynFormSetFactory getFactory() {
94
        return this.factory;
95
    }
96
    
97
    protected DynFormDefinition getDefinition() {
98
        return this.definition;
99
    }
100

    
101
    protected DynFormContext getContext() {
102
        return this.context;
103
    }
104

    
105
    public DynFormSPIManager getManager() {
106
        return this.manager;
107
    }
108
    
109
    public void loadDefaultValueFromTags(Tags tags) {
110
        this.setLayoutMode(getLayoutFromTags(tags));
111
        
112
        this.formWidth = tags.getInt(DynFormSPIManager.TAG_DYNFORM_WIDTH, this.formWidth);
113
        this.formHeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT, this.formHeight);
114
        this.readOnly = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_READONLY, this.readOnly);
115
        this.autosave = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_AUTOSAVE, this.autosave);
116
        this.useScrollBars = tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_USESCROLLBARS, this.useScrollBars);
117
        this.setAllowNew( tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_NEW, this._allowNew));
118
        this.setAllowUpdate(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_UPDATE, this._allowNew));
119
        this.setAllowDelete(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_DELETE, this._allowNew));
120
        this.setAllowSearch(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_SEARCH, this._allowNew));
121
        this.setAllowClose(tags.getBoolean(DynFormSPIManager.TAG_DYNFORM_ACTION_CLOSE, this._allowNew));
122
    }
123

    
124
    public boolean isInNewState() {
125
        return this._isInNewState;
126
    }
127
    
128
    protected void enterStateNew() {
129
        this._isInNewState = true;
130
    }
131
    
132
    protected void leaveTheNewState() {
133
        this._isInNewState = false;
134
    }
135
    
136
    public void addListener(JDynFormSetListener listener) {
137
        this.listeners.add(listener);
138
    }
139

    
140
    public void removeListener(JDynFormSetListener listener) {
141
        this.listeners.remove(listener);
142
    }
143

    
144
    protected void fireMessageEvent(final String message) {
145
        try {
146
            this.listeners.accept(new Visitor() {
147
                public void visit(Object listener) throws VisitCanceledException, BaseException {
148
                    ((JDynFormSetListener) listener).formMessage(message);
149
                }
150
            });
151

    
152
        } catch (Exception e) {
153
            LOGGER.info("Error calling to the form message event.", e);
154
        }
155
    }
156

    
157
    protected void fireCloseEvent() {
158
        try {
159
            this.listeners.accept(new Visitor() {
160
                public void visit(Object listener) throws VisitCanceledException, BaseException {
161
                    ((JDynFormSetListener) listener).formClose();
162
                }
163
            });
164
        } catch (Exception e) {
165
            LOGGER.info("Error calling to the form close event.", e);
166
        }
167
    }
168

    
169
    protected void fireFormMovedToEvent(final int position) {
170
        try {
171
            this.listeners.accept(new Visitor() {
172
                public void visit(Object listener) throws VisitCanceledException, BaseException {
173
                    ((JDynFormSetListener) listener).formMovedTo(position);
174
                }
175
            });
176
        } catch (Exception e) {
177
            LOGGER.info("Error calling to the form moved to event.", e);
178
        }
179
    }
180

    
181
    public int getLayoutMode() {
182
        return this.layoutMode;
183
    }
184

    
185
    public void setLayoutMode(int layoutMode) {
186
        if ( layoutMode < 0 || layoutMode > USE_SEPARATORS ) {
187
            throw new IllegalArgumentException("layoutMode (" + layoutMode + ") out of range. Valid values are 0 .. " + USE_SEPARATORS + ".");
188
        }
189
        this.layoutMode = layoutMode;
190
    }
191

    
192
    public boolean isReadOnly() {
193
        return readOnly;
194
    }
195

    
196
    public void setReadOnly(boolean readOnly) {
197
        this.readOnly = readOnly;
198
    }
199

    
200
    public boolean isAutosave() {
201
        return this.autosave;
202
    }
203

    
204
    public void setAutosave(boolean autosave) {
205
        this.autosave = autosave;
206
    }
207

    
208
    protected int confirmDialog(final String message, final String title, final int optionType,
209
            final int messageType) {
210
        return JOptionPane.showConfirmDialog(
211
                this.contents, message, title, optionType, messageType);
212
    }
213

    
214
    public void setFormSize(int width, int height) {
215
        this.formHeight = height;
216
        this.formWidth = width;
217
    }
218

    
219
    public int countValues() {
220
        return this.values.size();
221
    }
222

    
223
    public void setValues(DynObjectSet values) {
224
        List x = new ArrayList();
225
        DisposableIterator it;
226
        try {
227
            it = values.iterator();
228
        } catch (BaseException e) {
229
            LOGGER.info("Uf! o se que hacer con este error, lo relanzo sin mas.", e);
230
            throw new RuntimeException(e);
231
        }
232
        while ( it.hasNext() ) {
233
            DynObject obj = (DynObject) it.next();
234
            if ( obj instanceof org.gvsig.tools.lang.Cloneable ) {
235
                try {
236
                    obj = (DynObject) ((org.gvsig.tools.lang.Cloneable) obj).clone();
237
                } catch (CloneNotSupportedException e) {
238
                    // Do nothing
239
                }
240
            }
241
            x.add(obj);
242
        }
243
        this.values = x;
244
    }
245

    
246
    public void setValues(List values) {
247
//        this.values = new ArrayList<DynObject>();
248
//        this.values.addAll(values);
249
        this.values = values;
250
    }
251

    
252
    public DynObject get(int position) {
253
        return (DynObject) this.values.get(position);
254
    }
255

    
256
    public void addActionToPopupMenu(DataType tipo, String name, Action action) {
257
        try {
258
            this.actionsBuffer.add(new ActionStore(tipo, name, action));
259
        } catch (Exception ex) {
260
            String s = (tipo == null) ? "(null)" : tipo.getName();
261
            LOGGER.warn("Can't add popup menu '" + name + "' to the fields of type '" + s + "' of the form.", ex);
262
        }
263
    }
264

    
265
    public void addSeparatorToPopupMenu(DataType tipo) {
266
        try {
267
            this.actionsBuffer.add(new ActionStore(tipo));
268
        } catch (Exception ex) {
269
            String s = (tipo == null) ? "(null)" : tipo.getName();
270
            LOGGER.warn("Can't add separator to the popup menu to the fields of type '" + s + "' of the form.", ex);
271
        }
272

    
273
    }
274

    
275
    public void setUseScrollBars(boolean usesScrolls) {
276
        this.useScrollBars = usesScrolls;
277
    }
278

    
279
    public boolean getUseScrollBars() {
280
        return useScrollBars;
281
    }
282

    
283
    public DynFormSPIManager getServiceManager() {
284
        return this.manager;
285
    }
286

    
287
    public void message() {
288
        // Do nothing, ignore message handling
289
    }
290

    
291
    public void message(String msg) {
292
        // Do nothing, ignore message handling
293
    }
294

    
295
    public boolean allowUpdate() {
296
        return this._allowUpdate;
297
    }
298

    
299
    public boolean allowClose() {
300
        return this._allowClose;
301
    }
302

    
303
    public boolean allowDelete() {
304
        return this._allowDelete;
305
    }
306

    
307
    public boolean allowNew() {
308
        return this._allowNew ;
309
    }
310

    
311
    public boolean allowSearch() {
312
        return this._allowSearch;
313
    }
314

    
315
    public void setAllowUpdate(boolean allowUpdate) {
316
        this._allowUpdate = allowUpdate;
317
    }
318

    
319
    public void setAllowDelete(boolean allowDelete) {
320
        this._allowDelete = allowDelete;
321
    }
322

    
323
    public void setAllowNew(boolean allowNew) {
324
        this._allowNew = allowNew;
325
    }
326

    
327
    public void setAllowSearch(boolean allowSearch) {
328
        this._allowSearch = allowSearch;
329
    }
330

    
331
    public void setAllowClose(boolean allowClose) {
332
        this._allowClose = allowClose;
333
    }
334

    
335
    public void getFormValues(DynObject values) {
336
        throw new UnsupportedOperationException();
337
    }
338

    
339
    protected int getTagValueAsInt(String tagname, int defaultVaue) {
340
        if( definition.getTags().has(tagname) ) {
341
            try {
342
                int value = definition.getTags().getInt(tagname);
343
                return value;
344
            } catch (CoercionException ex) {
345
                LOGGER.warn("Can't parse tag '"+tagname+"' as int for text field '"+definition.getName()+"'.",ex);
346
            }
347
        }
348
        return defaultVaue;
349
    }
350

    
351
    protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) {
352
        if( definition.getTags().has(tagname) ) {
353
            try {
354
                boolean value = definition.getTags().getBoolean(tagname);
355
                return value;
356
            } catch (CoercionException ex) {
357
                LOGGER.warn("Can't parse tag '"+tagname+"' as boolean for text field '"+definition.getName()+"'.",ex);
358
            }
359
        }
360
        return defaultVaue;
361
    }
362
    
363
}