Preload2: Abstract out UI

am: 5cb8998363

Change-Id: Ia4a147e061397951ada72beac7c5781b8dbac918
This commit is contained in:
Andreas Gampe
2016-11-29 03:19:34 +00:00
committed by android-build-merger
4 changed files with 78 additions and 16 deletions

View File

@@ -32,7 +32,8 @@ import com.android.preload.actions.ShowDataAction;
import com.android.preload.classdataretrieval.ClassDataRetriever; import com.android.preload.classdataretrieval.ClassDataRetriever;
import com.android.preload.classdataretrieval.hprof.Hprof; import com.android.preload.classdataretrieval.hprof.Hprof;
import com.android.preload.classdataretrieval.jdwp.JDWPClassDataRetriever; import com.android.preload.classdataretrieval.jdwp.JDWPClassDataRetriever;
import com.android.preload.ui.UI; import com.android.preload.ui.IUI;
import com.android.preload.ui.SwingUI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@@ -66,7 +67,7 @@ public class Main {
private DumpTableModel dataTableModel; private DumpTableModel dataTableModel;
private DefaultListModel<Client> clientListModel; private DefaultListModel<Client> clientListModel;
private UI ui; private IUI ui;
// Actions that need to be updated once a device is selected. // Actions that need to be updated once a device is selected.
private Collection<DeviceSpecific> deviceSpecificActions; private Collection<DeviceSpecific> deviceSpecificActions;
@@ -89,13 +90,15 @@ public class Main {
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Main m = new Main(); Main m = new Main(new SwingUI());
top = m; top = m;
m.startUp(); m.startUp();
} }
public Main() { public Main(IUI ui) {
this.ui = ui;
clientListModel = new DefaultListModel<Client>(); clientListModel = new DefaultListModel<Client>();
dataTableModel = new DumpTableModel(); dataTableModel = new DumpTableModel();
@@ -124,11 +127,10 @@ public class Main {
} }
} }
ui = new UI(clientListModel, dataTableModel, actions); ui.prepare(clientListModel, dataTableModel, actions);
ui.setVisible(true);
} }
public static UI getUI() { public static IUI getUI() {
return top.ui; return top.ui;
} }
@@ -176,6 +178,7 @@ public class Main {
new ReloadListAction(clientUtils, getDevice(), clientListModel).run(); new ReloadListAction(clientUtils, getDevice(), clientListModel).run();
getUI().hideWaitDialog(); getUI().hideWaitDialog();
getUI().ready();
} }
private void initDevice() { private void initDevice() {

View File

@@ -32,7 +32,6 @@ import java.util.TreeSet;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
/** /**
* Compute an intersection of classes from the given data. A class is in the intersection if it * Compute an intersection of classes from the given data. A class is in the intersection if it
@@ -92,10 +91,8 @@ public class ComputeThresholdAction extends AbstractAction implements Runnable {
boolean ret = Main.getUI().showConfirmDialog("Computed a set with " + result.size() boolean ret = Main.getUI().showConfirmDialog("Computed a set with " + result.size()
+ " classes, would you like to save to disk?", "Save?"); + " classes, would you like to save to disk?", "Save?");
if (ret) { if (ret) {
JFileChooser jfc = new JFileChooser(); File f = Main.getUI().showSaveDialog();
int ret2 = jfc.showSaveDialog(Main.getUI()); if (f != null) {
if (ret2 == JFileChooser.APPROVE_OPTION) {
File f = jfc.getSelectedFile();
saveSet(result, f); saveSet(result, f);
} }
} }

View File

@@ -0,0 +1,43 @@
package com.android.preload.ui;
import com.android.ddmlib.Client;
import java.io.File;
import java.util.List;
import javax.swing.Action;
import javax.swing.ListModel;
import javax.swing.table.TableModel;
/**
* UI abstraction for the tool. This allows a graphical mode, command line mode,
* or silent mode.
*/
public interface IUI {
void prepare(ListModel<Client> clientListModel, TableModel dataTableModel,
List<Action> actions);
void ready();
Client getSelectedClient();
int getSelectedDataTableRow();
void showWaitDialog();
void updateWaitDialog(String s);
void hideWaitDialog();
void showMessageDialog(String s);
boolean showConfirmDialog(String title, String message);
String showInputDialog(String message);
<T> T showChoiceDialog(String title, String message, T[] choices);
File showSaveDialog();
File[] showOpenDialog(boolean multi);
}

View File

@@ -41,7 +41,7 @@ import javax.swing.ListModel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.table.TableModel; import javax.swing.table.TableModel;
public class UI extends JFrame { public class SwingUI extends JFrame implements IUI {
private JList<Client> clientList; private JList<Client> clientList;
private JTable dataTable; private JTable dataTable;
@@ -49,11 +49,13 @@ public class UI extends JFrame {
// Shared file chooser, means the directory is retained. // Shared file chooser, means the directory is retained.
private JFileChooser jfc; private JFileChooser jfc;
public UI(ListModel<Client> clientListModel, public SwingUI() {
TableModel dataTableModel,
List<Action> actions) {
super("Preloaded-classes computation"); super("Preloaded-classes computation");
}
@Override
public void prepare(ListModel<Client> clientListModel, TableModel dataTableModel,
List<Action> actions) {
getContentPane().add(new JScrollPane(clientList = new JList<Client>(clientListModel)), getContentPane().add(new JScrollPane(clientList = new JList<Client>(clientListModel)),
BorderLayout.WEST); BorderLayout.WEST);
clientList.setCellRenderer(new ClientListCellRenderer()); clientList.setCellRenderer(new ClientListCellRenderer());
@@ -74,18 +76,27 @@ public class UI extends JFrame {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600); setBounds(100, 100, 800, 600);
setVisible(true);
} }
@Override
public void ready() {
}
@Override
public Client getSelectedClient() { public Client getSelectedClient() {
return clientList.getSelectedValue(); return clientList.getSelectedValue();
} }
@Override
public int getSelectedDataTableRow() { public int getSelectedDataTableRow() {
return dataTable.getSelectedRow(); return dataTable.getSelectedRow();
} }
private JDialog currentWaitDialog = null; private JDialog currentWaitDialog = null;
@Override
public void showWaitDialog() { public void showWaitDialog() {
if (currentWaitDialog == null) { if (currentWaitDialog == null) {
currentWaitDialog = new JDialog(this, "Please wait...", true); currentWaitDialog = new JDialog(this, "Please wait...", true);
@@ -111,6 +122,7 @@ public class UI extends JFrame {
}); });
} }
@Override
public void updateWaitDialog(String s) { public void updateWaitDialog(String s) {
if (currentWaitDialog != null) { if (currentWaitDialog != null) {
((JLabel) currentWaitDialog.getContentPane().getComponent(0)).setText(s); ((JLabel) currentWaitDialog.getContentPane().getComponent(0)).setText(s);
@@ -124,6 +136,7 @@ public class UI extends JFrame {
} }
} }
@Override
public void hideWaitDialog() { public void hideWaitDialog() {
if (currentWaitDialog != null) { if (currentWaitDialog != null) {
currentWaitDialog.setVisible(false); currentWaitDialog.setVisible(false);
@@ -131,6 +144,7 @@ public class UI extends JFrame {
} }
} }
@Override
public void showMessageDialog(String s) { public void showMessageDialog(String s) {
// Hide the wait dialog... // Hide the wait dialog...
if (currentWaitDialog != null) { if (currentWaitDialog != null) {
@@ -147,6 +161,7 @@ public class UI extends JFrame {
} }
} }
@Override
public boolean showConfirmDialog(String title, String message) { public boolean showConfirmDialog(String title, String message) {
// Hide the wait dialog... // Hide the wait dialog...
if (currentWaitDialog != null) { if (currentWaitDialog != null) {
@@ -164,6 +179,7 @@ public class UI extends JFrame {
} }
} }
@Override
public String showInputDialog(String message) { public String showInputDialog(String message) {
// Hide the wait dialog... // Hide the wait dialog...
if (currentWaitDialog != null) { if (currentWaitDialog != null) {
@@ -180,6 +196,7 @@ public class UI extends JFrame {
} }
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T showChoiceDialog(String title, String message, T[] choices) { public <T> T showChoiceDialog(String title, String message, T[] choices) {
// Hide the wait dialog... // Hide the wait dialog...
@@ -203,6 +220,7 @@ public class UI extends JFrame {
} }
} }
@Override
public File showSaveDialog() { public File showSaveDialog() {
// Hide the wait dialog... // Hide the wait dialog...
if (currentWaitDialog != null) { if (currentWaitDialog != null) {
@@ -228,6 +246,7 @@ public class UI extends JFrame {
} }
} }
@Override
public File[] showOpenDialog(boolean multi) { public File[] showOpenDialog(boolean multi) {
// Hide the wait dialog... // Hide the wait dialog...
if (currentWaitDialog != null) { if (currentWaitDialog != null) {