📄 spotworldportal.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.gui;import com.sun.spot.spotworld.SpotWorld;import com.sun.spot.spotworld.common.LocaleUtil;import com.sun.spot.spotworld.gui.DesktopAppMenuItem; import com.sun.spot.spotworld.participants.Application;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.lang.reflect.InvocationHandler;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.InvalidPropertiesFormatException;import java.util.Properties;import java.util.Vector;import javax.swing.JButton;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPopupMenu;import javax.swing.MenuElement;import javax.swing.UIManager;import javax.swing.JSplitPane;import com.sun.spot.communication.util.Debug;import javax.swing.JFileChooser;/** * Top level viewer into spotworld, its job is to hold the actual views. * This version allows for two simultaneaous views to be loaded. These views are loaded as jar files, * and this class knows nothing of the details of the views. (Thoush they are assumed to be Components) * @author arshan, robert, randy */public class SpotWorldPortal extends JFrame implements ActionListener { JSplitPane topPane; SpotWorld myWorld; DesktopAppManager desktopAppManager; Vector<String> voList = null; public static final String SPOTWORLD_HEIGHT = "SPOTWORLD-HEIGHT"; public static final String SPOTWORLD_WIDTH = "SPOTWORLD-WIDTH"; public static final String LEFTPANE_HEIGHT = "LEFTPANE-HEIGHT"; public static final String LEFTPANE_WIDTH = "LEFTPANE-WIDTH"; public static final String LEFTPANE_VIEW = "LEFTPANE-VIEW"; public static final String RIGHTPANE_HEIGHT = "RIGHTPANE-HEIGHT"; public static final String RIGHTPANE_WIDTH = "RIGHTPANE-WIDTH"; public static final String RIGHTPANE_VIEW = "RIGHTPANE-VIEW"; public static final String NO_COMPONENT = "NOTHING"; private String SPOTWORLD_PROPERTY_COMMENT = "SPOT World Properties File"; // this is currently mac/*nix specific --Robert private static final String spotWorldPropsFileName = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "SPOTWorld.properties"; private static Properties SPOTWorldProperties; /** Creates a new instance of SpotWorldPortal */ public SpotWorldPortal() { init(); } public void init(){ // get the world ready for playtime myWorld = new SpotWorld(); desktopAppManager = DesktopAppManager.getInstance(); desktopAppManager.setSPOTWorldPortal(this); Debug.setMessageLevel(75); setTitle("SPOTWorld"); readPropertiesFromFile(); // command-q on a mac bypasses windowClosing() so we need to do some // MacOSX specific stuff to hear windowClosing events. -- Robert if (System.getProperty("os.name").toLowerCase().startsWith("mac os x")) { macQuitListener(); } this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {// saveSPOTWorldProperties(); writePropertiesToFile(); System.exit(0); } }); this.getContentPane().setLayout(new BorderLayout()); topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); topPane.setOneTouchExpandable(true); topPane.setDividerSize(7); Component comp = null; // Set the sizes and load the views. -- Robert if (!getSPOTWorldProperty(LEFTPANE_VIEW).equals(NO_COMPONENT)) { comp = myWorld.getView(getSPOTWorldProperty(LEFTPANE_VIEW)); comp.setName(getSPOTWorldProperty(LEFTPANE_VIEW)); topPane.setLeftComponent(comp); topPane.getLeftComponent().setPreferredSize(new Dimension( new Integer(getSPOTWorldProperty(LEFTPANE_WIDTH)), new Integer(getSPOTWorldProperty(LEFTPANE_HEIGHT)))); } if (!getSPOTWorldProperty(RIGHTPANE_VIEW).equals(NO_COMPONENT)) { comp = myWorld.getView(getSPOTWorldProperty(RIGHTPANE_VIEW)); comp.setName(getSPOTWorldProperty(RIGHTPANE_VIEW)); topPane.setRightComponent(comp); topPane.getRightComponent().setPreferredSize(new Dimension( new Integer(getSPOTWorldProperty(RIGHTPANE_WIDTH)), new Integer(getSPOTWorldProperty(RIGHTPANE_HEIGHT)))); } this.setJMenuBar(createMenuBar()); this.getContentPane().add(topPane); pack(); setVisible(true); testrun(); myWorld.setReadyToAddVirtualObjects(true); } public static String getSPOTWorldProperty(String key){ return SPOTWorldProperties.getProperty(key); } public static void setSPOTWorldProperty(String key, String value){ SPOTWorldProperties.setProperty(key, value); } private void macQuitListener() { try { Class applicationClass = Class.forName("com.apple.eawt.Application"); Object applicationInstance = applicationClass.newInstance(); Class applicationListener = Class.forName("com.apple.eawt.ApplicationListener"); Object proxy = Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[] { applicationListener }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) { if (method.getName().equals("handleQuit")) { exit(); } return null; } }); Method m = applicationClass.getMethod("addApplicationListener", applicationListener); m.invoke(applicationInstance, proxy); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (SecurityException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (NoSuchMethodException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.printStackTrace(); } } /* * User wants to quit, shut down everything. */ public void exit(){ writePropertiesToFile(); System.exit(0); } public void readPropertiesFromFile() { File file = new File(spotWorldPropsFileName); try { if (file.exists()) { FileInputStream is = new FileInputStream(file); SPOTWorldProperties = new Properties(); SPOTWorldProperties.loadFromXML(is); is.close(); } else { FileOutputStream os = new FileOutputStream(file); SPOTWorldProperties = new Properties(); setSPOTWorldProperty(SpotWorldPortal.SPOTWORLD_HEIGHT, ""); setSPOTWorldProperty(SpotWorldPortal.SPOTWORLD_WIDTH, ""); setSPOTWorldProperty(SpotWorldPortal.LEFTPANE_HEIGHT, "640"); setSPOTWorldProperty(SpotWorldPortal.LEFTPANE_WIDTH, "200"); setSPOTWorldProperty(SpotWorldPortal.LEFTPANE_VIEW, "Tree View"); setSPOTWorldProperty(SpotWorldPortal.RIGHTPANE_HEIGHT, "640"); setSPOTWorldProperty(SpotWorldPortal.RIGHTPANE_WIDTH, "800"); setSPOTWorldProperty(SpotWorldPortal.RIGHTPANE_VIEW, "Grid View"); SPOTWorldProperties.storeToXML(os, SPOTWORLD_PROPERTY_COMMENT); os.close(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -