use of com.android.uiautomator.tree.BasicTreeNode in project uiautomatorviewer by yangzaiCN.
the class UiAutomatorViewer method createContents.
/**
* Create contents of the application window.
*
* @param parent
*/
@Override
protected Control createContents(Composite parent) {
UiAutomatorModel.createInstance(this);
Composite basePane = new Composite(parent, SWT.NONE);
basePane.setLayout(new GridLayout(2, false));
mScreenshotCanvas = new Canvas(basePane, SWT.NONE);
mScreenshotCanvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
UiAutomatorModel.getModel().toggleExploreMode();
}
});
mScreenshotCanvas.setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_BLACK));
mScreenshotCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));
mScreenshotCanvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
if (mCachedScaleImage != null) {
// shifting the image here, so that there's a border around screen shot
// this makes highlighting red rectangles on the screen shot edges more visible
e.gc.drawImage(mCachedScaleImage, IMG_BORDER, IMG_BORDER);
Rectangle rect = UiAutomatorModel.getModel().getCurrentDrawingRect();
if (rect != null) {
e.gc.setForeground(e.gc.getDevice().getSystemColor(SWT.COLOR_RED));
if (UiAutomatorModel.getModel().isExploreMode()) {
// when we highlight nodes dynamically on mouse move,
// use dashed borders
e.gc.setLineStyle(SWT.LINE_DASH);
e.gc.setLineWidth(1);
} else {
// when highlighting nodes on tree node selection,
// use solid borders
e.gc.setLineStyle(SWT.LINE_SOLID);
e.gc.setLineWidth(2);
}
e.gc.drawRectangle(IMG_BORDER + getScaledSize(rect.x), IMG_BORDER + getScaledSize(rect.y), getScaledSize(rect.width), getScaledSize(rect.height));
}
}
}
});
mScreenshotCanvas.addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(MouseEvent e) {
if (UiAutomatorModel.getModel().isExploreMode()) {
UiAutomatorModel.getModel().updateSelectionForCoordinates(getInverseScaledSize(e.x - IMG_BORDER), getInverseScaledSize(e.y - IMG_BORDER));
}
}
});
mScreenshotCanvas.setBackgroundImage(ImageHelper.loadImageDescriptorFromResource("images/logo.png").createImage());
ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
toolBarManager.add(mOpenFilesAction);
toolBarManager.add(mExpandAllAction);
toolBarManager.add(mScreenshotAction);
toolBarManager.createControl(basePane);
mTreeViewer = new TreeViewer(basePane, SWT.BORDER);
Tree tree = mTreeViewer.getTree();
GridData gd_Tree = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
gd_Tree.widthHint = 350;
tree.setLayoutData(gd_Tree);
mTreeViewer.setContentProvider(new BasicTreeNodeContentProvider());
// default LabelProvider uses toString() to generate text to display
mTreeViewer.setLabelProvider(new LabelProvider());
mTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
if (event.getSelection().isEmpty()) {
UiAutomatorModel.getModel().setSelectedNode(null);
} else if (event.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object o = selection.toArray()[0];
if (o instanceof BasicTreeNode) {
UiAutomatorModel.getModel().setSelectedNode((BasicTreeNode) o);
}
}
}
});
// move focus so that it's not on tool bar (looks weird)
tree.setFocus();
Group grpNodeDetail = new Group(basePane, SWT.NONE);
grpNodeDetail.setLayout(new FillLayout(SWT.HORIZONTAL));
GridData gd_grpNodeDetail = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_grpNodeDetail.heightHint = FIXED_DETAIL_VIEW_HEIGHT;
gd_grpNodeDetail.minimumHeight = FIXED_DETAIL_VIEW_HEIGHT;
gd_grpNodeDetail.widthHint = FIXED_RHS_WIDTH;
gd_grpNodeDetail.minimumWidth = FIXED_RHS_WIDTH;
grpNodeDetail.setLayoutData(gd_grpNodeDetail);
grpNodeDetail.setText("Node Detail");
Composite tableContainer = new Composite(grpNodeDetail, SWT.NONE);
tableContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableColumnLayout columnLayout = new TableColumnLayout();
tableContainer.setLayout(columnLayout);
mTableViewer = new TableViewer(tableContainer, SWT.BORDER | SWT.FULL_SELECTION);
Table table = mTableViewer.getTable();
table.setLinesVisible(true);
// use ArrayContentProvider here, it assumes the input to the TableViewer
// is an array, where each element represents a row in the table
mTableViewer.setContentProvider(new ArrayContentProvider());
TableViewerColumn tableViewerColumnKey = new TableViewerColumn(mTableViewer, SWT.NONE);
TableColumn tblclmnKey = tableViewerColumnKey.getColumn();
tableViewerColumnKey.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof AttributePair) {
// first column, shows the attribute name
return ((AttributePair) element).key;
}
return super.getText(element);
}
});
columnLayout.setColumnData(tblclmnKey, new ColumnWeightData(1, ColumnWeightData.MINIMUM_WIDTH, true));
TableViewerColumn tableViewerColumnValue = new TableViewerColumn(mTableViewer, SWT.NONE);
tableViewerColumnValue.setEditingSupport(new AttributeTableEditingSupport(mTableViewer));
TableColumn tblclmnValue = tableViewerColumnValue.getColumn();
columnLayout.setColumnData(tblclmnValue, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
tableViewerColumnValue.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof AttributePair) {
// second column, shows the attribute value
return ((AttributePair) element).value;
}
return super.getText(element);
}
});
return basePane;
}
use of com.android.uiautomator.tree.BasicTreeNode in project uiautomatorviewer by yangzaiCN.
the class UiAutomatorModel method loadScreenshotAndXmlDump.
public boolean loadScreenshotAndXmlDump(File screenshotFile, File xmlDumpFile) {
if (screenshotFile != null && xmlDumpFile != null && screenshotFile.isFile() && xmlDumpFile.isFile()) {
ImageData[] data = null;
Image img = null;
try {
// use SWT's ImageLoader to read png from path
data = new ImageLoader().load(screenshotFile.getAbsolutePath());
} catch (SWTException e) {
e.printStackTrace();
return false;
}
// i.e. gifs or icons, we just care if it has at least one here
if (data.length < 1)
return false;
BasicTreeNode rootNode = new UiHierarchyXmlLoader().parseXml(xmlDumpFile.getAbsolutePath());
if (rootNode == null) {
System.err.println("null rootnode after parsing.");
return false;
}
try {
// Image is tied to ImageData and a Display, so we only need to create once
// per new image
img = new Image(mView.getShell().getDisplay(), data[0]);
} catch (SWTException e) {
e.printStackTrace();
return false;
}
// only update screenhot and xml if both are loaded successfully
if (mScreenshot != null) {
mScreenshot.dispose();
}
mScreenshot = img;
if (mRootNode != null) {
mRootNode.clearAllChildren();
}
// TODO: we should verify here if the coordinates in the XML matches the png
// or not: think loading a phone screenshot with a tablet XML dump
mRootNode = rootNode;
mScreenshotFile = screenshotFile;
mXmlDumpFile = xmlDumpFile;
mExploreMode = true;
mView.loadScreenshotAndXml();
return true;
}
return false;
}
use of com.android.uiautomator.tree.BasicTreeNode in project uiautomatorviewer by yangzaiCN.
the class UiAutomatorViewer method loadScreenshotAndXml.
/**
* Asks the Model for screenshot and xml tree data, then populates the screenshot
* area and tree view accordingly
*/
public void loadScreenshotAndXml() {
// re-layout screenshot canvas
GridData gd = new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 3);
Rectangle r = UiAutomatorModel.getModel().getScreenshot().getBounds();
mScale = calcScreenshotScale(r.width, r.height);
updateScaledImage(UiAutomatorModel.getModel().getScreenshot());
gd.minimumHeight = getScaledSize(r.height) + 2 * IMG_BORDER;
gd.minimumWidth = getScaledSize(r.width) + 2 * IMG_BORDER;
mScreenshotCanvas.setLayoutData(gd);
// load xml into tree
BasicTreeNode wrapper = new BasicTreeNode();
// putting another root node on top of existing root node
// because Tree seems to like to hide the root node
wrapper.addChild(UiAutomatorModel.getModel().getXmlRootNode());
mTreeViewer.setInput(wrapper);
mTreeViewer.getTree().setFocus();
// resize & reposition window
getShell().pack();
adjustShellLocation();
}
Aggregations