/*
   * @see org.openthinclient.console.Refreshable#refresh()
   */
  public void refresh() {
    final Realm realm = (Realm) getLookup().lookup(Realm.class);
    final DirectoryObject o = (DirectoryObject) getLookup().lookup(DirectoryObject.class);

    try {
      realm.getDirectory().refresh(o);
      fireCookieChange();
    } catch (final DirectoryException e) {
      ErrorManager.getDefault().notify(e);
    }
  }
  /*
   * @see org.openide.nodes.Node#destroy()
   */
  @Override
  public void destroy() throws IOException {
    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$

    try {
      realm.getDirectory().delete(object);
      super.destroy();
      final Node parentNode = getParentNode();
    } catch (final DirectoryException e) {
      ErrorManager.getDefault()
          .annotate(
              e,
              ErrorManager.EXCEPTION,
              Messages.getString("DirObjectNode.cantDelete"),
              null,
              null,
              null); //$NON-NLS-1$
      ErrorManager.getDefault().notify(e);
    }
  }
  /*
   * @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);
    }
  }