Package com.acelet.s.workflow

Source Code of com.acelet.s.workflow.WorkflowPanel

/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */

package com.acelet.s.workflow;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import com.acelet.lib.*;

import org.jgraph.JGraph;
import com.jgraph.JGraphEditor;
import com.jgraph.editor.JGraphEditorAction;
import com.jgraph.editor.JGraphEditorDiagram;
import com.jgraph.editor.JGraphEditorFactory;
import com.jgraph.editor.JGraphEditorFile;
import com.jgraph.editor.JGraphEditorKit;
import com.jgraph.editor.JGraphEditorModel;
import com.jgraph.editor.JGraphEditorTool;
import com.jgraph.editor.JGraphEditorResources;
import com.jgraph.editor.JGraphEditorSettings;
import com.jgraph.editor.factory.JGraphEditorDiagramPane;
import com.jgraph.editor.factory.JGraphEditorFactoryMethod;
import com.jgraph.editor.factory.JGraphEditorNavigator;
import com.jgraph.editor.factory.JGraphEditorToolbox;
import com.jgraph.pad.JGraphpadDiagram;
import com.jgraph.pad.JGraphpadFile;
import com.jgraph.pad.JGraphpadLibrary;
import com.jgraph.pad.action.JGraphpadCellAction;
import com.jgraph.pad.action.JGraphpadEditAction;
import com.jgraph.pad.action.JGraphpadFileAction;
import com.jgraph.pad.action.JGraphpadFormatAction;
import com.jgraph.pad.action.JGraphpadViewAction;
import com.jgraph.pad.factory.JGraphpadPane;
import com.jgraph.pad.factory.JGraphpadStatusBar;
import com.jgraph.pad.factory.JGraphpadWindowMenu;
import com.jgraph.pad.graph.JGraphpadMarqueeHandler;
import com.jgraph.pad.util.JGraphpadFocusManager;
import com.jgraph.pad.util.JGraphpadImageIcon;
import com.jgraph.pad.util.JGraphpadMouseAdapter;
import com.jgraph.pad.util.JGraphpadTreeModelAdapter;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import org.jgraph.plaf.basic.BasicGraphUI;
import org.w3c.dom.Node;

public class WorkflowPanel extends CommonPanel implements ActionListener, MenuListener,
WorkflowConstants {
  static int DEFAULT_WORKFLOW_PANEL_HEIGHT = 450;
  static int DEFAULT_WORKFLOW_PANEL_WIDTH = 650;

  static File saveRestoreDir =
    new File(System.getProperty("user.dir") + File.separator + WORKFLOW_DIR);

  static boolean isFirstTime = true;

  static boolean readOnly;

  static JGraphpad jgraphpad;
  static JGraphEditor graphEditor;

  static JGraphEditorSettings settings;
  static JGraphEditorKit graphEditorKit;
  static JGraphEditorModel graphEditorModel;
  static JGraphEditorFactory factory;

  JGraphEditorDiagramPane diagramPane;
  JGraphEditorFile graphEditorFile;
  JGraphpadDiagram graphpadDiagram;
  JGraph graph;

  String title = Phrase.get("TX_WORKFLOW");
  JPanel menuAndToolPanel;
  JMenuBar menuBar;
  JPanel toolbarPanel;
  JToolBar toolBar;
  MyToolbox toolbox;
  JPanel corePanel;
  CardLayout cardLayout;

  JMenu fileMenu;
  JMenu editMenu;

  JMenu openRecentMenu;
  JMenuItem openMenuItem;
  JMenuItem newMenuItem;
  JMenuItem saveMenuItem;
  JMenuItem saveAsMenuItem;
  JMenuItem closeMenuItem;

  JMenuItem cellPropertiesMenuItem;
  JMenuItem undoMenuItem;
  JMenuItem redoMenuItem;
  JMenuItem cutMenuItem;
  JMenuItem copyMenuItem;
  JMenuItem pasteMenuItem;
  JMenuItem deleteMenuItem;

  JButton undoButton;
  JButton redoButton;
  JButton cutButton;
  JButton copyButton;
  JButton pasteButton;
  JButton deleteButton;

  JGraphEditorAction undoAction;
  JGraphEditorAction redoAction;
  JGraphEditorAction cutAction;
  JGraphEditorAction copyAction;
  JGraphEditorAction pasteAction;
  JGraphEditorAction deleteAction;

  String xmlText;
  Spirit spirit;
  int height = DEFAULT_WORKFLOW_PANEL_HEIGHT;
  int width = DEFAULT_WORKFLOW_PANEL_WIDTH;
  boolean withOkButton;

  AbstractButton selectButton;
  AbstractButton vertexButton;
  AbstractButton edgeButton;
  AbstractButton orthogonalButton;

  String diagramName;
  boolean dataFromTemporaryFile;
  LoadFile loadFile;

  public WorkflowPanel() throws Exception {
    String helpTopic = "WorkflowWindows.WorkflowPanel";
    ShowHelp.setHelpTopic(this, helpTopic);

    readOnly = false;
    loadFile = new LoadFile();

    if (isFirstTime) {
      isFirstTime = false;
      new WorkflowProperties().printInfo();
    }
  }

  public void actionPerformed(ActionEvent event) {
    try {
      Object source = event.getSource();
      if (source == openMenuItem) {
        openDiagram();
      } else if (source == newMenuItem) {
        openNew();
      } else if (source == cellPropertiesMenuItem) {
        setCellProperties();
      } else if (source == saveMenuItem) {
        save();
      } else if (source == saveAsMenuItem) {
        saveAs();
      } else if (source == closeMenuItem) {
        close();
      } else if (source == selectButton) {
        setToolAndCursor(MyToolbox.selectTool);
      } else if (source == vertexButton) {
        setToolAndCursor(MyToolbox.vertexTool);
      } else if (source == edgeButton) {
        setToolAndCursor(MyToolbox.edgeTool);
      } else if (source == orthogonalButton) {
        setToolAndCursor(MyToolbox.orthogonalTool);
      } else {
        String command = event.getActionCommand();
        if (command.startsWith(WorkflowProperties.OPEN_RECENT_FILES)) {
          openRecent(command);
        } else
          super.actionPerformed(event);
      }
    } catch (Throwable ex) {
      InfoBox.exhibit(this, InfoBox.OK, ex);
    }
  }

  public void cancel() throws Exception {
    if (closeDiagram()) {
      clearup();
      super.cancel();
    }
  }

  boolean checkBeforeSave() throws Exception
    spirit = new Spirit(graphEditorFile);
    String msg = spirit.extract();
    if (msg.trim().length() > 0) {
      int answer = InfoBox.exhibit(this,
        Phrase.get("ER_INVALID_WORKFLOW_DATA_SAVE_ANYWAY") + "\n" + msg,
        Phrase.get("TX_ERROR"), InfoBox.YES | InfoBox.CANCEL, null,
        "Errors." + "ER_INVALID_WORKFLOW_DATA_SAVE_ANYWAY");
      if (answer == InfoBox.CANCEL)
        return false;
    }

    if (checkOrphan() == false)
      return false;
    if (checkEmpty() == false)
      return false;

    String errorMessage = spirit.checkError();
    if (errorMessage != null && errorMessage.trim().length() > 0) {
      int answer = InfoBox.exhibit(this,
        Phrase.get("ER_INVALID_WORKFLOW_DATA_SAVE_ANYWAY") + "\n" + errorMessage,
        Phrase.get("TX_ERROR"), InfoBox.YES | InfoBox.CANCEL, null,
        "Errors." + "ER_INVALID_WORKFLOW_DATA_SAVE_ANYWAY");
      if (answer == InfoBox.CANCEL)
        return false;
    }
    return true;
  }

  boolean checkBeforeStoreInDatabase() throws Exception
    spirit = new Spirit(graphEditorFile);
    String msg = spirit.extract();
    if (msg.trim().length() > 0) {
      int answer = InfoBox.exhibit(this,
        Phrase.get("ER_INVALID_WORKFLOW_DATA") + "\n" + msg,
        Phrase.get("TX_ERROR"), InfoBox.OK, null,
        "Errors." + "ER_INVALID_WORKFLOW_DATA");
      return false;
    }

    if (checkOrphan() == false)
      return false;
    if (checkEmpty() == false)
      return false;

    String errorMessage = spirit.checkError();
    if (errorMessage != null && errorMessage.trim().length() > 0) {
      int answer = InfoBox.exhibit(this,
        Phrase.get("ER_INVALID_WORKFLOW_DATA") + "\n" + errorMessage,
        Phrase.get("TX_ERROR"), InfoBox.OK, null,
        "Errors." + "ER_INVALID_WORKFLOW_DATA");
      return false;
    }
    return true;
  }

  boolean checkEmpty() throws Exception
    if (spirit.cellVector.size() == 0) {
      InfoBox.exhibit(this, Phrase.get("ER_EMPTY_DATA"),
        Phrase.get("TX_ERROR"), InfoBox.OK, null,
        "Errors." + "ER_EMPTY_DATA");
        return false;
    }
    return true;
  }

  Object checkFileVersion(Object aFile) throws Exception {
    String comingVersion = null;
    String correctVersion = new WorkflowFile().version;

    if ((aFile instanceof WorkflowFile))
      comingVersion = ((WorkflowFile) aFile).version;
    else
      throw new Exception(Phrase.get("ER_INVALID_FILE"));

    if (correctVersion.equals(comingVersion) == false) {
      String error = Phrase.get("ER_WRONG_VERSION") + "\n" + Phrase.get("TX_EXPECT") +
        " " + correctVersion + ", " + Phrase.get("TX_FOUND") + " " + comingVersion;
      throw new Exception(Phrase.get("ER_INVALID_FILE") + ": " + error);
    }
    return aFile;
  }

  boolean checkOrphan() throws Exception
   
    Vector orphanCellVector = spirit.getOrphanCellVector();
    if (orphanCellVector.size() > 0) {
      int answer = InfoBox.exhibit(this, Phrase.get("ER_THERE_ARE_ORPHAN_ACTIONS_REMOVE"),
        Phrase.get("TX_ERROR"), InfoBox.YES | InfoBox.NO | InfoBox.CANCEL, null,
        "Errors." + "ER_THERE_ARE_ORPHAN_ACTIONS_REMOVE");
      if (answer == InfoBox.YES) {
        removeOrphanCells(orphanCellVector);
        spirit = new Spirit(graphEditorFile);
        spirit.extract();
      } else if (answer == InfoBox.CANCEL)
        return false;
    }
    return true;
  }

  void clearup() throws Exception {
    toolbox.reset();

    DefaultMutableTreeNode root = (DefaultMutableTreeNode) graphEditorModel.getRoot();
    root.removeAllChildren();
    graphEditorFile = null;
  }

  boolean close() throws Exception {
    if (graphEditorFile != null && graphEditorFile.isModified()) {
      int answer = InfoBox.exhibit(this, Phrase.get("TX_WORKFLOW_MODIFIED_SAVE"),
        Phrase.get("TX_WARNING"), InfoBox.YES | InfoBox.NO | InfoBox.CANCEL, null,
        "Messages." + "TX_WORKFLOW_MODIFIED_SAVE");
      if (answer == InfoBox.CANCEL)
        return false;
      else if (answer == InfoBox.YES)
        save();
    }

    clearup();
    SwingUtilities.windowForComponent(this).setVisible(false);
    return true;
  }

  boolean closeDiagram() throws Exception {
    if (graphEditorFile != null && graphEditorFile.isModified()) {
      int answer = InfoBox.exhibit(this, Phrase.get("TX_WORKFLOW_MODIFIED_DISCARD"),
        Phrase.get("TX_WARNING"), InfoBox.YES | InfoBox.NO, null,
        "Messages." + "TX_WORKFLOW_MODIFIED_DISCARD");
      if (answer == InfoBox.NO)
        return false;
    }
    return true;
  }

  public JGraphEditorDiagramPane createDiagramPane(JGraphpadDiagram graphpadDiagram) {
    diagramPane = factory.createDiagramPane(graphpadDiagram);
    diagramPane.setPreferredSize(new Dimension(width, height));
    graph = diagramPane.getGraph();
    return diagramPane;
  }

  public JGraphEditorDiagramPane createDiagramPane(String fileName) {
    graphpadDiagram = new JGraphpadDiagram(fileName);
    return createDiagramPane(graphpadDiagram);
  }

  void defautEdit() {
    if (!graph.isSelectionEmpty())
      graph.startEditingAtCell(graph.getSelectionCell());
  }

  JGraphEditorFile createNewFile(String fileName) throws Exception {
    return new WorkflowFile(fileName);
  }

  public String extractWorkflowXmlText() throws Exception {
    return xmlText = new MemoryFileToXmlText(graphEditorModel, graphEditorFile).fileToText();
  }

  public String getWorkflowXmlText() throws Exception {
    return xmlText;
  }

  public JPanel init(String graphXmlText, boolean withOkButton) throws Exception {
    this.xmlText = graphXmlText;
    this.withOkButton = withOkButton;

    if (getReady())
      return this;
    else
      return null;
  }

  void insertOpenRecent(String fileName) throws Exception {
    for (int i = 0; i < openRecentMenu.getMenuComponentCount(); i++) {
      JMenuItem item = (JMenuItem) openRecentMenu.getMenuComponent(i);
      String text = item.getText();
      if (text.equals(fileName)) {
        openRecentMenu.remove(item);
      }
    }

    makeOpenRecentMenuItem(fileName, 0);
    writeOpenRecent();
  }

  protected boolean makeMainPanel() throws Exception {
    mainPanel = new JPanel(new BorderLayout());
   
    if (jgraphpad == null) {
      jgraphpad = new JGraphpad();
   
      Hashtable args = new Hashtable();
      graphEditor = jgraphpad.createEditor(args);
      settings = graphEditor.getSettings();
      graphEditorKit = graphEditor.getKit();
      graphEditorModel = graphEditor.getModel();
      factory = graphEditor.getFactory();
    }

    menuBar = makeMenuBar();
    makeTools();
   
    menuAndToolPanel = new JPanel(new GridLayout(2, 1));           
    menuAndToolPanel.add(menuBar);
    menuAndToolPanel.add(toolbarPanel);
    mainPanel.add("North", menuAndToolPanel);
   
    cardLayout = new CardLayout();
    corePanel = new JPanel(cardLayout);
    mainPanel.add("Center", corePanel);
   
    setup();

    if (xmlText != null && xmlText.trim().length() > 0)
      openDiagramFromData(xmlText);
    else
      openNew();

    return true;
  }

  public JMenuBar makeMenuBar() {
    fileMenu = new JMenu(Phrase.get("TX_FILE"));
    fileMenu.addMenuListener(this);

    makeOpenRecentMenu();

    ImageIcon newIcon = loadFile.receiveImageIcon("new.gif");
    ImageIcon openIcon = loadFile.receiveImageIcon("open.gif");
    ImageIcon saveIcon = loadFile.receiveImageIcon("save.gif");
    ImageIcon closeIcon = loadFile.receiveImageIcon("close.gif");
    ImageIcon propertiesIcon = loadFile.receiveImageIcon("properties.gif");

    newMenuItem = new JMenuItem(Phrase.get("TX_NEW"), newIcon);
    newMenuItem.addActionListener(this);

    openMenuItem = new JMenuItem(Phrase.get("TX_OPEN"), openIcon);
    openMenuItem.addActionListener(this);

    saveMenuItem = new JMenuItem(Phrase.get("TX_SAVE"), saveIcon);
    saveMenuItem.addActionListener(this);

    saveAsMenuItem = new JMenuItem(Phrase.get("TX_SAVEAS"));
    saveAsMenuItem.addActionListener(this);

    closeMenuItem = new JMenuItem(Phrase.get("TX_CLOSE"), closeIcon);
    closeMenuItem.addActionListener(this);

    fileMenu.add(newMenuItem);
    fileMenu.add(openMenuItem);
    fileMenu.add(openRecentMenu);
    fileMenu.addSeparator();
    fileMenu.add(saveMenuItem);
    fileMenu.add(saveAsMenuItem);
    fileMenu.addSeparator();
    fileMenu.add(closeMenuItem);

    editMenu = new JMenu(Phrase.get("TX_EDIT"));
    editMenu.addMenuListener(this);

    cellPropertiesMenuItem = new JMenuItem(Phrase.get("TX_PROPERTIES"), propertiesIcon);
    cellPropertiesMenuItem.addActionListener(this);

    undoAction = factory.getAction(JGraphpadEditAction.NAME_UNDO);
    undoMenuItem = new JMenuItem(undoAction);
    factory.configureActionItem(undoMenuItem, undoAction);

    redoAction = factory.getAction(JGraphpadEditAction.NAME_REDO);
    redoMenuItem = new JMenuItem(redoAction);
    factory.configureActionItem(redoMenuItem, redoAction);

    cutAction = factory.getAction(JGraphpadEditAction.NAME_CUT);
    cutMenuItem = new JMenuItem(cutAction);
    factory.configureActionItem(cutMenuItem, cutAction);

    copyAction = factory.getAction(JGraphpadEditAction.NAME_COPY);
    copyMenuItem = new JMenuItem(copyAction);
    factory.configureActionItem(copyMenuItem, copyAction);

    pasteAction = factory.getAction(JGraphpadEditAction.NAME_PASTE);
    pasteMenuItem = new JMenuItem(pasteAction);
    factory.configureActionItem(pasteMenuItem, pasteAction);
    deleteAction = factory.getAction(JGraphpadEditAction.NAME_DELETE);
    deleteMenuItem = new JMenuItem(deleteAction);
    factory.configureActionItem(deleteMenuItem, deleteAction);

    editMenu.add(cellPropertiesMenuItem);
    editMenu.addSeparator();
    editMenu.add(undoMenuItem);
    editMenu.add(redoMenuItem);
    editMenu.addSeparator();
    editMenu.add(cutMenuItem);
    editMenu.add(copyMenuItem);
    editMenu.add(pasteMenuItem);
    editMenu.add(deleteMenuItem);

    menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    return menuBar;
  }

  void makeTools() {
    ImageIcon selectIcon = loadFile.receiveImageIcon("com/jgraph/pad/images/select.gif");
    ImageIcon rectangleIcon = loadFile.receiveImageIcon("com/jgraph/pad/images/rectangle.gif");
    ImageIcon edgeIcon = loadFile.receiveImageIcon("com/jgraph/pad/images/edge.gif");
    ImageIcon orthogonalIcon = loadFile.receiveImageIcon("com/jgraph/pad/images/orthogonal.gif");

    selectButton = new JButton(selectIcon);
    selectButton.addActionListener(this);
    selectButton.setFocusable(false);
    selectButton.setMargin(new Insets(1, 1, 1, 1));
    selectButton.setToolTipText(Phrase.get("TX_SELECT"));

    vertexButton = new JButton(rectangleIcon);
    vertexButton.addActionListener(this);
    vertexButton.setFocusable(false);
    vertexButton.setMargin(new Insets(1, 1, 1, 1));
    vertexButton.setToolTipText(Phrase.get("TX_ACTION"));

    edgeButton = new JButton(edgeIcon);
    edgeButton.addActionListener(this);
    edgeButton.setFocusable(false);
    edgeButton.setMargin(new Insets(1, 1, 1, 1));
    edgeButton.setToolTipText(Phrase.get("TX_TRANSITION"));

    orthogonalButton = new JButton(orthogonalIcon);
    orthogonalButton.addActionListener(this);
    orthogonalButton.setFocusable(false);
    orthogonalButton.setMargin(new Insets(1, 1, 1, 1));
    orthogonalButton.setToolTipText(Phrase.get("TX_ORTHOGONAL_TRANSITION"));

    toolbox = new MyToolbox();
    toolbox.add(selectButton);
    toolbox.add(vertexButton);
    toolbox.add(edgeButton);
    toolbox.add(orthogonalButton);

    undoButton = new JButton(undoAction);
    factory.configureActionItem(undoButton, undoAction);
    undoButton.setText("");
    undoButton.setToolTipText(Phrase.get("TX_UNDO"));

    redoButton = new JButton(redoAction);
    factory.configureActionItem(redoButton, redoAction);
    redoButton.setText("");
    redoButton.setToolTipText(Phrase.get("TX_REDO"));

    cutButton = new JButton(cutAction);
    factory.configureActionItem(cutButton, cutAction);
    cutButton.setText("");
    cutButton.setToolTipText(Phrase.get("TX_CUT"));

    copyButton = new JButton(copyAction);
    factory.configureActionItem(copyButton, copyAction);
    copyButton.setText("");
    copyButton.setToolTipText(Phrase.get("TX_COPY"));

    pasteButton = new JButton(pasteAction);
    factory.configureActionItem(pasteButton, pasteAction);
    pasteButton.setText("");
    pasteButton.setToolTipText(Phrase.get("TX_PASTE"));

    deleteButton = new JButton(deleteAction);
    factory.configureActionItem(deleteButton, deleteAction);
    deleteButton.setText("");
    deleteButton.setToolTipText(Phrase.get("TX_DELETE"));

    toolBar = new JToolBar();
    toolBar.add(cutButton);
    toolBar.add(copyButton);
    toolBar.add(pasteButton);
    toolBar.add(deleteButton);
    toolBar.addSeparator();
    toolBar.add(undoButton);
    toolBar.add(redoButton);

    toolbarPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    toolbarPanel.add(toolbox);
    toolbarPanel.add(toolBar);
  }

  void makeOpenRecentMenu() {
    try {
      openRecentMenu = new JMenu(Phrase.get("TX_OPEN_RECENT"));
      String nameString = new WorkflowProperties().get(WorkflowProperties.OPEN_RECENT_FILES);
      if (nameString != null && nameString.trim().length() > 0) {
        StringTokenizer st = new StringTokenizer(nameString, "\t");
        while (st.hasMoreTokens()) {
          String text = st.nextToken();
          makeOpenRecentMenuItem(text, -1);
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void makeOpenRecentMenuItem(String text, int position) {
    JMenuItem menuItem = new JMenuItem(text);
    menuItem.addActionListener(this);
    menuItem.setActionCommand(WorkflowProperties.OPEN_RECENT_FILES + "\t" + text);
    if (position == 0)
      openRecentMenu.insert(menuItem, 0);
    else
      openRecentMenu.add(menuItem);
  }

  public void menuCanceled(MenuEvent e) {
  }

  public void menuDeselected(MenuEvent e) {
  }

  public void menuSelected(MenuEvent e) {
    Object source = e.getSource();
    if (source == editMenu) {
      Object[] selectedCells = graph.getSelectionCells();
      if (selectedCells != null && selectedCells.length > 0)
        cellPropertiesMenuItem.setEnabled(true);
      else
        cellPropertiesMenuItem.setEnabled(false);
    } else if (source == fileMenu) {
      if (dataFromTemporaryFile || graphEditorFile.isNew())
        saveMenuItem.setEnabled(false);
      else
        saveMenuItem.setEnabled(true);
    }
  }

  protected void ok() throws Exception {
    if (transferData(TRANSFER_FROM_DLG)) {
      answer = OK;
      SwingUtilities.windowForComponent(this).setVisible(false);
    }
  }

  void openDiagram() throws Exception {
    if (closeDiagram() == false)
      return;

    String description = fileExtension + " " + Phrase.get("TX_FILE");
    SingletonFileFilter sff = new SingletonFileFilter(fileExtension, description);
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(sff);
    chooser.setDialogTitle(Phrase.get("TX_OPEN"));
    chooser.setCurrentDirectory(saveRestoreDir);
    chooser.setApproveButtonText(Phrase.get("TX_OPEN"));
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);

    int retval = chooser.showOpenDialog(Globals.masterWindow);
    if (retval == JFileChooser.APPROVE_OPTION) {
      File theFile = chooser.getSelectedFile();
      if(theFile != null) {
        saveRestoreDir = chooser.getCurrentDirectory();
        String fileName = theFile.getAbsolutePath();
        dataFromTemporaryFile = false;
        openDiagram(fileName);
      }
    }
  }

  boolean openDiagram(String fileName) throws Exception {
    diagramName = fileName;

   
    Object aFile = readFile(fileName);
    graphEditorFile = (JGraphEditorFile) checkFileVersion(aFile);
    graphEditorFile.setFilename(fileName);
    graphEditorModel.addRoot(graphEditorFile);

    graphpadDiagram = Spirit.findDiagram(graphEditorFile);
    diagramPane = createDiagramPane(graphpadDiagram);

    refresh();

    graphEditorFile.setModified(false);

    if (dataFromTemporaryFile == false)
      insertOpenRecent(fileName);
    return true;
  }

  boolean openDiagramFromData(String xmlText) throws Exception {
    dataFromTemporaryFile = true;
    File tmpFile = File.createTempFile("workflow", "tmp");

    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream(tmpFile);
      GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);
      gzipOutputStream.write(xmlText.getBytes());
      gzipOutputStream.finish();
    } finally {
      if (fileOutputStream != null)
        fileOutputStream.close();
    }

    String fileName = tmpFile.getAbsolutePath();

    if (openDiagram(fileName)) {
      graphEditorFile.setModified(false);
      return true;
    } else
      return false;
  }

  boolean openNew() throws Exception {
    if (closeDiagram() == false)
      return false;

    String fileName = Kit.makeNowMMDDHHMMSS() + "." + fileExtension;
    diagramName = fileName;
    dataFromTemporaryFile = false;

    graphEditorFile = createNewFile(fileName);
    graphEditorModel.addRoot(graphEditorFile);
    graphEditorFile.setNew(true);

    diagramPane = createDiagramPane(fileName);
    graphpadDiagram = (JGraphpadDiagram) diagramPane.getDiagram();
    graphEditorModel.addChild(graphpadDiagram, graphEditorFile);

    refresh();
   
    graphEditorFile.setModified(false);

    return true;
  }

  void openRecent(String command) throws Exception {
    if (closeDiagram() == false)
      return;

    int index = command.indexOf("\t");
    String fileName = command.substring(index + 1);
    dataFromTemporaryFile = false;
    openDiagram(fileName);
  }

  protected boolean prepareData() throws Exception {
    return true;
  }

  Object readFile(String fileName) throws Exception {
    InputStream inputStream = null;
    try {
      inputStream = new GZIPInputStream(new FileInputStream(fileName));
      return graphEditorModel.readObject(inputStream);
    } finally {
      if (inputStream != null)
        inputStream.close();
    }
  }

  void refresh() throws Exception {
    corePanel.add(diagramPane, diagramName);
    for (int i = 0; i < corePanel.getComponentCount(); i++) {
      Component component = corePanel.getComponent(i);
      if (component != diagramPane)
        corePanel.remove(component);
    }
    cardLayout.show(corePanel, diagramName);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          graph = diagramPane.getGraph();
          graph.requestFocus();
        }
      });
  }

  void removeOrphanCells(Vector orphanCellVector) {
    Object[] cells = orphanCellVector.toArray();
    spirit.graphpadGraphModel.remove(cells);
  }

  void save() throws Exception {
    save(graphEditorFile.getFilename());
  }

  void save(String fileName) throws Exception {
    if (checkBeforeSave() == false)
      return;

    OutputStream out = null;
    try {
      out = new GZIPOutputStream(new FileOutputStream(fileName));
      graphEditorModel.writeObject(graphEditorFile, out);
      out.flush();
    } finally {
      if (out != null)
        out.close();
    }

    graphEditorModel.setModified(graphEditorFile, false);
    graphEditorFile.setNew(false);
  }

  void saveAs() throws Exception {
    String description = fileExtension + " " + Phrase.get("TX_FILE");
    SingletonFileFilter sff = new SingletonFileFilter(fileExtension, description);
    JFileChooser chooser = new JFileChooser();
    chooser.setFileFilter(sff);
    chooser.setDialogTitle(Phrase.get("TX_SAVEAS"));
    chooser.setCurrentDirectory(saveRestoreDir);
    chooser.setApproveButtonText(Phrase.get("TX_SAVE"));
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);

    int retval = chooser.showSaveDialog(Globals.masterWindow);
    if (retval == JFileChooser.APPROVE_OPTION) {
      File theFile = chooser.getSelectedFile();
      if(theFile != null) {
        saveRestoreDir = chooser.getCurrentDirectory();
        String fileName = theFile.getAbsolutePath();
        if (fileName.endsWith("." + fileExtension) == false)
          fileName = fileName + "." + fileExtension;
        if (new File(fileName).exists()) {
          int answer = InfoBox.exhibit(this, Phrase.get("TX_FILE_EXISTS_OVER_WRITE_IT"),
            Phrase.get("TX_WARNING"), InfoBox.YES | InfoBox.CANCEL, null,
            "Messages." + "TX_FILE_EXISTS_OVER_WRITE_IT");
          if (answer != InfoBox.YES)
            return;
        }

        save(fileName);
        insertOpenRecent(fileName);
      }
    }
  }

  void setActionProperties() throws Exception {
    ActionPropertiesPanel actionPropertiesPanel = new ActionPropertiesPanel(graph);
    if (actionPropertiesPanel.getReady() == false)
      return;
   
    Window window = SwingUtilities.windowForComponent(this);
    if (window instanceof JDialog)
      new CommonDlg((JDialog) window, Phrase.get("TX_ACTION_PROPERTIES"), actionPropertiesPanel);
    else
      new CommonDlg((JFrame) window, Phrase.get("TX_ACTION_PROPERTIES"), actionPropertiesPanel);
  }

  void setCellProperties() throws Exception {
    if (SelectedCell.isSelectedCellActionCell(graph))
      setActionProperties();
    else
      defautEdit();
  }

  void setToolAndCursor(JGraphEditorTool tool) {
    JGraph graph = diagramPane.getGraph();
    toolbox.setSelectionTool(tool, graph);
    graph.requestFocus();
  }

  void setup() {
  }

  protected boolean transferData(int direction) throws Exception {
    switch(direction) {
    case TRANSFER_FROM_DLG:
      if (checkBeforeStoreInDatabase() == false)
        return false;
      extractWorkflowXmlText();
      clearup();
      break;
    case TRANSFER_TO_DLG:
      if (withOkButton == false) {
        buttonPanel.setVisible(false);
      } else {
        closeMenuItem.setVisible(false);
      }
      graphEditorFile.setModified(false);
      break;
    }
    return true;
  }

  public void windowClosed() {
    try {
      clearup();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  void writeOpenRecent() throws Exception {
    int openRecentMax = WorkflowProperties.openRecentMax;

    StringBuffer buffer = new StringBuffer();
    int size = openRecentMenu.getMenuComponentCount();
    if (size > openRecentMax)
      size = openRecentMax;
    for (int i = 0; i < size; i++) {
      JMenuItem menuItem = (JMenuItem) openRecentMenu.getMenuComponent(i);
      buffer.append(menuItem.getText() + "\t");
    }
    buffer.setLength(buffer.length() - 1);
   
    new WorkflowProperties().put(WorkflowProperties.OPEN_RECENT_FILES, buffer.toString());
  }
}
TOP

Related Classes of com.acelet.s.workflow.WorkflowPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.