Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Pazderski2019-11-14 04:58:31 -0500
committerNiraj Modi2019-11-19 03:00:01 -0500
commit7b627530c28727b3a02ceba26562f3002339ca13 (patch)
tree2e4a8469ea56f17da4c2a9c8a88fa8aedd264242
parentc81c2aa19577c43e63ccbca01bce9ac7920bad5d (diff)
downloadeclipse.platform.swt-7b627530c28727b3a02ceba26562f3002339ca13.tar.gz
eclipse.platform.swt-7b627530c28727b3a02ceba26562f3002339ca13.tar.xz
eclipse.platform.swt-7b627530c28727b3a02ceba26562f3002339ca13.zip
Bug 349112 - Allow setting alignment on CCombo text field
Change-Id: Id8bd5ebbb452f8f345c54c405a311091600db319 Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java114
-rw-r--r--examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF2
-rw-r--r--examples/org.eclipse.swt.examples/pom.xml2
-rw-r--r--examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CComboTab.java30
-rw-r--r--tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CCombo.java20
5 files changed, 142 insertions, 26 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java
index b87b045a5e..db9e9d5706 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CCombo.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Paul Pazderski - Bug 349112: allow setting alignment on the text field
*******************************************************************************/
package org.eclipse.swt.custom;
@@ -32,8 +33,8 @@ import org.eclipse.swt.widgets.*;
* attempts to set the height of a Combo are ignored. CCombo can be used
* anywhere that having the increased flexibility is more important than
* getting native L&amp;F, but the decision should not be taken lightly.
- * There is no is no strict requirement that CCombo look or behave
- * the same as the native combo box.
+ * There is no strict requirement that CCombo look or behave the same as
+ * the native combo box.
* </p>
* <p>
* Note that although this class is a subclass of <code>Composite</code>,
@@ -41,7 +42,7 @@ import org.eclipse.swt.widgets.*;
* </p>
* <dl>
* <dt><b>Styles:</b>
- * <dd>BORDER, READ_ONLY, FLAT</dd>
+ * <dd>BORDER, READ_ONLY, FLAT, LEAD, LEFT, CENTER, TRAIL, RIGHT</dd>
* <dt><b>Events:</b>
* <dd>DefaultSelection, Modify, Selection, Verify</dd>
* </dl>
@@ -91,20 +92,17 @@ public class CCombo extends Composite {
* @see SWT#BORDER
* @see SWT#READ_ONLY
* @see SWT#FLAT
+ * @see SWT#LEAD
+ * @see SWT#LEFT
+ * @see SWT#CENTER
+ * @see SWT#RIGHT
+ * @see SWT#TRAIL
* @see Widget#getStyle()
*/
public CCombo (Composite parent, int style) {
super (parent, style = checkStyle (style));
_shell = super.getShell ();
- int textStyle = SWT.SINGLE;
- if ((style & SWT.READ_ONLY) != 0) textStyle |= SWT.READ_ONLY;
- if ((style & SWT.FLAT) != 0) textStyle |= SWT.FLAT;
- text = new Text (this, textStyle);
- int arrowStyle = SWT.ARROW | SWT.DOWN;
- if ((style & SWT.FLAT) != 0) arrowStyle |= SWT.FLAT;
- arrow = new Button (this, arrowStyle);
-
listener = event -> {
if (isDisposed ()) return;
if (popup == event.widget) {
@@ -134,6 +132,12 @@ public CCombo (Composite parent, int style) {
});
}
};
+
+ createText(parent, style);
+
+ int arrowStyle = SWT.ARROW | SWT.DOWN;
+ if ((style & SWT.FLAT) != 0) arrowStyle |= SWT.FLAT;
+ arrow = new Button (this, arrowStyle);
filter = event -> {
if (isDisposed ()) return;
if (event.type == SWT.Selection) {
@@ -151,11 +155,6 @@ public CCombo (Composite parent, int style) {
int [] comboEvents = {SWT.Dispose, SWT.FocusIn, SWT.Move, SWT.Resize, SWT.FocusOut};
for (int i=0; i<comboEvents.length; i++) this.addListener (comboEvents [i], listener);
- int [] textEvents = {SWT.DefaultSelection, SWT.DragDetect, SWT.KeyDown, SWT.KeyUp, SWT.MenuDetect, SWT.Modify,
- SWT.MouseDown, SWT.MouseUp, SWT.MouseDoubleClick, SWT.MouseEnter, SWT.MouseExit, SWT.MouseHover,
- SWT.MouseMove, SWT.MouseWheel, SWT.Traverse, SWT.FocusIn, SWT.Verify};
- for (int i=0; i<textEvents.length; i++) text.addListener (textEvents [i], listener);
-
int [] arrowEvents = {SWT.DragDetect, SWT.MouseDown, SWT.MouseEnter, SWT.MouseExit, SWT.MouseHover,
SWT.MouseMove, SWT.MouseUp, SWT.MouseWheel, SWT.Selection, SWT.FocusIn};
for (int i=0; i<arrowEvents.length; i++) arrow.addListener (arrowEvents [i], listener);
@@ -172,9 +171,57 @@ public CCombo (Composite parent, int style) {
initAccessible();
}
static int checkStyle (int style) {
- int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
+ int mask = SWT.BORDER | SWT.READ_ONLY | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.LEAD | SWT.CENTER | SWT.TRAIL;
return SWT.NO_FOCUS | (style & mask);
}
+void createText(Control parent, int style) {
+ String textValue = null, tooltip = null;
+ Point selection = null;
+ int limit = 0;
+ boolean enabled = false, focus = false, editable = false;
+ Font font = null;
+ Color fg = null, bg = null;
+ Menu menu = null;
+ if (text != null) {
+ textValue = text.getText();
+ tooltip = text.getToolTipText();
+ selection = text.getSelection();
+ limit = text.getTextLimit();
+ enabled = text.isEnabled();
+ editable = text.getEditable();
+ focus = text.isFocusControl();
+ font = text.getFont();
+ fg = text.getForeground();
+ bg = text.getBackground();
+ menu = text.getMenu();
+ text.dispose();
+ }
+
+ int textStyle = style | SWT.SINGLE;
+ if ((style & SWT.READ_ONLY) != 0) textStyle |= SWT.READ_ONLY;
+ if ((style & SWT.FLAT) != 0) textStyle |= SWT.FLAT;
+ textStyle |= style & (SWT.LEAD | SWT.CENTER | SWT.TRAIL);
+ text = new Text (this, textStyle);
+ if (textValue != null) {
+ text.setText(textValue);
+ text.setToolTipText(tooltip);
+ if (selection != null) text.setSelection(selection);
+ text.setTextLimit(limit);
+ text.setEnabled(enabled);
+ text.setEditable(editable);
+ if (focus) text.setFocus();
+ if (font != null) text.setFont(font);
+ if (fg != null) text.setForeground(fg);
+ if (bg != null) text.setBackground(bg);
+ if (menu != null) text.setMenu(menu);
+ internalLayout(true);
+ }
+
+ int [] textEvents = {SWT.DefaultSelection, SWT.DragDetect, SWT.KeyDown, SWT.KeyUp, SWT.MenuDetect, SWT.Modify,
+ SWT.MouseDown, SWT.MouseUp, SWT.MouseDoubleClick, SWT.MouseEnter, SWT.MouseExit, SWT.MouseHover,
+ SWT.MouseMove, SWT.MouseWheel, SWT.Traverse, SWT.FocusIn, SWT.Verify};
+ for (int i=0; i<textEvents.length; i++) text.addListener (textEvents [i], listener);
+}
/**
* Adds the argument to the end of the receiver's list.
* <p>
@@ -660,6 +707,16 @@ String getAssociatedLabel () {
}
return null;
}
+/**
+ * Returns the horizontal alignment.
+ * The alignment style (LEFT, CENTER or RIGHT) is returned.
+ *
+ * @return SWT.LEFT, SWT.RIGHT or SWT.CENTER
+ * @since 3.113
+ */
+public int getAlignment() {
+ return text.getStyle() & (SWT.LEFT | SWT.CENTER | SWT.RIGHT);
+}
@Override
public Control [] getChildren () {
checkWidget();
@@ -827,6 +884,8 @@ public int getStyle () {
int style = super.getStyle ();
style &= ~SWT.READ_ONLY;
if (!text.getEditable()) style |= SWT.READ_ONLY;
+ style &= ~(SWT.LEFT | SWT.CENTER | SWT.RIGHT);
+ style |= getAlignment();
return style;
}
/**
@@ -1463,6 +1522,25 @@ public void select (int index) {
}
}
}
+
+/**
+ * Set the horizontal alignment of the CLabel.
+ * Use the values LEFT, CENTER and RIGHT to align image and text within the available space.
+ *
+ * @param align the alignment style of LEFT, RIGHT or CENTER
+ *
+ * @exception SWTException <ul>
+ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
+ * <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER</li>
+ * </ul>
+ * @since 3.113
+ */
+public void setAlignment(int align) {
+ checkWidget();
+ int styleWithoutAlign = getStyle() & ~(SWT.LEFT | SWT.CENTER | SWT.RIGHT);
+ createText(getParent(), styleWithoutAlign | align);
+}
@Override
public void setBackground (Color color) {
super.setBackground(color);
diff --git a/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF b/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF
index fe1220e474..87d73bd575 100644
--- a/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF
+++ b/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %plugin.SWTStandaloneExampleSet.name
Bundle-SymbolicName: org.eclipse.swt.examples; singleton:=true
-Bundle-Version: 3.106.600.qualifier
+Bundle-Version: 3.106.700.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
diff --git a/examples/org.eclipse.swt.examples/pom.xml b/examples/org.eclipse.swt.examples/pom.xml
index bae768f91c..2e58a3d1e2 100644
--- a/examples/org.eclipse.swt.examples/pom.xml
+++ b/examples/org.eclipse.swt.examples/pom.xml
@@ -19,6 +19,6 @@
</parent>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.examples</artifactId>
- <version>3.106.600-SNAPSHOT</version>
+ <version>3.106.700-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CComboTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CComboTab.java
index 064dd4d05d..237ab29917 100644
--- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CComboTab.java
+++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CComboTab.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,12 +14,15 @@
package org.eclipse.swt.examples.controlexample;
-import org.eclipse.swt.*;
-import org.eclipse.swt.widgets.*;
-import org.eclipse.swt.custom.*;
-import org.eclipse.swt.layout.*;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CCombo;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Widget;
-class CComboTab extends Tab {
+class CComboTab extends AlignableTab {
/* Example widgets and groups that contain them */
CCombo combo1;
@@ -70,6 +73,9 @@ class CComboTab extends Tab {
if (flatButton.getSelection ()) style |= SWT.FLAT;
if (readOnlyButton.getSelection ()) style |= SWT.READ_ONLY;
if (borderButton.getSelection ()) style |= SWT.BORDER;
+ if (leftButton.getSelection ()) style |= SWT.LEFT;
+ if (centerButton.getSelection ()) style |= SWT.CENTER;
+ if (rightButton.getSelection ()) style |= SWT.RIGHT;
/* Create the example widgets */
combo1 = new CCombo (comboGroup, style);
@@ -129,5 +135,17 @@ class CComboTab extends Tab {
flatButton.setSelection ((combo1.getStyle () & SWT.FLAT) != 0);
readOnlyButton.setSelection ((combo1.getStyle () & SWT.READ_ONLY) != 0);
borderButton.setSelection ((combo1.getStyle () & SWT.BORDER) != 0);
+ leftButton.setSelection ((combo1.getStyle () & SWT.LEFT) != 0);
+ centerButton.setSelection ((combo1.getStyle () & SWT.CENTER) != 0);
+ rightButton.setSelection ((combo1.getStyle () & SWT.RIGHT) != 0);
+ }
+
+ @Override
+ void setExampleWidgetAlignment() {
+ int alignment = 0;
+ if (leftButton.getSelection ()) alignment = SWT.LEFT;
+ if (centerButton.getSelection ()) alignment = SWT.CENTER;
+ if (rightButton.getSelection ()) alignment = SWT.RIGHT;
+ combo1.setAlignment (alignment);
}
}
diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CCombo.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CCombo.java
index deef387cc1..c2e67a841d 100644
--- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CCombo.java
+++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CCombo.java
@@ -1029,4 +1029,24 @@ public void test_setTextLjava_lang_String() {
assertTrue(":c:" + i, ccombo.getText().equals(cases[i]));
}
}
+
+@Test
+public void test_setAlignment() {
+ assertEquals(":a:", SWT.LEAD, ccombo.getAlignment());
+
+ ccombo.setText("Trail");
+ ccombo.setAlignment(SWT.TRAIL);
+ assertEquals(":b:", SWT.TRAIL, ccombo.getAlignment());
+ assertEquals(":b:", "Trail", ccombo.getText());
+
+ ccombo.add("Center");
+ ccombo.select(ccombo.getItemCount() - 1);
+ ccombo.setAlignment(SWT.CENTER);
+ assertEquals(":c:", SWT.CENTER, ccombo.getAlignment());
+ assertEquals(":c:", "Center", ccombo.getText());
+
+ ccombo.setAlignment(SWT.LEFT);
+ assertEquals(":d:", SWT.LEFT, ccombo.getAlignment());
+ assertEquals(":d:", "Center", ccombo.getText());
+}
}

Back to the top