2011-05-13 144 views
0

我有一个org.eclipse.swt.text小部件。有时候,小部件不够大,无法显示其中的所有信息,有没有办法改变它显示信息的方式而不改变实际内容?org.eclipse.swt.text,自动截断文本?

即应显示“1234 ... 4321”,但当我做getText()时,应返回实际值“123456654321”。

或者是否有不同的SWT小部件可以处理?

回答

9

可惜你不能与org.eclipse.swt.widgets.Text做,因为SWT竟与本地部件集和大多数操作系统的本机文本控件的作品可以有一个状态,即价值。

解决方法是,您可以使用SWT.MULTI|SWT.WRAP标志创建org.eclipse.swt.widgets.Text或将原始文本小部件包装到实用程序类中。

>>Output

enter image description here

>>Sample Wrapper Code

import org.eclipse.swt.graphics.GC; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Control; 
import org.eclipse.swt.widgets.Text; 

public class MyText 
{ 
    private Text text; 
    private String value; 
    public MyText(Composite parent, int style) { 
     text = new Text(parent, style); 
    } 

    public Text getControl() { 
     return text; 
    } 

    public String getText() { 
     return value; 
    } 

    public void setText(String value) { 
     if(text != null && !text.isDisposed()){ 
      this.value = value; 
      text.setText(shortenText(value, text.getParent())); 
     } 
    } 

    private String shortenText(String textValue, Control control) 
    { 
     if (textValue == null) { 
      return null; 
     } 
     GC gc = new GC(control); 
     int maxWidth = control.getBounds().width - 25; 
     int maxExtent = gc.textExtent(textValue).x; 

     System.out.println(maxWidth + "\n" + maxExtent); 
     if (maxExtent < maxWidth) { 
      gc.dispose(); 
      return textValue; 
     } 
     int length = textValue.length(); 
     int charsToClip = Math.round(0.95f*length * (1 - ((float)maxWidth/maxExtent))); 
     int pivot = length/2; 
     int start = pivot - (charsToClip/2); 
     int end = pivot + (charsToClip/2) + 1; 
     while (start >= 0 && end < length) { 
      String s1 = textValue.substring(0, start); 
      String s2 = textValue.substring(end, length); 
      String s = s1 + "..." + s2; 
      int l = gc.textExtent(s).x; 
      if (l < maxWidth) { 
       gc.dispose(); 
       return s; 
      } 
      start--; 
      end++; 
     } 
     gc.dispose(); 
     return textValue; 
    } 
} 

注意shortenText()方法的实现。

>>Main

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Text; 

public class TextWidgetTest 
{ 
    public static void main(String[] args) 
    { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setText("Text Widget Test"); 
     shell.setLayout(new GridLayout()); 
     shell.setSize(300, 200); 

     final MyText text = new MyText(shell, SWT.SINGLE|SWT.BORDER); 
     text.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 
     text.setText("A very very very very very very very very very very very very very very very very very very very very long text"); 

     Button show = new Button(shell, SWT.PUSH); 
     show.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 
     show.setText("Show Actual Text"); 


     final Text console = new Text(shell, SWT.BORDER|SWT.MULTI|SWT.WRAP|SWT.V_SCROLL); 
     console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

     show.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       console.setText(text.getText()); 
      } 
     }); 

     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 

} 
+0

谢谢,这是一个很好的方式来做到这一点。我可能会这样做。 – Fredrik 2011-05-13 14:52:27

+0

你真的为此付出了很多努力:)。我的+1。 – Sandman 2011-05-13 22:38:49

1

使用SWT.MULTI | SWT.WRAP标志设置控件是否会对您有好处?这样,您就可以获得包装文本的多行控件。

这里是如何做到这一点:

Text myText = new Text(parent, SWT.MULTI|SWT.WRAP); 
+0

是啊,那会工作,但它可能会最终看起来不错吧GUI用几行。我想我会用Favonius的例子去。 – Fredrik 2011-05-13 14:54:03

+0

@Fredrik - 当然,可以选择最适合您的需求。很高兴你找到你的答案:)。 – Sandman 2011-05-13 22:41:17