svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / TableManager.java @ 44262
History | View | Annotate | Download (15.4 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.app.project.documents.table; |
26 |
|
27 |
import java.util.Collections; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import java.util.function.Function; |
32 |
|
33 |
import javax.swing.ImageIcon; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
|
36 |
import org.gvsig.andami.IconThemeHelper; |
37 |
import org.gvsig.andami.PluginServices; |
38 |
import org.gvsig.andami.messages.NotificationManager; |
39 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
40 |
import org.gvsig.app.ApplicationLocator; |
41 |
import org.gvsig.app.addlayer.AddLayerDialog; |
42 |
import org.gvsig.app.gui.WizardPanel; |
43 |
import org.gvsig.app.project.Project; |
44 |
import org.gvsig.app.project.ProjectManager; |
45 |
import org.gvsig.app.project.documents.AbstractDocument; |
46 |
import org.gvsig.app.project.documents.AbstractDocumentManager; |
47 |
import org.gvsig.app.project.documents.Document; |
48 |
import org.gvsig.app.project.documents.actions.CopyDocumentAction; |
49 |
import org.gvsig.app.project.documents.actions.CutDocumentAction; |
50 |
import org.gvsig.app.project.documents.actions.PasteDocumentAction; |
51 |
import org.gvsig.app.project.documents.gui.IDocumentWindow; |
52 |
import org.gvsig.app.project.documents.gui.WindowLayout; |
53 |
import org.gvsig.app.project.documents.table.TableDocument.TableLink; |
54 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
55 |
import org.gvsig.app.project.documents.table.gui.TableProperties; |
56 |
import org.gvsig.fmap.dal.DALLocator; |
57 |
import org.gvsig.fmap.dal.DataManager; |
58 |
import org.gvsig.fmap.dal.DataStoreParameters; |
59 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
60 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
61 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
62 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel; |
63 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
64 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.dynobject.DynObjectManager; |
67 |
import org.gvsig.tools.dynobject.DynStruct; |
68 |
import org.gvsig.tools.evaluator.Evaluator; |
69 |
import org.gvsig.tools.persistence.PersistenceManager; |
70 |
import org.gvsig.tools.util.MappedIterator; |
71 |
import org.gvsig.tools.util.UnmodifiableBasicMap; |
72 |
import org.gvsig.tools.util.UnmodifiableBasicSet; |
73 |
import org.gvsig.tools.util.UnmodifiableBasicSetMapped; |
74 |
|
75 |
/**
|
76 |
* Factory of Table.
|
77 |
*
|
78 |
* @author 2005- Vicente Caballero
|
79 |
* @author 2009- Joaquin del Cerro
|
80 |
*
|
81 |
*/
|
82 |
public class TableManager extends AbstractDocumentManager { |
83 |
|
84 |
private static class TableDocumentStoresRepository implements UnmodifiableBasicMap<String,DataStoreParameters> { |
85 |
|
86 |
@Override
|
87 |
public DataStoreParameters get(String name) { |
88 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
89 |
TableDocument tableDoc = (TableDocument) project.getDocument(name, TYPENAME); |
90 |
if( tableDoc==null ) { |
91 |
return null; |
92 |
} |
93 |
return tableDoc.getFeatureStore().getParameters();
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public boolean containsKey(String key) { |
98 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
99 |
for (Document document : project.getDocuments(TableManager.TYPENAME)) { |
100 |
if( StringUtils.equalsIgnoreCase(key, document.getName()) ) {
|
101 |
return true; |
102 |
} |
103 |
} |
104 |
return false; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public UnmodifiableBasicSet<String> keySet() { |
109 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
110 |
List<Document> docs = project.getDocuments(TableManager.TYPENAME); |
111 |
if( docs==null ) { |
112 |
return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
113 |
} |
114 |
return new UnmodifiableBasicSetMapped<>(docs, new Function<Document, String>() { |
115 |
@Override
|
116 |
public String apply(Document t) { |
117 |
return t.getName();
|
118 |
} |
119 |
}); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public Map<String, DataStoreParameters> toMap() { |
124 |
return null; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public boolean isEmpty() { |
129 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
130 |
return project.getDocuments(TableManager.TYPENAME).isEmpty();
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public int size() { |
135 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
136 |
int sz = project.getDocuments(TableManager.TYPENAME).size();
|
137 |
return sz;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public Iterator<DataStoreParameters> iterator() { |
142 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
143 |
List<Document> docs = project.getDocuments(TableManager.TYPENAME); |
144 |
if( docs==null ) { |
145 |
return Collections.EMPTY_LIST.iterator(); |
146 |
} |
147 |
return new MappedIterator<>(docs.iterator(), new Function<Document, DataStoreParameters>() { |
148 |
@Override
|
149 |
public DataStoreParameters apply(Document t) { |
150 |
return ((TableDocument)t).getFeatureStore().getParameters();
|
151 |
} |
152 |
}); |
153 |
} |
154 |
|
155 |
|
156 |
} |
157 |
|
158 |
public static final String PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME = |
159 |
"TableDocument";
|
160 |
public static final String PERSISTENCE_TABLELINK_DEFINITION_NAME = |
161 |
"TableLink";
|
162 |
|
163 |
public static String TYPENAME = "project.document.table"; |
164 |
|
165 |
private DynStruct persistenceDefinition = null; |
166 |
|
167 |
public ImageIcon getIcon() { |
168 |
return IconThemeHelper.getImageIcon("document-table-icon"); |
169 |
} |
170 |
|
171 |
public ImageIcon getIconSelected() { |
172 |
return IconThemeHelper.getImageIcon("document-table-icon-sel"); |
173 |
} |
174 |
|
175 |
public String getTitle() { |
176 |
return PluginServices.getText(this, "Tabla"); |
177 |
} |
178 |
|
179 |
public String getTypeName() { |
180 |
return TYPENAME;
|
181 |
} |
182 |
|
183 |
public int getPriority() { |
184 |
return 1; |
185 |
} |
186 |
|
187 |
public Iterator<? extends Document> createDocumentsByUser() { |
188 |
AddLayerDialog fopen = null;
|
189 |
try {
|
190 |
fopen = |
191 |
new AddLayerDialog(PluginServices.getText(this, "Nueva_tabla")); |
192 |
List<WizardPanel> wizards =
|
193 |
ApplicationLocator.getManager().getWizardPanels(); |
194 |
WizardPanel panel; |
195 |
Iterator<WizardPanel> iter = wizards.iterator();
|
196 |
while (iter.hasNext()) {
|
197 |
panel = iter.next(); |
198 |
fopen.addWizardTab(panel.getTabName(), panel); |
199 |
panel.initWizard(); |
200 |
} |
201 |
PluginServices.getMDIManager().addWindow(fopen); |
202 |
if (fopen.isAccepted()) {
|
203 |
panel = (WizardPanel) fopen.getSelectedTab(); |
204 |
@SuppressWarnings("unchecked") |
205 |
List<TableDocument> docs =
|
206 |
(List<TableDocument>) panel.executeWizard();
|
207 |
return docs.iterator();
|
208 |
} |
209 |
} catch (Exception e) { |
210 |
NotificationManager.addError(e); |
211 |
} finally {
|
212 |
if (fopen != null) { |
213 |
fopen.dispose(); |
214 |
fopen = null;
|
215 |
} |
216 |
} |
217 |
return null; |
218 |
} |
219 |
|
220 |
public AbstractDocument createDocumentByUser() { |
221 |
return (AbstractDocument) createDocumentsByUser().next(); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Registers in the points of extension the Factory with alias.
|
226 |
*
|
227 |
*/
|
228 |
public static void register() { |
229 |
|
230 |
TableManager factory = new TableManager();
|
231 |
// A?adimos nuestra extension para el tratamiento de la apertura de
|
232 |
// ficheros
|
233 |
// dentro de gvSIG
|
234 |
ToolsLocator.getExtensionPointManager().add("FileTableOpenDialog", "") |
235 |
.append("FileOpenTable", "", FilesystemExplorerWizardPanel.class); |
236 |
|
237 |
IconThemeHelper.registerIcon("document", "document-table-icon", TableManager.class); |
238 |
IconThemeHelper.registerIcon("document", "document-table-icon-sel", TableManager.class); |
239 |
IconThemeHelper.registerIcon("document", "document-table-icon-small", TableManager.class); |
240 |
|
241 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
242 |
manager.registerFactory(factory); |
243 |
|
244 |
if (factory.persistenceDefinition == null) { |
245 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
246 |
factory.persistenceDefinition = |
247 |
dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE, |
248 |
PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME, |
249 |
"Table document Persistence definition");
|
250 |
factory.persistenceDefinition.extend(manager |
251 |
.getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
|
252 |
|
253 |
factory.persistenceDefinition.addDynFieldObject("store")
|
254 |
.setClassOfValue(FeatureStore.class).setMandatory(true);
|
255 |
factory.persistenceDefinition.addDynFieldString("featureTypeId")
|
256 |
.setMandatory(false);
|
257 |
factory.persistenceDefinition.addDynFieldArray("attributeNames")
|
258 |
.setClassOfItems(String.class).setMandatory(false); |
259 |
factory.persistenceDefinition.addDynFieldObject("associatedLayer")
|
260 |
.setClassOfValue(FLyrVect.class).setMandatory(false);
|
261 |
factory.persistenceDefinition.addDynFieldObject("query")
|
262 |
.setClassOfValue(FeatureQuery.class).setMandatory(false);
|
263 |
factory.persistenceDefinition.addDynFieldObject("baseFilter")
|
264 |
.setClassOfValue(Evaluator.class).setMandatory(false);
|
265 |
factory.persistenceDefinition.addDynFieldObject("baseOrder")
|
266 |
.setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
|
267 |
factory.persistenceDefinition.addDynFieldList("linkTable")
|
268 |
.setClassOfItems(TableLink.class).setMandatory(false);
|
269 |
factory.persistenceDefinition.addDynFieldMap("patterns")
|
270 |
.setClassOfItems(String.class).setMandatory(false); |
271 |
} |
272 |
|
273 |
|
274 |
//Register also the TableLink
|
275 |
if (manager.getDefinition(PERSISTENCE_TABLELINK_DEFINITION_NAME) == null){ |
276 |
DynStruct tableLinkDefinition = |
277 |
manager.addDefinition(TableLink.class, |
278 |
PERSISTENCE_TABLELINK_DEFINITION_NAME, |
279 |
"TableLink Persistence definition", null, null); |
280 |
|
281 |
tableLinkDefinition.addDynFieldObject("source")
|
282 |
.setClassOfValue(TableDocument.class).setMandatory(true);
|
283 |
tableLinkDefinition.addDynFieldObject("target")
|
284 |
.setClassOfValue(TableDocument.class).setMandatory(true);
|
285 |
tableLinkDefinition.addDynFieldString("fieldSource").setMandatory(true); |
286 |
tableLinkDefinition.addDynFieldString("fieldTarget").setMandatory(true); |
287 |
tableLinkDefinition.addDynFieldBoolean("enabled").setMandatory(true); |
288 |
} |
289 |
|
290 |
ProjectManager.getInstance().registerDocumentFactory(factory); |
291 |
|
292 |
ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
|
293 |
ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
|
294 |
ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
|
295 |
|
296 |
DataManager dataManager = DALLocator.getDataManager(); |
297 |
dataManager.addStoresRepository("TableDocuments", new TableDocumentStoresRepository()); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Create a new table document.
|
302 |
*
|
303 |
* @return TableDocument.
|
304 |
*/
|
305 |
public AbstractDocument createDocument() { |
306 |
AbstractDocument doc = new TableDocument(this); |
307 |
if( this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT,doc).isCanceled() ) { |
308 |
return null; |
309 |
} |
310 |
return doc;
|
311 |
} |
312 |
|
313 |
public Class<? extends IDocumentWindow> getMainWindowClass() { |
314 |
return FeatureTableDocumentPanel.class;
|
315 |
} |
316 |
|
317 |
@Override
|
318 |
public IWindow getMainWindow(Document doc, WindowLayout layout) { |
319 |
IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
|
320 |
if (win == null) { |
321 |
win = (IDocumentWindow) this.createDocumentWindow(doc);
|
322 |
if (layout != null && win != null) { |
323 |
win.setWindowLayout(layout); |
324 |
} |
325 |
} |
326 |
if( this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW,win).isCanceled() ) { |
327 |
return null; |
328 |
} |
329 |
return win;
|
330 |
} |
331 |
|
332 |
@Override
|
333 |
public IWindow getPropertiesWindow(Document doc) { |
334 |
IWindow win = super.getPropertiesWindow(doc);
|
335 |
if( win == null ) { |
336 |
win = new TableProperties((TableDocument) doc);
|
337 |
} |
338 |
if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) { |
339 |
return null; |
340 |
} |
341 |
return win;
|
342 |
} |
343 |
|
344 |
protected Class<? extends Document> getDocumentClass() { |
345 |
return TableDocument.class;
|
346 |
} |
347 |
|
348 |
public DynStruct getDefinition(String className) { |
349 |
|
350 |
if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) { |
351 |
return this.persistenceDefinition; |
352 |
} |
353 |
if (this.persistenceDefinition.getFullName() |
354 |
.equalsIgnoreCase(className)) { |
355 |
return this.persistenceDefinition; |
356 |
} |
357 |
if (this.getDocumentClass().getName().equals(className)) { |
358 |
return this.persistenceDefinition; |
359 |
} |
360 |
return null; |
361 |
} |
362 |
|
363 |
public boolean manages(Object object) { |
364 |
return object instanceof TableDocument; |
365 |
} |
366 |
|
367 |
public TableDocument getTableDocument(VectorLayer layer) {
|
368 |
if (layer == null) { |
369 |
return null; |
370 |
} |
371 |
List<Document> tableDocs = |
372 |
getProject().getDocuments(TableManager.TYPENAME); |
373 |
for (Document document : tableDocs) { |
374 |
if (layer == ((TableDocument) document).getAssociatedLayer()) {
|
375 |
return (TableDocument) document;
|
376 |
} |
377 |
} |
378 |
return null; |
379 |
} |
380 |
|
381 |
public void removeTableDocument(VectorLayer layer) { |
382 |
TableDocument doc = getTableDocument(layer); |
383 |
// Only remove it if it exists
|
384 |
if (doc != null) { |
385 |
PluginServices.getMDIManager().closeSingletonWindow(doc); |
386 |
getProject().remove(doc); |
387 |
} |
388 |
} |
389 |
|
390 |
private Project getProject() {
|
391 |
return ProjectManager.getInstance().getCurrentProject();
|
392 |
} |
393 |
} |