001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package org.acm.seguin.ide.jedit.action;
017:
018: import java.awt.event.ActionEvent;
019:
020: import java.io.File;
021: import java.io.Serializable;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import org.acm.seguin.refactor.undo.FileSet;
025: import org.acm.seguin.refactor.undo.UndoAction;
026: import org.acm.seguin.util.FileSettings;
027: import org.acm.seguin.util.MissingSettingsException;
028:
029: import org.gjt.sp.jedit.jEdit;
030:
031: /**
032: * Stores the undo operation. The undo operation consists of a description of the refactoring that was performed to
033: * create this UndoAction and a list of files that have changed. <P>
034: *
035: * The files that have changed are indexed files, in that they have an index appended to the name of the file.
036: *
037: * @author <a href="mailto:JRefactoryPlugin@ladyshot.demon.co.uk">Mike Atkinson</a>
038: * @created 23 July 2003
039: * @version $Id: JEditUndoAction.java,v 1.2 2003/09/25 13:30:36 mikeatkinson Exp $
040: * @since 0.1
041: */
042: public class JEditUndoAction extends Action implements UndoAction,
043: Serializable {
044: /**
045: * A description of the refactoring
046: *
047: * @serial true
048: */
049: private String description;
050:
051: /**
052: * A linked list
053: *
054: * @serial true
055: */
056: private LinkedList list;
057:
058: /** Constructor for the UndoAction object */
059: public JEditUndoAction() {
060: //System.out.println("JEditUndoAction()");
061: description = "unknown";
062: list = new LinkedList();
063: }
064:
065: /**
066: * Sets the Description attribute of the UndoAction object
067: *
068: * @param description the description of the action
069: */
070: public void setDescription(String description) {
071: //System.out.println("JEditUndoAction.setDescription(" + description + ")");
072: this .description = description;
073: }
074:
075: /**
076: * Returns the text to be shown on the button and/or menu item.
077: *
078: * @return The text value
079: */
080: public String getText() {
081: return jEdit.getProperty("javastyle.action.undo");
082: }
083:
084: /**
085: * Gets the Description attribute of the UndoAction object
086: *
087: * @return The Description value
088: */
089: public String getDescription() {
090: //System.out.println("JEditUndoAction.setDescription() => " + description);
091: return description;
092: }
093:
094: /**
095: * Description of the Method
096: *
097: * @param ae Description of the Parameter
098: */
099: public void actionPerformed(ActionEvent ae) {
100: //System.out.println("JEditUndoAction.actionPerformed(" + ae + ")");
101: undo();
102: }
103:
104: /**
105: * Adds a file to this action
106: *
107: * @param oldFile the original file
108: * @param newFile the new file
109: */
110: public void add(File oldFile, File newFile) {
111: //System.out.println("JEditUndoAction.add(" + oldFile + "," + newFile + ")");
112: File dest = null;
113: if (oldFile == null) {
114: dest = null;
115: } else {
116: // Get the parent directory and the name
117: File parent = oldFile.getParentFile();
118: String name = oldFile.getName();
119:
120: // Find the file's next index location
121: dest = findNextStorageSlot(parent, name);
122:
123: // Renames the file
124: oldFile.renameTo(dest);
125: }
126:
127: //System.out.println(" list.add(new FileSet(" + oldFile + "," + dest + "," + newFile + "))");
128: list.add(new FileSet(oldFile, dest, newFile));
129: }
130:
131: /** Undo the current action */
132: public void undo() {
133: //System.out.println("JEditUndoAction.undo()");
134: Iterator iter = list.iterator();
135: while (iter.hasNext()) {
136: ((FileSet) iter.next()).undo();
137: }
138: }
139:
140: /**
141: * Gets the NextName attribute of the UndoAction object
142: *
143: * @param name Description of Parameter
144: * @param index Description of Parameter
145: * @param pattern Description of Parameter
146: * @return The NextName value
147: */
148: private String getNextName(String name, int index, String pattern) {
149: StringBuffer buffer = new StringBuffer(name);
150: char ch;
151:
152: for (int ndx = 0; ndx < pattern.length(); ndx++) {
153: ch = pattern.charAt(ndx);
154: if (ch == '#') {
155: buffer.append(index);
156: } else {
157: buffer.append(ch);
158: }
159: }
160:
161: return buffer.toString();
162: }
163:
164: /**
165: * Finds the next slot to store the original file
166: *
167: * @param dir the directory that the original file is contained in
168: * @param name the name of the original file
169: * @return Description of the Returned Value
170: */
171: private File findNextStorageSlot(File dir, String name) {
172: String pattern = ".#";
173:
174: try {
175: FileSettings umlBundle = FileSettings
176: .getRefactorySettings("uml");
177: umlBundle.setContinuallyReload(true);
178: pattern = umlBundle.getString("backup.ext");
179: } catch (MissingSettingsException mse) {
180: // The default is fine
181: }
182:
183: File dest = null;
184: int index = 0;
185: do {
186: dest = new File(dir, getNextName(name, index, pattern));
187: index++;
188: } while (dest.exists());
189: return dest;
190: }
191: }
|