Statistics
| Revision:

gvsig-attributeeditor / org.gvsig.attributeeditor / trunk / org.gvsig.attributeeditor / org.gvsig.featureform.swing / org.gvsig.featureform.swing.impl / src / main / java / org / gvsig / featureform / swing / impl / DefaultJFeatureForm.java @ 8

History | View | Annotate | Download (10.2 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.featureform.swing.impl;
24

    
25
import java.awt.Dimension;
26

    
27
import javax.swing.JComponent;
28

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.featureform.swing.JFeatureForm;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper;
40
import org.gvsig.tools.ToolsLocator;
41
import org.gvsig.tools.dynform.AbortActionException;
42
import org.gvsig.tools.dynform.DynFormLocator;
43
import org.gvsig.tools.dynform.DynFormManager;
44
import org.gvsig.tools.dynform.JDynForm;
45
import org.gvsig.tools.dynform.JDynFormSet;
46
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener;
47
import org.gvsig.tools.dynobject.DynObject;
48
import org.gvsig.tools.exception.BaseException;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.gvsig.tools.locator.LocatorException;
51
import org.gvsig.tools.service.ServiceException;
52
import org.gvsig.tools.swing.api.ToolsSwingLocator;
53
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
54
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
55

    
56
/**
57
 * @author fdiaz
58
 *
59
 */
60
public class DefaultJFeatureForm implements JFeatureForm, JDynFormSetListener {
61

    
62
    private static final Logger LOGGER = LoggerFactory
63
        .getLogger(DefaultJFeatureForm.class);
64

    
65
    private static final int PAGE_SIZE = 500;
66
    private JDynFormSet formset;
67
    private FeatureStore store;
68
    private FeaturePagingHelper ph;
69

    
70
    /**
71
     *
72
     */
73
    public DefaultJFeatureForm() {
74
        // TODO Auto-generated constructor stub
75
    }
76

    
77
    /*
78
     * (non-Javadoc)
79
     *
80
     * @see
81
     * org.gvsig.attributeeditor.swing.JFeatureForm#setPreferredSize(java.awt
82
     * .Dimension)
83
     */
84
    public void setPreferredSize(Dimension dimension) {
85
        if (formset != null) {
86
            formset.setFormSize(dimension.width, dimension.height);
87
        }
88
    }
89

    
90
    public JComponent asJComponent() {
91
        return formset.asJComponent();
92
    }
93

    
94
    /*
95
     * (non-Javadoc)
96
     *
97
     * @see
98
     * org.gvsig.attributeeditor.swing.JFeatureForm#bind(org.gvsig.fmap.dal.
99
     * feature.FeatureStore)
100
     */
101
    public void bind(FeatureStore store) throws ServiceException, DataException {
102
        if (this.store == store) {
103
            return;
104
        }
105
        this.store = store;
106
        DynFormManager formManager = DynFormLocator.getDynFormManager();
107
        formset = formManager.createJDynFormSet(store.getDefaultFeatureType());
108

    
109
        formset.setLayoutMode(JDynForm.USE_SEPARATORS);
110
        formset.setReadOnly(false);
111
        formset.setAllowClose(true);
112
        formset.setAllowDelete(false);
113
        formset.setAllowNew(false);
114
        formset.setAllowSearch(false);
115
        formset.setAllowUpdate(true);
116
        formset.setAutosave(true);
117

    
118
        formset.addListener(this);
119
        this.ph = null;
120

    
121
    }
122

    
123
    /*
124
     * (non-Javadoc)
125
     *
126
     * @see
127
     * org.gvsig.attributeeditor.swing.JFeatureForm#setQuery(org.gvsig.fmap.
128
     * dal.feature.FeatureQuery)
129
     */
130
    public void setQuery(FeatureQuery query) throws LocatorException,
131
        BaseException {
132
        if (this.ph != null) {
133
            if (this.formset != null && this.formset.isAutosave()
134
                && this.formset.countValues() > 0
135
                && this.formset.asJComponent().isVisible()) {
136
                if (!store.isEditing()) {
137
                    try {
138
                        store.edit();
139
                    } catch (DataException e1) {
140
                        throw new StoreEditException(e1, store.getName());
141
                    }
142
                }
143
                saveChanges(this.formset);
144
            }
145
        }
146
        this.ph =
147
            DALLocator.getDataManager().createFeaturePagingHelper(store, query,
148
                PAGE_SIZE);
149
        this.formset.setReadOnly(false);
150
        this.formset.setValues(ph.asListOfDynObjects());
151
    }
152

    
153
    /*
154
     * (non-Javadoc)
155
     *
156
     * @see org.gvsig.attributeeditor.swing.JFeatureForm#hideForm()
157
     */
158
    public void hideForm() {
159
        formset.asJComponent().setVisible(false);
160

    
161
    }
162

    
163
    /*
164
     * (non-Javadoc)
165
     *
166
     * @see org.gvsig.attributeeditor.swing.JFeatureForm#showForm()
167
     */
168
    public void showForm(MODE mode) {
169
        WindowManager winmgr = ToolsSwingLocator.getWindowManager();
170
        winmgr.showWindow(formset.asJComponent(), store.getName(), mode);
171
        formset.asJComponent().setVisible(true);
172
    }
173

    
174
    public void formMessage(String message) {
175
        // TODO Auto-generated method stub
176

    
177
    }
178

    
179
    public void formClose() {
180
        this.hideForm();
181
    }
182

    
183
    public void formMovedTo(int currentPosition) throws AbortActionException {
184
        LOGGER.info("formMovedTo " + currentPosition);
185
    }
186

    
187
    class StoreEditException extends AbortActionException {
188

    
189
        /**
190
         *
191
         */
192
        private static final long serialVersionUID = -7682017811778577130L;
193

    
194
        public StoreEditException(Throwable cause, String storename) {
195
            super("Can't edit the store '%(storename)'", cause,
196
                "cant_edit_the store_XstorenameX", serialVersionUID);
197
            setValue("storename", storename);
198
        }
199
    }
200

    
201
    public void formBeforeSave(JDynFormSet dynformSet)
202
        throws AbortActionException {
203
        LOGGER.info("formBeforeSave");
204
        if (!store.isEditing()) {
205
            try {
206
                store.edit();
207
            } catch (DataException e1) {
208
                throw new StoreEditException(e1, store.getName());
209
            }
210
        }
211
    }
212

    
213
    public void formBeforeNew(JDynFormSet dynformSet)
214
        throws AbortActionException {
215
        LOGGER.info("formBeforeNew");
216
    }
217

    
218
    public void formBeforeDelete(JDynFormSet dynformSet)
219
        throws AbortActionException {
220
        LOGGER.info("formBeforeDelete");
221
    }
222

    
223
    class GetFeatureException extends AbortActionException {
224

    
225
        /**
226
         *
227
         */
228
        private static final long serialVersionUID = -3545660152672760731L;
229

    
230
        public GetFeatureException(Throwable cause, int index) {
231
            super("Can't get the associated feature to index '%(index)'",
232
                cause, "cant_get_the_associated_feature_to_index_XindexX",
233
                serialVersionUID);
234
            setValue("index", index);
235
        }
236
    }
237

    
238
    class UpdateFeatureException extends AbortActionException {
239

    
240
        /**
241
         *
242
         */
243
        private static final long serialVersionUID = 6473287361543242618L;
244

    
245
        public UpdateFeatureException(Throwable cause, int index,
246
            String storename) {
247
            super(
248
                "Can't update the feature '%(index)' in the store '%(storename)'",
249
                cause,
250
                "cant_update_the_feature_XindexX_in_the_store_XstorenameX",
251
                serialVersionUID);
252
            setValue("storename", storename);
253
            setValue("index", index);
254
        }
255
    }
256

    
257
    public void formAfterSave(JDynFormSet dynformSet)
258
        throws AbortActionException {
259

    
260
        LOGGER.info("formAfterSave");
261
        saveChanges(dynformSet);
262
    }
263

    
264
    private void saveChanges(JDynFormSet dynformSet)
265
        throws AbortActionException {
266
        int index = dynformSet.getCurrentIndex();
267
        DynObject dynObject = dynformSet.get(index);
268
        dynformSet.getFormValues(dynObject);
269
        Feature feat = store.getFeature(dynObject);
270
        if (feat == null) {
271
            LOGGER.warn("Can't get the associated feature index " + index);
272
            I18nManager i18nManager = ToolsLocator.getI18nManager();
273
            dynformSet.message(i18nManager
274
                .getTranslation("error_saving_data_will_not_save"));
275
            throw new GetFeatureException(null, index);
276
        }
277

    
278
        try {
279
            ph.update((EditableFeature) feat);
280
        } catch (BaseException e) {
281
            throw new UpdateFeatureException(null, index, store.getName());
282
        }
283
    }
284

    
285
    public void saveChanges() throws BaseException {
286
        if (this.formset != null && this.formset.countValues() > 0
287
            && this.formset.asJComponent().isVisible()) {
288
            this.saveChanges(this.formset);
289
        }
290
    }
291

    
292
    class FinishEditingException extends AbortActionException {
293

    
294
        /**
295
         *
296
         */
297
        private static final long serialVersionUID = -2281299673602420199L;
298

    
299
        public FinishEditingException(Throwable cause, String storename) {
300
            super("Can't finish the edition of the store '%(storename)'",
301
                cause, "cant_finish_the_edition_of_the_store_XstorenameX",
302
                serialVersionUID);
303
            setValue("storename", storename);
304
        }
305
    }
306

    
307
    public void formAfterNew(JDynFormSet dynformSet)
308
        throws AbortActionException {
309
        LOGGER.info("formAfterNew");
310
    }
311

    
312
    public void formAfterDelete(JDynFormSet dynformSet)
313
        throws AbortActionException {
314
        LOGGER.info("formAfterDelete");
315
    }
316

    
317
    public void formBeforeSearch(JDynFormSet dynformSet)
318
        throws AbortActionException {
319
        LOGGER.info("formBeforeSearch");
320
    }
321

    
322
    public void formAfterSearch(JDynFormSet dynformSet)
323
        throws AbortActionException {
324
        LOGGER.info("formAfterSearch");
325
    }
326

    
327
    public long getDataSetSize() {
328
        if (this.ph != null) {
329
            return ph.getTotalSize();
330
        }
331
        return 0;
332
    }
333
}