/*
  * @see org.openthinclient.console.nodes.MyAbstractNode#getIcon(int)
  */
 @Override
 public Image getIcon(int type) {
   final DirectoryObject o = (DirectoryObject) getLookup().lookup(DirectoryObject.class);
   return IconManager.getInstance(DetailViewProvider.class, "icons")
       .getImage( //$NON-NLS-1$
           "tree." + o.getClass().getSimpleName()); // $NON-NLS-1$
 }
 /*
  * @see org.openide.nodes.Node#getActions(boolean)
  * This method checks the valid actions for the current selected directory-object.
  * If the selected directory-object is a client,
  * it will return the assigned system-actions like for example the "OpenVNCVonnectionActrion".
  */
 @Override
 public Action[] getActions(boolean context) {
   final DirectoryObject dirObject = (DirectoryObject) getLookup().lookup(DirectoryObject.class);
   final Class<? extends DirectoryObject> dirObjectClass = dirObject.getClass();
   if (dirObjectClass.equals(Client.class)) {
     if (isWritable())
       return new Action[] {
         SystemAction.get(EditAction.class),
         SystemAction.get(ClientLogAction.class),
         SystemAction.get(OpenVNCConnectionAction.class),
         SystemAction.get(DeleteNodeAction.class)
       };
     else return new Action[] {SystemAction.get(ClientLogAction.class)};
   } else if (isWritable()) {
     if (validDuplicateClassesSet.contains(dirObjectClass))
       return new Action[] {
         SystemAction.get(EditAction.class),
         SystemAction.get(DuplicateAction.class),
         SystemAction.get(DeleteNodeAction.class)
       };
     else
       return new Action[] {
         SystemAction.get(EditAction.class), SystemAction.get(DeleteNodeAction.class)
       };
   } else return new Action[] {};
 }
  /*
   * @see org.openide.nodes.AbstractNode#setName(java.lang.String)
   */
  @Override
  public void setName(String s) {
    if (null == s || s.length() == 0) {
      DialogDisplayer.getDefault()
          .notify(
              new NotifyDescriptor(
                  Messages.getString("DirObjectNode.nameInvalid", s), // $NON-NLS-1$ //$NON-NLS-2$
                  Messages.getString("DirObjectNode.cantChangeName"), // $NON-NLS-1$
                  NotifyDescriptor.DEFAULT_OPTION,
                  NotifyDescriptor.ERROR_MESSAGE,
                  null,
                  null));
      return;
    }

    final Node[] nodes = getParentNode().getChildren().getNodes();
    for (final Node node : nodes)
      if (node instanceof DirObjectNode) {
        final DirObjectNode don = (DirObjectNode) node;
        final DirectoryObject object =
            (DirectoryObject) don.getLookup().lookup(DirectoryObject.class);
        if (null != object && object.getName().equals(s)) {
          DialogDisplayer.getDefault()
              .notify(
                  new NotifyDescriptor(
                      Messages.getString("DirObjectNode.alreadyExists"), // $NON-NLS-1$
                      Messages.getString("DirObjectNode.cantChangeName"), // $NON-NLS-1$
                      NotifyDescriptor.DEFAULT_OPTION,
                      NotifyDescriptor.ERROR_MESSAGE,
                      null,
                      null));
          return;
        }
      }

    final DirectoryObject object = (DirectoryObject) getLookup().lookup(DirectoryObject.class);
    final Realm realm = (Realm) getLookup().lookup(Realm.class);

    if (null == realm || null == object)
      throw new IllegalStateException("Don't have a directory or object"); // $NON-NLS-1$

    final String oldName = object.getName();

    // reload the object so that we work on a copy.
    DirectoryObject copy = null;
    try {
      // disable caching!
      copy = realm.getDirectory().load(object.getClass(), object.getDn(), true);
    } catch (final DirectoryException e) {
      ErrorManager.getDefault().notify(e);
    }
    // copy connection descriptor for realm
    if (object instanceof Realm)
      ((Realm) copy).setConnectionDescriptor(((Realm) object).getConnectionDescriptor());

    copy.setName(s);

    try {
      realm.getDirectory().save(copy);
      // fireNameChange(oldName, s);

      // DN change. Refresh the parent.
      final Node parentNode = getParentNode();
      if (null != parentNode && parentNode instanceof Refreshable)
        ((Refreshable) parentNode).refresh();

    } catch (final DirectoryException e) {
      e.printStackTrace();

      object.setName(oldName);

      ErrorManager.getDefault()
          .annotate(
              e,
              ErrorManager.ERROR,
              null,
              Messages.getString("DirObjectNode.cantSave"),
              null,
              null); //$NON-NLS-1$
      ErrorManager.getDefault().notify(e);
    }
  }