001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.design.selection;
021:
022: import java.awt.BasicStroke;
023: import java.awt.Color;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Graphics2D;
027: import java.awt.Point;
028: import java.awt.RenderingHints;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.geom.AffineTransform;
032: import java.awt.geom.Area;
033: import java.awt.geom.Rectangle2D;
034: import javax.swing.Icon;
035: import javax.swing.ImageIcon;
036: import javax.swing.JLabel;
037: import javax.swing.Timer;
038: import javax.swing.border.EmptyBorder;
039: import org.netbeans.modules.bpel.design.DesignView;
040: import org.netbeans.modules.bpel.design.geometry.FPoint;
041: import org.netbeans.modules.bpel.design.geometry.FStroke;
042: import org.netbeans.modules.bpel.design.model.elements.ContentElement;
043: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
044:
045: public class GhostSelection implements ActionListener {
046:
047: private DesignView designView;
048:
049: private Area ghostArea;
050: private JLabel label;
051:
052: private float currentX;
053: private float currentY;
054:
055: private boolean enabled;
056:
057: private Timer timer;
058:
059: private int iteration;
060:
061: public GhostSelection(DesignView designView) {
062: this .designView = designView;
063: this .timer = new Timer(RESIZE_TIME / ITERATION_COUNT, this );
064: }
065:
066: public void init(String text, Point pt) {
067:
068: FPoint p = designView.getOverlayView().convertScreenToDiagram(
069: pt);
070:
071: currentX = p.x;
072: currentY = p.y;
073:
074: label = new JLabel(text);
075: label.setIcon(ERROR_BADGE);
076: label.setBorder(new EmptyBorder(2, 2, 2, 2));
077: label.setBackground(new Color(0xFFFFDD));
078: label.setOpaque(true);
079: label.setFont(label.getFont().deriveFont(Font.BOLD));
080: label.setForeground(Color.RED);
081:
082: ghostArea = null;
083: }
084:
085: public void initCentered(Pattern pattern, Point pt) {
086:
087: FPoint p = designView.getOverlayView().convertScreenToDiagram(
088: pt);
089:
090: label = null;
091:
092: currentX = p.x;
093: currentY = p.y;
094: enabled = false;
095: iteration = 0;
096:
097: if (pattern != null) {
098: ghostArea = pattern.createSelection();
099: } else {
100: ghostArea = new Area(ContentElement.TASK_SHAPE);
101: }
102:
103: Rectangle2D ghostBounds = ghostArea.getBounds2D();
104:
105: double size = Math.max(ghostBounds.getWidth(), ghostBounds
106: .getHeight());
107:
108: double k = Math.min(MAXIMUM_GHOST_SIZE / size, 1);
109:
110: double cx = ghostBounds.getCenterX();
111: double cy = ghostBounds.getCenterY();
112:
113: AffineTransform t = AffineTransform.getTranslateInstance(
114: currentX, currentY);
115: t.scale(k, k);
116: t.translate(-cx, -cy);
117:
118: ghostArea.transform(t);
119:
120: getDesignView().repaint();
121: }
122:
123: public void init(Pattern pattern, Point pt) {
124:
125: FPoint p = designView.getOverlayView().convertScreenToDiagram(
126: pt);
127:
128: label = null;
129:
130: currentX = p.x;
131: currentY = p.y;
132: iteration = 0;
133:
134: if (pattern != null) {
135: ghostArea = pattern.createSelection();
136: } else {
137: ghostArea = new Area(ContentElement.TASK_SHAPE);
138: }
139:
140: Rectangle2D ghostBounds = ghostArea.getBounds2D();
141:
142: timer.restart();
143: getDesignView().repaint();
144: }
145:
146: public void setEnabled(boolean enabled) {
147: this .enabled = enabled;
148: }
149:
150: public void move(FPoint p) {
151: move(p.x, p.y);
152: }
153:
154: public void move(float newX, float newY) {
155: if (moveImpl(newX, newY)) {
156: getDesignView().repaint();
157: }
158: }
159:
160: private boolean moveImpl(float newX, float newY) {
161: if (isEmpty())
162: return false;
163:
164: double dx = newX - currentX;
165: double dy = newY - currentY;
166:
167: if (dx == 0 && dy == 0)
168: return false;
169:
170: if (ghostArea != null) {
171: ghostArea.transform(AffineTransform.getTranslateInstance(
172: dx, dy));
173: }
174:
175: currentX = newX;
176: currentY = newY;
177:
178: return true;
179: }
180:
181: public void clear() {
182: if (isEmpty())
183: return;
184:
185: ghostArea = null;
186: label = null;
187:
188: iteration = 0;
189: getDesignView().repaint();
190: }
191:
192: public boolean isEmpty() {
193: return (ghostArea == null) && (label == null);
194: }
195:
196: public DesignView getDesignView() {
197: return designView;
198: }
199:
200: public EntitySelectionModel getSelectionModel() {
201: return getDesignView().getSelectionModel();
202: }
203:
204: public void paint(Graphics2D g2) {
205: // if (isEmpty()) return;
206: //
207: // Point p = getDesignView().getMousePosition();
208: // if (p != null) {
209: // FPoint mp = getDesignView().convertScreenToDiagram(p);
210: // moveImpl(mp.x, mp.y);
211: // }
212: //
213: // if (ghostArea != null) {
214: // g2.setStroke(new FStroke(2, 4, 3).createStroke(g2));
215: // g2.setPaint(enabled ? ENABLED_COLOR : DISABLED_COLOR);
216: //
217: // g2.setRenderingHint(
218: // RenderingHints.KEY_STROKE_CONTROL,
219: // RenderingHints.VALUE_STROKE_NORMALIZE);
220: //
221: // g2.draw(ghostArea);
222: // }
223: //
224: // if (label != null) {
225: // Dimension labelSize = label.getPreferredSize();
226: // label.setBounds(0, 0, labelSize.width, labelSize.height);
227: //
228: // double zoom = designView.getCorrectedZoom();
229: //
230: // double tx = currentX;
231: // double ty = currentY;
232: //
233: // Graphics2D lg2 = (Graphics2D) g2.create();
234: // lg2.setRenderingHint(
235: // RenderingHints.KEY_STROKE_CONTROL,
236: // RenderingHints.VALUE_STROKE_NORMALIZE);
237: // lg2.translate(tx, ty);
238: // lg2.scale(1.0 / zoom, 1.0 / zoom);
239: //
240: // if (lg2.getTransform() != null) {
241: // double ctx = lg2.getTransform().getTranslateX();
242: // double cty = lg2.getTransform().getTranslateY();
243: // lg2.translate(-(ctx - (int) ctx), -(cty - (int) cty));
244: // }
245: //
246: // lg2.translate(-labelSize.width / 2, -labelSize.height);
247: // lg2.setStroke(new BasicStroke(1));
248: //
249: // label.paint(lg2);
250: //
251: // lg2.dispose();
252: // }
253: }
254:
255: public void actionPerformed(ActionEvent e) {
256: if (ghostArea == null) {
257: timer.stop();
258: return;
259: }
260:
261: int steps = Math.max(1, ITERATION_COUNT - iteration++);
262:
263: Rectangle2D ghostBounds = ghostArea.getBounds2D();
264:
265: double size = Math.max(ghostBounds.getWidth(), ghostBounds
266: .getHeight());
267:
268: if (size > MAXIMUM_GHOST_SIZE) {
269: double k = Math.pow(MAXIMUM_GHOST_SIZE / size, 1.0 / steps);
270:
271: AffineTransform t = AffineTransform.getTranslateInstance(
272: currentX, currentY);
273: t.scale(k, k);
274: t.translate(-currentX, -currentY);
275:
276: ghostArea.transform(t);
277: } else {
278: timer.stop();
279: }
280:
281: if (steps == 1) {
282: timer.stop();
283: }
284:
285: getDesignView().repaint();
286: }
287:
288: private static int RESIZE_TIME = 300;
289: private static int ITERATION_COUNT = 10;
290: private static final Color ENABLED_COLOR = new Color(0x5D985C);
291: private static final Color DISABLED_COLOR = new Color(0xCC0000);
292: private static double MAXIMUM_GHOST_SIZE = 160;
293:
294: private static final Icon ERROR_BADGE = new ImageIcon(
295: DesignView.class.getResource("resources/error_badge.png")); // NOI18N
296: }
|