Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / project / document / table / FeatureTableDocument.java @ 27234

History | View | Annotate | Download (7.62 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {TableDocument implementation based on the gvSIG DAL API}
26
 */
27
package org.gvsig.project.document.table;
28

    
29
import java.util.ArrayList;
30
import java.util.Iterator;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.FeatureQuery;
34
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
39
import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel;
40
import org.gvsig.tools.evaluator.Evaluator;
41
import org.gvsig.tools.observer.Observable;
42
import org.gvsig.tools.observer.Observer;
43

    
44
import com.iver.andami.messages.NotificationManager;
45
import com.iver.andami.ui.mdiManager.IWindow;
46
import com.iver.cit.gvsig.project.Project;
47
import com.iver.cit.gvsig.project.documents.ProjectDocument;
48
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
49
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
50
import com.iver.utiles.XMLEntity;
51
import com.iver.utiles.XMLException;
52

    
53
/**
54
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
55
 */
56
public class FeatureTableDocument extends ProjectDocument implements Observer {
57

    
58
    private static final long serialVersionUID = -1842181135614158881L;
59

    
60
    private FeatureStore store;
61

    
62
        private String featureTypeId;
63

    
64
        private String[] attributeNames;
65

    
66
        private String linkTable;
67

    
68
        private String field1;
69

    
70
        private String field2;
71

    
72
        private LinkSelectionObserver linkSelectionObserver;
73

    
74
        private FLyrVect associatedTable;
75

    
76
        private FeatureQuery query;
77

    
78
        private Evaluator baseFilter;
79

    
80
        private FeatureQueryOrder baseOrder;
81

    
82

    
83

    
84
    public FeatureTableDocument(FeatureStore store) {
85
        this(store, store.createFeatureQuery());
86
    }
87

    
88
    public FeatureTableDocument(FeatureStore store, FeatureQuery query) {
89
        this.store = store;
90
        this.query = null;
91
        store.addObserver(this);
92
        this.featureTypeId = query.getFeatureTypeId();
93
        this.baseFilter = query.getFilter();
94
                this.baseOrder = query.getOrder();
95

    
96

    
97
    }
98

    
99
    public FeatureQuery getQuery(){
100
            if (this.query == null){
101
                    try {
102
                            FeatureType fType = null;
103
                                this.query = this.store.createFeatureQuery();
104
                                if (this.featureTypeId != null) {
105
                                        Iterator<FeatureType> iter;
106
                                        iter = this.store.getFeatureTypes().iterator();
107
                                        while (iter.hasNext()) {
108
                                                fType = iter.next();
109
                                                if (this.featureTypeId.equals(fType.getId())) {
110
                                                        this.query.setFeatureType(fType);
111
                                                        break;
112
                                                }
113
                                        }
114
                                    if (fType == null) {
115
                                                // TODO
116
                                            throw new RuntimeException("FeatureType '"
117
                                                                + this.featureTypeId + "'not found");
118
                                        }
119

    
120
                                } else {
121
                                        fType = store.getDefaultFeatureType();
122
                                }
123

    
124
                                if (this.attributeNames != null) {
125
                                        ArrayList<String> newNames = new ArrayList<String>();
126
                                        for (String name : this.attributeNames) {
127
                                                if (fType.getIndex(name) > -1) {
128
                                                        newNames.add(name);
129
                                                }
130
                                        }
131
                                        if (newNames.size() > 0) {
132
                                                this.query.setAttributeNames(newNames
133
                                                                .toArray(this.attributeNames));
134
                                        }
135
                                }
136

    
137
                                this.query.setFilter(this.baseFilter); // TODO check is valid
138
                                this.query.setOrder(this.baseOrder);
139

    
140

    
141
                    } catch (DataException e) {
142
                                // TODO Auto-generated catch block
143
                                NotificationManager.addError(e);
144
                                return null;
145
                        }
146

    
147
            }
148
            return this.query;
149
    }
150

    
151
    @Override
152
    public void afterAdd() {
153
        // Nothing to do
154
    }
155

    
156
    @Override
157
    public void afterRemove() {
158
        // Nothing to do
159
    }
160

    
161
    @Override
162
    public IWindow createWindow() {
163
        try {
164
                FeatureTableDocumentPanel tablePanel = new FeatureTableDocumentPanel(
165
                                        this);
166
                callCreateWindow(tablePanel);
167
                        return tablePanel;
168
        } catch (DataException e) {
169
            // TODO ?QU? SE HACE EN ESTOS CASOS?
170
            e.printStackTrace();
171
            NotificationManager.addError(e);
172
        }
173

    
174
        return null;
175
    }
176

    
177
    @Override
178
    public void exportToXML(XMLEntity root, Project project)
179
            throws SaveException {
180
        // TODO Auto-generated method stub
181

    
182
    }
183

    
184
    @Override
185
    public IWindow getProperties() {
186
        // TODO Auto-generated method stub
187
        return null;
188
    }
189

    
190
    /**
191
     * @return the store
192
     */
193
    public FeatureStore getStore() {
194
        return store;
195
    }
196

    
197
        @Override
198
        public void importFromXML(XMLEntity root, XMLEntity typeRoot,
199
                        int elementIndex, Project project, boolean removeDocumentsFromRoot)
200
                        throws XMLException, OpenException,
201
                        org.gvsig.fmap.dal.exception.ReadException {
202
                // TODO Auto-generated method stub
203

    
204
        }
205

    
206
        /**
207
         * Devuelve el identificador de la tabla que contiene el link.
208
         *
209
         * @return identificador ?nico de la tabla.
210
         */
211
        public String getLinkTable() {
212
                return linkTable;
213
        }
214

    
215
        /**
216
         * Devuelve el nombre del campo de la tabla a enlazar.
217
         *
218
         * @return Nombre del campo de la tabla a enlazar.
219
         */
220
        public String getField1() {
221
                return field1;
222
        }
223

    
224
        /**
225
         * Devuelve el nombre del campo de la tabla enlazada.
226
         *
227
         * @return Nombre del campo de la tabla enlazada.
228
         */
229
        public String getField2() {
230
                return field2;
231
        }
232

    
233
        /**
234
         * Inserta el identificador de la tabla, el campo de la primera tabla y el
235
         * campo de la segunda tabla.
236
         *
237
         * @param lt
238
         *            identificado de la tabla.
239
         * @param f1
240
         *            nombre del campo de la primera tabla.
241
         * @param f2
242
         *            nombre del campo de la segunda tabla.
243
         */
244
        public void setLinkTable(String lt, String f1, String f2) {
245
                linkTable = lt;
246
                field1 = f1;
247
                field2 = f2;
248
        }
249

    
250
        /**
251
         * Borra el identificador de la tabla y elimina del array de listener los
252
         * listener que sean del tipo: LinkSelectionObserver
253
         */
254
        public void removeLinkTable() {
255
                linkTable = null;
256
//                try {
257
                        getStore().deleteObserver(linkSelectionObserver);//removeLinksSelectionListener();
258
//                } catch (ReadException e) {
259
//                        e.printStackTrace();
260
//                }
261
        }
262
        public void addLinkSelectionObserver(LinkSelectionObserver lso) {
263
                linkSelectionObserver=lso;
264

    
265
        }
266

    
267
        public FLyrVect getAssociatedLayer() {
268
                return associatedTable;
269
        }
270

    
271
        /**
272
         * DOCUMENT ME!
273
         *
274
         * @param associatedTable
275
         *            DOCUMENT ME!
276
         */
277
        public void setAssociatedLayer(FLyrVect associatedTable) {
278
                this.associatedTable = associatedTable;
279
        }
280

    
281
        public void setStore(FeatureStore fs) {
282
                this.store=fs;
283
        }
284

    
285
        public void update(Observable arg0, Object arg1) {
286
                if (this.store.equals(arg0)) {
287
                        if (arg1 instanceof FeatureStoreNotification) {
288
                                FeatureStoreNotification event = (FeatureStoreNotification) arg1;
289
                                if (event.getType() == FeatureStoreNotification.TRANSFORM_CHANGE
290
                                                || event.getType() == FeatureStoreNotification.RESOURCE_CHANGED) {
291
                                        this.query = null;
292
                                }
293
                        }
294

    
295
                }
296

    
297
        }
298

    
299
}