📄 gui.java
字号:
} public 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; } public void addToFileHistory(File file) { // Fetch current history String[] history = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";"); String newFile = file.getAbsolutePath(); if (history.length > 0 && history[0].equals(newFile)) { // File already added return; } // Create new history StringBuilder newHistory = new StringBuilder(); newHistory.append(newFile); for (int i = 0, count = 1; i < history.length && count < 10; i++) { String historyFile = history[i]; if (newFile.equals(historyFile) || historyFile.length() == 0) { // File already added or empty file name } else { newHistory.append(';').append(historyFile); count++; } } setExternalToolsSetting("SIMCFG_HISTORY", newHistory.toString()); saveExternalToolsUserSettings(); } private void updateOpenHistoryMenuItems() { menuConfOpenSimulation.removeAll(); if (isVisualizedInApplet()) { return; } 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(false); } }); 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); if (isVisualizedInApplet()) { menuOpenSimulation.setEnabled(false); menuOpenSimulation.setToolTipText("Not available in applet version"); } menuConfOpenSimulation = new JMenu("Open & Reconfigure simulation"); menuConfOpenSimulation.setMnemonic(KeyEvent.VK_R); menu.add(menuConfOpenSimulation); if (isVisualizedInApplet()) { menuConfOpenSimulation.setEnabled(false); menuConfOpenSimulation.setToolTipText("Not available in applet version"); } menuItem = new JMenuItem("Save simulation"); menuItem.setMnemonic(KeyEvent.VK_S); menuItem.setActionCommand("save sim"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); if (isVisualizedInApplet()) { menuItem.setEnabled(false); menuItem.setToolTipText("Not available in applet version"); } menu.addSeparator(); menuItem = new JMenuItem("Close all plugins"); menuItem.setActionCommand("close plugins"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); menu.addSeparator(); menuItem = new JMenuItem("Exit"); menuItem.setMnemonic(KeyEvent.VK_X); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); menuItem.setActionCommand("quit"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); if (isVisualizedInApplet()) { menuItem.setEnabled(false); menuItem.setToolTipText("Not available in applet version"); } // Simulation menu menu = new JMenu("Simulation"); menu.setMnemonic(KeyEvent.VK_S); menuBar.add(menu); menuItem = new JMenuItem("Open Control"); menuItem.setMnemonic(KeyEvent.VK_C); menuItem.setActionCommand("start plugin"); menuItem.putClientProperty("class", SimControl.class); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); menuItem = new JMenuItem("Information"); menuItem.setMnemonic(KeyEvent.VK_I); menuItem.setActionCommand("start plugin"); menuItem.putClientProperty("class", SimInformation.class); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); // Mote type menu menu = new JMenu("Mote Types"); menu.setMnemonic(KeyEvent.VK_T); menuBar.add(menu); // Mote type classes sub menu menuMoteTypeClasses = new JMenu("Create mote type"); menuMoteTypeClasses.setMnemonic(KeyEvent.VK_C); menuMoteTypeClasses.addMenuListener(new MenuListener() { public void menuSelected(MenuEvent e) { // Clear menu menuMoteTypeClasses.removeAll(); // Recreate menu items JMenuItem menuItem; for (Class<? extends MoteType> moteTypeClass : moteTypeClasses) { /* Sort mote types according to abstraction level */ String abstractionLevelDescription = GUI.getAbstractionLevelDescriptionOf(moteTypeClass); if(abstractionLevelDescription == null) { abstractionLevelDescription = "[unknown cross-level]"; } /* Check if abstraction description already exists */ JSeparator abstractionLevelSeparator = null; for (Component component: menuMoteTypeClasses.getMenuComponents()) { if (component == null || !(component instanceof JSeparator)) { continue; } JSeparator existing = (JSeparator) component; if (abstractionLevelDescription.equals(existing.getToolTipText())) { abstractionLevelSeparator = existing; break; } } if (abstractionLevelSeparator == null) { abstractionLevelSeparator = new JSeparator(); abstractionLevelSeparator.setToolTipText(abstractionLevelDescription); menuMoteTypeClasses.add(abstractionLevelSeparator); } String description = GUI.getDescriptionOf(moteTypeClass); menuItem = new JMenuItem(description); menuItem.setActionCommand("create mote type"); menuItem.putClientProperty("class", moteTypeClass); menuItem.setToolTipText(abstractionLevelDescription); menuItem.addActionListener(guiEventHandler); if (isVisualizedInApplet() && moteTypeClass.equals(ContikiMoteType.class)) { menuItem.setEnabled(false); menuItem.setToolTipText("Not available in applet version"); } /* Add new item directly after cross level separator */ for (int i=0; i < menuMoteTypeClasses.getMenuComponentCount(); i++) { if (menuMoteTypeClasses.getMenuComponent(i) == abstractionLevelSeparator) { menuMoteTypeClasses.add(menuItem, i+1); break; } } } } public void menuDeselected(MenuEvent e) { } public void menuCanceled(MenuEvent e) { } }); menu.add(menuMoteTypeClasses); menuItem = new JMenuItem("Information"); menuItem.setActionCommand("start plugin"); menuItem.putClientProperty("class", MoteTypeInformation.class); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); // Mote menu menu = new JMenu("Motes"); menu.setMnemonic(KeyEvent.VK_M); menuBar.add(menu); // Mote types sub menu menuMoteTypes = new JMenu("Add motes of type"); menuMoteTypes.setMnemonic(KeyEvent.VK_A); menuMoteTypes.addMenuListener(new MenuListener() { public void menuSelected(MenuEvent e) { // Clear menu menuMoteTypes.removeAll(); if (mySimulation == null) { return; } // Recreate menu items JMenuItem menuItem; for (MoteType moteType : mySimulation.getMoteTypes()) { menuItem = new JMenuItem(moteType.getDescription()); menuItem.setActionCommand("add motes"); menuItem.setToolTipText(getDescriptionOf(moteType.getClass())); menuItem.putClientProperty("motetype", moteType); menuItem.addActionListener(guiEventHandler); menuMoteTypes.add(menuItem); } } public void menuDeselected(MenuEvent e) { } public void menuCanceled(MenuEvent e) { } }); menu.add(menuMoteTypes); menuItem = new JMenuItem("Remove all motes"); menuItem.setActionCommand("remove all motes"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); // Plugins menu if (menuPlugins == null) { menuPlugins = new JMenu("Plugins"); } else { menuPlugins.setText("Plugins"); } menuPlugins.setMnemonic(KeyEvent.VK_P); menuBar.add(menuPlugins); // Settings menu menu = new JMenu("Settings"); menuBar.add(menu); menuItem = new JMenuItem("External tools paths"); menuItem.setActionCommand("edit paths"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); if (isVisualizedInApplet()) { menuItem.setEnabled(false); menuItem.setToolTipText("Not available in applet version"); } menuItem = new JMenuItem("Manage project directories"); menuItem.setActionCommand("manage projects"); menuItem.addActionListener(guiEventHandler); menu.add(menuItem); if (isVisualizedInApplet()) { menuItem.setEnabled(false); menuItem.setToolTipText("Not available in applet version"); } menu.addSeparator(); menuItem = new JMenuItem("Java version: " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); menuItem.setEnabled(false); menu.add(menuItem); // Mote plugins popup menu (not available via menu bar) if (menuMotePluginClasses == null) { menuMotePluginClasses = new Vector<Class<? extends Plugin>>(); } return menuBar; } private static void configureFrame(final GUI gui, boolean createSimDialog) { // Create and set up the window. frame = new JFrame("COOJA Simulator"); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // Add menu bar frame.setJMenuBar(gui.createMenuBar()); JComponent newContentPane = gui.getDesktopPane(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.setSize(700, 700); frame.setLocationRelativeTo(null); frame.addWindowListener(gui.guiEventHandler); /* Restore frame size and position */ int framePosX = Integer.parseInt(getExternalToolsSetting("FRAME_POS_X", "0")); int framePosY = Integer.parseInt(getExternalToolsSetting("FRAME_POS_Y", "0")); int frameWidth = Integer.parseInt(getExternalToolsSetting("FRAME_WIDTH", "0")); int frameHeight = Integer.parseInt(getExternalToolsSetting("FRAME_HEIGHT", "0")); String frameScreen = getExternalToolsSetting("FRAME_SCREEN", ""); /* Restore position to the same graphics device */ GraphicsDevice device = null; GraphicsDevice all[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); for (GraphicsDevice gd : all) { if (gd.getIDstring().equals(frameScreen)) { device = gd; } } /* Check if frame should be maximized */ if (device != null) { if (frameWidth == Integer.MAX_VALUE && frameHeight == Integer.MAX_VALUE) { frame.setLocation(device.getDefaultConfiguration().getBounds().getLocation()); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); } else if (frameWidth > 0 && frameHeight > 0) { frame.setLocation(framePosX, framePosY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -