?? htmleditorkit.java
字號:
HTML.Tag alternateParentTag, HTML.Tag alternateAddTag, boolean adjustSelection) { super(name); this.html = html; this.parentTag = parentTag; this.addTag = addTag; this.alternateParentTag = alternateParentTag; this.alternateAddTag = alternateAddTag; this.adjustSelection = adjustSelection; } /** * A cover for HTMLEditorKit.insertHTML. If an exception it * thrown it is wrapped in a RuntimeException and thrown. */ protected void insertHTML(JEditorPane editor, HTMLDocument doc, int offset, String html, int popDepth, int pushDepth, HTML.Tag addTag) { try { getHTMLEditorKit(editor).insertHTML(doc, offset, html, popDepth, pushDepth, addTag); } catch (IOException ioe) { throw new RuntimeException("Unable to insert: " + ioe); } catch (BadLocationException ble) { throw new RuntimeException("Unable to insert: " + ble); } } /** * This is invoked when inserting at a boundary. It determines * the number of pops, and then the number of pushes that need * to be performed, and then invokes insertHTML. * @since 1.3 */ protected void insertAtBoundary(JEditorPane editor, HTMLDocument doc, int offset, Element insertElement, String html, HTML.Tag parentTag, HTML.Tag addTag) { insertAtBoundry(editor, doc, offset, insertElement, html, parentTag, addTag); } /** * This is invoked when inserting at a boundary. It determines * the number of pops, and then the number of pushes that need * to be performed, and then invokes insertHTML. * @deprecated As of Java 2 platform v1.3, use insertAtBoundary */ @Deprecated protected void insertAtBoundry(JEditorPane editor, HTMLDocument doc, int offset, Element insertElement, String html, HTML.Tag parentTag, HTML.Tag addTag) { // Find the common parent. Element e; Element commonParent; boolean isFirst = (offset == 0); if (offset > 0 || insertElement == null) { e = doc.getDefaultRootElement(); while (e != null && e.getStartOffset() != offset && !e.isLeaf()) { e = e.getElement(e.getElementIndex(offset)); } commonParent = (e != null) ? e.getParentElement() : null; } else { // If inserting at the origin, the common parent is the // insertElement. commonParent = insertElement; } if (commonParent != null) { // Determine how many pops to do. int pops = 0; int pushes = 0; if (isFirst && insertElement != null) { e = commonParent; while (e != null && !e.isLeaf()) { e = e.getElement(e.getElementIndex(offset)); pops++; } } else { e = commonParent; offset--; while (e != null && !e.isLeaf()) { e = e.getElement(e.getElementIndex(offset)); pops++; } // And how many pushes e = commonParent; offset++; while (e != null && e != insertElement) { e = e.getElement(e.getElementIndex(offset)); pushes++; } } pops = Math.max(0, pops - 1); // And insert! insertHTML(editor, doc, offset, html, pops, pushes, addTag); } } /** * If there is an Element with name <code>tag</code> at * <code>offset</code>, this will invoke either insertAtBoundary * or <code>insertHTML</code>. This returns true if there is * a match, and one of the inserts is invoked. */ /*protected*/ boolean insertIntoTag(JEditorPane editor, HTMLDocument doc, int offset, HTML.Tag tag, HTML.Tag addTag) { Element e = findElementMatchingTag(doc, offset, tag); if (e != null && e.getStartOffset() == offset) { insertAtBoundary(editor, doc, offset, e, html, tag, addTag); return true; } else if (offset > 0) { int depth = elementCountToTag(doc, offset - 1, tag); if (depth != -1) { insertHTML(editor, doc, offset, html, depth, 0, addTag); return true; } } return false; } /** * Called after an insertion to adjust the selection. */ /* protected */ void adjustSelection(JEditorPane pane, HTMLDocument doc, int startOffset, int oldLength) { int newLength = doc.getLength(); if (startOffset > 0) { String text; try { text = doc.getText(startOffset - 1, 1); } catch (BadLocationException ble) { text = null; } if (text != null && text.length() > 0 && text.charAt(0) == '\n') { pane.select(startOffset, startOffset); } else { pane.select(startOffset + 1, startOffset + 1); } } else { pane.select(1, 1); } } } /** * Inserts the HTML into the document. * * @param ae the event */ public void actionPerformed(ActionEvent ae) { JEditorPane editor = getEditor(ae); if (editor != null) { HTMLDocument doc = getHTMLDocument(editor); int offset = editor.getSelectionStart(); int length = doc.getLength(); boolean inserted; // Try first choice if (!insertIntoTag(editor, doc, offset, parentTag, addTag) && alternateParentTag != null) { // Then alternate. inserted = insertIntoTag(editor, doc, offset, alternateParentTag, alternateAddTag); } else { inserted = true; } if (adjustSelection && inserted) { adjustSelection(editor, doc, offset, length); } } } /** HTML to insert. */ protected String html; /** Tag to check for in the document. */ protected HTML.Tag parentTag; /** Tag in HTML to start adding tags from. */ protected HTML.Tag addTag; /** Alternate Tag to check for in the document if parentTag is * not found. */ protected HTML.Tag alternateParentTag; /** Alternate tag in HTML to start adding tags from if parentTag * is not found and alternateParentTag is found. */ protected HTML.Tag alternateAddTag; /** True indicates the selection should be adjusted after an insert. */ boolean adjustSelection; } /** * InsertHRAction is special, at actionPerformed time it will determine * the parent HTML.Tag based on the paragraph element at the selection * start. */ static class InsertHRAction extends InsertHTMLTextAction { InsertHRAction() { super("InsertHR", "<hr>", null, HTML.Tag.IMPLIED, null, null, false); } /** * Inserts the HTML into the document. * * @param ae the event */ public void actionPerformed(ActionEvent ae) { JEditorPane editor = getEditor(ae); if (editor != null) { HTMLDocument doc = getHTMLDocument(editor); int offset = editor.getSelectionStart(); Element paragraph = doc.getParagraphElement(offset); if (paragraph.getParentElement() != null) { parentTag = (HTML.Tag)paragraph.getParentElement(). getAttributes().getAttribute (StyleConstants.NameAttribute); super.actionPerformed(ae); } } } } /* * Returns the object in an AttributeSet matching a key */ static private Object getAttrValue(AttributeSet attr, HTML.Attribute key) { Enumeration names = attr.getAttributeNames(); while (names.hasMoreElements()) { Object nextKey = names.nextElement(); Object nextVal = attr.getAttribute(nextKey); if (nextVal instanceof AttributeSet) { Object value = getAttrValue((AttributeSet)nextVal, key); if (value != null) { return value; } } else if (nextKey == key) { return nextVal; } } return null; } /* * Action to move the focus on the next or previous hypertext link * or object. TODO: This method relies on support from the * javax.accessibility package. The text package should support * keyboard navigation of text elements directly. */ static class NavigateLinkAction extends TextAction implements CaretListener { private static int prevHypertextOffset = -1; private static boolean foundLink = false; private FocusHighlightPainter focusPainter = new FocusHighlightPainter(null); private Object selectionTag; private boolean focusBack = false; /* * Create this action with the appropriate identifier. */ public NavigateLinkAction(String actionName) { super(actionName); if ("previous-link-action".equals(actionName)) { focusBack = true; } } /** * Called when the caret position is updated. * * @param e the caret event */ public void caretUpdate(CaretEvent e) { if (foundLink) { foundLink = false; // TODO: The AccessibleContext for the editor should register // as a listener for CaretEvents and forward the events to // assistive technologies listening for such events. Object src = e.getSource(); if (src instanceof JTextComponent) { ((JTextComponent)src).getAccessibleContext().firePropertyChange( AccessibleContext.ACCESSIBLE_HYPERTEXT_OFFSET, new Integer(prevHypertextOffset), new Integer(e.getDot())); } } } /* * The operation to perform when this action is triggered. */ public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null || comp.isEditable()) { return; } Document doc = comp.getDocument(); if (doc == null) { return; } // TODO: Should start successive iterations from the // current caret position. ElementIterator ei = new ElementIterator(doc); int currentOffset = comp.getCaretPosition(); int prevStartOffset = -1; int prevEndOffset = -1; // highlight the next link or object after the current caret position Element nextElement = null; while ((nextElement = ei.next()) != null) { String name = nextElement.getName(); AttributeSet attr = nextElement.getAttributes(); Object href = getAttrValue(attr, HTML.Attribute.HREF); if (!(name.equals(HTML.Tag.OBJECT.toString())) && href == null) { continue; } int elementOffset = nextElement.getStartOffset(); if (focusBack) { if (elementOffset >= currentOffset && prevStartOffset >= 0) { foundLink = true; comp.setCaretPosition(prevStartOffset); moveCaretPosition(comp, prevStartOffset, prevEndOffset); prevHypertextOffset = prevStartOffset; return; } } else { // focus forward if (elementOffset > currentOffset) { foundLink = true; comp.setCaretPosition(elementOffset); moveCaretPosition(comp, elementOffset, nextElement.getEndOffset()); prevHypertextOffset = elementOffset; return; } } prevStartOffset = nextElement.getStartOffset(); prevEndOffset = nextElement.getEndOffset(); } if (focusBack && prevStartOffset >= 0) { foundLink = true; comp.setCaretPosition(prevStartOffset); moveCaretPosition(comp, prevStartOffset, prevEndOffset); prevHypertextOffset = prevStartOffset; return; } } /* * Moves the caret from mark to dot */ private void moveCaretPosition(JTextComponent comp, int mark, int dot) { Highlighter h = comp.getHighlighter(); if (h != null) { int p0 = Math.min(dot, mark); int p1 = Math.max(dot, mark); try { if (selectionTag != null) { h.changeHighlight(selectionTag, p
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -