📄 guiutilities.java
字号:
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; } /** * 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); } /** * 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 { 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:")) { fgColor = GUIUtilities.parseColor(s.substring(6), Color.black); } else if(s.startsWith("bgColor:")) { 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)); } /** * Converts a style into it's string representation. * @param style The style */ public static String getStyleString(SyntaxStyle style) { StringBuffer buf = new StringBuffer(); 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(); } /** * 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) { SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT]; try { styles[Token.COMMENT1] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.comment1"), family,size); styles[Token.COMMENT2] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.comment2"), family, size); styles[Token.LITERAL1] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.literal1"), family,size); styles[Token.LITERAL2] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.literal2"), family,size); styles[Token.LABEL] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.label"), family,size); styles[Token.KEYWORD1] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.keyword1"), family,size); styles[Token.KEYWORD2] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.keyword2"), family,size); styles[Token.KEYWORD3] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.keyword3"), family,size); styles[Token.FUNCTION] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.function"), family,size); styles[Token.MARKUP] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.markup"), family,size); styles[Token.OPERATOR] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.operator"), family,size); styles[Token.DIGIT] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.digit"), family,size); styles[Token.INVALID] = GUIUtilities.parseStyle( jEdit.getProperty("view.style.invalid"), family,size); } catch(Exception e) { Log.log(Log.ERROR,GUIUtilities.class,e); } return styles; } /** * 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) { // all this adjust_* crap is there to work around buggy // Unix Java versions which don't put windows where you // tell them to int x, y, width, height, adjust_x, adjust_y, adjust_width, adjust_height; try { width = Integer.parseInt(jEdit.getProperty(name + ".width")); height = Integer.parseInt(jEdit.getProperty(name + ".height")); } catch(NumberFormatException nf) { Dimension size = win.getSize(); width = size.width; height = size.height; } try { x = Integer.parseInt(jEdit.getProperty(name + ".x")); y = Integer.parseInt(jEdit.getProperty(name + ".y")); } catch(NumberFormatException nf) { Component parent = win.getParent(); if(parent == null) { Dimension screen = win.getToolkit().getScreenSize(); x = (screen.width - width) / 2; y = (screen.height - height) / 2; } else { Rectangle bounds = parent.getBounds(); x = bounds.x + (bounds.width - width) / 2; y = bounds.y + (bounds.height - height) / 2; } } try { adjust_x = Integer.parseInt(jEdit.getProperty(name + ".dx")); adjust_y = Integer.parseInt(jEdit.getProperty(name + ".dy")); adjust_width = Integer.parseInt(jEdit.getProperty(name + ".d-width")); adjust_height = Integer.parseInt(jEdit.getProperty(name + ".d-height")); } catch(NumberFormatException nf) { adjust_x = adjust_y = 0; adjust_width = adjust_height = 0; } Rectangle desired = new Rectangle(x,y,width,height); Rectangle required = new Rectangle(x - adjust_x, y - adjust_y,width - adjust_width, 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); win.setBounds(required); if(File.separatorChar == '/' && System.getProperty("java.version").compareTo("1.2") < 0) { win.setBounds(required); new UnixWorkaround(win,name,desired,required); } else win.setBounds(desired); } static class UnixWorkaround { Window win; String name; Rectangle desired; Rectangle required; long start; boolean windowOpened; UnixWorkaround(Window win, String name, Rectangle desired, Rectangle required) { this.win = win; this.name = name; this.desired = desired; this.required = required; start = System.currentTimeMillis(); win.addComponentListener(new ComponentHandler()); win.addWindowListener(new WindowHandler()); } class ComponentHandler extends ComponentAdapter { public void componentMoved(ComponentEvent evt) { if(System.currentTimeMillis() - start < 1000) { Rectangle r = win.getBounds(); if(!windowOpened && r.equals(required)) return; if(!r.equals(desired)) {// Log.log(Log.DEBUG,GUIUtilities.class,// "Window resize blocked: " + win.getBounds()); win.setBounds(desired); } } else win.removeComponentListener(this); } public void componentResized(ComponentEvent evt) { if(System.currentTimeMillis() - start < 1000) { Rectangle r = win.getBounds(); if(!windowOpened && r.equals(required)) return; if(!r.equals(desired)) {// Log.log(Log.DEBUG,GUIUtilities.class,// "Window resize blocked: " + win.getBounds()); win.setBounds(desired); } } else win.removeComponentListener(this); } } class WindowHandler extends WindowAdapter { public void windowOpened(WindowEvent evt) { windowOpened = true; Rectangle r = win.getBounds();// Log.log(Log.DEBUG,GUIUtilities.class,"Window "// + name + ": bounds after opening: " + r); if(r.x != desired.x || r.y != desired.y || r.width != desired.width || r.height != desired.height) { jEdit.setProperty(name + ".dx",String.valueOf( r.x - required.x)); jEdit.setProperty(name + ".dy",String.valueOf( r.y - required.y)); jEdit.setProperty(name + ".d-width",String.valueOf( r.width - required.width)); jEdit.setProperty(name + ".d-height",String.valueOf( r.height - required.height)); } win.removeWindowListener(this); } } } /** * Saves a window's geometry to the properties. * The geometry is saved to 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 saveGeometry(Window win, String name) { Rectangle bounds = win.getBounds(); jEdit.setProperty(name + ".x",String.valueOf(bounds.x)); jEdit.setProperty(name + ".y",String.valueOf(bounds.y)); jEdit.setProperty(name + ".width",String.valueOf(bounds.width)); jEdit.setProperty(name + ".height",String.valueOf(bounds.height)); } /** * Ensures that the splash screen is not visible. This should be * called before displaying any dialog boxes or windows at * startup. */ public static void hideSplashScreen() { if(splash != null) { splash.dispose(); splash = null; } } /** * Returns the default editor window image. */ public static Image getEditorIcon() { return ((ImageIcon)EDITOR_WINDOW_ICON).getImage(); } /** * Returns the default plugin window image. */ public static Image getPluginIcon() { return ((ImageIcon)PLUGIN_WINDOW_ICON).getImage(); } /** * Focuses on the specified component as soon as the window becomes * active. * @param win The window * @param comp The component */ public static void requestFocus(final Window win, final Component comp) { win.addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent evt) { comp.requestFocus(); win.removeWindowListener(this); } }); } /** * Returns if the specified event is the popup trigger event. * This implements precisely defined behavior, as opposed to * MouseEvent.isPopupTrigger(). * @param evt The event * @since jEdit 3.2pre8 */ public static boolean isPopupTrigger(MouseEvent evt) { if(macOS) return evt.isControlDown(); else return ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0); } // deprecated APIs /** * @deprecated Use loadMenu(name) instead */ public static JMenu loadMenu(View view, String name) { return loadMenu(name); } /** * @deprecated Use loadMenuItem(name) instead * @param view Unused * @param name The menu item name */ public static JMenuItem loadMenuItem(View view, String name) { return loadMenuItem(name,true); } /** * @deprecated Use loadToolBarIcon() instead */ public static Icon loadToolBarIcon(String iconName) { return loadIcon(iconName); } /** * @deprecated Use showVFSFileDialog() */ public static String showFileDialog(View view, String file, int type) { if(file == null) file = System.getProperty("user.dir"); File _file = new File(file); JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(_file); if(_file.isDirectory()) chooser.setSelectedFile(null); else chooser.setSelectedFile(_file); chooser.setDialogType(type); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int retVal = chooser.showDialog(view,null); if(retVal == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); if(selectedFile != null) return selectedFile.getAbsolutePath(); } return null; } // package-private members static void showSplashScreen() { splash = new SplashScreen(); } static void advanceSplashProgress() { if(splash != null) splash.advance(); } // private members private static SplashScreen splash; private static boolean macOS; private static Hashtable icons; private GUIUtilities() {} static { macOS = (System.getProperty("os.name").indexOf("Mac") != -1); icons = new Hashtable(); NEW_BUFFER_ICON = loadIcon("new.gif"); DIRTY_BUFFER_ICON = loadIcon("dirty.gif"); READ_ONLY_BUFFER_ICON = loadIcon("readonly.gif"); NORMAL_BUFFER_ICON = loadIcon("normal.gif"); EDITOR_WINDOW_ICON = loadIcon("jedit_icon1.gif"); PLUGIN_WINDOW_ICON = loadIcon("jedit_icon2.gif"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -