📄 guiutilities.java
字号:
/* * GUIUtilities.java - Various GUI utility functions * Copyright (C) 1999, 2000, 2001 Slava Pestov * * 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.gjt.sp.jedit;import gnu.regexp.REException;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.File;import java.net.*;import java.util.Hashtable;import java.util.StringTokenizer;import org.gjt.sp.jedit.browser.*;import org.gjt.sp.jedit.gui.*;import org.gjt.sp.jedit.io.VFS;import org.gjt.sp.jedit.msg.PropertiesChanged;import org.gjt.sp.jedit.syntax.SyntaxStyle;import org.gjt.sp.jedit.syntax.Token;import org.gjt.sp.util.Log;/** * Class with several useful GUI functions.<p> * * It provides methods for: * <ul> * <li>Loading menu bars, menus and menu items from the properties * <li>Loading popup menus from the properties * <li>Loading tool bars and tool bar buttons from the properties * <li>Displaying various common dialog boxes * <li>Converting string representations of colors to color objects * <li>Loading and saving window geometry from the properties * <li>Displaying file open and save dialog boxes * <li>Loading images and caching them * </ul> * * @author Slava Pestov * @version $Id: GUIUtilities.java,v 1.1.1.1 2001/09/02 05:37:21 spestov Exp $ */public class GUIUtilities{ // some icons public static final Icon NEW_BUFFER_ICON; public static final Icon DIRTY_BUFFER_ICON; public static final Icon READ_ONLY_BUFFER_ICON; public static final Icon NORMAL_BUFFER_ICON; public static final Icon EDITOR_WINDOW_ICON; public static final Icon PLUGIN_WINDOW_ICON; /** * Creates a menubar. Plugins should not need to call this method. * @param name The menu bar name * @since jEdit 3.2pre5 */ public static JMenuBar loadMenuBar(String name) { String menus = jEdit.getProperty(name); StringTokenizer st = new StringTokenizer(menus); JMenuBar mbar = new JMenuBar(); while(st.hasMoreTokens()) mbar.add(GUIUtilities.loadMenu(st.nextToken())); return mbar; } /** * Creates a menu. This form of loadMenu() does not need to be used * by plugins; use the other form instead. * @param view The view to load the menu for * @param name The menu name * @since jEdit 2.6pre2 */ public static JMenu loadMenu(String name) { if(name.equals("open-encoding")) return new OpenWithEncodingMenu(); else if(name.equals("recent-files")) return new RecentFilesMenu(); else if(name.equals("current-directory")) return new CurrentDirectoryMenu(); else if(name.equals("markers")) return new MarkersMenu(); else if(name.equals("macros")) return new MacrosMenu(); else if(name.equals("plugins")) return new PluginsMenu(); else return new EnhancedMenu(name); } /** * Creates a popup menu. * @param name The menu name * @since jEdit 2.6pre2 */ public static JPopupMenu loadPopupMenu(String name) { JPopupMenu menu = new JPopupMenu(); String menuItems = jEdit.getProperty(name); if(menuItems != null) { StringTokenizer st = new StringTokenizer(menuItems); while(st.hasMoreTokens()) { String menuItemName = st.nextToken(); if(menuItemName.equals("-")) menu.addSeparator(); else { if(menuItemName.startsWith("%")) menu.add(loadMenu(menuItemName.substring(1))); else menu.add(loadMenuItem(menuItemName,false)); } } } return menu; } /** * Creates a menu item. * @param name The menu item name * @since jEdit 2.6pre1 */ public static JMenuItem loadMenuItem(String name) { return loadMenuItem(name,true); } /** * Creates a menu item. * @param name The menu item name * @param setMnemonic True if the menu item should have a mnemonic * @since jEdit 3.1pre1 */ public static JMenuItem loadMenuItem(String name, boolean setMnemonic) { String label; EditAction action; // HACK if(name.startsWith("play-macro@")) { Macros.Macro macro = Macros.getMacro(name.substring(11)); if(macro != null) { label = macro.name; int index = label.lastIndexOf('/'); label = label.substring(index + 1) .replace('_',' '); action = macro.action; } else { label = name.substring(11); action = null; } } else { action = jEdit.getAction(name); label = jEdit.getProperty(name.concat(".label")); if(label == null) label = name; } char mnemonic; int index = label.indexOf('$'); if(index != -1 && label.length() - index > 1) { mnemonic = Character.toLowerCase(label.charAt(index + 1)); label = label.substring(0,index).concat(label.substring(++index)); } else mnemonic = '\0'; JMenuItem mi; if(action != null && action.isToggle()) mi = new EnhancedCheckBoxMenuItem(label,action); else mi = new EnhancedMenuItem(label,action); if(setMnemonic && mnemonic != '\0') mi.setMnemonic(mnemonic); return mi; } /** * Creates a toolbar. * @param name The toolbar name */ public static JToolBar loadToolBar(String name) { JToolBar toolBar = new JToolBar(); toolBar.setFloatable(false); toolBar.putClientProperty("JToolBar.isRollover",Boolean.TRUE); String buttons = jEdit.getProperty(name); if(buttons != null) { StringTokenizer st = new StringTokenizer(buttons); while(st.hasMoreTokens()) { String button = st.nextToken(); if(button.equals("-")) toolBar.addSeparator(); else { JButton b = loadToolButton(button); if(b != null) toolBar.add(b); } } } return toolBar; } /** * Loads a tool bar button. The tooltip is constructed from * the <code><i>name</i>.label</code> and * <code><i>name</i>.shortcut</code> properties and the icon is loaded * from the resource named '/org/gjt/sp/jedit/icons/' suffixed * with the value of the <code><i>name</i>.icon</code> property. * @param name The name of the button */ public static EnhancedButton loadToolButton(String name) { String label; EditAction action; // HACK if(name.startsWith("play-macro@")) { Macros.Macro macro = Macros.getMacro(name.substring(11)); if(macro != null) { label = macro.name; int index = label.lastIndexOf('/'); label = label.substring(index + 1) .replace('_',' '); action = macro.action; } else { label = name.substring(11); action = null; } } else { action = jEdit.getAction(name); label = jEdit.getProperty(name.concat(".label")); if(label == null) label = name; else label = prettifyMenuLabel(label); } Icon icon; String iconName = jEdit.getProperty(name + ".icon"); if(iconName != null) { icon = loadIcon(iconName); if(icon == null) return null; } else return null; String toolTip = label; String shortcut = jEdit.getProperty(name + ".shortcut"); if(shortcut != null) toolTip = toolTip + " (" + shortcut + ")"; return new EnhancedButton(icon,toolTip,action); } /** * Loads a tool bar icon. * @param iconName The icon name * @since jEdit 2.6pre7 */ public static Icon loadIcon(String iconName) { // check if there is a cached version first Icon icon = (Icon)icons.get(iconName); if(icon != null) return icon; // get the icon if(iconName.startsWith("file:")) { icon = new ImageIcon(iconName.substring(5)); } else { URL url = GUIUtilities.class.getResource( "/org/gjt/sp/jedit/icons/" + iconName); if(url == null) { Log.log(Log.ERROR,GUIUtilities.class, "Icon not found: " + iconName); return null; } icon = new ImageIcon(url); } icons.put(iconName,icon); return icon; } /** * `Prettifies' a menu item label by removing the `$' sign. This * can be used to process the contents of an <i>action</i>.label * property. */ public static String prettifyMenuLabel(String label) { int index = label.indexOf('$'); if(index != -1) { label = label.substring(0,index) .concat(label.substring(index + 1)); } return label; } /** * Displays a dialog box. * The title of the dialog is fetched from * the <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. The message * is formatted by the property manager with <code>args</code> as * positional parameters. * @param comp The component to display the dialog for * @param name The name of the dialog * @param args Positional parameters to be substituted into the * message text */ public static void message(Component comp, String name, Object[] args) { hideSplashScreen(); JOptionPane.showMessageDialog(comp, jEdit.getProperty(name.concat(".message"),args), jEdit.getProperty(name.concat(".title"),args), JOptionPane.INFORMATION_MESSAGE); } /** * Displays an error dialog box. * The title of the dialog is fetched from * the <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. The message * is formatted by the property manager with <code>args</code> as * positional parameters. * @param comp The component to display the dialog for * @param name The name of the dialog * @param args Positional parameters to be substituted into the * message text */ public static void error(Component comp, String name, Object[] args) { hideSplashScreen(); JOptionPane.showMessageDialog(comp, jEdit.getProperty(name.concat(".message"),args), jEdit.getProperty(name.concat(".title"),args), JOptionPane.ERROR_MESSAGE); } /** * Displays an input dialog box and returns any text the user entered. * The title of the dialog is fetched from * the <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. * @param comp The component to display the dialog for * @param name The name of the dialog * @param def The text to display by default in the input field */ public static String input(Component comp, String name, Object def) { return input(comp,name,null,def); } /** * Displays an input dialog box and returns any text the user entered. * The title of the dialog is fetched from * the <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. * @param comp The component to display the dialog for * @param name The name of the dialog * @param def The property whose text to display in the input field */ public static String inputProperty(Component comp, String name, String def) { return inputProperty(comp,name,null,def); } /** * Displays an input dialog box and returns any text the user entered. * The title of the dialog is fetched from * the <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. * @param comp The component to display the dialog for * @param name The name of the dialog * @param def The text to display by default in the input field * @param args Positional parameters to be substituted into the * message text * @since jEdit 3.1pre3 */ public static String input(Component comp, String name, Object[] args, Object def) { hideSplashScreen(); String retVal = (String)JOptionPane.showInputDialog(comp, jEdit.getProperty(name.concat(".message"),args), jEdit.getProperty(name.concat(".title")), JOptionPane.QUESTION_MESSAGE,null,null,def); return retVal; } /** * Displays an input dialog box and returns any text the user entered. * The title of the dialog is fetched from * the <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. * @param comp The component to display the dialog for * @param name The name of the dialog * @param args Positional parameters to be substituted into the * message text * @param def The property whose text to display in the input field * @since jEdit 3.1pre3 */ public static String inputProperty(Component comp, String name, Object[] args, String def) { hideSplashScreen(); String retVal = (String)JOptionPane.showInputDialog(comp, jEdit.getProperty(name.concat(".message"),args), jEdit.getProperty(name.concat(".title")), JOptionPane.QUESTION_MESSAGE, null,null,jEdit.getProperty(def)); if(retVal != null) jEdit.setProperty(def,retVal); return retVal; } /** * Displays a confirm dialog box and returns the button pushed by the * user. The title of the dialog is fetched from the * <code><i>name</i>.title</code> property. The message is fetched * from the <code><i>name</i>.message</code> property. * @param comp The component to display the dialog for * @param name The name of the dialog * @param args Positional parameters to be substituted into the * message text * @param buttons The buttons to display - for example, * JOptionPane.YES_NO_CANCEL_OPTION * @param type The dialog type - for example, * JOptionPane.WARNING_MESSAGE * @since jEdit 3.1pre3 */ public static int confirm(Component comp, String name, Object[] args, int buttons, int type) { hideSplashScreen(); return JOptionPane.showConfirmDialog(comp, jEdit.getProperty(name + ".message",args), jEdit.getProperty(name + ".title"),buttons,type); } /** * Displays a VFS file selection dialog box. * @param view The view * @param path The initial directory to display. May be null * @param type The dialog type * @param multipleSelection True if multiple selection should be allowed * @return The selected file(s) * @since jEdit 2.6pre2 */ public static String[] showVFSFileDialog(View view, String path, int type, boolean multipleSelection) { VFSFileChooserDialog fileChooser = new VFSFileChooserDialog( view,path,type,multipleSelection); String[] selectedFiles = fileChooser.getSelectedFiles(); if(selectedFiles == null) return null; return selectedFiles; } /** * Converts a color name to a color object. The name must either be * a known string, such as `red', `green', etc (complete list is in * the <code>java.awt.Color</code> class) or a hex color value * prefixed with `#', for example `#ff0088'. * @param name The color name */ public static Color parseColor(String name) { return parseColor(name, Color.black); } public static Color parseColor(String name, Color defaultColor) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -