FileDocCategorySizeDatePackage
CarbonUIEnhancer.javaAPI DocAzureus 3.0.3.420618Mon Jun 18 19:38:00 BST 2007org.gudy.azureus2.ui.swt.osx

CarbonUIEnhancer

public class CarbonUIEnhancer extends Object
Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html * Contributors: IBM Corporation - initial API and implementation Aelitis - Adaptation for Azureus

Fields Summary
private static final int
kHICommandPreferences
private static final int
kHICommandAbout
private static final int
kHICommandServices
private static final int
kHICommandWizard
private static final int
kHICommandNatTest
private static final int
kHICommandSpeedTest
private static final int
kHICommandRestart
private static final int
typeAEList
private static final int
kCoreEventClass
private static final int
kAEOpenDocuments
private static final int
kAEReopenApplication
private static final int
kAEOpenContents
private static final int
kURLEventClass
private static final int
typeText
private static final String
RESOURCE_BUNDLE
private static String
fgAboutActionName
private static String
fgWizardActionName
private static String
fgNatTestActionName
private static String
fgRestartActionName
private static String
fgSpeedTestActionName
private static int
memmove_type
static final Object
target
Constructors Summary
public CarbonUIEnhancer()


     
      if (fgAboutActionName == null) {
         fgAboutActionName = MessageText.getString("MainWindow.menu.help.about").replaceAll("&", "");
      }
      if(fgWizardActionName == null) {
          fgWizardActionName = MessageText.getString("MainWindow.menu.file.configure").replaceAll("&", "");
      }
      if(fgNatTestActionName == null) {
      	fgNatTestActionName = MessageText.getString("MainWindow.menu.tools.nattest").replaceAll("&", "");
      }
      if(fgRestartActionName == null) {
          fgRestartActionName = MessageText.getString("MainWindow.menu.file.restart").replaceAll("&", "");
      }
      if(fgSpeedTestActionName == null){
          fgSpeedTestActionName = MessageText.getString("MainWindow.menu.tools.speedtest").replaceAll("&", "");
      }
      earlyStartup();
      registerTorrentFile();
   
Methods Summary
private static java.lang.StringOSXtoString(int i)

		char[] c = new char[4];
		c[0] = (char)((i >> 24) & 0xff);
		c[1] = (char)((i >> 16) & 0xff);
		c[2] = (char)((i >> 8) & 0xff);
		c[3] = (char)(i & 0xff);
		return new String(c);
	
intappleEventProc(int nextHandler, int theEvent, int userData)

		int eventClass = OS.GetEventClass(theEvent);
		//int eventKind = OS.GetEventKind(theEvent);

		//System.out.println("appleEventProc " + OSXtoString(eventClass) + ";"
		//		+ OS.GetEventKind(theEvent) + ";" + OSXtoString(theEvent) + ";"
		//		+ OSXtoString(userData));

		// Process teh odoc event
		if (eventClass == OS.kEventClassAppleEvent) {
			int[] aeEventID = new int[1];
			if (OS.GetEventParameter(theEvent, OS.kEventParamAEEventID, OS.typeType,
					null, 4, null, aeEventID) != OS.noErr) {
				return OS.eventNotHandledErr;
			}
			//System.out.println("EventID = " + OSXtoString(aeEventID[0]));
			if (aeEventID[0] != kAEOpenDocuments 
					&& aeEventID[0] != kURLEventClass
					&& aeEventID[0] != kAEReopenApplication 
					&& aeEventID[0] != kAEOpenContents
					&& aeEventID[0] != OS.kAEQuitApplication) {
				return OS.eventNotHandledErr;
			}

			// Handle Event
			EventRecord eventRecord = new EventRecord();
			OS.ConvertEventRefToEventRecord(theEvent, eventRecord);
			OS.AEProcessAppleEvent(eventRecord);

			// Tell Mac we are handling this event
			return OS.noErr;
		}

		return OS.eventNotHandledErr;
	
public voidearlyStartup()

      final Display display= Display.getDefault();
      display.syncExec(
      		new AERunnable() {
            public void runSupport() {
               hookApplicationMenu(display);
            }
         }
      );
   
public voidhookApplicationMenu(Display display)
See Apple Technical Q&A 1079 (http://developer.apple.com/qa/qa2001/qa1079.html)
Also http://developer.apple.com/documentation/Carbon/Reference/Menu_Manager/menu_mgr_ref/function_group_10.html

            // Callback target
      Object target= new Object() {
         int commandProc(int nextHandler, int theEvent, int userData) {
            if (OS.GetEventKind(theEvent) == OS.kEventProcessCommand) {
               HICommand command= new HICommand();
               OS.GetEventParameter(theEvent, OS.kEventParamDirectObject, OS.typeHICommand, null, HICommand.sizeof, null, command);
               switch (command.commandID) {
               case kHICommandPreferences: {
              	 UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
              	 if (uiFunctions != null) {
              		 uiFunctions.showConfig(null);
              	 }
                  return OS.noErr;
               }
               case kHICommandAbout:
                 AboutWindow.show(display);
                 return OS.noErr;
               case kHICommandRestart: {
              	 UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
              	 if (uiFunctions != null) {
              		 uiFunctions.dispose(true, false);
              	 }
                  return OS.noErr;
               }
               case kHICommandWizard:
                  new ConfigureWizard(AzureusCoreFactory.getSingleton(), false);
                  return OS.noErr;
               case kHICommandNatTest:
                 new NatTestWindow();
                 return OS.noErr;
               case kHICommandSpeedTest:
                 new SpeedTestWizard(AzureusCoreFactory.getSingleton(), display);  
                 return OS.noErr;
                 
               case OS.kAEQuitApplication:
           			UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
          			if (uiFunctions != null) {
          				uiFunctions.dispose(false, false);
            			return OS.noErr;
          			} else {
          				UIExitUtilsSWT.setSkipCloseCheck(true);
          			}
               default:
                  break;
               }
            }
            return OS.eventNotHandledErr;
         }
      };
            final Callback commandCallback= new Callback(target, "commandProc", 3); //$NON-NLS-1$
      int commandProc= commandCallback.getAddress();
      if (commandProc == 0) {
         commandCallback.dispose();
         return;  // give up
      }

      // Install event handler for commands
      int[] mask= new int[] {
         OS.kEventClassCommand, OS.kEventProcessCommand
      };
      OS.InstallEventHandler(OS.GetApplicationEventTarget(), commandProc, mask.length / 2, mask, 0, null);

      // create About menu command
      int[] outMenu= new int[1];
      short[] outIndex= new short[1];
      if (OS.GetIndMenuItemWithCommandID(0, kHICommandPreferences, 1, outMenu, outIndex) == OS.noErr && outMenu[0] != 0) {
         int menu= outMenu[0];

         int l= fgAboutActionName.length();
         char buffer[]= new char[l];
         fgAboutActionName.getChars(0, l, buffer, 0);
         int str= OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
         OS.InsertMenuItemTextWithCFString(menu, str, (short) 0, 0, kHICommandAbout);
         OS.CFRelease(str);
                  // add separator between About & Preferences
         OS.InsertMenuItemTextWithCFString(menu, 0, (short) 1, OS.kMenuItemAttrSeparator, 0);

         // enable pref menu
         OS.EnableMenuCommand(menu, kHICommandPreferences);
               // disable services menu
         OS.DisableMenuCommand(menu, kHICommandServices);

          // wizard menu
         l= fgWizardActionName.length();
         buffer= new char[l];
         fgWizardActionName.getChars(0, l, buffer, 0);
         str= OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
         OS.InsertMenuItemTextWithCFString(menu, str, (short) 3, 0, kHICommandWizard);
         OS.CFRelease(str);
         
         
         // NAT test menu
         l= fgNatTestActionName.length();
         buffer= new char[l];
         fgNatTestActionName.getChars(0, l, buffer, 0);
         str= OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
         OS.InsertMenuItemTextWithCFString(menu, str, (short) 4, 0, kHICommandNatTest);
         OS.CFRelease(str);
         

          //SpeedTest
          l = fgSpeedTestActionName.length();
          buffer = new char[l];
          fgSpeedTestActionName.getChars(0,l,buffer,0);
          str= OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
          OS.InsertMenuItemTextWithCFString(menu, str, (short) 5, 0, kHICommandSpeedTest);
          OS.CFRelease(str);


          OS.InsertMenuItemTextWithCFString(menu, 0, (short) 6, OS.kMenuItemAttrSeparator, 0);

          // restart menu
         l= fgRestartActionName.length();
         buffer= new char[l];
         fgRestartActionName.getChars(0, l, buffer, 0);
         str= OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
         OS.InsertMenuItemTextWithCFString(menu, str, (short) 7, 0, kHICommandRestart);
         OS.CFRelease(str);

          OS.InsertMenuItemTextWithCFString(menu, 0, (short) 8, OS.kMenuItemAttrSeparator, 0);
      }

      // schedule disposal of callback object
      display.disposeExec(
         new AERunnable() {
            public void runSupport() {
               commandCallback.dispose();
//               stopSidekick();
            }
         }
      );
   
private static voidmemmove(byte[] dest, int src, int size)

		switch (memmove_type) {
			case 0:
				try {
					OSXAccess.memmove(dest, src, size);
					memmove_type = 0;
					return;
				} catch (Throwable e) {
				}
				// FALL THROUGH

			case 1:
				try {
					Class cMemMove = Class.forName("org.eclipse.swt.internal.carbon.OS");

					Method method = cMemMove.getMethod("memmove", new Class[] {
						byte[].class,
						Integer.TYPE,
						Integer.TYPE
					});

					method.invoke(null, new Object[] {
						dest,
						new Integer(src),
						new Integer(size)
					});
					memmove_type = 1;
					return;
				} catch (Throwable e) {
				}

				// FALL THROUGH
			case 2:
				try {
					Class cMemMove = Class.forName("org.eclipse.swt.internal.carbon.OS");

					Method method = cMemMove.getMethod("memcpy", new Class[] {
						byte[].class,
						Integer.TYPE,
						Integer.TYPE
					});

					method.invoke(null, new Object[] {
						dest,
						new Integer(src),
						new Integer(size)
					});

					memmove_type = 2;
					return;
				} catch (Throwable e) {
				}

				// FALL THROUGH

			default:
				break;
		}

		memmove_type = 3;
	
public static voidregisterToolbarToggle(Shell shell)

		final Callback toolbarToggleCB = new Callback(target, "toolbarToggle", 3);
		int toolbarToggle = toolbarToggleCB.getAddress();
		if (toolbarToggle == 0) {
			Debug.out("OSX: Could not find callback 'toolbarToggle'");
			toolbarToggleCB.dispose();
			return;
		}

		shell.getDisplay().disposeExec(new Runnable() {
			public void run() {
				toolbarToggleCB.dispose();
			}
		});

		//	 add the button to the window trim
		int windowHandle = OS.GetControlOwner(shell.handle);
		OS.ChangeWindowAttributes(windowHandle, OS.kWindowToolbarButtonAttribute, 0);

		int[] mask = new int[] {
			OS.kEventClassWindow,
			OS.kEventWindowToolbarSwitchMode
		};
		// register the handler with the OS
		OS.InstallEventHandler(OS.GetApplicationEventTarget(), toolbarToggle,
				mask.length / 2, mask, 0, null);
	
private voidregisterTorrentFile()

 		int result;

 		Callback clickDockIconCallback = new Callback(target, "clickDockIcon", 3);
 		int clickDocIcon = clickDockIconCallback.getAddress();
 		if (clickDocIcon == 0) {
 			clickDockIconCallback.dispose();
 		} else {
 			result = OS.AEInstallEventHandler(kCoreEventClass, kAEReopenApplication,
 					clickDocIcon, 0, false);

 			if (result != OS.noErr) {
 				Debug.out("OSX: Could Install ReopenApplication Event Handler. Error: " + result);
 			}
 		}

 		Callback openContentsCallback = new Callback(target, "openContents", 3);
 		int openContents = openContentsCallback.getAddress();
 		if (openContents == 0) {
 			openContentsCallback.dispose();
 		} else {
 			result = OS.AEInstallEventHandler(kCoreEventClass, kAEOpenContents,
 					openContents, 0, false);

 			if (result != OS.noErr) {
 				Debug.out("OSX: Could Install OpenContents Event Handler. Error: " + result);
 			}
 		}

		Callback openDocCallback = new Callback(target, "openDocProc", 3);
		int openDocProc = openDocCallback.getAddress();
		if (openDocProc == 0) {
			Debug.out("OSX: Could not find Callback 'openDocProc'");
			openDocCallback.dispose();
			return;
		}

		result = OS.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
				openDocProc, 0, false);

		if (result != OS.noErr) {
			Debug.out("OSX: Could not Install OpenDocs Event Handler. Error: " + result);
			return;
		}

		result = OS.AEInstallEventHandler(kURLEventClass, kURLEventClass,
				openDocProc, 0, false);
		if (result != OS.noErr) {
			Debug.out("OSX: Could not Install URLEventClass Event Handler. Error: " + result);
			return;
		}
		
		///
		
		Callback quitAppCallback = new Callback(target, "quitAppProc", 3);
		int quitAppProc = quitAppCallback.getAddress();
		if (quitAppProc == 0) {
			Debug.out("OSX: Could not find Callback 'quitApp'");
			quitAppCallback.dispose();
		} else {
			result = OS.AEInstallEventHandler(kCoreEventClass, OS.kAEQuitApplication,
					quitAppProc, 0, false);
			if (result != OS.noErr) {
				Debug.out("OSX: Could not install QuitApplication Event Handler. Error: "
						+ result);
			}
		}

		///

		int appTarget = OS.GetApplicationEventTarget();
		Callback appleEventCallback = new Callback(this, "appleEventProc", 3);
		int appleEventProc = appleEventCallback.getAddress();
		int[] mask3 = new int[] {
				OS.kEventClassAppleEvent,
				OS.kEventAppleEvent,
				kURLEventClass,
				kAEReopenApplication,
				kAEOpenContents,};
		result = OS.InstallEventHandler(appTarget, appleEventProc,
				mask3.length / 2, mask3, 0, null);
		if (result != OS.noErr) {
			Debug.out("OSX: Could Install Event Handler. Error: " + result);
			return;
		}
	
private static voidstopSidekick()

       try
       {
           Runtime.getRuntime().exec(new String[]{"osascript", "-e", "tell application \"Azureus\" to quit"});
       }
       catch (IOException e)
       {
           Debug.printStackTrace(e);
       }