Methods Summary |
---|
void | breakConnectionWithEditor()
// unhook outline viewer
mEditor.getSelectionSynchronizer().removeViewer(getViewer());
|
public void | createControl(org.eclipse.swt.widgets.Composite parent)
// create outline viewer page
getViewer().createControl(parent);
// configure outline viewer
getViewer().setEditPartFactory(new UiElementTreeEditPartFactory());
setupOutline();
setupContextMenu();
setupTooltip();
setupDoubleClick();
|
public void | dispose()
breakConnectionWithEditor();
// dispose
super.dispose();
|
private void | doCreateMenuAction(org.eclipse.jface.action.IMenuManager manager, java.util.List selected)Adds the menu actions to the context menu when the given UI node is selected in
the tree view.
if (selected != null) {
boolean hasXml = false;
for (UiElementNode uiNode : selected) {
if (uiNode.getXmlNode() != null) {
hasXml = true;
break;
}
}
if (hasXml) {
manager.add(new CopyCutAction(mEditor.getLayoutEditor(), mEditor.getClipboard(),
null, selected, true /* cut */));
manager.add(new CopyCutAction(mEditor.getLayoutEditor(), mEditor.getClipboard(),
null, selected, false /* cut */));
// Can't paste with more than one element selected (the selection is the target)
if (selected.size() <= 1) {
// Paste is not valid if it would add a second element on a terminal element
// which parent is a document -- an XML document can only have one child. This
// means paste is valid if the current UI node can have children or if the parent
// is not a document.
UiElementNode ui_root = selected.get(0).getUiRoot();
if (ui_root.getDescriptor().hasChildren() ||
!(ui_root.getUiParent() instanceof UiDocumentNode)) {
manager.add(new PasteAction(mEditor.getLayoutEditor(),
mEditor.getClipboard(),
selected.get(0)));
}
}
manager.add(new Separator());
}
}
// Append "add" and "remove" actions. They do the same thing as the add/remove
// buttons on the side.
//
// "Add" makes sense only if there's 0 or 1 item selected since the
// one selected item becomes the target.
if (selected == null || selected.size() <= 1) {
manager.add(mAddAction);
}
if (selected != null) {
manager.add(mDeleteAction);
manager.add(new Separator());
manager.add(mUpAction);
manager.add(mDownAction);
}
if (selected != null && selected.size() == 1) {
manager.add(new Separator());
Action propertiesAction = new Action("Properties") {
@Override
public void run() {
EclipseUiHelper.showView(EclipseUiHelper.PROPERTY_SHEET_VIEW_ID,
true /* activate */);
}
};
propertiesAction.setToolTipText("Displays properties of the selected element.");
manager.add(propertiesAction);
}
|
private com.android.ide.eclipse.editors.layout.parts.UiElementTreeEditPart | findPartForModel(com.android.ide.eclipse.editors.layout.parts.UiElementTreeEditPart rootPart, com.android.ide.eclipse.editors.uimodel.UiElementNode uiNode)Utility method that tries to find an edit part that matches a given model UI node.
if (rootPart.getModel() == uiNode) {
return rootPart;
}
for (Object part : rootPart.getChildren()) {
if (part instanceof UiElementTreeEditPart) {
UiElementTreeEditPart found = findPartForModel(
(UiElementTreeEditPart) part, uiNode);
if (found != null) {
return found;
}
}
}
return null;
|
public org.eclipse.swt.widgets.Control | getControl()
return getViewer().getControl();
|
private java.util.List | getModelSelections()Returns the currently selected model element, which is either an
{@link UiViewTreeEditPart} or an {@link UiLayoutTreeEditPart}.
Returns null if there is no selection or if the implicit root is "selected"
(which actually represents the lack of a real element selection.)
List<UiElementTreeEditPart> parts = getViewerSelections();
if (parts != null) {
ArrayList<UiElementNode> selected = new ArrayList<UiElementNode>();
for (UiElementTreeEditPart part : parts) {
if (part instanceof UiViewTreeEditPart || part instanceof UiLayoutTreeEditPart) {
selected.add((UiElementNode) part.getModel());
}
}
return selected.size() > 0 ? selected : null;
}
return null;
|
private java.util.List | getViewerSelections()Returns the currently selected element, if any, in the viewer.
This returns the viewer's elements (i.e. an {@link UiElementTreeEditPart})
and not the underlying model node.
When there is no actual selection, this might still return the root node,
which is of type {@link UiDocumentTreeEditPart}.
ISelection selection = getSelection();
if (selection instanceof StructuredSelection) {
StructuredSelection structuredSelection = (StructuredSelection)selection;
if (structuredSelection.size() > 0) {
ArrayList<UiElementTreeEditPart> selected = new ArrayList<UiElementTreeEditPart>();
for (Iterator it = structuredSelection.iterator(); it.hasNext(); ) {
Object selectedObj = it.next();
if (selectedObj instanceof UiElementTreeEditPart) {
selected.add((UiElementTreeEditPart) selectedObj);
}
}
return selected.size() > 0 ? selected : null;
}
}
return null;
|
public void | reloadModel()Updates the outline view with the model of the {@link GraphicalLayoutEditor}.
This attemps to preserve the selection, if any.
// Attemps to preserve the UiNode selection, if any
List<UiElementNode> uiNodes = null;
try {
// get current selection using the model rather than the edit part as
// reloading the content may change the actual edit part.
uiNodes = getModelSelections();
// perform the update
getViewer().setContents(mEditor.getModel());
} finally {
// restore selection
if (uiNodes != null) {
setModelSelection(uiNodes.get(0));
}
}
|
public void | setActionBars(org.eclipse.ui.IActionBars actionBars)
IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(mAddAction);
toolBarManager.add(mDeleteAction);
toolBarManager.add(new Separator());
toolBarManager.add(mUpAction);
toolBarManager.add(mDownAction);
IMenuManager menuManager = actionBars.getMenuManager();
menuManager.add(mAddAction);
menuManager.add(mDeleteAction);
menuManager.add(new Separator());
menuManager.add(mUpAction);
menuManager.add(mDownAction);
|
private void | setModelSelection(com.android.ide.eclipse.editors.uimodel.UiElementNode uiNodeToSelect)Selects the corresponding model element in the tree viewer.
if (uiNodeToSelect != null) {
// find an edit part that has the requested model element
UiElementTreeEditPart part = findPartForModel(
(UiElementTreeEditPart) getViewer().getContents(),
uiNodeToSelect);
// if we found a part, select it and reveal it
if (part != null) {
setViewerSelection(part);
getViewer().reveal(part);
}
}
|
void | setNewEditor(GraphicalLayoutEditor editor)
mEditor = editor;
setupOutline();
|
private void | setViewerSelection(com.android.ide.eclipse.editors.layout.parts.UiElementTreeEditPart selectedPart)Selects the corresponding edit part in the tree viewer.
if (selectedPart != null && !(selectedPart instanceof UiDocumentTreeEditPart)) {
LinkedList<UiElementTreeEditPart> segments = new LinkedList<UiElementTreeEditPart>();
for (UiElementTreeEditPart part = selectedPart;
!(part instanceof UiDocumentTreeEditPart);
part = (UiElementTreeEditPart) part.getParent()) {
segments.add(0, part);
}
setSelection(new TreeSelection(new TreePath(segments.toArray())));
}
|
private void | setupContextMenu()
MenuManager menuManager = new MenuManager();
menuManager.setRemoveAllWhenShown(true);
menuManager.addMenuListener(new IMenuListener() {
/**
* The menu is about to be shown. The menu manager has already been
* requested to remove any existing menu item. This method gets the
* tree selection and if it is of the appropriate type it re-creates
* the necessary actions.
*/
public void menuAboutToShow(IMenuManager manager) {
List<UiElementNode> selected = getModelSelections();
if (selected != null) {
doCreateMenuAction(manager, selected);
return;
}
doCreateMenuAction(manager, null /* ui_node */);
}
});
Control control = getControl();
Menu contextMenu = menuManager.createContextMenu(control);
control.setMenu(contextMenu);
|
private void | setupDoubleClick()Sets up double-click action on the tree.
By default, double-click (a.k.a. "default selection") on a valid list item will
show the property view.
final Tree tree = (Tree) getControl();
tree.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) {
EclipseUiHelper.showView(EclipseUiHelper.PROPERTY_SHEET_VIEW_ID,
true /* activate */);
}
});
|
private void | setupOutline()
getViewer().setEditDomain(mEditor.getEditDomain());
// hook outline viewer
mEditor.getSelectionSynchronizer().addViewer(getViewer());
// initialize outline viewer with model
getViewer().setContents(mEditor.getModel());
|
private void | setupTooltip()Sets up a custom tooltip when hovering over tree items.
The tooltip will display the element's javadoc, if any, or the item's getText otherwise.
final Tree tree = (Tree) getControl();
/*
* Reference:
* http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet125.java?view=markup
*/
final Listener listener = new Listener() {
Shell tip = null;
Label label = null;
public void handleEvent(Event event) {
switch(event.type) {
case SWT.Dispose:
case SWT.KeyDown:
case SWT.MouseExit:
case SWT.MouseDown:
case SWT.MouseMove:
if (tip != null) {
tip.dispose();
tip = null;
label = null;
}
break;
case SWT.MouseHover:
if (tip != null) {
tip.dispose();
tip = null;
label = null;
}
String tooltip = null;
TreeItem item = tree.getItem(new Point(event.x, event.y));
if (item != null) {
Object data = item.getData();
if (data instanceof UiElementTreeEditPart) {
Object model = ((UiElementTreeEditPart) data).getModel();
if (model instanceof UiElementNode) {
tooltip = ((UiElementNode) model).getDescriptor().getTooltip();
}
}
if (tooltip == null) {
tooltip = item.getText();
} else {
tooltip = item.getText() + ":\r" + tooltip;
}
}
if (tooltip != null) {
Shell shell = tree.getShell();
Display display = tree.getDisplay();
tip = new Shell(shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
tip.setBackground(display .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
FillLayout layout = new FillLayout();
layout.marginWidth = 2;
tip.setLayout(layout);
label = new Label(tip, SWT.NONE);
label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
label.setData("_TABLEITEM", item);
label.setText(tooltip);
label.addListener(SWT.MouseExit, this);
label.addListener(SWT.MouseDown, this);
Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = item.getBounds(0);
Point pt = tree.toDisplay(rect.x, rect.y);
tip.setBounds(pt.x, pt.y, size.x, size.y);
tip.setVisible(true);
}
}
}
};
tree.addListener(SWT.Dispose, listener);
tree.addListener(SWT.KeyDown, listener);
tree.addListener(SWT.MouseMove, listener);
tree.addListener(SWT.MouseHover, listener);
|