@Override public void focusGained(final FocusEvent e) { final JTextComponent component = getComponent(); if (!component.isEnabled() || !component.isEditable()) { super.focusGained(e); return; } mFocused = true; if (!shouldSelectAllOnFocus) { shouldSelectAllOnFocus = true; super.focusGained(e); return; } if (isMultiLineEditor) { super.focusGained(e); return; } final int end = component.getDocument().getLength(); final int dot = getDot(); final int mark = getMark(); if (dot == mark) { if (dot == 0) { component.setCaretPosition(end); component.moveCaretPosition(0); } else if (dot == end) { component.setCaretPosition(0); component.moveCaretPosition(end); } } super.focusGained(e); }
/** Select or grow image when clicked. */ public void mousePressed(MouseEvent e) { Dimension size = fComponent.getSize(); if (e.getX() >= size.width - 7 && e.getY() >= size.height - 7 && getSelectionState() == 2) { // Click in selected grow-box: if (DEBUG) System.out.println("ImageView: grow!!! Size=" + fWidth + "x" + fHeight); Point loc = fComponent.getLocationOnScreen(); fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight); fGrowProportionally = e.isShiftDown(); } else { // Else select image: fGrowBase = null; JTextComponent comp = (JTextComponent) fContainer; int start = fElement.getStartOffset(); int end = fElement.getEndOffset(); int mark = comp.getCaret().getMark(); int dot = comp.getCaret().getDot(); if (e.isShiftDown()) { // extend selection if shift key down: if (mark <= start) comp.moveCaretPosition(end); else comp.moveCaretPosition(start); } else { // just select image, without shift: if (mark != start) comp.setCaretPosition(start); if (dot != end) comp.moveCaretPosition(end); } } }
/** * @param baseURL may be <code>null</code> * @see PropertyPanel.PropertyField#setText */ void setText(String text, String baseURL, DocumentListener documentListener) { // textComponent.getDocument().removeDocumentListener(documentListener); // if (editorType == EDITOR_TYPE_STYLED) { // // --- set content type --- ((JEditorPane) textComponent).setContentType("text/html"); // // --- set base URL --- try { // Note: baseURL is null if no baseURL is set for the corresponding property // Note: baseURL is empty if corporate baseURL is used but not set if (baseURL != null && !baseURL.equals("")) { ((HTMLDocument) textComponent.getDocument()).setBase(new URL(baseURL)); } } catch (MalformedURLException mue) { System.out.println("*** TextEditorPanel.setText(): invalid base URL: " + mue); } // // --- set text --- try { if (text.length() <= 59) { // ### 59 <html><head></head><body></body></html> + whitespace text = "<html><body><p></p></body></html>"; } textComponent.setText(text); textComponent.setCaretPosition(0); } catch (Throwable e) { textComponent.setText( "<html><body><font color=#FF0000>Page can't be displayed</font></body></html>"); System.out.println("*** TextEditorPanel.setText(): error while HTML rendering: " + e); } } else { textComponent.setText(text); textComponent.setCaretPosition(0); } // textComponent.getDocument().addDocumentListener(documentListener); }
private void insertNewlineWithAutoIndent(JTextComponent text) { try { int caretPos = text.getCaretPosition(); StyledDocument doc = (StyledDocument) text.getDocument(); Element map = doc.getDefaultRootElement(); int lineNum = map.getElementIndex(caretPos); Element line = map.getElement(lineNum); int start = line.getStartOffset(); int end = line.getEndOffset() - 1; int len = end - start; String s = doc.getText(start, len); String leadingWS = PythonIndentation.getLeadingWhitespace(doc, start, caretPos - start); StringBuffer sb = new StringBuffer("\n"); sb.append(leadingWS); // TODO better control over automatic indentation indentationLogic.checkIndent(leadingWS, lineNum + 1); // If there is only whitespace between the caret and // the EOL, pressing Enter auto-indents the new line to // the same place as the previous line. int nonWhitespacePos = PythonIndentation.atEndOfLine(doc, caretPos, start, s, len); if (nonWhitespacePos == -1) { if (leadingWS.length() == len) { // If the line was nothing but whitespace, select it // so its contents get removed. text.setSelectionStart(start); } else { // Select the whitespace between the caret and the EOL // to remove it text.setSelectionStart(caretPos); } text.setSelectionEnd(end); text.replaceSelection(sb.toString()); // auto-indentation for python statements like if, while, for, try, // except, def, class and auto-deindentation for break, continue, // pass, return analyseDocument(doc, lineNum, indentationLogic); // auto-completion: add colon if it is obvious if (indentationLogic.shouldAddColon()) { doc.insertString(caretPos, ":", null); indentationLogic.setLastLineEndsWithColon(); } int lastLineChange = indentationLogic.shouldChangeLastLineIndentation(); int nextLineChange = indentationLogic.shouldChangeNextLineIndentation(); if (lastLineChange != 0) { Debug.log(5, "change line %d indentation by %d columns", lineNum + 1, lastLineChange); changeIndentation((DefaultStyledDocument) doc, lineNum, lastLineChange); // nextLineChange was determined based on indentation of last line before // the change nextLineChange += lastLineChange; } if (nextLineChange != 0) { Debug.log(5, "change line %d indentation by %d columns", lineNum + 2, nextLineChange); changeIndentation((DefaultStyledDocument) doc, lineNum + 1, nextLineChange); } } // If there is non-whitespace between the caret and the // EOL, pressing Enter takes that text to the next line // and auto-indents it to the same place as the last // line. Additional auto-indentation or dedentation for // specific python statements is only done for the next line. else { text.setCaretPosition(nonWhitespacePos); doc.insertString(nonWhitespacePos, sb.toString(), null); analyseDocument(doc, lineNum, indentationLogic); int nextLineChange = indentationLogic.shouldChangeNextLineIndentation(); if (nextLineChange != 0) { Debug.log(5, "change line %d indentation by %d columns", lineNum + 2, nextLineChange); changeIndentation((DefaultStyledDocument) doc, lineNum + 1, nextLineChange); } } } catch (BadLocationException ble) { text.replaceSelection("\n"); Debug.error(me + "Problem while inserting new line with auto-indent\n%s", ble.getMessage()); } }
private void highlightCompletedText(int start) { editor.setCaretPosition(getLength()); editor.moveCaretPosition(start); }