001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package net.sourceforge.jrefactory.action;
053:
054: import java.awt.event.ActionEvent;
055: import javax.swing.JComboBox;
056: import javax.swing.JDialog;
057: import javax.swing.JMenuItem;
058: import javax.swing.JPopupMenu;
059: import net.sourceforge.jrefactory.uml.UMLPackage;
060: import org.acm.seguin.refactor.Refactoring;
061: import org.acm.seguin.refactor.RefactoringFactory;
062: import org.acm.seguin.refactor.type.AddChildRefactoring;
063: import org.acm.seguin.summary.TypeSummary;
064: import org.acm.seguin.summary.query.GetPackageSummary;
065:
066: /**
067: * This refactoring Action adds a child class (subclass) to a class.
068: *
069: * @author Chris Seguin
070: * @author Mike Atkinson
071: * @since 2.9.12
072: * @version $Id: AddChildClassAction.java,v 1.1 2003/12/02 23:34:19 mikeatkinson Exp $
073: */
074: public class AddChildClassAction extends RefactoringAction {
075: /**
076: * Constructor for the AddChildClassAction object
077: *
078: * @param init Description of Parameter
079: * @since 2.9.12
080: */
081: public AddChildClassAction(SelectedFileSet init) {
082: super (init);
083: initNames();
084: }
085:
086: /**
087: * Constructor for the AddChildClassAction object
088: *
089: * @param initPackage The package containing the class (and child class)
090: * @param initType The class to create a child of.
091: * @param initMenu if not null then the menu that contained the action.
092: * @param initItem if not null then the menuitem that contained the action.
093: * @since 2.9.12
094: */
095: public AddChildClassAction(UMLPackage initPackage,
096: TypeSummary initType, JPopupMenu initMenu,
097: JMenuItem initItem) {
098: this (null);
099: currentPackage = initPackage;
100: typeSummary = initType;
101: setPopupMenuListener(new AddChildClassListener(initMenu,
102: initItem));
103: initNames();
104: }
105:
106: /**
107: * Is this action enabled?
108: *
109: * @return If <code>true</code> then enable this action's menu item or button.
110: * @since 2.9.12
111: */
112: public boolean isEnabled() {
113: return isSingleJavaFile();
114: }
115:
116: /**
117: * Called to activate the AddChildClassListener.
118: *
119: * @param typeSummaryArray Description of Parameter
120: * @param evt Description of Parameter
121: * @since 2.9.12
122: */
123: protected void activateListener(TypeSummary[] typeSummaryArray,
124: ActionEvent evt) {
125: typeSummary = typeSummaryArray[0];
126: AddChildClassListener accl = new AddChildClassListener(null,
127: null);
128: accl.actionPerformed(evt);
129: }
130:
131: /**
132: * Initialise the Action values (NAME, SHORT_DESCRIPTION, LONG_DESCRIPTION).
133: *
134: * @since 2.9.12
135: */
136: private void initNames() {
137: putValue(NAME, "Add Child Class");
138: putValue(SHORT_DESCRIPTION, "Add Child Class");
139: putValue(LONG_DESCRIPTION,
140: "Allows the user to add a child class");
141: }
142:
143: /**
144: * Adds a child class listener
145: *
146: * @author Chris Seguin
147: * @since 2.9.12
148: * @created September 12, 2001
149: */
150: public class AddChildClassListener extends DialogViewListener {
151: /**
152: * Constructor for the AddChildClassListener object
153: *
154: * @param initMenu The popup menu
155: * @param initItem The current item
156: * @since 2.9.12
157: */
158: public AddChildClassListener(JPopupMenu initMenu,
159: JMenuItem initItem) {
160: super (initMenu, initItem);
161: }
162:
163: /**
164: * Gets the typeSummaries attribute of the AddChildClassListener object
165: *
166: * @return The typeSummaries value
167: * @since 2.9.12
168: */
169: public TypeSummary[] getTypeSummaries() {
170: return new TypeSummary[] { typeSummary };
171: }
172:
173: /**
174: * Creates an appropriate dialog to prompt the user for additional input
175: *
176: * @return the dialog box
177: * @since 2.9.12
178: */
179: protected JDialog createDialog() {
180: return new AddChildClassDialog();
181: }
182: }
183:
184: /**
185: * Creates a dialog box to prompt for the new parent name
186: *
187: * @author Chris Seguin
188: * @since 2.9.12
189: * @created September 12, 2001
190: */
191: public class AddChildClassDialog extends ClassNameDialog {
192: private JComboBox packageNameBox;
193:
194: /**
195: * Constructor for AddAbstractParentDialog
196: *
197: * @since 2.9.12
198: */
199: public AddChildClassDialog() {
200: super (2);
201:
202: PackageList packageList = new PackageList();
203: packageNameBox = packageList.add(this );
204:
205: String name;
206: if (currentPackage == null) {
207: name = GetPackageSummary.query(typeSummary).getName();
208: } else {
209: name = currentPackage.getSummary().getName();
210: }
211: packageNameBox.setSelectedItem(name);
212:
213: pack();
214:
215: org.acm.seguin.awt.CenterDialog
216: .center(this , currentPackage);
217: }
218:
219: /**
220: * Returns the window title
221: *
222: * @return the title
223: * @since 2.9.12
224: */
225: public String getWindowTitle() {
226: return "Add a child class";
227: }
228:
229: /**
230: * Gets the label for the text
231: *
232: * @return the text for the label
233: * @since 2.9.12
234: */
235: public String getLabelText() {
236: return "Child class:";
237: }
238:
239: /**
240: * Adds an abstract parent class to all specified classes.
241: *
242: * @return the refactoring
243: * @since 2.9.12
244: */
245: protected Refactoring createRefactoring() {
246: // Create system
247: AddChildRefactoring refactoring = RefactoringFactory.get()
248: .addChild();
249: refactoring.setChildName(getClassName());
250:
251: // Add the type
252: refactoring.setParentClass(typeSummary);
253:
254: refactoring.setPackageName((String) packageNameBox
255: .getSelectedItem());
256:
257: return refactoring;
258: }
259: }
260:
261: }
|