[1] | 1 | package negotiator.parties;
|
---|
| 2 |
|
---|
| 3 | import java.awt.Component;
|
---|
| 4 | import java.awt.Container;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 |
|
---|
| 7 | import javax.swing.JButton;
|
---|
| 8 |
|
---|
| 9 | public class ControllableComponent {
|
---|
| 10 | private HashMap<String, Component> componentMap = new HashMap<String, Component>();
|
---|
| 11 | private Component topComponent;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * @param component
|
---|
| 15 | * {@link Component}.
|
---|
| 16 | * @return map with all component names in a awt component.
|
---|
| 17 | */
|
---|
| 18 | public ControllableComponent(Component component) {
|
---|
| 19 | this.topComponent = component;
|
---|
| 20 | findComponents(component);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Register given component and check all children of this component.
|
---|
| 25 | *
|
---|
| 26 | * @param component
|
---|
| 27 | */
|
---|
| 28 | private void findComponents(Component component) {
|
---|
| 29 | componentMap.put(component.getName(), component);
|
---|
| 30 | if (component instanceof Container) {
|
---|
| 31 | for (Component child : ((Container) component).getComponents()) {
|
---|
| 32 | findComponents(child);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Get a component with given name.
|
---|
| 39 | *
|
---|
| 40 | * @param name
|
---|
| 41 | * name of the component
|
---|
| 42 | * @return Component with given name, or null
|
---|
| 43 | */
|
---|
| 44 | public Object get(String name) {
|
---|
| 45 | return componentMap.get(name);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Click a button
|
---|
| 50 | *
|
---|
| 51 | * @param name
|
---|
| 52 | * the name of the button
|
---|
| 53 | * @throws if
|
---|
| 54 | * there is no button with given name.
|
---|
| 55 | */
|
---|
| 56 | public void clickButton(String name) {
|
---|
| 57 | Component c = componentMap.get(name);
|
---|
| 58 | if (c == null) {
|
---|
| 59 | throw new IllegalArgumentException(
|
---|
| 60 | "the component does not contain an element with name "
|
---|
| 61 | + name);
|
---|
| 62 | }
|
---|
| 63 | if (!(c instanceof JButton)) {
|
---|
| 64 | throw new IllegalArgumentException("The component " + name
|
---|
| 65 | + " is not a JButton");
|
---|
| 66 | }
|
---|
| 67 | ((JButton) c).doClick();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * Waits till the component that we contain is actually visible.
|
---|
| 72 | *
|
---|
| 73 | * @throws InterruptedException
|
---|
| 74 | */
|
---|
| 75 | public void waitTillVisible() throws InterruptedException {
|
---|
| 76 | while (!topComponent.isVisible()) {
|
---|
| 77 | Thread.sleep(100);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Wait in parallel thread till panel becomes visible. Then click on button.
|
---|
| 83 | *
|
---|
| 84 | * @param control
|
---|
| 85 | * @param buttonName
|
---|
| 86 | */
|
---|
| 87 | public void delayedClickOnButton(final String buttonName) {
|
---|
| 88 | new Thread(new Runnable() {
|
---|
| 89 |
|
---|
| 90 | @Override
|
---|
| 91 | public void run() {
|
---|
| 92 | try {
|
---|
| 93 | waitTillVisible();
|
---|
| 94 | } catch (InterruptedException e) {
|
---|
| 95 | e.printStackTrace();
|
---|
| 96 | }
|
---|
| 97 | clickButton(buttonName);
|
---|
| 98 | }
|
---|
| 99 | }).start();
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | }
|
---|