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