-
Notifications
You must be signed in to change notification settings - Fork 177
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
a9762c4
f9f0471
044e1a2
ce06906
4222a50
67301df
eefecfc
29a95f4
48ac14e
e56c367
1079359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! |
||
|
||
|
||
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; | ||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
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.
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