Skip to content

Implements a StyleProcessor class to allow access to a list of set styles for a widget in String format. #2392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@
</message_arguments>
</filter>
</resource>
<resource path="Eclipse SWT/win32/org/eclipse/swt/widgets/StyleProcessor.java" type="org.eclipse.swt.widgets.StyleProcessor">
<filter comment="Testing" id="1110441988">
<message_arguments>
<message_argument value="org.eclipse.swt.widgets.StyleProcessor"/>
</message_arguments>
</filter>
</resource>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package org.eclipse.swt.widgets;


import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.internal.win32.version.*;


/**
* Instances of this class represent a selectable user interface object that
* issues notification when pressed and released.
Expand Down Expand Up @@ -265,6 +268,23 @@ long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
return OS.CallWindowProc (ButtonProc, hwnd, msg, wParam, lParam);
}

private static StyleProcessor STYLE_PROCESSOR = new StyleProcessor();

static {
StyleProcessor processor = new StyleProcessor();

if (COMMAND_LINK) {
processor.someOf("COMMAND");
} else {
processor.oneOf("PUSH, ARROW, CHECK, RADIO, TOGGLE");
}
processor.ifOneOf("PUSH, TOGGLE").thenOneOf("CENTER, LEFT, RIGHT")
.ifOneOf("CHECK, RADIO").thenOneOf("LEFT, RIGHT, CENTER")
.ifOneOf("ARROW").thenOneOf("UP, DOWN, LEFT, RIGHT");

STYLE_PROCESSOR = processor;
}

static int checkStyle (int style) {
style = checkBits (style, SWT.PUSH, SWT.ARROW, SWT.CHECK, SWT.RADIO, SWT.TOGGLE, COMMAND_LINK ? SWT.COMMAND : 0);
if ((style & (SWT.PUSH | SWT.TOGGLE)) != 0) {
Expand All @@ -278,6 +298,15 @@ static int checkStyle (int style) {
return checkBits (style, SWT.UP, SWT.DOWN, SWT.LEFT, SWT.RIGHT, 0, 0);
}
return style;

}
/**
* test
* @return ArrayList<String> styles
* @since 3.131
*/
public ArrayList<String> getStyles() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One would never want to surface the List implementation class in any API; elsewhere too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @merks,

Answering to your question...

Given the implementation details I see, could this not all be done externally without introducing APIs in SWT?

We initially explored other approaches such as implementing it in a utility class or using annotations, however based on the comments and guidance that we got Issue #611. We were advised to directly change the SWT repository, specifically the widget classes instead. That being said, we are flexible and happy to revise the implementation.

One would never want to surface the List implementation class in any API; elsewhere too.

You are right with what you've said. My teammate and I are planning to change this ArrayList to List instead and avoiding making the method public. We've only did it this way for testing purposes to see if it works and get feedback.

Thank you so much!
Teammate - @WillPeltz

return STYLE_PROCESSOR.process(this.style);
}

void click () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package org.eclipse.swt.widgets;

import java.util.*;

import org.eclipse.swt.*;

public class StyleProcessor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this really become public API or can it just be package protected?

Copy link
Author

@neilcabanilla neilcabanilla Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @merks,

You're absolutely right, we actually intended for StyleProcessor to be package private, so that it can only be accessed within the widgets package. My teammate and I forgot that part, thank you for clarifying.

Thank you so much!
Teammate - @WillPeltz



private static class Rule {
ArrayList<String> ifOneOf;
ArrayList<String> thenOneOf;
ArrayList<String> thenSomeOf;

Rule(ArrayList<String> ifOneOf) {
this.ifOneOf = ifOneOf;
this.thenOneOf = new ArrayList<>();
this.thenSomeOf = new ArrayList<>();
}
}

private ArrayList<Rule> rules = new ArrayList<>();
private ArrayList<ArrayList<String>> oneOfArr = new ArrayList<>();
private ArrayList<String> someOfArr = new ArrayList<>();
private Rule currentRule = null;

public StyleProcessor oneOf(String styles) {
String[] tempHolder = styles.split(", ");

oneOfArr.add(new ArrayList<>(Arrays.asList(tempHolder)));

return this;
}

public StyleProcessor someOf(String styles) {
String[] tempHolder = styles.split(", ");

someOfArr.addAll(Arrays.asList(tempHolder));

return this;
}

public StyleProcessor ifOneOf(String styles) {
String[] tempHolder = styles.split(", ");
ArrayList<String> tempList = new ArrayList<>(Arrays.asList(tempHolder));
currentRule = new Rule(tempList);
rules.add(currentRule);
return this;
}


public StyleProcessor thenOneOf(String styles) {
if (currentRule == null) {
throw new IllegalStateException("No 'ifOneOf' defined before 'thenOneOf'");
}
currentRule.thenOneOf.addAll(Arrays.asList(styles.split(", ")));
return this;
}


public StyleProcessor thenSomeOf(String styles) {
if (currentRule == null) {
throw new IllegalStateException("No 'ifOneOf' defined before 'thenSomeOf'");
}
currentRule.thenSomeOf.addAll(Arrays.asList(styles.split(", ")));
return this;
}



public ArrayList<String> process(int style) {
ArrayList<String> finalList = new ArrayList<>();
for (ArrayList<String> a: oneOfArr) {
for (String s: a) {
try {
if ((style & SWT.class.getField(s).getInt(null)) != 0) {
finalList.add(s);
break;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Invalid Style: " + s);
}
}
}

for (String s : someOfArr) {
try {
if ((style & SWT.class.getField(s).getInt(null)) != 0) {
finalList.add(s);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Invalid Style: " + s);
}
}

for (Rule rule : rules) {
boolean matched = false;

for (String condition : rule.ifOneOf) {
try {
int flag = SWT.class.getField(condition).getInt(null);
if ((style & flag) != 0) {
matched = true;
break;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Invalid condition: " + condition);
}
}

if (matched) {
for (String s : rule.thenOneOf) {
try {
int flag = SWT.class.getField(s).getInt(null);
if ((style & flag) != 0) {
finalList.add(s);
break;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Invalid thenOneOf: " + s);
}
}

for (String s : rule.thenSomeOf) {
try {
int flag = SWT.class.getField(s).getInt(null);
if ((style & flag) != 0) {
finalList.add(s);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
System.out.println("Invalid thenSomeOf: " + s);;
}
}
}
}

return finalList;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,21 @@ void applySegments () {
ignoreVerify = oldIgnoreVerify;
}

private static StyleProcessor STYLE_PROCESSOR = new StyleProcessor();

static {
StyleProcessor processor = new StyleProcessor();

processor.oneOf("SINGLE, MULTI")
.oneOf("LEFT, CENTER, RIGHT")
.someOf("BORDER, SEARCH").ifOneOf("MULTI").thenSomeOf("H_SCROLL, V_SCROLL, WRAP")
.ifOneOf("SINGLE").thenSomeOf("PASSWORD");

STYLE_PROCESSOR = processor;
}

static int checkStyle (int style) {
System.out.println(STYLE_PROCESSOR.process(style));
if ((style & SWT.SINGLE) != 0 && (style & SWT.MULTI) != 0) {
style &= ~SWT.MULTI;
}
Expand All @@ -651,6 +665,15 @@ static int checkStyle (int style) {
return style | SWT.SINGLE;
}

/**
* test
* @return ArrayList<String> styles
* @since 3.131
*/
public ArrayList<String> getStyles() {
return STYLE_PROCESSOR.process(this.style);
}

void clearSegments (boolean applyText) {
if (clearSegmentsCount++ != 0) return;
if (segments == null) return;
Expand Down
Loading