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