/** This method loads the names of the state groups that have been defined so far */
 private static void populateStateGroupsList() {
   StateLabelManager.updateAvailableStateGroups();
   String[] stateGrpNames =
       StateLabelManager.getStateNames(StateLabelManager.availableStateGroups);
   DefaultListModel model = new DefaultListModel();
   for (String stateName : stateGrpNames) {
     model.addElement(stateName);
   }
   StateLabelManager.stateGroupsList.setModel(model);
 }
 public static void update() {
   // clear the new label text field
   StateLabelManager.stateLabelTextField.setText("");
   // refresh state group array
   StateLabelManager.availableStateGroups =
       ApplicationSettings.getApplicationView().getCurrentPetriNetView().getStateGroups();
   // refresh dropdown menu
   StateLabelManager.populateStateLabelDropdown();
   // refresh the list of state group names
   StateLabelManager.populateLists();
 }
  public static void stateLabelManagerDialog() {
    // build popup
    StateLabelManager.popupDialog =
        new JDialog(QueryManager.getEditor(), "State Label Manager", true);
    StateLabelManager.popupDialog.setMinimumSize(new Dimension(730, 450));
    Container contentPane = StateLabelManager.popupDialog.getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    contentPane.add(StateLabelManager.getStateLabelManagementPanel(true));
    contentPane.add(StateLabelManager.getStatesAssignmentManagerPanel());
    JPanel okButtonPanel = new JPanel();
    okButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton okButton = new JButton("OK");
    StateLabelManager.popupDialog.getRootPane().setDefaultButton(okButton);
    ActionListener okButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            StateLabelManager.clearAll();
            StateLabelManager.popupDialog.dispose();
          }
        };
    okButton.addActionListener(okButtonListener);
    okButtonPanel.add(okButton);
    contentPane.add(okButtonPanel);

    // take care of popup closing
    StateLabelManager.popupDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    StateLabelManager.popupDialog.addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosing(final WindowEvent we) {
            StateLabelManager.clearAll();
            StateLabelManager.popupDialog.dispose();
          }
        });

    // load in the latest info
    StateLabelManager.update();

    // show popup
    StateLabelManager.popupDialog.pack();
    StateLabelManager.popupDialog.setLocationRelativeTo(null);
    StateLabelManager.popupDialog.setVisible(true);
  }
  /**
   * Creates the state label assignment manager
   *
   * @return
   */
  private static JPanel getStatesAssignmentManagerPanel() {
    JPanel stateAssignmentManagerPanel = new JPanel();
    stateAssignmentManagerPanel.setBorder(
        (new TitledBorder(new EtchedBorder(), "State Label Definition")));
    stateAssignmentManagerPanel.setLayout(
        new BoxLayout(stateAssignmentManagerPanel, BoxLayout.Y_AXIS));

    // panel to hold the state definitions and state label panels, as well
    // as the assignment buttons
    JPanel stateAssignmentPanel = new JPanel();
    stateAssignmentPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

    // Create button panel
    JPanel stateButtonsPanel = new JPanel();
    JButton createStateDefinitionButton = new JButton("Define State Group");
    ActionListener createStateDefinitionButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            // check whether places have been defined on the underlying
            // model
            boolean okToProceed = QueryManager.getData().checkCurrentData("States");
            if (okToProceed) {
              // bring up the state editor popup
              StateGroupEditor stateEditor = new StateGroupEditor();
              PetriNetView pnModel =
                  ApplicationSettings.getApplicationView().getCurrentPetriNetView();
              stateEditor.addState(pnModel);
              StateLabelManager.update();
            }
          }
        };
    createStateDefinitionButton.addActionListener(createStateDefinitionButtonListener);
    JButton editStateDefinitionButton = new JButton("Edit State Group");
    ActionListener editStateDefinitionButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            int[] selectedIndices = StateLabelManager.stateGroupsList.getSelectedIndices();
            if (selectedIndices.length > 0) {
              if (selectedIndices.length > 1) {
                JOptionPane.showMessageDialog(
                    QueryManager.getEditor().getContentPane(),
                    "To edit a state group, please select a single state \n"
                        + "group from the list of states defined on the model.",
                    "Warning",
                    JOptionPane.INFORMATION_MESSAGE);
              } else {
                int selectedIndex = selectedIndices[0];
                // bring up the state editor popup
                String selectedStateName =
                    (String)
                        StateLabelManager.stateGroupsList.getModel().getElementAt(selectedIndex);
                StateGroup selectedState = StateLabelManager.getStateGroup(selectedStateName);
                StateGroupEditor stateEditor = new StateGroupEditor();
                PetriNetView pnModel =
                    ApplicationSettings.getApplicationView().getCurrentPetriNetView();
                stateEditor.editState(pnModel, selectedState);
                StateLabelManager.update();
              }
            } else {
              JOptionPane.showMessageDialog(
                  QueryManager.getEditor().getContentPane(),
                  "To edit a state group, please select a single state \n"
                      + "group from the list of states defined on the model.",
                  "Warning",
                  JOptionPane.INFORMATION_MESSAGE);
            }
          }
        };
    editStateDefinitionButton.addActionListener(editStateDefinitionButtonListener);
    JButton deleteStateDefinitionButton = new JButton("Delete State Group");
    ActionListener deleteStateDefinitionButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            int[] stateGroupsListSelectedIndices =
                StateLabelManager.stateGroupsList.getSelectedIndices();
            if (stateGroupsListSelectedIndices.length > 0) {
              PetriNetView pnModel =
                  ApplicationSettings.getApplicationView().getCurrentPetriNetView();
              for (int selectionIndex : stateGroupsListSelectedIndices) {
                String selectedStateName =
                    (String)
                        StateLabelManager.stateGroupsList.getModel().getElementAt(selectionIndex);
                StateGroup state = StateLabelManager.getStateGroup(selectedStateName);
                // remove state from _dataLayer
                pnModel.removeStateGroup(state);
                // remove all references to the state in all state
                // labels
                QueryManager.getData().removeStateFromAllLabels(selectedStateName);
              }
              StateLabelManager.update();
            } else {
              JOptionPane.showMessageDialog(
                  QueryManager.getEditor().getContentPane(),
                  "To delete a state group, please select it from the list \n"
                      + "of defined state groups on the left panel.",
                  "Warning",
                  JOptionPane.INFORMATION_MESSAGE);
            }
          }
        };
    deleteStateDefinitionButton.addActionListener(deleteStateDefinitionButtonListener);
    stateButtonsPanel.add(createStateDefinitionButton);
    stateButtonsPanel.add(editStateDefinitionButton);
    stateButtonsPanel.add(deleteStateDefinitionButton);

    // panel for the state definitions
    JPanel stateDefinitionsPanel = new JPanel();
    stateDefinitionsPanel.setBorder(
        (new TitledBorder(new EtchedBorder(), "State Groups Defined On The Model")));
    StateLabelManager.stateGroupsList.setLayoutOrientation(JList.VERTICAL);
    StateLabelManager.stateGroupsList.setSelectionModel(new ToggleSelectionModel());
    StateLabelManager.stateGroupsList.setVisibleRowCount(-1);
    StateLabelManager.populateStateGroupsList();
    JScrollPane stateDefinitionsListScroller = new JScrollPane(StateLabelManager.stateGroupsList);
    stateDefinitionsListScroller.setPreferredSize(new Dimension(300, 200));
    stateDefinitionsPanel.add(stateDefinitionsListScroller);
    stateAssignmentPanel.add(stateDefinitionsPanel);

    // panel for the assignment buttons
    JPanel assignmentButtonsPanel = new JPanel();
    assignmentButtonsPanel.setLayout(new BoxLayout(assignmentButtonsPanel, BoxLayout.Y_AXIS));
    JButton assignStateToStateLabelButton = new JButton("->");
    ActionListener assignStateToStateLabelButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (StateLabelManager.currentStateLabel != null) {
              int[] stateGroupsListSelectedIndices =
                  StateLabelManager.stateGroupsList.getSelectedIndices();
              if (stateGroupsListSelectedIndices.length > 0) {
                for (int selectionIndex : stateGroupsListSelectedIndices) {
                  String selectedStateName =
                      (String)
                          StateLabelManager.stateGroupsList.getModel().getElementAt(selectionIndex);
                  // assign state to state label
                  QueryManager.getData()
                      .addStateLabel(StateLabelManager.currentStateLabel, selectedStateName);
                }
                StateLabelManager.update();
              }
            } else {
              JOptionPane.showMessageDialog(
                  QueryManager.getEditor().getContentPane(),
                  "Before you can assign state groups to a state label, you have \n"
                      + "to specify the state label. Please choose a label from the \n"
                      + "dropdown menu.\n",
                  "Warning",
                  JOptionPane.INFORMATION_MESSAGE);
            }
          }
        };
    assignStateToStateLabelButton.addActionListener(assignStateToStateLabelButtonListener);
    assignmentButtonsPanel.add(assignStateToStateLabelButton);
    JButton removeStateFromStateLabelButton = new JButton("<-");
    ActionListener removeStateFromStateLabelButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (StateLabelManager.currentStateLabel != null) {
              int[] statesAssignedSelectedIndices =
                  StateLabelManager.statesAssignedToLabelList.getSelectedIndices();
              if (statesAssignedSelectedIndices.length > 0) {
                for (int selectionIndex : statesAssignedSelectedIndices) {
                  String selectedStateName =
                      (String)
                          StateLabelManager.statesAssignedToLabelList
                              .getModel()
                              .getElementAt(selectionIndex);
                  // remove state from label
                  QueryManager.getData()
                      .removeStateFromStateLabel(
                          StateLabelManager.currentStateLabel, selectedStateName);
                }
              }
              StateLabelManager.update();
            }
          }
        };
    removeStateFromStateLabelButton.addActionListener(removeStateFromStateLabelButtonListener);
    assignmentButtonsPanel.add(removeStateFromStateLabelButton);
    stateAssignmentPanel.add(assignmentButtonsPanel);

    // panel for the states assigned to the state label
    JPanel statesAssignedToStateLabelPanel = new JPanel();
    statesAssignedToStateLabelPanel.setBorder(
        (new TitledBorder(new EtchedBorder(), "State Groups Assigned To Label")));
    StateLabelManager.statesAssignedToLabelList.setLayoutOrientation(JList.VERTICAL);
    StateLabelManager.statesAssignedToLabelList.setSelectionModel(new ToggleSelectionModel());
    StateLabelManager.statesAssignedToLabelList.setVisibleRowCount(-1);
    StateLabelManager.populateStatesAssignedToLabelList(null);
    JScrollPane statesAssignedListScroller =
        new JScrollPane(StateLabelManager.statesAssignedToLabelList);
    statesAssignedListScroller.setPreferredSize(new Dimension(300, 200));
    statesAssignedToStateLabelPanel.add(statesAssignedListScroller);
    stateAssignmentPanel.add(statesAssignedToStateLabelPanel);

    // Add components to panel
    stateAssignmentManagerPanel.add(stateButtonsPanel);
    stateAssignmentManagerPanel.add(stateAssignmentPanel);

    return stateAssignmentManagerPanel;
  }
  /**
   * This method launches a popup that enables the assignment of a state label to a States node
   *
   * @param nodeInput
   */
  public static void stateLabelAssignmentDialog(final StatesNode nodeInput) {
    // update our local copy of the node object. Need this to be able to
    // refer to the node from the ActionListeners
    StateLabelManager.node = nodeInput;

    // build popup
    StateLabelManager.popupDialog =
        new JDialog(QueryManager.getEditor(), "State Label Assignment", true);
    StateLabelManager.popupDialog.setMinimumSize(new Dimension(730, 450));
    Container contentPane = StateLabelManager.popupDialog.getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    contentPane.add(StateLabelManager.getStateLabelManagementPanel(false));
    contentPane.add(StateLabelManager.getStatesAssignmentManagerPanel());
    String[] buttonNames = {"OK", "Cancel"};
    ActionListener okButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            if (StateLabelManager.currentStateLabel != null) {
              if (StateLabelManager.stateLabelHasStateGroupAssigned()) {
                // assign state label to node
                StateLabelManager.node.setStateLabel(StateLabelManager.currentStateLabel);
                StateLabelManager.node.setNodeLabel(StateLabelManager.currentStateLabel);

                if (MacroManager.getEditor() == null)
                  QueryManager.getData().updateNode(StateLabelManager.node);
                else MacroManager.getEditor().updateNode(StateLabelManager.node);

                StateLabelManager.clearAll();
                StateLabelManager.popupDialog.dispose();
              } else {
                JOptionPane.showMessageDialog(
                    QueryManager.getEditor().getContentPane(),
                    "Please ensure that the state label you have selected refers to \n"
                        + "at least one state group before trying to assign the label to \n"
                        + "the States node.",
                    "Warning",
                    JOptionPane.ERROR_MESSAGE);
              }
            } else {
              JOptionPane.showMessageDialog(
                  QueryManager.getEditor().getContentPane(),
                  "Please select a state label from the dropdown menu \n"
                      + "that you wish to assign to the node.",
                  "Warning",
                  JOptionPane.ERROR_MESSAGE);
            }
          }
        };
    ActionListener cancelButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            StateLabelManager.clearAll();
            StateLabelManager.popupDialog.dispose();
          }
        };
    ActionListener[] buttonListeners = {okButtonListener, cancelButtonListener};
    contentPane.add(new ButtonBar(buttonNames, buttonListeners));

    // take care of popup closing
    StateLabelManager.popupDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    StateLabelManager.popupDialog.addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowClosing(final WindowEvent we) {
            StateLabelManager.clearAll();
            StateLabelManager.popupDialog.dispose();
          }
        });

    // load in the latest info
    StateLabelManager.update();

    // show popup
    StateLabelManager.popupDialog.pack();
    StateLabelManager.popupDialog.setLocationRelativeTo(null);
    StateLabelManager.popupDialog.setVisible(true);
  }
 /**
  * This method loads the names of the state groups and adds them to the source / destination
  * JLists
  */
 private static void populateLists() {
   StateLabelManager.populateStateGroupsList();
   StateLabelManager.populateStatesAssignedToLabelList(StateLabelManager.currentStateLabel);
 }
  /**
   * Creates the top bit of the interface, which is choosing a state label name
   *
   * @param withStateLabelControlButtons
   * @return
   */
  private static JPanel getStateLabelManagementPanel(boolean withStateLabelControlButtons) {
    JPanel stateLabelManagementPanel = new JPanel();
    stateLabelManagementPanel.setLayout(new BoxLayout(stateLabelManagementPanel, BoxLayout.Y_AXIS));
    stateLabelManagementPanel.setBorder((new TitledBorder(new EtchedBorder(), "State Label")));

    JPanel stateLabelSelectionPanel = new JPanel();
    stateLabelSelectionPanel.setLayout(new SpringLayout());
    if (StateLabelManager.node != null) {
      String assignedStateLabel = StateLabelManager.node.getStateLabel();
      if (assignedStateLabel != null) StateLabelManager.setCurrentStateLabel(assignedStateLabel);
    }
    StateLabelManager.populateStateLabelDropdown();
    ActionListener stateLabelsComboListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            String stateLabel = (String) cb.getSelectedItem();
            if (!stateLabel.equals("-- Select --")) {
              // indicate that this is the state label that we're dealing
              // with now
              StateLabelManager.setCurrentStateLabel(stateLabel);
              StateLabelManager.populateLists();
            }
          }
        };
    StateLabelManager.stateLabelDropdown.addActionListener(stateLabelsComboListener);
    stateLabelSelectionPanel.add(StateLabelManager.stateLabelDropdown);
    SpringLayoutUtilities.makeCompactGrid(stateLabelSelectionPanel, 1, 1, 6, 6, 6, 6);
    stateLabelManagementPanel.add(stateLabelSelectionPanel);

    ActionListener createStateLabelButtonListener =
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            StateLabelManager.createNewLabelPopup();
          }
        };

    if (!withStateLabelControlButtons) {
      // just a create state label button - used when right-clicking on a
      // StatesNode
      JPanel stateLabelButtonPanel = new JPanel();
      stateLabelButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
      String[] buttonNames = {"Create State Label"};
      ActionListener[] buttonListeners = {createStateLabelButtonListener};
      stateLabelButtonPanel.add(new ButtonBar(buttonNames, buttonListeners));
      stateLabelManagementPanel.add(stateLabelButtonPanel);
    } else {
      // have a delete and edit state label button as well - used when the
      // state label manager is
      // invoked from the Tools menu
      JPanel stateLabelButtonPanel = new JPanel();
      stateLabelButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
      String[] buttonNames = {"Create State Label", "Edit State Label", "Delete State Label"};
      ActionListener editStateLabelButtonListener =
          new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
              String selectedStateLabel =
                  (String) StateLabelManager.stateLabelDropdown.getSelectedItem();
              if (!selectedStateLabel.equals("-- Select --")
                  && !selectedStateLabel.equals("CREATE NEW STATE LABEL")) {
                StateLabelManager.createEditLabelPopup();
              }
            }
          };
      ActionListener deleteStateLabelButtonListener =
          new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
              String selectedStateLabel =
                  (String) StateLabelManager.stateLabelDropdown.getSelectedItem();
              if (!selectedStateLabel.equals("-- Select --")
                  && !selectedStateLabel.equals("CREATE NEW STATE LABEL")) {
                QueryManager.getData().removeStateLabel(selectedStateLabel);
                StateLabelManager.setCurrentStateLabel(null);
                StateLabelManager.update();
              }
            }
          };
      ActionListener[] buttonListeners = {
        createStateLabelButtonListener, editStateLabelButtonListener, deleteStateLabelButtonListener
      };
      stateLabelButtonPanel.add(new ButtonBar(buttonNames, buttonListeners));
      stateLabelManagementPanel.add(stateLabelButtonPanel);
    }

    return stateLabelManagementPanel;
  }