/**
   * Mouse Listener methods.
   *
   * <p>spv
   */
  public void mouseTriggered(MouseEvent me) {
    if (me.isPopupTrigger()) {
      actionJumpToError.setEnabled(parseError != null && parseError.hasLineNumbers());
      ((GUIMultiModel) handler.getGUIPlugin()).doEnables();

      contextPopup.show(me.getComponent(), me.getX(), me.getY());
    }
  }
  /**
   * Helper method that initializes the items for the context menu. This menu will include cut,
   * copy, paste, undo/redo, and find/replace functionality.
   */
  private void initContextMenu() {

    contextPopup = new JPopupMenu();
    // Edit menu stuff
    contextPopup.add(GUIPrism.getClipboardPlugin().getUndoAction());
    contextPopup.add(GUIPrism.getClipboardPlugin().getRedoAction());
    contextPopup.add(new JSeparator());
    contextPopup.add(GUIPrism.getClipboardPlugin().getCutAction());
    contextPopup.add(GUIPrism.getClipboardPlugin().getCopyAction());
    contextPopup.add(GUIPrism.getClipboardPlugin().getPasteAction());
    contextPopup.add(GUIPrism.getClipboardPlugin().getDeleteAction());
    contextPopup.add(new JSeparator());
    contextPopup.add(GUIPrism.getClipboardPlugin().getSelectAllAction());
    contextPopup.add(new JSeparator());
    // Model menu stuff
    contextPopup.add(((GUIMultiModel) handler.getGUIPlugin()).getParseModel());
    contextPopup.add(((GUIMultiModel) handler.getGUIPlugin()).getBuildModel());
    contextPopup.add(new JSeparator());
    contextPopup.add(((GUIMultiModel) handler.getGUIPlugin()).getExportMenu());
    contextPopup.add(((GUIMultiModel) handler.getGUIPlugin()).getViewMenu());
    contextPopup.add(((GUIMultiModel) handler.getGUIPlugin()).getComputeMenu());
    // contextPopup.add(actionJumpToError);
    // contextPopup.add(actionSearch);

    if (editor.getContentType().equals("text/prism")) {

      JMenu insertMenu = new JMenu("Insert elements");
      JMenu insertModelTypeMenu = new JMenu("Model type");
      insertMenu.add(insertModelTypeMenu);
      JMenu insertModule = new JMenu("Module");
      insertMenu.add(insertModule);
      JMenu insertVariable = new JMenu("Variable");
      insertMenu.add(insertVariable);

      insertModelTypeMenu.add(insertDTMC);
      insertModelTypeMenu.add(insertCTMC);
      insertModelTypeMenu.add(insertMDP);
      // contextPopup.add(new JSeparator());
      // contextPopup.add(insertMenu);
    }
  }
  public boolean canDoClipBoardAction(Action action) {
    if (action == GUIPrism.getClipboardPlugin().getPasteAction()) {
      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      return (clipboard.getContents(null) != null);
    } else if (action == GUIPrism.getClipboardPlugin().getCutAction()
        || action == GUIPrism.getClipboardPlugin().getCopyAction()
        || action == GUIPrism.getClipboardPlugin().getDeleteAction()) {
      return (editor.getSelectedText() != null);
    } else if (action == GUIPrism.getClipboardPlugin().getSelectAllAction()) {
      return true;
    }

    return handler.canDoClipBoardAction(action);
  }
 /**
  * Notifies the GUI that the document has been modified.
  *
  * @param event Event generated by the change in the document.
  */
 public void removeUpdate(DocumentEvent event) {
   if (handler != null) handler.hasModified(true);
 }
  /**
   * Constructor, initialises the editor components.
   *
   * @param initialText The initial text to be displayed in the editor.
   * @param handler The GUI handler for this component.
   */
  public GUITextModelEditor(String initialText, GUIMultiModelHandler handler) {
    this.handler = handler;
    setLayout(new BorderLayout());

    // Setup the editor with it's custom editor kits. To switch between
    // editor kits just use setContentType() for the desired content type.
    editor =
        new JEditorPane() {
          @Override
          public String getToolTipText(MouseEvent event) {
            if (parseError != null) {
              try {
                int offset = this.viewToModel(new Point(event.getX(), event.getY()));

                int startOffset =
                    computeDocumentOffset(parseError.getBeginLine(), parseError.getBeginColumn());
                int endOffset =
                    computeDocumentOffset(parseError.getEndLine(), parseError.getEndColumn()) + 1;

                if (offset >= startOffset && offset <= endOffset) return parseError.getMessage();
              } catch (BadLocationException e) {
              }
            }

            return null;
          }
        };

    editor.setToolTipText("dummy");

    editor.setEditorKitForContentType("text/prism", new PrismEditorKit(handler));
    editor.setEditorKitForContentType("text/pepa", new PepaEditorKit(handler));
    // The default editor kit is the Prism one.
    editor.setContentType("text/prism");
    editor.setBackground(Color.white);
    editor.addMouseListener(editorMouseListener);
    editor.setEditable(true);
    editor.setText(initialText);
    editor.getDocument().addDocumentListener(this);
    editor.addCaretListener(
        new CaretListener() {
          public void caretUpdate(CaretEvent e) {
            GUITextModelEditor.this
                .handler
                .getGUIPlugin()
                .getSelectionChangeHandler()
                .notifyListeners(new GUIEvent(1));
          }
        });
    editor.getDocument().putProperty(PlainDocument.tabSizeAttribute, new Integer(4));

    editor.addMouseListener(this);
    errorHighlightPainter =
        new DefaultHighlighter.DefaultHighlightPainter(new Color(255, 192, 192));
    undoManager = new GUIUndoManager(GUIPrism.getGUI());
    undoManager.setLimit(200);

    // Setup the scrollpane
    editorScrollPane = new JScrollPane(editor);
    add(editorScrollPane, BorderLayout.CENTER);
    gutter = new GUITextModelEditorGutter(editor);

    // Get the 'show line numbers' setting to determine
    // if the line numbers should be shown.
    showLineNumbersSetting =
        handler
            .getGUIPlugin()
            .getPrism()
            .getSettings()
            .getBoolean(PrismSettings.MODEL_SHOW_LINE_NUMBERS);
    if (showLineNumbersSetting) {
      editorScrollPane.setRowHeaderView(gutter);
    }

    // Add a Prism settings listener to catch changes made to the
    // 'show line numbers' setting.
    handler
        .getGUIPlugin()
        .getPrism()
        .getSettings()
        .addSettingsListener(
            new PrismSettingsListener() {
              public void notifySettings(PrismSettings settings) {
                // Check if the setting has changed.
                if (settings.getBoolean(PrismSettings.MODEL_SHOW_LINE_NUMBERS)
                    != showLineNumbersSetting) {
                  showLineNumbersSetting = !showLineNumbersSetting;
                  if (showLineNumbersSetting) {
                    editorScrollPane.setRowHeaderView(gutter);
                  } else {
                    editorScrollPane.setRowHeaderView(null);
                  }
                }
              }
            });

    // initialize the actions for the context menu
    initActions();

    // method to initialize the context menu popup
    initContextMenu();

    InputMap inputMap = editor.getInputMap();
    inputMap.clear();

    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_undo");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_undo");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_redo");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_selectall");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_D, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_delete");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_cut");
    inputMap.put(
        KeyStroke.getKeyStroke(
            KeyEvent.VK_Z,
            Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
                | java.awt.event.InputEvent.SHIFT_MASK),
        "prism_redo");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_paste");
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "prism_jumperr");

    ActionMap actionMap = editor.getActionMap();
    actionMap.put("prism_undo", GUIPrism.getClipboardPlugin().getUndoAction());
    actionMap.put("prism_redo", GUIPrism.getClipboardPlugin().getRedoAction());
    actionMap.put("prism_selectall", GUIPrism.getClipboardPlugin().getSelectAllAction());
    actionMap.put("prism_cut", GUIPrism.getClipboardPlugin().getCutAction());
    actionMap.put("prism_copy", GUIPrism.getClipboardPlugin().getCopyAction());
    actionMap.put("prism_paste", GUIPrism.getClipboardPlugin().getPasteAction());
    actionMap.put("prism_delete", GUIPrism.getClipboardPlugin().getDeleteAction());
    actionMap.put("prism_jumperr", actionJumpToError);

    // Attempt to programmatically allow all accelerators
    /*ArrayList plugins = ((GUIMultiModel)handler.getGUIPlugin()).getGUI().getPlugins();
    Iterator it = plugins.iterator();

    while (it.hasNext())
    {
    	GUIPlugin plugin = ((GUIPlugin)it.next());
    	System.out.println(plugin.getName());
    	JMenu firstMenu = plugin.getMenu();

    	Stack<MenuElement> menuStack = new Stack<MenuElement>();

    	menuStack.add(firstMenu);

    	while (!menuStack.empty())
    	{
    		MenuElement menu = menuStack.pop();

    		if (menu instanceof JMenuItem)
    		{
    			JMenuItem menuItem = ((JMenuItem)menu);

    			KeyStroke accelerator = menuItem.getAccelerator();
    			Action action = menuItem.getAction();

    			if (action != null && accelerator != null && menuItem.getText() != null)
    			{
    				System.out.println(menuItem.getText() + " " + menuItem.getName());
    				inputMap.put(accelerator, "prism_" + menuItem.getText());
    				actionMap.put("prism_" + menuItem.getText(), action);
    			}
    		}

    		MenuElement[] subelements = menu.getSubElements();

    		if (subelements != null)
    		{
    			for (int i = 0; i < subelements.length; i++)
    				menuStack.push(subelements[i]);
    		}
    	}
    }*/

    editor.getDocument().addUndoableEditListener(undoManager);
    editor
        .getDocument()
        .addUndoableEditListener(
            new UndoableEditListener() {
              public void undoableEditHappened(UndoableEditEvent e) {
                System.out.println("adding undo edit");
              }
            });
  }