001: /*
002: * %W% %E%
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.jumpimpl.module.windowing;
028:
029: import java.io.IOException;
030: import java.util.Map;
031: import java.util.Vector;
032: import java.util.NoSuchElementException;
033:
034: import com.sun.jump.executive.JUMPExecutive;
035: import com.sun.jump.message.JUMPMessage;
036: import com.sun.jump.message.JUMPMessageSender;
037: import com.sun.jump.message.JUMPMessageHandler;
038: import com.sun.jump.message.JUMPMessageDispatcher;
039: import com.sun.jump.message.JUMPMessageDispatcherTypeException;
040: import com.sun.jump.command.JUMPIsolateLifecycleRequest;
041: import com.sun.jump.command.JUMPExecutiveWindowRequest;
042: import com.sun.jump.command.JUMPIsolateWindowRequest;
043: import com.sun.jump.command.JUMPResponse;
044: import com.sun.jump.module.windowing.JUMPWindowingModule;
045: import com.sun.jump.module.JUMPModule;
046: import com.sun.jump.common.JUMPIsolate;
047: import com.sun.jump.common.JUMPWindow;
048:
049: import com.sun.jumpimpl.process.RequestSenderHelper;
050: import com.sun.jumpimpl.process.JUMPProcessProxyImpl;
051:
052: class WindowingModuleImpl implements JUMPWindowingModule,
053: JUMPMessageHandler {
054:
055: private final Object lock = new Object();
056: private Vector windows;
057: private RequestSenderHelper requestSender;
058: private JUMPMessageSender executiveMessageSender;
059:
060: private JUMPMessageSender getMessageSender(JUMPWindow window) {
061: JUMPIsolate isolate = window.getIsolate();
062: if (isolate != null) {
063: return (JUMPMessageSender) isolate;
064: }
065:
066: return executiveMessageSender;
067: }
068:
069: private void setForeground(JUMPWindow window, boolean notifyIsolate) {
070: if (window == null) {
071: throw new IllegalArgumentException();
072: }
073:
074: WindowImpl oldFgWindow = (WindowImpl) getForeground();
075: if (oldFgWindow != window && oldFgWindow != null) {
076: // FIXME: comment away by now. reason: directfb window manager takes care about
077: // windows switching. The bad thing about this comment is that without
078: // explicit setting window background GCI continues to eat VM's
079: // resources trying to handle user input and update screen
080: // setBackground(oldFgWindow, notifyIsolate);
081: }
082:
083: if (notifyIsolate) {
084: JUMPResponse response = requestSender.sendRequest(
085: getMessageSender(window),
086: new JUMPExecutiveWindowRequest(
087: JUMPExecutiveWindowRequest.ID_FOREGROUND,
088: window));
089: if (!requestSender.handleBooleanResponse(response)) {
090: return;
091: }
092: }
093:
094: synchronized (lock) {
095: if (windows.contains(window)) {
096: ((WindowImpl) window)
097: .setState(JUMPWindow.FOREGROUND_STATE);
098: windows.remove(window);
099: windows.add(window);
100: }
101: }
102: }
103:
104: private void setBackground(JUMPWindow window, boolean notifyIsolate) {
105: if (window == null) {
106: throw new IllegalArgumentException();
107: }
108:
109: if (notifyIsolate) {
110: JUMPResponse response = requestSender.sendRequest(
111: getMessageSender(window),
112: new JUMPExecutiveWindowRequest(
113: JUMPExecutiveWindowRequest.ID_BACKGROUND,
114: window));
115: if (!requestSender.handleBooleanResponse(response)) {
116: return;
117: }
118: }
119:
120: synchronized (lock) {
121: if (windows.contains(window) && (windows.size() > 1)) {
122: ((WindowImpl) window)
123: .setState(JUMPWindow.BACKGROUND_STATE);
124: }
125: }
126: }
127:
128: WindowingModuleImpl() {
129: // instantiate stuff that will track JUMPWindow-s
130: // created by presentation if any
131: new WindowingExecutiveClient();
132:
133: JUMPExecutive executive = JUMPExecutive.getInstance();
134: int isolateId = executive.getProcessId();
135:
136: windows = new Vector();
137: requestSender = new RequestSenderHelper(executive);
138: executiveMessageSender = JUMPProcessProxyImpl
139: .createProcessProxyImpl(executive.getProcessId());
140:
141: try {
142: JUMPExecutive e = JUMPExecutive.getInstance();
143: JUMPMessageDispatcher md = e.getMessageDispatcher();
144: md.registerHandler(JUMPIsolateWindowRequest.MESSAGE_TYPE,
145: this );
146: md.registerHandler(
147: JUMPIsolateLifecycleRequest.MESSAGE_TYPE, this );
148: } catch (JUMPMessageDispatcherTypeException dte) {
149: dte.printStackTrace();
150: // FIXME: someone else listeneing -- what to do?
151: } catch (IOException ex) {
152: ex.printStackTrace();
153: // FIXME: can't register, what to do?
154: }
155: }
156:
157: public void handleMessage(JUMPMessage message) {
158: if (JUMPIsolateWindowRequest.MESSAGE_TYPE.equals(message
159: .getType())) {
160: JUMPIsolateWindowRequest cmd = (JUMPIsolateWindowRequest) JUMPIsolateWindowRequest
161: .fromMessage(message);
162:
163: WindowImpl window = WindowImpl.getWindow(
164: cmd.getIsolateId(), cmd.getWindowId());
165:
166: synchronized (lock) {
167: if (!windows.contains(window)) {
168: window.setState(JUMPWindow.BACKGROUND_STATE);
169: windows.add(0, window);
170: }
171: }
172:
173: if (JUMPIsolateWindowRequest.ID_NOTIFY_WINDOW_FOREGROUND
174: .equals(cmd.getCommandId())) {
175:
176: setForeground(window, false);
177: return;
178: }
179:
180: if (JUMPIsolateWindowRequest.ID_NOTIFY_WINDOW_BACKGROUND
181: .equals(cmd.getCommandId())) {
182:
183: setBackground(window, false);
184: return;
185: }
186: return;
187:
188: }
189:
190: if (JUMPIsolateLifecycleRequest.MESSAGE_TYPE.equals(message
191: .getType())) {
192:
193: JUMPIsolateLifecycleRequest cmd = (JUMPIsolateLifecycleRequest) JUMPIsolateLifecycleRequest
194: .fromMessage(message);
195:
196: if (JUMPIsolateLifecycleRequest.ID_ISOLATE_DESTROYED
197: .equals(cmd.getCommandId())) {
198:
199: int isolateId = cmd.getIsolateId();
200:
201: synchronized (lock) {
202: // remove JUMPWindow-s hosted by the destroyed isolate
203: // from the list
204: int idx = 0;
205: while (idx != windows.size()) {
206: JUMPIsolate isolate = ((JUMPWindow) windows
207: .elementAt(idx)).getIsolate();
208: if (isolate != null
209: && isolate.getIsolateId() == isolateId) {
210: windows.remove(idx);
211: continue;
212: }
213: ++idx;
214: }
215:
216: // enshure there is one foreground window
217: if (getForeground() == null) {
218: nextWindow();
219: }
220: }
221: }
222: }
223: }
224:
225: public void load(Map config) {
226: }
227:
228: public void unload() {
229: }
230:
231: public JUMPWindow[] getWindows() {
232: synchronized (lock) {
233: JUMPWindow[] res = new JUMPWindow[windows.size()];
234: for (int i = 0; i != res.length; ++i) {
235: res[i] = (JUMPWindow) windows.elementAt(i);
236: }
237: return res;
238: }
239: }
240:
241: public JUMPWindow getForeground() {
242: try {
243: synchronized (lock) {
244: JUMPWindow res = (JUMPWindow) windows.lastElement();
245: return WindowImpl.FOREGROUND_STATE.equals(res
246: .getState()) ? res : null;
247: }
248: } catch (NoSuchElementException e) {
249: return null;
250: }
251: }
252:
253: public void setForeground(JUMPWindow window) {
254: setForeground(window, true);
255: }
256:
257: public void setBackground(JUMPWindow window) {
258: setBackground(window, true);
259: }
260:
261: public JUMPWindow nextWindow() {
262: synchronized (lock) {
263: JUMPWindow fg = null;
264: for (int i = windows.size() - 2; i >= 0; --i) {
265: fg = (JUMPWindow) windows.elementAt(i);
266: setForeground(fg);
267: if (fg == getForeground()) {
268: break;
269: }
270: }
271: return fg;
272: }
273: }
274:
275: public JUMPWindow idToWindow(long id) {
276: JUMPWindow res = null;
277: synchronized (lock) {
278: for (int i = 0, count = windows.size(); i != count; ++i) {
279: if (((JUMPWindow) windows.elementAt(i)).getId() == id) {
280: break;
281: }
282: }
283: }
284: return res;
285: }
286: }
|