001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: DeveloperUtil.java,v 1.9 2007/01/28 21:25:10 jesper Exp $
023: package net.infonode.docking.util;
024:
025: import net.infonode.docking.*;
026:
027: import javax.swing.*;
028: import java.awt.*;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031:
032: /**
033: * <p>
034: * Utility methods to make certain tasks easier during the development of an application using IDW.
035: * </p>
036: *
037: * <p>
038: * <strong>Note:</strong> These methods might be changed/removed or not be compatible with future versions
039: * of IDW.
040: * </p>
041: *
042: * @author $Author: jesper $
043: * @version $Revision: 1.9 $
044: * @since IDW 1.4.0
045: */
046: public class DeveloperUtil {
047: private static String INDENT_STRING = " ";
048:
049: /**
050: * <p>
051: * Returns a Java code pseudo-like string with information about the current window layout in a docking
052: * window.
053: * </p>
054: *
055: * <p>
056: * If the given window is a root window a complete layout is returned i.e. windows
057: * inside the root window, windows on window bars and floating windows. This is useful when for
058: * example creating a default layout. Just add all the views to the root window, drag them around
059: * to create a nice layout and the call this function to retrieve the layout as a string.
060: * </p>
061: *
062: * <p>
063: * <strong>Note:</strong> The returned string contains pseudo-like Java code. All views in the layout
064: * are called <i>View: "title" - view class</i>.
065: * </p>
066: *
067: * <p>
068: * <strong>Note:</strong> The method might be changed/removed or not be compatible with future versions
069: * of IDW.
070: * </p>
071: *
072: * @param window the docking window to retrieve layout for
073: * @return the layout as a pseudo-like Java code
074: */
075: public static String getWindowLayoutAsString(DockingWindow window) {
076: return getDockingWindowLayout(window, 0);
077: }
078:
079: /**
080: * <p>
081: * Creates a JFrame with a text area that shows the layout of the given window as pseudo-like Java code,
082: * i.e. the layout retrieved by {@link DeveloperUtil#getWindowLayoutAsString(DockingWindow)}. The frame
083: * also has a button that when clicked gets the current layout from the window.
084: * </p>
085: *
086: * <p>
087: * The frame is useful when designing window layouts in an application. Just create a frame and use your
088: * root window as window. Drag around your views, press the "Get Layout" button and you'll se your layout
089: * in the text area.
090: * </p>
091: *
092: * <p>
093: * <strong>Note:</strong> The method might be changed/removed or not be compatible with future versions
094: * of IDW.
095: * </p>
096: *
097: * @param title frame title
098: * @param window the docking window to retrieve layout for
099: * @return the frame
100: */
101: public static JFrame createWindowLayoutFrame(String title,
102: final DockingWindow window) {
103: JFrame frame = new JFrame(title);
104: frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
105:
106: final JTextArea layoutArea = new JTextArea(
107: getWindowLayoutAsString(window));
108: JButton getLayoutButton = new JButton("Get Current Layout");
109: getLayoutButton.addActionListener(new ActionListener() {
110: public void actionPerformed(ActionEvent e) {
111: layoutArea.setText(getWindowLayoutAsString(window));
112: }
113: });
114:
115: Box box = new Box(BoxLayout.X_AXIS);
116: box.add(getLayoutButton);
117:
118: frame.getContentPane().add(new JScrollPane(layoutArea),
119: BorderLayout.CENTER);
120: frame.getContentPane().add(box, BorderLayout.NORTH);
121:
122: frame.pack();
123:
124: return frame;
125: }
126:
127: private static String getDockingWindowLayout(DockingWindow window,
128: int depth) {
129: if (window instanceof RootWindow)
130: return getRootWindowLayout((RootWindow) window, depth);
131:
132: String s = depth > 0 ? "\n" : "";
133:
134: for (int i = 0; i < depth; i++)
135: s += INDENT_STRING;
136:
137: if (window instanceof TabWindow)
138: s += getTabWindowLayout((TabWindow) window, depth + 1);
139: else if (window instanceof SplitWindow)
140: s += getSplitWindowLayout((SplitWindow) window, depth + 1);
141: else
142: s += getViewLayout((View) window, depth + 1);
143:
144: return s;
145: }
146:
147: private static String getRootWindowLayout(RootWindow window,
148: int depth) {
149: String s = "";
150: if (window.getWindow() != null)
151: s += "<rootWindow>.setWindow("
152: + getDockingWindowLayout(window.getWindow(), depth)
153: + ");\n\n";
154:
155: for (int i = 0; i < window.getChildWindowCount(); i++) {
156: DockingWindow w = window.getChildWindow(i);
157: if (w != window.getWindow()) {
158: if (w instanceof WindowBar) {
159: WindowBar bar = (WindowBar) w;
160:
161: if (bar.getChildWindowCount() > 0) {
162: for (int k = 0; k < bar.getChildWindowCount(); k++)
163: s += "<rootWindow>.getWindowBar(Direction."
164: + bar.getDirection().toString()
165: .toUpperCase()
166: + ").addTab("
167: + getDockingWindowLayout(bar
168: .getChildWindow(k), depth)
169: + ");\n";
170: s += "\n";
171: }
172: } else if (w instanceof FloatingWindow) {
173: FloatingWindow fw = (FloatingWindow) w;
174: Point loc = fw.getTopLevelAncestor().getLocation();
175: Dimension size = fw.getRootPane().getSize();
176: s += "<rootWindow>.createFloatingWindow(new Point("
177: + loc.x + ", " + loc.y
178: + "), new Dimension(" + size.width + ", "
179: + size.height + "), ";
180: s += getDockingWindowLayout(fw.getChildWindow(0),
181: depth);
182: s += ");\n\n";
183: }
184: }
185: }
186:
187: return s;
188: }
189:
190: private static String getTabWindowLayout(TabWindow window, int depth) {
191: if (window.getChildWindowCount() == 1
192: && window.getChildWindow(0) instanceof View) {
193: return getViewLayout((View) window.getChildWindow(0), depth);
194: }
195:
196: String s = "new TabWindow(new DockingWindow[]{";
197:
198: for (int i = 0; i < window.getChildWindowCount(); i++) {
199: s += getDockingWindowLayout(window.getChildWindow(i), depth);
200: if (i < window.getChildWindowCount() - 1)
201: s += ", ";
202: }
203:
204: s += "})";
205:
206: return s;
207: }
208:
209: private static String getSplitWindowLayout(SplitWindow window,
210: int depth) {
211: String s = "new SplitWindow(" + window.isHorizontal() + ", "
212: + window.getDividerLocation() + "f, ";
213: s += getDockingWindowLayout(window.getLeftWindow(), depth)
214: + ", ";
215: s += getDockingWindowLayout(window.getRightWindow(), depth);
216: s += ")";
217:
218: return s;
219: }
220:
221: private static String getViewLayout(View view, int depth) {
222: return "View: \"" + view.getTitle() + "\" - " + view.getClass();
223: }
224: }
|