blob: dc6a4f389b107d4ebe48418d0fd6184ea250c1e7 [file] [log] [blame]
Andreas Gampe43747492016-11-28 15:07:39 -08001package com.android.preload.ui;
2
3import com.android.ddmlib.Client;
4import com.android.ddmlib.ClientData;
5import java.io.File;
6import java.util.LinkedList;
7import java.util.List;
8import javax.swing.Action;
9import javax.swing.ListModel;
10import javax.swing.table.TableModel;
11
12public class SequenceUI implements IUI {
13
14 private ListModel<Client> clientListModel;
15 @SuppressWarnings("unused")
16 private TableModel dataTableModel;
17 private List<Action> actions;
18
19 private List<Object> sequence = new LinkedList<>();
20
21 public SequenceUI() {
22 }
23
24 @Override
25 public boolean isSingleThreaded() {
26 return true;
27 }
28
29 @Override
30 public void prepare(ListModel<Client> clientListModel, TableModel dataTableModel,
31 List<Action> actions) {
32 this.clientListModel = clientListModel;
33 this.dataTableModel = dataTableModel;
34 this.actions = actions;
35 }
36
37 public SequenceUI action(Action a) {
38 sequence.add(a);
39 return this;
40 }
41
42 public SequenceUI action(Class<? extends Action> actionClass) {
43 for (Action a : actions) {
44 if (actionClass.equals(a.getClass())) {
45 sequence.add(a);
46 return this;
47 }
48 }
49 throw new IllegalArgumentException("No action of class " + actionClass + " found.");
50 }
51
52 public SequenceUI confirmYes() {
53 sequence.add(Boolean.TRUE);
54 return this;
55 }
56
57 public SequenceUI confirmNo() {
58 sequence.add(Boolean.FALSE);
59 return this;
60 }
61
62 public SequenceUI input(String input) {
63 sequence.add(input);
64 return this;
65 }
66
67 public SequenceUI input(File... f) {
68 sequence.add(f);
69 return this;
70 }
71
72 public SequenceUI output(File f) {
73 sequence.add(f);
74 return this;
75 }
76
77 public SequenceUI tableRow(int i) {
78 sequence.add(i);
79 return this;
80 }
81
82 private class ClientSelector {
83 private String pkg;
84
85 public ClientSelector(String pkg) {
86 this.pkg = pkg;
87 }
88
89 public Client getClient() {
90 for (int i = 0; i < clientListModel.getSize(); i++) {
91 ClientData cd = clientListModel.getElementAt(i).getClientData();
92 if (cd != null) {
93 String s = cd.getClientDescription();
94 if (pkg.equals(s)) {
95 return clientListModel.getElementAt(i);
96 }
97 }
98 }
99 throw new RuntimeException("Didn't find client " + pkg);
100 }
101 }
102
103 public SequenceUI client(String pkg) {
104 sequence.add(new ClientSelector(pkg));
105 return this;
106 }
107
108 public SequenceUI choice(String pattern) {
109 sequence.add(pattern);
110 return this;
111 }
112
113 @Override
114 public void ready() {
115 // Run the actions.
116 // No iterator or foreach loop as the sequence will be emptied while running.
117 try {
118 while (!sequence.isEmpty()) {
119 Object next = sequence.remove(0);
120 if (next instanceof Action) {
121 ((Action)next).actionPerformed(null);
122 } else {
123 throw new IllegalStateException("Didn't expect a non-action: " + next);
124 }
125 }
126 } catch (Exception e) {
127 e.printStackTrace(System.out);
128 }
129
130 // Now shut down.
131 System.exit(0);
132 }
133
134 @Override
135 public Client getSelectedClient() {
136 Object next = sequence.remove(0);
137 if (next instanceof ClientSelector) {
138 return ((ClientSelector)next).getClient();
139 }
140 throw new IllegalStateException("Unexpected: " + next);
141 }
142
143 @Override
144 public int getSelectedDataTableRow() {
145 Object next = sequence.remove(0);
146 if (next instanceof Integer) {
147 return ((Integer)next).intValue();
148 }
149 throw new IllegalStateException("Unexpected: " + next);
150 }
151
152 @Override
153 public void showWaitDialog() {
154 }
155
156 @Override
157 public void updateWaitDialog(String s) {
158 System.out.println(s);
159 }
160
161 @Override
162 public void hideWaitDialog() {
163 }
164
165 @Override
166 public void showMessageDialog(String s) {
167 System.out.println(s);
168 }
169
170 @Override
171 public boolean showConfirmDialog(String title, String message) {
172 Object next = sequence.remove(0);
173 if (next instanceof Boolean) {
174 return ((Boolean)next).booleanValue();
175 }
176 throw new IllegalStateException("Unexpected: " + next);
177 }
178
179 @Override
180 public String showInputDialog(String message) {
181 Object next = sequence.remove(0);
182 if (next instanceof String) {
183 return (String)next;
184 }
185 throw new IllegalStateException("Unexpected: " + next);
186 }
187
188 @Override
189 public <T> T showChoiceDialog(String title, String message, T[] choices) {
190 Object next = sequence.remove(0);
191 if (next instanceof String) {
192 String s = (String)next;
193 for (T t : choices) {
194 if (t.toString().contains(s)) {
195 return t;
196 }
197 }
198 return null;
199 }
200 throw new IllegalStateException("Unexpected: " + next);
201 }
202
203 @Override
204 public File showSaveDialog() {
205 Object next = sequence.remove(0);
206 if (next instanceof File) {
207 System.out.println(next);
208 return (File)next;
209 }
210 throw new IllegalStateException("Unexpected: " + next);
211 }
212
213 @Override
214 public File[] showOpenDialog(boolean multi) {
215 Object next = sequence.remove(0);
216 if (next instanceof File[]) {
217 return (File[])next;
218 }
219 throw new IllegalStateException("Unexpected: " + next);
220 }
221
222}