001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ElementProperty.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.old;
009:
010: import java.awt.*;
011: import java.awt.geom.AffineTransform;
012: import java.awt.geom.Area;
013: import java.awt.geom.Rectangle2D;
014:
015: import javax.swing.JMenuItem;
016: import javax.swing.JPopupMenu;
017:
018: import com.uwyn.rife.swing.JDialogError;
019:
020: abstract class ElementProperty {
021: public static final int TITLE = 0;
022: public static final int EXIT = 1;
023: public static final int CONSUMED_PARAMETER = 2;
024: public static final int USED_PARAMETER = 3;
025: public static final int ADDED_PARAMETER = 4;
026:
027: public static final String VALID_CHARS = "abcdefghjiklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789*?_";
028:
029: private Element mElement = null;
030: private String mName = null;
031:
032: private float mScaleFactor = 1f;
033:
034: private Rectangle2D mNameBoundsOrig = null;
035: private Rectangle2D mNameBoundsScaled = null;
036: private Area mHotSpotOrig = null;
037: private Area mHotSpotScaled = null;
038:
039: public ElementProperty(Element element, String name) {
040: setElement(element);
041: setName(name);
042: }
043:
044: public void drawActive(Graphics2D g2d) {
045: Rectangle clip = g2d.getClipBounds();
046: if (clip == null || clip.intersects(getNameBounds())) {
047: Composite previous_composite = g2d.getComposite();
048: g2d.setComposite(AlphaComposite.getInstance(
049: AlphaComposite.SRC_OVER, 0.5f));
050: g2d.setColor(Color.white);
051: g2d.fill(getHotSpot());
052: g2d.setComposite(previous_composite);
053: }
054: }
055:
056: public boolean equals(Object object) {
057: if (object instanceof ElementProperty) {
058: ElementProperty property = (ElementProperty) object;
059: if (property.getElement() == getElement()
060: && property.getClass() == getClass()
061: && property.getName().equals(getName())) {
062: return true;
063: }
064: }
065:
066: return false;
067: }
068:
069: public void setElement(Element element) {
070: mElement = element;
071: }
072:
073: public Element getElement() {
074: return mElement;
075: }
076:
077: public void setName(String name) {
078: mName = name;
079: clearNameBounds();
080: clearHotSpot();
081: }
082:
083: public String getName() {
084: return mName;
085: }
086:
087: public boolean isValidName(String name) {
088: ElementProperty temp_property = getElement().getProperty(
089: getClass(), name);
090: if (temp_property == this || temp_property == null) {
091:
092: return true;
093: } else {
094: return false;
095: }
096: }
097:
098: public void clearNameBounds() {
099: setNameBounds(null);
100: }
101:
102: public void setNameBounds(Rectangle2D nameBounds) {
103: mNameBoundsOrig = nameBounds;
104: mNameBoundsScaled = null;
105: }
106:
107: public Rectangle2D getNameBounds() {
108: if (mNameBoundsOrig == null) {
109: return null;
110: }
111:
112: if (mNameBoundsScaled == null) {
113: scaleNameBounds();
114: }
115: return mNameBoundsScaled;
116: }
117:
118: public void clearHotSpot() {
119: setHotSpot(null);
120: }
121:
122: public void setHotSpot(Area hotSpot) {
123: mHotSpotOrig = hotSpot;
124: mHotSpotScaled = null;
125: }
126:
127: public void setHotSpot(Shape hotSpot) {
128: mHotSpotOrig = new Area(hotSpot);
129: mHotSpotScaled = null;
130: }
131:
132: public Area getHotSpot() {
133: if (mHotSpotOrig == null) {
134: return null;
135: }
136:
137: if (mHotSpotScaled == null) {
138: scaleHotSpot();
139: }
140: return mHotSpotScaled;
141: }
142:
143: private void resetScaledMembers() {
144: mNameBoundsScaled = null;
145: mHotSpotScaled = null;
146: }
147:
148: public synchronized void scale(float multiplier) {
149: mScaleFactor *= multiplier;
150:
151: resetScaledMembers();
152: }
153:
154: private void scaleHotSpot() {
155: AffineTransform scale_transform = AffineTransform
156: .getScaleInstance(mScaleFactor, mScaleFactor);
157: mHotSpotScaled = mHotSpotOrig
158: .createTransformedArea(scale_transform);
159: }
160:
161: private void scaleNameBounds() {
162: mNameBoundsScaled = new Rectangle2D.Float(
163: (float) (mNameBoundsOrig.getX() * mScaleFactor),
164: (float) (mNameBoundsOrig.getY() * mScaleFactor),
165: (float) (mNameBoundsOrig.getWidth() * mScaleFactor),
166: (float) (mNameBoundsOrig.getHeight() * mScaleFactor));
167: }
168:
169: public void showPopupMenu(Point clickedPoint) {
170: JPopupMenu popup = new JPopupMenu();
171: popup.addPopupMenuListener(getElement().getStructurePanel());
172: showPopupMenuReal(popup, clickedPoint);
173: popup.show(getElement(), clickedPoint.x, clickedPoint.y);
174: }
175:
176: protected class Delete implements DynamicMenuAction {
177: public void execute(JMenuItem menuItem) {
178: getElement().removeProperty(ElementProperty.this );
179: getElement().repaint();
180: }
181: }
182:
183: protected class Edit implements DynamicMenuAction {
184: private Point mClickedPoint = null;
185:
186: public Edit(Point clickedPoint) {
187: mClickedPoint = clickedPoint;
188: }
189:
190: public void execute(JMenuItem menuItem) {
191: getElement().getStructurePanel().editElementProperty(
192: ElementProperty.this , mClickedPoint);
193: getElement().repaint();
194: }
195: }
196:
197: public abstract void draw(Graphics2D g2d);
198:
199: public abstract Color getColor();
200:
201: public abstract Font getDrawFont();
202:
203: public abstract int getEditAlignment();
204:
205: public abstract Font getEditFont();
206:
207: public abstract Rectangle getEditRectangle(Point clickedPoint);
208:
209: public abstract JDialogError getUnicityErrorDialog();
210:
211: protected abstract void showPopupMenuReal(JPopupMenu popupMenu,
212: Point clickedPoint);
213: }
|