001: package org.acm.seguin.uml.refactor;
002:
003: import java.awt.event.ActionEvent;
004: import java.util.Iterator;
005: import javax.swing.JMenuItem;
006: import javax.swing.JOptionPane;
007: import javax.swing.JPopupMenu;
008: import org.acm.seguin.awt.ExceptionPrinter;
009: import org.acm.seguin.refactor.Refactoring;
010: import org.acm.seguin.refactor.RefactoringException;
011: import org.acm.seguin.refactor.RefactoringFactory;
012: import org.acm.seguin.refactor.field.RenameFieldRefactoring;
013: import org.acm.seguin.refactor.method.RenameParameterRefactoring;
014: import org.acm.seguin.summary.*;
015: import org.acm.seguin.uml.PopupMenuListener;
016: import org.acm.seguin.uml.loader.ReloaderSingleton;
017:
018: /**
019: * Performs the hungarian name renaming to an entire class
020: *
021: *@author Chris Seguin
022: *@created July 16, 2002
023: */
024: public class BatchRename extends PopupMenuListener {
025: private TypeSummary type;
026:
027: /**
028: * Constructor for the BatchRename object
029: *
030: *@param initMenu The popup menu
031: *@param initItem The current item
032: *@param typeSummary Description of the Parameter
033: */
034: public BatchRename(JPopupMenu initMenu, JMenuItem initItem,
035: TypeSummary typeSummary) {
036: super (initMenu, initItem);
037: type = typeSummary;
038: }
039:
040: /**
041: * A menu item has been selected, display the dialog box
042: *
043: *@param evt the action event
044: */
045: public void actionPerformed(ActionEvent evt) {
046: super .actionPerformed(evt);
047: run(type);
048:
049: // Update the GUIs
050: ReloaderSingleton.reload();
051: }
052:
053: /**
054: * Main processing method for the BatchRename object
055: *
056: *@param typeSummary Description of the Parameter
057: */
058: public void run(TypeSummary typeSummary) {
059: // Over the fields
060: Iterator iter = typeSummary.getFields();
061: if (iter != null) {
062: while (iter.hasNext()) {
063: FieldSummary nextFieldSummary = (FieldSummary) iter
064: .next();
065: System.out.println("Renaming: "
066: + nextFieldSummary.getName());
067: try {
068: renameField(typeSummary, nextFieldSummary);
069: } catch (Exception exc) {
070: exc.printStackTrace();
071: }
072: }
073: }
074:
075: // Over the methods
076: iter = typeSummary.getMethods();
077: if (iter != null) {
078: while (iter.hasNext()) {
079: MethodSummary next = (MethodSummary) iter.next();
080: visit(next);
081: }
082: }
083:
084: // Over the types
085: iter = typeSummary.getTypes();
086: if (iter != null) {
087: while (iter.hasNext()) {
088: TypeSummary next = (TypeSummary) iter.next();
089: run(next);
090: }
091: }
092: }
093:
094: /**
095: * Renames a field to use hungarian notation
096: *
097: *@param fieldSummary Description of the Parameter
098: *@param typeSummary Description of the Parameter
099: */
100: private void renameField(TypeSummary typeSummary,
101: FieldSummary fieldSummary) {
102: HungarianNamer namer = new HungarianNamer();
103: String newName = namer.getDefaultName(fieldSummary, "m_");
104:
105: if (newName.equals(fieldSummary.getName()))
106: return;
107:
108: RenameFieldRefactoring rfr = RefactoringFactory.get()
109: .renameField();
110: rfr.setClass(typeSummary);
111: rfr.setField(fieldSummary.getName());
112: rfr.setNewName(newName);
113: runRefactoring(rfr);
114: }
115:
116: /**
117: * Renames a parameter to have hungarian notation
118: *
119: *@param method Description of the Parameter
120: *@param param Description of the Parameter
121: */
122: private void renameParameter(MethodSummary method,
123: ParameterSummary param) {
124: HungarianNamer namer = new HungarianNamer();
125: RenameParameterRefactoring rpr = RefactoringFactory.get()
126: .renameParameter();
127: rpr.setMethodSummary(method);
128: rpr.setParameterSummary(param);
129: rpr.setNewName(namer.getDefaultName(param, "a_"));
130: runRefactoring(rpr);
131: }
132:
133: /**
134: * Description of the Method
135: *
136: *@param refactoring Description of the Parameter
137: */
138: private void runRefactoring(Refactoring refactoring) {
139: // Update the code
140: try {
141: refactoring.run();
142: } catch (RefactoringException re) {
143: JOptionPane.showMessageDialog(null, re.getMessage(),
144: "Refactoring Exception", JOptionPane.ERROR_MESSAGE);
145: } catch (Throwable thrown) {
146: ExceptionPrinter.print(thrown, true);
147: }
148: }
149:
150: /**
151: * Description of the Method
152: *
153: *@param methodSummary Description of the Parameter
154: */
155: private void visit(MethodSummary methodSummary) {
156: Iterator iter = methodSummary.getParameters();
157: if (iter != null) {
158: while (iter.hasNext()) {
159: ParameterSummary nextParameterSummary = (ParameterSummary) iter
160: .next();
161: System.out.println("Renaming: "
162: + nextParameterSummary.getName());
163: try {
164: renameParameter(methodSummary, nextParameterSummary);
165: } catch (Exception exc) {
166: exc.printStackTrace();
167: }
168: }
169: }
170: }
171: }
|