001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.refactor.undo;
010:
011: import java.io.File;
012: import java.io.Serializable;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import org.acm.seguin.util.FileSettings;
016: import org.acm.seguin.util.MissingSettingsException;
017:
018: /**
019: * Stores the undo operation. The undo operation consists of a description of
020: * the refactoring that was performed to create this UndoAction and a list of
021: * files that have changed. <P>
022: *
023: * The files that have changed are indexed files, in that they have an index
024: * appended to the name of the file.
025: *
026: *@author Chris Seguin
027: */
028: public class DefaultUndoAction implements UndoAction, Serializable {
029: /**
030: * A description of the refactoring
031: *
032: *@serial true
033: */
034: private String description;
035:
036: /**
037: * A linked list
038: *
039: *@serial true
040: */
041: private LinkedList list;
042:
043: /**
044: * Constructor for the UndoAction object
045: *
046: */
047: public DefaultUndoAction() {
048: //System.out.println("DefaultUndoAction()");
049: description = "unknown";
050: list = new LinkedList();
051: }
052:
053: /**
054: * Sets the Description attribute of the UndoAction object
055: *
056: *@param description the description of the action
057: */
058: public void setDescription(String description) {
059: //System.out.println("DefaultUndoAction.setDescription("+description+")");
060: this .description = description;
061: }
062:
063: /**
064: * Gets the Description attribute of the UndoAction object
065: *
066: *@return The Description value
067: */
068: public String getDescription() {
069: //System.out.println("DefaultUndoAction.setDescription() => "+description);
070: return description;
071: }
072:
073: /**
074: * Adds a file to this action
075: *
076: *@param oldFile the original file
077: *@param newFile the new file
078: */
079: public void add(File oldFile, File newFile) {
080: //System.out.println("DefaultUndoAction.add("+oldFile+","+newFile+")");
081: File dest = null;
082: if (oldFile == null) {
083: dest = null;
084: } else {
085: // Get the parent directory and the name
086: File parent = oldFile.getParentFile();
087: String name = oldFile.getName();
088:
089: // Find the file's next index location
090: dest = findNextStorageSlot(parent, name);
091:
092: // Renames the file
093: oldFile.renameTo(dest);
094: }
095:
096: //System.out.println(" list.add(new FileSet("+oldFile+","+dest+","+newFile+"))");
097: list.add(new FileSet(oldFile, dest, newFile));
098: }
099:
100: /**
101: * Undo the current action
102: */
103: public void undo() {
104: //System.out.println("UndoAction.undo()");
105: Iterator iter = list.iterator();
106: while (iter.hasNext()) {
107: ((FileSet) iter.next()).undo();
108: }
109: }
110:
111: /**
112: * Gets the NextName attribute of the UndoAction object
113: *
114: *@param name Description of Parameter
115: *@param index Description of Parameter
116: *@param pattern Description of Parameter
117: *@return The NextName value
118: */
119: private String getNextName(String name, int index, String pattern) {
120: StringBuffer buffer = new StringBuffer(name);
121: char ch;
122:
123: for (int ndx = 0; ndx < pattern.length(); ndx++) {
124: ch = pattern.charAt(ndx);
125: if (ch == '#') {
126: buffer.append(index);
127: } else {
128: buffer.append(ch);
129: }
130: }
131:
132: return buffer.toString();
133: }
134:
135: /**
136: * Finds the next slot to store the original file
137: *
138: *@param dir the directory that the original file is contained in
139: *@param name the name of the original file
140: *@return Description of the Returned Value
141: */
142: private File findNextStorageSlot(File dir, String name) {
143: String pattern = ".#";
144:
145: try {
146: FileSettings umlBundle = FileSettings
147: .getRefactorySettings("uml");
148: umlBundle.setContinuallyReload(true);
149: pattern = umlBundle.getString("backup.ext");
150: } catch (MissingSettingsException mse) {
151: // The default is fine
152: }
153:
154: File dest = null;
155: int index = 0;
156: do {
157: dest = new File(dir, getNextName(name, index, pattern));
158: index++;
159: } while (dest.exists());
160: return dest;
161: }
162: }
|