📄 guiutilities.java
字号:
/** * 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); } //}}} //{{{ input() method /** * 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); } //}}} //{{{ inputProperty() method /** * 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); } //}}} //{{{ input() method /** * 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; } //}}} //{{{ inputProperty() method /** * 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; } //}}} //{{{ confirm() method /** * 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); } //}}} //{{{ showVFSFileDialog() method /** * Displays a VFS file selection dialog box. * @param view The view, should be non-null * @param path The initial directory to display. May be null * @param type The dialog type. One of * {@link org.gjt.sp.jedit.browser.VFSBrowser#OPEN_DIALOG}, * {@link org.gjt.sp.jedit.browser.VFSBrowser#SAVE_DIALOG}, or * {@link org.gjt.sp.jedit.browser.VFSBrowser#CHOOSE_DIRECTORY_DIALOG}. * @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) { // the view should not be null, but some plugins might do this if(view == null) { Log.log(Log.WARNING,GUIUtilities.class, "showVFSFileDialog(): given null view, assuming jEdit.getActiveView()"); view = jEdit.getActiveView(); } hideSplashScreen(); VFSFileChooserDialog fileChooser = new VFSFileChooserDialog( view,path,type,multipleSelection); String[] selectedFiles = fileChooser.getSelectedFiles(); if(selectedFiles == null) return null; return selectedFiles; } //}}} //}}} //{{{ Colors and styles //{{{ parseColor() method /** * 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); } //}}} //{{{ parseColor() method public static Color parseColor(String name, Color defaultColor) { if(name == null) return defaultColor; else if(name.startsWith("#")) { try { return Color.decode(name); } catch(NumberFormatException nf) { return defaultColor; } } else if("red".equals(name)) return Color.red; else if("green".equals(name)) return Color.green; else if("blue".equals(name)) return Color.blue; else if("yellow".equals(name)) return Color.yellow; else if("orange".equals(name)) return Color.orange; else if("white".equals(name)) return Color.white; else if("lightGray".equals(name)) return Color.lightGray; else if("gray".equals(name)) return Color.gray; else if("darkGray".equals(name)) return Color.darkGray; else if("black".equals(name)) return Color.black; else if("cyan".equals(name)) return Color.cyan; else if("magenta".equals(name)) return Color.magenta; else if("pink".equals(name)) return Color.pink; else return defaultColor; } //}}} //{{{ getColorHexString() method /** * Converts a color object to its hex value. The hex value * prefixed is with `#', for example `#ff0088'. * @param c The color object */ public static String getColorHexString(Color c) { String colString = Integer.toHexString(c.getRGB() & 0xffffff); return "#000000".substring(0,7 - colString.length()).concat(colString); } //}}} //{{{ parseStyle() method /** * Converts a style string to a style object. * @param str The style string * @param family Style strings only specify font style, not font family * @param size Style strings only specify font style, not font family * @exception IllegalArgumentException if the style is invalid * @since jEdit 3.2pre6 */ public static SyntaxStyle parseStyle(String str, String family, int size) throws IllegalArgumentException { return parseStyle(str,family,size,true); } //}}} //{{{ parseStyle() method /** * Converts a style string to a style object. * @param str The style string * @param family Style strings only specify font style, not font family * @param size Style strings only specify font style, not font family * @param color If false, the styles will be monochrome * @exception IllegalArgumentException if the style is invalid * @since jEdit 4.0pre4 */ public static SyntaxStyle parseStyle(String str, String family, int size, boolean color) throws IllegalArgumentException { Color fgColor = Color.black; Color bgColor = null; boolean italic = false; boolean bold = false; StringTokenizer st = new StringTokenizer(str); while(st.hasMoreTokens()) { String s = st.nextToken(); if(s.startsWith("color:")) { if(color) fgColor = GUIUtilities.parseColor(s.substring(6), Color.black); } else if(s.startsWith("bgColor:")) { if(color) bgColor = GUIUtilities.parseColor(s.substring(8), null); } else if(s.startsWith("style:")) { for(int i = 6; i < s.length(); i++) { if(s.charAt(i) == 'i') italic = true; else if(s.charAt(i) == 'b') bold = true; else throw new IllegalArgumentException( "Invalid style: " + s); } } else throw new IllegalArgumentException( "Invalid directive: " + s); } return new SyntaxStyle(fgColor,bgColor, new Font(family, (italic ? Font.ITALIC : 0) | (bold ? Font.BOLD : 0), size)); } //}}} //{{{ getStyleString() method /** * Converts a style into it's string representation. * @param style The style */ public static String getStyleString(SyntaxStyle style) { StringBuffer buf = new StringBuffer(); if(style.getForegroundColor() != null) { buf.append("color:" + getColorHexString(style.getForegroundColor())); } if(style.getBackgroundColor() != null) { buf.append(" bgColor:" + getColorHexString(style.getBackgroundColor())); } if(!style.getFont().isPlain()) { buf.append(" style:" + (style.getFont().isItalic() ? "i" : "") + (style.getFont().isBold() ? "b" : "")); } return buf.toString(); } //}}} //{{{ loadStyles() method /** * Loads the syntax styles from the properties, giving them the specified * base font family and size. * @param family The font family * @param size The font size * @since jEdit 3.2pre6 */ public static SyntaxStyle[] loadStyles(String family, int size) { return loadStyles(family,size,true); } //}}} //{{{ loadStyles() method /** * Loads the syntax styles from the properties, giving them the specified * base font family and size. * @param family The font family * @param size The font size * @param color If false, the styles will be monochrome * @since jEdit 4.0pre4 */ public static SyntaxStyle[] loadStyles(String family, int size, boolean color) { SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT]; // start at 1 not 0 to skip Token.NULL for(int i = 1; i < styles.length; i++) { try { String styleName = "view.style." + Token.tokenToString((byte)i) .toLowerCase(); styles[i] = GUIUtilities.parseStyle( jEdit.getProperty(styleName), family,size,color); } catch(Exception e) { Log.log(Log.ERROR,GUIUtilities.class,e); } } return styles; } //}}} //}}} //{{{ Loading, saving window geometry //{{{ loadGeometry() method /** * Loads a windows's geometry from the properties. * The geometry is loaded from the <code><i>name</i>.x</code>, * <code><i>name</i>.y</code>, <code><i>name</i>.width</code> and * <code><i>name</i>.height</code> properties. * * @param win The window * @param name The window name */ public static void loadGeometry(Window win, String name) { int x, y, width, height; Dimension size = win.getSize(); GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); Rectangle gcbounds = gd.getDefaultConfiguration().getBounds(); x = gcbounds.x; y = gcbounds.y; width = jEdit.getIntegerProperty(name + ".width",size.width); height = jEdit.getIntegerProperty(name + ".height",size.height); Component parent = win.getParent(); if(parent == null) { x += (gcbounds.width - width) / 2; y += (gcbounds.height - height) / 2; } else { Rectangle bounds = parent.getBounds(); x += bounds.x + (bounds.width - width) / 2; y += bounds.y + (bounds.height - height) / 2; } x = jEdit.getIntegerProperty(name + ".x",x); y = jEdit.getIntegerProperty(name + ".y",y); int extState = jEdit.getIntegerProperty(name + ".extendedState", 0); Rectangle desired = new Rectangle(x,y,width,height); adjustForScreenBounds(desired); if(OperatingSystem.isX11() && Debug.GEOMETRY_WORKAROUND) new UnixWorkaround(win,name,desired,extState); else { win.setBounds(desired); if(win instanceof Frame) setExtendedState((Frame)win,extState); } } //}}} //{{{ adjustForScreenBounds() method /** * Gives a rectangle the specified bounds, ensuring it is within the * screen bounds. * @since jEdit 4.2pre3 */ public static void adjustForScreenBounds(Rectangle desired) { // Make sure the window is displayed in visible region Rectangle osbounds = OperatingSystem.getScreenBounds(desired); if(desired.x < osbounds.x || desired.x+desired.width > desired.x + osbounds.width) { if (desired.width > osbounds.width) desired.width = osbounds.width; desired.x = (osbounds.width - desired.width) / 2; } if(desired.y < osbounds.y || desired.y+desired.height > osbounds.y + osbounds.height) { if (desired.height >= osbounds.height) desired.height = osbounds.height; desired.y = (osbounds.height - desired.height) / 2; } } //}}} //{{{ UnixWorkaround class static class UnixWorkaround { Window win; String name; Rectangle desired; Rectangle required; long start; boolean windowOpened; //{{{ UnixWorkaround constructor UnixWorkaround(Window win, String name, Rectangle desired, int extState) { this.win = win; this.name = name; this.desired = desired; int adjust_x, adjust_y, adjust_width, adjust_height; adjust_x = jEdit.getIntegerProperty(name + ".dx",0); adjust_y = jEdit.getIntegerProperty(name + ".dy",0); adjust_width = jEdit.getIntegerProperty(name + ".d-width",0); adjust_height = jEdit.getIntegerProperty(name + ".d-height",0); required = new Rectangle( desired.x - adjust_x, desired.y - adjust_y, desired.width - adjust_width, desired.height - adjust_height); Log.log(Log.DEBUG,GUIUtilities.class,"Window " + name + ": desired geometry is " + desired); Log.log(Log.DEBUG,GUIUtilities.class,"Window " + name + ": setting geometry to " + required); start = System.currentTimeMillis(); win.setBounds(required); if(win instanceof Frame) setExtendedState((Frame)win,extState); win.addComponentListener(new ComponentHandler());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -