📄 gui.java
字号:
/* * Copyright (c) 2006, Swedish Institute of Computer Science. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. 3. Neither the name of the * Institute nor the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $Id: GUI.java,v 1.40 2007/04/02 16:50:07 fros4943 Exp $ */package se.sics.cooja;import java.awt.*;import java.awt.event.*;import java.beans.PropertyVetoException;import java.io.*;import java.net.URL;import java.net.URLClassLoader;import java.util.*;import java.util.List;import javax.swing.*;import javax.swing.event.MenuEvent;import javax.swing.event.MenuListener;import javax.swing.filechooser.FileFilter;import org.apache.log4j.Logger;import org.apache.log4j.xml.DOMConfigurator;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import se.sics.cooja.MoteType.MoteTypeCreationException;import se.sics.cooja.contikimote.*;import se.sics.cooja.dialogs.*;import se.sics.cooja.plugins.*;/** * Main file of COOJA Simulator. Typically contains a visualizer for the * simulator, but can also be started without visualizer. * * This class loads external Java classes (in project directories), and handles the * COOJA plugins as well as the configuration system. If provides a number of * help methods for the rest of the COOJA system, and is the starting point for * loading and saving simulation configs. * * @author Fredrik Osterlind */public class GUI { /** * External tools default Win32 settings filename. */ public static final String EXTERNAL_TOOLS_WIN32_SETTINGS_FILENAME = "/external_tools_win32.config"; /** * External tools default Linux/Unix settings filename. */ public static final String EXTERNAL_TOOLS_LINUX_SETTINGS_FILENAME = "/external_tools_linux.config"; /** * External tools user settings filename. */ public static final String EXTERNAL_TOOLS_USER_SETTINGS_FILENAME = ".cooja.user.properties"; /** * Logger settings filename. */ public static final String LOG_CONFIG_FILE = "log4j_config.xml"; /** * Default project configuration filename. */ public static final String PROJECT_DEFAULT_CONFIG_FILENAME = "/cooja_default.config"; /** * User project configuration filename. */ public static final String PROJECT_CONFIG_FILENAME = "cooja.config"; /** * File filter only showing saved simulations files (*.csc). */ public static final FileFilter SAVED_SIMULATIONS_FILES = new FileFilter() { public boolean accept(File file) { if (file.isDirectory()) return true; if (file.getName().endsWith(".csc")) return true; return false; } public String getDescription() { return "COOJA Configuration files"; } public String toString() { return ".csc"; } }; /** * Main frame for current GUI. Null when COOJA is run without visualizer! */ public static JFrame frame; private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(GUI.class); // External tools setting names private static Properties defaultExternalToolsSettings; private static Properties currentExternalToolsSettings; private static final String externalToolsSettingNames[] = new String[] { "PATH_CONTIKI", "PATH_COOJA_CORE_RELATIVE", "PATH_MAKE", "PATH_SHELL", "PATH_C_COMPILER", "COMPILER_ARGS", "PATH_LINKER", "PATH_NM", "NM_ARGS", "PATH_OBJDUMP", "OBJDUMP_ARGS", "LINKER_ARGS_1", "LINKER_ARGS_2", "CONTIKI_STANDARD_PROCESSES", "CMD_GREP_PROCESSES", "REGEXP_PARSE_PROCESSES", "CMD_GREP_INTERFACES", "REGEXP_PARSE_INTERFACES", "CMD_GREP_SENSORS", "REGEXP_PARSE_SENSORS", "CONTIKI_MAIN_TEMPLATE_FILENAME", "DEFAULT_PROJECTDIRS", "CORECOMM_TEMPLATE_FILENAME", "PATH_JAVAC"}; private static final int FRAME_NEW_OFFSET = 30; private static final int FRAME_STANDARD_WIDTH = 150; private static final int FRAME_STANDARD_HEIGHT = 300; private GUI myGUI; private Simulation mySimulation; protected GUIEventHandler guiEventHandler = new GUIEventHandler(); private JMenu menuPlugins, menuMoteTypeClasses, menuMoteTypes; private JMenu menuOpenSimulation, menuConfOpenSimulation; private Vector<Class<? extends Plugin>> menuMotePluginClasses; private JDesktopPane myDesktopPane; private Vector<Plugin> startedPlugins = new Vector<Plugin>(); // Platform configuration variables // Maintained via method reparseProjectConfig() private ProjectConfig projectConfig; private Vector<File> currentProjectDirs = new Vector<File>(); private ClassLoader projectDirClassLoader; private Vector<Class<? extends MoteType>> moteTypeClasses = new Vector<Class<? extends MoteType>>(); private Vector<Class<? extends Plugin>> pluginClasses = new Vector<Class<? extends Plugin>>(); private Vector<Class<? extends Plugin>> pluginClassesTemporary = new Vector<Class<? extends Plugin>>(); private Vector<Class<? extends RadioMedium>> radioMediumClasses = new Vector<Class<? extends RadioMedium>>(); private Vector<Class<? extends IPDistributor>> ipDistributorClasses = new Vector<Class<? extends IPDistributor>>(); private Vector<Class<? extends Positioner>> positionerClasses = new Vector<Class<? extends Positioner>>(); /** * Creates a new COOJA Simulator GUI. * * @param desktop Desktop pane * @param createSimDialog If true, a create simulation dialog will be displayed at startup */ public GUI(JDesktopPane desktop, boolean createSimDialog) { myGUI = this; mySimulation = null; myDesktopPane = desktop; // Load default and overwrite with user settings (if any) loadExternalToolsDefaultSettings(); loadExternalToolsUserSettings(); // Add menu bar if (frame != null) { frame.setJMenuBar(createMenuBar()); } // Register default project directories String defaultProjectDirs = getExternalToolsSetting( "DEFAULT_PROJECTDIRS", null); if (defaultProjectDirs != null) { String[] defaultProjectDirsArr = defaultProjectDirs.split(";"); if (defaultProjectDirsArr.length > 0) { for (String defaultProjectDir : defaultProjectDirsArr) { File projectDir = new File(defaultProjectDir); if (projectDir.exists() && projectDir.isDirectory()) { currentProjectDirs.add(projectDir); } } } } // Load extendable parts (using current project config) reparseProjectConfig(); // Start all standard GUI plugins for (Class<? extends Plugin> visPluginClass : pluginClasses) { int pluginType = visPluginClass.getAnnotation(PluginType.class).value(); if (pluginType == PluginType.COOJA_STANDARD_PLUGIN) { startPlugin(visPluginClass, this, null, null); } } if (createSimDialog && frame != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { myGUI.doCreateSimulation(true); } }); } } /** * @return True if simulator is visualized */ public boolean isVisualized() { return frame != null; } private Vector<File> getFileHistory() { Vector<File> history = new Vector<File>(); // Fetch current history String[] historyArray = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";"); for (String file: historyArray) { history.add(new File(file)); } return history; } private void addToFileHistory(File file) { // Fetch current history String[] history = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";"); // Create new history String[] newHistory = null; if (history == null || history.length <= 1 && history[0].equals("")) { newHistory = new String[1]; } else { newHistory = new String[Math.min(5, history.length+1)]; System.arraycopy(history, 0, newHistory, 1, newHistory.length-1); } newHistory[0] = file.getAbsolutePath(); // Abort if file added is equal to last file if (history.length >= 1 && file.getAbsolutePath().equals(new File(history[0]).getAbsolutePath())) { return; } String newHistoryConfig = null; for (String path: newHistory) { if (newHistoryConfig == null) newHistoryConfig = path; else newHistoryConfig += ";" + path; } setExternalToolsSetting("SIMCFG_HISTORY", newHistoryConfig); saveExternalToolsUserSettings(); } private void updateOpenHistoryMenuItems() { menuConfOpenSimulation.removeAll(); JMenuItem browseItem = new JMenuItem("Browse..."); browseItem.setActionCommand("confopen sim"); browseItem.addActionListener(guiEventHandler); menuConfOpenSimulation.add(browseItem); menuConfOpenSimulation.add(new JSeparator()); Vector<File> openFilesHistory = getFileHistory(); for (File file: openFilesHistory) { JMenuItem lastItem = new JMenuItem(file.getName()); lastItem.setActionCommand("confopen last sim"); lastItem.putClientProperty("file", file); lastItem.setToolTipText(file.getAbsolutePath()); lastItem.addActionListener(guiEventHandler); menuConfOpenSimulation.add(lastItem); } menuOpenSimulation.removeAll(); browseItem = new JMenuItem("Browse..."); browseItem.setActionCommand("open sim"); browseItem.addActionListener(guiEventHandler); menuOpenSimulation.add(browseItem); menuOpenSimulation.add(new JSeparator()); for (File file: openFilesHistory) { JMenuItem lastItem = new JMenuItem(file.getName()); lastItem.setActionCommand("open last sim"); lastItem.putClientProperty("file", file); lastItem.setToolTipText(file.getAbsolutePath()); lastItem.addActionListener(guiEventHandler); menuOpenSimulation.add(lastItem); } } private JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu menu; JMenuItem menuItem; // File menu menu = new JMenu("File"); menu.addMenuListener(new MenuListener() { public void menuSelected(MenuEvent e) { updateOpenHistoryMenuItems(); } public void menuDeselected(MenuEvent e) { } public void menuCanceled(MenuEvent e) { } }); menu.setMnemonic(KeyEvent.VK_F); menuBar.add(menu); menuItem = new JMenuItem("New simulation"); menuItem.setMnemonic(KeyEvent.VK_N); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); menuItem.setActionCommand("new sim"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); menuItem = new JMenuItem("Reload simulation"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reloadCurrentSimulation(); } }); menu.add(menuItem); menuItem = new JMenuItem("Close simulation"); menuItem.setMnemonic(KeyEvent.VK_C); menuItem.setActionCommand("close sim"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); menuOpenSimulation = new JMenu("Open simulation"); menuOpenSimulation.setMnemonic(KeyEvent.VK_O); menu.add(menuOpenSimulation); menuConfOpenSimulation = new JMenu("Open & Reconfigure simulation"); menuConfOpenSimulation.setMnemonic(KeyEvent.VK_R); menu.add(menuConfOpenSimulation); menuItem = new JMenuItem("Save simulation"); menuItem.setMnemonic(KeyEvent.VK_S); menuItem.setActionCommand("save sim"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); menu.addSeparator(); menuItem = new JMenuItem("Close all plugins"); menuItem.setActionCommand("close plugins");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -