?? accessiblehtml.java
字號:
} /** * Given a point in local coordinates, return the zero-based index * of the character under that Point. If the point is invalid, * this method returns -1. * * @param p the Point in local coordinates * @return the zero-based index of the character under Point p; if * Point is invalid returns -1. */ public int getIndexAtPoint(Point p) { View v = getView(); if (v != null) { return v.viewToModel(p.x, p.y, getBounds()); } else { return -1; } } /** * Determine the bounding box of the character at the given * index into the string. The bounds are returned in local * coordinates. If the index is invalid an empty rectangle is * returned. * * @param i the index into the String * @return the screen coordinates of the character's the bounding box, * if index is invalid returns an empty rectangle. */ public Rectangle getCharacterBounds(int i) { try { return editor.getUI().modelToView(editor, i); } catch (BadLocationException e) { return null; } } /** * Return the number of characters (valid indicies) * * @return the number of characters */ public int getCharCount() { if (validateIfNecessary()) { Element elem = elementInfo.getElement(); return elem.getEndOffset() - elem.getStartOffset(); } return 0; } /** * Return the zero-based offset of the caret. * * Note: That to the right of the caret will have the same index * value as the offset (the caret is between two characters). * @return the zero-based offset of the caret. */ public int getCaretPosition() { View v = getView(); if (v == null) { return -1; } Container c = v.getContainer(); if (c == null) { return -1; } if (c instanceof JTextComponent) { return ((JTextComponent)c).getCaretPosition(); } else { return -1; } } /** * IndexedSegment extends Segment adding the offset into the * the model the <code>Segment</code> was asked for. */ private class IndexedSegment extends Segment { /** * Offset into the model that the position represents. */ public int modelOffset; } public String getAtIndex(int part, int index) { return getAtIndex(part, index, 0); } public String getAfterIndex(int part, int index) { return getAtIndex(part, index, 1); } public String getBeforeIndex(int part, int index) { return getAtIndex(part, index, -1); } /** * Gets the word, sentence, or character at <code>index</code>. * If <code>direction</code> is non-null this will find the * next/previous word/sentence/character. */ private String getAtIndex(int part, int index, int direction) { if (model instanceof AbstractDocument) { ((AbstractDocument)model).readLock(); } try { if (index < 0 || index >= model.getLength()) { return null; } switch (part) { case AccessibleText.CHARACTER: if (index + direction < model.getLength() && index + direction >= 0) { return model.getText(index + direction, 1); } break; case AccessibleText.WORD: case AccessibleText.SENTENCE: IndexedSegment seg = getSegmentAt(part, index); if (seg != null) { if (direction != 0) { int next; if (direction < 0) { next = seg.modelOffset - 1; } else { next = seg.modelOffset + direction * seg.count; } if (next >= 0 && next <= model.getLength()) { seg = getSegmentAt(part, next); } else { seg = null; } } if (seg != null) { return new String(seg.array, seg.offset, seg.count); } } break; default: break; } } catch (BadLocationException e) { } finally { if (model instanceof AbstractDocument) { ((AbstractDocument)model).readUnlock(); } } return null; } /* * Returns the paragraph element for the specified index. */ private Element getParagraphElement(int index) { if (model instanceof PlainDocument ) { PlainDocument sdoc = (PlainDocument)model; return sdoc.getParagraphElement(index); } else if (model instanceof StyledDocument) { StyledDocument sdoc = (StyledDocument)model; return sdoc.getParagraphElement(index); } else { Element para = null; for (para = model.getDefaultRootElement(); ! para.isLeaf(); ) { int pos = para.getElementIndex(index); para = para.getElement(pos); } if (para == null) { return null; } return para.getParentElement(); } } /* * Returns a <code>Segment</code> containing the paragraph text * at <code>index</code>, or null if <code>index</code> isn't * valid. */ private IndexedSegment getParagraphElementText(int index) throws BadLocationException { Element para = getParagraphElement(index); if (para != null) { IndexedSegment segment = new IndexedSegment(); try { int length = para.getEndOffset() - para.getStartOffset(); model.getText(para.getStartOffset(), length, segment); } catch (BadLocationException e) { return null; } segment.modelOffset = para.getStartOffset(); return segment; } return null; } /** * Returns the Segment at <code>index</code> representing either * the paragraph or sentence as identified by <code>part</code>, or * null if a valid paragraph/sentence can't be found. The offset * will point to the start of the word/sentence in the array, and * the modelOffset will point to the location of the word/sentence * in the model. */ private IndexedSegment getSegmentAt(int part, int index) throws BadLocationException { IndexedSegment seg = getParagraphElementText(index); if (seg == null) { return null; } BreakIterator iterator; switch (part) { case AccessibleText.WORD: iterator = BreakIterator.getWordInstance(getLocale()); break; case AccessibleText.SENTENCE: iterator = BreakIterator.getSentenceInstance(getLocale()); break; default: return null; } seg.first(); iterator.setText(seg); int end = iterator.following(index - seg.modelOffset + seg.offset); if (end == BreakIterator.DONE) { return null; } if (end > seg.offset + seg.count) { return null; } int begin = iterator.previous(); if (begin == BreakIterator.DONE || begin >= seg.offset + seg.count) { return null; } seg.modelOffset = seg.modelOffset + begin - seg.offset; seg.offset = begin; seg.count = end - begin; return seg; } /** * Return the AttributeSet for a given character at a given index * * @param i the zero-based index into the text * @return the AttributeSet of the character */ public AttributeSet getCharacterAttribute(int i) { if (model instanceof StyledDocument) { StyledDocument doc = (StyledDocument)model; Element elem = doc.getCharacterElement(i); if (elem != null) { return elem.getAttributes(); } } return null; } /** * Returns the start offset within the selected text. * If there is no selection, but there is * a caret, the start and end offsets will be the same. * * @return the index into the text of the start of the selection */ public int getSelectionStart() { return editor.getSelectionStart(); } /** * Returns the end offset within the selected text. * If there is no selection, but there is * a caret, the start and end offsets will be the same. * * @return the index into teh text of the end of the selection */ public int getSelectionEnd() { return editor.getSelectionEnd(); } /** * Returns the portion of the text that is selected. * * @return the String portion of the text that is selected */ public String getSelectedText() { return editor.getSelectedText(); } /* * Returns the text substring starting at the specified * offset with the specified length. */ private String getText(int offset, int length) throws BadLocationException { if (model != null && model instanceof StyledDocument) { StyledDocument doc = (StyledDocument)model; return model.getText(offset, length); } else { return null; } } } } /* * ElementInfo for images */ private class IconElementInfo extends ElementInfo implements Accessible { private int width = -1; private int height = -1; IconElementInfo(Element element, ElementInfo parent) { super(element, parent); } protected void invalidate(boolean first) { super.invalidate(first); width = height = -1; } private int getImageSize(Object key) { if (validateIfNecessary()) { int size = getIntAttr(getAttributes(), key, -1); if (size == -1) { View v = getView(); size = 0; if (v instanceof ImageView) { Image img = ((ImageView)v).getImage(); if (img != null) { if (key == HTML.Attribute.WIDTH) { size = img.getWidth(null); } else { size = img.getHeight(null); } } } } return size; } return 0; } // begin AccessibleIcon implementation ... private AccessibleContext accessibleContext; public AccessibleContext getAccessibleContext() { if (accessibleContext == null) { accessibleContext = new IconAccessibleContext(this); } return accessibleContext; } /* * AccessibleContext for images */ protected class IconAccessibleContext extends HTMLAccessibleContext implements AccessibleIcon { public IconAccessibleContext(ElementInfo elementInfo) { super(elementInfo); } /** * Gets the accessibleName property of this object. The accessibleName * property of an object is a localized String that designates the purpose * of the object. For example, the accessibleName property of a label * or button might be the text of the label or button itself. In the * case of an object that doesn't display its name, the accessibleName * should still be set. For example, in the case of a text field used * to enter the name of a city, the accessibleName for the en_US locale * could be 'city.' * * @return the localized name of the object; null if this * object does not have a name * * @see #setAccessibleName */ public String getAccessibleName() { return getAccessibleIconDescription(); } /** * Gets the accessibleDescription property of this object. If this * property isn't set, returns the content type of this * <code>JEditorPane</code> instead (e.g. "plain/text", "html/text"). * * @return the localized description of the object; <code>null</code> * if this object does not have a description * * @see #setAccessibleName */ public String getAccessibleDescription() { return editor.getContentType(); } /** * Gets the role of this object. The role of the object is the generic * purpose or use of the class of this object. For example, the role * of a push button is AccessibleRole.PUSH_BUTTON. The roles in * AccessibleRole are provided so component developers can pick from * a set of predefined roles. This enables assistive technologies to * provide a consistent interface to various tweaked subclasses of * components (e.g., use AccessibleRole.PUSH_BUTTON for all components * that act like a push button) as well as distinguish between sublasses * that behave differently (e.g., AccessibleRole.CHECK_BOX for check boxes * and AccessibleRole.RADIO_BUTTON for radio buttons). * <p>Note that the AccessibleRole class is also extensible, so * custom component developers can define their own AccessibleRole's * if the set of predefined roles is inadequate. * * @return an instance of AccessibleRole describing the role of the object * @see AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.ICON; } public AccessibleIcon [] getAccessibleIcon() { AccessibleIcon [] icons = new AccessibleIcon[1]; icons[0] = this; return icons; } /** * Gets the description of the icon. This is meant to be a brief * textual description of the object. For example, it might be
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -