mainwindow.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 673 行 · 第 1/2 页

JAVA
673
字号
        System.exit(1);
    }

    /**
     * Setup the Main Toolbar with File, Tools and Help.
     */
    private void buildMenu() {
        // setup file menu
        JMenuItem exitMenuItem = new JMenuItem();

        // Setup ResourceUtils
        ResourceUtils.resButton(connectMenu, "&" + Default.getString(Default.APPLICATION_NAME));
        ResourceUtils.resButton(contactsMenu, Res.getString("menuitem.contacts"));
        ResourceUtils.resButton(actionsMenu, Res.getString("menuitem.actions"));
        ResourceUtils.resButton(exitMenuItem, Res.getString("menuitem.exit"));
        ResourceUtils.resButton(pluginsMenu, Res.getString("menuitem.plugins"));

        exitMenuItem.setIcon(null);

        mainWindowBar.add(connectMenu);
        mainWindowBar.add(contactsMenu);
        mainWindowBar.add(actionsMenu);
        //mainWindowBar.add(pluginsMenu);
        mainWindowBar.add(helpMenu);


        preferenceMenuItem = new JMenuItem(SparkRes.getImageIcon(SparkRes.PREFERENCES_IMAGE));
        preferenceMenuItem.setText(Res.getString("title.spark.preferences"));
        preferenceMenuItem.addActionListener(this);
        connectMenu.add(preferenceMenuItem);
        connectMenu.addSeparator();

        JMenuItem logoutMenuItem = new JMenuItem();
        ResourceUtils.resButton(logoutMenuItem, Res.getString("menuitem.logout.no.status"));
        logoutMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                logout(false);
            }
        });

        JMenuItem logoutWithStatus = new JMenuItem();
        ResourceUtils.resButton(logoutWithStatus, Res.getString("menuitem.logout.with.status"));
        logoutWithStatus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                logout(true);
            }
        });


        if (Spark.isWindows()) {
            connectMenu.add(logoutMenuItem);
            connectMenu.add(logoutWithStatus);
        }

        connectMenu.addSeparator();

        if (!Spark.isMac()) {
            connectMenu.add(exitMenuItem);
        }


        Action updateAction = new AbstractAction() {
            public void actionPerformed(ActionEvent actionEvent) {
                checkForUpdates(true);
            }
        };

        updateAction.putValue(Action.NAME, Res.getString("menuitem.check.for.updates"));
        updateAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.DOWNLOAD_16x16));

        // Add Error Dialog Viewer
        Action viewErrors = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                File logDir = new File(Spark.getLogDirectory(), "errors.log");
                if (!logDir.exists()) {
                    JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "No error logs found.", "Error Log", JOptionPane.INFORMATION_MESSAGE);
                }
                else {
                    showErrorLog();
                }
            }
        };

        viewErrors.putValue(Action.NAME, "View Logs");

        final Action viewHelpGuideAction = new AbstractAction() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    BrowserLauncher.openURL("http://www.igniterealtime.org/builds/spark/docs/spark_user_guide.pdf");
                }
                catch (IOException e) {
                    Log.error("Unable to load online help.", e);
                }
            }
        };

        if (!Spark.isCustomBuild()) {
            viewHelpGuideAction.putValue(Action.NAME, Res.getString("menuitem.user.guide"));
            viewHelpGuideAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_QUESTION));
            helpMenu.add(viewHelpGuideAction);
            helpMenu.add(helpMenuItem);
        }

        // Build Help Menu
        helpMenu.add(updateAction);
        helpMenu.addSeparator();
        helpMenu.add(viewErrors);


        helpMenu.add(menuAbout);

        // ResourceUtils - Adds mnemonics
        ResourceUtils.resButton(preferenceMenuItem, Res.getString("menuitem.preferences"));
        ResourceUtils.resButton(helpMenu, Res.getString("menuitem.help"));
        ResourceUtils.resButton(menuAbout, Res.getString("menuitem.about"));
        ResourceUtils.resButton(helpMenuItem, Res.getString("menuitem.online.help"));

        // Register shutdown with the exit menu.
        exitMenuItem.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                shutdown();
            }
        });

        helpMenuItem.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                try {
                    BrowserLauncher.openURL("http://www.igniterealtime.org/forum/forum.jspa?forumID=49");
                }
                catch (Exception browserException) {
                    Log.error("Error launching browser:", browserException);
                }
            }
        });

        // Show About Box
        menuAbout.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                showAboutBox();
            }
        });

        // Execute spark update checker after one minute.
        final TimerTask task = new SwingTimerTask() {
            public void doRun() {
                checkForUpdates(false);
            }
        };

        TaskEngine.getInstance().schedule(task, 60000);

    }

    /**
     * Returns the JMenuBar for the MainWindow. You would call this if you
     * wished to add or remove menu items to the main menubar. (File | Tools | Help)
     *
     * @return the Jive Talker Main Window MenuBar
     */
    public JMenuBar getMenu() {
        return mainWindowBar;
    }

    /**
     * Returns the Menu in the JMenuBar by it's name. For example:<p>
     * <pre>
     * JMenu toolsMenu = getMenuByName("Tools");
     * </pre>
     * </p>
     *
     * @param name the name of the Menu.
     * @return the JMenu item with the requested name.
     */
    public JMenu getMenuByName(String name) {
        for (int i = 0; i < getMenu().getMenuCount(); i++) {
            JMenu menu = getMenu().getMenu(i);
            if (menu.getText().equals(name)) {
                return menu;
            }
        }
        return null;
    }

    /**
     * Returns true if the Spark window is in focus.
     *
     * @return true if the Spark window is in focus.
     */
    public boolean isInFocus() {
        return focused;
    }

    private class MainWindowFocusListener implements WindowFocusListener {

        public void windowGainedFocus(WindowEvent e) {
            focused = true;
        }

        public void windowLostFocus(WindowEvent e) {
            focused = false;
        }
    }

    /**
     * Return the top toolbar in the Main Window to allow for customization.
     *
     * @return the MainWindows top toolbar.
     */
    public JToolBar getTopToolBar() {
        return topToolbar;
    }

    /**
     * Checks for the latest update on the server.
     *
     * @param forced true if you want to bypass the normal checking security.
     */
    private void checkForUpdates(final boolean forced) {
        final CheckUpdates updater = new CheckUpdates();
        try {
            final SwingWorker updateThread = new SwingWorker() {
                public Object construct() {
                    try {
                        Thread.sleep(50);
                    }
                    catch (InterruptedException e) {
                        Log.error(e);
                    }
                    return "ok";
                }

                public void finished() {
                    try {
                        updater.checkForUpdate(forced);
                    }
                    catch (Exception e) {
                        Log.error("There was an error while checking for a new update.", e);
                    }
                }
            };

            updateThread.start();

        }
        catch (Exception e) {
            Log.warning("Error updating.", e);
        }
    }

    /**
     * Displays the About Box for Spark.
     */
    private static void showAboutBox() {
        JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Default.getString(Default.APPLICATION_NAME) + " " + JiveInfo.getVersion(),
            "About", JOptionPane.INFORMATION_MESSAGE);
    }

    /**
     * Displays the Spark error log.
     */
    private void showErrorLog() {
        final File logDir = new File(Spark.getLogDirectory(), "errors.log");

        // Read file and show
        final String errorLogs = URLFileSystem.getContents(logDir);

        final JFrame frame = new JFrame("Client Logs");
        frame.setLayout(new BorderLayout());
        frame.setIconImage(SparkManager.getApplicationImage().getImage());

        final JTextPane pane = new JTextPane();
        pane.setBackground(Color.white);
        pane.setFont(new Font("Dialog", Font.PLAIN, 12));
        pane.setEditable(false);
        pane.setText(errorLogs);

        frame.add(new JScrollPane(pane), BorderLayout.CENTER);

        final JButton copyButton = new JButton("Copy To Clipboard");
        frame.add(copyButton, BorderLayout.SOUTH);

        copyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SparkManager.setClipboard(errorLogs);
                copyButton.setEnabled(false);
            }
        });

        frame.pack();
        frame.setSize(600, 400);

        GraphicUtils.centerWindowOnScreen(frame);
        frame.setVisible(true);
    }

    /**
     * Saves the layout on closing of the main window.
     */
    public void saveLayout() {
        try {
            LayoutSettings settings = LayoutSettingsManager.getLayoutSettings();
            settings.setMainWindowHeight(getHeight());
            settings.setMainWindowWidth(getWidth());
            settings.setMainWindowX(getX());
            settings.setMainWindowY(getY());
            LayoutSettingsManager.saveLayoutSettings();
        }
        catch (Exception e) {
            // Don't let this cause a real problem shutting down.
        }
    }

    /**
     * Return true if the MainWindow is docked.
     *
     * @return true if the window is docked.
     */
    public boolean isDocked() {
        LocalPreferences preferences = SettingsManager.getLocalPreferences();
        return preferences.isDockingEnabled();
    }

    /**
     * Returns the inner split pane.
     *
     * @return the split pane.
     */
    public JSplitPane getSplitPane() {
        // create the split pane only if required.
        if (splitPane == null) {
            splitPane = new JSplitPane();
        }
        return this.splitPane;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?