下面列出了javax.swing.text.View#setSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static Rectangle2D modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException {
Document doc = tc.getDocument();
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).readLock();
}
try {
Rectangle alloc = getVisibleEditorRect(tc);
if (alloc != null) {
View rootView = tc.getUI().getRootView(tc);
rootView.setSize(alloc.width, alloc.height);
Shape s = rootView.modelToView(pos, alloc, bias);
if (s != null) {
return s.getBounds2D();
}
}
} finally {
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).readUnlock();
}
}
return null;
}
static int viewToModel(JTextComponent tc, double x, double y, Position.Bias[] biasReturn) {
int offs = -1;
Document doc = tc.getDocument();
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).readLock();
}
try {
Rectangle alloc = getVisibleEditorRect(tc);
if (alloc != null) {
View rootView = tc.getUI().getRootView(tc);
View documentView = rootView.getView(0);
if (documentView instanceof EditorView) {
documentView.setSize(alloc.width, alloc.height);
offs = ((EditorView) documentView).viewToModelChecked(x, y, alloc, biasReturn);
} else {
rootView.setSize(alloc.width, alloc.height);
offs = rootView.viewToModel((float) x, (float) y, alloc, biasReturn);
}
}
} finally {
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).readUnlock();
}
}
return offs;
}
@Override
public void reshape(int x, int y, int w, int h) {
int oldH = getHeight();
super.reshape(x, y, w, h);
if (!isLineWrap()) {
return;
}
if (oldH == 0) {
return;
}
if (w > getVisibleRect().width) {
w = getVisibleRect().width;
}
View view = (View) getClientProperty(BasicHTML.propertyKey);
if (view != null && view instanceof Renderer) {
view.setSize(w - occupiedWidth, h);
}
}
void setSizeForWidth(float width) {
if (width > MAX_WIDTH.get()) {
View v = (View)getClientProperty(BasicHTML.propertyKey);
if (v != null) {
width = 0.0f;
for (View row : getRows(v)) {
float rWidth = row.getPreferredSpan(View.X_AXIS);
if (width < rWidth) {
width = rWidth;
}
}
v.setSize(width, v.getPreferredSpan(View.Y_AXIS));
}
}
}
private Component buildDistributionLabel() {
String content = distributionInfo;
// Use java native JLabel and let it auto-detect html content
JLabel resizer = new JLabel(content);
final int desiredTextViewWidth = imageWidth - (MARGIN * 2);
// If the splash.txt file contains non-HTML text, view is null
View view = (View) resizer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
if (view == null) {
// must not be HTML content in the splash screen text (this shouldn't
// happen, but let's just protect against this anyway).
JLabel label = new GDLabel(content) {
@Override
public Dimension getPreferredSize() {
Dimension preferredSize = super.getPreferredSize();
preferredSize.width = desiredTextViewWidth;
return preferredSize;
}
};
return label;
}
view.setSize(desiredTextViewWidth, 0);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
JLabel distribLabel = new GHtmlLabel(content);
distribLabel.setPreferredSize(new Dimension((int) Math.ceil(w), (int) Math.ceil(h + 10)));
return distribLabel;
}
/**
* Sets the size of the view. This should cause
* layout of the view if it has any layout duties.
*
* @param width the width >= 0
* @param height the height >= 0
*/
public void setSize(float width, float height) {
sync();
if (getImage() == null) {
View view = getAltView();
if (view != null) {
view.setSize(Math.max(0f, width - (float)(DEFAULT_WIDTH + leftInset + rightInset)),
Math.max(0f, height - (float)(topInset + bottomInset)));
}
}
}
/**
* Sets the size of the view. This should cause
* layout of the view if it has any layout duties.
*
* @param width the width >= 0
* @param height the height >= 0
*/
public void setSize(float width, float height) {
sync();
if (getImage() == null) {
View view = getAltView();
if (view != null) {
view.setSize(Math.max(0f, width - (float)(DEFAULT_WIDTH + leftInset + rightInset)),
Math.max(0f, height - (float)(topInset + bottomInset)));
}
}
}
/**
* @see javax.swing.plaf.basic.BasicTextUI#getPreferredSize(javax.swing.JComponent)
*/
public Dimension getPreferredSize(JComponent c) {
// The following code has been derived from BasicTextUI.
Document doc = ((JTextComponent) c).getDocument();
Insets i = c.getInsets();
Dimension d = c.getSize();
View rootView = getRootView((JTextComponent) c);
if (doc instanceof AbstractDocument) {
((AbstractDocument) doc).readLock();
}
try {
if ((d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) {
rootView.setSize(d.width - i.left - i.right, d.height - i.top - i.bottom);
} else if (d.width == 0 && d.height == 0) {
// Probably haven't been layed out yet, force some sort of
// initial sizing.
rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
d.width = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) + (long) i.left + (long) i.right, Integer.MAX_VALUE);
d.height = (int) Math.min((long) rootView.getPreferredSpan(View.Y_AXIS) + (long) i.top + (long) i.bottom, Integer.MAX_VALUE);
} finally {
if (doc instanceof AbstractDocument) {
((AbstractDocument) doc).readUnlock();
}
}
// Fix: The preferred width is always two pixels too small on a Mac.
d.width += 2;
// We'd like our heights to be odd by default.
if ((d.height & 1) == 0) {
d.height--;
}
return d;
}
public static java.awt.Dimension getPreferredSize(String text,
boolean width, int prefSize) {
JLabel resizer = new JLabel(text);
View view = (View) resizer
.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width ? prefSize : 0, width ? 0 : prefSize);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS) + 2;
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
}