下面列出了怎么用javax.swing.plaf.TextUI的API类实例代码及写法,或者点击链接到github查看源代码。
void repaintNewCaret() {
if (component != null) {
TextUI mapper = component.getUI();
Document doc = component.getDocument();
if ((mapper != null) && (doc != null)) {
Rectangle2D newLoc;
try {
newLoc = mapper.modelToView2D(component, this.dot, this.dotBias);
} catch (BadLocationException e) {
newLoc = null;
}
if (newLoc != null) {
adjustVisibility(newLoc.getBounds());
if (getMagicCaretPosition() == null) {
setMagicCaretPosition(new Point((int) newLoc.getX(),
(int) newLoc.getY()));
}
}
damage(newLoc.getBounds());
}
}
}
void repaintNewCaret() {
if (component != null) {
TextUI mapper = component.getUI();
Document doc = component.getDocument();
if ((mapper != null) && (doc != null)) {
Rectangle2D newLoc;
try {
newLoc = mapper.modelToView2D(component, this.dot, this.dotBias);
} catch (BadLocationException e) {
newLoc = null;
}
if (newLoc != null) {
adjustVisibility(newLoc.getBounds());
if (getMagicCaretPosition() == null) {
setMagicCaretPosition(new Point((int) newLoc.getX(),
(int) newLoc.getY()));
}
}
damage(newLoc.getBounds());
}
}
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
TextUI componentUI = component.getUI();
Keymap km = component.getKeymap();
if (componentUI != null && km != null) {
EditorKit kit = componentUI.getEditorKit(component);
if (kit instanceof BaseKit) {
Action a = ((BaseKit)kit).getActionByName(editorActionName);
if (a != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
}
}
return ret;
}
/**
* Invokes the <code>getNextVisualPositionFrom</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int getNextVisualPositionFrom(JTextComponent a, int b, Position.Bias c, int d, Position.Bias[] e)
throws BadLocationException {
int returnValue =
((TextUI) (uis.elementAt(0))).getNextVisualPositionFrom(a,b,c,d,e);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getNextVisualPositionFrom(a,b,c,d,e);
}
return returnValue;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Returns a string anchor if the passed in element has a
* USEMAP that contains the passed in location.
*/
private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
Element elem, AttributeSet attr, int offset,
int x, int y) {
Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
if (useMap != null && (useMap instanceof String)) {
Map m = hdoc.getMap((String)useMap);
if (m != null && offset < hdoc.getLength()) {
Rectangle bounds;
TextUI ui = html.getUI();
try {
Shape lBounds = ui.modelToView(html, offset,
Position.Bias.Forward);
Shape rBounds = ui.modelToView(html, offset + 1,
Position.Bias.Backward);
bounds = lBounds.getBounds();
bounds.add((rBounds instanceof Rectangle) ?
(Rectangle)rBounds : rBounds.getBounds());
} catch (BadLocationException ble) {
bounds = null;
}
if (bounds != null) {
AttributeSet area = m.getArea(x - bounds.x,
y - bounds.y,
bounds.width,
bounds.height);
if (area != null) {
return (String)area.getAttribute(HTML.Attribute.
HREF);
}
}
}
}
return null;
}
/**
* Paints a highlight.
*
* @param g the graphics context
* @param offs0 the starting model offset >= 0
* @param offs1 the ending model offset >= offs1
* @param bounds the bounding box for the highlight
* @param c the editor
*/
public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);
// --- render ---
Color color = getColor();
if (color == null) {
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
g.fillRect(r.x, r.y, r.width, r.height);
} else {
// different lines
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
p1.y - (p0.y + p0.height));
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
}
} catch (BadLocationException e) {
// can't render
}
}
@Override
public int viewToModel2D(JTextComponent a, Point2D b, Position.Bias[] c) {
int returnValue =
((TextUI) (uis.elementAt(0))).viewToModel2D(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).viewToModel2D(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getRootView</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public View getRootView(JTextComponent a) {
View returnValue =
((TextUI) (uis.elementAt(0))).getRootView(a);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getRootView(a);
}
return returnValue;
}
/**
* Invokes the <code>modelToView</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public Rectangle modelToView(JTextComponent a, int b, Position.Bias c)
throws BadLocationException {
Rectangle returnValue =
((TextUI) (uis.elementAt(0))).modelToView(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).modelToView(a,b,c);
}
return returnValue;
}
protected static void uninstallSearchField(final JTextComponent c) {
c.setBorder(UIManager.getBorder("TextField.border"));
c.removeAll();
final TextUI ui = c.getUI();
if (ui instanceof AquaTextFieldUI) {
((AquaTextFieldUI)ui).setPaintingDelegate(null);
}
}
/**
* Invokes the <code>modelToView</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public Rectangle modelToView(JTextComponent a, int b, Position.Bias c)
throws BadLocationException {
Rectangle returnValue =
((TextUI) (uis.elementAt(0))).modelToView(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).modelToView(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>getNextVisualPositionFrom</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int getNextVisualPositionFrom(JTextComponent a, int b, Position.Bias c, int d, Position.Bias[] e)
throws BadLocationException {
int returnValue =
((TextUI) (uis.elementAt(0))).getNextVisualPositionFrom(a,b,c,d,e);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getNextVisualPositionFrom(a,b,c,d,e);
}
return returnValue;
}
@Override
public Rectangle2D modelToView2D(JTextComponent a, int b, Position.Bias c) throws BadLocationException {
Rectangle2D returnValue =
((TextUI) (uis.elementAt(0))).modelToView2D(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).modelToView2D(a,b,c);
}
return returnValue;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Returns true if the View representing <code>e</code> contains
* the location <code>x</code>, <code>y</code>. <code>offset</code>
* gives the offset into the Document to check for.
*/
private boolean doesElementContainLocation(JEditorPane editor,
Element e, int offset,
int x, int y) {
if (e != null && offset > 0 && e.getStartOffset() == offset) {
try {
TextUI ui = editor.getUI();
Shape s1 = ui.modelToView(editor, offset,
Position.Bias.Forward);
if (s1 == null) {
return false;
}
Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
s1.getBounds();
Shape s2 = ui.modelToView(editor, e.getEndOffset(),
Position.Bias.Backward);
if (s2 != null) {
Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
s2.getBounds();
r1.add(r2);
}
return r1.contains(x, y);
} catch (BadLocationException ble) {
}
}
return true;
}
/**
* Invokes the <code>modelToView</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public Rectangle modelToView(JTextComponent a, int b, Position.Bias c)
throws BadLocationException {
Rectangle returnValue =
((TextUI) (uis.elementAt(0))).modelToView(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).modelToView(a,b,c);
}
return returnValue;
}
/**
* Invokes the <code>viewToModel</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int viewToModel(JTextComponent a, Point b) {
int returnValue =
((TextUI) (uis.elementAt(0))).viewToModel(a,b);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).viewToModel(a,b);
}
return returnValue;
}
/**
* Invokes the <code>viewToModel</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int viewToModel(JTextComponent a, Point b, Position.Bias[] c) {
int returnValue =
((TextUI) (uis.elementAt(0))).viewToModel(a,b,c);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).viewToModel(a,b,c);
}
return returnValue;
}
protected static void installSearchField(final JTextComponent c) {
final SearchFieldBorder border = getSearchTextFieldBorder();
c.setBorder(border);
c.setLayout(border.getCustomLayout());
c.add(getFindButton(c), BorderLayout.WEST);
c.add(getCancelButton(c), BorderLayout.EAST);
c.add(getPromptLabel(c), BorderLayout.CENTER);
final TextUI ui = c.getUI();
if (ui instanceof AquaTextFieldUI) {
((AquaTextFieldUI)ui).setPaintingDelegate(border);
}
}
/**
* Invokes the <code>viewToModel</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int viewToModel(JTextComponent a, Point b) {
int returnValue =
((TextUI) (uis.elementAt(0))).viewToModel(a,b);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).viewToModel(a,b);
}
return returnValue;
}
/**
* Invokes the <code>viewToModel</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
@Deprecated(since = "9")
@Override
public int viewToModel(JTextComponent a, Point b) {
int returnValue =
((TextUI) (uis.elementAt(0))).viewToModel(a,b);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).viewToModel(a,b);
}
return returnValue;
}
/**
* Invokes the <code>getRootView</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public View getRootView(JTextComponent a) {
View returnValue =
((TextUI) (uis.elementAt(0))).getRootView(a);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getRootView(a);
}
return returnValue;
}
/**
* Invokes the <code>getNextVisualPositionFrom</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int getNextVisualPositionFrom(JTextComponent a, int b, Position.Bias c, int d, Position.Bias[] e)
throws BadLocationException {
int returnValue =
((TextUI) (uis.elementAt(0))).getNextVisualPositionFrom(a,b,c,d,e);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getNextVisualPositionFrom(a,b,c,d,e);
}
return returnValue;
}
protected static void installSearchField(final JTextComponent c) {
final SearchFieldBorder border = getSearchTextFieldBorder();
c.setBorder(border);
c.setLayout(border.getCustomLayout());
c.add(getFindButton(c), BorderLayout.WEST);
c.add(getCancelButton(c), BorderLayout.EAST);
c.add(getPromptLabel(c), BorderLayout.CENTER);
final TextUI ui = c.getUI();
if (ui instanceof AquaTextFieldUI) {
((AquaTextFieldUI)ui).setPaintingDelegate(border);
}
}
@SuppressWarnings("LeakingThisInConstructor")
public BraceMatchingSidebarComponent(JTextComponent editor) {
this.editor = editor;
this.mimeType = DocumentUtilities.getMimeType(editor);
this.prefs = MimeLookup.getLookup(MimePath.EMPTY).lookup(Preferences.class);
final Lookup.Result r = MimeLookup.getLookup(org.netbeans.lib.editor.util.swing.DocumentUtilities.getMimeType(editor)).lookupResult(
FontColorSettings.class);
prefListenerGC = new PrefListener();
this.colorResult = r;
r.addLookupListener(WeakListeners.create(LookupListener.class, this , r));
prefs.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, prefListenerGC, prefs));
loadPreferences();
editorPane = findEditorPane(editor);
Component parent = editor.getParent();
if (parent instanceof JLayeredPane) {
parent = parent.getParent();
}
if (parent instanceof JViewport) {
this.viewport = (JViewport)parent;
// see #219015; need to listen on viewport change to show/hide the tooltip
viewport.addChangeListener(WeakListeners.change(this, viewport));
}
TextUI ui = editor.getUI();
if (ui instanceof BaseTextUI) {
baseUI = (BaseTextUI)ui;
MasterMatcher.get(editor).addMatchListener(this);
} else {
baseUI = null;
}
setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
updatePreferredSize();
}
/**
* Invokes the <code>getNextVisualPositionFrom</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int getNextVisualPositionFrom(JTextComponent a, int b, Position.Bias c, int d, Position.Bias[] e)
throws BadLocationException {
int returnValue =
((TextUI) (uis.elementAt(0))).getNextVisualPositionFrom(a,b,c,d,e);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getNextVisualPositionFrom(a,b,c,d,e);
}
return returnValue;
}
/**
* Invokes the <code>getNextVisualPositionFrom</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
*/
public int getNextVisualPositionFrom(JTextComponent a, int b, Position.Bias c, int d, Position.Bias[] e)
throws BadLocationException {
int returnValue =
((TextUI) (uis.elementAt(0))).getNextVisualPositionFrom(a,b,c,d,e);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getNextVisualPositionFrom(a,b,c,d,e);
}
return returnValue;
}
/**
* Invokes the <code>getToolTipText</code> method on each UI handled by this object.
*
* @return the value obtained from the first UI, which is
* the UI obtained from the default <code>LookAndFeel</code>
* @since 1.4
*/
public String getToolTipText(JTextComponent a, Point b) {
String returnValue =
((TextUI) (uis.elementAt(0))).getToolTipText(a,b);
for (int i = 1; i < uis.size(); i++) {
((TextUI) (uis.elementAt(i))).getToolTipText(a,b);
}
return returnValue;
}
protected static void uninstallSearchField(final JTextComponent c) {
c.setBorder(UIManager.getBorder("TextField.border"));
c.removeAll();
final TextUI ui = c.getUI();
if (ui instanceof AquaTextFieldUI) {
((AquaTextFieldUI)ui).setPaintingDelegate(null);
}
}