001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.plugin;
034:
035: import java.io.File;
036: import java.io.StringWriter;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.Iterator;
040:
041: import com.vividsolutions.jump.I18N;
042: import com.vividsolutions.jump.util.FileUtil;
043: import com.vividsolutions.jump.util.java2xml.Java2XML;
044: import com.vividsolutions.jump.workbench.model.Layer;
045: import com.vividsolutions.jump.workbench.model.Task;
046: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
047: import com.vividsolutions.jump.workbench.ui.GUIUtil;
048: import com.vividsolutions.jump.workbench.ui.WorkbenchFrame;
049:
050: /**
051: * Subclass this to implement a 'Save Project' plugin.
052: */
053: public abstract class AbstractSaveProjectPlugIn extends AbstractPlugIn {
054: public AbstractSaveProjectPlugIn() {
055: }
056:
057: protected void save(Task task, File file, WorkbenchFrame frame)
058: throws Exception {
059: //First use StringWriter to make sure no errors occur before we touch the
060: //original file -- we don't want to damage the original if an error occurs.
061: //[Jon Aquino]
062: StringWriter stringWriter = new StringWriter();
063:
064: try {
065: new Java2XML().write(task, "project", stringWriter);
066: } finally {
067: stringWriter.flush();
068: }
069:
070: FileUtil.setContents(file.getAbsolutePath(), stringWriter
071: .toString());
072: task.setName(GUIUtil.nameWithoutExtension(file));
073: task.setProjectFile(file);
074:
075: ArrayList ignoredLayers = new ArrayList(ignoredLayers(task));
076:
077: if (!ignoredLayers.isEmpty()) {
078: String warning = I18N
079: .get("ui.plugin.AbstractSaveProjectPlugIn.some-layers-were-not-saved-to-the-task-file")
080: + " ";
081:
082: for (int i = 0; i < ignoredLayers.size(); i++) {
083: Layer ignoredLayer = (Layer) ignoredLayers.get(i);
084:
085: if (i > 0) {
086: warning += "; ";
087: }
088:
089: warning += ignoredLayer.getName();
090: }
091:
092: warning += " ("
093: + I18N
094: .get("ui.plugin.AbstractSaveProjectPlugIn.data-source-is-write-only")
095: + ")";
096:
097: frame.warnUser(warning);
098: }
099: }
100:
101: private Collection ignoredLayers(Task task) {
102: ArrayList ignoredLayers = new ArrayList();
103:
104: for (Iterator i = task.getLayerManager().getLayers().iterator(); i
105: .hasNext();) {
106: Layer layer = (Layer) i.next();
107:
108: if (!layer.hasReadableDataSource()) {
109: ignoredLayers.add(layer);
110: }
111: }
112:
113: return ignoredLayers;
114: }
115: }
|