From b0d3d207c73f84310ee19e6ad35dbb2ccf0d66ef Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 22 Jun 2023 11:48:52 +0200 Subject: [PATCH] Improve editable text --- .../argeo/cms/swt/widgets/EditableText.java | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/EditableText.java b/swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/EditableText.java index 0612e8f9b..6ba03e86e 100644 --- a/swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/EditableText.java +++ b/swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/EditableText.java @@ -21,6 +21,14 @@ public class EditableText extends StyledControl { private boolean useTextAsLabel = false; + /** + * Message to display if there is not value. Only used with SWT.FLAT (label + * displayed with a {@link Text}) + * + * @see Text#setMessage(String) + */ + private String message; + public EditableText(Composite parent, int style) { super(parent, style); editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY)); @@ -54,9 +62,11 @@ public class EditableText extends StyledControl { } protected Text createTextLabel(Composite box, String style) { - Text lbl = new Text(box, getStyle() | (multiLine ? SWT.MULTI : SWT.SINGLE)); + Text lbl = new Text(box, getStyle() | (multiLine ? SWT.MULTI | SWT.WRAP : SWT.SINGLE)); lbl.setEditable(false); - lbl.setLayoutData(CmsSwtUtils.fillWidth()); + if (message != null) + lbl.setMessage(message); + lbl.setLayoutData(multiLine ? CmsSwtUtils.fillAll() : CmsSwtUtils.fillWidth()); if (style != null) CmsSwtUtils.style(lbl, style); CmsSwtUtils.markup(lbl); @@ -68,14 +78,15 @@ public class EditableText extends StyledControl { protected Text createText(Composite box, String style, boolean editable) { highlight = new Composite(box, SWT.NONE); highlight.setBackground(highlightColor); - GridData highlightGd = new GridData(SWT.FILL, SWT.FILL, false, false); + GridData highlightGd = new GridData(SWT.FILL, SWT.FILL, false, multiLine); highlightGd.widthHint = 5; - highlightGd.heightHint = 3; + if (!multiLine) + highlightGd.heightHint = 3; highlight.setLayoutData(highlightGd); final Text text = new Text(box, getStyle() | (multiLine ? SWT.MULTI : SWT.SINGLE) | SWT.WRAP); text.setEditable(editable); - GridData textLayoutData = CmsSwtUtils.fillWidth(); + GridData textLayoutData = multiLine ? CmsSwtUtils.fillAll() : CmsSwtUtils.fillWidth(); // textLayoutData.heightHint = preferredHeight; text.setLayoutData(textLayoutData); if (style != null) @@ -132,4 +143,31 @@ public class EditableText extends StyledControl { this.useTextAsLabel = useTextAsLabel; } + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + Control control = getControl(); + if (control != null && control instanceof Text txt) + txt.setMessage(this.message); + } + + @Override + protected void setContainerLayoutData(Composite composite) { + if (multiLine) + composite.setLayoutData(CmsSwtUtils.fillAll()); + else + super.setContainerLayoutData(composite); + } + + @Override + protected void setControlLayoutData(Control control) { +// if (multiLine) +// control.setLayoutData(CmsSwtUtils.fillAll()); +// else + super.setControlLayoutData(control); + } + } -- 2.30.2