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 org.acm.seguin.ide.elixir;
053:
054: import com.elixirtech.fx.FrameManager;
055: import com.elixirtech.tree.TNode;
056: import com.elixirtech.tree.TModel;
057: import com.elixirtech.tree.TParent;
058: import com.elixirtech.util.SortUtil;
059: import java.io.File;
060: import java.io.BufferedReader;
061: import java.io.FileReader;
062: import java.io.IOException;
063: import java.awt.event.ActionListener;
064: import java.awt.event.ActionEvent;
065: import java.util.Enumeration;
066: import javax.swing.Icon;
067: import javax.swing.JPopupMenu;
068: import javax.swing.JMenuItem;
069: import javax.swing.tree.TreePath;
070: import javax.swing.tree.TreeNode;
071: import java.util.Vector;
072: import java.util.StringTokenizer;
073: import org.acm.seguin.ide.common.UMLIcon;
074:
075: /**
076: * Stores a leaf node for a UML class diagram
077: *
078: *@author Chris Seguin
079: *@created October 18, 2001
080: */
081: class UMLLeaf implements TNode {
082: private UMLDocManager docManager;
083: private File file;
084: private String packageName;
085: private TNode parent;
086:
087: /**
088: * Constructor for the UMLLeaf object
089: *
090: *@param parent the parent file
091: *@param file the file
092: *@param docManager the document manager
093: */
094: public UMLLeaf(TNode parent, File file, UMLDocManager docManager) {
095: this .parent = parent;
096: this .file = file;
097: this .docManager = docManager;
098:
099: loadPackageName();
100: }
101:
102: /**
103: * Sets a new name for the node.
104: *
105: *@param name The new Name value
106: */
107: public void setName(String name) {
108: file = new File(name);
109: }
110:
111: /**
112: * Sets a new parent for the node.
113: *
114: *@param value The new Parent value
115: */
116: public void setParent(TParent value) {
117: parent = value;
118: }
119:
120: /**
121: * Can we add children to this
122: *
123: *@return The AllowsChildren value
124: */
125: public boolean getAllowsChildren() {
126: return false;
127: }
128:
129: /**
130: * Return the child from an index
131: *
132: *@param idx Description of Parameter
133: *@return The ChildAt value
134: */
135: public TreeNode getChildAt(int idx) {
136: return null;
137: }
138:
139: /**
140: * Count the children
141: *
142: *@return The ChildCount value
143: */
144: public int getChildCount() {
145: return 0;
146: }
147:
148: /**
149: * Get the full name of the node, usually composed by walking the TreePath
150: * a/b/c etc.
151: *
152: *@return The FullName value
153: */
154: public String getFullName() {
155: try {
156: return file.getCanonicalPath();
157: } catch (IOException ioe) {
158: return file.getPath();
159: }
160: }
161:
162: /**
163: * Get the icon to show in the tree
164: *
165: *@param expanded Description of Parameter
166: *@return The Icon value
167: */
168: public Icon getIcon(boolean expanded) {
169: return new UMLIcon();
170: }
171:
172: /**
173: * Return the index of a child. This has no children so always returns -1.
174: *
175: *@param child Description of Parameter
176: *@return The Index value
177: */
178: public int getIndex(TreeNode child) {
179: return -1;
180: }
181:
182: /**
183: * Get the name to show in the tree
184: *
185: *@return The Name value
186: */
187: public String getName() {
188: return packageName + " Class Diagram";
189: }
190:
191: /**
192: * Gets the Parent attribute of the UMLLeaf object
193: *
194: *@return The Parent value
195: */
196: public TreeNode getParent() {
197: return parent;
198: }
199:
200: /**
201: * Get the popup menu to show for this node
202: *
203: *@return The PopupMenu value
204: */
205: public JPopupMenu getPopupMenu() {
206: JPopupMenu result = new JPopupMenu();
207: JMenuItem item = new JMenuItem("Open");
208: item.addActionListener(new OpenFileAdapter(getFullName()));
209: result.add(item);
210: return result;
211: }
212:
213: /**
214: * Return the tooltip help to be shown when the mouse is over this node.
215: *
216: *@return The ToolTipText value
217: */
218: public String getToolTipText() {
219: if (packageName.length() > 0) {
220: return "The class diagram for the package " + packageName;
221: } else {
222: return "The class diagram for the top level package";
223: }
224: }
225:
226: /**
227: * Return the model which this node belongs to
228: *
229: *@return The TreeModel value
230: */
231: public TModel getTreeModel() {
232: return parent.getTreeModel();
233: }
234:
235: /**
236: * Get the TreePath which represents this node
237: *
238: *@return The TreePath value
239: */
240: public TreePath getTreePath() {
241: return parent.getTreePath().pathByAddingChild(this );
242: }
243:
244: /**
245: * Gets the Leaf attribute of the UMLLeaf object
246: *
247: *@return The Leaf value
248: */
249: public boolean isLeaf() {
250: return true;
251: }
252:
253: /**
254: * Gets an enumeration of the children
255: *
256: *@return An empty enumeration
257: */
258: public Enumeration children() {
259: Vector result = new Vector();
260: return result.elements();
261: }
262:
263: /**
264: * Perform double-click action. Hopefully this will open the file.
265: */
266: public void doDoubleClick() {
267: FrameManager.current().open(getFullName());
268: }
269:
270: /**
271: * Notify the TreeModel and hence the TreeModel listeners that this node
272: * has changed
273: */
274: public void fireChanged() {
275: }
276:
277: /**
278: * Loads the package name from the file
279: */
280: private void loadPackageName() {
281: try {
282: packageName = "Unknown";
283:
284: BufferedReader input = new BufferedReader(new FileReader(
285: file));
286:
287: String line = input.readLine();
288: if (line.charAt(0) == 'V') {
289: StringTokenizer tok = new StringTokenizer(line, "[:]");
290: if (tok.hasMoreTokens()) {
291: // Skip the first - it is the letter v
292: String temp = tok.nextToken();
293: if (tok.hasMoreTokens()) {
294: // Skip the second - it is the version (1.1)
295: temp = tok.nextToken();
296: if (tok.hasMoreTokens()) {
297: // Third item is the package name
298: packageName = tok.nextToken();
299: }
300: }
301: }
302: }
303:
304: input.close();
305: } catch (IOException ioe) {
306: }
307: }
308:
309: /**
310: * Sort the children of this node based on the comparator. Since there are
311: * no children, this does nothing.
312: *
313: *@param c the comparator
314: */
315: public void sortChildren(SortUtil.Comparator c) {
316: }
317:
318: /**
319: * Return the name of the node as its String representation
320: *
321: *@return Gets the string representation of this node
322: */
323: public String toString() {
324: return getName();
325: }
326:
327: /**
328: * Opens a file when the button is pressed
329: *
330: *@author Chris Seguin
331: *@created October 18, 2001
332: */
333: private class OpenFileAdapter implements ActionListener {
334: private String name;
335:
336: /**
337: * Constructor for the OpenFileAdapter object
338: *
339: *@param init the name of the file
340: */
341: public OpenFileAdapter(String init) {
342: name = init;
343: }
344:
345: /**
346: * Opens the file
347: *
348: *@param evt the action event
349: */
350: public void actionPerformed(ActionEvent evt) {
351: FrameManager.current().open(name);
352: }
353: }
354: }
355: // EOF
|