/** * 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(); } }
/** * 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(); } }
static Program getProgram(NSBundle bundle) { NSString CFBundleName = NSString.stringWith("CFBundleName"); NSString CFBundleDisplayName = NSString.stringWith("CFBundleDisplayName"); NSString fullPath = bundle.bundlePath(); NSString identifier = bundle.bundleIdentifier(); id bundleName = bundle.objectForInfoDictionaryKey(CFBundleDisplayName); if (bundleName == null) { bundleName = bundle.objectForInfoDictionaryKey(CFBundleName); } if (bundleName == null) { if (fullPath == null) return null; bundleName = fullPath.lastPathComponent().stringByDeletingPathExtension(); } NSString name = new NSString(bundleName.id); Program program = new Program(); program.name = name.getString(); if (fullPath != null) program.fullPath = fullPath.getString(); program.identifier = identifier != null ? identifier.getString() : ""; return program; }
/** * This is usually extracted from the the database using JDBC, but this is really inconvenient for * users who are trying to generate SQL at some. A specific version of the data has been written * into the property list of the framework and this can be used as a hard-coded equivalent. */ public NSDictionary jdbcInfo() { // you can swap this code out to write the property list out in order // to get a fresh copy of the JDBCInfo.plist. // try { // String jdbcInfoS = NSPropertyListSerialization.stringFromPropertyList(super.jdbcInfo()); // FileOutputStream fos = new FileOutputStream("/tmp/JDBCInfo.plist"); // fos.write(jdbcInfoS.getBytes()); // fos.close(); // } // catch(Exception e) { // throw new IllegalStateException("problem writing JDBCInfo.plist",e); // } NSDictionary jdbcInfo; // have a look at the JDBC connection URL to see if the flag has been set to // specify that the hard-coded jdbcInfo information should be used. if (shouldUseBundledJdbcInfo()) { if (NSLog.debugLoggingAllowedForLevel(NSLog.DebugLevelDetailed)) { NSLog.debug.appendln( "Loading jdbcInfo from JDBCInfo.plist as opposed to using the JDBCPlugIn default implementation."); } InputStream jdbcInfoStream = NSBundle.bundleForClass(getClass()).inputStreamForResourcePath("JDBCInfo.plist"); if (jdbcInfoStream == null) { throw new IllegalStateException("Unable to find 'JDBCInfo.plist' in this plugin jar."); } try { jdbcInfo = (NSDictionary) NSPropertyListSerialization.propertyListFromData( new NSData(jdbcInfoStream, 2048), "US-ASCII"); } catch (IOException e) { throw new RuntimeException("Failed to load 'JDBCInfo.plist' from this plugin jar.", e); } finally { try { jdbcInfoStream.close(); } catch (IOException e) { } } } else { jdbcInfo = super.jdbcInfo(); } return jdbcInfo; }
protected void makeScalePopUpButton() { if (_scalePopUpButton == null) { /* Create the pop up button. */ _scalePopUpButton = new NSPopUpButton(new NSRect(0f, 0f, 1f, 1f), false); _scalePopUpButton.setBezelStyle(NSButtonCell.RegularSquareBezelStyle); /* Fill it with the scales. */ int numberOfDefaultItems = MenuLabels.length; for (int i = 0; i < numberOfDefaultItems; i++) { String label = NSBundle.localizedString(MenuLabels[i]); _scalePopUpButton.addItem(label); NSMenuItem item = (NSMenuItem) _scalePopUpButton.itemAtIndex(i); if (ScaleFactors[i] != 0f) { Number factor = new Float(ScaleFactors[i]); item.setRepresentedObject(factor); } } _scalePopUpButton.selectItemAtIndex(DefaultSelectedItem); /* Hook it up. */ _scalePopUpButton.setTarget(this); _scalePopUpButton.setAction(new NSSelector("scalePopUpAction", new Class[] {getClass()})); /* Pick a suitable font. */ _scalePopUpButton.setFont(NSFont.toolTipsFontOfSize(10f)); /* Make sure the pop up is big enough to fit the cells. */ _scalePopUpButton.sizeToFit(); /* Don't ever let it become the first responder. */ _scalePopUpButton.setRefusesFirstResponder(true); /* Put it in the scroll view. */ addSubview(_scalePopUpButton); } }
/** * 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(); } }