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 @ 46301
History | View | Annotate | Download (19.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 |
|
31 |
import javax.swing.ImageIcon; |
32 |
import javax.swing.JOptionPane; |
33 |
|
34 |
import org.gvsig.andami.IconThemeHelper; |
35 |
import org.gvsig.andami.PluginServices; |
36 |
import org.gvsig.andami.messages.NotificationManager; |
37 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
38 |
import org.gvsig.app.ApplicationLocator; |
39 |
import org.gvsig.app.addlayer.AddLayerDialog; |
40 |
import org.gvsig.app.gui.WizardPanel; |
41 |
import org.gvsig.app.project.Project; |
42 |
import org.gvsig.app.project.ProjectManager; |
43 |
import org.gvsig.app.project.documents.AbstractDocument; |
44 |
import org.gvsig.app.project.documents.AbstractDocumentManager; |
45 |
import org.gvsig.app.project.documents.Document; |
46 |
import org.gvsig.app.project.documents.actions.CopyDocumentAction; |
47 |
import org.gvsig.app.project.documents.actions.CutDocumentAction; |
48 |
import org.gvsig.app.project.documents.actions.PasteDocumentAction; |
49 |
import org.gvsig.app.project.documents.gui.IDocumentWindow; |
50 |
import org.gvsig.app.project.documents.gui.WindowLayout; |
51 |
import org.gvsig.app.project.documents.table.TableDocument.TableLink; |
52 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
53 |
import org.gvsig.app.project.documents.table.gui.TableProperties; |
54 |
import org.gvsig.fmap.dal.AbstractStoresRepository; |
55 |
import org.gvsig.fmap.dal.DALLocator; |
56 |
import org.gvsig.fmap.dal.DataManager; |
57 |
import org.gvsig.fmap.dal.DataStoreParameters; |
58 |
import org.gvsig.fmap.dal.exception.InitializeException; |
59 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
60 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
61 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
62 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
63 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
64 |
import org.gvsig.fmap.dal.feature.FeatureType; |
65 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel; |
66 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
67 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.dynobject.DynObjectManager; |
70 |
import org.gvsig.tools.dynobject.DynStruct; |
71 |
import org.gvsig.tools.evaluator.Evaluator; |
72 |
import org.gvsig.tools.i18n.I18nManager; |
73 |
import org.gvsig.tools.persistence.PersistenceManager; |
74 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
75 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
76 |
import org.gvsig.tools.util.Invocable; |
77 |
import org.gvsig.tools.util.MappedIterator; |
78 |
import org.gvsig.tools.util.UnmodifiableBasicSet; |
79 |
import org.slf4j.LoggerFactory; |
80 |
|
81 |
/**
|
82 |
* Factory of Table.
|
83 |
*
|
84 |
* @author gvSIG Tem
|
85 |
*
|
86 |
*/
|
87 |
@SuppressWarnings("UseSpecificCatch") |
88 |
public class TableManager extends AbstractDocumentManager { |
89 |
|
90 |
final static private org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TableDocument.class); |
91 |
|
92 |
private static class TableDocumentStoresRepository extends AbstractStoresRepository { |
93 |
|
94 |
public TableDocumentStoresRepository(String name, String label) { |
95 |
super(name, label);
|
96 |
} |
97 |
|
98 |
@Override
|
99 |
protected DataStoreParameters getMyParameters(String name) { |
100 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
101 |
TableDocument tableDoc = (TableDocument) project.getDocument(name, TYPENAME); |
102 |
if( tableDoc==null || tableDoc.getFeatureStore()==null) { |
103 |
return null; |
104 |
} |
105 |
return tableDoc.getFeatureStore().getParameters();
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
protected boolean isEmptyMyRepository() { |
110 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
111 |
return project.getDocuments(TableManager.TYPENAME).isEmpty();
|
112 |
} |
113 |
|
114 |
@Override
|
115 |
protected int getMySize() { |
116 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
117 |
int sz = project.getDocuments(TableManager.TYPENAME).size();
|
118 |
return sz;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
protected UnmodifiableBasicSet<String> getMyKeySet() { |
123 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
124 |
final List<Document> docs = project.getDocuments(TableManager.TYPENAME); |
125 |
if( docs==null ) { |
126 |
return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
127 |
} |
128 |
UnmodifiableBasicSet<String> x = new UnmodifiableBasicSet<String>() { |
129 |
@Override
|
130 |
public boolean isEmpty() { |
131 |
return docs.isEmpty();
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public int size() { |
136 |
return docs.size();
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public Iterator<String> iterator() { |
141 |
return new MappedIterator<>(docs.iterator(), (Document t) -> { |
142 |
if( t==null ) { |
143 |
return null; |
144 |
} |
145 |
return ((TableDocument)t).getName();
|
146 |
}); |
147 |
} |
148 |
}; |
149 |
return x;
|
150 |
} |
151 |
|
152 |
|
153 |
@Override
|
154 |
public Iterator<DataStoreParameters> iterator() { |
155 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
156 |
List<Document> docs = project.getDocuments(TableManager.TYPENAME); |
157 |
if( docs==null ) { |
158 |
return Collections.EMPTY_LIST.iterator(); |
159 |
} |
160 |
return new MappedIterator<>(docs.iterator(), (Document t) -> { |
161 |
if( t==null || ((TableDocument)t).getFeatureStore()==null ) { |
162 |
return null; |
163 |
} |
164 |
return ((TableDocument)t).getFeatureStore().getParameters();
|
165 |
}); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public void add(String name, DataStoreParameters parameters) { |
170 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
171 |
TableDocument doc = (TableDocument) ProjectManager.getInstance().createDocument(TYPENAME); |
172 |
doc.setName(name); |
173 |
//FIXME:
|
174 |
DataManager dataManager = DALLocator.getDataManager(); |
175 |
FeatureStore store; |
176 |
try {
|
177 |
store = (FeatureStore) dataManager.openStore( |
178 |
parameters.getProviderName(), |
179 |
parameters |
180 |
); |
181 |
doc.setStore(store); |
182 |
project.addDocument(doc); |
183 |
} catch (InitializeException | ProviderNotRegisteredException | ValidateDataParametersException ex) {
|
184 |
LOGGER.warn("Can't add table to project", ex);
|
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
@Override
|
190 |
public void add(String name, FeatureStore store) { |
191 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
192 |
TableDocument doc = (TableDocument) ProjectManager.getInstance().createDocument(TYPENAME); |
193 |
doc.setName(name); |
194 |
doc.setStore(store); |
195 |
project.addDocument(doc); |
196 |
} |
197 |
} |
198 |
|
199 |
public static final String PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME = |
200 |
"TableDocument";
|
201 |
public static final String PERSISTENCE_TABLELINK_DEFINITION_NAME = |
202 |
"TableLink";
|
203 |
|
204 |
public static String TYPENAME = "project.document.table"; |
205 |
|
206 |
private DynStruct persistenceDefinition = null; |
207 |
|
208 |
@Override
|
209 |
public ImageIcon getIcon() { |
210 |
return IconThemeHelper.getImageIcon("document-table-icon"); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public ImageIcon getIconSelected() { |
215 |
return IconThemeHelper.getImageIcon("document-table-icon-sel"); |
216 |
} |
217 |
|
218 |
@Override
|
219 |
public String getTitle() { |
220 |
return PluginServices.getText(this, "Tabla"); |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public String getTypeName() { |
225 |
return TYPENAME;
|
226 |
} |
227 |
|
228 |
@Override
|
229 |
public int getPriority() { |
230 |
return 1; |
231 |
} |
232 |
|
233 |
@Override
|
234 |
public Iterator<? extends Document> createDocumentsByUser() { |
235 |
return this.createDocumentsByUser(null); |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public Iterator<? extends Document> createDocumentsByUser(Invocable whenDocumentsLoaded) { |
240 |
AddLayerDialog fopen = null;
|
241 |
try {
|
242 |
fopen = |
243 |
new AddLayerDialog(PluginServices.getText(this, "Nueva_tabla")); |
244 |
List<WizardPanel> wizards =
|
245 |
ApplicationLocator.getManager().getWizardPanels(); |
246 |
WizardPanel panel; |
247 |
Iterator<WizardPanel> iter = wizards.iterator();
|
248 |
while (iter.hasNext()) {
|
249 |
panel = iter.next(); |
250 |
panel.initWizard(); |
251 |
fopen.addWizardTab(panel.getTabName(), panel); |
252 |
} |
253 |
PluginServices.getMDIManager().addWindow(fopen); |
254 |
if (fopen.isAccepted()) {
|
255 |
WizardPanel selectedPanel = (WizardPanel) fopen.getSelectedTab(); |
256 |
if( whenDocumentsLoaded==null ) { |
257 |
List<TableDocument> docs = (List) selectedPanel.executeWizard(); |
258 |
this.checkDocuments(docs);
|
259 |
return docs.iterator();
|
260 |
} |
261 |
Thread task = new Thread(() -> { |
262 |
List<TableDocument> docs = (List) selectedPanel.executeWizard(); |
263 |
checkDocuments(docs); |
264 |
whenDocumentsLoaded.call(docs.iterator()); |
265 |
}, "DocumentTableLoader");
|
266 |
task.start(); |
267 |
return Collections.EMPTY_LIST.iterator(); |
268 |
} |
269 |
} catch (Exception e) { |
270 |
NotificationManager.addError(e); |
271 |
} finally {
|
272 |
if (fopen != null) { |
273 |
fopen.dispose(); |
274 |
fopen = null;
|
275 |
} |
276 |
} |
277 |
return null; |
278 |
} |
279 |
|
280 |
private void checkDocuments(List<TableDocument> docs) { |
281 |
if( docs==null ) { |
282 |
return;
|
283 |
} |
284 |
for (TableDocument doc : docs) {
|
285 |
try {
|
286 |
FeatureStore store = ((TableDocument)doc).getDataStore(); |
287 |
FeatureType type = store.getDefaultFeatureType(); |
288 |
if( ! type.supportReferences() ) {
|
289 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
290 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
291 |
dialogs.messageDialog( |
292 |
"\""+ doc.getName() + "\"\n"+ |
293 |
i18n.getTranslation("_The_table_has_no_primary_key_or_OID") +"\n" + |
294 |
i18n.getTranslation("_Many_features_selection_deletion_modification_will_not_be_available_as_they_require_it_for_proper_operation"),
|
295 |
null,
|
296 |
i18n.getTranslation("_Warning"),
|
297 |
JOptionPane.WARNING_MESSAGE,
|
298 |
"TableDoNotSupportReferences"
|
299 |
); |
300 |
} |
301 |
} catch (Exception ex) { |
302 |
|
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public AbstractDocument createDocumentByUser() { |
309 |
return (AbstractDocument) createDocumentsByUser().next(); |
310 |
} |
311 |
|
312 |
/**
|
313 |
* Registers in the points of extension the Factory with alias.
|
314 |
*
|
315 |
*/
|
316 |
public static void register() { |
317 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
318 |
|
319 |
TableManager factory = new TableManager();
|
320 |
// A?adimos nuestra extension para el tratamiento de la apertura de
|
321 |
// ficheros
|
322 |
// dentro de gvSIG
|
323 |
ToolsLocator.getExtensionPointManager().add("FileTableOpenDialog", "") |
324 |
.append("FileOpenTable", "", FilesystemExplorerWizardPanel.class); |
325 |
|
326 |
IconThemeHelper.registerIcon("document", "document-table-icon", TableManager.class); |
327 |
IconThemeHelper.registerIcon("document", "document-table-icon-sel", TableManager.class); |
328 |
IconThemeHelper.registerIcon("document", "document-table-icon-small", TableManager.class); |
329 |
|
330 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
331 |
manager.registerFactory(factory); |
332 |
|
333 |
if (factory.persistenceDefinition == null) { |
334 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
335 |
factory.persistenceDefinition = |
336 |
dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE, |
337 |
PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME, |
338 |
"Table document Persistence definition");
|
339 |
factory.persistenceDefinition.extend(manager |
340 |
.getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
|
341 |
|
342 |
factory.persistenceDefinition.addDynFieldObject("store")
|
343 |
.setClassOfValue(FeatureStore.class).setMandatory(true);
|
344 |
factory.persistenceDefinition.addDynFieldString("featureTypeId")
|
345 |
.setMandatory(false);
|
346 |
factory.persistenceDefinition.addDynFieldArray("attributeNames")
|
347 |
.setClassOfItems(String.class).setMandatory(false); |
348 |
factory.persistenceDefinition.addDynFieldObject("associatedLayer")
|
349 |
.setClassOfValue(FLyrVect.class).setMandatory(false);
|
350 |
factory.persistenceDefinition.addDynFieldObject("query")
|
351 |
.setClassOfValue(FeatureQuery.class).setMandatory(false);
|
352 |
factory.persistenceDefinition.addDynFieldObject("baseFilter")
|
353 |
.setClassOfValue(Evaluator.class).setMandatory(false);
|
354 |
factory.persistenceDefinition.addDynFieldObject("baseOrder")
|
355 |
.setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
|
356 |
factory.persistenceDefinition.addDynFieldList("linkTable")
|
357 |
.setClassOfItems(TableLink.class).setMandatory(false);
|
358 |
factory.persistenceDefinition.addDynFieldMap("patterns")
|
359 |
.setClassOfItems(String.class).setMandatory(false); |
360 |
} |
361 |
|
362 |
|
363 |
//Register also the TableLink
|
364 |
if (manager.getDefinition(PERSISTENCE_TABLELINK_DEFINITION_NAME) == null){ |
365 |
DynStruct tableLinkDefinition = |
366 |
manager.addDefinition(TableLink.class, |
367 |
PERSISTENCE_TABLELINK_DEFINITION_NAME, |
368 |
"TableLink Persistence definition", null, null); |
369 |
|
370 |
tableLinkDefinition.addDynFieldObject("source")
|
371 |
.setClassOfValue(TableDocument.class).setMandatory(true);
|
372 |
tableLinkDefinition.addDynFieldObject("target")
|
373 |
.setClassOfValue(TableDocument.class).setMandatory(true);
|
374 |
tableLinkDefinition.addDynFieldString("fieldSource").setMandatory(true); |
375 |
tableLinkDefinition.addDynFieldString("fieldTarget").setMandatory(true); |
376 |
tableLinkDefinition.addDynFieldBoolean("enabled").setMandatory(true); |
377 |
} |
378 |
|
379 |
ProjectManager.getInstance().registerDocumentFactory(factory); |
380 |
|
381 |
ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
|
382 |
ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
|
383 |
ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
|
384 |
|
385 |
DataManager dataManager = DALLocator.getDataManager(); |
386 |
dataManager.getStoresRepository().addRepository( |
387 |
new TableDocumentStoresRepository(
|
388 |
"PROJECT_TABLES",
|
389 |
i18n.getTranslation("_Project_tables")
|
390 |
) |
391 |
); |
392 |
} |
393 |
|
394 |
/**
|
395 |
* Create a new table document.
|
396 |
*
|
397 |
* @return TableDocument.
|
398 |
*/
|
399 |
@Override
|
400 |
public AbstractDocument createDocument() { |
401 |
AbstractDocument doc = new TableDocument(this); |
402 |
if( this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT,doc).isCanceled() ) { |
403 |
return null; |
404 |
} |
405 |
return doc;
|
406 |
} |
407 |
|
408 |
@Override
|
409 |
public Class<? extends IDocumentWindow> getMainWindowClass() { |
410 |
return FeatureTableDocumentPanel.class;
|
411 |
} |
412 |
|
413 |
@Override
|
414 |
public IWindow getMainWindow(Document doc, WindowLayout layout) { |
415 |
IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
|
416 |
if (win == null) { |
417 |
win = (IDocumentWindow) this.createDocumentWindow(doc);
|
418 |
if (layout != null && win != null) { |
419 |
win.setWindowLayout(layout); |
420 |
} |
421 |
} |
422 |
if( this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW,win).isCanceled() ) { |
423 |
return null; |
424 |
} |
425 |
return win;
|
426 |
} |
427 |
|
428 |
@Override
|
429 |
public IWindow getPropertiesWindow(Document doc) { |
430 |
IWindow win = super.getPropertiesWindow(doc);
|
431 |
if( win == null ) { |
432 |
win = new TableProperties((TableDocument) doc);
|
433 |
} |
434 |
if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) { |
435 |
return null; |
436 |
} |
437 |
return win;
|
438 |
} |
439 |
|
440 |
@Override
|
441 |
protected Class<? extends Document> getDocumentClass() { |
442 |
return TableDocument.class;
|
443 |
} |
444 |
|
445 |
@Override
|
446 |
public DynStruct getDefinition(String className) { |
447 |
|
448 |
if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) { |
449 |
return this.persistenceDefinition; |
450 |
} |
451 |
if (this.persistenceDefinition.getFullName() |
452 |
.equalsIgnoreCase(className)) { |
453 |
return this.persistenceDefinition; |
454 |
} |
455 |
if (this.getDocumentClass().getName().equals(className)) { |
456 |
return this.persistenceDefinition; |
457 |
} |
458 |
return null; |
459 |
} |
460 |
|
461 |
@Override
|
462 |
public boolean manages(Object object) { |
463 |
return object instanceof TableDocument; |
464 |
} |
465 |
|
466 |
public TableDocument getTableDocument(VectorLayer layer) {
|
467 |
if (layer == null) { |
468 |
return null; |
469 |
} |
470 |
List<Document> tableDocs = |
471 |
getProject().getDocuments(TableManager.TYPENAME); |
472 |
for (Document document : tableDocs) { |
473 |
if (layer == ((TableDocument) document).getAssociatedLayer()) {
|
474 |
return (TableDocument) document;
|
475 |
} |
476 |
} |
477 |
return null; |
478 |
} |
479 |
|
480 |
public void removeTableDocument(VectorLayer layer) { |
481 |
TableDocument doc = getTableDocument(layer); |
482 |
// Only remove it if it exists
|
483 |
if (doc != null) { |
484 |
PluginServices.getMDIManager().closeSingletonWindow(doc); |
485 |
getProject().remove(doc); |
486 |
} |
487 |
} |
488 |
|
489 |
private Project getProject() {
|
490 |
return ProjectManager.getInstance().getCurrentProject();
|
491 |
} |
492 |
} |