Text-Control, selbst gezeichnet
Ein recht großes Manke des SWT-Text-Controls ist, dass der enthaltenden Text im Zustand disabled nicht kopiert werden kann.
Ferner ist der Text grau und somit recht schlecht lesbar. Der folgende Code behebt diese beiden Nachteile:
public void setUneditable(final Text text, final boolean uneditable) { if (WidgetUtils.canBeUsed(text)) { text.setData(FormToolkit.KEY_DRAW_BORDER, !uneditable); text.setData(UNEDITABLE_FLAG_KEY, uneditable); text.setEditable(!uneditable); final String painterAddedKey = "KEY_PAINTER_ADDED"; final Composite pComp = text.getParent(); if (uneditable && null == pComp.getData(painterAddedKey)) { pComp.setData(painterAddedKey, Boolean.TRUE); pComp.addPaintListener(getUneditableBorderPaintListener()); } } } public static boolean isUneditable(final Control control) { return (Boolean)control.getData(UNEDITABLE_FLAG_KEY) || (control instanceof Button && !control.isEnabled()); } private PaintListener getUneditableBorderPaintListener() { if (uneditableBorderPaintListener == null) { uneditableBorderPaintListener = new PaintListener() { public void paintControl(PaintEvent event) { Composite composite = (Composite) event.widget; Control[] children = composite.getChildren(); GC gc = event.gc; gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY); for (int i = 0; i < children.length; i++) { Control c = children[i]; if ((Boolean)c.getData(UNEDITABLE_FLAG_KEY)) { final Rectangle b = c.getBounds(); int x1 = b.x - 1; int x2 = b.x + b.width; int by = b.y + b.height; gc.drawLine(x1, by, x2, by); gc.drawLine(x1, by - 1, x1, by); gc.drawLine(x2, by, x2, by - 1); } } } }; } return uneditableBorderPaintListener; }
Verwendung:
Text myText = ... setUneditable(myText, true);