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

View File

@@ -16,6 +16,7 @@
package com.android.preload.actions;
import com.android.preload.Main;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
@@ -28,7 +29,11 @@ public abstract class AbstractThreadedAction extends AbstractAction implements R
@Override
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 javax.swing.AbstractAction;
import javax.swing.JFileChooser;
/**
* 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
* used to filter classes from the intersection.
*/
public class ComputeThresholdAction extends AbstractAction implements Runnable {
public class ComputeThresholdAction extends AbstractThreadedAction {
protected int threshold;
private Pattern blacklist;
private DumpTableModel dataTableModel;
@@ -72,7 +71,7 @@ public class ComputeThresholdAction extends AbstractAction implements Runnable {
return;
}
new Thread(this).start();
super.actionPerformed(e);
}
@Override
@@ -92,10 +91,8 @@ public class ComputeThresholdAction extends AbstractAction implements Runnable {
boolean ret = Main.getUI().showConfirmDialog("Computed a set with " + result.size()
+ " classes, would you like to save to disk?", "Save?");
if (ret) {
JFileChooser jfc = new JFileChooser();
int ret2 = jfc.showSaveDialog(Main.getUI());
if (ret2 == JFileChooser.APPROVE_OPTION) {
File f = jfc.getSelectedFile();
File f = Main.getUI().showSaveDialog();
if (f != null) {
saveSet(result, f);
}
}

View File

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

View File

@@ -27,7 +27,7 @@ import java.util.Collection;
import javax.swing.AbstractAction;
public class ImportAction extends AbstractAction implements Runnable {
public class ImportAction extends AbstractThreadedAction {
private File[] lastOpenFiles;
private DumpTableModel dataTableModel;
@@ -40,7 +40,7 @@ public class ImportAction extends AbstractAction implements Runnable {
public void actionPerformed(ActionEvent e) {
lastOpenFiles = Main.getUI().showOpenDialog(true);
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()) {
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 {

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.table.TableModel;
public class UI extends JFrame {
public class SwingUI extends JFrame implements IUI {
private JList<Client> clientList;
private JTable dataTable;
@@ -49,11 +49,18 @@ public class UI extends JFrame {
// Shared file chooser, means the directory is retained.
private JFileChooser jfc;
public UI(ListModel<Client> clientListModel,
TableModel dataTableModel,
List<Action> actions) {
public SwingUI() {
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)),
BorderLayout.WEST);
clientList.setCellRenderer(new ClientListCellRenderer());
@@ -74,18 +81,27 @@ public class UI extends JFrame {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
setVisible(true);
}
@Override
public void ready() {
}
@Override
public Client getSelectedClient() {
return clientList.getSelectedValue();
}
@Override
public int getSelectedDataTableRow() {
return dataTable.getSelectedRow();
}
private JDialog currentWaitDialog = null;
@Override
public void showWaitDialog() {
if (currentWaitDialog == null) {
currentWaitDialog = new JDialog(this, "Please wait...", true);
@@ -111,6 +127,7 @@ public class UI extends JFrame {
});
}
@Override
public void updateWaitDialog(String s) {
if (currentWaitDialog != null) {
((JLabel) currentWaitDialog.getContentPane().getComponent(0)).setText(s);
@@ -124,6 +141,7 @@ public class UI extends JFrame {
}
}
@Override
public void hideWaitDialog() {
if (currentWaitDialog != null) {
currentWaitDialog.setVisible(false);
@@ -131,6 +149,7 @@ public class UI extends JFrame {
}
}
@Override
public void showMessageDialog(String s) {
// Hide the wait dialog...
if (currentWaitDialog != null) {
@@ -147,6 +166,7 @@ public class UI extends JFrame {
}
}
@Override
public boolean showConfirmDialog(String title, String message) {
// Hide the wait dialog...
if (currentWaitDialog != null) {
@@ -164,6 +184,7 @@ public class UI extends JFrame {
}
}
@Override
public String showInputDialog(String message) {
// Hide the wait dialog...
if (currentWaitDialog != null) {
@@ -180,6 +201,7 @@ public class UI extends JFrame {
}
}
@Override
@SuppressWarnings("unchecked")
public <T> T showChoiceDialog(String title, String message, T[] choices) {
// Hide the wait dialog...
@@ -203,6 +225,7 @@ public class UI extends JFrame {
}
}
@Override
public File showSaveDialog() {
// Hide the wait dialog...
if (currentWaitDialog != null) {
@@ -228,6 +251,7 @@ public class UI extends JFrame {
}
}
@Override
public File[] showOpenDialog(boolean multi) {
// Hide the wait dialog...
if (currentWaitDialog != null) {