Merge changes I37c8daa6,I5a05b65d,If56347fd

* changes:
  Preload2: Add isSingleThreaded
  Preload2: Fix action inheritance
  Preload2: Abstract out UI
This commit is contained in:
Treehugger Robot
2016-11-29 03:08:20 +00:00
committed by Gerrit Code Review
8 changed files with 103 additions and 27 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

@@ -16,6 +16,7 @@
package com.android.preload.actions; package com.android.preload.actions;
import com.android.preload.Main;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
@@ -28,7 +29,11 @@ public abstract class AbstractThreadedAction extends AbstractAction implements R
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
new Thread(this).start(); if (Main.getUI().isSingleThreaded()) {
run();
} else {
new Thread(this).start();
}
} }
} }

View File

@@ -32,14 +32,13 @@ 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
* appears in at least the number of threshold given packages. An optional blacklist can be * appears in at least the number of threshold given packages. An optional blacklist can be
* used to filter classes from the intersection. * used to filter classes from the intersection.
*/ */
public class ComputeThresholdAction extends AbstractAction implements Runnable { public class ComputeThresholdAction extends AbstractThreadedAction {
protected int threshold; protected int threshold;
private Pattern blacklist; private Pattern blacklist;
private DumpTableModel dataTableModel; private DumpTableModel dataTableModel;
@@ -72,7 +71,7 @@ public class ComputeThresholdAction extends AbstractAction implements Runnable {
return; return;
} }
new Thread(this).start(); super.actionPerformed(e);
} }
@Override @Override
@@ -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

@@ -19,14 +19,11 @@ package com.android.preload.actions;
import com.android.preload.DumpDataIO; import com.android.preload.DumpDataIO;
import com.android.preload.DumpTableModel; import com.android.preload.DumpTableModel;
import com.android.preload.Main; import com.android.preload.Main;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
import javax.swing.AbstractAction; public class ExportAction extends AbstractThreadedAction {
public class ExportAction extends AbstractAction implements Runnable {
private File lastSaveFile; private File lastSaveFile;
private DumpTableModel dataTableModel; private DumpTableModel dataTableModel;
@@ -39,7 +36,7 @@ public class ExportAction extends AbstractAction implements Runnable {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
lastSaveFile = Main.getUI().showSaveDialog(); lastSaveFile = Main.getUI().showSaveDialog();
if (lastSaveFile != null) { if (lastSaveFile != null) {
new Thread(this).start(); super.actionPerformed(e);
} }
} }

View File

@@ -27,7 +27,7 @@ import java.util.Collection;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
public class ImportAction extends AbstractAction implements Runnable { public class ImportAction extends AbstractThreadedAction {
private File[] lastOpenFiles; private File[] lastOpenFiles;
private DumpTableModel dataTableModel; private DumpTableModel dataTableModel;
@@ -40,7 +40,7 @@ public class ImportAction extends AbstractAction implements Runnable {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
lastOpenFiles = Main.getUI().showOpenDialog(true); lastOpenFiles = Main.getUI().showOpenDialog(true);
if (lastOpenFiles != null) { if (lastOpenFiles != null) {
new Thread(this).start(); super.actionPerformed(e);
} }
} }

View File

@@ -58,7 +58,12 @@ public class RunMonkeyAction extends AbstractAction implements DeviceSpecific {
if (packages.isEmpty()) { if (packages.isEmpty()) {
packages = DEFAULT_MONKEY_PACKAGES; packages = DEFAULT_MONKEY_PACKAGES;
} }
new Thread(new RunMonkeyRunnable(packages)).start(); Runnable r = new RunMonkeyRunnable(packages);
if (Main.getUI().isSingleThreaded()) {
r.run();
} else {
new Thread(r).start();
}
} }
private class RunMonkeyRunnable implements Runnable { private class RunMonkeyRunnable implements Runnable {

View File

@@ -0,0 +1,45 @@
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();
boolean isSingleThreaded();
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,18 @@ 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 boolean isSingleThreaded() {
return false;
}
@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 +81,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 +127,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 +141,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 +149,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 +166,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 +184,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 +201,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 +225,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 +251,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) {