/** Returns the next available session name in the series untitled1.tet, untitled2.tet, etc. */
  private String getNewSessionName() {
    String base = "untitled";
    String suffix = ".tet";
    int i = 0; // Sequence 1, 2, 3, ...

    loop:
    while (true) {
      i++;

      String name = base + i + suffix;

      for (Object _o : framesMap.keySet()) {
        if (_o instanceof SessionEditor) {
          SessionEditor sessionEditor = (SessionEditor) _o;
          SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
          SessionWrapper sessionWrapper = workbench.getSessionWrapper();

          if (sessionWrapper.getName().equals(name)) {
            continue loop;
          }
        }
      }

      return name;
    }
  }
  /**
   * Adds a component to the middle layer of the desktop--that is, the layer for session node
   * editors. Note: The comp is a SessionEditor
   */
  public void addSessionEditor(SessionEditorIndirectRef editorRef) {
    SessionEditor editor = (SessionEditor) editorRef;

    JInternalFrame frame = new TetradInternalFrame(null);

    frame.getContentPane().add(editor);
    framesMap.put(editor, frame);
    editor.addPropertyChangeListener(this);

    // Set the "small" size of the frame so that it has sensible
    // bounds when the users unmazimizes it.
    Dimension fullSize = desktopPane.getSize();
    int smallSize = Math.min(fullSize.width - MARGIN, fullSize.height - MARGIN);
    Dimension size = new Dimension(smallSize, smallSize);
    setGoodBounds(frame, desktopPane, size);
    desktopPane.add(frame);

    // Set the frame to be maximized. This step must come after the frame
    // is added to the desktop. -Raul. 6/21/01
    try {
      frame.setMaximum(true);
    } catch (Exception e) {
      throw new RuntimeException("Problem setting frame to max: " + frame);
    }

    desktopPane.setLayer(frame, 0);
    frame.moveToFront();
    frame.setTitle(editor.getName());
    frame.setVisible(true);

    setMainTitle(editor.getName());
  }
  /**
   * Queries the user as to whether they would like to save their sessions.
   *
   * @return true if the transaction was ended successfully, false if not (that is, canceled).
   */
  public boolean closeAllSessions() {
    while (existsSession()) {
      SessionEditor sessionEditor = getFrontmostSessionEditor();
      SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
      SessionWrapper wrapper = workbench.getSessionWrapper();

      if (!wrapper.isSessionChanged()) {
        closeFrontmostSession();
        continue;
      }

      String name = sessionEditor.getName();

      int ret =
          JOptionPane.showConfirmDialog(
              JOptionUtils.centeringComp(),
              "Would you like to save the changes you made to " + name + "?",
              "Advise needed...",
              JOptionPane.YES_NO_CANCEL_OPTION);

      if (ret == JOptionPane.NO_OPTION) {
        closeFrontmostSession();
        continue;
      } else if (ret == JOptionPane.CANCEL_OPTION) {
        return false;
      }

      SaveSessionAsAction action = new SaveSessionAsAction();
      action.actionPerformed(
          new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Dummy close action"));

      if (!action.isSaved()) {
        int ret2 =
            JOptionPane.showConfirmDialog(
                JOptionUtils.centeringComp(),
                "This session was not saved. Close session and continue anyway?",
                "Advise needed...",
                JOptionPane.OK_CANCEL_OPTION);

        if (ret2 == JOptionPane.CANCEL_OPTION) {
          return false;
        }
      }

      closeFrontmostSession();
    }

    return true;
  }
  public void closeEmptySessions() {
    JInternalFrame[] frames = desktopPane.getAllFramesInLayer(0);

    for (JInternalFrame frame : frames) {
      Object o = frame.getContentPane().getComponents()[0];

      if (o instanceof SessionEditor) {
        SessionEditor sessionEditor = (SessionEditor) o;
        SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
        Graph graph = workbench.getGraph();
        if (graph.getNumNodes() == 0) {
          frame.dispose();
        }
      }
    }
  }
  public boolean existsSessionByName(String filename) {
    JInternalFrame[] allFrames = desktopPane.getAllFramesInLayer(0);

    for (JInternalFrame allFrame : allFrames) {
      Object o = allFrame.getContentPane().getComponents()[0];

      if (o instanceof SessionEditor) {
        SessionEditor editor = (SessionEditor) o;

        String editorName = editor.getName();
        if (editorName.equals(filename)) {
          return true;
        }
      }
    }

    return false;
  }
예제 #6
0
  /** Performs the action of saving a session to a file. */
  public void actionPerformed(ActionEvent e) {

    // Get the frontmost SessionWrapper.
    SessionEditorIndirectRef sessionEditorRef =
        DesktopController.getInstance().getFrontmostSessionEditor();
    SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
    SessionEditorWorkbench workbench = sessionEditor.getSessionWorkbench();
    SessionWrapper sessionWrapper = workbench.getSessionWrapper();
    TetradMetadata metadata = new TetradMetadata();

    // Select the file to save this to.
    File file =
        EditorUtils.getSaveFile(
            sessionEditor.getName(),
            "tet",
            JOptionUtils.centeringComp(),
            true,
            "Save Session As...");

    if (file == null) {
      this.saved = false;
      return;
    }

    if ((DesktopController.getInstance().existsSessionByName(file.getName())
        && !(sessionWrapper.getName().equals(file.getName())))) {
      this.saved = false;
      JOptionPane.showMessageDialog(
          JOptionUtils.centeringComp(),
          "Another session by that name is currently open. Please "
              + "\nclose that session first.");
      return;
    }

    sessionWrapper.setName(file.getName());
    sessionEditor.setName(file.getName());

    // Save it.
    try {
      FileOutputStream out = new FileOutputStream(file);
      ObjectOutputStream objOut = new ObjectOutputStream(out);
      objOut.writeObject(metadata);
      objOut.writeObject(sessionWrapper);
      out.close();

      FileInputStream in = new FileInputStream(file);
      ObjectInputStream objIn = new ObjectInputStream(in);
      objIn.readObject();

      sessionWrapper.setSessionChanged(false);
      sessionWrapper.setNewSession(false);
      this.saved = true;
    } catch (Exception e2) {
      this.saved = false;
      e2.printStackTrace();
      JOptionPane.showMessageDialog(
          JOptionUtils.centeringComp(), "An error occurred while attempting to save the session.");
    }

    DesktopController.getInstance().putMetadata(sessionWrapper, metadata);
    sessionEditor.firePropertyChange("name", null, file.getName());
  }