1 22 23 package org.gjt.sp.jedit; 24 25 27 import java.awt.Color ; 28 import java.awt.Component ; 29 import java.awt.Container ; 30 import java.awt.Dimension ; 31 import java.awt.Font ; 32 import java.awt.Frame ; 33 import java.awt.GraphicsConfiguration ; 34 import java.awt.GraphicsDevice ; 35 import java.awt.GraphicsEnvironment ; 36 import java.awt.Image ; 37 import java.awt.Point ; 38 import java.awt.Rectangle ; 39 import java.awt.Window ; 40 41 import java.awt.event.ComponentAdapter ; 42 import java.awt.event.ComponentEvent ; 43 import java.awt.event.MouseEvent ; 44 import java.awt.event.WindowAdapter ; 45 import java.awt.event.WindowEvent ; 46 import java.awt.event.WindowStateListener ; 47 48 import java.net.URL ; 49 50 import java.util.Hashtable ; 51 import java.util.Locale ; 52 import java.util.Map ; 53 import java.util.StringTokenizer ; 54 55 import javax.swing.*; 56 57 import org.gjt.sp.jedit.browser.VFSFileChooserDialog; 58 59 import org.gjt.sp.jedit.gui.EnhancedButton; 60 import org.gjt.sp.jedit.gui.FloatingWindowContainer; 61 import org.gjt.sp.jedit.gui.SplashScreen; 62 import org.gjt.sp.jedit.gui.VariableGridLayout; 63 64 import org.gjt.sp.jedit.menu.EnhancedCheckBoxMenuItem; 65 import org.gjt.sp.jedit.menu.EnhancedMenu; 66 import org.gjt.sp.jedit.menu.EnhancedMenuItem; 67 68 import org.gjt.sp.jedit.syntax.SyntaxStyle; 69 import org.gjt.sp.jedit.syntax.Token; 70 71 import org.gjt.sp.jedit.textarea.TextAreaMouseHandler; 72 73 import org.gjt.sp.util.Log; 74 76 95 public class GUIUtilities 96 { 97 102 public static Icon NEW_BUFFER_ICON; 103 104 108 public static Icon DIRTY_BUFFER_ICON; 109 110 114 public static Icon READ_ONLY_BUFFER_ICON; 115 116 120 public static Icon NORMAL_BUFFER_ICON; 121 122 126 public static Icon WINDOW_ICON; 127 129 131 136 public static void setIconPath(String iconPath) 137 { 138 GUIUtilities.iconPath = iconPath; 139 if(icons != null) 140 icons.clear(); 141 } 143 149 public static Icon loadIcon(String iconName) 150 { 151 if(icons == null) 152 icons = new Hashtable <String , Icon>(); 153 154 Icon icon = icons.get(iconName); 156 if(icon != null) 157 return icon; 158 159 URL url; 160 161 try 162 { 163 if(MiscUtilities.isURL(iconName)) 165 url = new URL (iconName); 166 else 167 url = new URL (iconPath + iconName); 168 } 169 catch(Exception e) 170 { 171 try 172 { 173 url = new URL (defaultIconPath + iconName); 174 } 175 catch(Exception ex) 176 { 177 Log.log(Log.ERROR,GUIUtilities.class, 178 "Icon not found: " + iconName); 179 Log.log(Log.ERROR,GUIUtilities.class,ex); 180 return null; 181 } 182 } 183 184 icon = new ImageIcon(url); 185 186 icons.put(iconName,icon); 187 return icon; 188 } 190 194 public static Image getEditorIcon() 195 { 196 return ((ImageIcon)loadIcon("jedit-icon.gif")).getImage(); 197 } 199 203 public static Image getPluginIcon() 204 { 205 return getEditorIcon(); 206 } 208 210 212 218 public static JMenuBar loadMenuBar(String name) 219 { 220 return loadMenuBar(jEdit.getActionContext(),name); 221 } 223 230 public static JMenuBar loadMenuBar(ActionContext context, String name) 231 { 232 String menus = jEdit.getProperty(name); 233 StringTokenizer st = new StringTokenizer (menus); 234 235 JMenuBar mbar = new JMenuBar(); 236 237 while(st.hasMoreTokens()) 238 { 239 String menuName = st.nextToken(); 240 mbar.add(loadMenu(context, menuName)); 241 } 242 243 return mbar; 244 } 246 257 public static JMenu loadMenu(String name) 258 { 259 return loadMenu(jEdit.getActionContext(),name); 260 } 262 276 public static JMenu loadMenu(ActionContext context, String name) 277 { 278 return new EnhancedMenu(name, 279 jEdit.getProperty(name.concat(".label")), 280 context); 281 } 283 290 public static JPopupMenu loadPopupMenu(String name) 291 { 292 return loadPopupMenu(jEdit.getActionContext(),name); 293 } 295 305 public static JPopupMenu loadPopupMenu(ActionContext context, String name) 306 { 307 JPopupMenu menu = new JPopupMenu(); 308 309 String menuItems = jEdit.getProperty(name); 310 if(menuItems != null) 311 { 312 StringTokenizer st = new StringTokenizer (menuItems); 313 while(st.hasMoreTokens()) 314 { 315 String menuItemName = st.nextToken(); 316 if(menuItemName.equals("-")) 317 menu.addSeparator(); 318 else 319 menu.add(loadMenuItem(context,menuItemName,false)); 320 } 321 } 322 323 return menu; 324 } 326 336 public static JMenuItem loadMenuItem(String name) 337 { 338 return loadMenuItem(jEdit.getActionContext(),name,true); 339 } 341 348 public static JMenuItem loadMenuItem(String name, boolean setMnemonic) 349 { 350 return loadMenuItem(jEdit.getActionContext(),name,setMnemonic); 351 } 353 363 public static JMenuItem loadMenuItem(ActionContext context, String name, 364 boolean setMnemonic) 365 { 366 if(name.charAt(0) == '%') 367 return loadMenu(context,name.substring(1)); 368 369 String label = jEdit.getProperty(name + ".label"); 370 if(label == null) 371 label = name; 372 373 char mnemonic; 374 int index = label.indexOf('$'); 375 if(index != -1 && label.length() - index > 1) 376 { 377 mnemonic = Character.toLowerCase(label.charAt(index + 1)); 378 label = label.substring(0,index).concat(label.substring(++index)); 379 } 380 else 381 mnemonic = '\0'; 382 383 JMenuItem mi; 384 if(jEdit.getBooleanProperty(name + ".toggle")) 385 mi = new EnhancedCheckBoxMenuItem(label,name,context); 386 else 387 mi = new EnhancedMenuItem(label,name,context); 388 389 if(!OperatingSystem.isMacOS() && setMnemonic && mnemonic != '\0') 390 mi.setMnemonic(mnemonic); 391 392 return mi; 393 } 395 public static JMenuItem loadMenuItem(EditAction editAction, 397 boolean setMnemonic) 398 { 399 String name = editAction.getName(); 400 ActionContext context = jEdit.getActionContext(); 401 402 String label = editAction.getLabel(); 404 if(label == null) 405 label = name; 406 407 char mnemonic; 408 int index = label.indexOf('$'); 409 if(index != -1 && label.length() - index > 1) 410 { 411 mnemonic = Character.toLowerCase(label.charAt(index + 1)); 412 label = label.substring(0,index).concat(label.substring(++index)); 413 } 414 else 415 mnemonic = '\0'; 416 417 JMenuItem mi; 418 if(jEdit.getBooleanProperty(name + ".toggle")) 419 mi = new EnhancedCheckBoxMenuItem(label,name,context); 420 else 421 mi = new EnhancedMenuItem(label,name,context); 422 423 if(!OperatingSystem.isMacOS() && setMnemonic && mnemonic != '\0') 424 mi.setMnemonic(mnemonic); 425 426 return mi; 427 } 429 435 public static Box loadToolBar(String name) 436 { 437 return loadToolBar(jEdit.getActionContext(),name); 438 } 440 449 public static Box loadToolBar(ActionContext context, String name) 450 { 451 Box toolBar = new Box(BoxLayout.X_AXIS); 452 453 String buttons = jEdit.getProperty(name); 454 if(buttons != null) 455 { 456 StringTokenizer st = new StringTokenizer (buttons); 457 while(st.hasMoreTokens()) 458 { 459 String button = st.nextToken(); 460 if(button.equals("-")) 461 toolBar.add(Box.createHorizontalStrut(12)); 462 else 463 { 464 JButton b = loadToolButton(context,button); 465 if(b != null) 466 toolBar.add(b); 467 } 468 } 469 } 470 471 toolBar.add(Box.createGlue()); 472 473 return toolBar; 474 } 476 485 public static EnhancedButton loadToolButton(String name) 486 { 487 return loadToolButton(jEdit.getActionContext(),name); 488 } 490 503 public static EnhancedButton loadToolButton(ActionContext context, 504 String name) 505 { 506 String label = jEdit.getProperty(name + ".label"); 507 508 if(label == null) 509 label = name; 510 511 Icon icon; 512 String iconName = jEdit.getProperty(name + ".icon"); 513 if(iconName == null) 514 icon = loadIcon("BrokenImage.png"); 515 else 516 { 517 icon = loadIcon(iconName); 518 if(icon == null) 519 icon = loadIcon("BrokenImage.png"); 520 } 521 522 String toolTip = prettifyMenuLabel(label); 523 String shortcut1 = jEdit.getProperty(name + ".shortcut"); 524 String shortcut2 = jEdit.getProperty(name + ".shortcut2"); 525 if(shortcut1 != null || shortcut2 != null) 526 { 527 toolTip = toolTip + " (" 528 + (shortcut1 != null 529 ? shortcut1 : "") 530 + ((shortcut1 != null && shortcut2 != null) 531 ? " or " : "") 532 + (shortcut2 != null 533 ? shortcut2 534 : "") + ')'; 535 } 536 537 return new EnhancedButton(icon,toolTip,name,context); 538 } 540 546 public static String prettifyMenuLabel(String label) 547 { 548 int index = label.indexOf('$'); 549 if(index != -1) 550 { 551 label = label.substring(0,index) 552 .concat(label.substring(index + 1)); 553 } 554 return label; 555 } 557 559 561 574 public static void message(Component comp, String name, Object [] args) 575 { 576 hideSplashScreen(); 577 578 JOptionPane.showMessageDialog(comp, 579 jEdit.getProperty(name.concat(".message"),args), 580 jEdit.getProperty(name.concat(".title"),args), 581 JOptionPane.INFORMATION_MESSAGE); 582 } 584 597 public static void error(Component comp, String name, Object [] args) 598 { 599 hideSplashScreen(); 600 601 JOptionPane.showMessageDialog(comp, 602 jEdit.getProperty(name.concat(".message"),args), 603 jEdit.getProperty(name.concat(".title"),args), 604 JOptionPane.ERROR_MESSAGE); 605 } 607 617 public static String input(Component comp, String name, Object def) 618 { 619 return input(comp,name,null,def); 620 } 622 632 public static String inputProperty(Component comp, String name, 633 String def) 634 { 635 return inputProperty(comp,name,null,def); 636 } 638 651 public static String input(Component comp, String name, 652 Object [] args, Object def) 653 { 654 hideSplashScreen(); 655 656 String retVal = (String )JOptionPane.showInputDialog(comp, 657 jEdit.getProperty(name.concat(".message"),args), 658 jEdit.getProperty(name.concat(".title")), 659 JOptionPane.QUESTION_MESSAGE,null,null,def); 660 return retVal; 661 } 663 676 public static String inputProperty(Component comp, String name, 677 Object [] args, String def) 678 { 679 hideSplashScreen(); 680 681 String retVal = (String )JOptionPane.showInputDialog(comp, 682 jEdit.getProperty(name.concat(".message"),args), 683 jEdit.getProperty(name.concat(".title")), 684 JOptionPane.QUESTION_MESSAGE, 685 null,null,jEdit.getProperty(def)); 686 if(retVal != null) 687 jEdit.setProperty(def,retVal); 688 return retVal; 689 } 691 707 public static int confirm(Component comp, String name, 708 Object [] args, int buttons, int type) 709 { 710 hideSplashScreen(); 711 712 return JOptionPane.showConfirmDialog(comp, 713 jEdit.getProperty(name + ".message",args), 714 jEdit.getProperty(name + ".title"),buttons,type); 715 } 717 727 public static int listConfirm(Component comp, String name, String [] args, 728 Object [] listModel) 729 { 730 JList list = new JList(listModel); 731 list.setVisibleRowCount(8); 732 733 Object [] message = { 734 jEdit.getProperty(name + ".message",args), 735 new JScrollPane(list) 736 }; 737 738 return JOptionPane.showConfirmDialog(comp, 739 message, 740 jEdit.getProperty(name + ".title"), 741 JOptionPane.YES_NO_OPTION, 742 JOptionPane.QUESTION_MESSAGE); 743 } 745 758 public static String [] showVFSFileDialog(View view, String path, 759 int type, boolean multipleSelection) 760 { 761 if(view == null) 763 { 764 Log.log(Log.WARNING,GUIUtilities.class, 765 "showVFSFileDialog(): given null view, assuming jEdit.getActiveView()"); 766 view = jEdit.getActiveView(); 767 } 768 769 hideSplashScreen(); 770 771 VFSFileChooserDialog fileChooser = new VFSFileChooserDialog( 772 view,path,type,multipleSelection); 773 String [] selectedFiles = fileChooser.getSelectedFiles(); 774 if(selectedFiles == null) 775 return null; 776 777 return selectedFiles; 778 } 780 782 784 792 public static Color parseColor(String name) 793 { 794 return parseColor(name, Color.black); 795 } 797 public static Color parseColor(String name, Color defaultColor) 799 { 800 if(name == null) 801 return defaultColor; 802 else if(name.charAt(0) == '#') 803 { 804 try 805 { 806 return Color.decode(name); 807 } 808 catch(NumberFormatException nf) 809 { 810 return defaultColor; 811 } 812 } 813 else if("red".equals(name)) 814 return Color.red; 815 else if("green".equals(name)) 816 return Color.green; 817 else if("blue".equals(name)) 818 return Color.blue; 819 else if("yellow".equals(name)) 820 return Color.yellow; 821 else if("orange".equals(name)) 822 return Color.orange; 823 else if("white".equals(name)) 824 return Color.white; 825 else if("lightGray".equals(name)) 826 return Color.lightGray; 827 else if("gray".equals(name)) 828 return Color.gray; 829 else if("darkGray".equals(name)) 830 return Color.darkGray; 831 else if("black".equals(name)) 832 return Color.black; 833 else if("cyan".equals(name)) 834 return Color.cyan; 835 else if("magenta".equals(name)) 836 return Color.magenta; 837 else if("pink".equals(name)) 838 return Color.pink; 839 else 840 return defaultColor; 841 } 843 849 public static String getColorHexString(Color c) 850 { 851 String colString = Integer.toHexString(c.getRGB() & 0xffffff); 852 return "#000000".substring(0,7 - colString.length()).concat(colString); 853 } 855 864 public static SyntaxStyle parseStyle(String str, String family, int size) 865 throws IllegalArgumentException 866 { 867 return parseStyle(str,family,size,true); 868 } 870 880 public static SyntaxStyle parseStyle(String str, String family, int size, 881 boolean color) 882 throws IllegalArgumentException 883 { 884 Color fgColor = Color.black; 885 Color bgColor = null; 886 boolean italic = false; 887 boolean bold = false; 888 StringTokenizer st = new StringTokenizer (str); 889 while(st.hasMoreTokens()) 890 { 891 String s = st.nextToken(); 892 if(s.startsWith("color:")) 893 { 894 if(color) 895 fgColor = GUIUtilities.parseColor(s.substring(6), Color.black); 896 } 897 else if(s.startsWith("bgColor:")) 898 { 899 if(color) 900 bgColor = GUIUtilities.parseColor(s.substring(8), null); 901 } 902 else if(s.startsWith("style:")) 903 { 904 for(int i = 6; i < s.length(); i++) 905 { 906 if(s.charAt(i) == 'i') 907 italic = true; 908 else if(s.charAt(i) == 'b') 909 bold = true; 910 else 911 throw new IllegalArgumentException ( 912 "Invalid style: " + s); 913 } 914 } 915 else 916 throw new IllegalArgumentException ( 917 "Invalid directive: " + s); 918 } 919 return new SyntaxStyle(fgColor,bgColor, 920 new Font (family, 921 (italic ? Font.ITALIC : 0) | (bold ? Font.BOLD : 0), 922 size)); 923 } 925 930 public static String getStyleString(SyntaxStyle style) 931 { 932 StringBuilder buf = new StringBuilder (); 933 934 if (style.getForegroundColor() != null) 935 { 936 buf.append("color:").append(getColorHexString(style.getForegroundColor())); 937 } 938 939 if (style.getBackgroundColor() != null) 940 { 941 buf.append(" bgColor:").append(getColorHexString(style.getBackgroundColor())); 942 } 943 944 Font font = style.getFont(); 945 if (!font.isPlain()) 946 { 947 buf.append(" style:"); 948 if (font.isItalic()) 949 buf.append('i'); 950 if (font.isBold()) 951 buf.append('b'); 952 } 953 954 return buf.toString(); 955 } 957 965 public static SyntaxStyle[] loadStyles(String family, int size) 966 { 967 return loadStyles(family,size,true); 968 } 970 979 public static SyntaxStyle[] loadStyles(String family, int size, boolean color) 980 { 981 SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT]; 982 983 for(int i = 1; i < styles.length; i++) 985 { 986 try 987 { 988 String styleName = "view.style." 989 + Token.tokenToString((byte)i) 990 .toLowerCase(Locale.ENGLISH); 991 styles[i] = GUIUtilities.parseStyle( 992 jEdit.getProperty(styleName), 993 family,size,color); 994 } 995 catch(Exception e) 996 { 997 Log.log(Log.ERROR,GUIUtilities.class,e); 998 } 999 } 1000 1001 return styles; 1002 } 1004 1006 1008 1019 public static void loadGeometry(Window win, Container parent, String name ) { 1020 int x, y, width, height; 1021 1022 Dimension size = win.getSize(); 1023 width = jEdit.getIntegerProperty(name + ".width", size.width); 1024 height = jEdit.getIntegerProperty(name + ".height", size.height); 1025 x = jEdit.getIntegerProperty(name + ".x",50); 1026 y = jEdit.getIntegerProperty(name + ".y",50); 1027 if(parent != null) 1028 { 1029 Point location = parent.getLocation(); 1030 x = location.x + x; 1031 y = location.y + y; 1032 } 1033 1034 int extState = jEdit.getIntegerProperty(name + ".extendedState", Frame.NORMAL); 1035 1036 Rectangle desired = new Rectangle (x,y,width,height); 1037 try 1038 { 1039 if(!Debug.DISABLE_MULTIHEAD) 1040 adjustForScreenBounds(desired); 1041 } 1042 catch(Exception e) 1043 { 1044 1045 Log.log(Log.ERROR,GUIUtilities.class,e); 1046 } 1047 1048 if(OperatingSystem.isX11() && Debug.GEOMETRY_WORKAROUND) 1049 new UnixWorkaround(win,name,desired,extState); 1050 else 1051 { 1052 win.setBounds(desired); 1053 if(win instanceof Frame ) 1054 ((Frame )win).setExtendedState(extState); 1055 } 1056 1057 } 1059 1069 public static void loadGeometry(Window win, String name) 1070 { 1071 loadGeometry(win, win.getParent(), name); 1072 } 1074 1080 public static void adjustForScreenBounds(Rectangle desired) 1081 { 1082 Rectangle osbounds = OperatingSystem.getScreenBounds(desired); 1084 1085 if(desired.x < osbounds.x || desired.x+desired.width 1086 > desired.x + osbounds.width) 1087 { 1088 if (desired.width > osbounds.width) 1089 desired.width = osbounds.width; 1090 desired.x = (osbounds.width - desired.width) / 2; 1091 } 1092 if(desired.y < osbounds.y || desired.y+desired.height 1093 > osbounds.y + osbounds.height) 1094 { 1095 if (desired.height >= osbounds.height) 1096 desired.height = osbounds.height; 1097 desired.y = (osbounds.height - desired.height) / 2; 1098 } 1099 } 1101 static class UnixWorkaround 1103 { 1104 Window win; 1105 String name; 1106 Rectangle desired; 1107 Rectangle required; 1108 long start; 1109 boolean windowOpened; 1110 1111 UnixWorkaround(Window win, String name, Rectangle desired, 1113 int extState) 1114 { 1115 this.win = win; 1116 this.name = name; 1117 this.desired = desired; 1118 1119 int adjust_x, adjust_y, adjust_width, adjust_height; 1120 adjust_x = jEdit.getIntegerProperty(name + ".dx",0); 1121 adjust_y = jEdit.getIntegerProperty(name + ".dy",0); 1122 adjust_width = jEdit.getIntegerProperty(name + ".d-width",0); 1123 adjust_height = jEdit.getIntegerProperty(name + ".d-height",0); 1124 1125 required = new Rectangle ( 1126 desired.x - adjust_x, 1127 desired.y - adjust_y, 1128 desired.width - adjust_width, 1129 desired.height - adjust_height); 1130 1131 Log.log(Log.DEBUG,GUIUtilities.class,"Window " + name 1132 + ": desired geometry is " + desired); 1133 Log.log(Log.DEBUG,GUIUtilities.class,"Window " + name 1134 + ": setting geometry to " + required); 1135 1136 start = System.currentTimeMillis(); 1137 1138 win.setBounds(required); 1139 if(win instanceof Frame ) 1140 ((Frame )win).setExtendedState(extState); 1141 1142 win.addComponentListener(new ComponentHandler()); 1143 win.addWindowListener(new WindowHandler()); 1144 } 1146 class ComponentHandler extends ComponentAdapter 1148 { 1149 public void componentMoved(ComponentEvent evt) 1151 { 1152 if(System.currentTimeMillis() - start < 1000L) 1153 { 1154 Rectangle r = win.getBounds(); 1155 if(!windowOpened && r.equals(required)) 1156 return; 1157 1158 if(!r.equals(desired)) 1159 { 1160 Log.log(Log.DEBUG,GUIUtilities.class, 1161 "Window resize blocked: " + win.getBounds()); 1162 win.setBounds(desired); 1163 } 1164 } 1165 1166 win.removeComponentListener(this); 1167 } 1169 public void componentResized(ComponentEvent evt) 1171 { 1172 if(System.currentTimeMillis() - start < 1000L) 1173 { 1174 Rectangle r = win.getBounds(); 1175 if(!windowOpened && r.equals(required)) 1176 return; 1177 1178 if(!r.equals(desired)) 1179 { 1180 Log.log(Log.DEBUG,GUIUtilities.class, 1181 "Window resize blocked: " + win.getBounds()); 1182 win.setBounds(desired); 1183 } 1184 } 1185 1186 win.removeComponentListener(this); 1187 } } 1190 class WindowHandler extends WindowAdapter 1192 { 1193 public void windowOpened(WindowEvent evt) 1195 { 1196 windowOpened = true; 1197 1198 Rectangle r = win.getBounds(); 1199 Log.log(Log.DEBUG,GUIUtilities.class,"Window " 1200 + name + ": bounds after opening: " + r); 1201 1202 jEdit.setIntegerProperty(name + ".dx", 1203 r.x - required.x); 1204 jEdit.setIntegerProperty(name + ".dy", 1205 r.y - required.y); 1206 jEdit.setIntegerProperty(name + ".d-width", 1207 r.width - required.width); 1208 jEdit.setIntegerProperty(name + ".d-height", 1209 r.height - required.height); 1210 1211 win.removeWindowListener(this); 1212 } } } 1216 1228 public static void saveGeometry(Window win, String name) { 1229 saveGeometry (win, win.getParent(), name); 1230 } 1232 1245 public static void saveGeometry(Window win, Container parent, String name) 1246 { 1247 if(win instanceof Frame ) 1248 { 1249 jEdit.setIntegerProperty(name + ".extendedState", 1250 ((Frame )win).getExtendedState()); 1251 } 1252 1253 Rectangle bounds = win.getBounds(); 1254 int x = bounds.x; 1255 int y = bounds.y; 1256 if (parent != null) 1257 { 1258 Rectangle parentBounds = parent.getBounds(); 1259 x = x - parentBounds.x; 1260 y = y - parentBounds.y; 1261 } 1262 jEdit.setIntegerProperty(name + ".x",x); 1263 jEdit.setIntegerProperty(name + ".y",y); 1264 jEdit.setIntegerProperty(name + ".width", bounds.width); 1265 jEdit.setIntegerProperty(name + ".height", bounds.height); 1266 } 1268 1275 public static void centerOnScreen(Window win) 1276 { 1277 GraphicsDevice gd = GraphicsEnvironment 1278 .getLocalGraphicsEnvironment() 1279 .getDefaultScreenDevice(); 1280 Rectangle gcbounds = gd.getDefaultConfiguration().getBounds(); 1281 int x = gcbounds.x + (gcbounds.width - win.getWidth()) / 2; 1282 int y = gcbounds.y + (gcbounds.height - win.getHeight()) / 2; 1283 win.setLocation(x,y); 1284 } 1286 1288 1294 public static void hideSplashScreen() 1295 { 1296 if(splash != null) 1297 { 1298 splash.dispose(); 1299 splash = null; 1300 } 1301 } 1303 1312 public static JComponent createMultilineLabel(String str) 1313 { 1314 JPanel panel = new JPanel(new VariableGridLayout( 1315 VariableGridLayout.FIXED_NUM_COLUMNS,1,1,1)); 1316 int lastOffset = 0; 1317 while(true) 1318 { 1319 int index = str.indexOf('\n',lastOffset); 1320 if(index == -1) 1321 break; 1322 else 1323 { 1324 panel.add(new JLabel(str.substring(lastOffset,index))); 1325 lastOffset = index + 1; 1326 } 1327 } 1328 1329 if(lastOffset != str.length()) 1330 panel.add(new JLabel(str.substring(lastOffset))); 1331 1332 return panel; 1333 } 1335 1342 public static void requestFocus(final Window win, final Component comp) 1343 { 1344 win.addWindowFocusListener(new WindowAdapter () 1345 { 1346 public void windowGainedFocus(WindowEvent evt) 1347 { 1348 SwingUtilities.invokeLater(new Runnable (){ 1349 public void run() { 1350 comp.requestFocusInWindow(); 1351 } 1352 }); 1353 win.removeWindowFocusListener(this); 1354 } 1355 }); 1356 } 1358 1366 public static boolean isPopupTrigger(MouseEvent evt) 1367 { 1368 return TextAreaMouseHandler.isRightButton(evt.getModifiers()); 1369 } 1371 1376 public static boolean isMiddleButton(int modifiers) 1377 { 1378 return TextAreaMouseHandler.isMiddleButton(modifiers); 1379 } 1381 1386 public static boolean isRightButton(int modifiers) 1387 { 1388 return TextAreaMouseHandler.isRightButton(modifiers); 1389 } 1391 1401 public static void showPopupMenu(JPopupMenu popup, Component comp, 1402 int x, int y) 1403 { 1404 showPopupMenu(popup,comp,x,y,true); 1405 } 1407 1421 public static void showPopupMenu(JPopupMenu popup, Component comp, 1422 int x, int y, boolean point) 1423 { 1424 int offsetX = 0; 1425 int offsetY = 0; 1426 1427 int extraOffset = (point ? 1 : 0); 1428 1429 Component win = comp; 1430 while(!(win instanceof Window || win == null)) 1431 { 1432 offsetX += win.getX(); 1433 offsetY += win.getY(); 1434 win = win.getParent(); 1435 } 1436 1437 if(win != null) 1438 { 1439 Dimension size = popup.getPreferredSize(); 1440 1441 Rectangle screenSize = new Rectangle (); 1442 1443 GraphicsEnvironment ge = GraphicsEnvironment 1444 .getLocalGraphicsEnvironment(); 1445 1446 GraphicsDevice [] devices = ge.getScreenDevices(); 1447 1448 for (int j = 0; j < devices.length; j++) 1449 { 1450 GraphicsDevice device = devices[j]; 1451 1452 GraphicsConfiguration [] gc = 1453 device.getConfigurations(); 1454 1455 for (int i=0; i < gc.length; i++) 1456 { 1457 screenSize = 1458 screenSize.union( 1459 gc[i].getBounds()); 1460 } 1461 } 1462 1463 if(x + offsetX + size.width + win.getX() > screenSize.width 1464 && x + offsetX + win.getX() >= size.width) 1465 { 1466 if(point) 1468 x -= (size.width + extraOffset); 1469 else 1470 x = (win.getWidth() - size.width - offsetX + extraOffset); 1471 } 1472 else 1473 { 1474 x += extraOffset; 1475 } 1476 1477 if(y + offsetY + size.height + win.getY() > screenSize.height 1481 && y + offsetY + win.getY() >= size.height) 1482 { 1483 if(point) 1484 y = (win.getHeight() - size.height - offsetY + extraOffset); 1485 else 1486 y = -size.height - 1; 1487 } 1488 else 1489 { 1490 y += extraOffset; 1491 } 1492 1493 popup.show(comp,x,y); 1494 } 1495 else 1496 popup.show(comp,x + extraOffset,y + extraOffset); 1497 1498 } 1500 1509 public static boolean isAncestorOf(Component comp1, Component comp2) 1510 { 1511 while(comp2 != null) 1512 { 1513 if(comp1 == comp2) 1514 return true; 1515 else 1516 comp2 = comp2.getParent(); 1517 } 1518 1519 return false; 1520 } 1522 1528 public static JDialog getParentDialog(Component c) 1529 { 1530 return (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, c); 1531 } 1533 1541 public static Component getComponentParent(Component comp, Class clazz) 1542 { 1543 while(true) 1544 { 1545 if(comp == null) 1546 break; 1547 1548 if(comp instanceof JComponent) 1549 { 1550 Component real = (Component )((JComponent)comp) 1551 .getClientProperty("KORTE_REAL_FRAME"); 1552 if(real != null) 1553 comp = real; 1554 } 1555 1556 if(comp.getClass().equals(clazz)) 1557 return comp; 1558 else if(comp instanceof JPopupMenu) 1559 comp = ((JPopupMenu)comp).getInvoker(); 1560 else if(comp instanceof FloatingWindowContainer) 1561 { 1562 comp = ((FloatingWindowContainer)comp) 1563 .getDockableWindowManager(); 1564 } 1565 else 1566 comp = comp.getParent(); 1567 } 1568 return null; 1569 } 1571 1576 public static View getView(Component comp) 1577 { 1578 return (View)getComponentParent(comp,View.class); 1579 } 1581 1590 public static void addSizeSaver(Frame frame, String name) 1591 { 1592 addSizeSaver(frame,frame.getParent(),name); 1593 } 1595 1605 public static void addSizeSaver(Frame frame, Container parent, String name) 1606 { 1607 SizeSaver ss = new SizeSaver(frame,parent,name); 1608 frame.addWindowStateListener(ss); 1609 frame.addComponentListener(ss); 1610 } 1612 1620 public static void initContinuousLayout(JSplitPane split) 1621 { 1622 boolean continuousLayout = split.isContinuousLayout(); 1623 if (continuousLayout != jEdit.getBooleanProperty("appearance.continuousLayout")) 1624 split.setContinuousLayout(!continuousLayout); 1625 } 1627 1629 static void init() 1631 { 1632 NEW_BUFFER_ICON = loadIcon("new.gif"); 1635 DIRTY_BUFFER_ICON = loadIcon("dirty.gif"); 1636 READ_ONLY_BUFFER_ICON = loadIcon("readonly.gif"); 1637 NORMAL_BUFFER_ICON = loadIcon("normal.gif"); 1638 WINDOW_ICON = loadIcon("jedit-icon.gif"); 1639 } 1641 static void showSplashScreen() 1643 { 1644 splash = new SplashScreen(); 1645 } 1647 static void advanceSplashProgress() 1649 { 1650 if(splash != null) 1651 splash.advance(); 1652 } 1654 static void advanceSplashProgress(String label) 1656 { 1657 if(splash != null) 1658 splash.advance(label); 1659 } 1661 1663 private static SplashScreen splash; 1665 private static Map <String , Icon> icons; 1666 private static String iconPath = "jeditresource:/org/gjt/sp/jedit/icons/"; 1667 private static String defaultIconPath = "jeditresource:/org/gjt/sp/jedit/icons/"; 1668 1669 private GUIUtilities() {} 1670 1672 1674 1684 static class SizeSaver extends ComponentAdapter implements WindowStateListener 1685 { 1686 private Frame frame; 1687 private Container parent; 1688 private String name; 1689 1690 1697 SizeSaver(Frame frame, String name) 1698 { 1699 this.frame = frame; 1700 this.parent = frame.getParent(); 1701 } 1703 1711 public SizeSaver(Frame frame, Container parent, String name) 1712 { 1713 if ((null == frame) || (null == name)) 1714 { 1715 throw new NullPointerException (); 1716 } 1717 this.frame = frame; 1718 this.parent = parent; 1719 this.name = name; 1720 } 1722 public void windowStateChanged(WindowEvent wse) 1724 { 1725 int extendedState = wse.getNewState(); 1726 jEdit.setIntegerProperty(name + ".extendedState",extendedState); 1727 Rectangle bounds = frame.getBounds(); 1728 save(extendedState, bounds); 1729 } 1731 private void save(int extendedState, Rectangle bounds) 1733 { 1734 switch (extendedState) 1735 { 1736 case Frame.MAXIMIZED_VERT: 1737 jEdit.setIntegerProperty(name + ".x",bounds.x); 1738 jEdit.setIntegerProperty(name + ".width",bounds.width); 1739 break; 1740 1741 case Frame.MAXIMIZED_HORIZ: 1742 jEdit.setIntegerProperty(name + ".y",bounds.y); 1743 jEdit.setIntegerProperty(name + ".height",bounds.height); 1744 break; 1745 1746 case Frame.NORMAL: 1747 saveGeometry(frame,parent,name ); 1748 break; 1749 } 1750 } 1752 public void componentResized(ComponentEvent ce) 1754 { 1755 componentMoved(ce); 1756 } 1758 public void componentMoved(ComponentEvent ce) 1760 { 1761 final Rectangle bounds = frame.getBounds(); 1762 final Runnable sizeSaver = new Runnable () 1763 { 1764 public void run() 1765 { 1766 int extendedState = frame.getExtendedState(); 1767 save(extendedState, bounds); 1768 } 1769 }; 1770 new Thread ("Sizesavingdelay") 1771 { 1772 public void run() 1773 { 1774 try 1775 { 1776 Thread.sleep(500L); 1777 } 1778 catch (InterruptedException ie) 1779 { 1780 } 1781 SwingUtilities.invokeLater(sizeSaver); 1782 } 1783 }.start(); 1784 } } 1787 } 1789 | Popular Tags |