001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.toolkit;
014:
015: import java.awt.Component;
016: import java.awt.Container;
017: import java.awt.Dimension;
018: import java.awt.Point;
019: import java.awt.Toolkit;
020: import java.awt.Window;
021: import java.util.ArrayList;
022: import java.util.Stack;
023:
024: import org.apache.avalon.framework.logger.LogEnabled;
025: import org.apache.avalon.framework.logger.Logger;
026: import org.rapla.components.util.Assert;
027: import org.rapla.components.util.Tools;
028:
029: /**All rapla-windows are registered on the FrameControllerList.
030: The FrameControllerList is responsible for positioning the windows
031: and closing all open windows on exit.
032: */
033: final public class FrameControllerList implements LogEnabled {
034: public final static String ROLE = FrameControllerList.class
035: .getName();
036: private Stack openFrameController = new Stack();
037: private Window mainWindow = null;
038: Point center;
039: Logger logger = null;
040: ArrayList listenerList = new ArrayList();
041:
042: public FrameControllerList() {
043: Dimension screenSize = Toolkit.getDefaultToolkit()
044: .getScreenSize();
045: center = new Point(screenSize.width / 2, screenSize.height / 2);
046: }
047:
048: public void enableLogging(Logger logger) {
049: this .logger = logger;
050: }
051:
052: protected Logger getLogger() {
053: return logger;
054: }
055:
056: /** the center will be used by the
057: <code>centerWindow()</code> function. */
058: public void setCenter(Container window) {
059: center.x = window.getLocationOnScreen().x
060: + window.getSize().width / 2;
061: center.y = window.getLocationOnScreen().y
062: + window.getSize().height / 2;
063: }
064:
065: /** the center will be used by the
066: <code>centerWindow(Window)</code> function.
067: @see #centerWindow(Window)
068: */
069: public void setCenter(Point center) {
070: this .center = center;
071: }
072:
073: /** the main-window will be used by the
074: <code>placeRelativeToMain(Window)</code> function.
075: @see #placeRelativeToMain(Window)
076: */
077: public void setMainWindow(Window window) {
078: this .mainWindow = window;
079: }
080:
081: public Window getMainWindow() {
082: return mainWindow;
083: }
084:
085: /** places the window relative to the main-window if set.
086: Otherwise the the <code>centerWindow(Window)</code> method is called.
087: @param newWindow the window to place
088: */
089: public void placeRelativeToMain(Window newWindow) {
090: if (getLogger() != null && getLogger().isDebugEnabled()
091: && mainWindow != null)
092: getLogger().debug(
093: "placeRelativeToMainWindow("
094: + Tools.left(mainWindow.toString(), 60)
095: + ")");
096: if (mainWindow == null)
097: centerWindow(newWindow);
098: else
099: placeRelativeToWindow(newWindow, mainWindow);
100: }
101:
102: /** adds a window to the FrameControllerList */
103: public void add(FrameController c) {
104: Assert.notNull(c);
105: Assert.isTrue(!openFrameController.contains(c),
106: "Duplicated Entries are not allowed");
107: openFrameController.add(c);
108: }
109:
110: /** removes a window from the FrameControllerList */
111: public void remove(FrameController c) {
112: openFrameController.remove(c);
113: String s = c.toString();
114: if (getLogger() != null && getLogger().isDebugEnabled())
115: getLogger().debug(
116: "Frame closed " + Tools.left(s, 60) + "...");
117: fireFrameClosed(c);
118: if (openFrameController.size() == 0)
119: fireListEmpty();
120: }
121:
122: /** closes all windows registered on the FrameControllerList */
123: public void closeAll() {
124: while (!openFrameController.empty()) {
125: FrameController c = (FrameController) openFrameController
126: .peek();
127: int size = openFrameController.size();
128: c.close();
129: if (size <= openFrameController.size())
130: getLogger().error(
131: "removeFrameController() not called in close() in "
132: + c);
133: }
134: }
135:
136: public void addFrameControllerListener(
137: FrameControllerListener listener) {
138: listenerList.add(listener);
139: }
140:
141: public void removeFrameControllerListener(
142: FrameControllerListener listener) {
143: listenerList.remove(listener);
144: }
145:
146: public FrameControllerListener[] getFrameControllerListeners() {
147: synchronized (listenerList) {
148: return (FrameControllerListener[]) listenerList
149: .toArray(new FrameControllerListener[] {});
150: }
151: }
152:
153: protected void fireFrameClosed(FrameController controller) {
154: if (listenerList.size() == 0)
155: return;
156: FrameControllerListener[] listeners = getFrameControllerListeners();
157: for (int i = 0; i < listeners.length; i++) {
158: listeners[i].frameClosed(controller);
159: }
160: }
161:
162: protected void fireListEmpty() {
163: if (listenerList.size() == 0)
164: return;
165: FrameControllerListener[] listeners = getFrameControllerListeners();
166: for (int i = 0; i < listeners.length; i++) {
167: listeners[i].listEmpty();
168: }
169: }
170:
171: /** centers the window around the specified center */
172: public void centerWindow(Window window) {
173: Dimension preferredSize = window.getSize();
174: int x = center.x - (preferredSize.width / 2);
175: int y = center.y - (preferredSize.height / 2);
176: fitIntoScreen(x, y, window);
177: }
178:
179: /** centers the window around the specified center */
180: static public void centerWindowOnScreen(Window window) {
181: Dimension screenSize = Toolkit.getDefaultToolkit()
182: .getScreenSize();
183: Dimension preferredSize = window.getSize();
184: int x = screenSize.width / 2 - (preferredSize.width / 2);
185: int y = screenSize.height / 2 - (preferredSize.height / 2);
186: fitIntoScreen(x, y, window);
187: }
188:
189: /** Tries to place the window, that it fits into the screen. */
190: static public void fitIntoScreen(int x, int y, Component window) {
191: Dimension screenSize = Toolkit.getDefaultToolkit()
192: .getScreenSize();
193: Dimension windowSize = window.getSize();
194: if (x + windowSize.width > screenSize.width)
195: x = screenSize.width - windowSize.width;
196:
197: if (y + windowSize.height > screenSize.height)
198: y = screenSize.height - windowSize.height;
199:
200: if (x < 0)
201: x = 0;
202: if (y < 0)
203: y = 0;
204: window.setLocation(x, y);
205: }
206:
207: /** places the window relative to the owner-window.
208: The newWindow will be placed in the middle of the owner-window.
209: @param newWindow the window to place
210: @param owner the window to place into
211: */
212: public static void placeRelativeToWindow(Window newWindow,
213: Window owner) {
214: placeRelativeToComponent(newWindow, owner, null);
215: }
216:
217: public static void placeRelativeToComponent(Window newWindow,
218: Component component, Point point) {
219: if (component == null)
220: return;
221: Dimension dlgSize = newWindow.getSize();
222: Dimension parentSize = component.getSize();
223: Point loc = component.getLocationOnScreen();
224:
225: if (point != null) {
226: int x = loc.x + point.x - (dlgSize.width) / 2;
227: int y = loc.y + point.y - ((dlgSize.height) * 2) / 3;
228: //System.out.println (loc + ", " + point + " x: " + x + " y: " + y);
229: fitIntoScreen(x, y, newWindow);
230: } else {
231: int x = (parentSize.width - dlgSize.width) / 2 + loc.x;
232: int y = loc.y + 10;
233: fitIntoScreen(x, y, newWindow);
234: }
235: }
236:
237: }
|