Dies ist eine alte Version des Dokuments!
Text-Control, selbst gezeichnet
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); // mark control text.setEditable(!uneditable); final String painterAddedKey = "painterAddedKey"; //$NON-NLS-1$ 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 (m_uneditableBorderPaintListener == null) { m_uneditableBorderPaintListener = new PaintListener() { public void paintControl(PaintEvent event) { Composite composite = (Composite) event.widget; Control[] children = composite.getChildren(); GC gc = event.gc; gc.setForeground(getUneditableTextBorderColor()); 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 m_uneditableBorderPaintListener; } private Color getUneditableTextBorderColor() { final String colorKey = "COLOR_NONEDITABLE_TEXT_BOREDER"; //$NON-NLS-1$ Color borderColor = getFormToolkit().getColors().getColor(colorKey); if (null == borderColor) { Color origBorderColor = getFormToolkit().getColors().getBorderColor(); int r = brighter(origBorderColor.getRed(), 2, 3); int g = brighter(origBorderColor.getGreen(), 2, 3); int b = brighter(origBorderColor.getBlue(), 2, 3); borderColor = getFormToolkit().getColors().createColor(colorKey, r, g, b); } return borderColor; } private int brighter(final int color, final int x, final int y) { return (255 - color) * x / y + color; }
Verwendung:
Text myText = new Text(); setUneditable(myText, true);