001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.action;
042:
043: import org.netbeans.api.visual.action.AlignWithMoveDecorator;
044: import org.netbeans.api.visual.action.AlignWithWidgetCollector;
045: import org.netbeans.api.visual.action.ResizeProvider;
046: import org.netbeans.api.visual.action.ResizeStrategy;
047: import org.netbeans.api.visual.widget.LayerWidget;
048: import org.netbeans.api.visual.widget.Widget;
049:
050: import java.awt.*;
051:
052: /**
053: * @author David Kaspar
054: */
055: public final class AlignWithResizeStrategyProvider extends
056: AlignWithSupport implements ResizeStrategy, ResizeProvider {
057:
058: private boolean outerBounds;
059:
060: public AlignWithResizeStrategyProvider(
061: AlignWithWidgetCollector collector,
062: LayerWidget interractionLayer,
063: AlignWithMoveDecorator decorator, boolean outerBounds) {
064: super (collector, interractionLayer, decorator);
065: this .outerBounds = outerBounds;
066: }
067:
068: public Rectangle boundsSuggested(Widget widget,
069: Rectangle originalBounds, Rectangle suggestedBounds,
070: ControlPoint controlPoint) {
071: Insets insets = widget.getBorder().getInsets();
072: int minx = insets.left + insets.right;
073: int miny = insets.top + insets.bottom;
074:
075: suggestedBounds = widget.convertLocalToScene(suggestedBounds);
076:
077: Point suggestedLocation, point;
078: int tempx, tempy;
079:
080: switch (controlPoint) {
081: case BOTTOM_CENTER:
082: suggestedLocation = new Point(suggestedBounds.x
083: + suggestedBounds.width / 2, suggestedBounds.y
084: + suggestedBounds.height);
085: if (!outerBounds)
086: suggestedLocation.y -= insets.bottom;
087:
088: point = super .locationSuggested(widget, new Rectangle(
089: suggestedLocation), suggestedLocation, false, true,
090: false, false);
091:
092: if (!outerBounds)
093: point.y += insets.bottom;
094:
095: suggestedBounds.height = Math.max(miny, point.y
096: - suggestedBounds.y);
097: break;
098: case BOTTOM_LEFT:
099: suggestedLocation = new Point(suggestedBounds.x,
100: suggestedBounds.y + suggestedBounds.height);
101: if (!outerBounds) {
102: suggestedLocation.y -= insets.bottom;
103: suggestedLocation.x += insets.left;
104: }
105:
106: point = super .locationSuggested(widget, new Rectangle(
107: suggestedLocation), suggestedLocation, true, true,
108: false, false);
109:
110: if (!outerBounds) {
111: point.y += insets.bottom;
112: point.x -= insets.left;
113: }
114:
115: suggestedBounds.height = Math.max(miny, point.y
116: - suggestedBounds.y);
117:
118: tempx = Math.min(point.x, suggestedBounds.x
119: + suggestedBounds.width - minx);
120: suggestedBounds.width = suggestedBounds.x
121: + suggestedBounds.width - tempx;
122: suggestedBounds.x = tempx;
123: break;
124: case BOTTOM_RIGHT:
125: suggestedLocation = new Point(suggestedBounds.x
126: + suggestedBounds.width, suggestedBounds.y
127: + suggestedBounds.height);
128: if (!outerBounds) {
129: suggestedLocation.y -= insets.bottom;
130: suggestedLocation.x -= insets.right;
131: }
132:
133: point = super .locationSuggested(widget, new Rectangle(
134: suggestedLocation), suggestedLocation, true, true,
135: false, false);
136:
137: if (!outerBounds) {
138: point.y += insets.bottom;
139: point.x += insets.right;
140: }
141:
142: suggestedBounds.height = Math.max(miny, point.y
143: - suggestedBounds.y);
144:
145: suggestedBounds.width = Math.max(minx, point.x
146: - suggestedBounds.x);
147: break;
148: case CENTER_LEFT:
149: suggestedLocation = new Point(suggestedBounds.x,
150: suggestedBounds.y + suggestedBounds.height / 2);
151: if (!outerBounds)
152: suggestedLocation.x += insets.left;
153:
154: point = super .locationSuggested(widget, new Rectangle(
155: suggestedLocation), suggestedLocation, true, false,
156: false, false);
157:
158: if (!outerBounds)
159: point.x -= insets.left;
160:
161: tempx = Math.min(point.x, suggestedBounds.x
162: + suggestedBounds.width - minx);
163: suggestedBounds.width = suggestedBounds.x
164: + suggestedBounds.width - tempx;
165: suggestedBounds.x = tempx;
166: break;
167: case CENTER_RIGHT:
168: suggestedLocation = new Point(suggestedBounds.x
169: + suggestedBounds.width, suggestedBounds.y
170: + suggestedBounds.height / 2);
171: if (!outerBounds)
172: suggestedLocation.x -= insets.right;
173:
174: point = super .locationSuggested(widget, new Rectangle(
175: suggestedLocation), suggestedLocation, true, false,
176: false, false);
177:
178: if (!outerBounds)
179: point.x += insets.right;
180:
181: suggestedBounds.width = Math.max(minx, point.x
182: - suggestedBounds.x);
183: break;
184: case TOP_CENTER:
185: suggestedLocation = new Point(suggestedBounds.x
186: + suggestedBounds.width / 2, suggestedBounds.y);
187: if (!outerBounds)
188: suggestedLocation.y += insets.top;
189:
190: point = super .locationSuggested(widget, new Rectangle(
191: suggestedLocation), suggestedLocation, false, true,
192: false, false);
193:
194: if (!outerBounds)
195: point.y -= insets.top;
196:
197: tempy = Math.min(point.y, suggestedBounds.y
198: + suggestedBounds.height - miny);
199: suggestedBounds.height = suggestedBounds.y
200: + suggestedBounds.height - tempy;
201: suggestedBounds.y = tempy;
202: break;
203: case TOP_LEFT:
204: suggestedLocation = new Point(suggestedBounds.x,
205: suggestedBounds.y);
206: if (!outerBounds) {
207: suggestedLocation.y += insets.top;
208: suggestedLocation.x += insets.left;
209: }
210:
211: point = super .locationSuggested(widget, new Rectangle(
212: suggestedLocation), suggestedLocation, true, true,
213: false, false);
214:
215: if (!outerBounds) {
216: point.y -= insets.top;
217: point.x -= insets.left;
218: }
219:
220: tempy = Math.min(point.y, suggestedBounds.y
221: + suggestedBounds.height - miny);
222: suggestedBounds.height = suggestedBounds.y
223: + suggestedBounds.height - tempy;
224: suggestedBounds.y = tempy;
225:
226: tempx = Math.min(point.x, suggestedBounds.x
227: + suggestedBounds.width - minx);
228: suggestedBounds.width = suggestedBounds.x
229: + suggestedBounds.width - tempx;
230: suggestedBounds.x = tempx;
231: break;
232: case TOP_RIGHT:
233: suggestedLocation = new Point(suggestedBounds.x
234: + suggestedBounds.width, suggestedBounds.y);
235: if (!outerBounds) {
236: suggestedLocation.y += insets.top;
237: suggestedLocation.x -= insets.right;
238: }
239:
240: point = super .locationSuggested(widget, new Rectangle(
241: suggestedLocation), suggestedLocation, true, true,
242: false, false);
243:
244: if (!outerBounds) {
245: point.y -= insets.top;
246: point.x += insets.right;
247: }
248:
249: tempy = Math.min(point.y, suggestedBounds.y
250: + suggestedBounds.height - miny);
251: suggestedBounds.height = suggestedBounds.y
252: + suggestedBounds.height - tempy;
253: suggestedBounds.y = tempy;
254:
255: suggestedBounds.width = Math.max(minx, point.x
256: - suggestedBounds.x);
257: break;
258: }
259: return widget.convertSceneToLocal(suggestedBounds);
260: }
261:
262: public void resizingStarted(Widget widget) {
263: show();
264: }
265:
266: public void resizingFinished(Widget widget) {
267: hide();
268: }
269:
270: }
|