/**
  * 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();
   }
 }
Beispiel #3
0
 /**
  * Adds to the receiver the pattern of glyphs generated by drawing the given string using the
  * given font starting at the point (x, y).
  *
  * @param string the text to use
  * @param x the x coordinate of the starting point
  * @param y the y coordinate of the starting point
  * @param font the font to use
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the font is null
  *       <li>ERROR_INVALID_ARGUMENT - if the font has been disposed
  *     </ul>
  *
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  */
 public void addString(String string, float x, float y, Font font) {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   if (font == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   if (font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   NSString str = NSString.stringWith(string);
   NSTextStorage textStorage = ((NSTextStorage) new NSTextStorage().alloc());
   textStorage.initWithString_(str);
   NSLayoutManager layoutManager = (NSLayoutManager) new NSLayoutManager().alloc().init();
   NSTextContainer textContainer = (NSTextContainer) new NSTextContainer().alloc();
   NSSize size = new NSSize();
   size.width = Float.MAX_VALUE;
   size.height = Float.MAX_VALUE;
   textContainer.initWithContainerSize(size);
   textStorage.addLayoutManager(layoutManager);
   layoutManager.addTextContainer(textContainer);
   NSRange range = new NSRange();
   range.length = str.length();
   textStorage.beginEditing();
   textStorage.addAttribute(OS.NSFontAttributeName(), font.handle, range);
   textStorage.endEditing();
   range = layoutManager.glyphRangeForTextContainer(textContainer);
   if (range.length != 0) {
     int glyphs = OS.malloc(4 * range.length * 2);
     layoutManager.getGlyphs(glyphs, range);
     NSBezierPath path = NSBezierPath.bezierPath();
     NSPoint point = new NSPoint();
     point.x = x;
     point.y = y;
     path.moveToPoint(point);
     path.appendBezierPathWithGlyphs(glyphs, range.length, font.handle);
     NSAffineTransform transform = NSAffineTransform.transform();
     transform.scaleXBy(1, -1);
     transform.translateXBy(0, -((2 * y) + textStorage.size().height));
     path.transformUsingAffineTransform(transform);
     OS.free(glyphs);
     handle.appendBezierPath(path);
   }
   textContainer.release();
   layoutManager.release();
   textStorage.release();
 }
 static boolean isExecutable(String fileName) {
   long /*int*/ ptr = OS.malloc(1);
   NSString path = NSString.stringWith(fileName);
   boolean result = false;
   NSFileManager manager = NSFileManager.defaultManager();
   if (manager.fileExistsAtPath(path, ptr)) {
     byte[] isDirectory = new byte[1];
     OS.memmove(isDirectory, ptr, 1);
     if (isDirectory[0] == 0 && manager.isExecutableFileAtPath(path)) {
       NSWorkspace ws = NSWorkspace.sharedWorkspace();
       NSString type = ws.typeOfFile(path, 0);
       result =
           type != null
               && (ws.type(type, NSString.stringWith("public.unix-executable"))
                   || //$NON-NLS-1$
                   OS.UTTypeEqual(
                       type.id, NSString.stringWith("public.shell-script").id)); // $NON-NLS-1$
     }
   }
   OS.free(ptr);
   return result;
 }
 static NSURL getURL(String fileName) {
   NSString unescapedStr;
   String lowercaseName = fileName.toLowerCase();
   if (lowercaseName.startsWith(PREFIX_HTTP)
       || lowercaseName.startsWith(PREFIX_HTTPS)
       || lowercaseName.startsWith(PREFIX_FILE)) {
     unescapedStr = NSString.stringWith("%#"); // $NON-NLS-1$
   } else {
     unescapedStr = NSString.stringWith("%"); // $NON-NLS-1$
   }
   NSString fullPath = NSString.stringWith(fileName);
   if (NSFileManager.defaultManager().fileExistsAtPath(fullPath)) {
     fullPath = NSURL.fileURLWithPath(fullPath).absoluteString();
   }
   long /*int*/ ptr =
       OS.CFURLCreateStringByAddingPercentEscapes(
           0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8);
   NSString escapedString = new NSString(ptr);
   NSURL url = NSURL.URLWithString(escapedString);
   OS.CFRelease(ptr);
   return url;
 }
Beispiel #6
0
 /**
  * Returns a device independent representation of the receiver.
  *
  * @return the PathData for the receiver
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  *
  * @see PathData
  */
 public PathData getPathData() {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   int count = handle.elementCount();
   int pointCount = 0, typeCount = 0;
   byte[] types = new byte[count];
   float[] pointArray = new float[count * 6];
   int points = OS.malloc(3 * NSPoint.sizeof);
   if (points == 0) SWT.error(SWT.ERROR_NO_HANDLES);
   NSPoint pt = new NSPoint();
   for (int i = 0; i < count; i++) {
     int element = handle.elementAtIndex_associatedPoints_(i, points);
     switch (element) {
       case OS.NSMoveToBezierPathElement:
         types[typeCount++] = SWT.PATH_MOVE_TO;
         OS.memmove(pt, points, NSPoint.sizeof);
         pointArray[pointCount++] = (int) pt.x;
         pointArray[pointCount++] = (int) pt.y;
         break;
       case OS.NSLineToBezierPathElement:
         types[typeCount++] = SWT.PATH_LINE_TO;
         OS.memmove(pt, points, NSPoint.sizeof);
         pointArray[pointCount++] = (int) pt.x;
         pointArray[pointCount++] = (int) pt.y;
         break;
       case OS.NSCurveToBezierPathElement:
         types[typeCount++] = SWT.PATH_CUBIC_TO;
         OS.memmove(pt, points, NSPoint.sizeof);
         pointArray[pointCount++] = (int) pt.x;
         pointArray[pointCount++] = (int) pt.y;
         OS.memmove(pt, points + NSPoint.sizeof, NSPoint.sizeof);
         pointArray[pointCount++] = (int) pt.x;
         pointArray[pointCount++] = (int) pt.y;
         OS.memmove(pt, points + NSPoint.sizeof + NSPoint.sizeof, NSPoint.sizeof);
         pointArray[pointCount++] = (int) pt.x;
         pointArray[pointCount++] = (int) pt.y;
         break;
       case OS.NSClosePathBezierPathElement:
         types[typeCount++] = SWT.PATH_CLOSE;
         break;
     }
   }
   OS.free(points);
   if (pointCount != pointArray.length) {
     float[] temp = new float[pointCount];
     System.arraycopy(pointArray, 0, temp, 0, pointCount);
     pointArray = temp;
   }
   PathData data = new PathData();
   data.types = types;
   data.points = pointArray;
   return data;
 }
 public void setTag(int tag) {
   OS.object_setInstanceVariable(id, "tag", tag);
 }
 public int tag() {
   int[] tag = new int[1];
   OS.object_getInstanceVariable(id, "tag", tag);
   return tag[0];
   //        return OS.objc_msgSend(id, OS.sel_tag);
 }
 /**
  * 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();
   }
 }