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 all 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 @@ -15,13 +15,16 @@
package org.eclipse.swt.widgets;


import java.util.List;

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,11 @@ static int checkStyle (int style) {
return checkBits (style, SWT.UP, SWT.DOWN, SWT.LEFT, SWT.RIGHT, 0, 0);
}
return style;

}

List<String> getStyles() {
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.*;

class StyleProcessor {


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 @@ -14,7 +14,7 @@
package org.eclipse.swt.widgets;


import java.util.*;
import java.util.List;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
Expand Down 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,11 @@ static int checkStyle (int style) {
return style | SWT.SINGLE;
}


List<String> getStyles() {
return STYLE_PROCESSOR.process(this.style);
}

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