Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Rudolf2014-12-16 09:32:54 +0000
committerJudith Gull2014-12-16 10:46:53 +0000
commitedcf638f750d5ce4c540514513c76b3569dfbad9 (patch)
tree910bbc21955045fcdad13bd09184430b0b10aca1
parent24f0c7795c47059fdc2294e766c9a29a66d94aa7 (diff)
downloadorg.eclipse.scout.rt-edcf638f750d5ce4c540514513c76b3569dfbad9.tar.gz
org.eclipse.scout.rt-edcf638f750d5ce4c540514513c76b3569dfbad9.tar.xz
org.eclipse.scout.rt-edcf638f750d5ce4c540514513c76b3569dfbad9.zip
Bug 455182 Swing: support word wrapping on SwingScoutLabelField Java 1.6
compatibility Change-Id: I490c9b045c3894f436c13fb2782f919ad0e50a24 Signed-off-by: Michael Rudolf <Michael.Rudolf@bsiag.com> Change-Id: I490c9b045c3894f436c13fb2782f919ad0e50a24 Reviewed-on: https://git.eclipse.org/r/38319 Tested-by: Hudson CI Reviewed-by: Judith Gull <jgu@bsiag.com>
-rw-r--r--org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/form/fields/labelfield/SwingScoutLabelField.java98
1 files changed, 83 insertions, 15 deletions
diff --git a/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/form/fields/labelfield/SwingScoutLabelField.java b/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/form/fields/labelfield/SwingScoutLabelField.java
index 3b99bf1797..7b30e08495 100644
--- a/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/form/fields/labelfield/SwingScoutLabelField.java
+++ b/org.eclipse.scout.rt.ui.swing/src/org/eclipse/scout/rt/ui/swing/form/fields/labelfield/SwingScoutLabelField.java
@@ -14,11 +14,15 @@ import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
+import javax.swing.SizeRequirements;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
+import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
+import javax.swing.text.GlyphView;
import javax.swing.text.Highlighter;
+import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
@@ -118,34 +122,98 @@ public class SwingScoutLabelField extends SwingScoutValueFieldComposite<ILabelFi
@Override
public ViewFactory getViewFactory() {
+
return new HTMLFactory() {
@Override
- public View create(Element elem) {
- View v = super.create(elem);
+ public View create(Element element) {
+ View v = super.create(element);
if (v instanceof InlineView) {
// javax.swing.text.html.BRView (the <br> tag) is also a LabelView but
// our overridden class must not change it's behavior
- // javax.swing.text.html.BRView is not visible, use HTML tag to check
- Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
+ Object o = element.getAttributes().getAttribute(StyleConstants.NameAttribute);
if ((o instanceof HTML.Tag) && o == HTML.Tag.BR) {
return v;
}
- return new InlineView(elem) {
+ return new InlineView(element) {
+ @Override
+ public int getBreakWeight(int axis, float pos, float len) {
+ // default behavior if no text wrap required
+ if (!m_textWrap) {
+ return super.getBreakWeight(axis, pos, len);
+ }
+ // --> GlyphView.getBreakWeight()
+ if (axis == View.X_AXIS) {
+ checkPainter();
+ int offset = getStartOffset();
+ int maxPos = getGlyphPainter().getBoundedPosition(this, offset, pos, len);
+ if (maxPos == offset) {
+ // can't even fit a single character
+ return View.BadBreakWeight;
+ }
+ // <-- GlyphView.getBreakWeight()
+ // if line break found enforce break
+ try {
+ int separatorIndex = getDocument().getText(offset, maxPos - offset).indexOf(System.getProperty("line.separator"));
+ if (separatorIndex >= 0) {
+ return View.ForcedBreakWeight;
+ }
+ }
+ catch (BadLocationException ex) {
+ //nop
+ }
+ }
+ return super.getBreakWeight(axis, pos, len);
+ }
+
+ @Override
+ public View breakView(int axis, int offset, float pos, float len) {
+ // default behavior if no text wrap required
+ if (!m_textWrap) {
+ return super.breakView(axis, offset, pos, len);
+ }
+ // --> GlyphView.breakView()
+ if (axis == View.X_AXIS) {
+ checkPainter();
+ int maxPos = getGlyphPainter().getBoundedPosition(this, offset, pos, len);
+ // <-- GlyphView.breakView()
+ // if line break found create new text fragment to display
+ try {
+ int separatorIndex = getDocument().getText(offset, maxPos - offset).indexOf(System.getProperty("line.separator"));
+ if (separatorIndex >= 0) {
+ GlyphView glyphView = (GlyphView) createFragment(offset, offset + separatorIndex + 1);
+ return glyphView;
+ }
+ }
+ catch (BadLocationException ex) {
+ //nop
+ }
+ }
+ return super.breakView(axis, offset, pos, len);
+ }
+ };
+ }
+ else if (v instanceof ParagraphView) {
+ return new ParagraphView(element) {
@Override
- public float getMinimumSpan(int axis) {
+ protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
// default behavior if no text wrap required
if (!m_textWrap) {
- return super.getMinimumSpan(axis);
+ return super.calculateMinorAxisRequirements(axis, r);
}
- // to support word wrapping we have to return "0" for the X_AXIS
- switch (axis) {
- case View.X_AXIS:
- return 0;
- case View.Y_AXIS:
- return super.getMinimumSpan(axis);
- default:
- throw new IllegalArgumentException("Invalid axis: " + axis);
+ // enforce behavior as defined by FlowView
+ // --> FlowView.calculateMinorAxisRequirements()
+ if (r == null) {
+ r = new SizeRequirements();
}
+ float pref = layoutPool.getPreferredSpan(axis);
+ float min = layoutPool.getMinimumSpan(axis);
+ // Don't include insets, Box.getXXXSpan will include them.
+ r.minimum = (int) min;
+ r.preferred = Math.max(r.minimum, (int) pref);
+ r.maximum = Integer.MAX_VALUE;
+ r.alignment = 0.5f;
+ return r;
+ // <-- FlowView.calculateMinorAxisRequirements()
}
};
}

Back to the top