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);
  }
  /**
   * 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);
    }
  }
  /** Helper method to initialize the actions used for the buttons. */
  private void initActions() {

    /*actionUndo = new AbstractAction() {
    	public void actionPerformed(ActionEvent ae) {
    		try {
    			// do redo
    			undoManager.undo();

    			// notify undo manager/toolbar of change
    			GUIPrism.getGUI().notifyEventListeners(
    					new GUIClipboardEvent(GUIClipboardEvent.UNDOMANAGER_CHANGE,
    							GUIPrism.getGUI().getFocussedPlugin().getFocussedComponent()));
    		} catch (CannotUndoException ex) {
    			//GUIPrism.getGUI().getMultiLogger().logMessage(PrismLogLevel.PRISM_ERROR, ex.getMessage());
    		}
    	}
    };
    actionUndo.putValue(Action.LONG_DESCRIPTION, "Undo the most recent action.");
    actionUndo.putValue(Action.NAME, "Undo");
    actionUndo.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("smallUndo.png"));

    actionRedo = new AbstractAction() {
    	public void actionPerformed(ActionEvent ae) {
    		try {
    			// do redo
    			undoManager.redo();

    			// notify undo manager/toolbar of change
    			GUIPrism.getGUI().notifyEventListeners(
    					new GUIClipboardEvent(GUIClipboardEvent.UNDOMANAGER_CHANGE,
    							GUIPrism.getGUI().getFocussedPlugin().getFocussedComponent()));
    		} catch (CannotRedoException ex) {
    			//GUIPrism.getGUI().getMultiLogger().logMessage(PrismLogLevel.PRISM_ERROR, ex.getMessage());
    		}
    	}
    };


    actionRedo.putValue(Action.LONG_DESCRIPTION, "Redos the most recent undo");
    actionRedo.putValue(Action.NAME, "Redo");
    actionRedo.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("smallRedo.png"));
    */
    actionJumpToError =
        new AbstractAction() {
          public void actionPerformed(ActionEvent ae) {
            jumpToError();
          }
        };

    actionJumpToError.putValue(Action.NAME, "Jump to error");
    actionJumpToError.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("tinyError.png"));
    actionJumpToError.putValue(
        Action.ACCELERATOR_KEY,
        KeyStroke.getKeyStroke(
            KeyEvent.VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

    // search and replace action
    actionSearch =
        new AbstractAction() {
          public void actionPerformed(ActionEvent ae) {
            /*
                		// System.out.println("search button pressed");
                		if (GUIMultiModelHandler.isDoingSearch()) {

            } else {
            	try {
            		GUIMultiModelHandler.setDoingSearch(true);
            		FindReplaceForm.launch(GUIPrism.getGUI().getMultiModel());
            	} catch (PluginNotFoundException pnfe) {
            		GUIPrism.getGUI().getMultiLogger().logMessage(prism.log.PrismLogLevel.PRISM_ERROR,
            				pnfe.getMessage());
            	}
            }
            */
          }
        };
    actionSearch.putValue(Action.LONG_DESCRIPTION, "Opens a find and replace dialog.");
    // actionSearch.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("find.png"));
    actionSearch.putValue(Action.NAME, "Find/Replace");
    // actionSearch.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R,
    // Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

    insertDTMC =
        new AbstractAction() {
          public void actionPerformed(ActionEvent ae) {
            int caretPosition = editor.getCaretPosition();
            try {
              editor.getDocument().insertString(caretPosition, "dtmc", new SimpleAttributeSet());
            } catch (BadLocationException ble) {
              // todo log?
            }
          }
        };

    insertDTMC.putValue(
        Action.LONG_DESCRIPTION, "Marks this model as a \"Discrete-Time Markov Chain\"");
    // actionSearch.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("find.png"));
    insertDTMC.putValue(Action.NAME, "Probabilistic (DTMC)");

    insertCTMC =
        new AbstractAction() {
          public void actionPerformed(ActionEvent ae) {
            int caretPosition = editor.getCaretPosition();
            try {
              editor.getDocument().insertString(caretPosition, "ctmc", new SimpleAttributeSet());
            } catch (BadLocationException ble) {
              // todo log?
            }
          }
        };

    insertCTMC.putValue(
        Action.LONG_DESCRIPTION, "Marks this model as a \"Continous-Time Markov Chain\"");
    // actionSearch.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("find.png"));
    insertCTMC.putValue(Action.NAME, "Stochastic (CTMC)");

    insertMDP =
        new AbstractAction() {
          public void actionPerformed(ActionEvent ae) {
            int caretPosition = editor.getCaretPosition();
            try {
              editor.getDocument().insertString(caretPosition, "mdp", new SimpleAttributeSet());
            } catch (BadLocationException ble) {
              // todo log?
            }
          }
        };

    insertMDP.putValue(
        Action.LONG_DESCRIPTION, "Marks this model as a \"Markov Decision Process\"");
    // actionSearch.putValue(Action.SMALL_ICON, GUIPrism.getIconFromImage("find.png"));
    insertMDP.putValue(Action.NAME, "Non-deterministic (MDP)");
  }
  /**
   * 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");
              }
            });
  }