Java Code Examples for org.openide.cookies.InstanceCookie#Of

The following examples show how to use org.openide.cookies.InstanceCookie#Of . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetStarted.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Action extractAction( DataObject dob ) {
    OpenCookie oc = dob.getCookie( OpenCookie.class );
    if( null != oc )
        return new LinkAction( dob );

    InstanceCookie.Of instCookie = dob.getCookie(InstanceCookie.Of.class);
    if( null != instCookie && instCookie.instanceOf( Action.class ) ) {
        try {
            Action res = (Action) instCookie.instanceCreate();
            if( null != res ) {
                res.putValue(Action.NAME, dob.getNodeDelegate().getDisplayName() );
            }
            return res;
        } catch( Exception e ) {
            Logger.getLogger(SampleProjectAction.class.getName()).log( Level.INFO, null, e );
        }
    }
    return null;
}
 
Example 2
Source File: SettingChildren.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected Node copyNode (Node node) {
    boolean filter = false;
    try {
        DataObject d = (DataObject) node.getCookie (DataObject.class);
        if (d != null) {
            InstanceCookie.Of inst = (InstanceCookie.Of)d.getCookie(InstanceCookie.Of.class);
            if (inst != null && (inst.instanceOf(Node.class) || inst.instanceOf(Node.Handle.class))) {
                // This is just a node, not a real setting. E.g. ModuleNode, LoaderPoolNode. As such,
                // it itself should not display any origin information, it would make no sense. However
                // its children might have a legitimate DataObject cookie from the SFS.
                d = null;
            }
        }
        DataFolder folder = (DataFolder) node.getCookie (DataFolder.class);
        FileSystem fs = d == null || folder != null ? null : d.getPrimaryFile ().getFileSystem ();
        filter = fs == null ? false : fs.isDefault();
    } catch (FileStateInvalidException e) {
        // ignore
    }

    return filter ? new SettingFilterNode (node) : 
        node.isLeaf() ? node.cloneNode() : new TrivialFilterNode(node);
}
 
Example 3
Source File: FolderLookup.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Overrides superclass method. It returns instance
 * for <code>DataObject</code>&<code>InstanceCookie</code> 'pair'. 
 * If the instance is of <code>FolderLookup.Lkp</code> class it is created otherwise
 * new <code>Lkp.ICItem</code> created and returned.
 *
 * @param dobj the data object that is the source of the cookie
 * @param cookie the instance cookie to read the instance from
 * @exception IOException when there I/O error
 * @exception ClassNotFoundException if the class cannot be found */
protected Object instanceForCookie(DataObject dobj, InstanceCookie cookie)
throws IOException, ClassNotFoundException {
    boolean isLookup;
    
    if(cookie instanceof InstanceCookie.Of) {
        isLookup = ((InstanceCookie.Of)cookie).instanceOf(Lookup.class);
    } else {
        isLookup = Lookup.class.isAssignableFrom(cookie.instanceClass ());
    }

    if(isLookup) {
        // Is underlying FolderLookup create it.
        return cookie.instanceCreate();
    } else {
        return new ICItem(dobj, rootName, cookie);
    }
}
 
Example 4
Source File: InstanceDataObjectTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Tests instanceOf attribute.
 */
public void testInstanceOfAttribute () throws Exception {
    FileObject fo = FileUtil.createData (lfs.getRoot (), "BB/AAA/X.instance");
    fo.setAttribute ("instanceClass", "java.lang.Number");
    fo.setAttribute ("instanceCreate", new Long (1L));
    fo.setAttribute ("instanceOf", "java.lang.Object,java.lang.Long");
    
    DataObject obj = DataObject.find (fo);
    assertNotNull("Object found", obj);
    
    InstanceCookie.Of c = (InstanceCookie.Of)getCookie(obj, InstanceCookie.Of.class);
    assertNotNull ("Cookie found", c);
    
    assertTrue("Instance of object", c.instanceOf(Object.class));
    assertTrue("Not declared to be Serializable", !c.instanceOf(Serializable.class));
    assertTrue("Declared to be also Long", c.instanceOf(Long.class));
    assertTrue("Nobody knows about it being number", !c.instanceOf(Number.class));
    
    assertEquals ("Class is defined to be Number", Number.class, c.instanceClass());
    Object o = c.instanceCreate ();
    assertTrue ("It is a long", o instanceof Long);
    assertEquals ("It is 1", 1, ((Long)o).intValue());
}
 
Example 5
Source File: XMLPropertiesConvertorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@RandomlyFails // NB-Core-Build #8238
public void testUpgradeSetting2() throws Exception {
    String res = "Settings/testUpgradeSetting2/ObsoleteClass.settings";
    FileObject fo = FileUtil.getConfigFile(res);
    assertNotNull(res, fo);
    long last = fo.lastModified().getTime();
    
    DataObject dobj = DataObject.find(fo);
    InstanceCookie.Of ic = (InstanceCookie.Of) dobj.getCookie(InstanceCookie.Of.class);
    assertNotNull (dobj + " does not contain instance cookie", ic);
    assertTrue("instanceOf failed", ic.instanceOf(FooSetting.class));
    assertEquals("instanceClass failed", FooSetting.class, ic.instanceClass());
    
    FooSetting foo = (FooSetting) ic.instanceCreate();
    assertEquals("too early upgrade", last, fo.lastModified().getTime());
    
    foo.setProperty1("A");
    Thread.sleep(3000);
    assertTrue("upgrade failed", last != fo.lastModified().getTime());
}
 
Example 6
Source File: XMLPropertiesConvertorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@RandomlyFails // if Thread.sleep is commented out
public void testUpgradeSetting() throws Exception {
    String res = "Settings/org-netbeans-modules-settings-convertors-FooSettingSerialData.settings";
    FileObject fo = FileUtil.getConfigFile(res);
    assertNotNull(res, fo);
    long last = fo.lastModified().getTime();
    
    DataObject dobj = DataObject.find (fo);
    InstanceCookie.Of ic = (InstanceCookie.Of) dobj.getCookie(InstanceCookie.Of.class);
    assertNotNull (dobj + " does not contain instance cookie", ic);
    assertTrue("instanceOf failed", ic.instanceOf(FooSetting.class));
    assertEquals("instanceClass failed", FooSetting.class, ic.instanceClass());
    
    FooSetting foo = (FooSetting) ic.instanceCreate();
    assertEquals("too early upgrade", last, fo.lastModified().getTime());
    
    foo.setProperty1("A");
    Thread.sleep(3000);
    assertTrue("upgrade failed", last != fo.lastModified().getTime());
}
 
Example 7
Source File: XMLSettingsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected InstanceCookie acceptCookie(InstanceCookie cookie) throws IOException, ClassNotFoundException {
    if (cookie instanceof InstanceCookie.Of) {
        if (((InstanceCookie.Of)cookie).instanceOf(SuperClazz.class)) {
            //System.out.println("acceptCookie: OK");
            return cookie;
        }
        System.out.println("acceptCookie: not IC.Of");
    }
    System.out.println("acceptCookie: strange class: " + cookie.instanceName());
    return null;
}
 
Example 8
Source File: AnnotationTypeActionsFolder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isAction(InstanceCookie ic) {
    if (ic instanceof InstanceCookie.Of) {
        return ((InstanceCookie.Of) ic).instanceOf(Action.class);
    } else {
        return Action.class.isAssignableFrom(ic.getClass());
    }
}
 
Example 9
Source File: InstanceNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** initialize the icon base according to state of the settings instance (valid/broken) */
private void initIconBase() {
    InstanceCookie.Of ic = ic();
    String iconBase = INSTANCE_ICON_BASE;
    if (ic == null) {//XXX && io.instanceOf(XMLSettingsSupport.BrokenSettings.class)) {
        iconBase = "org/openide/loaders/instanceBroken.gif"; // NOI18N
    }
    setIconBaseWithExtension(iconBase);
}
 
Example 10
Source File: InstanceDataObjectTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void testParticularInstanceDefinition(FileObject fo) throws Exception {
    assertNotNull(fo);
    String filename = fo.getNameExt();
    DataObject dobj = DataObject.find(fo);
    InstanceCookie.Of ic = (InstanceCookie.Of) getCookie(dobj, InstanceCookie.Of.class);
    assertNotNull(filename, ic);
    
    assertTrue(filename, ic.instanceOf(Runnable.class));
    assertTrue(filename, ic.instanceOf(TestDefinitions.class));
    
    assertEquals(filename, ic.instanceClass(), TestDefinitions.class);
    assertNotNull(filename, ic.instanceCreate());
}
 
Example 11
Source File: OptionsAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Expands the node in explorer.
 */
private static void expandNodes (Node n, final int depth, final Collection<Node> list) {
    if (depth == 0) {
        return;
    }
    
    DataObject obj = (DataObject)n.getCookie(DataObject.class);
    if (obj instanceof DataShadow) {
        obj = ((DataShadow)obj).getOriginal();
    }
    
    if (obj != null) {
        if (!obj.getPrimaryFile().getPath().startsWith ("UI/Services")) { // NOI18N
            return;
        }

        InstanceCookie ic = (InstanceCookie)obj.getCookie (InstanceCookie.class);
        if (ic != null) {

            if (ic instanceof InstanceCookie.Of) {
                if (((InstanceCookie.Of)ic).instanceOf (Node.class)) {
                    return;
                }
            } 
        }
    }
    
    // ok, expand this node
    if (!list.contains (n)) {
        list.add (n);
    }
 
    Node[] arr = n.getChildren().getNodes(true);
    for (int i = 0; i < arr.length; i++) {
        final Node p = arr[i];
        expandNodes(p, depth - 1, list);
    }
}
 
Example 12
Source File: XMLPropertiesConvertorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@RandomlyFails // NB-Core-Build #8155
    public void testUpgradeSettingWithUnknownClass() throws Exception {
        String res = "Settings/org-netbeans-modules-settings-convertors-FooSettingSerialDataUnknownClass.settings";
        FileObject fo = FileUtil.getConfigFile(res);
        assertNotNull(res, fo);
        long last = fo.lastModified().getTime();
        
        DataObject dobj = DataObject.find (fo);
        InstanceCookie.Of ic = (InstanceCookie.Of) dobj.getCookie(InstanceCookie.Of.class);
        assertNotNull (dobj + " does not contain instance cookie", ic);
        assertTrue("instanceOf failed", ic.instanceOf(FooSetting.class));
        assertEquals("instanceClass failed", FooSetting.class, ic.instanceClass());
        
        FooSetting foo = (FooSetting) ic.instanceCreate();
        assertEquals("too early upgrade", last, fo.lastModified().getTime());
        
        foo.setProperty1("A");
        Thread.sleep(3000);
        
        DataInputStream dis = new DataInputStream(new BufferedInputStream(fo.getInputStream(), 1024));
        StringBuffer sb = new StringBuffer(dis.readLine());
        sb.append(dis.readLine());
        sb.append(dis.readLine());
        dis.close();
        String line = sb.toString();
        
        assertTrue("upgrade failed: " + line, line.indexOf("properties") > 0);
//        assertTrue("upgrade failed", last != fo.lastModified().getTime());
        
    }
 
Example 13
Source File: XMLPropertiesConvertorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Checks whether the instance is the same.
 */
public void testSame() throws Exception {
    FileObject tsFO = root.createFolder("testSame");
    assertNotNull("folder 'testSame' is not created!", tsFO);
    DataFolder folder = (DataFolder) DataObject.find(tsFO).getCookie(DataFolder.class);
    assertNotNull("missing data folder" + folder);
    
    FooSetting ser = new FooSetting("A");
    
    InstanceDataObject i = InstanceDataObject.create (folder, null, ser, null);
    
    InstanceCookie.Of ic = (InstanceCookie.Of) i.getCookie(InstanceCookie.Of.class);
    assertNotNull (i + " does not contain instance cookie", ic);
    
    assertTrue("instanceOf failed", ic.instanceOf(ser.getClass()));
    assertEquals("instanceClass", ser.getClass(), ic.instanceClass());
    
    Object n = ic.instanceCreate ();
    assertEquals("Value is different from stored one", System.identityHashCode(ser), System.identityHashCode(n));
    
    ser.setProperty1("B");
    ic = (InstanceCookie.Of) i.getCookie(InstanceCookie.Of.class);
    assertEquals("Value is different from stored one", ser, ic.instanceCreate());
    
    for (int j = 0; j <100; j++) {
        ser.setProperty1(String.valueOf(j));
    }
    ic = (InstanceCookie.Of) i.getCookie(InstanceCookie.Of.class);
    assertEquals("Value is different from stored one", ser, ic.instanceCreate());
    
    i.delete();
}
 
Example 14
Source File: XMLPropertiesConvertorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Test whether instances survive garbage collection.
 */
public void testSameWithGC () throws Exception {
    FileObject tsFO = root.createFolder("testSameWithGC");
    assertNotNull("folder 'testSameWithGC' is not created!", tsFO);
    DataFolder folder = (DataFolder) DataObject.find(tsFO).getCookie(DataFolder.class);
    assertNotNull("missing data folder" + folder);
    
    FooSetting ser = new FooSetting("testSameWithGC");
    
    FileObject prim = InstanceDataObject.create (folder, "MyName", ser, null).getPrimaryFile ();
    String name = prim.getName ();
    String ext = prim.getExt ();
    prim = null;

    System.gc ();
    System.gc ();
    System.gc ();
    System.gc ();
    System.gc ();
    System.gc ();
    System.gc ();
    System.gc ();
    System.gc ();
    
    FileObject fo = folder.getPrimaryFile ().getFileObject (name, ext);
    assertNotNull ("MyName.settings not found", fo);
    
    DataObject obj = DataObject.find (fo);
    
    InstanceCookie.Of ic = (InstanceCookie.Of) obj.getCookie(InstanceCookie.Of.class);
    assertNotNull (obj + " does not contain instance cookie", ic);
    
    assertTrue("instanceOf failed", ic.instanceOf(ser.getClass()));
    assertEquals("instanceClass", ser.getClass(), ic.instanceClass());
    
    Object value = ic.instanceCreate ();
    assertEquals("Value is different from stored one", System.identityHashCode(ser), System.identityHashCode(value));
}
 
Example 15
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private InstanceCookie.Of ic () {
    return (InstanceCookie.Of) getDataObject().getCookie(InstanceCookie.Of.class);
}
 
Example 16
Source File: InstanceProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** create own InstanceCookie implementation */
private InstanceCookie.Of createInstance(Object inst) {
    return new InstanceCookieImpl(inst);
}
 
Example 17
Source File: DefaultJavaPlatformProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public JavaPlatform[] getInstalledPlatforms() {
    final List<JavaPlatform> platforms = new ArrayList<JavaPlatform>();
    final FileObject storage = getStorage();
    if (storage != null) {
        for (FileObject platformDefinition : storage.getChildren()) {
            try {
                final DataObject dobj = DataObject.find(platformDefinition);
                final InstanceCookie ic = dobj.getCookie(InstanceCookie.class);
                if (ic == null) {
                    LOG.log(
                        Level.WARNING,
                        "The file: {0} has no InstanceCookie",  //NOI18N
                        platformDefinition.getNameExt());
                    continue;
                } else  if (ic instanceof InstanceCookie.Of) {
                    if (((InstanceCookie.Of)ic).instanceOf(JavaPlatform.class)) {
                        platforms.add((JavaPlatform) ic.instanceCreate());
                    } else {
                        LOG.log(
                            Level.WARNING,
                            "The file: {0} is not an instance of JavaPlatform", //NOI18N
                            platformDefinition.getNameExt());
                    }
                } else {
                    Object instance = ic.instanceCreate();
                    if (instance instanceof JavaPlatform) {
                        platforms.add((JavaPlatform) instance);
                    } else {
                        LOG.log(
                            Level.WARNING,
                            "The file: {0} is not an instance of JavaPlatform", //NOI18N
                            platformDefinition.getNameExt());
                    }
                }
            } catch (ClassNotFoundException | IOException e) {
                Exceptions.printStackTrace(e);
            }
        }
    }
    return platforms.toArray(new JavaPlatform[platforms.size()]);
}
 
Example 18
Source File: ActionsList.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isSeparator(InstanceCookie o) throws Exception {
    return (o instanceof InstanceCookie.Of && ((InstanceCookie.Of) o).instanceOf(JSeparator.class))
        || JSeparator.class.isAssignableFrom(o.instanceClass());
}
 
Example 19
Source File: InstanceNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private InstanceCookie.Of ic () {
    return getDataObject().getCookie(InstanceCookie.Of.class);
}
 
Example 20
Source File: AndroidSdkProvider.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
protected AndroidSdk[] updateSDKs() {
    final List<AndroidSdk> platforms = new ArrayList<>();
    final FileObject storage = getStorage();
    if (storage != null) {
        try {
            for (FileObject platformDefinition : storage.getChildren()) {
                DataObject dobj = DataObject.find(platformDefinition);
                InstanceCookie ic = dobj.getCookie(InstanceCookie.class);
                if (ic == null) {
                    FileObject[] childrens = storage.getChildren();
                    for (FileObject children : childrens) {
                        try {
                            DataObject dob = DataObject.find(children);
                            if (dob instanceof XMLDataObject) {
                                platforms.add(PlatformConvertor.localInstance(dob));
                            }
                        } catch (DataObjectNotFoundException dataObjectNotFoundException) {
                        }
                    }
                    LOG.log(
                            Level.WARNING,
                            "The file: {0} has no InstanceCookie", //NOI18N
                            platformDefinition.getNameExt());
                } else if (ic instanceof InstanceCookie.Of) {
                    if (((InstanceCookie.Of) ic).instanceOf(AndroidSdk.class)) {
                        platforms.add((AndroidSdk) ic.instanceCreate());
                    } else {
                        LOG.log(
                                Level.WARNING,
                                "The file: {0} is not an instance of AndroidSdkPlatform", //NOI18N
                                platformDefinition.getNameExt());
                    }
                } else {
                    Object instance = ic.instanceCreate();
                    if (instance instanceof AndroidSdk) {
                        platforms.add((AndroidSdk) instance);
                    } else {
                        LOG.log(
                                Level.WARNING,
                                "The file: {0} is not an instance of AndroidSdkPlatform", //NOI18N
                                platformDefinition.getNameExt());
                    }
                }
            }
        } catch (ClassNotFoundException | IOException cnf) {
            Exceptions.printStackTrace(cnf);
        }
    }
    return platforms.toArray(new AndroidSdk[platforms.size()]);
}