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.api.visual.layout;
042:
043: import org.netbeans.api.visual.widget.Widget;
044: import org.netbeans.api.visual.graph.GraphScene;
045: import org.netbeans.api.visual.graph.GraphPinScene;
046: import org.netbeans.api.visual.graph.layout.GraphLayout;
047: import org.netbeans.modules.visual.layout.*;
048: import org.netbeans.modules.visual.util.GeomUtil;
049:
050: /**
051: * This class is a factory of all built-in layouts.
052: *
053: * @author David Kaspar
054: */
055: public final class LayoutFactory {
056:
057: private static final AbsoluteLayout LAYOUT_ABSOLUTE = new AbsoluteLayout();
058: private static final OverlayLayout LAYOUT_OVERLAY = new OverlayLayout();
059:
060: /**
061: * Alignment of children widgets within a calculated widget used by FlowLayout (vertical and horizontal flow layout).
062: */
063: public static enum SerialAlignment {
064:
065: LEFT_TOP, CENTER, RIGHT_BOTTOM, JUSTIFY
066: }
067:
068: /**
069: * Alignment of children widgets within a calculated connection widgets used by default layout used in a connection widget.
070: */
071: public enum ConnectionWidgetLayoutAlignment {
072:
073: NONE, CENTER, TOP_CENTER, BOTTOM_CENTER, CENTER_LEFT, CENTER_RIGHT, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
074: }
075:
076: private LayoutFactory() {
077: }
078:
079: /**
080: * Creates an absolute layout where widgets are located at placed defined by their preferredLocation.
081: * The instance can be shared by multiple widgets.
082: * @return the absolute layout
083: */
084: public static Layout createAbsoluteLayout() {
085: return LAYOUT_ABSOLUTE;
086: }
087:
088: /**
089: * Creates a vertical flow layout with default style where widgets are placed vertically one to the bottom from another.
090: * The instance can be shared by multiple widgets.
091: * If child widget constraint is an Number value,
092: * then its integer value is takes as a weight in which the remaining height of the parent widget is split.
093: * @return the vertical flow layout
094: * @deprecated use createVerticalFlowLayout method instead
095: */
096: public static Layout createVerticalLayout() {
097: GeomUtil.LOG
098: .warning("LayoutFactory.createVerticalLayout() method is deprecated. Use LayoutFactory.createVerticalFlowLayout() method instead."); // NOI18N
099: return createVerticalFlowLayout(null, 0);
100: }
101:
102: /**
103: * Creates a vertical flow layout with a specific style where widgets are placed vertically one to the bottom from another.
104: * The instance can be shared by multiple widgets.
105: * If child widget constraint is an Number value,
106: * then its integer value is takes as a weight in which the remaining height of the parent widget is split.
107: * @param alignment the alignment
108: * @param gap the gap between widgets
109: * @return the vertical flow layout
110: * @deprecated use createVerticalFlowLayout (alignment, gap) method instead
111: */
112: public static Layout createVerticalLayout(
113: SerialAlignment alignment, int gap) {
114: GeomUtil.LOG
115: .warning("LayoutFactory.createVerticalLayout(alignment,gap) method is deprecated. Use LayoutFactory.createVerticalFlowLayout(alignment,gap) method instead."); // NOI18N
116: return new FlowLayout(true, alignment != null ? alignment
117: : SerialAlignment.JUSTIFY, gap);
118: }
119:
120: /**
121: * Creates a vertical flow layout with default style where widgets are placed vertically one to the bottom from another.
122: * The instance can be shared by multiple widgets.
123: * If child widget constraint is an Number value,
124: * then its integer value is takes as a weight in which the remaining height of the parent widget is split.
125: * @return the vertical flow layout
126: */
127: public static Layout createVerticalFlowLayout() {
128: return createVerticalFlowLayout(null, 0);
129: }
130:
131: /**
132: * Creates a vertical flow layout with a specific style where widgets are placed vertically one to the bottom from another.
133: * The instance can be shared by multiple widgets.
134: * If child widget constraint is an Number value,
135: * then its integer value is takes as a weight in which the remaining height of the parent widget is split.
136: * @param alignment the alignment
137: * @param gap the gap between widgets
138: * @return the vertical flow layout
139: */
140: public static Layout createVerticalFlowLayout(
141: SerialAlignment alignment, int gap) {
142: return new FlowLayout(true, alignment != null ? alignment
143: : SerialAlignment.JUSTIFY, gap);
144: }
145:
146: /**
147: * Creates a horizontal flow layout with default style where widgets are placed horizontally one to the right from another.
148: * The instance can be shared by multiple widgets.
149: * If child widget constraint is an Number value,
150: * then its integer value is takes as a weight in which the remaining width of the parent widget is split.
151: * @return the horizontal flow layout
152: * @deprecated use createHorizontalFlowLayout method instead
153: */
154: public static Layout createHorizontalLayout() {
155: GeomUtil.LOG
156: .warning("LayoutFactory.createHorizontalLayout() method is deprecated. Use LayoutFactory.createHorizontalFlowLayout() method instead."); // NOI18N
157: return createHorizontalFlowLayout(null, 0);
158: }
159:
160: /**
161: * Creates a horizontal flow layout with a specific style where widgets are placed horizontally one to the right from another.
162: * The instance can be shared by multiple widgets.
163: * If child widget constraint is an Number value,
164: * then its integer value is takes as a weight in which the remaining width of the parent widget is split.
165: * @param alignment the alignment
166: * @param gap the gap between widgets
167: * @return the horizontal flow layout
168: * @deprecated use createHorizontalFlowLayout (alignment, gap) method instead
169: */
170: public static Layout createHorizontalLayout(
171: SerialAlignment alignment, int gap) {
172: GeomUtil.LOG
173: .warning("LayoutFactory.createHorizontalLayout(alignment,gap) method is deprecated. Use LayoutFactory.createHorizontalFlowLayout(alignment,gap) method instead."); // NOI18N
174: return new FlowLayout(false, alignment != null ? alignment
175: : SerialAlignment.JUSTIFY, gap);
176: }
177:
178: /**
179: * Creates a horizontal flow layout with default style where widgets are placed horizontally one to the right from another.
180: * The instance can be shared by multiple widgets.
181: * If child widget constraint is an Number value,
182: * then its integer value is takes as a weight in which the remaining width of the parent widget is split.
183: * @return the horizontal flow layout
184: */
185: public static Layout createHorizontalFlowLayout() {
186: return createHorizontalFlowLayout(null, 0);
187: }
188:
189: /**
190: * Creates a horizontal flow layout with a specific style where widgets are placed horizontally one to the right from another.
191: * The instance can be shared by multiple widgets.
192: * If child widget constraint is an Number value,
193: * then its integer value is takes as a weight in which the remaining width of the parent widget is split.
194: * @param alignment the alignment
195: * @param gap the gap between widgets
196: * @return the horizontal flow layout
197: */
198: public static Layout createHorizontalFlowLayout(
199: SerialAlignment alignment, int gap) {
200: return new FlowLayout(false, alignment != null ? alignment
201: : SerialAlignment.JUSTIFY, gap);
202: }
203:
204: /**
205: * Creates a card layout where all children widgets except the active one are hidden. The active one is the only shown.
206: * The active widget could be managed using LayoutFactory.getActiveCard and LayoutFactory.setActiveCard methods.
207: * The instance cannot be shared.
208: * @param cardLayoutWidget the widget where the card layout is going to be used
209: * @return the card layout
210: */
211: public static Layout createCardLayout(Widget cardLayoutWidget) {
212: assert cardLayoutWidget != null;
213: return new CardLayout(cardLayoutWidget);
214: }
215:
216: /**
217: * Returns active card of a specified widget where a card layout is used.
218: * @param cardLayoutWidget the widget with card layout
219: * @return the active widget
220: */
221: public static Widget getActiveCard(Widget cardLayoutWidget) {
222: Layout layout = cardLayoutWidget.getLayout();
223: return layout instanceof CardLayout ? ((CardLayout) layout)
224: .getActiveChildWidget() : null;
225: }
226:
227: /**
228: * Sets active card of a specified widget where a card layout is used.
229: * @param widget the widget with card layout
230: * @param activeChildWidget the new active widget
231: */
232: public static void setActiveCard(Widget widget,
233: Widget activeChildWidget) {
234: Layout layout = widget.getLayout();
235: if (layout instanceof CardLayout)
236: ((CardLayout) layout)
237: .setActiveChildWidget(activeChildWidget);
238: }
239:
240: /**
241: * Returns a fill layout where all children widgets has the boundary at the biggest one of them or
242: * they are expanded to the parent widget boundaries during justification.
243: * The instance can be shared by multiple widgets.
244: * @return the fill layout
245: * @deprecated use createOverlayLayout method instead
246: */
247: public static Layout createFillLayout() {
248: GeomUtil.LOG
249: .warning("LayoutFactory.createFillLayout() method is deprecated. Use LayoutFactory.createOverlayLayout() method instead."); // NOI18N
250: return createOverlayLayout();
251: }
252:
253: /**
254: * Returns a overlay layout where all children widgets has the boundary at the biggest one of them or
255: * they are expanded to the parent widget boundaries during justification.
256: * The instance can be shared by multiple widgets.
257: * @return the overlay layout
258: */
259: public static Layout createOverlayLayout() {
260: return LAYOUT_OVERLAY;
261: }
262:
263: /**
264: * Returns a scene layout which performs one-time layout using specified devolve-layout.
265: * The instance cannot be shared.
266: * @param widget the
267: * @param devolveLayout the layout that is going to be used for one-time layout
268: * @param animate if true, then setting preferredLocation is gone animated
269: * @return the scene layout
270: */
271: public static SceneLayout createDevolveWidgetLayout(Widget widget,
272: Layout devolveLayout, boolean animate) {
273: return new DevolveWidgetLayout(widget, devolveLayout, animate);
274: }
275:
276: /**
277: * Creates a scene layout which performs a specified graph-oriented layout on a specified GraphScene.
278: * @param graphScene the graph scene
279: * @param graphLayout the graph layout
280: * @return the scene layout
281: */
282: public static <N, E> SceneLayout createSceneGraphLayout(
283: final GraphScene<N, E> graphScene,
284: final GraphLayout<N, E> graphLayout) {
285: assert graphScene != null && graphLayout != null;
286: return new SceneLayout(graphScene) {
287: protected void performLayout() {
288: graphLayout.layoutGraph(graphScene);
289: }
290: };
291: }
292:
293: /**
294: * Creates a scene layout which performs a specified graph-oriented layout on a specified GraphPinScene.
295: * @param graphPinScene the graph pin scene
296: * @param graphLayout the graph layout
297: * @return the scene layout
298: */
299: public static <N, E> SceneLayout createSceneGraphLayout(
300: final GraphPinScene<N, E, ?> graphPinScene,
301: final GraphLayout<N, E> graphLayout) {
302: assert graphPinScene != null && graphLayout != null;
303: return new SceneLayout(graphPinScene) {
304: protected void performLayout() {
305: graphLayout.layoutGraph(graphPinScene);
306: }
307: };
308: }
309:
310: }
|