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 @ 1225

History | View | Annotate | Download (9.1 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 org.gvsig.tools.dynform.spi.dynformfield.VisitableSet;
19
import org.gvsig.tools.dynobject.DynField_v2;
20
import org.gvsig.tools.dynobject.DynObject;
21
import org.gvsig.tools.dynobject.DynObjectSet;
22
import org.gvsig.tools.exception.BaseException;
23
import org.gvsig.tools.service.Manager;
24
import org.gvsig.tools.service.Service;
25
import org.gvsig.tools.service.ServiceException;
26
import org.gvsig.tools.service.spi.ServiceManager;
27
import org.gvsig.tools.visitor.VisitCanceledException;
28
import org.gvsig.tools.visitor.Visitor;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
public abstract class AbstractJDynFormSet implements JDynFormSet, Service {
33

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

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

    
39
    protected JComponent contents = null;
40

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

    
45
    protected boolean autosave = true;
46

    
47
    protected int layoutMode = USE_PLAIN;
48

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

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

    
54
    private boolean useScrollBars = true;
55
    private boolean _allowUpdate;
56
    private boolean _allowDelete;
57
    private boolean _allowNew;
58
    private boolean _allowSearch;
59
    private boolean _allowClose;
60

    
61
    public AbstractJDynFormSet(ServiceManager manager, DynFormDefinition definition) throws ServiceException {
62
        this.manager = (DynFormSPIManager) manager;
63
        this.definition = definition;
64
        this.listeners = new DefaultVisitableSet();
65
    }
66

    
67
    public void addListener(JDynFormSetListener listener) {
68
        this.listeners.add(listener);
69
    }
70

    
71
    public void removeListener(JDynFormSetListener listener) {
72
        this.listeners.remove(listener);
73
    }
74

    
75
    protected void fireMessageEvent(final String message) {
76
        try {
77
            this.listeners.accept(new Visitor() {
78
                public void visit(Object listener) throws VisitCanceledException, BaseException {
79
                    ((JDynFormSetListener) listener).formMessage(message);
80
                }
81
            });
82

    
83
        } catch (Exception e) {
84
            logger.info("Error calling to the form message event.", e);
85
        }
86
    }
87

    
88
    protected void fireCloseEvent() {
89
        try {
90
            this.listeners.accept(new Visitor() {
91
                public void visit(Object listener) throws VisitCanceledException, BaseException {
92
                    ((JDynFormSetListener) listener).formClose();
93
                }
94
            });
95
        } catch (Exception e) {
96
            logger.info("Error calling to the form close event.", e);
97
        }
98
    }
99

    
100
    protected void fireFormMovedToEvent(final int position) {
101
        try {
102
            this.listeners.accept(new Visitor() {
103
                public void visit(Object listener) throws VisitCanceledException, BaseException {
104
                    ((JDynFormSetListener) listener).formMovedTo(position);
105
                }
106
            });
107
        } catch (Exception e) {
108
            logger.info("Error calling to the form moved to event.", e);
109
        }
110
    }
111

    
112
    public int getLayoutMode() {
113
        return this.layoutMode;
114
    }
115

    
116
    public void setLayoutMode(int layoutMode) {
117
        if ( layoutMode < 0 || layoutMode > USE_SEPARATORS ) {
118
            throw new IllegalArgumentException("layoutMode (" + layoutMode + ") out of range. Valid values are 0 .. " + USE_SEPARATORS + ".");
119
        }
120
        this.layoutMode = layoutMode;
121
    }
122

    
123
    public boolean isReadOnly() {
124
        return readOnly;
125
    }
126

    
127
    public void setReadOnly(boolean readOnly) {
128
        this.readOnly = readOnly;
129
    }
130

    
131
    public boolean isAutosave() {
132
        return this.autosave;
133
    }
134

    
135
    public void setAutosave(boolean autosave) {
136
        this.autosave = autosave;
137
    }
138

    
139
    protected int confirmDialog(final String message, final String title, final int optionType,
140
            final int messageType) {
141
        return JOptionPane.showConfirmDialog(
142
                this.contents, message, title, optionType, messageType);
143
    }
144

    
145
    public void setFormSize(int width, int height) {
146
        this.formHeight = height;
147
        this.formWidth = width;
148
    }
149

    
150
    public int countValues() {
151
        return this.values.size();
152
    }
153

    
154
    public void setValues(DynObjectSet values) throws ServiceException {
155
        List x = new ArrayList();
156
        DisposableIterator it;
157
        try {
158
            it = values.iterator();
159
        } catch (BaseException e) {
160
            logger.info("Uf! o se que hacer con este error, lo relanzo sin mas.", e);
161
            throw new RuntimeException(e);
162
        }
163
        while ( it.hasNext() ) {
164
            DynObject obj = (DynObject) it.next();
165
            if ( obj instanceof org.gvsig.tools.lang.Cloneable ) {
166
                try {
167
                    obj = (DynObject) ((org.gvsig.tools.lang.Cloneable) obj).clone();
168
                } catch (CloneNotSupportedException e) {
169
                    // Do nothing
170
                }
171
            }
172
            x.add(obj);
173
        }
174
        this.values = x;
175
    }
176

    
177
    public void setValues(List values) throws ServiceException {
178
//        this.values = new ArrayList<DynObject>();
179
//        this.values.addAll(values);
180
        this.values = values;
181
    }
182

    
183
    public DynObject get(int position) {
184
        return (DynObject) this.values.get(position);
185
    }
186

    
187
    public void addActionToPopupMenu(DataType tipo, String name, Action action) {
188
        try {
189
            this.actionsBuffer.add(new ActionStore(tipo, name, action));
190
        } catch (Exception ex) {
191
            String s = (tipo == null) ? "(null)" : tipo.getName();
192
            logger.warn("Can't add popup menu '" + name + "' to the fields of type '" + s + "' of the form.", ex);
193
        }
194
    }
195

    
196
    public void addSeparatorToPopupMenu(DataType tipo) {
197
        try {
198
            this.actionsBuffer.add(new ActionStore(tipo));
199
        } catch (Exception ex) {
200
            String s = (tipo == null) ? "(null)" : tipo.getName();
201
            logger.warn("Can't add separator to the popup menu to the fields of type '" + s + "' of the form.", ex);
202
        }
203

    
204
    }
205

    
206
    public void setUseScrollBars(boolean usesScrolls) {
207
        this.useScrollBars = usesScrolls;
208
    }
209

    
210
    public boolean getUseScrollBars() {
211
        return useScrollBars;
212
    }
213

    
214
    public Manager getManager() {
215
        return this.manager;
216
    }
217

    
218
    public DynFormSPIManager getServiceManager() {
219
        return this.manager;
220
    }
221

    
222
    public void message() {
223
        // Do nothing, ignore message handling
224
    }
225

    
226
    public void message(String msg) {
227
        // Do nothing, ignore message handling
228
    }
229

    
230
    public boolean allowUpdate() {
231
        return this._allowUpdate;
232
    }
233

    
234
    public boolean allowClose() {
235
        return this._allowClose;
236
    }
237

    
238
    public boolean allowDelete() {
239
        return this._allowDelete;
240
    }
241

    
242
    public boolean allowNew() {
243
        return this._allowNew ;
244
    }
245

    
246
    public boolean allowSearch() {
247
        return this._allowSearch;
248
    }
249

    
250
    public void setAllowUpdate(boolean allowUpdate) {
251
        this._allowUpdate = allowUpdate;
252
    }
253

    
254
    public void setAllowDelete(boolean allowDelete) {
255
        this._allowDelete = allowDelete;
256
    }
257

    
258
    public void setAllowNew(boolean allowNew) {
259
        this._allowNew = allowNew;
260
    }
261

    
262
    public void setAllowSearch(boolean allowSearch) {
263
        this._allowSearch = allowSearch;
264
    }
265

    
266
    public void setAllowClose(boolean allowClose) {
267
        this._allowClose = allowClose;
268
    }
269

    
270
    public void getFormValues(DynObject values) {
271
        throw new UnsupportedOperationException();
272
    }
273

    
274
    protected int getTagValueAsInt(String tagname, int defaultVaue) {
275
        if( definition.getTags().has(tagname) ) {
276
            try {
277
                int value = definition.getTags().getInt(tagname);
278
                return value;
279
            } catch (CoercionException ex) {
280
                logger.warn("Can't parse tag '"+tagname+"' as int for text field '"+definition.getName()+"'.",ex);
281
            }
282
        }
283
        return defaultVaue;
284
    }
285

    
286
    protected boolean getTagValueAsBoolean(String tagname, boolean defaultVaue) {
287
        if( definition.getTags().has(tagname) ) {
288
            try {
289
                boolean value = definition.getTags().getBoolean(tagname);
290
                return value;
291
            } catch (CoercionException ex) {
292
                logger.warn("Can't parse tag '"+tagname+"' as boolean for text field '"+definition.getName()+"'.",ex);
293
            }
294
        }
295
        return defaultVaue;
296
    }
297
    
298
}