1 11 package org.eclipse.ui.carbon; 12 13 import java.text.MessageFormat ; 14 import java.util.MissingResourceException ; 15 import java.util.ResourceBundle ; 16 17 import org.eclipse.core.runtime.IProduct; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.jface.action.ActionContributionItem; 20 import org.eclipse.jface.action.IAction; 21 import org.eclipse.swt.internal.Callback; 22 import org.eclipse.swt.internal.carbon.HICommand; 23 import org.eclipse.swt.internal.carbon.OS; 24 import org.eclipse.swt.widgets.Display; 25 import org.eclipse.swt.widgets.Menu; 26 import org.eclipse.swt.widgets.MenuItem; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.swt.widgets.Widget; 29 import org.eclipse.ui.IStartup; 30 import org.eclipse.ui.IWindowListener; 31 import org.eclipse.ui.IWorkbenchWindow; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.internal.WorkbenchWindow; 34 35 43 public class CarbonUIEnhancer implements IStartup { 44 45 private static final int kHICommandPreferences = ('p' << 24) + ('r' << 16) + ('e' << 8) + 'f'; 46 private static final int kHICommandAbout = ('a' << 24) + ('b' << 16) + ('o' << 8) + 'u'; 47 private static final int kHICommandServices = ('s' << 24) + ('e' << 16) + ('r' << 8) + 'v'; 48 49 private static final String RESOURCE_BUNDLE = "org.eclipse.ui.carbon.Messages"; private static final String TOOLBAR_BUTTON_TOGGLE_FLAGS = "toolbarButtonToggleFlags"; 52 private String fAboutActionName; 53 54 57 public CarbonUIEnhancer() { 58 IProduct product = Platform.getProduct(); 59 String productName = null; 60 if (product != null) 61 productName = product.getName(); 62 63 ResourceBundle resourceBundle = ResourceBundle.getBundle(RESOURCE_BUNDLE); 64 try { 65 if (productName != null) { 66 String format = resourceBundle.getString("AboutAction.format"); if (format != null) 68 fAboutActionName= MessageFormat.format(format, new Object [] { productName } ); 69 } 70 if (fAboutActionName == null) 71 fAboutActionName = resourceBundle.getString("AboutAction.name"); } catch (MissingResourceException e) { 73 } 74 75 if (fAboutActionName == null) 76 fAboutActionName = "About"; } 78 79 82 public void earlyStartup() { 83 final Display display = Display.getDefault(); 84 display.syncExec(new Runnable () { 85 public void run() { 86 hookApplicationMenu(display); 87 hookToolbarButtonCallback(); 88 hookWorkbenchListener(); 89 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 91 .getWorkbenchWindows(); 92 for (int i = 0; i < windows.length; i++) { 93 modifyWindowShell(windows[i]); 94 } 95 } 96 }); 97 } 98 99 105 protected void hookWorkbenchListener() { 106 PlatformUI.getWorkbench().addWindowListener(new IWindowListener() { 107 108 public void windowActivated(IWorkbenchWindow window) { 109 } 111 112 public void windowDeactivated(IWorkbenchWindow window) { 113 } 115 116 public void windowClosed(IWorkbenchWindow window) { 117 } 119 120 public void windowOpened(IWorkbenchWindow window) { 121 modifyWindowShell(window); 122 }}); 123 } 124 125 133 protected void modifyWindowShell(IWorkbenchWindow window) { 134 boolean coolBarInitiallyVsible = ((WorkbenchWindow) window) 139 .getCoolBarVisible(); 140 boolean perspectiveBarInitiallyVsible = ((WorkbenchWindow) window) 141 .getPerspectiveBarVisible(); 142 143 if (coolBarInitiallyVsible || perspectiveBarInitiallyVsible) { 144 Shell shell = window.getShell(); 147 boolean[] switchArray = new boolean[] { coolBarInitiallyVsible, 148 perspectiveBarInitiallyVsible }; 149 shell.setData(TOOLBAR_BUTTON_TOGGLE_FLAGS, switchArray); 150 int windowHandle = OS.GetControlOwner(shell.handle); 151 OS.ChangeWindowAttributes(windowHandle, 152 OS.kWindowToolbarButtonAttribute, 0); 153 } 154 } 155 156 162 protected void hookToolbarButtonCallback() { 163 Object target = new Object () { 164 int toolbarProc (int nextHandler, int theEvent, int userData) { 165 int eventKind = OS.GetEventKind (theEvent); 166 if (eventKind != OS.kEventWindowToolbarSwitchMode) 167 return OS.eventNotHandledErr; 168 169 int [] theWindow = new int [1]; 170 OS.GetEventParameter (theEvent, OS.kEventParamDirectObject, OS.typeWindowRef, null, 4, null, theWindow); 171 172 int [] theRoot = new int [1]; 173 OS.GetRootControl (theWindow [0], theRoot); 174 Widget widget = Display.getCurrent().findWidget(theRoot [0]); 175 176 if (!(widget instanceof Shell)) { 177 return OS.eventNotHandledErr; 178 } 179 Shell shell = (Shell) widget; 180 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 181 .getWorkbenchWindows(); 182 for (int i = 0; i < windows.length; i++) { 183 if (windows[i].getShell() == shell) { 184 WorkbenchWindow castedWindow = (WorkbenchWindow) windows[i]; 185 boolean[] switchFlags = (boolean[]) shell 188 .getData(TOOLBAR_BUTTON_TOGGLE_FLAGS); 189 if (switchFlags == null) 190 return OS.eventNotHandledErr; 191 boolean coolbarVisible = castedWindow 192 .getCoolBarVisible(); 193 boolean perspectivebarVisible = castedWindow 194 .getPerspectiveBarVisible(); 195 if (switchFlags[0]) 198 castedWindow.setCoolBarVisible(!coolbarVisible); 199 if (switchFlags[1]) 200 castedWindow 201 .setPerspectiveBarVisible(!perspectivebarVisible); 202 shell.layout(); 203 return OS.noErr; 204 } 205 } 206 return OS.eventNotHandledErr; 207 } 208 209 }; 210 211 final Callback commandCallback = new Callback(target, "toolbarProc", 3); int commandProc = commandCallback.getAddress(); 213 if (commandProc == 0) { 214 commandCallback.dispose(); 215 return; } 217 218 int[] mask = new int[] { OS.kEventClassWindow, OS.kEventWindowToolbarSwitchMode }; 219 OS.InstallEventHandler(OS.GetApplicationEventTarget(), commandProc, 220 mask.length / 2, mask, 0, null); 221 222 } 223 224 227 private void hookApplicationMenu(Display display) { 228 229 Object target = new Object () { 231 int commandProc(int nextHandler, int theEvent, int userData) { 232 if (OS.GetEventKind(theEvent) == OS.kEventProcessCommand) { 233 HICommand command = new HICommand(); 234 OS.GetEventParameter(theEvent, OS.kEventParamDirectObject, 235 OS.typeHICommand, null, HICommand.sizeof, null, command); 236 switch (command.commandID) { 237 case kHICommandPreferences: 238 return runAction("preferences"); case kHICommandAbout: 240 return runAction("about"); default: 242 break; 243 } 244 } 245 return OS.eventNotHandledErr; 246 } 247 }; 248 249 final Callback commandCallback = new Callback(target, "commandProc", 3); int commandProc = commandCallback.getAddress(); 251 if (commandProc == 0) { 252 commandCallback.dispose(); 253 return; } 255 256 int[] mask = new int[] { OS.kEventClassCommand, OS.kEventProcessCommand }; 258 OS.InstallEventHandler(OS.GetApplicationEventTarget(), commandProc, 259 mask.length / 2, mask, 0, null); 260 261 int[] outMenu = new int[1]; 263 short[] outIndex = new short[1]; 264 if (OS.GetIndMenuItemWithCommandID(0, kHICommandPreferences, 1, outMenu, outIndex) == OS.noErr 265 && outMenu[0] != 0) { 266 int menu = outMenu[0]; 267 268 int l = fAboutActionName.length(); 269 char buffer[] = new char[l]; 270 fAboutActionName.getChars(0, l, buffer, 0); 271 int str = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l); 272 OS.InsertMenuItemTextWithCFString(menu, str, (short) 0, 0, kHICommandAbout); 273 OS.CFRelease(str); 274 275 OS.InsertMenuItemTextWithCFString(menu, 0, (short) 1, OS.kMenuItemAttrSeparator, 0); 277 278 OS.EnableMenuCommand(menu, kHICommandPreferences); 280 281 OS.DisableMenuCommand(menu, kHICommandServices); 283 } 284 285 display.disposeExec(new Runnable () { 287 public void run() { 288 commandCallback.dispose(); 289 } 290 }); 291 } 292 293 296 private int runAction(String actionId) { 297 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 298 if (window != null) { 299 Shell shell = window.getShell(); 300 Menu menubar = shell.getMenuBar(); 301 if (menubar != null) { 302 for (int i = 0; i < menubar.getItemCount(); i++) { 303 MenuItem mi = menubar.getItem(i); 304 Menu m = mi.getMenu(); 305 for (int j = 0; j < m.getItemCount(); j++) { 306 MenuItem mi2 = m.getItem(j); 307 Object o = mi2.getData(); 308 if (o instanceof ActionContributionItem) { 309 ActionContributionItem aci = (ActionContributionItem) o; 310 String id = aci.getId(); 311 if (id != null && id.equals(actionId)) { 312 IAction action = aci.getAction(); 313 if (action != null && action.isEnabled()) { 314 action.run(); 315 return OS.noErr; 316 } 317 } 318 } 319 } 320 } 321 } 322 } 323 return OS.eventNotHandledErr; 324 } 325 } 326 | Popular Tags |