📄 abstractguiapplication.java~1~
字号:
/*
* 11/20/2004
*
* AbstractGUIApplication.java - A basic GUI application.
* Copyright (C) 2004 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.app;
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.LookAndFeel;
import javax.swing.MenuSelectionManager;
import javax.swing.UIManager;
import org.fife.RUtilities;
import org.fife.ui.AboutDialog;
import org.fife.ui.CustomizableToolBar;
import org.fife.ui.SplashScreen;
import org.fife.ui.StatusBar;
import org.fife.help.HelpDialog;
/**
* A basic, generic GUI application. Your Swing application can
* override this class to have some of its basic code already implemented.<p>
*
* This class currently helps you implement:
* <ul>
* <li>A menu bar.</li>
* <li>A customizable toolbar.</li>
* <li>A status bar.</li>
* <li>A "Help" dialog.</li>
* <li>An "About" dialog.</li>
* <li>Easy localization.</li>
* <li>A way to capture <code>Exceptions</code> and report them to
* the user.</li>
* <li>An easy to organize/maintain the actions associated with the
* application's menu bar, toolbar, etc.</li>
* <li>A "splash screen" interface.</li>
* <li>Necessary Mac OS X hooks.</li>
* </ul>
*
* @author Robert Futrell
* @version 0.5
* @see org.fife.ui.app.AbstractPluggableGUIApplication
*/
public abstract class AbstractGUIApplication
extends JFrame
implements GUIApplication {
/**
* This property is fired whenever the status bar changes.
*/
public static final String STATUS_BAR_PROPERTY = "statusBar";
/**
* This property is fired whenever the status bar is made visible or
* invisible.
*/
public static final String STATUS_BAR_VISIBLE_PROPERTY = "statusBarVisible";
/**
* This property is fired whenever the toolbar changes.
*/
public static final String TOOL_BAR_PROPERTY = "toolBar";
/**
* This property is fired whenever the toolbar is made visible or
* invisible.
*/
public static final String TOOL_BAR_VISIBLE_PROPERTY = "toolBarVisible";
/**
* The key for getting the About action from <code>getAction</code>.
*/
public static final String ABOUT_ACTION_KEY = "aboutAction";
/**
* The key for getting the Exit action from <code>getAction</code>.
*/
public static final String EXIT_ACTION_KEY = "exitAction";
/**
* The key for getting the Help action from <code>getAction</code>.
*/
public static final String HELP_ACTION_KEY = "helpAction";
/**
* The About dialog.
*/
private AboutDialog aboutDialog;
/**
* The toolbar.
*/
private CustomizableToolBar toolBar;
/**
* The status bar.
*/
private StatusBar statusBar;
/**
* The map of actions for this application.
*/
private HashMap actionMap;
/**
* This application's resource bundle.
*/
private ResourceBundle resourceBundle;
/**
* The directory in which this application was installed.
*/
private String installLocation;
/**
* Locale language string (e.g., "en" or "es").
*/
private String language;
/**
* The content pane.
*/
private JPanel contentPane;
/**
* Panels containing toolbars. At least one of these always exists;
* it contains the main toolbar. The last-indexed panel will contain
* a toolbar and the <code>mainContentPanel</code>. Each
* increasing toolbar panel is contained in the previous one (i.e.,
* if there are 3 of these, 0 contains 1 and 1 contains 2, which also
* contains <code>mainContentPanel</code>).
*/
private JPanel[] toolBarPanels;
/**
* This panel contains <code>actualContentPane</code>. This panel is here
* so that the subclass <code>AbstractPluggableGUIApplication</code> can
* also place GUI plugins inside of it.
*/
protected JPanel mainContentPanel;
/**
* This panel contains the actual content of the application (i.e.,
* when the use calls <code>getContentPane</code> to add
* stuff, this is the panel they get).
*/
protected JPanel actualContentPane;
private static final String STATUS_BAR_LOCATION = BorderLayout.SOUTH;
private static final String TOOL_BAR_LOCATION = BorderLayout.NORTH;
/**
* An integer constant representing the OS, such as
* <code>OS_WINDOWS</code> or <code>OS_LINUX</code>.
*/
private int os;
/*****************************************************************************/
/**
* Constructor.
*
* @param jarFile The name (not full path) of the JAR file containing the
* main class of this application (e.g. "Foobar.jar").
*/
public AbstractGUIApplication(String jarFile) {
this(null, jarFile);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param title The title for this frame.
* @param jarFile The name (not full path) of the JAR file containing the
* main class of this application (e.g. "Foobar.jar").
*/
public AbstractGUIApplication(String title, String jarFile) {
super();
initialize(title, jarFile, loadPreferences());
}
/*****************************************************************************/
/**
* Constructor. This constructor is useful when you are making a clone of
* the current application (e.g., "Open in New Window...") and you want
* the two instances to have the same properties.
*
* @param title The title for this frame.
* @param jarFile The name (not full path) of the JAR file containing the
* main class of this application (e.g. "Foobar.jar").
* @param prefs The preferences with which to initialize this application.
*/
public AbstractGUIApplication(String title, String jarFile,
GUIApplicationPreferences prefs) {
super();
initialize(title, jarFile, prefs);
}
/*****************************************************************************/
/**
* Initializes this GUI application. This is separate from the
* constructors because we need the ability to call
* <code>loadPreferences</code> to get the preferences with which to
* initialize this class.
*
* @param title The title for this frame.
* @param jarFile The name (not full path) of the JAR file containing the
* main class of this application (e.g. "Foobar.jar").
* @param prefs The preferences with which to initialize this application.
*/
private void initialize(String title, String jarFile,
GUIApplicationPreferences prefs) {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
// Set up the localization stuff.
// NOTE: We default to English instead of using the JVM's default
// locale as we want to be sure all of RText's UI uses the same l10n.
// For this reason, our locale must be one RText has been translated
// into, and we have no guarantee that the user's JVM defaults to one
// of these locales. RText will remember the locale last selected by
// the user and use it the next time it starts.
setLanguage(prefs == null ? "en" : prefs.language);
Locale locale = null;
String language = getLanguage();
int underscore = language.indexOf('_');
if (underscore > -1) {
// Use Locale(lang, country) constructor.
locale = new Locale(language.substring(0, underscore),
language.substring(underscore + 1));
}
else {
// Use Locale(lang) constructor.
locale = new Locale(language);
}
Locale.setDefault(locale);
JComponent.setDefaultLocale(locale);
setTitle(title);
setInstallLocation(RUtilities.getLocationOfJar(jarFile));
// contentPane contains the status bar to the south and toolBarPane
// in the center (which contains everything else).
contentPane = new JPanel(new BorderLayout());
super.setContentPane(contentPane);
// Toolbar panels each contain 1 toolbar, with the last one
// also containing the mainContentPanel in its center. Each
// increasing index of a toolbar panel is contained in the
// lower-indexed one.
toolBarPanels = new JPanel[1];
toolBarPanels[0] = new JPanel(new BorderLayout());
contentPane.add(toolBarPanels[0]);
// actualContentPane contains the actual user content added via
// add().
actualContentPane = new JPanel(new BorderLayout());
// mainContentPanel contains both actualContentPane and possible
// GUIPlugins on any of the 4 sides if we're an instance of
// AbstractPluggableGUIApplication.
mainContentPanel = createMainContentPanel(actualContentPane);
toolBarPanels[0].add(mainContentPanel);
// Set the LnF if the saved one is different from the default. If
// any jars containing 3rd party Look and Feels are specified by
// getLookAndFeelJarFileURLs(), then they are added to the UIManager's
// classpath first so that if the saved LnF is in them, it can be
// loaded.
String lnfName = UIManager.getLookAndFeel().getClass().getName();
URLClassLoader cl = null;
try {
ExtendedLookAndFeelInfo[] lnfInfo = get3rdPartyLookAndFeelInfo();
int count = lnfInfo == null ? 0 : lnfInfo.length;
// 3rd party Look and Feel jars? Add them to classpath.
// NOTE: The lines of code below MUST be in the order they're
// in or stuff breaks for some reason; I'm not sure why...
if (count > 0) {
URL[] lnfJarURLs = new URL[count];
for (int i = 0; i < count; i++) {
lnfJarURLs[i] = lnfInfo[i].getURL(this);
}
cl = new URLClassLoader(lnfJarURLs,
this.getClass().getClassLoader());
if (prefs != null && prefs.lookAndFeel != null) {
// We must load the class for some reason...
Class c = cl.loadClass(prefs.lookAndFeel);
if (!c.getName().equals(lnfName)) {
LookAndFeel lnf = (LookAndFeel) c.newInstance();
UIManager.setLookAndFeel(lnf);
}
}
}
// No 3rd party Look and Feel jars (which is most likely)?
// Then simply update the LnF if necessary.
else {
cl = (URLClassLoader)this.getClass().getClassLoader();
if (prefs != null && prefs.lookAndFeel != null &&
!prefs.lookAndFeel.equals(lnfName)) {
UIManager.setLookAndFeel(prefs.lookAndFeel);
}
}
}
catch (Exception e) {
e.printStackTrace();
displayException(e);
}
finally {
// Must set this value AFTER UIManager.setLookAndFeel() call
// as that call resets this value to null (for the new LnF).
// We also put this in a finally block so our Look and Feel
// menu will still work even if an Exception is thrown above.
UIManager.getLookAndFeelDefaults().put("ClassLoader", cl);
}
// Create the splash screen, if this application has one.
SplashScreen splashScreen = createSplashScreen();
if (splashScreen != null) {
splashScreen.setVisible(true);
// Create the status bar.
}
preStatusBarInit(prefs, splashScreen);
StatusBar statusBar = createStatusBar(prefs);
setStatusBar(statusBar);
// Create the toolbar.
preToolBarInit(prefs, splashScreen);
CustomizableToolBar toolBar = createToolBar(prefs);
setToolBar(toolBar);
// Create the menu bar.
preMenuBarInit(prefs, splashScreen);
JMenuBar menuBar = createMenuBar(prefs);
setJMenuBar(menuBar);
// Do the rest of the subclass's custom initialization.
preDisplayInit(prefs, splashScreen);
// 1.5.2004/pwy: If running on a Mac OS X system, enable the
// application menu and other Apple specific functions.
possibleMacOSXRegistration();
// Set location/appearance properties.
Toolkit.getDefaultToolkit().setDynamicLayout(true);
pack();
if (prefs != null) {
if (prefs.location != null) {
setLocation(prefs.location);
}
if (prefs.size == null || prefs.size.equals(new Dimension( -1, -1))) {
setExtendedState(MAXIMIZED_BOTH);
}
else {
setSize(prefs.size);
}
setToolBarVisible(prefs.toolbarVisible);
setStatusBarVisible(prefs.statusBarVisible);
}
else {
setToolBarVisible(true);
setStatusBarVisible(true);
}
// Clean up the splash screen if necessary.
if (splashScreen != null) {
splashScreen.setVisible(false);
splashScreen = null;
//splashScreen.dispose();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -