svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / DefaultProject.java @ 47790
History | View | Annotate | Download (51.9 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40558 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 41148 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40558 | jjdelcerro | *
|
11 | 41148 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40558 | jjdelcerro | *
|
16 | 41148 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40558 | jjdelcerro | *
|
20 | 41148 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.app.project; |
||
24 | |||
25 | import java.awt.Color; |
||
26 | 43913 | jjdelcerro | import java.awt.image.BufferedImage; |
27 | 40435 | jjdelcerro | import java.beans.PropertyChangeEvent; |
28 | import java.beans.PropertyChangeListener; |
||
29 | import java.beans.PropertyChangeSupport; |
||
30 | import java.io.File; |
||
31 | import java.io.FileInputStream; |
||
32 | import java.io.FileNotFoundException; |
||
33 | import java.io.FileOutputStream; |
||
34 | 43820 | jjdelcerro | import java.io.IOException; |
35 | 40435 | jjdelcerro | import java.io.InputStream; |
36 | import java.io.OutputStream; |
||
37 | import java.io.Serializable; |
||
38 | import java.text.DateFormat; |
||
39 | import java.text.MessageFormat; |
||
40 | 44088 | omartinez | import java.text.SimpleDateFormat; |
41 | 40435 | jjdelcerro | import java.util.ArrayList; |
42 | import java.util.Collections; |
||
43 | import java.util.Date; |
||
44 | import java.util.HashMap; |
||
45 | 42711 | fdiaz | import java.util.HashSet; |
46 | 40435 | jjdelcerro | import java.util.Iterator; |
47 | import java.util.List; |
||
48 | import java.util.Map; |
||
49 | 42711 | fdiaz | import java.util.Set; |
50 | 46296 | jjdelcerro | import java.util.function.Predicate; |
51 | 43913 | jjdelcerro | import java.util.zip.ZipEntry; |
52 | 43820 | jjdelcerro | import java.util.zip.ZipException; |
53 | import java.util.zip.ZipFile; |
||
54 | 43913 | jjdelcerro | import java.util.zip.ZipOutputStream; |
55 | import javax.imageio.ImageIO; |
||
56 | 44485 | jjdelcerro | import javax.swing.JOptionPane; |
57 | 46461 | jjdelcerro | import javax.swing.SwingUtilities; |
58 | 44088 | omartinez | import org.apache.commons.io.FileUtils; |
59 | import org.apache.commons.io.FilenameUtils; |
||
60 | 43913 | jjdelcerro | import org.apache.commons.io.IOUtils; |
61 | 45814 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
62 | 40435 | jjdelcerro | import org.cresques.cts.IProjection; |
63 | import org.gvsig.andami.PluginServices; |
||
64 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
65 | import org.gvsig.andami.ui.mdiManager.MDIManager; |
||
66 | import org.gvsig.andami.ui.mdiManager.SingletonWindow; |
||
67 | import org.gvsig.andami.ui.mdiManager.WindowInfo; |
||
68 | import org.gvsig.app.ApplicationLocator; |
||
69 | import org.gvsig.app.ApplicationManager; |
||
70 | import org.gvsig.app.extension.ProjectExtension; |
||
71 | import org.gvsig.app.extension.Version; |
||
72 | 45978 | jjdelcerro | import org.gvsig.app.project.ProjectManager.ProjectEventImpl; |
73 | 40435 | jjdelcerro | import org.gvsig.app.project.documents.AbstractDocument; |
74 | import org.gvsig.app.project.documents.Document; |
||
75 | 47236 | jjdelcerro | import org.gvsig.app.project.documents.DocumentManager; |
76 | 40435 | jjdelcerro | import org.gvsig.app.project.documents.exceptions.SaveException; |
77 | import org.gvsig.app.project.documents.gui.IDocumentWindow; |
||
78 | import org.gvsig.app.project.documents.gui.ProjectWindow; |
||
79 | import org.gvsig.app.project.documents.view.DefaultViewDocument; |
||
80 | import org.gvsig.app.project.documents.view.ViewManager; |
||
81 | 41350 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper; |
82 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.FLayer; |
83 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
84 | import org.gvsig.tools.ToolsLocator; |
||
85 | 47219 | fdiaz | import org.gvsig.tools.dispose.DisposeUtils; |
86 | 40435 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
87 | 44485 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
88 | 40435 | jjdelcerro | import org.gvsig.tools.observer.ObservableHelper; |
89 | import org.gvsig.tools.observer.Observer; |
||
90 | import org.gvsig.tools.persistence.PersistenceManager; |
||
91 | import org.gvsig.tools.persistence.Persistent; |
||
92 | 42711 | fdiaz | import org.gvsig.tools.persistence.PersistentContext; |
93 | 40435 | jjdelcerro | import org.gvsig.tools.persistence.PersistentState; |
94 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
95 | 44485 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
96 | import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
||
97 | 40435 | jjdelcerro | import org.gvsig.utils.StringUtilities; |
98 | import org.slf4j.Logger; |
||
99 | import org.slf4j.LoggerFactory; |
||
100 | |||
101 | /**
|
||
102 | * Clase que representa un proyecto de gvSIG
|
||
103 | 41148 | jjdelcerro | *
|
104 | 40435 | jjdelcerro | */
|
105 | public class DefaultProject implements Serializable, PropertyChangeListener, |
||
106 | 41148 | jjdelcerro | Project { |
107 | 40435 | jjdelcerro | |
108 | 41148 | jjdelcerro | private Logger LOG = LoggerFactory.getLogger(DefaultProject.class); |
109 | /**
|
||
110 | * @deprecated see ApplicationLocator.getManager().getVersion()
|
||
111 | */
|
||
112 | public static String VERSION = Version.format(); |
||
113 | 40435 | jjdelcerro | |
114 | 41350 | jjdelcerro | private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper(); |
115 | 42088 | fdiaz | |
116 | 40435 | jjdelcerro | private ObservableHelper observableHelper = new ObservableHelper(); |
117 | |||
118 | 41148 | jjdelcerro | /**
|
119 | *
|
||
120 | */
|
||
121 | private static final long serialVersionUID = -4449622027521773178L; |
||
122 | 40435 | jjdelcerro | |
123 | 41148 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(Project.class); |
124 | 40435 | jjdelcerro | |
125 | 41148 | jjdelcerro | private static ProjectPreferences preferences = new ProjectPreferences(); |
126 | 40435 | jjdelcerro | |
127 | 41148 | jjdelcerro | /**
|
128 | * Index used by the generator of unique names of documents.
|
||
129 | */
|
||
130 | 45978 | jjdelcerro | private Map<String, Integer> nextDocumentIndexByType = new HashMap<>(); |
131 | 40435 | jjdelcerro | |
132 | 41148 | jjdelcerro | private PropertyChangeSupport change; |
133 | 40435 | jjdelcerro | |
134 | 41148 | jjdelcerro | private boolean modified = false; |
135 | 40435 | jjdelcerro | |
136 | 41148 | jjdelcerro | private String name = null; |
137 | 40435 | jjdelcerro | |
138 | 41148 | jjdelcerro | private String creationDate = null; |
139 | 40435 | jjdelcerro | |
140 | 41148 | jjdelcerro | private String modificationDate = null; |
141 | 40435 | jjdelcerro | |
142 | 41148 | jjdelcerro | private String owner = null; |
143 | 40435 | jjdelcerro | |
144 | 41148 | jjdelcerro | private String comments = null; |
145 | 40435 | jjdelcerro | |
146 | 41148 | jjdelcerro | private Color selectionColor = null; |
147 | 40435 | jjdelcerro | |
148 | 41148 | jjdelcerro | private List<Document> documents = null; |
149 | 40435 | jjdelcerro | |
150 | 41148 | jjdelcerro | private List<ProjectExtent> extents = null; |
151 | 40435 | jjdelcerro | |
152 | 41148 | jjdelcerro | private IProjection projection;
|
153 | 40435 | jjdelcerro | |
154 | 42466 | jjdelcerro | private File fname = null; |
155 | 42711 | fdiaz | |
156 | private Set<String> unloadedObjects; |
||
157 | 43439 | jjdelcerro | private PersistenceException loadErrors = null; |
158 | 42711 | fdiaz | |
159 | 41148 | jjdelcerro | /**
|
160 | * Creates a new Project object.
|
||
161 | */
|
||
162 | DefaultProject() { |
||
163 | this.change = new PropertyChangeSupport(this); |
||
164 | this.clean();
|
||
165 | 45978 | jjdelcerro | notifyProjectManagerEvent(ProjectNotification.NEW_PROJECT); |
166 | 41148 | jjdelcerro | } |
167 | 40435 | jjdelcerro | |
168 | 41148 | jjdelcerro | protected void clean() { |
169 | this.owner = ""; |
||
170 | this.comments = ""; |
||
171 | this.name = PluginServices.getText(this, "untitled"); |
||
172 | this.creationDate = DateFormat.getDateInstance().format(new Date()); |
||
173 | this.modificationDate = this.creationDate; |
||
174 | 40435 | jjdelcerro | |
175 | 47790 | fdiaz | this.documents = new ArrayList<>(); |
176 | this.extents = new ArrayList<>(); |
||
177 | 40435 | jjdelcerro | |
178 | 41148 | jjdelcerro | this.setSelectionColor(getPreferences().getDefaultSelectionColor());
|
179 | 40435 | jjdelcerro | |
180 | 41148 | jjdelcerro | this.projection = null; // se inicializa en el getProjection() |
181 | } |
||
182 | 40435 | jjdelcerro | |
183 | 41148 | jjdelcerro | public static ProjectPreferences getPreferences() { |
184 | return preferences;
|
||
185 | } |
||
186 | 40435 | jjdelcerro | |
187 | 44088 | omartinez | @Override
|
188 | 41148 | jjdelcerro | public void propertyChange(PropertyChangeEvent evt) { |
189 | change.firePropertyChange(evt); |
||
190 | } |
||
191 | 40435 | jjdelcerro | |
192 | 43913 | jjdelcerro | @Override
|
193 | 41148 | jjdelcerro | public synchronized void addPropertyChangeListener( |
194 | PropertyChangeListener arg0) {
|
||
195 | change.addPropertyChangeListener(arg0); |
||
196 | } |
||
197 | 40435 | jjdelcerro | |
198 | 43913 | jjdelcerro | @Override
|
199 | public void removePropertyChangeListener(PropertyChangeListener listener) { |
||
200 | change.removePropertyChangeListener(listener); |
||
201 | } |
||
202 | |||
203 | 41148 | jjdelcerro | /**
|
204 | * Return the creation date of the project
|
||
205 | *
|
||
206 | * @return
|
||
207 | */
|
||
208 | 44088 | omartinez | @Override
|
209 | 41148 | jjdelcerro | public String getCreationDate() { |
210 | return creationDate;
|
||
211 | } |
||
212 | 40435 | jjdelcerro | |
213 | 41148 | jjdelcerro | protected void setCreationDate(String creationDate) { |
214 | this.creationDate = creationDate;
|
||
215 | change.firePropertyChange("setCreationDate", null, null); |
||
216 | } |
||
217 | 40435 | jjdelcerro | |
218 | 44088 | omartinez | @Override
|
219 | 40435 | jjdelcerro | public Document createDocument(String type) { |
220 | 41148 | jjdelcerro | logger.info("createDocument('{}')", type);
|
221 | 40435 | jjdelcerro | return ProjectManager.getInstance().createDocument(type);
|
222 | } |
||
223 | |||
224 | /**
|
||
225 | * Return the name of the project
|
||
226 | 41148 | jjdelcerro | *
|
227 | 40435 | jjdelcerro | * @return
|
228 | */
|
||
229 | 44088 | omartinez | @Override
|
230 | 40435 | jjdelcerro | public String getName() { |
231 | return name;
|
||
232 | } |
||
233 | |||
234 | 41148 | jjdelcerro | /**
|
235 | * Set the name of he project.
|
||
236 | *
|
||
237 | * @param string
|
||
238 | */
|
||
239 | 44088 | omartinez | @Override
|
240 | 41148 | jjdelcerro | public void setName(String name) { |
241 | this.name = name;
|
||
242 | change.firePropertyChange("setName", null, null); |
||
243 | } |
||
244 | 40435 | jjdelcerro | |
245 | 41148 | jjdelcerro | /**
|
246 | * Return the comments associateds with the project
|
||
247 | *
|
||
248 | * @return comments
|
||
249 | */
|
||
250 | 44088 | omartinez | @Override
|
251 | 41148 | jjdelcerro | public String getComments() { |
252 | return comments;
|
||
253 | } |
||
254 | 40435 | jjdelcerro | |
255 | 41148 | jjdelcerro | /**
|
256 | * Set the comments associateds with the project
|
||
257 | *
|
||
258 | * @param comments as string
|
||
259 | */
|
||
260 | 44088 | omartinez | @Override
|
261 | 41148 | jjdelcerro | public void setComments(String string) { |
262 | comments = string; |
||
263 | change.firePropertyChange("setComments", null, null); |
||
264 | } |
||
265 | 40435 | jjdelcerro | |
266 | 41148 | jjdelcerro | /**
|
267 | * Retuen the modification date of the project.
|
||
268 | *
|
||
269 | * @return modification date as string
|
||
270 | */
|
||
271 | 44088 | omartinez | @Override
|
272 | 41148 | jjdelcerro | public String getModificationDate() { |
273 | return modificationDate;
|
||
274 | } |
||
275 | 40435 | jjdelcerro | |
276 | 41148 | jjdelcerro | protected void setModificationDate(String string) { |
277 | modificationDate = string; |
||
278 | change.firePropertyChange("setModificationDate", null, null); |
||
279 | } |
||
280 | 40435 | jjdelcerro | |
281 | 41148 | jjdelcerro | /**
|
282 | * Return the author of the project,
|
||
283 | *
|
||
284 | * @return author as string
|
||
285 | */
|
||
286 | 44088 | omartinez | @Override
|
287 | 41148 | jjdelcerro | public String getOwner() { |
288 | return owner;
|
||
289 | } |
||
290 | 40435 | jjdelcerro | |
291 | 41148 | jjdelcerro | /**
|
292 | * Sets the author of the project
|
||
293 | *
|
||
294 | * @param author name as string
|
||
295 | */
|
||
296 | 44088 | omartinez | @Override
|
297 | 41148 | jjdelcerro | public void setOwner(String owner) { |
298 | this.owner = owner;
|
||
299 | change.firePropertyChange("setOwner", null, null); |
||
300 | } |
||
301 | 40435 | jjdelcerro | |
302 | 41148 | jjdelcerro | /**
|
303 | * Obtiene el color de selecci�n que se usar� en el proyecto
|
||
304 | *
|
||
305 | * @return
|
||
306 | */
|
||
307 | 44088 | omartinez | @Override
|
308 | 41148 | jjdelcerro | public Color getSelectionColor() { |
309 | 47790 | fdiaz | // if (selectionColor == null) {
|
310 | // selectionColor = getPreferences().getDefaultSelectionColor();
|
||
311 | // }
|
||
312 | 41148 | jjdelcerro | return selectionColor;
|
313 | } |
||
314 | 40435 | jjdelcerro | |
315 | 41148 | jjdelcerro | /**
|
316 | * Sets the selecction color
|
||
317 | *
|
||
318 | * @param selection color as string
|
||
319 | */
|
||
320 | 44088 | omartinez | @Override
|
321 | 41148 | jjdelcerro | public void setSelectionColor(String selectionColor) { |
322 | 47790 | fdiaz | if(StringUtils.isBlank(selectionColor)){
|
323 | return;
|
||
324 | } |
||
325 | 41148 | jjdelcerro | this.setSelectionColor(StringUtilities.string2Color(selectionColor));
|
326 | } |
||
327 | 40435 | jjdelcerro | |
328 | 41148 | jjdelcerro | /**
|
329 | * Sets the selecction color
|
||
330 | *
|
||
331 | * @param selection color as Color
|
||
332 | */
|
||
333 | 44088 | omartinez | @Override
|
334 | 41148 | jjdelcerro | public void setSelectionColor(Color selectionColor) { |
335 | 47790 | fdiaz | if(selectionColor == null) { |
336 | return;
|
||
337 | } |
||
338 | 41148 | jjdelcerro | this.selectionColor = selectionColor;
|
339 | 47790 | fdiaz | // MapContext.setSelectionColor(selectionColor);
|
340 | 41148 | jjdelcerro | change.firePropertyChange("selectionColor", null, selectionColor); |
341 | } |
||
342 | 40435 | jjdelcerro | |
343 | 44088 | omartinez | @Override
|
344 | 41148 | jjdelcerro | public IProjection getProjection() {
|
345 | if (projection == null) { |
||
346 | projection = getPreferences().getDefaultProjection(); |
||
347 | } |
||
348 | return projection;
|
||
349 | } |
||
350 | 40435 | jjdelcerro | |
351 | 44088 | omartinez | @Override
|
352 | 41148 | jjdelcerro | public void setProjection(IProjection projection) { |
353 | this.projection = projection;
|
||
354 | } |
||
355 | 40435 | jjdelcerro | |
356 | 41148 | jjdelcerro | /**
|
357 | * Sets the modified state of project.
|
||
358 | *
|
||
359 | * Can't set to not modified.
|
||
360 | *
|
||
361 | * @param modified as boolean
|
||
362 | */
|
||
363 | 44088 | omartinez | @Override
|
364 | 41148 | jjdelcerro | public void setModified(boolean modified) { |
365 | this.modified = modified;
|
||
366 | if (modified == false) { |
||
367 | List<Document> documents = this.getDocuments(); |
||
368 | for (int i = 0; i < documents.size(); i++) { |
||
369 | documents.get(i).setModified(false);
|
||
370 | } |
||
371 | } |
||
372 | } |
||
373 | 40435 | jjdelcerro | |
374 | 44088 | omartinez | @Override
|
375 | 41148 | jjdelcerro | public boolean hasChanged() { |
376 | 45679 | jjdelcerro | if( modified ) {
|
377 | 41148 | jjdelcerro | return true; |
378 | } |
||
379 | 45679 | jjdelcerro | if (this.getDocuments().isEmpty() ) { |
380 | return false; |
||
381 | } |
||
382 | for (Document document : this.getDocuments()) { |
||
383 | if( document.isTemporary()) {
|
||
384 | continue;
|
||
385 | } |
||
386 | if( document.isModified() ) {
|
||
387 | return true; |
||
388 | } |
||
389 | } |
||
390 | 41148 | jjdelcerro | return false; |
391 | } |
||
392 | 40435 | jjdelcerro | |
393 | 41148 | jjdelcerro | /**
|
394 | * Return a list of documents in the project.
|
||
395 | *
|
||
396 | * @return documents as List of IProjectDocument
|
||
397 | */
|
||
398 | 44088 | omartinez | @Override
|
399 | 41148 | jjdelcerro | public List<Document> getDocuments() { |
400 | 46737 | jjdelcerro | return this.getDocuments((Predicate<Document>)null); |
401 | 41148 | jjdelcerro | } |
402 | 40435 | jjdelcerro | |
403 | 41148 | jjdelcerro | /**
|
404 | * Return a list with all documents of especified type.
|
||
405 | *
|
||
406 | * @param type of document
|
||
407 | *
|
||
408 | * @return List of IProjectDocument
|
||
409 | */
|
||
410 | 44088 | omartinez | @Override
|
411 | 41148 | jjdelcerro | public List<Document> getDocuments(String type) { |
412 | 46737 | jjdelcerro | return this.getDocuments((Document t) -> t!=null && |
413 | (type==null || StringUtils.equalsIgnoreCase(type, t.getTypeName()))
|
||
414 | ); |
||
415 | } |
||
416 | |||
417 | @Override
|
||
418 | public List<Document> getDocuments(Class<? extends Document> documentClass) { |
||
419 | return this.getDocuments((Document t) -> t!=null && |
||
420 | (documentClass==null || documentClass.isAssignableFrom(t.getClass()))
|
||
421 | ); |
||
422 | } |
||
423 | |||
424 | @Override
|
||
425 | public List<Document> getDocuments(Predicate<Document> filter) { |
||
426 | List<Document> docs; |
||
427 | if( filter == null ) { |
||
428 | docs = new ArrayList<Document>(this.documents); |
||
429 | } else {
|
||
430 | docs = new ArrayList<Document>(); |
||
431 | 41148 | jjdelcerro | for (Document document : this.documents) { |
432 | 46737 | jjdelcerro | if ( filter.test(document) ) {
|
433 | 41148 | jjdelcerro | docs.add(document); |
434 | } |
||
435 | } |
||
436 | } |
||
437 | 45814 | jjdelcerro | Collections.sort(docs, (Document o1, Document o2) -> StringUtils.compareIgnoreCase(o1.getName(), o2.getName())); |
438 | 41148 | jjdelcerro | return Collections.unmodifiableList(docs); |
439 | } |
||
440 | 40435 | jjdelcerro | |
441 | 41148 | jjdelcerro | /**
|
442 | * Adds a document to the project
|
||
443 | *
|
||
444 | 41248 | jjdelcerro | * @param document as Document
|
445 | 41148 | jjdelcerro | */
|
446 | 44088 | omartinez | @Override
|
447 | 41148 | jjdelcerro | public void add(Document document) { |
448 | 41248 | jjdelcerro | this.addDocument(document);
|
449 | } |
||
450 | 42088 | fdiaz | |
451 | 44088 | omartinez | @Override
|
452 | 41248 | jjdelcerro | public void addDocument(Document document) { |
453 | 40435 | jjdelcerro | logger.info("add('{}')", document.toString());
|
454 | |||
455 | 41148 | jjdelcerro | if (notifyObservers(ProjectNotification.BEFORE_ADD_DOCUMENT, document).isProcessCanceled()) {
|
456 | return;
|
||
457 | 40435 | jjdelcerro | } |
458 | 41148 | jjdelcerro | document.addPropertyChangeListener(this);
|
459 | document.setProject(this);
|
||
460 | document.setName(this.getUniqueNameForDocument(document.getTypeName(),
|
||
461 | document.getName())); |
||
462 | documents.add(document); |
||
463 | 47219 | fdiaz | DisposeUtils.bind(document); |
464 | 41148 | jjdelcerro | document.afterAdd(); |
465 | this.setModified(true); |
||
466 | notifyObservers(ProjectNotification.AFTER_ADD_DOCUMENT, document); |
||
467 | 40435 | jjdelcerro | change.firePropertyChange("addDocument", null, document); |
468 | 41148 | jjdelcerro | } |
469 | 40435 | jjdelcerro | |
470 | 44088 | omartinez | @Override
|
471 | 41148 | jjdelcerro | public void remove(Document doc) { |
472 | 41248 | jjdelcerro | this.removeDocument(doc);
|
473 | } |
||
474 | 42088 | fdiaz | |
475 | 44088 | omartinez | @Override
|
476 | 41248 | jjdelcerro | public void removeDocument(Document doc) { |
477 | 40435 | jjdelcerro | logger.info("remove('{}')", doc.toString());
|
478 | 41148 | jjdelcerro | if (notifyObservers(ProjectNotification.BEFORE_REMOVE_DOCUMENT, doc).isProcessCanceled()) {
|
479 | return;
|
||
480 | 40435 | jjdelcerro | } |
481 | 41148 | jjdelcerro | documents.remove(doc); |
482 | this.setModified(true); |
||
483 | 40435 | jjdelcerro | change.firePropertyChange("delDocument", doc, null); |
484 | 41148 | jjdelcerro | doc.afterRemove(); |
485 | notifyObservers(ProjectNotification.AFTER_REMOVE_DOCUMENT, doc); |
||
486 | 47219 | fdiaz | DisposeUtils.disposeQuietly(doc); |
487 | 41148 | jjdelcerro | } |
488 | 40435 | jjdelcerro | |
489 | 44088 | omartinez | @Override
|
490 | 41148 | jjdelcerro | public Iterator<Document> iterator() { |
491 | return documents.iterator();
|
||
492 | } |
||
493 | 40435 | jjdelcerro | |
494 | 44088 | omartinez | @Override
|
495 | 41148 | jjdelcerro | public boolean isEmpty() { |
496 | return documents.isEmpty();
|
||
497 | } |
||
498 | 40435 | jjdelcerro | |
499 | 41148 | jjdelcerro | /**
|
500 | * Return the view that contains the especified layer.
|
||
501 | *
|
||
502 | * @param layer
|
||
503 | *
|
||
504 | * @return name of the view that contains the layer
|
||
505 | *
|
||
506 | * @throws RuntimeException Si la capa que se pasa como par�metro no se
|
||
507 | * encuentra en ninguna vista
|
||
508 | */
|
||
509 | 44088 | omartinez | @Override
|
510 | 41148 | jjdelcerro | public String getViewName(FLayer layer) { |
511 | List<Document> views = getDocuments(ViewManager.TYPENAME); |
||
512 | for (int v = 0; v < views.size(); v++) { |
||
513 | DefaultViewDocument pView = (DefaultViewDocument) views.get(v); |
||
514 | FLayers layers = pView.getMapContext().getLayers(); |
||
515 | if (isView(layers, layer)) {
|
||
516 | return pView.getName();
|
||
517 | } |
||
518 | } |
||
519 | 40435 | jjdelcerro | |
520 | 41148 | jjdelcerro | throw new RuntimeException(MessageFormat.format( |
521 | "The layer '{1}' is not in a view", layer.getName()));
|
||
522 | } |
||
523 | 40435 | jjdelcerro | |
524 | 41148 | jjdelcerro | private boolean isView(FLayers layers, FLayer layer) { |
525 | for (int i = 0; i < layers.getLayersCount(); i++) { |
||
526 | if (layers.getLayer(i) instanceof FLayers) { |
||
527 | if (isView((FLayers) layers.getLayer(i), layer)) {
|
||
528 | return true; |
||
529 | } |
||
530 | } |
||
531 | if (layers.getLayer(i) == layer) {
|
||
532 | return true; |
||
533 | } |
||
534 | } |
||
535 | return false; |
||
536 | } |
||
537 | 40435 | jjdelcerro | |
538 | 44088 | omartinez | @Override
|
539 | 41148 | jjdelcerro | public void addExtent(ProjectExtent arg1) { |
540 | extents.add(arg1); |
||
541 | change.firePropertyChange("addExtent", null, null); |
||
542 | } |
||
543 | 40435 | jjdelcerro | |
544 | 44088 | omartinez | @Override
|
545 | 41148 | jjdelcerro | public ProjectExtent removeExtent(int arg0) { |
546 | change.firePropertyChange("delExtent", null, null); |
||
547 | return extents.remove(arg0);
|
||
548 | } |
||
549 | 40435 | jjdelcerro | |
550 | 44088 | omartinez | @Override
|
551 | 41148 | jjdelcerro | public ProjectExtent[] getExtents() { |
552 | return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]); |
||
553 | } |
||
554 | 40435 | jjdelcerro | |
555 | 41148 | jjdelcerro | /**
|
556 | * Obtiene un documento a partir de su nombre y el nombre de registro en el
|
||
557 | * pointExtension, este �ltimo se puede obtener del
|
||
558 | * Project****Factory.registerName.
|
||
559 | *
|
||
560 | * @param name Nombre del documento
|
||
561 | * @param type nombre de registro en el extensionPoint
|
||
562 | *
|
||
563 | * @return Documento
|
||
564 | */
|
||
565 | 44088 | omartinez | @Override
|
566 | 41148 | jjdelcerro | public Document getDocument(String name, String type) { |
567 | 42293 | jjdelcerro | if (type == null) { |
568 | 41148 | jjdelcerro | for (int i = 0; i < documents.size(); i++) { |
569 | Document document = documents.get(i);
|
||
570 | 47784 | jjdelcerro | if( document == null ) { |
571 | continue;
|
||
572 | } |
||
573 | if( StringUtils.equalsIgnoreCase(name, document.getName()) ) {
|
||
574 | 42293 | jjdelcerro | return document;
|
575 | } |
||
576 | } |
||
577 | } else {
|
||
578 | for (int i = 0; i < documents.size(); i++) { |
||
579 | Document document = documents.get(i);
|
||
580 | 47784 | jjdelcerro | if( document == null ) { |
581 | continue;
|
||
582 | } |
||
583 | if( StringUtils.equalsIgnoreCase(type, document.getTypeName())
|
||
584 | && StringUtils.equalsIgnoreCase(name, document.getName()) ) { |
||
585 | 41148 | jjdelcerro | return document;
|
586 | } |
||
587 | } |
||
588 | } |
||
589 | return null; |
||
590 | } |
||
591 | 42711 | fdiaz | |
592 | 46296 | jjdelcerro | public Document getDocument(Predicate<Document>condition) { |
593 | for (Document document : documents) { |
||
594 | 47784 | jjdelcerro | if( document == null ) { |
595 | continue;
|
||
596 | } |
||
597 | 46296 | jjdelcerro | if( condition.test(document) ) {
|
598 | return document;
|
||
599 | } |
||
600 | } |
||
601 | return null; |
||
602 | } |
||
603 | |||
604 | 44088 | omartinez | @Override
|
605 | 42293 | jjdelcerro | public Document getDocument(String name) { |
606 | return this.getDocument(name, null); |
||
607 | 42711 | fdiaz | } |
608 | 40435 | jjdelcerro | |
609 | 41148 | jjdelcerro | public String getUniqueNameForDocument(String type, String name) { |
610 | Document document = getDocument(name, type);
|
||
611 | if (document == null) { |
||
612 | return name;
|
||
613 | } |
||
614 | 40435 | jjdelcerro | |
615 | 41148 | jjdelcerro | String newName = null; |
616 | int num = this.getNextDocumentIndex(type); |
||
617 | while (document != null) { |
||
618 | newName = name + " - " + num++;
|
||
619 | document = getDocument(newName, type); |
||
620 | } |
||
621 | this.setNextDocumentIndex(type, num);
|
||
622 | return newName;
|
||
623 | } |
||
624 | 40435 | jjdelcerro | |
625 | 41148 | jjdelcerro | private int getNextDocumentIndex(String type) { |
626 | if (nextDocumentIndexByType.get(type) == null) { |
||
627 | 47790 | fdiaz | nextDocumentIndexByType.put(type, 1);
|
628 | 41148 | jjdelcerro | return 1; |
629 | } |
||
630 | 47790 | fdiaz | return nextDocumentIndexByType.get(type);
|
631 | 41148 | jjdelcerro | } |
632 | 40435 | jjdelcerro | |
633 | 41148 | jjdelcerro | private void setNextDocumentIndex(String type, int newIndex) { |
634 | if (nextDocumentIndexByType.get(type) == null) { |
||
635 | 47790 | fdiaz | nextDocumentIndexByType.put(type, newIndex); |
636 | 41148 | jjdelcerro | } else {
|
637 | 47790 | fdiaz | nextDocumentIndexByType.put(type, newIndex); |
638 | 41148 | jjdelcerro | } |
639 | } |
||
640 | 40435 | jjdelcerro | |
641 | 44088 | omartinez | @Override
|
642 | 41148 | jjdelcerro | public void saveState(File out) throws PersistenceException { |
643 | FileOutputStream fout;
|
||
644 | if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_FILE, out).isProcessCanceled()) {
|
||
645 | return;
|
||
646 | 40435 | jjdelcerro | } |
647 | 41148 | jjdelcerro | try {
|
648 | fout = new FileOutputStream(out); |
||
649 | saveState(fout, new File(out.getParent())); |
||
650 | } catch (FileNotFoundException e) { |
||
651 | 44088 | omartinez | createProjectCopy(true);
|
652 | 41148 | jjdelcerro | throw new PersistenceException(e); |
653 | } |
||
654 | 43820 | jjdelcerro | if( !isValidZIP(out) ) {
|
655 | throw new PersistenceException(new ZipException()); |
||
656 | } |
||
657 | 42466 | jjdelcerro | this.fname = out;
|
658 | 44088 | omartinez | //sacar copia al bak
|
659 | createProjectCopy(false);
|
||
660 | 41148 | jjdelcerro | notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out); |
661 | } |
||
662 | 40435 | jjdelcerro | |
663 | 43820 | jjdelcerro | boolean isValidZIP(final File file) { |
664 | ZipFile zipfile = null; |
||
665 | try {
|
||
666 | zipfile = new ZipFile(file); |
||
667 | return true; |
||
668 | } catch (IOException e) { |
||
669 | 44088 | omartinez | //sacar copia.
|
670 | 43820 | jjdelcerro | return false; |
671 | } finally {
|
||
672 | try {
|
||
673 | if (zipfile != null) { |
||
674 | zipfile.close(); |
||
675 | zipfile = null;
|
||
676 | } |
||
677 | } catch (IOException e) { |
||
678 | } |
||
679 | } |
||
680 | } |
||
681 | |||
682 | 43913 | jjdelcerro | @Override
|
683 | 42466 | jjdelcerro | public File getFile() { |
684 | return this.fname; |
||
685 | } |
||
686 | 42711 | fdiaz | |
687 | 43913 | jjdelcerro | @Deprecated
|
688 | @Override
|
||
689 | 41148 | jjdelcerro | public void saveState(OutputStream out) throws PersistenceException { |
690 | saveState(out, null);
|
||
691 | } |
||
692 | 43913 | jjdelcerro | |
693 | @Override
|
||
694 | public void saveState(File file, BufferedImage preview) { |
||
695 | FileOutputStream fout=null; |
||
696 | ZipOutputStream zout=null; |
||
697 | try {
|
||
698 | fout = new FileOutputStream(file); |
||
699 | zout = new ZipOutputStream(fout); |
||
700 | this.saveState(zout, file.getParentFile());
|
||
701 | 40435 | jjdelcerro | |
702 | 43913 | jjdelcerro | zout.putNextEntry(new ZipEntry("preview.jpg")); |
703 | try {
|
||
704 | ImageIO.write(preview, "jpg", zout); |
||
705 | } catch (IOException ex) { |
||
706 | LOG.warn("Can't save preview image'.", ex);
|
||
707 | } |
||
708 | IOUtils.closeQuietly(zout); |
||
709 | IOUtils.closeQuietly(fout); |
||
710 | |||
711 | if( !isValidZIP(file) ) {
|
||
712 | throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'"); |
||
713 | } |
||
714 | this.fname = file;
|
||
715 | 44088 | omartinez | if (file.exists()) {
|
716 | createProjectCopy(false);
|
||
717 | } |
||
718 | 44485 | jjdelcerro | I18nManager i18n = ToolsLocator.getI18nManager(); |
719 | ThreadSafeDialogsManager dialog = ToolsSwingLocator.getThreadSafeDialogsManager(); |
||
720 | dialog.message( |
||
721 | i18n.getTranslation("wrote_project"),
|
||
722 | JOptionPane.INFORMATION_MESSAGE
|
||
723 | ); |
||
724 | 43913 | jjdelcerro | } catch (Exception ex) { |
725 | 44088 | omartinez | createProjectCopy(true);
|
726 | throw new RuntimeException("Can't write project in '" + file.getAbsolutePath() + ".", ex); |
||
727 | 43913 | jjdelcerro | } finally {
|
728 | IOUtils.closeQuietly(zout); |
||
729 | 44088 | omartinez | IOUtils.closeQuietly(fout); |
730 | |||
731 | 43913 | jjdelcerro | } |
732 | } |
||
733 | |||
734 | @Override
|
||
735 | 41148 | jjdelcerro | public void saveState(OutputStream out, File rootFolder) |
736 | throws PersistenceException {
|
||
737 | 43913 | jjdelcerro | if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM, rootFolder).isProcessCanceled()) {
|
738 | 41148 | jjdelcerro | return;
|
739 | 40435 | jjdelcerro | } |
740 | 41148 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
741 | PersistentState state = null;
|
||
742 | state = manager.getState(this, true); |
||
743 | try {
|
||
744 | if (rootFolder != null) { |
||
745 | state.relativizeFiles(rootFolder); |
||
746 | } |
||
747 | } catch (Exception ex) { |
||
748 | state.getContext().addError(ex); |
||
749 | } |
||
750 | 43077 | jjdelcerro | manager.saveState(state, out, true);
|
751 | 41148 | jjdelcerro | if (state.getContext().getErrors() != null) { |
752 | 44088 | omartinez | createProjectCopy(true);
|
753 | 41148 | jjdelcerro | throw state.getContext().getErrors();
|
754 | } |
||
755 | 42466 | jjdelcerro | this.fname = null; |
756 | 43913 | jjdelcerro | notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder); |
757 | 41148 | jjdelcerro | } |
758 | 44088 | omartinez | |
759 | private File getNameFileForProject(File actualProjectFile, boolean error) { |
||
760 | // if there is a error, creates a file with a unique timecode
|
||
761 | // not error: return path with .gvsproj.bak extension that could exists
|
||
762 | |||
763 | if (actualProjectFile==null) { |
||
764 | LOG.info("Doesn't have previous project to create a bak");
|
||
765 | return null; |
||
766 | } |
||
767 | String noExt = FilenameUtils.removeExtension(actualProjectFile.getAbsolutePath());
|
||
768 | String timeCode = new SimpleDateFormat("_MMddHHmmss").format(new Date()); |
||
769 | String ext = ".gvsproj.bak"; |
||
770 | 40435 | jjdelcerro | |
771 | 44088 | omartinez | int code = 0; |
772 | if (noExt.length() > 230) { |
||
773 | noExt = noExt.substring(0, 229); |
||
774 | } |
||
775 | |||
776 | File bakProjectFile;
|
||
777 | if (error) {
|
||
778 | bakProjectFile = new File(noExt + timeCode + ext); |
||
779 | while (bakProjectFile.exists()) {
|
||
780 | code = code + 1;
|
||
781 | bakProjectFile = new File(noExt + timeCode + "_" + code + ext); |
||
782 | } |
||
783 | } else {
|
||
784 | bakProjectFile = new File(noExt + ext); |
||
785 | } |
||
786 | return bakProjectFile;
|
||
787 | } |
||
788 | public File createProjectCopy(boolean error) { |
||
789 | File actualProjectFile = ApplicationLocator.getProjectManager().getCurrentProject().getFile();
|
||
790 | File bakProjectFile = getNameFileForProject(actualProjectFile, error);
|
||
791 | if (bakProjectFile==null){ |
||
792 | return null; |
||
793 | } |
||
794 | if (!error){
|
||
795 | try {
|
||
796 | if (bakProjectFile.exists()){
|
||
797 | FileUtils.deleteQuietly(bakProjectFile); |
||
798 | } |
||
799 | } catch (Exception ex) { |
||
800 | 45978 | jjdelcerro | LOG.warn("Not possible to delete file", ex);
|
801 | 44088 | omartinez | } |
802 | try {
|
||
803 | FileUtils.copyFile(actualProjectFile, bakProjectFile); |
||
804 | } catch (IOException ex) { |
||
805 | 45978 | jjdelcerro | LOG.warn("Can't create bak copy from project", ex);
|
806 | 44088 | omartinez | } |
807 | } else {
|
||
808 | 44204 | omartinez | File bakProjectFileOld = getNameFileForProject(actualProjectFile, false); |
809 | if (bakProjectFileOld.exists()) {
|
||
810 | try {
|
||
811 | FileUtils.moveFile(bakProjectFileOld, bakProjectFile); |
||
812 | } catch (IOException ex) { |
||
813 | 45978 | jjdelcerro | LOG.warn("Can't rename file", ex);
|
814 | 44204 | omartinez | } |
815 | } else {
|
||
816 | 45978 | jjdelcerro | LOG.info("Can't find bak project. Probably doesn't exist or already has been renamed");
|
817 | 44088 | omartinez | } |
818 | } |
||
819 | |||
820 | return bakProjectFile;
|
||
821 | } |
||
822 | |||
823 | 43913 | jjdelcerro | @Deprecated
|
824 | 44088 | omartinez | @Override
|
825 | 41148 | jjdelcerro | public void loadState(InputStream in) { |
826 | loadState(in, null);
|
||
827 | } |
||
828 | 40435 | jjdelcerro | |
829 | 41148 | jjdelcerro | public void loadState(InputStream in, File rootFolder) { |
830 | 43913 | jjdelcerro | if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STREAM, rootFolder).isProcessCanceled()) {
|
831 | 41148 | jjdelcerro | return;
|
832 | } |
||
833 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
834 | try {
|
||
835 | 43439 | jjdelcerro | PersistentContext context = manager.getNewContext(); |
836 | context.setCollectErrors(true);
|
||
837 | PersistentState state = manager.loadState(in, context); |
||
838 | 41148 | jjdelcerro | try {
|
839 | if (rootFolder != null) { |
||
840 | state.derelativizeFiles(rootFolder); |
||
841 | } |
||
842 | } catch (Exception ex) { |
||
843 | state.getContext().addError(ex); |
||
844 | } |
||
845 | this.loadFromState(state);
|
||
846 | 42711 | fdiaz | this.unloadedObjects = getUnloadedObjects(state.getContext());
|
847 | 43439 | jjdelcerro | this.loadErrors = state.getContext().getErrors();
|
848 | 43879 | jjdelcerro | |
849 | 41148 | jjdelcerro | } catch (PersistenceException e) {
|
850 | LOG.info("Can't load project to stream", e);
|
||
851 | } |
||
852 | 42466 | jjdelcerro | this.fname = null; |
853 | 43913 | jjdelcerro | notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM, rootFolder); |
854 | 40435 | jjdelcerro | |
855 | 41148 | jjdelcerro | } |
856 | 40435 | jjdelcerro | |
857 | 42711 | fdiaz | |
858 | |||
859 | /**
|
||
860 | * @param context
|
||
861 | * @return
|
||
862 | */
|
||
863 | private Set<String> getUnloadedObjects(PersistentContext context) { |
||
864 | Set unloadedObjects = new HashSet(); |
||
865 | |||
866 | Iterator statesIterator = context.iterator();
|
||
867 | String className = null; |
||
868 | |||
869 | while (statesIterator.hasNext()) {
|
||
870 | PersistentState aState = (PersistentState) statesIterator.next(); |
||
871 | try {
|
||
872 | className = aState.getTheClassName(); |
||
873 | DynStruct definition = aState.getDefinition(); |
||
874 | if (definition == null) { |
||
875 | unloadedObjects.add(className); |
||
876 | } |
||
877 | } catch (Throwable e) { |
||
878 | // do nothing
|
||
879 | } |
||
880 | } |
||
881 | |||
882 | if(unloadedObjects.isEmpty()){
|
||
883 | return null; |
||
884 | } |
||
885 | |||
886 | return unloadedObjects;
|
||
887 | } |
||
888 | |||
889 | 43439 | jjdelcerro | @Override
|
890 | 42711 | fdiaz | public Set<String> getUnloadedObjects() { |
891 | return this.unloadedObjects; |
||
892 | } |
||
893 | |||
894 | 43439 | jjdelcerro | @Override
|
895 | public List<Exception> getLoadErrors() { |
||
896 | return this.loadErrors; |
||
897 | } |
||
898 | |||
899 | @Override
|
||
900 | 41148 | jjdelcerro | public void loadState(File in) { |
901 | if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_FILE, in).isProcessCanceled()) {
|
||
902 | return;
|
||
903 | } |
||
904 | FileInputStream fin;
|
||
905 | try {
|
||
906 | fin = new FileInputStream(in); |
||
907 | loadState(fin, new File(in.getParent())); |
||
908 | } catch (FileNotFoundException e) { |
||
909 | 42466 | jjdelcerro | LOG.info("Can't load project to stream", e);
|
910 | 41148 | jjdelcerro | } |
911 | 44088 | omartinez | if ("bak".equals(FilenameUtils.getExtension(in.getAbsolutePath()))) { |
912 | this.fname = null; |
||
913 | } else {
|
||
914 | this.fname = in;
|
||
915 | } |
||
916 | 41148 | jjdelcerro | notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in); |
917 | } |
||
918 | 40435 | jjdelcerro | |
919 | 43913 | jjdelcerro | //@SuppressWarnings("unchecked")
|
920 | @Override
|
||
921 | 41148 | jjdelcerro | public void loadFromState(PersistentState state) |
922 | throws PersistenceException {
|
||
923 | this.clean();
|
||
924 | 43976 | jjdelcerro | PersistentContext context = state.getContext(); |
925 | |||
926 | 43913 | jjdelcerro | notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STATE, state); |
927 | 40435 | jjdelcerro | |
928 | 41148 | jjdelcerro | this.setComments(state.getString("comments")); |
929 | this.setCreationDate(state.getString("creationDate")); |
||
930 | this.setModificationDate(state.getString("modificationDate")); |
||
931 | this.setName(state.getString("name")); |
||
932 | this.setOwner(state.getString("owner")); |
||
933 | this.setSelectionColor((Color) state.get("selectionColor")); |
||
934 | this.setProjection((IProjection) state.get("projection")); |
||
935 | 40435 | jjdelcerro | |
936 | 41350 | jjdelcerro | this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper"); |
937 | 42088 | fdiaz | |
938 | 41148 | jjdelcerro | List<ProjectExtent> extents = (List<ProjectExtent>) state |
939 | .get("extents");
|
||
940 | for (int i = 0; i < extents.size(); i++) { |
||
941 | this.addExtent(extents.get(i));
|
||
942 | } |
||
943 | 40435 | jjdelcerro | |
944 | 41148 | jjdelcerro | List<AbstractDocument> documents = (List<AbstractDocument>) state |
945 | .get("documents");
|
||
946 | for (int i = 0; i < documents.size(); i++) { |
||
947 | 43913 | jjdelcerro | AbstractDocument doc = documents.get(i);
|
948 | if( doc != null ) { |
||
949 | this.add(doc);
|
||
950 | } |
||
951 | 41148 | jjdelcerro | } |
952 | 40435 | jjdelcerro | |
953 | 43976 | jjdelcerro | try {
|
954 | List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation"); |
||
955 | 40435 | jjdelcerro | |
956 | 43976 | jjdelcerro | for (int i = 0; i < persistentWindows.size(); i++) { |
957 | try {
|
||
958 | DocumentWindowInfo persistentWindow = persistentWindows.get(i); |
||
959 | String docName = persistentWindow.getDocumentName();
|
||
960 | String docType = persistentWindow.getDocumentType();
|
||
961 | Document doc = this.getDocument(docName, docType); |
||
962 | IWindow win = doc.getFactory().getMainWindow(doc); |
||
963 | if(win!=null){ |
||
964 | win.getWindowInfo().setWindowInfo(persistentWindow.getWindowInfo()); |
||
965 | PluginServices.getMDIManager().addWindow(win); |
||
966 | } |
||
967 | } catch(Exception ex) { |
||
968 | if( !context.getCollectErrors() ) {
|
||
969 | throw ex;
|
||
970 | } |
||
971 | context.addError(ex); |
||
972 | } |
||
973 | 42088 | fdiaz | } |
974 | 40435 | jjdelcerro | |
975 | 43976 | jjdelcerro | if (state.hasValue("projectWindowInfo")) { |
976 | WindowInfo projectWindowInfo = (WindowInfo) state.get("projectWindowInfo");
|
||
977 | 46461 | jjdelcerro | SwingUtilities.invokeLater(() -> {
|
978 | ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(org.gvsig.app.extension.ProjectExtension.class); |
||
979 | pe.setProject(this);
|
||
980 | pe.showProjectWindow(projectWindowInfo); |
||
981 | }); |
||
982 | 43976 | jjdelcerro | } |
983 | } catch(Exception ex) { |
||
984 | if( !context.getCollectErrors() ) {
|
||
985 | throw ex;
|
||
986 | } |
||
987 | context.addError(ex); |
||
988 | 41148 | jjdelcerro | } |
989 | 43913 | jjdelcerro | notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state); |
990 | 41148 | jjdelcerro | } |
991 | 40435 | jjdelcerro | |
992 | 44088 | omartinez | @Override
|
993 | 41148 | jjdelcerro | public void saveToState(PersistentState state) throws PersistenceException { |
994 | state.set("version", VERSION);
|
||
995 | state.set("comments", getComments());
|
||
996 | state.set("creationDate", this.getCreationDate()); |
||
997 | 40435 | jjdelcerro | |
998 | 41148 | jjdelcerro | state.set("modificationDate", this.getModificationDate()); |
999 | state.set("name", this.getName()); |
||
1000 | state.set("owner", this.getOwner()); |
||
1001 | state.set("selectionColor", this.getSelectionColor()); |
||
1002 | 40435 | jjdelcerro | |
1003 | 41148 | jjdelcerro | state.set("projection", this.getProjection()); |
1004 | 40435 | jjdelcerro | |
1005 | 41148 | jjdelcerro | state.set("extents", this.extents); |
1006 | 42088 | fdiaz | List<Document> docs = this.getDocuments(); |
1007 | 44443 | jjdelcerro | List<Document> noTempDocs = new ArrayList<>(); |
1008 | for (Document document : docs) { |
||
1009 | 42088 | fdiaz | if(!document.isTemporary()){
|
1010 | noTempDocs.add(document); |
||
1011 | } |
||
1012 | } |
||
1013 | state.set("documents", noTempDocs);
|
||
1014 | 40435 | jjdelcerro | |
1015 | 41350 | jjdelcerro | state.set("propertiesHelper",propertiesHelper);
|
1016 | 42088 | fdiaz | |
1017 | 44443 | jjdelcerro | List<DocumentWindowInfo> persistentWindows = new ArrayList<>(); |
1018 | 41148 | jjdelcerro | MDIManager mdiMan = PluginServices.getMDIManager(); |
1019 | IWindow[] windows = mdiMan.getOrderedWindows();
|
||
1020 | for (int i = windows.length - 1; i >= 0; i--) { |
||
1021 | IWindow window = windows[i]; |
||
1022 | if (window instanceof IDocumentWindow) { |
||
1023 | WindowInfo wi = mdiMan.getWindowInfo(window); |
||
1024 | DocumentWindowInfo dwi = new DocumentWindowInfo(
|
||
1025 | wi, |
||
1026 | ((IDocumentWindow) window).getDocument().getTypeName(), |
||
1027 | ((IDocumentWindow) window).getDocument().getName()); |
||
1028 | persistentWindows.add(dwi); |
||
1029 | } else if (window instanceof ProjectWindow) { |
||
1030 | state.set("projectWindowInfo", mdiMan.getWindowInfo(window));
|
||
1031 | } |
||
1032 | } |
||
1033 | state.set("documentWindowsInformation", persistentWindows);
|
||
1034 | 40435 | jjdelcerro | |
1035 | 41148 | jjdelcerro | } |
1036 | 40435 | jjdelcerro | |
1037 | 44088 | omartinez | @Override
|
1038 | 45978 | jjdelcerro | @Deprecated
|
1039 | 41350 | jjdelcerro | public Object getProperty(Object key) { |
1040 | return this.propertiesHelper.getProperty(key); |
||
1041 | } |
||
1042 | |||
1043 | 44088 | omartinez | @Override
|
1044 | 45978 | jjdelcerro | @Deprecated
|
1045 | 41350 | jjdelcerro | public void setProperty(Object key, Object obj) { |
1046 | this.propertiesHelper.setProperty(key, obj);
|
||
1047 | } |
||
1048 | |||
1049 | 44088 | omartinez | @Override
|
1050 | 45978 | jjdelcerro | @Deprecated
|
1051 | 41350 | jjdelcerro | public Map getExtendedProperties() { |
1052 | return this.propertiesHelper.getExtendedProperties(); |
||
1053 | } |
||
1054 | |||
1055 | 45978 | jjdelcerro | @Override
|
1056 | public Object getProperty(String name) { |
||
1057 | return this.propertiesHelper.getProperty(name); |
||
1058 | } |
||
1059 | |||
1060 | @Override
|
||
1061 | public void setProperty(String name, Object value) { |
||
1062 | this.propertiesHelper.setProperty(name, value);
|
||
1063 | } |
||
1064 | |||
1065 | @Override
|
||
1066 | public Map<String, Object> getProperties() { |
||
1067 | return this.propertiesHelper.getExtendedProperties(); |
||
1068 | } |
||
1069 | |||
1070 | 41148 | jjdelcerro | public static class DocumentWindowInfo implements Persistent { |
1071 | 40435 | jjdelcerro | |
1072 | 41148 | jjdelcerro | public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo"; |
1073 | 40435 | jjdelcerro | |
1074 | 41148 | jjdelcerro | private WindowInfo windowInfo;
|
1075 | private String documentType; |
||
1076 | private String documentName; |
||
1077 | 40435 | jjdelcerro | |
1078 | 41148 | jjdelcerro | public DocumentWindowInfo() {
|
1079 | } |
||
1080 | 40435 | jjdelcerro | |
1081 | 41148 | jjdelcerro | DocumentWindowInfo(WindowInfo wi, String docType, String docName) { |
1082 | windowInfo = wi; |
||
1083 | documentType = docType; |
||
1084 | documentName = docName; |
||
1085 | } |
||
1086 | 40435 | jjdelcerro | |
1087 | 41148 | jjdelcerro | public WindowInfo getWindowInfo() {
|
1088 | return windowInfo;
|
||
1089 | } |
||
1090 | 40435 | jjdelcerro | |
1091 | 41148 | jjdelcerro | public String getDocumentType() { |
1092 | return documentType;
|
||
1093 | } |
||
1094 | 40435 | jjdelcerro | |
1095 | 41148 | jjdelcerro | public String getDocumentName() { |
1096 | return documentName;
|
||
1097 | } |
||
1098 | 40435 | jjdelcerro | |
1099 | 47790 | fdiaz | @Override
|
1100 | 41148 | jjdelcerro | public void saveToState(PersistentState state) |
1101 | throws PersistenceException {
|
||
1102 | state.set("windowInfo", this.windowInfo); |
||
1103 | state.set("documentType", this.documentType); |
||
1104 | state.set("documentName", this.documentName); |
||
1105 | } |
||
1106 | 40435 | jjdelcerro | |
1107 | 47790 | fdiaz | @Override
|
1108 | 41148 | jjdelcerro | public void loadFromState(PersistentState state) |
1109 | throws PersistenceException {
|
||
1110 | this.windowInfo = (WindowInfo) state.get("windowInfo"); |
||
1111 | this.documentType = state.getString("documentType"); |
||
1112 | this.documentName = state.getString("documentName"); |
||
1113 | } |
||
1114 | 40435 | jjdelcerro | |
1115 | 41148 | jjdelcerro | public static void registerPersistent() { |
1116 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
1117 | DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME); |
||
1118 | if (definition == null) { |
||
1119 | definition = manager.addDefinition( |
||
1120 | DocumentWindowInfo.class, |
||
1121 | PERSISTENCE_DEFINITION_NAME, |
||
1122 | "DocumentWindowInfo persistence definition",
|
||
1123 | null,
|
||
1124 | null
|
||
1125 | ); |
||
1126 | definition.addDynFieldObject("windowInfo").setMandatory(true).setClassOfValue(WindowInfo.class); |
||
1127 | definition.addDynFieldString("documentType").setMandatory(true); |
||
1128 | definition.addDynFieldString("documentName").setMandatory(true); |
||
1129 | } |
||
1130 | 40435 | jjdelcerro | |
1131 | 41148 | jjdelcerro | } |
1132 | } |
||
1133 | 40435 | jjdelcerro | |
1134 | 41148 | jjdelcerro | public static void registerPersistent() { |
1135 | AbstractDocument.registerPersistent();
|
||
1136 | DocumentWindowInfo.registerPersistent(); |
||
1137 | ProjectExtent.registerPersistent(); |
||
1138 | 40435 | jjdelcerro | |
1139 | 41148 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
1140 | DynStruct definition = manager.addDefinition(DefaultProject.class, |
||
1141 | "Project", "Project Persistence definition", null, null); |
||
1142 | definition.addDynFieldString("version").setMandatory(true); |
||
1143 | definition.addDynFieldString("comments").setMandatory(true); |
||
1144 | definition.addDynFieldString("creationDate").setMandatory(true); |
||
1145 | definition.addDynFieldString("modificationDate").setMandatory(true); |
||
1146 | definition.addDynFieldString("name").setMandatory(true); |
||
1147 | definition.addDynFieldString("owner").setMandatory(true); |
||
1148 | 40435 | jjdelcerro | |
1149 | 41148 | jjdelcerro | definition.addDynFieldObject("selectionColor")
|
1150 | .setClassOfValue(Color.class).setMandatory(true); |
||
1151 | definition.addDynFieldObject("projection")
|
||
1152 | .setClassOfValue(IProjection.class).setMandatory(true);
|
||
1153 | 40435 | jjdelcerro | |
1154 | 41148 | jjdelcerro | definition.addDynFieldList("extents")
|
1155 | .setClassOfItems(ProjectExtent.class).setMandatory(true);
|
||
1156 | 40435 | jjdelcerro | |
1157 | 41148 | jjdelcerro | definition.addDynFieldList("documents").setClassOfItems(Document.class) |
1158 | .setMandatory(true);
|
||
1159 | 40435 | jjdelcerro | |
1160 | 41148 | jjdelcerro | definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false); |
1161 | 40435 | jjdelcerro | |
1162 | 41148 | jjdelcerro | definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false); |
1163 | 40435 | jjdelcerro | |
1164 | 42088 | fdiaz | |
1165 | 41350 | jjdelcerro | definition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
|
1166 | .setMandatory(false);
|
||
1167 | |||
1168 | 41148 | jjdelcerro | } |
1169 | 40435 | jjdelcerro | |
1170 | 41148 | jjdelcerro | /**
|
1171 | * @deprecated use getPreferences().setDefaultSelectionColor()
|
||
1172 | */
|
||
1173 | public static void setDefaultSelectionColor(Color color) { |
||
1174 | getPreferences().setDefaultSelectionColor(color); |
||
1175 | } |
||
1176 | 40435 | jjdelcerro | |
1177 | 41148 | jjdelcerro | /**
|
1178 | * @deprecated use getPreferences().getDefaultSelectionColor()
|
||
1179 | */
|
||
1180 | public static Color getDefaultSelectionColor() { |
||
1181 | return getPreferences().getDefaultSelectionColor();
|
||
1182 | } |
||
1183 | 40435 | jjdelcerro | |
1184 | 41148 | jjdelcerro | /**
|
1185 | * @deprecated use getPreferences().getDefaultMapUnits()
|
||
1186 | */
|
||
1187 | public static int getDefaultMapUnits() { |
||
1188 | return getPreferences().getDefaultMapUnits();
|
||
1189 | } |
||
1190 | 40435 | jjdelcerro | |
1191 | 41148 | jjdelcerro | /**
|
1192 | * @deprecated use getPreferences().getDefaultDistanceUnits()
|
||
1193 | */
|
||
1194 | public static int getDefaultDistanceUnits() { |
||
1195 | return getPreferences().getDefaultDistanceUnits();
|
||
1196 | } |
||
1197 | 40435 | jjdelcerro | |
1198 | 41148 | jjdelcerro | /**
|
1199 | * @deprecated use getPreferences().getDefaultDistanceArea()
|
||
1200 | */
|
||
1201 | public static int getDefaultDistanceArea() { |
||
1202 | return getPreferences().getDefaultDistanceArea();
|
||
1203 | } |
||
1204 | 40435 | jjdelcerro | |
1205 | 41148 | jjdelcerro | /**
|
1206 | * @deprecated use getPreferences().setDefaultMapUnits()
|
||
1207 | */
|
||
1208 | public static void setDefaultMapUnits(int mapUnits) { |
||
1209 | getPreferences().setDefaultMapUnits(mapUnits); |
||
1210 | } |
||
1211 | 40435 | jjdelcerro | |
1212 | 41148 | jjdelcerro | /**
|
1213 | * @deprecated use getPreferences().setDefaultDistanceUnits()
|
||
1214 | */
|
||
1215 | public static void setDefaultDistanceUnits(int distanceUnits) { |
||
1216 | getPreferences().setDefaultDistanceUnits(distanceUnits); |
||
1217 | } |
||
1218 | 40435 | jjdelcerro | |
1219 | 41148 | jjdelcerro | /**
|
1220 | * @deprecated use getPreferences().setDefaultDistanceArea()
|
||
1221 | */
|
||
1222 | public static void setDefaultDistanceArea(int distanceArea) { |
||
1223 | getPreferences().setDefaultDistanceArea(distanceArea); |
||
1224 | } |
||
1225 | 40435 | jjdelcerro | |
1226 | 41148 | jjdelcerro | /**
|
1227 | * @deprecated use getPreferences().setDefaultProjection()
|
||
1228 | */
|
||
1229 | public static void setDefaultProjection(IProjection defaultProjection) { |
||
1230 | getPreferences().setDefaultProjection(defaultProjection); |
||
1231 | } |
||
1232 | 40435 | jjdelcerro | |
1233 | 41148 | jjdelcerro | /**
|
1234 | * @deprecated use getPreferences().getDefaultProjection()
|
||
1235 | */
|
||
1236 | public static IProjection getDefaultProjection() { |
||
1237 | return getPreferences().getDefaultProjection();
|
||
1238 | } |
||
1239 | 40435 | jjdelcerro | |
1240 | 41148 | jjdelcerro | /**
|
1241 | * @deprecated see {@link #setSelectionColor(String)}, to be remove in gvSIG
|
||
1242 | * 2.1.0
|
||
1243 | */
|
||
1244 | public void setColor(String color) { |
||
1245 | this.setSelectionColor(StringUtilities.string2Color(color));
|
||
1246 | } |
||
1247 | 40435 | jjdelcerro | |
1248 | 41148 | jjdelcerro | /**
|
1249 | * Return the selection color
|
||
1250 | *
|
||
1251 | * @return selection color as string
|
||
1252 | * @deprecated use {@link #getSelectionColor()}
|
||
1253 | */
|
||
1254 | public String getColor() { |
||
1255 | return StringUtilities.color2String(selectionColor);
|
||
1256 | } |
||
1257 | 40435 | jjdelcerro | |
1258 | 41148 | jjdelcerro | /**
|
1259 | * Return the list of views of the project
|
||
1260 | *
|
||
1261 | * @return views as ArrayList of ProjectDocument
|
||
1262 | *
|
||
1263 | * @deprecated see {@link #getDocumentsByType(String)}
|
||
1264 | */
|
||
1265 | public List<Document> getViews() { |
||
1266 | return getDocuments(ViewManager.TYPENAME);
|
||
1267 | } |
||
1268 | 40435 | jjdelcerro | |
1269 | 41148 | jjdelcerro | /**
|
1270 | * Add a view to the project
|
||
1271 | *
|
||
1272 | * @deprecated see {@link #add(AbstractDocument)}
|
||
1273 | */
|
||
1274 | public void addView(DefaultViewDocument v) { |
||
1275 | add(v); |
||
1276 | } |
||
1277 | 40435 | jjdelcerro | |
1278 | 41148 | jjdelcerro | /**
|
1279 | * Remove a view of the project
|
||
1280 | *
|
||
1281 | * @param index of the view as integer
|
||
1282 | *
|
||
1283 | * @deprecated see {@link #remove(AbstractDocument)}
|
||
1284 | */
|
||
1285 | public void delView(int i) { |
||
1286 | List<Document> list = getDocuments(ViewManager.TYPENAME); |
||
1287 | remove(list.get(i)); |
||
1288 | } |
||
1289 | 40435 | jjdelcerro | |
1290 | 41148 | jjdelcerro | /**
|
1291 | * @deprecated see {@link #getDocument(String, String)}
|
||
1292 | */
|
||
1293 | public Document getProjectDocumentByName(String name, String type) { |
||
1294 | return this.getDocument(name, type); |
||
1295 | } |
||
1296 | 40435 | jjdelcerro | |
1297 | 41148 | jjdelcerro | /**
|
1298 | * @deprecated see {@link #getDocuments(String)}
|
||
1299 | */
|
||
1300 | public List<Document> getDocumentsByType(String type) { |
||
1301 | return this.getDocuments(type); |
||
1302 | } |
||
1303 | 40435 | jjdelcerro | |
1304 | 41148 | jjdelcerro | /**
|
1305 | * @deprecated aun por decidir que API darle al copy/paste
|
||
1306 | */
|
||
1307 | public String exportToXML(AbstractDocument[] selectedItems) |
||
1308 | throws SaveException {
|
||
1309 | // FIXME jjdc:hay que decirdir que API darle al copy/paste
|
||
1310 | throw new UnsupportedOperationException("This method is not supported"); |
||
1311 | } |
||
1312 | 40435 | jjdelcerro | |
1313 | 41148 | jjdelcerro | /**
|
1314 | * @deprecated aun por decidir que API darle al copy/paste
|
||
1315 | */
|
||
1316 | public void importFromXML(String sourceString, String docType) { |
||
1317 | // FIXME jjdc:hay que decirdir que API darle al copy/paste
|
||
1318 | throw new UnsupportedOperationException("This method is not supported"); |
||
1319 | } |
||
1320 | 40435 | jjdelcerro | |
1321 | 41148 | jjdelcerro | /**
|
1322 | * @deprecated aun por decidir que API darle al copy/paste
|
||
1323 | */
|
||
1324 | public boolean isValidXMLForImport(String sourceString, String docType) { |
||
1325 | // FIXME jjdc:hay que decirdir que API darle al copy/paste
|
||
1326 | throw new UnsupportedOperationException("This method is not supported"); |
||
1327 | } |
||
1328 | |||
1329 | 44088 | omartinez | @Override
|
1330 | 41148 | jjdelcerro | public boolean canImportDocuments(String data, String doctype) { |
1331 | // TODO Auto-generated method stub
|
||
1332 | return false; |
||
1333 | } |
||
1334 | |||
1335 | 44088 | omartinez | @Override
|
1336 | 41148 | jjdelcerro | public String exportDocumentsAsText(List<Document> documents) { |
1337 | // TODO Auto-generated method stub
|
||
1338 | return null; |
||
1339 | } |
||
1340 | |||
1341 | 44088 | omartinez | @Override
|
1342 | 41148 | jjdelcerro | public void importDocuments(String data, String doctype) { |
1343 | // TODO Auto-generated method stub
|
||
1344 | |||
1345 | } |
||
1346 | |||
1347 | 44088 | omartinez | @Override
|
1348 | 41148 | jjdelcerro | public Document getActiveDocument() { |
1349 | 41248 | jjdelcerro | return this.getActiveDocument((Class<? extends Document>)null); |
1350 | 41148 | jjdelcerro | } |
1351 | |||
1352 | 44088 | omartinez | @Override
|
1353 | 41248 | jjdelcerro | public Document getActiveDocument(String documentTypeName) { |
1354 | ApplicationManager application = ApplicationLocator.getManager(); |
||
1355 | 47236 | jjdelcerro | for (DocumentManager documentManager : application.getProjectManager().getDocumentManagers()) {
|
1356 | if( StringUtils.equalsIgnoreCase(documentTypeName, documentManager.getTypeName())) {
|
||
1357 | return this.getActiveDocument(documentManager.getClassOfDocument()); |
||
1358 | 41248 | jjdelcerro | } |
1359 | } |
||
1360 | return null; |
||
1361 | } |
||
1362 | 46737 | jjdelcerro | |
1363 | @Override
|
||
1364 | public List<Document> getOpenDocuments(Class<? extends Document> documentClass) { |
||
1365 | return this.getOpenDocuments((Document doc) -> { |
||
1366 | return doc!=null && (documentClass==null || documentClass.isAssignableFrom(doc.getClass())); |
||
1367 | }); |
||
1368 | } |
||
1369 | |||
1370 | @Override
|
||
1371 | public List<Document> getOpenDocuments(String type) { |
||
1372 | return this.getOpenDocuments((Document doc) -> { |
||
1373 | return doc!=null && StringUtils.equals(type, doc.getTypeName()); |
||
1374 | }); |
||
1375 | } |
||
1376 | |||
1377 | @SuppressWarnings("UnnecessaryContinue") |
||
1378 | @Override
|
||
1379 | public List<Document> getOpenDocuments(Predicate<Document>filter) { |
||
1380 | ApplicationManager application = ApplicationLocator.getManager(); |
||
1381 | 42088 | fdiaz | |
1382 | 46737 | jjdelcerro | List<Document> docs = new ArrayList<>(); |
1383 | IWindow[] windows = application.getUIManager().getOrderedWindows();
|
||
1384 | for (IWindow window : windows) {
|
||
1385 | if (window instanceof SingletonWindow && window instanceof IDocumentWindow) { |
||
1386 | // Cogemos no la primera ventana, si no la primera
|
||
1387 | // ventana de tipo documento (SingletonWindow).
|
||
1388 | 47236 | jjdelcerro | // Y si, por si las moscas, no es un documento, atrapamos
|
1389 | 46737 | jjdelcerro | // los errores y continuamos si no puede hacer un cast
|
1390 | // del Model a Document
|
||
1391 | try {
|
||
1392 | Document doc = (Document) ((SingletonWindow) window).getWindowModel(); |
||
1393 | if (filter == null) { |
||
1394 | docs.add(doc); |
||
1395 | continue;
|
||
1396 | } |
||
1397 | if ( filter.test(doc) ) {
|
||
1398 | docs.add(doc); |
||
1399 | continue;
|
||
1400 | } |
||
1401 | // Ojo, no debe tener en cuenta los subdocumentos.
|
||
1402 | } catch (ClassCastException e) { |
||
1403 | // Do nothing, skip this window
|
||
1404 | } |
||
1405 | } |
||
1406 | } |
||
1407 | return docs;
|
||
1408 | } |
||
1409 | |||
1410 | 44088 | omartinez | @Override
|
1411 | 41148 | jjdelcerro | public Document getActiveDocument(Class<? extends Document> documentClass) { |
1412 | ApplicationManager application = ApplicationLocator.getManager(); |
||
1413 | |||
1414 | Document document = null; |
||
1415 | IWindow[] windows = application.getUIManager().getOrderedWindows();
|
||
1416 | IWindow window = null;
|
||
1417 | for (int i = 0; i < windows.length; i++) { |
||
1418 | window = windows[i]; |
||
1419 | 42165 | fdiaz | if (window instanceof SingletonWindow && window instanceof IDocumentWindow) { |
1420 | 41248 | jjdelcerro | // Cogemos no la primera ventana, si no la primera
|
1421 | 41148 | jjdelcerro | // ventana de tipo documento (SingletonWindow).
|
1422 | 47236 | jjdelcerro | // Y si, por si las moscas, no es un documento, atrapamos
|
1423 | 41148 | jjdelcerro | // los errores y continuamos si no puede hacer un cast
|
1424 | // del Model a Document
|
||
1425 | try {
|
||
1426 | document = (Document) ((SingletonWindow) window).getWindowModel();
|
||
1427 | if (documentClass == null) { |
||
1428 | return document;
|
||
1429 | } |
||
1430 | if (documentClass.isAssignableFrom(document.getClass())) {
|
||
1431 | return document;
|
||
1432 | } |
||
1433 | 41248 | jjdelcerro | if( document instanceof DocumentsContainer ) { |
1434 | Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentClass);
|
||
1435 | return subdoc;
|
||
1436 | } |
||
1437 | 47236 | jjdelcerro | return null; |
1438 | } catch (ClassCastException e) { |
||
1439 | // Do nothing, skip this window
|
||
1440 | } |
||
1441 | } |
||
1442 | } |
||
1443 | return null; |
||
1444 | } |
||
1445 | |||
1446 | @Override
|
||
1447 | public Document getFirstDocument(Class<? extends Document> documentClass) { |
||
1448 | ApplicationManager application = ApplicationLocator.getManager(); |
||
1449 | 42088 | fdiaz | |
1450 | 47236 | jjdelcerro | Document document = null; |
1451 | IWindow[] windows = application.getUIManager().getOrderedWindows();
|
||
1452 | IWindow window = null;
|
||
1453 | for (int i = 0; i < windows.length; i++) { |
||
1454 | window = windows[i]; |
||
1455 | if (window instanceof SingletonWindow && window instanceof IDocumentWindow) { |
||
1456 | // Cogemos no la primera ventana, si no la primera
|
||
1457 | // ventana de tipo documento (SingletonWindow).
|
||
1458 | // Y si, por si las moscas, no es un documento, atrapamos
|
||
1459 | // los errores y continuamos si no puede hacer un cast
|
||
1460 | // del Model a Document
|
||
1461 | try {
|
||
1462 | document = (Document) ((SingletonWindow) window).getWindowModel();
|
||
1463 | if (documentClass == null) { |
||
1464 | return document;
|
||
1465 | } |
||
1466 | if (documentClass.isAssignableFrom(document.getClass())) {
|
||
1467 | return document;
|
||
1468 | } |
||
1469 | if( document instanceof DocumentsContainer ) { |
||
1470 | Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentClass);
|
||
1471 | return subdoc;
|
||
1472 | } |
||
1473 | |||
1474 | 41148 | jjdelcerro | } catch (ClassCastException e) { |
1475 | // Do nothing, skip this window
|
||
1476 | } |
||
1477 | } |
||
1478 | } |
||
1479 | return null; |
||
1480 | } |
||
1481 | 40435 | jjdelcerro | |
1482 | 44088 | omartinez | @Override
|
1483 | 46684 | jjdelcerro | public IWindow getActiveWindow(Class<? extends Document> documentClass) { |
1484 | ApplicationManager application = ApplicationLocator.getManager(); |
||
1485 | |||
1486 | Document document;
|
||
1487 | IWindow[] windows = application.getUIManager().getOrderedWindows();
|
||
1488 | for (IWindow window : windows) {
|
||
1489 | if (window instanceof SingletonWindow && window instanceof IDocumentWindow) { |
||
1490 | // Cogemos no la primera ventana, si no la primera
|
||
1491 | // ventana de tipo documento (SingletonWindow).
|
||
1492 | // Y por si las mosca no es un documento, atrapamos
|
||
1493 | // los errores y continuamos si no puede hacer un cast
|
||
1494 | // del Model a Document
|
||
1495 | try {
|
||
1496 | if (documentClass == null) { |
||
1497 | return window;
|
||
1498 | } |
||
1499 | document = (Document) ((SingletonWindow) window).getWindowModel();
|
||
1500 | if (documentClass.isAssignableFrom(document.getClass())) {
|
||
1501 | return window;
|
||
1502 | } |
||
1503 | } catch (ClassCastException e) { |
||
1504 | // Do nothing, skip this window
|
||
1505 | } |
||
1506 | } |
||
1507 | } |
||
1508 | return null; |
||
1509 | } |
||
1510 | |||
1511 | @Override
|
||
1512 | 41148 | jjdelcerro | public void addObserver(Observer o) { |
1513 | observableHelper.addObserver(o); |
||
1514 | } |
||
1515 | 40435 | jjdelcerro | |
1516 | 44088 | omartinez | @Override
|
1517 | 41148 | jjdelcerro | public void deleteObserver(Observer o) { |
1518 | observableHelper.deleteObserver(o); |
||
1519 | } |
||
1520 | |||
1521 | 44088 | omartinez | @Override
|
1522 | 41148 | jjdelcerro | public void deleteObservers() { |
1523 | observableHelper.deleteObservers(); |
||
1524 | } |
||
1525 | |||
1526 | private ProjectNotification notifyObservers(int type) { |
||
1527 | 43913 | jjdelcerro | return notifyObservers(new DefaultProjectNotification(type)); |
1528 | 41148 | jjdelcerro | } |
1529 | 40435 | jjdelcerro | |
1530 | 41148 | jjdelcerro | private ProjectNotification notifyObservers(int type, Document document) { |
1531 | 43913 | jjdelcerro | return notifyObservers(new DefaultProjectNotification(type, document)); |
1532 | 41148 | jjdelcerro | } |
1533 | 43913 | jjdelcerro | |
1534 | private ProjectNotification notifyObservers(int type, PersistentState state) { |
||
1535 | return notifyObservers(new DefaultProjectNotification(type, state)); |
||
1536 | } |
||
1537 | 40435 | jjdelcerro | |
1538 | 41148 | jjdelcerro | private ProjectNotification notifyObservers(int type, File file) { |
1539 | 43913 | jjdelcerro | return notifyObservers(new DefaultProjectNotification(type, file)); |
1540 | } |
||
1541 | |||
1542 | private ProjectNotification notifyObservers(ProjectNotification notifycation) {
|
||
1543 | 41148 | jjdelcerro | try {
|
1544 | observableHelper.notifyObservers(this, notifycation);
|
||
1545 | } catch (Exception ex) { |
||
1546 | LOG.info("Can't notify observers", ex);
|
||
1547 | } |
||
1548 | 45978 | jjdelcerro | notifyProjectManagerEvent(notifycation.getNotificationType()); |
1549 | 41148 | jjdelcerro | return notifycation;
|
1550 | } |
||
1551 | 43913 | jjdelcerro | |
1552 | 45978 | jjdelcerro | private void notifyProjectManagerEvent(int type) { |
1553 | try {
|
||
1554 | ProjectManager.getInstance().notifyProjectEvent(new ProjectEventImpl(this, type)); |
||
1555 | } catch (Exception ex) { |
||
1556 | LOG.info("Can't notify project manager events", ex);
|
||
1557 | } |
||
1558 | } |
||
1559 | |||
1560 | 41148 | jjdelcerro | class DefaultProjectNotification implements ProjectNotification { |
1561 | 40435 | jjdelcerro | |
1562 | 41148 | jjdelcerro | private int type; |
1563 | private Document document; |
||
1564 | private File file; |
||
1565 | private boolean processCanceled = false; |
||
1566 | 43913 | jjdelcerro | private PersistentState state = null; |
1567 | 40435 | jjdelcerro | |
1568 | 43913 | jjdelcerro | DefaultProjectNotification(int type) {
|
1569 | 41148 | jjdelcerro | this.type = type;
|
1570 | 43913 | jjdelcerro | } |
1571 | |||
1572 | DefaultProjectNotification(int type, Document document) { |
||
1573 | this(type);
|
||
1574 | 41148 | jjdelcerro | this.document = document;
|
1575 | 43913 | jjdelcerro | } |
1576 | |||
1577 | DefaultProjectNotification(int type, File file) { |
||
1578 | this(type);
|
||
1579 | 41148 | jjdelcerro | this.file = file;
|
1580 | } |
||
1581 | 40435 | jjdelcerro | |
1582 | 43913 | jjdelcerro | DefaultProjectNotification(int type, PersistentState state) {
|
1583 | this(type);
|
||
1584 | this.state = state;
|
||
1585 | } |
||
1586 | |||
1587 | @Override
|
||
1588 | 41148 | jjdelcerro | public int getNotificationType() { |
1589 | return type;
|
||
1590 | } |
||
1591 | 40435 | jjdelcerro | |
1592 | 43913 | jjdelcerro | @Override
|
1593 | 41148 | jjdelcerro | public Document getDocument() { |
1594 | return document;
|
||
1595 | } |
||
1596 | |||
1597 | 43913 | jjdelcerro | @Override
|
1598 | 41148 | jjdelcerro | public void cancelProcess() { |
1599 | processCanceled = true;
|
||
1600 | } |
||
1601 | |||
1602 | 43913 | jjdelcerro | @Override
|
1603 | 41148 | jjdelcerro | public boolean isProcessCanceled() { |
1604 | return processCanceled;
|
||
1605 | } |
||
1606 | |||
1607 | 43913 | jjdelcerro | @Override
|
1608 | 41148 | jjdelcerro | public File getFile() { |
1609 | return file;
|
||
1610 | } |
||
1611 | |||
1612 | 43913 | jjdelcerro | @Override
|
1613 | public PersistentState getState() {
|
||
1614 | return this.state; |
||
1615 | } |
||
1616 | |||
1617 | @Override
|
||
1618 | public Project getProject() {
|
||
1619 | return DefaultProject.this;
|
||
1620 | } |
||
1621 | 41148 | jjdelcerro | } |
1622 | |||
1623 | 40435 | jjdelcerro | } |