protected final void doActionPerformed(ActionEvent evt) {
    try {
      ApplicationDiagram diag =
          (ApplicationDiagram) (ApplicationContext.getFocusManager().getFocusObject());
      Point clickPosition = getDiagramLocation(evt);
      if (clickPosition == null)
        clickPosition = GraphicUtil.rectangleGetCenter(diag.getMainView().getViewRect());
      java.awt.Image image = SRSystemClipboard.getClipboardImage();
      if (image == null) return;
      DbObject diagGo = diag.getDiagramGO();
      Db db = diagGo.getDb();

      ImageIcon icon = new ImageIcon(image);
      int imageHeigth = icon.getIconHeight();
      int imageWidth = icon.getIconWidth();

      db.beginTrans(Db.WRITE_TRANS, LocaleMgr.action.getString("pasteDiagramImage"));

      DbObject imageGo = diagGo.createComponent(DbGraphic.fImageGoDiagramImage.getMetaClass());
      imageGo.set(DbGraphic.fImageGoDiagramImage, image);
      imageGo.set(
          DbGraphic.fGraphicalObjectRectangle,
          new Rectangle(
              clickPosition.x - (imageWidth / 2),
              clickPosition.y - (imageHeigth / 2),
              imageWidth,
              imageHeigth));
      db.commitTrans();
    } catch (Exception e) {
      org.modelsphere.jack.util.ExceptionHandler.processUncatchedException(
          ApplicationContext.getDefaultMainFrame(), e);
    }
  }
  public final DynamicNode getDynamicNode(DbObject dbo, int which, boolean load)
      throws DbException {
    SrVector path = new SrVector(10);
    Db db = dbo.getDb();
    db.beginTrans(Db.READ_TRANS);
    while (dbo != null && !(dbo instanceof DbRoot)) {
      path.addElement(dbo);
      dbo = getDbParent(dbo, which);
    }
    if (db instanceof DbRAM) {
      if (DB_RAM != ((DynamicNode) getRoot()).getUserObject()) path.addElement(DB_RAM);
    } else path.addElement(db);

    DynamicNode nodeFound = (DynamicNode) getRoot();
    for (int i = path.size(); --i >= 0; ) {
      if (load) loadChildren(nodeFound);
      Object userObj = path.elementAt(i);
      dbo = null;
      DbObject dbParent = null;
      GroupParams group = GroupParams.defaultGroupParams;
      DynamicNode groupNode = null;
      DynamicNode node = null;
      if (userObj instanceof DbObject) {
        dbo = (DbObject) userObj;
        dbParent = getDbParent(dbo, which);
        group = getGroupParams(dbParent, dbo);
      }
      if (group.name == null) {
        node = getDynamicNode(nodeFound, userObj, 0);
      } else {
        groupNode = getGroupNode(nodeFound, group, 0);
        if (groupNode != null) node = getDynamicNode(groupNode, userObj, 0);
      }
      if (node == null && nodeFound.hasLoaded() && !nodeFound.isLeaf() && which == Db.NEW_VALUE) {

        SemanticalModel model = ApplicationContext.getSemanticalModel();
        boolean visible = model.isVisibleOnScreen(dbParent, dbo, Explorer.class);

        if (visible) {
          node = createPrimaryNode(dbParent, dbo);
          if (group.name == null) {
            insertNodeInto(node, nodeFound, getInsertionIndex(dbParent, dbo, nodeFound, node));
          } else {
            if (groupNode == null) {
              groupNode = createGroupNode(group);
              insertNodeInto(groupNode, nodeFound, getSortedIndex(nodeFound, groupNode));
            }
            insertNodeInto(node, groupNode, getInsertionIndex(dbParent, dbo, groupNode, node));
          }
        }
      }
      nodeFound = node;
      if (nodeFound == null) break;
    }
    db.commitTrans();
    return nodeFound;
  }
 private void insertProjects(SrVector children, Db db) throws DbException {
   db.beginTrans(Db.READ_TRANS);
   DbObject parent = db.getRoot();
   DbEnumeration dbEnum = parent.getComponents().elements(DbProject.metaClass);
   while (dbEnum.hasMoreElements())
     children.addElement(createPrimaryNode(parent, dbEnum.nextElement()));
   dbEnum.close();
   db.commitTrans();
 }
  protected final void refresh(DynamicNode parentNode) throws DbException {
    if (ApplicationContext.getFocusManager().isGuiLocked()) return;

    if (parentNode == null) return;
    if (!parentNode.hasLoaded() || parentNode.isLeaf()) {
      if (parentNode.getUserObject() instanceof DbObject)
        updateNode((DbObject) parentNode.getUserObject());
      return;
    }
    Object userObject = parentNode.getUserObject();
    if (userObject == ROOT) {
      int count = getChildCount(parentNode);
      for (int i = 0; i < count; i++) {
        DynamicNode node = (DynamicNode) getChild(parentNode, i);
        userObject = node.getUserObject();
        if (userObject == DB_RAM) refresh(node);
        else {
          if (((Db) userObject).isValid()) {
            ((Db) userObject).beginTrans(Db.READ_TRANS);
            refresh(node);
            ((Db) userObject).commitTrans();
          } else {
            removeNode(node);
          }
        }
      }
      return;
    }
    if (userObject == DB_RAM) {
      int count = getChildCount(parentNode);
      for (int i = count - 1; i >= 0; i--) {
        DynamicNode node = (DynamicNode) getChild(parentNode, i);
        Db db = ((DbObject) node.getUserObject()).getDb();
        if (!db.isValid()) {
          removeNode(node);
          continue;
        }
        db.beginTrans(Db.READ_TRANS);
        refresh(node);
        // refresh the display text for the projects - we do not want to
        // apply a full update
        // using update(dbo) because we want to preserve the expanded
        // state for projects.
        node.setDisplayText(getDisplayText(null, (DbObject) node.getUserObject()));
        db.commitTrans();
      }
      // check for missing Db
      Db[] dbs = Db.getDbs();
      for (int i = 0; i < dbs.length; i++) {
        if (!dbs[i].isValid() || !(dbs[i] instanceof DbRAM)) continue;
        DynamicNode dbNode = getDynamicNode(parentNode, dbs[i], 0);
        if (dbNode != null) continue;
        dbs[i].beginTrans(Db.READ_TRANS);
        DbEnumeration dbEnum = dbs[i].getRoot().getComponents().elements(DbProject.metaClass);
        if (dbEnum.hasMoreElements()) {
          getDynamicNode(dbEnum.nextElement(), false);
        }
        dbEnum.close();
        dbs[i].commitTrans();
      }
      return;
    }

    SrVector children = new SrVector(10);
    if (userObject instanceof Db) {
      insertProjects(children, (Db) userObject);
      children.sort();
    } else if (((DbObject) userObject).isAlive()) {
      insertComponents(children, (DbObject) userObject);
      if (childrenAreSorted((DbObject) userObject))
        children.sort(getComparator((DbObject) userObject));
    }

    DynamicNode groupNode = null;
    int index = 0;
    int iGroup = 0;
    for (int i = 0; i < children.size(); i++) {
      DynamicNode srcNode = (DynamicNode) children.elementAt(i);
      GroupParams group = srcNode.getGroupParams();
      if (group.name == null) {
        refreshNode(srcNode, parentNode, index);
        index++;
      } else {
        if (groupNode == null || !groupNode.toString().equals(group.name)) {
          if (groupNode != null) deleteNodes(groupNode, iGroup);
          groupNode = getGroupNode(parentNode, group, index);
          if (groupNode == null) {
            groupNode = createGroupNode(group);
            insertNodeInto(groupNode, parentNode, index);
          } else if (groupNode != getChild(parentNode, index)) {
            removeNodeFromParent(groupNode);
            insertNodeInto(groupNode, parentNode, index);
          }
          index++;
          iGroup = 0;
        }
        refreshNode(srcNode, groupNode, iGroup);
        iGroup++;
      }
    }
    if (groupNode != null) deleteNodes(groupNode, iGroup);
    deleteNodes(parentNode, index);

    // Refresh subnodes in a separate pass to avoid interference from
    // automatic
    // adding of a primary node when adding a secondary node.
    refreshChildren(parentNode);
  }
  protected final void doActionPerformed() {
    DbObject[] selObjs = ORActionFactory.getActiveObjects();
    if (selObjs == null || selObjs.length > 1 || selObjs.length == 0) {
      throw new RuntimeException("Expecting selection of one database");
    }
    boolean userHasCancelled = false;
    DbORDatabase database = null;
    try {
      selObjs[0].getDb().beginTrans(Db.READ_TRANS);
      DbORModel model = null;

      if (selObjs[0] instanceof DbORDatabase) {
        database = (DbORDatabase) selObjs[0];
      } else {
        model =
            (selObjs[0] instanceof DbORModel)
                ? (DbORModel) selObjs[0]
                : (DbORModel) selObjs[0].getCompositeOfType(DbORModel.metaClass);
        if (model != null) {
          if (model instanceof DbORDataModel) {
            database = ((DbORDataModel) model).getDeploymentDatabase();
          } else if (model instanceof DbORDomainModel) {
            database = ((DbORDomainModel) model).getDeploymentDatabase();
          } else if (model instanceof DbOROperationLibrary) {
            database = ((DbOROperationLibrary) model).getDeploymentDatabase();
          } // end if
        } // end if
      } // end if
      selObjs[0].getDb().commitTrans();

      if ((model != null) && (database == null)) {
        String transName =
            MessageFormat.format(
                kCreation, new Object[] {DbORDataModel.fDeploymentDatabase.getGUIName()});
        selObjs[0].getDb().beginWriteTrans(transName);
        int option =
            JOptionPane.showConfirmDialog(
                ApplicationContext.getDefaultMainFrame(),
                kNoActiveDatabase,
                ApplicationContext.getApplicationName(),
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE);
        if (option == 0) {
          DbObject composite = model.getProject();
          DbSMSTargetSystem ts = model.getTargetSystem();
          database = AnyORObject.createDatabase(composite, ts);
          if (model instanceof DbORDataModel) {
            DbORDataModel dm = (DbORDataModel) model;
            dm.setDeploymentDatabase(database);
          } else if (model instanceof DbOROperationLibrary) {
            DbOROperationLibrary ol = (DbOROperationLibrary) model;
            ol.setDeploymentDatabase(database);
          } else if (model instanceof DbORDomainModel) {
            DbORDomainModel dm = (DbORDomainModel) model;
            dm.setDeploymentDatabase(database);
          } // end if
        } else if (option == 1) {
          userHasCancelled = true;
        } // end if

        selObjs[0].getDb().commitTrans();
      } // end if
    } catch (DbException e) {
      ExceptionHandler.processUncatchedException(ApplicationContext.getDefaultMainFrame(), e);
    } // end try

    if (userHasCancelled) {
      return;
    }

    DbSMSTargetSystem target = null;
    try {
      Db db = database.getDb();
      db.beginTrans(Db.READ_TRANS);
      target = database.getTargetSystem();
      DBMSDefaultForwardWizardModel model =
          new DBMSDefaultForwardWizardModel(target.getID().intValue());
      model.setAbstractPackage(database);
      model.setDatabaseName(database.getName());
      db.commitTrans();
      Wizard wizard = new Wizard(model);
      wizard.setVisible(true);
    } catch (InstantiationException e) {
      Component parent = ApplicationContext.getDefaultMainFrame();
      Db.abortAllTrans();
      ExceptionHandler.showErrorMessage(parent, e.getMessage());
    } catch (DbException e) {
      ExceptionHandler.processUncatchedException(ApplicationContext.getDefaultMainFrame(), e);
    } catch (RuntimeException e) {
      Component parent = ApplicationContext.getDefaultMainFrame();
      String pattern = kPattern;
      String targetName = kUnknown;

      try {
        Db db = target.getDb();
        db.beginReadTrans();
        targetName = target.getName() + " " + target.getVersion();
        db.commitTrans();
      } catch (DbException ex) {
        // leave targetName's value to unknown
      }

      String msg = MessageFormat.format(pattern, new Object[] {targetName});
      Db.abortAllTrans();
      ExceptionHandler.showErrorMessage(parent, msg);
    }
  }