001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.style.internal;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import net.refractions.udig.project.command.AbstractCommand;
024: import net.refractions.udig.project.command.UndoableCommand;
025: import net.refractions.udig.project.internal.Layer;
026: import net.refractions.udig.project.internal.LayerDecorator;
027: import net.refractions.udig.project.internal.StyleBlackboard;
028: import net.refractions.udig.project.internal.StyleEntry;
029:
030: import org.eclipse.core.runtime.IProgressMonitor;
031:
032: /**
033: * Layer decorator that provides commit/revert on StyleBlackboard.
034: *
035: * @author jgarnett
036: * @since 0.7.0
037: */
038: public class StyleLayer extends LayerDecorator {
039:
040: /** Our working copy of the blackboard */
041: StyleBlackboard blackboard;
042:
043: /** Our backup copy of the blackboard before modifications */
044: StyleBlackboard originalBlackboard;
045:
046: /**
047: * Construct <code>StyleLayer</code>.
048: *
049: * @param layer
050: */
051: public StyleLayer(Layer layer) {
052: super (layer);
053: }
054:
055: /*
056: * @see net.refractions.udig.project.LayerDecorator#getStyleBlackboard()
057: */
058: public synchronized StyleBlackboard getStyleBlackboard() {
059: if (blackboard == null) {
060: if (originalBlackboard == null)
061: originalBlackboard = layer.getStyleBlackboard();
062: blackboard = (StyleBlackboard) layer.getStyleBlackboard()
063: .clone();
064: }
065: return blackboard;
066: }
067:
068: /**
069: * Revert to the current blackboard, any recent modifications will be lost.
070: */
071: public synchronized void revert() {
072: blackboard = null;
073: getStyleBlackboard(); //reloads the blackboard currently in use
074: }
075:
076: /**
077: * Revert to the initial blackboard, any existing modifications will be lost.
078: */
079: public synchronized void revertAll() {
080: blackboard = (StyleBlackboard) originalBlackboard.clone();
081: }
082:
083: /**
084: * Apply our blackboard to the original layer.
085: * <p>
086: * Will check to ensure they are indeed different before.
087: */
088: public void apply() {
089: ApplyStyleCommand applyCommand = new ApplyStyleCommand(layer,
090: layer.getStyleBlackboard(), getStyleBlackboard());
091: layer.getContextModel().getMap().sendCommandASync(applyCommand);
092: }
093: }
094:
095: /**
096: * MapCommand that changes the blackboard.
097: * <p>
098: * This command is used to submit a change to the the Map.
099: * </p>
100: * @author jgarnett
101: * @since 0.6.0
102: */
103: class ApplyStyleCommand extends AbstractCommand implements
104: UndoableCommand {
105: StyleBlackboard oldStyleBlackboard;
106: StyleBlackboard newStyleBlackboard;
107: Layer layer;
108:
109: /**
110: * Construct <code>ApplyStyleCommand</code>.
111: *
112: * @param layer
113: * @param oldStyleBlackboard
114: * @param newStyleBlackboard
115: */
116: public ApplyStyleCommand(Layer layer,
117: StyleBlackboard oldStyleBlackboard,
118: StyleBlackboard newStyleBlackboard) {
119: this .oldStyleBlackboard = oldStyleBlackboard;
120: this .newStyleBlackboard = newStyleBlackboard;
121: this .layer = layer;
122: }
123:
124: /* overwrite with the orignal blackboard
125: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
126: */
127: public void rollback(IProgressMonitor monitor) throws Exception {
128: layer.setStyleBlackboard(oldStyleBlackboard);
129: }
130:
131: /** overwrite with new blackboard */
132: public void run(IProgressMonitor monitor) throws Exception {
133: //FIXME: This is a temporary solution
134: List<StyleEntry> l = new ArrayList<StyleEntry>(
135: newStyleBlackboard.getContent());
136: List<String> selected = new ArrayList<String>();
137: for (Iterator itr = l.iterator(); itr.hasNext();) {
138: StyleEntry entry = (StyleEntry) itr.next();
139: if (entry.getStyle() != null) {
140: newStyleBlackboard.put(entry.getID(), entry.getStyle());
141: }
142: }
143: newStyleBlackboard.setSelected(selected
144: .toArray(new String[selected.size()]));
145: layer.setStyleBlackboard(newStyleBlackboard);
146: }
147:
148: /**
149: * @see net.refractions.udig.project.command.MapCommand#getName()
150: */
151: public String getName() {
152: return layer.getName();
153: }
154:
155: /*
156: * @see java.lang.Object#toString()
157: */
158: public String toString() {
159: return "StyleLayer<" + getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
160: }
161: }
|