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.uml;
053:
054: import net.sourceforge.jrefactory.ast.ModifierHolder;
055: import java.awt.*;
056: import java.awt.event.MouseListener;
057: import java.awt.event.MouseMotionListener;
058: import javax.swing.JPanel;
059: import org.acm.seguin.uml.line.DragPanelAdapter;
060: import org.acm.seguin.uml.line.EndPointPanel;
061: import org.acm.seguin.uml.line.SizableLabel;
062:
063: /**
064: * Displays a single UML line
065: *
066: *@author Chris Seguin
067: *@created July 6, 1999
068: */
069: public class UMLLine extends EndPointPanel {
070: // Instance Variables
071: /**
072: * Description of the Field
073: */
074: protected SizableLabel label;
075: /**
076: * Description of the Field
077: */
078: protected int wide;
079: /**
080: * Description of the Field
081: */
082: protected int high;
083: /**
084: * Stores the icon for this object
085: */
086: protected UMLIcon icon;
087: /**
088: * Description of the Field
089: */
090: private int iconWidth = 8;
091: /**
092: * Description of the Field
093: */
094: private int iconHeight = 8;
095: private int protection;
096: private UMLType parent;
097: private Font font;
098:
099: // Class Variables
100: /**
101: * Description of the Field
102: */
103: public final static int PUBLIC = 0;
104: /**
105: * Description of the Field
106: */
107: public final static int PROTECTED_PRIVATE = 1;
108: /**
109: * Description of the Field
110: */
111: public final static int PROTECTED = 2;
112: /**
113: * Description of the Field
114: */
115: public final static int DEFAULT = 3;
116: /**
117: * Description of the Field
118: */
119: public final static int PRIVATE = 4;
120: /**
121: * Description of the Field
122: */
123: protected static Color[] protectionColors = null;
124: /**
125: * Description of the Field
126: */
127: protected static Font defaultFont = null;
128: /**
129: * Description of the Field
130: */
131: protected static Font staticFont = null;
132: /**
133: * Description of the Field
134: */
135: protected static Font abstractFont = null;
136: /**
137: * Description of the Field
138: */
139: protected static Font titleFont = null;
140: /**
141: * Description of the Field
142: */
143: protected static Font abstractTitleFont = null;
144: /**
145: * Description of the Field
146: */
147: protected final static int iconMargin = 1;
148: /**
149: * Description of the Field
150: */
151: protected final static int labelMargin = 1;
152:
153: /**
154: * Create a new instance of a UMLLine
155: *
156: *@param parent Description of Parameter
157: *@param adapter Description of Parameter
158: */
159: public UMLLine(UMLType parent, DragPanelAdapter adapter) {
160: super (null, true);
161: this .parent = parent;
162: label = new SizableLabel("");
163: label.setLocation(iconWidth + 2 * iconMargin + labelMargin,
164: labelMargin);
165: add(label);
166:
167: // Add listeners
168: addMouseListener(adapter);
169: addMouseMotionListener(adapter);
170:
171: icon = new ProtectionIcon(iconWidth, iconHeight);
172: }
173:
174: /**
175: * Set the protection code
176: *
177: *@param code the code
178: */
179: public void setProtection(int code) {
180: protection = code;
181: if (icon instanceof ProtectionIcon) {
182: ((ProtectionIcon) icon).setProtection(code);
183: }
184: }
185:
186: /**
187: * Set the text
188: *
189: *@param msg the message
190: */
191: public void setLabelText(String msg) {
192: label.setText(msg);
193:
194: // Reset the height and width
195: Dimension labelSize = label.getPreferredSize();
196: label.setSize(labelSize);
197: high = Math.max(iconWidth + 2 * iconMargin, labelSize.height
198: + 2 * labelMargin);
199: wide = labelSize.width + iconWidth + 2 * iconMargin + 2
200: * labelMargin;
201: }
202:
203: /**
204: * Set the font
205: *
206: *@param font the new font
207: */
208: public void setLabelFont(Font font) {
209: label.setSLFont(font);
210:
211: // Reset the height and width
212: Dimension labelSize = label.getPreferredSize();
213: label.setSize(labelSize);
214: high = Math.max(iconWidth + 2 * iconMargin, labelSize.height
215: + 2 * labelMargin);
216: wide = labelSize.width + iconWidth + 2 * iconMargin + 2
217: * labelMargin;
218:
219: // Save the font
220: this .font = font;
221: }
222:
223: /**
224: * Sets the Selected attribute of the UMLLine object
225: *
226: *@param value The new Selected value
227: */
228: public void setSelected(boolean value) {
229: parent.setSelected(value);
230: }
231:
232: /**
233: * Returns the minimum size
234: *
235: *@return The size
236: */
237: public Dimension getMinimumSize() {
238: return getPreferredSize();
239: }
240:
241: /**
242: * Returns the preferred size
243: *
244: *@return The size
245: */
246: public Dimension getPreferredSize() {
247: return new Dimension(wide, high);
248: }
249:
250: /**
251: * Gets the ParentType attribute of the UMLLine object
252: *
253: *@return The ParentType value
254: */
255: public UMLType getParentType() {
256: return parent;
257: }
258:
259: /**
260: * Gets the Selected attribute of the UMLLine object
261: *
262: *@return The Selected value
263: */
264: public boolean isSelected() {
265: return parent.isSelected();
266: }
267:
268: /**
269: * Paint this object
270: *
271: *@param g the graphics object
272: */
273: public void paint(Graphics g) {
274: setBackground(getDefaultBackground());
275: super .paint(g);
276: drawIcon(g, 0, 0);
277: }
278:
279: /**
280: * Print this object
281: *
282: *@param g the graphics object
283: *@param x the x coordinate
284: *@param y the y coordinate
285: */
286: public void print(Graphics g, int x, int y) {
287: Point pt = label.getLocation();
288: label.print(g, x + pt.x, y + pt.y);
289: drawIcon(g, x, y);
290: }
291:
292: /**
293: * Add a mouse listener
294: *
295: *@param listener the new listener
296: */
297: public void addMouseListener(MouseListener listener) {
298: label.addMouseListener(listener);
299: super .addMouseListener(listener);
300: }
301:
302: /**
303: * Remove a mouse listener
304: *
305: *@param listener the new listener
306: */
307: public void removeMouseListener(MouseListener listener) {
308: label.removeMouseListener(listener);
309: super .removeMouseListener(listener);
310: }
311:
312: /**
313: * Add a mouse listener
314: *
315: *@param listener the new listener
316: */
317: public void addMouseMotionListener(MouseMotionListener listener) {
318: label.addMouseMotionListener(listener);
319: super .addMouseMotionListener(listener);
320: }
321:
322: /**
323: * Remove a mouse listener
324: *
325: *@param listener the new listener
326: */
327: public void removeMouseMotionListener(MouseMotionListener listener) {
328: label.removeMouseMotionListener(listener);
329: super .removeMouseMotionListener(listener);
330: }
331:
332: /**
333: * Sets the scaling factor
334: *
335: *@param value scaling factor
336: */
337: public void scale(double value) {
338: super .scale(value);
339: label.scale(value);
340: }
341:
342: /**
343: * Return the default background color
344: *
345: *@return the color
346: */
347: protected Color getDefaultBackground() {
348: return parent.getBackground();
349: }
350:
351: /**
352: * Draws the icon
353: *
354: *@param g the graphics object
355: *@param x Description of Parameter
356: *@param y Description of Parameter
357: */
358: protected void drawIcon(Graphics g, int x, int y) {
359: // Initialize local variables
360: int nY = y + (high - icon.getIconHeight()) / 2;
361: double scale = getScale();
362:
363: // Draw the icons
364: icon.setScale(scale);
365: icon.paintIcon(this , g, x, nY);
366: }
367:
368: /**
369: * Add the protection information for this field or method
370: *
371: *@param modifiers the modifier information
372: *@return Description of the Returned Value
373: */
374: protected static int getProtectionCode(ModifierHolder modifiers) {
375: if (modifiers.isPublic()) {
376: return PUBLIC;
377: } else if (modifiers.isProtected() && modifiers.isPrivate()) {
378: return PROTECTED_PRIVATE;
379: } else if (modifiers.isPrivate()) {
380: return PRIVATE;
381: } else if (modifiers.isProtected()) {
382: return PROTECTED;
383: } else {
384: return DEFAULT;
385: }
386: }
387:
388: /**
389: * Get the font appropriate for the level of protection
390: *
391: *@param title is this a title
392: *@param modifiers the modifiers
393: *@return Description of the Returned Value
394: */
395: protected static Font getProtectionFont(boolean title,
396: ModifierHolder modifiers) {
397: if (staticFont == null) {
398: initFonts();
399: }
400:
401: if (modifiers == null) {
402: return defaultFont;
403: }
404:
405: if (modifiers.isAbstract()) {
406: if (title) {
407: return abstractTitleFont;
408: } else {
409: return abstractFont;
410: }
411: } else if (modifiers.isStatic()) {
412: return staticFont;
413: } else if (title) {
414: return titleFont;
415: } else {
416: return defaultFont;
417: }
418: }
419:
420: /**
421: * Get the color associated with a level of protection
422: *
423: *@param level the level that we need to know
424: *@return the color
425: */
426: protected static Color getProtectionColor(int level) {
427: if (protectionColors == null) {
428: // Initialize
429: protectionColors = new Color[5];
430:
431: protectionColors[0] = Color.green;
432: protectionColors[1] = Color.blue;
433: protectionColors[2] = Color.yellow;
434: protectionColors[3] = Color.orange;
435: protectionColors[4] = Color.red;
436: }
437:
438: return protectionColors[level];
439: }
440:
441: /**
442: * Initialize the fonts
443: */
444: private static void initFonts() {
445: defaultFont = new Font("Serif", Font.PLAIN, 12);
446: staticFont = new Font("Serif", Font.BOLD, 12);
447: abstractFont = new Font("Serif", Font.ITALIC, 12);
448: titleFont = new Font("Serif", Font.PLAIN, 16);
449: abstractTitleFont = new Font("Serif", Font.ITALIC, 16);
450: }
451: }
|