Reorganise some remaining code

This commit is contained in:
2015-10-18 16:25:46 +01:00
parent 91af6d2141
commit f0b55c3bc4
4 changed files with 88 additions and 76 deletions

View File

@@ -5,22 +5,16 @@ import java.util.Random;
import org.synthuse.Api;
import org.synthuse.objects.MenuItem;
import com.sun.jna.platform.win32.WinDef.HMENU;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.WPARAM;
public class Test {
protected Api api;
protected HWND hWndFound;
protected HashMap<HWND, HMENU> windowMap;
protected HashMap<MenuItem, WPARAM> menuItemMap;
protected HashMap<Slider, HashMap<Amount, MenuItem>> sliderMap;
protected HashMap<Slider, HWND> valueMap;
public Test() {
api = new Api();
windowMap = new HashMap<HWND, HMENU>();
menuItemMap = new HashMap<MenuItem, WPARAM>();
sliderMap = new HashMap<Slider, HashMap<Amount, MenuItem>>();
valueMap = new HashMap<Slider, HWND>();
}
@@ -34,15 +28,15 @@ public class Test {
for (int k = 0; k < 5; ++k) {
Slider slider = Slider.values()[new Random().nextInt(Slider.values().length)];
for (int j = 0; j < 5; ++j) {
for (int i = 0; i < 20; ++i) {
test.moveSlider(slider, Amount.DECREASE_MUCH);
for (int i = 0; i < 10; ++i) {
test.moveSlider(slider, Amount.INCREASE_LITTLE);
System.out.println(test.getValue(slider));
Thread.sleep(100);
Thread.sleep(200);
}
Thread.sleep(200);
for (int i = 0; i < 20; ++i) {
test.moveSlider(slider, Amount.INCREASE_MUCH);
Thread.sleep(100);
Thread.sleep(400);
for (int i = 0; i < 10; ++i) {
test.moveSlider(slider, Amount.INCREASE_LITTLE);
Thread.sleep(200);
}
}
}
@@ -50,7 +44,7 @@ public class Test {
protected void moveSlider(Slider slider, Amount amount) throws Exception {
MenuItem menuItem = sliderMap.get(slider).get(amount);
activateItem(menuItem);
api.activateItem(menuItem);
}
protected float getValue(Slider slider) {
@@ -77,7 +71,7 @@ public class Test {
for (Amount amount : Amount.values()) {
String label = slider.getLabel(amount);
path[2] = String.format(" %s", label);
MenuItem menuItem = loadMenuItem(hWndTopWindow, true, path);
MenuItem menuItem = api.loadMenuItem(hWndTopWindow, true, path);
amountMap.put(amount, menuItem);
}
sliderMap.put(slider, amountMap);
@@ -115,44 +109,5 @@ public class Test {
}
}
public boolean activateItem(MenuItem menuItem) throws Exception {
HWND hWnd = menuItem.hWnd;
HMENU hMenu = menuItem.hMenu;
String[] path = menuItem.path;
boolean exact = menuItem.exact;
WPARAM wParam;
if (menuItemMap.containsKey(menuItem)) {
wParam = menuItemMap.get(menuItem);
} else {
wParam = api.loadMenuItem(hWnd, hMenu, exact, path);
}
return api.user32.PostMessage(hWnd, Api.WM_COMMAND, wParam, null).intValue() > 0;
}
public boolean activateItem(HWND hWnd, boolean exact, String... path) throws Exception {
MenuItem menuItem = loadMenuItem(hWnd, exact, path);
return activateItem(menuItem);
}
protected MenuItem loadMenuItem(HWND hWnd, boolean exact, String... path) throws Exception {
HMENU hMenu;
if (windowMap.containsKey(hWnd)) {
hMenu = windowMap.get(hWnd);
} else {
hMenu = api.user32.GetMenu(hWnd);
windowMap.put(hWnd, hMenu);
}
MenuItem menuItem = new MenuItem(hWnd, hMenu, path);
WPARAM wParam = api.loadMenuItem(hWnd, hMenu, exact, path);
menuItemMap.put(menuItem, wParam);
return menuItem;
}
protected WPARAM loadMenuItem(MenuItem menuItem) throws Exception {
HWND hWnd = menuItem.hWnd;
HMENU hMenu = menuItem.hMenu;
String[] path = menuItem.path;
boolean exact = menuItem.exact;
return api.loadMenuItem(hWnd, hMenu, exact, path);
}
}