Java Code Examples for org.openide.cookies.InstanceCookie#instanceClass()

The following examples show how to use org.openide.cookies.InstanceCookie#instanceClass() . 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: MenuBar.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
           * Accepts only cookies that can provide <code>Menu</code>.
           * @param cookie an <code>InstanceCookie</code> to test
           * @return true if the cookie can provide accepted instances
           */
  	    protected @Override InstanceCookie acceptCookie(InstanceCookie cookie)
  	    throws IOException, ClassNotFoundException {
// [pnejedly] Don't try to optimize this by InstanceCookie.Of
// It will load the classes few ms later from instanceCreate
// anyway and more instanceOf calls take longer
          	Class c = cookie.instanceClass();
              boolean action = Action.class.isAssignableFrom (c);
              if (action) {
                  cookie.instanceCreate();
              }
          	boolean is =
              	Presenter.Menu.class.isAssignableFrom (c) ||
              	JMenuItem.class.isAssignableFrom (c) ||
              	JSeparator.class.isAssignableFrom (c) ||
                  action;
          	return is ? cookie : null;
  	    }
 
Example 2
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void init() {
    try {
        InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
        if (ic == null) {
            bean = null;
            return;
        }
        Class<?> clazz = ic.instanceClass();
        if (BeanContext.class.isAssignableFrom(clazz)) {
            bean = ic.instanceCreate();
        } else if (BeanContextProxy.class.isAssignableFrom(clazz)) {
            bean = ((BeanContextProxy) ic.instanceCreate()).getBeanContextProxy();
        } else {
            bean = null;
        }
    } catch (Exception ex) {
        bean = null;
        Exceptions.printStackTrace(ex);
    }
    if (bean != null) {
        // attaches a listener to the bean
        ((BeanContext) bean).addBeanContextMembershipListener (contextL);
    }
    updateKeys();
}
 
Example 3
Source File: GsfDataNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void loadActions(List<Action> actions, DataFolder df) throws IOException, ClassNotFoundException {
    DataObject[] dob = df.getChildren();
    int i;
    int k = dob.length;

    for (i = 0; i < k; i++) {
        InstanceCookie ic = dob[i].getCookie(InstanceCookie.class);
        if (ic == null) {
            LOG.log(Level.WARNING, "Not an action instance, or broken action: {0}", dob[i].getPrimaryFile());
            continue;
        }
        Class<?> clazz = ic.instanceClass();

        if (JSeparator.class.isAssignableFrom(clazz)) {
            actions.add(null);
        } else {
            actions.add((Action)ic.instanceCreate());
        }
    }
}
 
Example 4
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static Children getChildren(DataObject dobj, boolean noBeanInfo) {
    if (noBeanInfo) {
        return Children.LEAF;
    }
    InstanceCookie inst = (InstanceCookie)dobj.getCookie(InstanceCookie.class);
    if (inst == null) return Children.LEAF;
    try {
        Class<?> clazz = inst.instanceClass();
        if (BeanContext.class.isAssignableFrom(clazz) ||
            BeanContextProxy.class.isAssignableFrom(clazz)) {
            return new InstanceChildren ();
        } else {
            return Children.LEAF;
        }
    } catch (Exception ex) {
        return Children.LEAF;
    }
}
 
Example 5
Source File: InstanceNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
    public boolean canCopy() {
        if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) {
            return super.canCopy();
        }
        try {
            InstanceCookie ic = ic();
            if (ic == null) {
                return false;
            }
            Class<?> clazz = ic.instanceClass();
//XXX            if (XMLSettingsSupport.BrokenSettings.class.isAssignableFrom(clazz))
//                return false;
            return (!SharedClassObject.class.isAssignableFrom(clazz));
        } catch (Exception ex) {
            return false;
        }
    }
 
Example 6
Source File: InstanceNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean canCut() {
    if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) {
        return super.canCut();
    }
    try {
        InstanceCookie ic = ic();
        if (ic == null) {
            return false;
        }
        Class<?> clazz = ic.instanceClass();
        return (!SharedClassObject.class.isAssignableFrom(clazz));
    } catch (Exception ex) {
        return false;
    }
}
 
Example 7
Source File: InstanceNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Indicate whether the node may be destroyed.
 * @return tests {@link DataObject#isDeleteAllowed}
 */
@Override
public boolean canDestroy() {
    if (!getDataObject().getPrimaryFile().hasExt(XML_EXT)) {
        return super.canDestroy();
    }
    try {
        InstanceCookie ic = ic();
        if (ic == null) {
            return true;
        }
        Class<?> clazz = ic.instanceClass();
        return (!SharedClassObject.class.isAssignableFrom(clazz));
    } catch (Exception ex) {
        return false;
    }
}
 
Example 8
Source File: InstanceNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** try to find setter setName/setDisplayName, if none declared return null */
private Method getDeclaredSetter() {
    Method nameSetter = null;
    try {
        InstanceCookie ic = ic();
        if (ic == null) {
            return null;
        }
        Class<?> clazz = ic.instanceClass();
        // find the setter for the name
        try {
            nameSetter = clazz.getMethod ("setName", String.class); // NOI18N
        } catch (NoSuchMethodException e) {
            nameSetter = clazz.getMethod ("setDisplayName", String.class); // NOI18N
        }
    } catch (Exception ex) {
    }
    return nameSetter;
}
 
Example 9
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Gets the short description of this feature. */
public String getShortDescription() {
    if (noBeanInfo) return super.getShortDescription();
    
    try {
        InstanceCookie ic = ic();
        if (ic == null) {
            // it must be unrecognized instance
            return getDataObject().getPrimaryFile().toString();
        }
        
        Class<?> clazz = ic.instanceClass();
        BeanDescriptor bd = Utilities.getBeanInfo(clazz).getBeanDescriptor();
        String desc = bd.getShortDescription();
        return (desc.equals(bd.getDisplayName()))? getDisplayName(): desc;
    } catch (Exception ex) {
        return super.getShortDescription();
    }
}
 
Example 10
Source File: InstanceNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static Children getChildren(DataObject dobj, boolean noBeanInfo) {
    if (noBeanInfo) {
        return Children.LEAF;
    }
    InstanceCookie inst = dobj.getCookie(InstanceCookie.class);
    if (inst == null) {
        return Children.LEAF;
    }
    try {
        Class<?> clazz = inst.instanceClass();
        if (BeanContext.class.isAssignableFrom(clazz) ||
            BeanContextProxy.class.isAssignableFrom(clazz)) {
            return new InstanceChildren ((InstanceDataObject) dobj);
        } else {
            return Children.LEAF;
        }
    } catch (Exception ex) {
        return Children.LEAF;
    }
}
 
Example 11
Source File: Toolbar.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Accepts only cookies that can provide <code>Toolbar</code>.
 * @param cookie an <code>InstanceCookie</code> to test
 * @return true if the cookie can provide accepted instances
 */
@Override
protected InstanceCookie acceptCookie (InstanceCookie cookie)
    throws IOException, ClassNotFoundException {
    boolean is;
    boolean action;
    
    if (cookie instanceof InstanceCookie.Of) {
        InstanceCookie.Of of = (InstanceCookie.Of)cookie;
        action = of.instanceOf (Action.class);
        is = of.instanceOf (Component.class) ||
             of.instanceOf (Presenter.Toolbar.class) ||
             action;
    } else {
        Class c = cookie.instanceClass();
        action = Action.class.isAssignableFrom (c);
        is = Component.class.isAssignableFrom(c) ||
             Presenter.Toolbar.class.isAssignableFrom(c) ||
             action;
    }
    if (action) {
        cookie.instanceCreate();
    }
    
    return is ? cookie : null;
}
 
Example 12
Source File: InstanceNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Try to get display name of the bean.
 */
private String getNameForBean() {
    try {
        InstanceCookie ic = ic();
        if (ic == null) {
            // it must be unrecognized setting
            return NbBundle.getMessage(InstanceNode.class,
                "LBL_BrokenSettings"); //NOI18N
        }
        Class<?> clazz = ic.instanceClass();
        Method nameGetter;
        try {
            nameGetter = clazz.getMethod ("getName", (Class<?>[]) null); // NOI18N
            if (nameGetter.getReturnType () != String.class) {
                throw new NoSuchMethodException();
            }
        } catch (NoSuchMethodException e) {
            try {
                nameGetter = clazz.getMethod ("getDisplayName", (Class<?>[]) null); // NOI18N
                if (nameGetter.getReturnType () != String.class) {
                    throw new NoSuchMethodException();
                }
            } catch (NoSuchMethodException ee) {
                return null;
            }
        }
        Object bean = ic.instanceCreate();
        return (String) nameGetter.invoke (bean);
    } catch (Exception ex) {
        return null;
    }
}
 
Example 13
Source File: InstanceNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void propertyChange(PropertyChangeEvent evt) {
    if (evt != null && !evt.getPropertyName().equals(InstanceDataObject.PROP_COOKIE)) return;
    
    if (contextL != null && bean != null) {
        ((BeanContext) bean).removeBeanContextMembershipListener(contextL);
    }
    
    try {
        InstanceCookie ic = dobj.getCookie(InstanceCookie.class);
        if (ic == null) {
            bean = null;
            return;
        }
        Class<?> clazz = ic.instanceClass();
        if (BeanContext.class.isAssignableFrom(clazz)) {
            bean = ic.instanceCreate();
        } else if (BeanContextProxy.class.isAssignableFrom(clazz)) {
            bean = ((BeanContextProxy) dobj.instanceCreate()).getBeanContextProxy();
        } else {
            bean = null;
        }
    } catch (Exception ex) {
        bean = null;
        Exceptions.printStackTrace(ex);
    }
    if (bean != null) {
        // attaches a listener to the bean
        ((BeanContext) bean).addBeanContextMembershipListener (contextL);
    }
    updateKeys();
}
 
Example 14
Source File: InstanceSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Find context help for some instance.
* Helper method useful in nodes or data objects that provide an instance cookie;
* they may choose to supply their own help context based on this.
* All API classes which can provide help contexts will be tested for
* (including <code>HelpCtx</code> instances themselves).
* <code>JComponent</code>s are checked for an attached help ID property,
* as with {@link HelpCtx#findHelp(java.awt.Component)} (but not traversing parents).
* <p>Also, partial compliance with the JavaHelp section on JavaBeans help is implemented--i.e.,
* if a Bean in its <code>BeanInfo</code> provides a <code>BeanDescriptor</code> which
* has the attribute <code>helpID</code>, this will be returned. The value is not
* defaulted (because it would usually be nonsense and would mask a useful default
* help for the instance container), nor is the help set specification checked,
* since someone should have installed the proper help set anyway, and the APIs
* cannot add a new reference to a help set automatically.
* See <code>javax.help.HelpUtilities.getIDStringFromBean</code> for details.
* <p>Special IDs are added, corresponding to the class name, for all standard visual components.
* @param instance the instance to check for help (it is permissible for the {@link InstanceCookie#instanceCreate} to return <code>null</code>)
* @return the help context found on the instance or inferred from a Bean,
* or <code>null</code> if none was found (or it was {@link HelpCtx#DEFAULT_HELP})
 *
 *
 * @deprecated use org.openide.util.HelpCtx.findHelp (Object)
*/
@Deprecated
public static HelpCtx findHelp (InstanceCookie instance) {
    try {
        Class<?> clazz = instance.instanceClass();
        // [a.n] I have moved the code here as those components's BeanInfo do not contain helpID
        // - it is faster
        // Help on some standard components. Note that borders/layout managers do not really work here.
        if (java.awt.Component.class.isAssignableFrom (clazz) || java.awt.MenuComponent.class.isAssignableFrom (clazz)) {
            String name = clazz.getName ();
            String[] pkgs = new String[] { "java.awt.", "javax.swing.", "javax.swing.border." }; // NOI18N
            for (int i = 0; i < pkgs.length; i++) {
                if (name.startsWith (pkgs[i]) && name.substring (pkgs[i].length ()).indexOf ('.') == -1)
                    return new HelpCtx (name);
            }
        }
        Object o = instance.instanceCreate();
        if (o != null && o != instance) {
            HelpCtx h = HelpCtx.findHelp(o);
            if (h != HelpCtx.DEFAULT_HELP) {
                return h;
            }
        }
    } catch (Exception e) {
        // #63238: Ignore - generally not important context for us to be checking errors.
    }
    return null;
}
 
Example 15
Source File: KitsTrackerImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Class<?> instanceClass(FileObject f) {
    try {
        DataObject d = DataObject.find(f);
        InstanceCookie ic = d.getLookup().lookup(InstanceCookie.class);
        if (ic != null) {
            return ic.instanceClass();
        }
    } catch (Exception e) {
        // ignore
    }
    return null;
}
 
Example 16
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Try to get display name of the bean.
 */
private String getNameForBean() {
    try {
        InstanceCookie ic = ic();
        if (ic == null) {
            // it must be unrecognized setting
            return NbBundle.getMessage(SerialDataNode.class,
                "LBL_BrokenSettings"); //NOI18N
        }
        Class<?> clazz = ic.instanceClass();
        Method nameGetter;
        Class[] param = new Class [0];
        try {
            nameGetter = clazz.getMethod ("getName", param); // NOI18N
            if (nameGetter.getReturnType () != String.class) throw new NoSuchMethodException ();
        } catch (NoSuchMethodException e) {
            try {
                nameGetter = clazz.getMethod ("getDisplayName", param); // NOI18N
                if (nameGetter.getReturnType () != String.class) throw new NoSuchMethodException ();
            } catch (NoSuchMethodException ee) {
                return null;
            }
        }
        Object bean = ic.instanceCreate();
        setSettingsInstance(bean);
        return (String) nameGetter.invoke (bean);
    } catch (Exception ex) {
        // ignore
        return null;
    }
}
 
Example 17
Source File: MenuBar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Accepts only cookies that can provide a <code>Component</code>
 * or a <code>Presenter.Toolbar</code>.
 * @param cookie the instance cookie to test
 * @return true if the cookie is accepted.
 */
protected @Override InstanceCookie acceptCookie(InstanceCookie cookie)
        throws IOException, ClassNotFoundException {
    Class cls = cookie.instanceClass();
    boolean is =
            Component.class.isAssignableFrom(cls) ||
            Presenter.Toolbar.class.isAssignableFrom(cls) ||
            Action.class.isAssignableFrom(cls);
    return is ? cookie : null;
}
 
Example 18
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Indicate whether the node may be destroyed.
 * @return tests {@link DataObject#isDeleteAllowed}
 */
public boolean canDestroy() {
    try {
        InstanceCookie ic = ic();
        if (ic == null) return true;
        Class<?> clazz = ic.instanceClass();
        return (!SharedClassObject.class.isAssignableFrom(clazz));
    } catch (Exception ex) {
        return true;
    }
}
 
Example 19
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean canCut() {
    try {
        InstanceCookie ic = ic();
        if (ic == null) return false;
        Class<?> clazz = ic.instanceClass();
        return (!SharedClassObject.class.isAssignableFrom(clazz));
    } catch (Exception ex) {
        return false;
    }
}
 
Example 20
Source File: SerialDataNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean canCopy() {
    try {
        InstanceCookie ic = ic();
        if (ic == null) return false;
        Class<?> clazz = ic.instanceClass();
        return (!SharedClassObject.class.isAssignableFrom(clazz));
    } catch (Exception ex) {
        return false;
    }
}