public int computeDocumentOffset(int line, int column) throws BadLocationException { if (line < 0 || column < 0) throw new BadLocationException("Negative line/col", -1); Element lineElement = editor.getDocument().getDefaultRootElement().getElement(line - 1); int beginLineOffset = lineElement.getStartOffset(); int endLineOffset = lineElement.getEndOffset(); String text = editor.getDocument().getText(beginLineOffset, endLineOffset - beginLineOffset); int parserChar = 1; int documentChar = 0; while (parserChar < column) { if (documentChar < text.length() && text.charAt(documentChar) == '\t') { parserChar += 8; documentChar += 1; } else { parserChar += 1; documentChar += 1; } } return beginLineOffset + documentChar; }
/** * Loads the model editor with the text from the given stream reader, discards any previous * content in the editor. * * @param reader A character stream reader * @param object An object describing the stream; this might be a string, a File, a URL, etc. * @throws IOException Thrown if there was an IO error reading the input stream. */ public void read(Reader reader, Object object) throws IOException { editor.getDocument().removeUndoableEditListener(undoManager); editor.read(reader, object); // For some unknown reason the listeners have to be added both here // and in the constructor, if they're not added here the editor won't // be listening. editor.getDocument().addDocumentListener(this); editor.getDocument().addUndoableEditListener(undoManager); }
public void delete() { try { editor .getDocument() .remove( editor.getSelectionStart(), editor.getSelectionEnd() - editor.getSelectionStart()); } catch (BadLocationException ex) { // GUIPrism.getGUI().getMultiLogger().logMessage(PrismLogLevel.PRISM_ERROR, ex.getMessage()); } }
private void applyFontSize() { Document document = myEditorPane.getDocument(); if (!(document instanceof StyledDocument)) { return; } StyledDocument styledDocument = (StyledDocument) document; if (myFontSizeStyle == null) { myFontSizeStyle = styledDocument.addStyle("active", null); } EditorColorsManager colorsManager = EditorColorsManager.getInstance(); EditorColorsScheme scheme = colorsManager.getGlobalScheme(); StyleConstants.setFontSize(myFontSizeStyle, scheme.getQuickDocFontSize().getSize()); if (Registry.is("documentation.component.editor.font")) { StyleConstants.setFontFamily(myFontSizeStyle, scheme.getEditorFontName()); } styledDocument.setCharacterAttributes(0, document.getLength(), myFontSizeStyle, false); }
void doSourceFileUpdate() { BufferedReader bufferIn = null; String str1 = new String(); String strContent = new String(); String sliderValue = new String(); String newLine = System.getProperty("line.separator"); StringBuffer strBuf = new StringBuffer(); RandomAccessFile access = null; String tempFile = "temp" + round + ".html"; File fp = new File(tempFile); boolean firstChoice = true; int posFound = 0; int i = 0; PSlider slider; // round += 1; // Read the original source file to input buffer try { bufferIn = new BufferedReader(new FileReader(exampleSource)); } catch (FileNotFoundException fe) { System.err.println("Example Source File not found " + fe); // System.exit(-1);; } // get the first line of the buffer. try { str1 = bufferIn.readLine(); } catch (IOException ie) { System.err.println("Error reading line from the buffer " + ie); // System.exit(-1);; } // Transfer the whole content of the input buffer to the string try { do strContent += str1 + newLine; while ((str1 = bufferIn.readLine()) != null); } catch (IOException ie) { System.err.println("Error readding content of the input buffer " + ie); // System.exit(-1);; } // do the replacement. // First having to update the code part that is active in this section before // doing variables updated. Look at the current active radiobutton to make decision if (secondBox.isSelected() == true) firstChoice = false; if (firstChoice == true) { String tempStr = new String(); strBuf = new StringBuffer(strContent); posFound = strContent.indexOf(textStr2); tempStr = "<font color=blue>/***********" + newLine + textStr2 + newLine + "***********/</font>"; strBuf.replace(posFound, posFound + textStr2.length(), tempStr); strContent = new String(strBuf); } else { String tempStr = new String(); strBuf = new StringBuffer(strContent); posFound = strContent.indexOf(textStr1); tempStr = "<font color=blue>/***********" + newLine + textStr1 + newLine + "***********/</font>"; strBuf.replace(posFound, posFound + textStr1.length(), tempStr); strContent = new String(strBuf); } for (i = COMPONENTS; i > 0; i--) { // get the current value of slider slider = (PSlider) vSlider.elementAt(i - 1); sliderValue = slider.getValue(); // construct the search string str1 = "JPOT$" + i; // get the position of the search string in the content string. while ((posFound = strContent.indexOf(str1)) != -1) { strBuf = new StringBuffer(strContent); strBuf.replace(posFound, posFound + str1.length(), sliderValue); strContent = new String(strBuf); } } boolean delete = fp.delete(); try { fp.createNewFile(); } catch (IOException ie) { System.err.println("Couldn't create the new file " + ie); // System.exit(-1);; } try { access = new RandomAccessFile(fp, "rw"); } catch (IOException ie) { System.err.println("Error in accessing the file " + ie); // System.exit(-1);; } try { access.writeBytes(strContent); access.close(); } catch (IOException ie) { System.err.println("Error in writing to file " + ie); // System.exit(-1);; } try { textPane.getDocument().putProperty(Document.StreamDescriptionProperty, null); URL url = new URL("file:" + tempFile + "#gohere"); textPane.setPage(url); delete = fp.delete(); } catch (IOException ie) { System.err.println("Can not get the example html file " + ie); // System.exit(-1);; } }
/** * 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"); } }); }