0001: /*
0002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
0003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0004: *
0005: * This program is free software; you can redistribute it and/or
0006: * modify it under the terms of the GNU General Public License version
0007: * 2 only, as published by the Free Software Foundation.
0008: *
0009: * This program is distributed in the hope that it will be useful, but
0010: * WITHOUT ANY WARRANTY; without even the implied warranty of
0011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012: * General Public License version 2 for more details (a copy is
0013: * included at /legal/license.txt).
0014: *
0015: * You should have received a copy of the GNU General Public License
0016: * version 2 along with this work; if not, write to the Free Software
0017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0018: * 02110-1301 USA
0019: *
0020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0021: * Clara, CA 95054 or visit www.sun.com if you need additional
0022: * information or have any questions.
0023: */
0024:
0025: package com.sun.jumpimpl.presentation.simplebasis;
0026:
0027: import com.sun.jump.command.JUMPIsolateLifecycleRequest;
0028: import com.sun.jump.command.JUMPIsolateWindowRequest;
0029: import com.sun.jump.common.JUMPWindow;
0030: import com.sun.jump.executive.JUMPExecutive;
0031: import com.sun.jump.executive.JUMPIsolateFactory;
0032: import com.sun.jump.message.JUMPMessageDispatcher;
0033: import com.sun.jump.message.JUMPMessageDispatcherTypeException;
0034: import com.sun.jump.module.lifecycle.JUMPApplicationLifecycleModule;
0035: import com.sun.jump.module.lifecycle.JUMPApplicationLifecycleModuleFactory;
0036: import com.sun.jump.module.presentation.JUMPPresentationModule;
0037: import com.sun.jump.common.JUMPApplication;
0038: import com.sun.jump.common.JUMPContent;
0039: import com.sun.jump.executive.JUMPApplicationProxy;
0040: import com.sun.jump.executive.JUMPIsolateProxy;
0041: import com.sun.jump.message.JUMPMessage;
0042: import com.sun.jump.message.JUMPMessageHandler;
0043: import com.sun.jump.module.installer.JUMPInstallerModule;
0044: import com.sun.jump.module.installer.JUMPInstallerModuleFactory;
0045: import com.sun.jump.module.windowing.JUMPWindowingModule;
0046: import com.sun.jump.module.windowing.JUMPWindowingModuleFactory;
0047: import java.awt.BorderLayout;
0048: import java.awt.Color;
0049: import java.awt.Container;
0050: import java.awt.Dimension;
0051: import java.awt.Font;
0052: import java.awt.Frame;
0053: import java.awt.Graphics;
0054: import java.awt.GridLayout;
0055: import java.awt.Image;
0056: import java.awt.Toolkit;
0057: import java.awt.event.ActionEvent;
0058: import java.awt.event.ActionListener;
0059: import java.awt.event.FocusEvent;
0060: import java.awt.event.FocusListener;
0061: import java.awt.event.KeyEvent;
0062: import java.awt.event.KeyListener;
0063: import java.io.IOException;
0064: import java.net.URL;
0065: import java.util.Map;
0066: import java.util.Vector;
0067:
0068: /**
0069: * A simple JUMP launcher that uses Personal Basis components.
0070: */
0071: public class SimpleBasisAMS implements JUMPPresentationModule,
0072: JUMPMessageHandler {
0073:
0074: private Frame frame = null;
0075: private CommandContainer commandContainer = null;
0076: private ScreenContainer screenContainer = null;
0077: private JUMPWindowingModuleFactory wmf = null;
0078: private JUMPWindowingModule wm = null;
0079: private JUMPApplicationLifecycleModuleFactory almf = null;
0080: private JUMPApplicationLifecycleModule alm = null;
0081: private JUMPIsolateFactory lcm = null;
0082: private JUMPApplicationProxy currentApp = null;
0083: private Object timeoutObject = null;
0084: private boolean appWindowDisplayState = false;
0085: private static final int TIMEOUT_VAL = 2000;
0086: protected static final int MAX_TITLE_CHARS = 15;
0087: private SimpleBasisAMSImageButton applicationsScreenButtons[] = null;
0088: private SimpleBasisAMSImageButton switchScreenButtons[] = null;
0089: private SimpleBasisAMSImageButton killScreenButtons[] = null;
0090:
0091: static final int SCREEN_ROWS = 3;
0092: static final int SCREEN_COLUMNS = 3;
0093: static final int SCREEN_DISPLAY_ICONS = SCREEN_ROWS
0094: * SCREEN_COLUMNS;
0095:
0096: private int CURRENT_SCREEN = 0;
0097: private static final int APPLICATIONS_SCREEN = 1;
0098: private static final int SWITCHTO_SCREEN = 2;
0099: private static final int KILL_SCREEN = 3;
0100: private static final int HELP_SCREEN = 4;
0101: private static final int PREFS_SCREEN = 5;
0102:
0103: private static final Color APPLICATIONS_SCREEN_COLOR = new Color(
0104: 179, 229, 188);
0105: private static final Color SWITCHTO_SCREEN_COLOR = new Color(87,
0106: 188, 132);
0107: private static final Color KILL_SCREEN_COLOR = new Color(229, 183,
0108: 179);
0109: private static final Color HELP_SCREEN_COLOR = new Color(225, 227,
0110: 187);
0111: private static final Color PREFS_SCREEN_COLOR = new Color(255, 188,
0112: 157);
0113:
0114: private static final Color BUTTON_BLUE_COLOR = new Color(86, 135,
0115: 248);
0116:
0117: int applicationsScreenPageNumber = 0;
0118: int switchToScreenPageNumber = 0;
0119: int killScreenPageNumber = 0;
0120:
0121: SimpleBasisAMSInstall installer = null;
0122: static boolean verbose;
0123: Map map = null;
0124:
0125: JUMPMessageDispatcher md = null;
0126: Object isolateWindowHandler = null;
0127: Object lifecycleHandler = null;
0128:
0129: /**
0130: * Creates a new instance of SimpleBasisAMS
0131: */
0132: public SimpleBasisAMS() {
0133: }
0134:
0135: /**
0136: * load the presentation module
0137: * @param map the configuration data required for loading this module.
0138: */
0139: public void load(Map map) {
0140: this .map = map;
0141:
0142: // check if verbose mode is used
0143: String verboseStr = System
0144: .getProperty("jump.presentation.verbose");
0145: if (verboseStr == null && map != null) {
0146: verboseStr = (String) map.get("jump.presentation.verbose");
0147: }
0148: if (verboseStr != null
0149: && verboseStr.toLowerCase().equals("true")) {
0150: verbose = true;
0151: }
0152: }
0153:
0154: public void stop() {
0155: }
0156:
0157: public void unload() {
0158: }
0159:
0160: class ScreenContainer extends Container implements FocusListener,
0161: KeyListener {
0162:
0163: public ScreenContainer() {
0164: addKeyListener(this );
0165: addFocusListener(this );
0166: }
0167:
0168: public boolean isFocusable() {
0169: return true;
0170: }
0171:
0172: public void focusGained(FocusEvent e) {
0173: transferFocus();
0174: }
0175:
0176: public void focusLost(FocusEvent e) {
0177: }
0178:
0179: public void keyTyped(KeyEvent e) {
0180: }
0181:
0182: public void keyPressed(KeyEvent e) {
0183: }
0184:
0185: public void keyReleased(KeyEvent e) {
0186: }
0187: }
0188:
0189: class CommandContainer extends Container implements FocusListener,
0190: KeyListener {
0191: public CommandContainer() {
0192: addKeyListener(this );
0193: addFocusListener(this );
0194: }
0195:
0196: public Dimension getPreferredSize() {
0197: return new Dimension(480, 50);
0198: }
0199:
0200: public boolean isFocusable() {
0201: return true;
0202: }
0203:
0204: public void focusGained(FocusEvent e) {
0205: transferFocus();
0206: }
0207:
0208: public void focusLost(FocusEvent e) {
0209: }
0210:
0211: public void keyTyped(KeyEvent e) {
0212: }
0213:
0214: public void keyPressed(KeyEvent e) {
0215: ;
0216: }
0217:
0218: public void keyReleased(KeyEvent e) {
0219: }
0220: }
0221:
0222: private boolean setup() {
0223:
0224: // the loading of apps will happen in its own thread
0225: LoadAppsThread loadAppsThread = new LoadAppsThread();
0226: loadAppsThread.start();
0227:
0228: frame = new Frame();
0229: frame.setLayout(new BorderLayout());
0230:
0231: commandContainer = new CommandContainer();
0232:
0233: commandContainer.setLayout(new GridLayout(0, 4));
0234: addCommandButton("Apps", commandContainer,
0235: new ApplicationsScreenActionListener());
0236: addCommandButton("Switch", commandContainer,
0237: new SwitchToScreenActionListener());
0238: addCommandButton("Kill", commandContainer,
0239: new KillScreenActionListener());
0240: addCommandButton("Help", commandContainer,
0241: new HelpScreenActionListener());
0242: addCommandButton("Install", commandContainer,
0243: new InstallScreenActionListener());
0244: addCommandButton("Remove", commandContainer,
0245: new RemoveScreenActionListener());
0246: addCommandButton("Prefs", commandContainer,
0247: new PrefsScreenActionListener());
0248: addCommandButton("Exit", commandContainer,
0249: new ExitActionListener());
0250:
0251: screenContainer = new ScreenContainer();
0252: screenContainer.setLayout(new BorderLayout());
0253:
0254: frame.add(screenContainer, BorderLayout.CENTER);
0255: frame.add(commandContainer, BorderLayout.NORTH);
0256:
0257: wmf = JUMPWindowingModuleFactory.getInstance();
0258: wm = wmf.getModule();
0259:
0260: almf = JUMPApplicationLifecycleModuleFactory.getInstance();
0261: alm = almf
0262: .getModule(JUMPApplicationLifecycleModuleFactory.POLICY_ONE_LIVE_INSTANCE_ONLY);
0263:
0264: JUMPExecutive e = JUMPExecutive.getInstance();
0265: md = e.getMessageDispatcher();
0266:
0267: lcm = e.getIsolateFactory();
0268:
0269: try {
0270: isolateWindowHandler = md.registerHandler(
0271: JUMPIsolateWindowRequest.MESSAGE_TYPE, this );
0272: lifecycleHandler = md.registerHandler(
0273: JUMPIsolateLifecycleRequest.MESSAGE_TYPE, this );
0274: } catch (JUMPMessageDispatcherTypeException ex) {
0275: ex.printStackTrace();
0276: } catch (IOException ex) {
0277: ex.printStackTrace();
0278: }
0279:
0280: try {
0281: loadAppsThread.join();
0282: } catch (InterruptedException ex) {
0283: ex.printStackTrace();
0284: }
0285:
0286: installer = new SimpleBasisAMSInstall(this );
0287: return true;
0288: }
0289:
0290: public void refreshApplicationsScreen() {
0291: // the loading of apps will happen in its own thread
0292: LoadAppsThread loadAppsThread = new LoadAppsThread();
0293: loadAppsThread.start();
0294: try {
0295: loadAppsThread.join();
0296: } catch (InterruptedException ex) {
0297: ex.printStackTrace();
0298: }
0299: }
0300:
0301: class LoadAppsThread extends Thread {
0302: public void run() {
0303: JUMPApplication apps[] = getInstalledApps();
0304: applicationsScreenButtons = new SimpleBasisAMSImageButton[apps.length];
0305: for (int i = 0; i < apps.length; i++) {
0306: trace("Loading: " + apps[i].getTitle());
0307: applicationsScreenButtons[i] = createScreenButton(
0308: apps[i], new LaunchAppActionListener(apps[i]),
0309: APPLICATIONS_SCREEN_COLOR);
0310: }
0311: }
0312: }
0313:
0314: class LaunchThread extends Thread {
0315: JUMPApplication app = null;
0316:
0317: public LaunchThread(JUMPApplication app) {
0318: this .app = app;
0319: }
0320:
0321: public void run() {
0322: timeoutObject = new Object();
0323: launchApp(app);
0324: }
0325: }
0326:
0327: public void handleMessage(JUMPMessage message) {
0328: if (JUMPIsolateWindowRequest.MESSAGE_TYPE.equals(message
0329: .getType())) {
0330: trace("==== MESSAGE RECEIVED: JUMPIsolateWindowRequest.MESSAGE_TYPE");
0331: JUMPIsolateWindowRequest cmd = (JUMPIsolateWindowRequest) JUMPIsolateWindowRequest
0332: .fromMessage(message);
0333:
0334: int isolateID = cmd.getIsolateId();
0335: int windowID = cmd.getWindowId();
0336: JUMPWindow window = wm.idToWindow(isolateID);
0337:
0338: if (JUMPIsolateWindowRequest.ID_NOTIFY_WINDOW_FOREGROUND
0339: .equals(cmd.getCommandId())) {
0340: trace("====== COMMAND RECEIVED: JUMPIsolateWindowRequest.ID_NOTIFY_WINDOW_FOREGROUND");
0341: appWindowDisplayState = true;
0342: synchronized (timeoutObject) {
0343: System.out
0344: .println("****** Calling notify() on timout object. ******");
0345: timeoutObject.notify();
0346: }
0347: } else if (JUMPIsolateWindowRequest.ID_NOTIFY_WINDOW_BACKGROUND
0348: .equals(cmd.getCommandId())) {
0349: trace("====== COMMAND RECEIVED: JUMPIsolateWindowRequest.ID_NOTIFY_WINDOW_BACKGROUND");
0350: }
0351: } else if (JUMPIsolateLifecycleRequest.MESSAGE_TYPE
0352: .equals(message.getType())) {
0353: trace("==== MESSAGE RECEIVED: JUMPIsolateLifecycleRequest.MESSAGE_TYPE");
0354: JUMPIsolateLifecycleRequest cmd = (JUMPIsolateLifecycleRequest) JUMPIsolateLifecycleRequest
0355: .fromMessage(message);
0356:
0357: int isolateID = cmd.getIsolateId();
0358:
0359: if (JUMPIsolateLifecycleRequest.ID_ISOLATE_DESTROYED
0360: .equals(cmd.getCommandId())) {
0361:
0362: trace("====== COMMAND RECEIVED: JUMPIsolateLifecycleRequest.ID_ISOLATE_DESTROYED");
0363:
0364: JUMPIsolateProxy isolateProxy = lcm
0365: .getIsolate(isolateID);
0366: JUMPApplicationProxy apps[] = isolateProxy.getApps();
0367:
0368: // the killApp(app) call below may not be needed and needs
0369: // to undergo testing to see if this is necessary. In fact,
0370: // it may be the case that the 'app' value returned by
0371: // window application is null. If killApp(app) is not needed,
0372: // then it should be replaced with a "currentApp = null".
0373: if (apps != null) {
0374: for (int i = 0; i < apps.length; ++i) {
0375: trace("====== killApp( + "
0376: + apps[i].getApplication() + ")");
0377: killApp(apps[i]);
0378: }
0379: }
0380: }
0381: }
0382: }
0383:
0384: public void displayDialog(String str,
0385: ActionListener okActionListener,
0386: ActionListener cancelActionListener) {
0387: trace("ENTERING DIALOG SCREEN");
0388: if (currentApp != null) {
0389: pauseApp(currentApp);
0390: bringWindowToBack(currentApp);
0391: }
0392: Container dialogContainer = new Container();
0393: dialogContainer.setBackground(Color.white);
0394: dialogContainer.setLayout(new BorderLayout());
0395:
0396: final String displayStr = str;
0397: Container textContainer = new Container() {
0398: public void paint(Graphics g) {
0399: super .paint(g);
0400: g.drawString(displayStr, 10, 20);
0401: }
0402: };
0403: textContainer.setBackground(Color.white);
0404:
0405: Container buttonContainer = new Container();
0406: buttonContainer.setLayout(new GridLayout(1, 2));
0407: addDialogButton("OK", buttonContainer, okActionListener);
0408: addDialogButton("Cancel", buttonContainer, cancelActionListener);
0409:
0410: dialogContainer.add(textContainer, BorderLayout.CENTER);
0411: dialogContainer.add(buttonContainer, BorderLayout.SOUTH);
0412:
0413: showScreen(dialogContainer);
0414: }
0415:
0416: private void pageUp() {
0417: int currentScreen = getCurrentScreen();
0418:
0419: if (currentScreen == APPLICATIONS_SCREEN) {
0420: // Determine if there is possibly more icons to display
0421: // beyond this page
0422: if (applicationsScreenPageNumber > 0) {
0423: applicationsScreenPageNumber--;
0424: doApplicationsScreen();
0425: }
0426:
0427: } else if (currentScreen == SWITCHTO_SCREEN) {
0428: // Determine if there is possibly more icons to display
0429: // beyond this page
0430: if (switchToScreenPageNumber > 0) {
0431: switchToScreenPageNumber--;
0432: doSwitchToScreen();
0433: }
0434: } else if (currentScreen == KILL_SCREEN) {
0435: // Determine if there is possibly more icons to display
0436: // beyond this page
0437: if (killScreenPageNumber > 0) {
0438: killScreenPageNumber--;
0439: doKillScreen();
0440: }
0441: } else {
0442: installer.pageUp();
0443: }
0444: }
0445:
0446: private void pageDown() {
0447: int currentScreen = getCurrentScreen();
0448: if (currentScreen == APPLICATIONS_SCREEN) {
0449: // Find out number of total screen pages
0450: int totalApplicationsScreenPages = getTotalScreenPages(
0451: applicationsScreenButtons.length,
0452: SCREEN_DISPLAY_ICONS);
0453:
0454: // Don't scroll beyond the last page
0455: if (applicationsScreenPageNumber < (totalApplicationsScreenPages - 1)) {
0456: applicationsScreenPageNumber++;
0457: doApplicationsScreen();
0458: }
0459:
0460: } else if (currentScreen == SWITCHTO_SCREEN) {
0461: // Find out number of total screen pages
0462: int totalSwitchToScreenPages = getTotalScreenPages(
0463: switchScreenButtons.length, SCREEN_DISPLAY_ICONS);
0464: // Don't scroll beyond the last page
0465: if (switchToScreenPageNumber < (totalSwitchToScreenPages - 1)) {
0466: switchToScreenPageNumber++;
0467: doSwitchToScreen();
0468: }
0469:
0470: } else if (currentScreen == KILL_SCREEN) {
0471: // Find out number of total screen pages
0472: int totalKillScreenPages = getTotalScreenPages(
0473: killScreenButtons.length, SCREEN_DISPLAY_ICONS);
0474: // Don't scroll beyond the last page
0475: if (killScreenPageNumber < (totalKillScreenPages - 1)) {
0476: killScreenPageNumber++;
0477: doKillScreen();
0478: }
0479:
0480: } else {
0481: installer.pageDown();
0482: }
0483: }
0484:
0485: /*
0486: * Utility method to determine the number of pages to store
0487: * a total number of icons
0488: */
0489: int getTotalScreenPages(int numIcons, int screenIconCapacity) {
0490: int totalScreenPages = 0;
0491: int div = numIcons / screenIconCapacity;
0492: int mod = numIcons % screenIconCapacity;
0493: if (mod == 0) {
0494: totalScreenPages = div;
0495: } else {
0496: totalScreenPages = div + 1;
0497: }
0498: return totalScreenPages;
0499: }
0500:
0501: /**
0502: * Display the screen containing application icons.
0503: */
0504: void doApplicationsScreen() {
0505: trace("ENTERING SCREEN: APPLICATIONS");
0506: if (currentApp != null) {
0507: pauseApp(currentApp);
0508: bringWindowToBack(currentApp);
0509: }
0510: showScreen(createApplicationsScreen());
0511: setCurrentScreen(APPLICATIONS_SCREEN);
0512: }
0513:
0514: private Container createApplicationsScreen() {
0515: Container applicationsScreen = new Container();
0516: applicationsScreen.setLayout(new DisplayLayout());
0517: int firstPositionIndex = applicationsScreenPageNumber
0518: * SCREEN_DISPLAY_ICONS;
0519: for (int i = firstPositionIndex; i < (applicationsScreenPageNumber
0520: * SCREEN_DISPLAY_ICONS + SCREEN_DISPLAY_ICONS); i++) {
0521: if (i < applicationsScreenButtons.length) {
0522: applicationsScreen.add(applicationsScreenButtons[i]);
0523: } else {
0524: applicationsScreen.add(createScreenButton(null,
0525: APPLICATIONS_SCREEN_COLOR));
0526: }
0527: }
0528: return applicationsScreen;
0529: }
0530:
0531: class ScreenButtonsKeyListener implements KeyListener {
0532: SimpleBasisAMSImageButton button = null;
0533:
0534: public ScreenButtonsKeyListener(SimpleBasisAMSImageButton button) {
0535: this .button = button;
0536: }
0537:
0538: public void keyTyped(KeyEvent e) {
0539: }
0540:
0541: public void keyPressed(KeyEvent e) {
0542: int keyCode = e.getKeyCode();
0543: if (keyCode == KeyEvent.VK_PAGE_UP
0544: || keyCode == KeyEvent.VK_F1) {
0545: pageUp();
0546: } else if (keyCode == KeyEvent.VK_PAGE_DOWN
0547: || keyCode == KeyEvent.VK_F2) {
0548: pageDown();
0549: } else if (keyCode == KeyEvent.VK_ENTER) {
0550: button.doAction();
0551: } else if (keyCode == KeyEvent.VK_UP
0552: || keyCode == KeyEvent.VK_LEFT) {
0553: if (e.getComponent() == screenContainer.getComponent(0)) {
0554: commandContainer.requestFocusInWindow();
0555: } else {
0556: button.transferFocusBackward();
0557: }
0558: } else if (keyCode == KeyEvent.VK_DOWN
0559: | keyCode == KeyEvent.VK_RIGHT) {
0560: button.transferFocus();
0561: }
0562: }
0563:
0564: public void keyReleased(KeyEvent e) {
0565: }
0566: }
0567:
0568: class CommandButtonsKeyListener implements KeyListener {
0569: SimpleBasisAMSImageButton button = null;
0570:
0571: public CommandButtonsKeyListener(
0572: SimpleBasisAMSImageButton button) {
0573: this .button = button;
0574: }
0575:
0576: public void keyTyped(KeyEvent e) {
0577: }
0578:
0579: public void keyPressed(KeyEvent e) {
0580: int keyCode = e.getKeyCode();
0581: if (keyCode == KeyEvent.VK_PAGE_UP
0582: || keyCode == KeyEvent.VK_F1) {
0583: pageUp();
0584: } else if (keyCode == KeyEvent.VK_PAGE_DOWN
0585: || keyCode == KeyEvent.VK_F2) {
0586: pageDown();
0587: } else if (keyCode == KeyEvent.VK_ENTER) {
0588: button.doAction();
0589: } else if (keyCode == KeyEvent.VK_DOWN
0590: || keyCode == KeyEvent.VK_UP) {
0591: screenContainer.requestFocusInWindow();
0592: } else if (keyCode == KeyEvent.VK_LEFT) {
0593: button.transferFocusBackward();
0594: } else if (keyCode == KeyEvent.VK_RIGHT) {
0595: button.transferFocus();
0596: }
0597: }
0598:
0599: public void keyReleased(KeyEvent e) {
0600: }
0601: }
0602:
0603: /**
0604: * Display the switch-to screen, consisting of icons
0605: * pertaining to currently running applications.
0606: */
0607: private void doSwitchToScreen() {
0608: trace("ENTERING SCREEN: SWITCH");
0609: if (currentApp == null) {
0610: trace("*** currentApp is NULL. ***");
0611: } else {
0612: trace("*** currentApp -> "
0613: + currentApp.getApplication().getTitle() + " ***");
0614: }
0615: if (currentApp != null) {
0616: pauseApp(currentApp);
0617: bringWindowToBack(currentApp);
0618: }
0619: showScreen(createSwitchToScreen());
0620: setCurrentScreen(SWITCHTO_SCREEN);
0621: }
0622:
0623: private Container createSwitchToScreen() {
0624: Container switchToScreen = new Container();
0625: switchToScreen.setLayout(new DisplayLayout());
0626: JUMPApplicationProxy apps[] = getRunningApps();
0627: switchScreenButtons = new SimpleBasisAMSImageButton[apps.length];
0628: for (int i = 0; i < apps.length; i++) {
0629: switchScreenButtons[i] = createScreenButton(apps[i]
0630: .getApplication(), new SwitchToActionListener(
0631: apps[i]), SWITCHTO_SCREEN_COLOR);
0632: }
0633: int firstPositionIndex = switchToScreenPageNumber
0634: * SCREEN_DISPLAY_ICONS;
0635: for (int i = firstPositionIndex; i < (switchToScreenPageNumber
0636: * SCREEN_DISPLAY_ICONS + SCREEN_DISPLAY_ICONS); i++) {
0637: if (i < switchScreenButtons.length) {
0638: switchToScreen.add(switchScreenButtons[i]);
0639: } else {
0640: switchToScreen.add(createScreenButton(null,
0641: SWITCHTO_SCREEN_COLOR));
0642: }
0643: }
0644: return switchToScreen;
0645: }
0646:
0647: /**
0648: * Display the kill screen, consisting of icons pertaining to currently
0649: * running applications that can be killed .
0650: */
0651: private void doKillScreen() {
0652: trace("ENTERING SCREEN: KILL");
0653: if (currentApp != null) {
0654: pauseApp(currentApp);
0655: bringWindowToBack(currentApp);
0656: }
0657: showScreen(createKillScreen());
0658: setCurrentScreen(KILL_SCREEN);
0659: }
0660:
0661: private Container createKillScreen() {
0662: Container killScreen = new Container();
0663: killScreen.setLayout(new DisplayLayout());
0664: JUMPApplicationProxy apps[] = getRunningApps();
0665: killScreenButtons = new SimpleBasisAMSImageButton[apps.length];
0666: for (int i = 0; i < apps.length; i++) {
0667: killScreenButtons[i] = createScreenButton(apps[i]
0668: .getApplication(), new KillActionListener(apps[i]),
0669: KILL_SCREEN_COLOR);
0670: }
0671: int firstPositionIndex = killScreenPageNumber
0672: * SCREEN_DISPLAY_ICONS;
0673: for (int i = firstPositionIndex; i < (killScreenPageNumber
0674: * SCREEN_DISPLAY_ICONS + SCREEN_DISPLAY_ICONS); i++) {
0675: if (i < killScreenButtons.length) {
0676: killScreen.add(killScreenButtons[i]);
0677: } else {
0678: killScreen.add(createScreenButton(null,
0679: KILL_SCREEN_COLOR));
0680: }
0681: }
0682: return killScreen;
0683: }
0684:
0685: private void doRemoveScreen() {
0686: trace("ENTERING SCREEN: INSTALLER");
0687: if (currentApp != null) {
0688: pauseApp(currentApp);
0689: bringWindowToBack(currentApp);
0690: }
0691: installer.showRemoveScreen();
0692: }
0693:
0694: private void doInstallScreen() {
0695: trace("ENTERING SCREEN: INSTALLER");
0696: if (currentApp != null) {
0697: pauseApp(currentApp);
0698: bringWindowToBack(currentApp);
0699: }
0700: installer.showInstallScreen();
0701: }
0702:
0703: private void doHelpScreen() {
0704: trace("ENTERING SCREEN: KILL");
0705: if (currentApp != null) {
0706: pauseApp(currentApp);
0707: bringWindowToBack(currentApp);
0708: }
0709: Container helpContainer = new Container() {
0710: public void paint(Graphics g) {
0711: super .paint(g);
0712: g.drawString(" NOTE: When there is no mouse support,",
0713: 10, 20);
0714: g.drawString(
0715: " the TAB key must be pressed to initially",
0716: 10, 40);
0717: g.drawString(" bring focus to this window.", 10, 60);
0718: g.drawString(" F1 : PAGE UP", 10, 100);
0719: g.drawString(" F2 : PAGE DOWN", 10, 120);
0720: }
0721: };
0722: helpContainer.setBackground(HELP_SCREEN_COLOR);
0723: showScreen(helpContainer);
0724: setCurrentScreen(HELP_SCREEN);
0725: }
0726:
0727: private void doPrefsScreen() {
0728: trace("ENTERING SCREEN: PREFS");
0729: if (currentApp != null) {
0730: pauseApp(currentApp);
0731: bringWindowToBack(currentApp);
0732: }
0733: Container prefsContainer = new Container() {
0734: public void paint(Graphics g) {
0735: super .paint(g);
0736: g
0737: .drawString(" NOTE: This screen is currently,",
0738: 10, 20);
0739: g.drawString(" unimplemented.", 10, 40);
0740: }
0741: };
0742: prefsContainer.setBackground(PREFS_SCREEN_COLOR);
0743: showScreen(prefsContainer);
0744: setCurrentScreen(PREFS_SCREEN);
0745: }
0746:
0747: class InstallScreenActionListener implements ActionListener {
0748: public void actionPerformed(ActionEvent e) {
0749: doInstallScreen();
0750: }
0751: }
0752:
0753: class RemoveScreenActionListener implements ActionListener {
0754: public void actionPerformed(ActionEvent e) {
0755: doRemoveScreen();
0756: }
0757: }
0758:
0759: class ApplicationsScreenActionListener implements ActionListener {
0760:
0761: public ApplicationsScreenActionListener() {
0762: }
0763:
0764: public void actionPerformed(ActionEvent e) {
0765: doApplicationsScreen();
0766: }
0767: }
0768:
0769: class SwitchToScreenActionListener implements ActionListener {
0770:
0771: public SwitchToScreenActionListener() {
0772: }
0773:
0774: public void actionPerformed(ActionEvent e) {
0775: doSwitchToScreen();
0776: }
0777: }
0778:
0779: class KillScreenActionListener implements ActionListener {
0780: public void actionPerformed(ActionEvent e) {
0781: doKillScreen();
0782: }
0783: }
0784:
0785: class HelpScreenActionListener implements ActionListener {
0786: public void actionPerformed(ActionEvent e) {
0787: doHelpScreen();
0788: }
0789: }
0790:
0791: class PrefsScreenActionListener implements ActionListener {
0792: public void actionPerformed(ActionEvent e) {
0793: doPrefsScreen();
0794: }
0795: }
0796:
0797: class LaunchAppActionListener implements ActionListener {
0798: JUMPApplication app;
0799:
0800: public LaunchAppActionListener(JUMPApplication app) {
0801: this .app = app;
0802: }
0803:
0804: public void actionPerformed(ActionEvent e) {
0805: Thread launchThread = new LaunchThread(app);
0806: launchThread.start();
0807: }
0808: }
0809:
0810: class SwitchToActionListener implements ActionListener {
0811: JUMPApplicationProxy app;
0812:
0813: public SwitchToActionListener(JUMPApplicationProxy app) {
0814: this .app = app;
0815: }
0816:
0817: public void actionPerformed(ActionEvent e) {
0818: switchToApp(app);
0819: }
0820: }
0821:
0822: class KillActionListener implements ActionListener {
0823: JUMPApplicationProxy app;
0824:
0825: public KillActionListener(JUMPApplicationProxy app) {
0826: this .app = app;
0827: }
0828:
0829: public void actionPerformed(ActionEvent e) {
0830: killApp(app);
0831: doKillScreen();
0832: }
0833: }
0834:
0835: class ExitActionListener implements ActionListener {
0836:
0837: public void actionPerformed(ActionEvent e) {
0838: killAllApps();
0839: // shut down the server
0840: new com.sun.jumpimpl.os.JUMPOSInterfaceImpl()
0841: .shutdownServer();
0842: try {
0843: md.cancelRegistration(isolateWindowHandler);
0844: md.cancelRegistration(lifecycleHandler);
0845: } catch (Exception ex) {
0846: System.out.println("ERROR: " + ex.getMessage());
0847: ex.printStackTrace();
0848: } finally {
0849: }
0850: System.exit(0);
0851: }
0852: }
0853:
0854: private Image getIconImage(JUMPApplication app) {
0855: URL iconPath = app.getIconPath();
0856: if (iconPath != null) {
0857: return Toolkit.getDefaultToolkit().createImage(iconPath);
0858: } else {
0859: return null;
0860: }
0861: }
0862:
0863: private SimpleBasisAMSImageButton addCommandButton(String label,
0864: Container container, ActionListener action) {
0865:
0866: SimpleBasisAMSImageButton button = new SimpleBasisAMSImageButton();
0867: if (button == null) {
0868: return null;
0869: }
0870: button.addActionListener(action);
0871: button.setEnabled(true);
0872: button.setForeground(BUTTON_BLUE_COLOR);
0873: button.setTextShadow(false);
0874: button.setPaintBorders(true);
0875: button.setLabel(label);
0876: button.setFont(new Font("Monospaced", Font.PLAIN, 12));
0877: CommandButtonsKeyListener keyListener = new CommandButtonsKeyListener(
0878: button);
0879: button.addKeyListener(keyListener);
0880:
0881: container.add(button);
0882: return button;
0883: }
0884:
0885: private SimpleBasisAMSImageButton addDialogButton(String label,
0886: Container container, ActionListener action) {
0887:
0888: SimpleBasisAMSImageButton button = new SimpleBasisAMSImageButton();
0889: if (button == null) {
0890: return null;
0891: }
0892: button.addActionListener(action);
0893: button.setEnabled(true);
0894: button.setForeground(BUTTON_BLUE_COLOR);
0895: button.setTextShadow(true);
0896: button.setPaintBorders(true);
0897: button.setLabel(label);
0898:
0899: container.add(button);
0900: return button;
0901: }
0902:
0903: private void clearCommandScreen() {
0904: commandContainer.removeAll();
0905: }
0906:
0907: private void refreshCommandScreen() {
0908: commandContainer.validate();
0909: commandContainer.repaint();
0910: }
0911:
0912: public void clearScreen() {
0913: screenContainer.removeAll();
0914: }
0915:
0916: public void showScreen(Container container) {
0917: clearScreen();
0918: screenContainer.add(container, BorderLayout.CENTER);
0919: refreshScreen();
0920: }
0921:
0922: public void refreshScreen() {
0923: screenContainer.validate();
0924: screenContainer.repaint();
0925: }
0926:
0927: SimpleBasisAMSImageButton createScreenButton(JUMPApplication app,
0928: ActionListener action, Color color) {
0929: SimpleBasisAMSImageButton button = null;
0930: // When app is null, create an empty button
0931: if (app != null) {
0932: Image image = getIconImage(app);
0933: if (image != null) {
0934: button = new SimpleBasisAMSImageButton(
0935: getIconImage(app));
0936: } else {
0937: button = new SimpleBasisAMSImageButton();
0938: }
0939: if (button == null) {
0940: return null;
0941: }
0942:
0943: button.setLabel(app.getTitle().trim());
0944: button.setTextShadow(true);
0945: button.addActionListener(action);
0946:
0947: } else {
0948: button = new SimpleBasisAMSImageButton();
0949: if (button == null) {
0950: return null;
0951: }
0952: button.setEnabled(true);
0953: button.setForeground(color);
0954: button.setPaintBorders(true);
0955: button.setFocusable(false);
0956: }
0957:
0958: button.setEnabled(true);
0959: button.setForeground(color);
0960: button.setPaintBorders(true);
0961:
0962: ScreenButtonsKeyListener keyListener = new ScreenButtonsKeyListener(
0963: button);
0964: button.addKeyListener(keyListener);
0965:
0966: return button;
0967: }
0968:
0969: SimpleBasisAMSImageButton createScreenButton(String title,
0970: ActionListener action, Color color) {
0971: SimpleBasisAMSImageButton button = null;
0972: // Create an empty button
0973: button = new SimpleBasisAMSImageButton();
0974: if (button == null) {
0975: return null;
0976: }
0977: button.setEnabled(true);
0978: button.setForeground(color);
0979: button.setPaintBorders(true);
0980: button.setLabel(title);
0981: button.setTextShadow(true);
0982: button.addActionListener(action);
0983: ScreenButtonsKeyListener keyListener = new ScreenButtonsKeyListener(
0984: button);
0985: button.addKeyListener(keyListener);
0986:
0987: return button;
0988: }
0989:
0990: SimpleBasisAMSImageButton createScreenButton(ActionListener action,
0991: Color color) {
0992: SimpleBasisAMSImageButton button = null;
0993: // Create an empty button
0994: button = new SimpleBasisAMSImageButton();
0995: if (button == null) {
0996: return null;
0997: }
0998: button.setEnabled(true);
0999: button.setForeground(color);
1000: button.setPaintBorders(true);
1001: button.addActionListener(action);
1002: ScreenButtonsKeyListener keyListener = new ScreenButtonsKeyListener(
1003: button);
1004: button.addKeyListener(keyListener);
1005:
1006: return button;
1007: }
1008:
1009: private JUMPApplicationProxy launchApp(JUMPApplication app) {
1010: // There currently isn't a way to extract application arguments.
1011: // Will use null for now.
1012:
1013: JUMPApplicationProxy appProxy = null;
1014: synchronized (timeoutObject) {
1015: appProxy = alm.launchApplication(app, null);
1016: try {
1017: // Use a timeout to detect whether or not a JUMPWindow is created
1018: // after launching the application. The detection is done in
1019: // handleMessage(). If a JUMPWindow isn't detected during the
1020: // timeout, it is assumed that there is a problem.
1021: timeoutObject.wait(TIMEOUT_VAL);
1022: } catch (InterruptedException ex) {
1023: ex.printStackTrace();
1024: }
1025: }
1026:
1027: if (appWindowDisplayState) {
1028: bringWindowToFront(appProxy);
1029: currentApp = appProxy;
1030: }
1031: refreshCommandScreen();
1032:
1033: return appProxy;
1034: }
1035:
1036: private void killAllApps() {
1037: JUMPApplicationProxy apps[] = getRunningApps();
1038: for (int i = 0; i < apps.length; i++) {
1039: killApp(apps[i]);
1040: }
1041: }
1042:
1043: private void killApp(JUMPApplicationProxy app) {
1044: destroyApp(app);
1045: currentApp = null;
1046: }
1047:
1048: private void bringWindowToFront(JUMPApplicationProxy app) {
1049: if (app == null) {
1050: trace("ERROR: Cannot do a bringWindowToFront... app is null.");
1051: return;
1052: }
1053:
1054: JUMPWindow[] windows = app.getIsolateProxy().getWindows();
1055: if (windows != null) {
1056: for (int i = 0; i < windows.length; i++) {
1057: wm.setForeground(windows[i]);
1058: }
1059: }
1060: }
1061:
1062: private void bringWindowToBack(JUMPApplicationProxy app) {
1063: if (app == null) {
1064: trace("ERROR: Cannot do a bringWindowToBack... app is null.");
1065: return;
1066: }
1067:
1068: JUMPWindow[] windows = app.getIsolateProxy().getWindows();
1069: if (windows != null) {
1070: for (int i = 0; i < windows.length; i++) {
1071: wm.setBackground(windows[i]);
1072: }
1073: }
1074: }
1075:
1076: private void switchToApp(JUMPApplicationProxy app) {
1077: resumeApp(app);
1078: bringWindowToFront(app);
1079: currentApp = app;
1080: }
1081:
1082: private void resumeApp(JUMPApplicationProxy app) {
1083: if (app == null) {
1084: return;
1085: }
1086: trace("*** Trying to resume: "
1087: + app.getApplication().getTitle());
1088: app.resumeApp();
1089: }
1090:
1091: private void destroyApp(JUMPApplicationProxy app) {
1092: if (app == null) {
1093: return;
1094: }
1095: trace("*** Trying to kill: " + app.getApplication().getTitle());
1096: app.destroyApp();
1097: }
1098:
1099: private void pauseApp(JUMPApplicationProxy app) {
1100: if (app == null) {
1101: return;
1102: }
1103: trace("*** Trying to pause: " + app.getApplication().getTitle());
1104: app.pauseApp();
1105: }
1106:
1107: boolean isRunningApp(JUMPApplication app) {
1108: JUMPApplicationProxy proxy[] = getRunningApps();
1109: for (int i = 0; i < proxy.length; i++) {
1110: if (proxy[i].getApplication().equals(app)) {
1111: return true;
1112: }
1113: }
1114: return false;
1115: }
1116:
1117: private JUMPApplicationProxy[] getRunningApps() {
1118: JUMPIsolateProxy[] ips = lcm.getActiveIsolates();
1119: Vector appsVector = new Vector();
1120: for (int i = 0; i < ips.length; i++) {
1121: JUMPIsolateProxy ip = ips[i];
1122: JUMPApplicationProxy appProxy[] = ip.getApps();
1123: if (appProxy != null) {
1124: for (int j = 0; j < appProxy.length; j++) {
1125: appsVector.add(appProxy[j]);
1126: }
1127: }
1128: }
1129:
1130: return (JUMPApplicationProxy[]) appsVector
1131: .toArray(new JUMPApplicationProxy[] {});
1132: }
1133:
1134: private JUMPApplication[] getInstalledApps() {
1135: JUMPInstallerModule installers[] = JUMPInstallerModuleFactory
1136: .getInstance().getAllInstallers();
1137: Vector appsVector = new Vector();
1138: if (installers == null) {
1139: return null;
1140: }
1141: for (int i = 0; i < installers.length; i++) {
1142: JUMPContent[] content = installers[i].getInstalled();
1143: if (content != null) {
1144: for (int j = 0; j < content.length; j++) {
1145: appsVector.add((JUMPApplication) content[j]);
1146: }
1147: }
1148: }
1149:
1150: return (JUMPApplication[]) appsVector
1151: .toArray(new JUMPApplication[] {});
1152: }
1153:
1154: static void trace(String str) {
1155: if (verbose) {
1156: System.out.println(str);
1157: }
1158: }
1159:
1160: class DisplayLayout extends GridLayout {
1161: public DisplayLayout() {
1162: super (SCREEN_ROWS, SCREEN_COLUMNS);
1163: }
1164: }
1165:
1166: /**
1167: * Implementation of the interface's start() method.
1168: */
1169: public void start() {
1170: System.err.println("*** Starting SimpleBasisAMS ***");
1171: if (setup()) {
1172: if (frame != null) {
1173: frame.setVisible(true);
1174: }
1175: doApplicationsScreen();
1176: } else {
1177: System.err
1178: .println("*** Setup of SimpleBasisAMS failed. ***");
1179: }
1180: }
1181:
1182: void setCurrentScreen(int screen) {
1183: CURRENT_SCREEN = screen;
1184: }
1185:
1186: int getCurrentScreen() {
1187: return CURRENT_SCREEN;
1188: }
1189:
1190: }
|