/** * Finds the program that is associated with an extension. The extension may or may not begin with * a '.'. Note that a <code>Display</code> must already exist to guarantee that this method * returns an appropriate result. * * @param extension the program extension * @return the program or <code>null</code> * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT when extension is null * </ul> */ public static Program findProgram(String extension) { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { if (extension == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (extension.length() == 0) return null; Program program = null; char[] chars; if (extension.charAt(0) != '.') { chars = new char[extension.length()]; extension.getChars(0, chars.length, chars, 0); } else { chars = new char[extension.length() - 1]; extension.getChars(1, extension.length(), chars, 0); } NSString ext = NSString.stringWithCharacters(chars, chars.length); if (ext != null) { byte[] fsRef = new byte[80]; if (OS.LSGetApplicationForInfo( OS.kLSUnknownType, OS.kLSUnknownCreator, ext.id, OS.kLSRolesAll, fsRef, null) == OS.noErr) { long /*int*/ url = OS.CFURLCreateFromFSRef(OS.kCFAllocatorDefault(), fsRef); if (url != 0) { NSString bundlePath = new NSURL(url).path(); NSBundle bundle = NSBundle.bundleWithPath(bundlePath); if (bundle != null) program = getProgram(bundle); OS.CFRelease(url); } } } return program; } finally { pool.release(); } }
/** * Returns the receiver's image data. This is the icon that is associated with the receiver in the * operating system. * * @return the image data for the program, may be null */ public ImageData getImageData() { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString fullPath; if (this.fullPath != null) { fullPath = NSString.stringWith(this.fullPath); } else { fullPath = workspace.fullPathForApplication(NSString.stringWith(name)); } if (fullPath != null) { NSImage nsImage = workspace.iconForFile(fullPath); if (nsImage != null) { NSSize size = new NSSize(); size.width = size.height = 16; nsImage.setSize(size); nsImage.retain(); Image image = Image.cocoa_new(Display.getCurrent(), SWT.BITMAP, nsImage); ImageData imageData = image.getImageData(); image.dispose(); return imageData; } } return null; } finally { pool.release(); } }
/** * Answers all available programs in the operating system. Note that a <code>Display</code> must * already exist to guarantee that this method returns an appropriate result. * * @return an array of programs */ public static Program[] getPrograms() { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { List<Program> vector = new ArrayList<>(); NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSArray array = new NSArray( OS.NSSearchPathForDirectoriesInDomains( OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true)); int count = (int) /*64*/ array.count(); for (int i = 0; i < count; i++) { NSString path = new NSString(array.objectAtIndex(i)); NSFileManager fileManager = NSFileManager.defaultManager(); NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path); if (enumerator != null) { id id; while ((id = enumerator.nextObject()) != null) { enumerator.skipDescendents(); NSString fullPath = path.stringByAppendingPathComponent(new NSString(id.id)); if (workspace.isFilePackageAtPath(fullPath)) { NSBundle bundle = NSBundle.bundleWithPath(fullPath); if (bundle != null) { Program program = getProgram(bundle); if (program != null) vector.add(program); } } } } } return vector.toArray(new Program[vector.size()]); } finally { pool.release(); } }
/** * Executes the program with the file as the single argument in the operating system. It is the * responsibility of the programmer to ensure that the file contains valid data for this program. * * @param fileName the file or program name * @return <code>true</code> if the file is launched, otherwise <code>false</code> * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT when fileName is null * </ul> */ public boolean execute(String fileName) { if (fileName == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSURL url = getURL(fileName); NSArray urls = NSArray.arrayWithObject(url); return workspace.openURLs(urls, NSString.stringWith(identifier), 0, null, 0); } finally { pool.release(); } }
/** * Launches the operating system executable associated with the file or URL (http:// or https://). * If the file is an executable then the executable is launched. The program is launched with the * specified working directory only when the <code>workingDir</code> exists and <code>fileName * </code> is an executable. Note that a <code>Display</code> must already exist to guarantee that * this method returns an appropriate result. * * @param fileName the file name or program name or URL (http:// or https://) * @param workingDir the name of the working directory or null * @return <code>true</code> if the file is launched, otherwise <code>false</code> * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT when fileName is null * </ul> * * @since 3.6 */ public static boolean launch(String fileName, String workingDir) { if (fileName == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { if (workingDir != null && isExecutable(fileName)) { try { Compatibility.exec(new String[] {fileName}, null, workingDir); return true; } catch (IOException e) { return false; } } NSURL url = getURL(fileName); NSWorkspace workspace = NSWorkspace.sharedWorkspace(); return workspace.openURL(url); } finally { pool.release(); } }
/** * Displays the Tracker rectangles for manipulation by the user. Returns when the user has either * finished manipulating the rectangles or has cancelled the Tracker. * * @return <code>true</code> if the user did not cancel the Tracker, <code>false</code> otherwise * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public boolean open() { checkWidget(); cancelled = false; tracking = true; window = (NSWindow) new NSWindow().alloc(); NSRect frame = NSScreen.mainScreen().frame(); window = window.initWithContentRect_styleMask_backing_defer_( frame, OS.NSBorderlessWindowMask, OS.NSBackingStoreBuffered, false); window.setOpaque(false); window.setContentView(null); NSGraphicsContext context = window.graphicsContext(); NSGraphicsContext.setCurrentContext(context); context.setCompositingOperation(OS.NSCompositeClear); NSBezierPath.fillRect(frame); window.orderFrontRegardless(); drawRectangles(window, rectangles, false); /* * If exactly one of UP/DOWN is specified as a style then set the cursor * orientation accordingly (the same is done for LEFT/RIGHT styles below). */ int vStyle = style & (SWT.UP | SWT.DOWN); if (vStyle == SWT.UP || vStyle == SWT.DOWN) { cursorOrientation |= vStyle; } int hStyle = style & (SWT.LEFT | SWT.RIGHT); if (hStyle == SWT.LEFT || hStyle == SWT.RIGHT) { cursorOrientation |= hStyle; } Point cursorPos; boolean down = false; NSApplication application = NSApplication.sharedApplication(); NSEvent currentEvent = application.currentEvent(); switch (currentEvent.type()) { case OS.NSLeftMouseDown: case OS.NSRightMouseDown: case OS.NSOtherMouseDown: down = true; } if (down) { cursorPos = display.getCursorLocation(); } else { if ((style & SWT.RESIZE) != 0) { cursorPos = adjustResizeCursor(true); } else { cursorPos = adjustMoveCursor(); } } if (cursorPos != null) { oldX = cursorPos.x; oldY = cursorPos.y; } /* Tracker behaves like a Dialog with its own OS event loop. */ while (tracking && !cancelled) { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); NSEvent event = application.nextEventMatchingMask( 0, NSDate.distantFuture(), OS.NSDefaultRunLoopMode, true); if (event == null) continue; int type = event.type(); switch (type) { case OS.NSLeftMouseUp: case OS.NSRightMouseUp: case OS.NSOtherMouseUp: case OS.NSMouseMoved: case OS.NSLeftMouseDragged: case OS.NSRightMouseDragged: case OS.NSOtherMouseDragged: mouse(event); break; case OS.NSKeyDown: // case OS.NSKeyUp: case OS.NSFlagsChanged: key(event); break; } /* * Don't dispatch mouse and key events in general, EXCEPT once this * tracker has finished its work. */ boolean dispatch = true; if (!(tracking && !cancelled)) { switch (type) { case OS.NSLeftMouseDown: case OS.NSLeftMouseUp: case OS.NSRightMouseDown: case OS.NSRightMouseUp: case OS.NSOtherMouseDown: case OS.NSOtherMouseUp: case OS.NSMouseMoved: case OS.NSLeftMouseDragged: case OS.NSRightMouseDragged: case OS.NSOtherMouseDragged: case OS.NSMouseEntered: case OS.NSMouseExited: case OS.NSKeyDown: case OS.NSKeyUp: case OS.NSFlagsChanged: dispatch = false; } } if (dispatch) application.sendEvent(event); if (clientCursor != null && resizeCursor == null) { clientCursor.handle.set(); } pool.release(); } if (!isDisposed()) { drawRectangles(window, rectangles, true); } if (window != null) window.close(); tracking = false; window = null; return !cancelled; }
/** * Answer all program extensions in the operating system. Note that a <code>Display</code> must * already exist to guarantee that this method returns an appropriate result. * * @return an array of extensions */ public static String[] getExtensions() { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSMutableSet supportedDocumentTypes = (NSMutableSet) NSMutableSet.set(); NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString CFBundleDocumentTypes = NSString.stringWith("CFBundleDocumentTypes"); NSString CFBundleTypeExtensions = NSString.stringWith("CFBundleTypeExtensions"); NSArray array = new NSArray( OS.NSSearchPathForDirectoriesInDomains( OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true)); int count = (int) /*64*/ array.count(); for (int i = 0; i < count; i++) { NSString path = new NSString(array.objectAtIndex(i)); NSFileManager fileManager = NSFileManager.defaultManager(); NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path); if (enumerator != null) { id id; while ((id = enumerator.nextObject()) != null) { enumerator.skipDescendents(); NSString filePath = new NSString(id.id); NSString fullPath = path.stringByAppendingPathComponent(filePath); if (workspace.isFilePackageAtPath(fullPath)) { NSBundle bundle = NSBundle.bundleWithPath(fullPath); id = bundle != null ? bundle.infoDictionary().objectForKey(CFBundleDocumentTypes) : null; if (id != null) { NSDictionary documentTypes = new NSDictionary(id.id); NSEnumerator documentTypesEnumerator = documentTypes.objectEnumerator(); while ((id = documentTypesEnumerator.nextObject()) != null) { NSDictionary documentType = new NSDictionary(id.id); id = documentType.objectForKey(CFBundleTypeExtensions); if (id != null) { supportedDocumentTypes.addObjectsFromArray(new NSArray(id.id)); } } } } } } } int i = 0; String[] exts = new String[(int) /*64*/ supportedDocumentTypes.count()]; NSEnumerator enumerator = supportedDocumentTypes.objectEnumerator(); id id; while ((id = enumerator.nextObject()) != null) { String ext = new NSString(id.id).getString(); if (!ext.equals("*")) exts[i++] = "." + ext; } if (i != exts.length) { String[] temp = new String[i]; System.arraycopy(exts, 0, temp, 0, i); exts = temp; } return exts; } finally { pool.release(); } }