下面列出了怎么用javax.swing.text.View的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateHtmlViews(int index) {
String title = tabPane.getTitleAt(index);
boolean isHTML = BasicHTML.isHTMLString(title);
if (isHTML) {
if (htmlViews==null) { // Initialize vector
htmlViews = createHTMLVector();
} else { // Vector already exists
View v = BasicHTML.createHTMLView(tabPane, title);
htmlViews.insertElementAt(v, index);
}
} else { // Not HTML
if (htmlViews!=null) { // Add placeholder
htmlViews.insertElementAt(null, index);
} // else nada!
}
updateMnemonics();
}
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
// sja fix getTheme().setThemeTextColor(g, isSelected, isPressed && tracking, tabPane.isEnabledAt(tabIndex));
if (tabPane.isEnabledAt(tabIndex)) {
g2d.setColor(Color.black);
} else {
g2d.setColor(Color.gray);
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
@Override
public Dimension getPreferredSize(JComponent c) {
Font font = c.getFont();
Insets insets = c.getInsets();
Dimension prefSize = new Dimension(insets.left + insets.right,
insets.top + insets.bottom);
String text = ((JToolTip) c).getTipText();
if ((text == null) || text.equals("")) {
text = "";
} else {
View v = (c != null) ? (View) c.getClientProperty("html") : null;
if (v != null) {
// fix for 302 - add extra pixels for the HTML view as well
prefSize.width += (int) (v.getPreferredSpan(View.X_AXIS) + 6);
prefSize.height += (int) (v.getPreferredSpan(View.Y_AXIS) + 2);
} else {
FontMetrics fm = SubstanceMetricsUtilities.getFontMetrics(font);
prefSize.width += fm.stringWidth(text) + 6;
prefSize.height += fm.getHeight() + 2;
}
}
return prefSize;
}
protected void layoutLabel(final int tabPlacement, final FontMetrics metrics, final int tabIndex, final String title, final Icon icon, final Rectangle tabRect, final Rectangle iconRect, final Rectangle textRect, final boolean isSelected) {
textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
final View v = getTextViewForTab(tabIndex);
if (v != null) {
tabPane.putClientProperty("html", v);
}
SwingUtilities.layoutCompoundLabel(tabPane, metrics, title, icon, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.TRAILING, tabRect, iconRect, textRect, textIconGap);
tabPane.putClientProperty("html", null);
final int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
final int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
iconRect.x += xNudge;
iconRect.y += yNudge;
textRect.x += xNudge;
textRect.y += yNudge;
}
/**
* Calculates the tab width.
* @param tabPlacement the placement (left, right, bottom, top) of the tab
* @param tabIndex the index of the tab with respect to other tabs
* @param metrics the font metrics
* @return the tab width
*/
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
int width = tabInsets.left + tabInsets.right + 3;
Component tabComponent = tabPane.getTabComponentAt(tabIndex);
if (tabComponent != null) {
width += tabComponent.getPreferredSize().width;
} else {
Icon icon = getIconForTab(tabIndex);
if (icon != null) {
width += icon.getIconWidth() + textIconGap;
}
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
width += (int) v.getPreferredSpan(View.X_AXIS);
} else {
// plain text
String title = tabPane.getTitleAt(tabIndex);
width += SwingUtilities2.stringWidth(tabPane, metrics, title);
}
}
return width;
}
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
g2d.setColor(getNonSelectedTabTitleColor());
if (tabPane.getSelectedIndex() == tabIndex) {
boolean pressed = isPressedAt(tabIndex);
boolean enabled = tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex);
Color textColor = getSelectedTabTitleColor(enabled, pressed);
Color shadowColor = getSelectedTabTitleShadowColor(enabled);
AquaUtils.paintDropShadowText(g2d, tabPane, font, metrics, textRect.x, textRect.y, 0, 1, textColor, shadowColor, title);
return;
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
/** Helper for displaying hierarchy - called recursively.
* @param origView view for which the displayHierarchy method was originally
* called. It is marked by asterisk.
* @param col column offset
* @param index index of this view in parent children[] array
*/
private void displayHierarchyHelper(View origView, int col, int index) {
StringBuffer buf = new StringBuffer();
buf.append(((this == origView) ? "*" : " ")); // NOI18N
for (int i = 0; i < col; i++) {
buf.append(' ');
}
buf.append('[');
buf.append(Integer.toString(index));
buf.append("] "); // NOI18N
buf.append(this.toString());
System.out.println(buf);
int childrenCnt = getViewCount();
if (childrenCnt > 0) {
for (int i = 0; i < childrenCnt; i++) {
((BaseView)getView(i)).displayHierarchyHelper(origView, col + 1, i);
}
}
}
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
// sja fix getTheme().setThemeTextColor(g, isSelected, isPressed && tracking, tabPane.isEnabledAt(tabIndex));
if (tabPane.isEnabledAt(tabIndex)) {
g2d.setColor(Color.black);
} else {
g2d.setColor(Color.gray);
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
private void paintText(SynthContext ss,
Graphics g, int tabPlacement,
Font font, FontMetrics metrics, int tabIndex,
String title, Rectangle textRect,
boolean isSelected) {
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
v.paint(g, textRect);
} else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
g.setColor(ss.getStyle().getColor(ss, ColorType.TEXT_FOREGROUND));
ss.getStyle().getGraphicsUtils(ss).paintText(ss, g, title,
textRect, mnemIndex);
}
}
private void paintText(SynthContext ss,
Graphics g, int tabPlacement,
Font font, FontMetrics metrics, int tabIndex,
String title, Rectangle textRect,
boolean isSelected) {
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
v.paint(g, textRect);
} else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
g.setColor(ss.getStyle().getColor(ss, ColorType.TEXT_FOREGROUND));
ss.getStyle().getGraphicsUtils(ss).paintText(ss, g, title,
textRect, mnemIndex);
}
}
/**
* {@inheritDoc}
*/
@Override
public Dimension getPreferredSize(JComponent c) {
SynthContext context = getContext(c);
Insets insets = c.getInsets();
Dimension prefSize = new Dimension(insets.left+insets.right,
insets.top+insets.bottom);
String text = ((JToolTip)c).getTipText();
if (text != null) {
View v = (c != null) ? (View) c.getClientProperty("html") : null;
if (v != null) {
prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
} else {
Font font = context.getStyle().getFont(context);
FontMetrics fm = c.getFontMetrics(font);
prefSize.width += context.getStyle().getGraphicsUtils(context).
computeStringWidth(context, font, fm, text);
prefSize.height += fm.getHeight();
}
}
context.dispose();
return prefSize;
}
private void updateHtmlViews(int index) {
String title = tabPane.getTitleAt(index);
boolean isHTML = BasicHTML.isHTMLString(title);
if (isHTML) {
if (htmlViews==null) { // Initialize vector
htmlViews = createHTMLVector();
} else { // Vector already exists
View v = BasicHTML.createHTMLView(tabPane, title);
htmlViews.insertElementAt(v, index);
}
} else { // Not HTML
if (htmlViews!=null) { // Add placeholder
htmlViews.insertElementAt(null, index);
} // else nada!
}
updateMnemonics();
}
@Override
public void paint(Graphics graphics, Shape allocation)
{
if (box.isDisplayed())
{
Graphics2D g = (Graphics2D) graphics;
// super.paint(g, allocation);
box.getVisualContext().updateGraphics(g);
box.drawBackground(g);
int n = getViewCount();
// Rectangle alloc = allocation instanceof Rectangle ? (Rectangle)
// allocation : allocation.getBounds();
for (int i = 0; i < n; i++)
{
View v = getView(i);
v.paint(g, allocation);
}
}
}
protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
int height = 0;
Component c = tabPane.getTabComponentAt(tabIndex);
if (c != null) {
height = c.getPreferredSize().height;
} else {
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
height += (int) v.getPreferredSpan(View.Y_AXIS);
} else {
// plain text
height += fontHeight;
}
Icon icon = getIconForTab(tabIndex);
if (icon != null) {
height = Math.max(height, icon.getIconHeight());
}
}
Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
height += tabInsets.top + tabInsets.bottom + 2;
return height;
}
private static void check(TestTabbedPaneUI ui, int... indices) {
for(int i = 0; i < ui.getTabbedPane().getTabCount(); i++) {
System.out.print("Checking tab #" + i);
View view = ui.getTextViewForTab(i);
boolean found = false;
for (int j = 0; j < indices.length; j++) {
if (indices[j]== i) {
found = true;
break;
}
}
System.out.print("; view = " + view);
if (found) {
if (view == null) {
throw new RuntimeException("View is unexpectedly null");
}
} else if (view != null) {
throw new RuntimeException("View is unexpectedly not null");
}
System.out.println(" ok");
}
System.out.println("");
}
@Override
public void componentAdded(ContainerEvent e) {
JTabbedPane tp = (JTabbedPane) e.getContainer();
Component child = e.getChild();
if (child instanceof UIResource) {
return;
}
int index = tp.indexOfComponent(child);
String title = tp.getTitleAt(index);
boolean isHTML = BasicHTML.isHTMLString(title);
if (isHTML) {
if (htmlViews == null) { // Initialize vector
htmlViews = createHTMLVector();
}
else { // Vector already exists
View v = BasicHTML.createHTMLView(tp, title);
htmlViews.insertElementAt(v, index);
}
}
else { // Not HTML
if (htmlViews != null) { // Add placeholder
htmlViews.insertElementAt(null, index);
} // else nada!
}
}
protected int calculateTabWidth(final int tabPlacement, final int tabIndex, final FontMetrics metrics) {
final Insets tabInsets = getTabInsets(tabPlacement, tabIndex);
int width = tabInsets.left + tabInsets.right + 3;
final Component tabComponent = tabPane.getTabComponentAt(tabIndex);
if (tabComponent != null) {
width += tabComponent.getPreferredSize().width;
} else {
final Icon icon = getIconForTab(tabIndex);
if (icon != null) {
width += icon.getIconWidth() + textIconGap;
}
final View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
width += (int)v.getPreferredSpan(View.X_AXIS);
} else {
// plain text
final String title = tabPane.getTitleAt(tabIndex);
width += SwingUtilities2.stringWidth(tabPane, metrics, title);
}
}
return width;
}
public Dimension getPreferredSize(JComponent c) {
Font font = c.getFont();
FontMetrics fm = c.getFontMetrics(font);
Insets insets = c.getInsets();
Dimension prefSize = new Dimension(insets.left+insets.right,
insets.top+insets.bottom);
String text = ((JToolTip)c).getTipText();
if ((text == null) || text.equals("")) {
text = "";
}
else {
View v = (c != null) ? (View) c.getClientProperty("html") : null;
if (v != null) {
prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
} else {
prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
prefSize.height += fm.getHeight();
}
}
return prefSize;
}
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
g2d.setColor(getNonSelectedTabTitleColor());
if (tabPane.getSelectedIndex() == tabIndex) {
boolean pressed = isPressedAt(tabIndex);
boolean enabled = tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex);
Color textColor = getSelectedTabTitleColor(enabled, pressed);
Color shadowColor = getSelectedTabTitleShadowColor(enabled);
AquaUtils.paintDropShadowText(g2d, tabPane, font, metrics, textRect.x, textRect.y, 0, 1, textColor, shadowColor, title);
return;
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
public Dimension getMaximumSize(final JComponent c) {
final Dimension d = getPreferredSize(c);
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
}
return d;
}
public Dimension getMinimumSize(JComponent c) {
Dimension d = getPreferredSize(c);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
}
return d;
}
public int getPreviousLineCount0() {
int lineCount = 0;
View parent = this.getParent();
int count = parent.getViewCount();
for (int i = 0; i < count; i++) {
if (parent.getView(i) == this) {
break;
} else {
lineCount += parent.getView(i).getViewCount();
}
}
return lineCount;
}
/**
* Get first view in the hierarchy that is an instance of the given class.
* It allows to skip various wrapper-views around the doc-view that holds
* the child views for the lines.
*
* @param component component from which the root view is fetched.
* @param rootViewClass class of the view to return.
* @return view being instance of the requested class or null if there
* is not one.
*/
public static View getRootView(JTextComponent component, Class rootViewClass) {
View view = null;
TextUI textUI = component.getUI();
if (textUI != null) {
view = textUI.getRootView(component);
while (view != null && !rootViewClass.isInstance(view)
&& view.getViewCount() == 1 // must be wrapper view
) {
view = view.getView(0); // get the only child
}
}
return view;
}
public void paint(Graphics g, JComponent c)
{
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
String text = layout(b, SwingUtilities2.getFontMetrics(b, g),
b.getWidth(), b.getHeight());
clearTextShiftOffset();
// perform UI specific press action, e.g. Windows L&F shifts text
if (model.isArmed() && model.isPressed()) {
paintButtonPressed(g,b);
}
// Paint the Icon
if(b.getIcon() != null) {
paintIcon(g,c,iconRect);
}
if (text != null && !text.equals("")){
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
paintText(g, b, textRect, text);
}
}
if (b.isFocusPainted() && b.hasFocus()) {
// paint UI specific focus
paintFocus(g,b,viewRect,textRect,iconRect);
}
}
private Vector<View> createHTMLVector() {
final Vector<View> htmlViews = new Vector<View>();
final int count = tabPane.getTabCount();
if (count > 0) {
for (int i = 0; i < count; i++) {
final String title = tabPane.getTitleAt(i);
if (BasicHTML.isHTMLString(title)) {
htmlViews.addElement(BasicHTML.createHTMLView(tabPane, title));
} else {
htmlViews.addElement(null);
}
}
}
return htmlViews;
}
/**
* Returns the preferred span of the View used to display the alt text,
* or 0 if the view does not exist.
*/
private float getPreferredSpanFromAltView(int axis) {
if (getImage() == null) {
View view = getAltView();
if (view != null) {
return view.getPreferredSpan(axis);
}
}
return 0f;
}
public Dimension getMinimumSize(JComponent c) {
Dimension d = getPreferredSize(c);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
}
return d;
}
public Dimension getMaximumSize(JComponent c) {
Dimension d = getPreferredSize(c);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
}
return d;
}
public Dimension getMaximumSize(final JComponent c) {
final Dimension d = getPreferredSize(c);
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
}
return d;
}
private Vector<View> createHTMLVector() {
Vector<View> htmlViews = new Vector<View>();
int count = tabPane.getTabCount();
if (count>0) {
for (int i=0 ; i<count; i++) {
String title = tabPane.getTitleAt(i);
if (BasicHTML.isHTMLString(title)) {
htmlViews.addElement(BasicHTML.createHTMLView(tabPane, title));
} else {
htmlViews.addElement(null);
}
}
}
return htmlViews;
}