| 1 |
/* |
| 2 |
* Copyright (c) Eclipse contributors and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
*/ |
| 8 |
package org.eclipse.emf.edit.ui.action; |
| 9 |
|
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.Arrays; |
| 13 |
import java.util.LinkedHashSet; |
| 14 |
import java.util.LinkedList; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Set; |
| 17 |
|
| 18 |
import org.eclipse.emf.common.ui.viewer.IViewerProvider; |
| 19 |
import org.eclipse.emf.edit.ui.EMFEditUIPlugin; |
| 20 |
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; |
| 21 |
import org.eclipse.jface.action.Action; |
| 22 |
import org.eclipse.jface.viewers.TreeViewer; |
| 23 |
import org.eclipse.jface.viewers.Viewer; |
| 24 |
import org.eclipse.swt.SWT; |
| 25 |
import org.eclipse.swt.custom.BusyIndicator; |
| 26 |
import org.eclipse.swt.widgets.Event; |
| 27 |
import org.eclipse.swt.widgets.Tree; |
| 28 |
import org.eclipse.swt.widgets.TreeItem; |
| 29 |
import org.eclipse.ui.IWorkbenchPart; |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* An action for expanding all items in a {@link TreeViewer tree viewer}. |
| 34 |
* It also provides a general utility method, {@link #expandAll(TreeViewer, long)} with specialized behavior for expanding large trees. |
| 35 |
* |
| 36 |
* @since 2.14 |
| 37 |
*/ |
| 38 |
public final class ExpandAllAction extends Action |
| 39 |
{ |
| 40 |
/** |
| 41 |
* The provider of the tree viewer. |
| 42 |
*/ |
| 43 |
private IViewerProvider viewerProvider; |
| 44 |
|
| 45 |
/** |
| 46 |
* How many milliseconds to spend expanding the tree. |
| 47 |
*/ |
| 48 |
private long timeout; |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates an instance that will {@link #expandAll(TreeViewer, long) expand} for 2000ms. |
| 52 |
*/ |
| 53 |
public ExpandAllAction() |
| 54 |
{ |
| 55 |
this(2000); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Creates an instance that will {@link #expandAll(TreeViewer, long) expand} for {@code timeout} milliseconds. |
| 60 |
*/ |
| 61 |
public ExpandAllAction(long timeout) |
| 62 |
{ |
| 63 |
super( |
| 64 |
EMFEditUIPlugin.INSTANCE.getString("_UI_ExpandAll_menu_item"), |
| 65 |
ExtendedImageRegistry.INSTANCE.getImageDescriptor(EMFEditUIPlugin.INSTANCE.getImage("full/ctool16/ExpandAll"))); |
| 66 |
this.timeout = timeout; |
| 67 |
setToolTipText(EMFEditUIPlugin.INSTANCE.getString("_UI_ExpandAll_tool_tip")); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* {@link #expandAll(TreeViewer, long) Expands} the tree for the {@link #timeout} milliseconds. |
| 72 |
*/ |
| 73 |
@Override |
| 74 |
public void run() |
| 75 |
{ |
| 76 |
Viewer viewer = viewerProvider.getViewer(); |
| 77 |
if (viewer instanceof TreeViewer) |
| 78 |
{ |
| 79 |
expandAll((TreeViewer)viewer, timeout); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Does essentially the same thing as {@link TreeViewer#expandAll()} |
| 85 |
* but it limits the amount of time spent expanding to the specified number of milliseconds. |
| 86 |
* This allows the operation to be applied to very large trees, and even to infinite trees. |
| 87 |
* Because the operation time is limited, the expansion is done in a specific order. |
| 88 |
* I.e., expansion starts with the selection or, if there isn't one, with the {@link Tree#getTopItem() top} visible item. |
| 89 |
* This ensures that what the user has selected or actually sees is expanded first. |
| 90 |
* Time permitting, the siblings around these items are expanded first. |
| 91 |
* Given sufficient time, this will expand the full tree, but will generally expand the part of the tree the user sees. |
| 92 |
* Redraw is disabled during the expansion process. |
| 93 |
* |
| 94 |
* @param treeViewer the tree viewer to operate upon. |
| 95 |
* @param timeout the number of milliseconds to spend on expanding items before timing out. |
| 96 |
*/ |
| 97 |
public static void expandAll(TreeViewer treeViewer, final long timeout) |
| 98 |
{ |
| 99 |
final Tree tree = treeViewer.getTree(); |
| 100 |
final TreeItem[] selection = tree.getSelection(); |
| 101 |
final TreeItem topItem = tree.getTopItem(); |
| 102 |
|
| 103 |
// We're most interested in expanding the selection if there is one. |
| 104 |
// Failing that, it makes the most sense to start expanding to top most visible element instead. |
| 105 |
// |
| 106 |
TreeItem[] itemsOfInterest; |
| 107 |
if (selection.length == 0 && topItem != null) |
| 108 |
{ |
| 109 |
itemsOfInterest = new TreeItem []{ topItem }; |
| 110 |
|
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
itemsOfInterest = selection; |
| 115 |
} |
| 116 |
|
| 117 |
// We need the root items repeatedly because these are the siblings of items with a null parent. |
| 118 |
List<TreeItem> rootItems = Arrays.asList(tree.getItems()); |
| 119 |
|
| 120 |
// We'll keep a list of lists because we'd first like to expand all the selected elements, |
| 121 |
// then their closest siblings, then the closest siblings of the parent, and so on. |
| 122 |
// The first list in this list will be for the items of interest directly. |
| 123 |
// |
| 124 |
List<List<TreeItem>> prioritizedItems = new ArrayList<List<TreeItem>>(); |
| 125 |
prioritizedItems.add(new ArrayList<TreeItem>()); |
| 126 |
|
| 127 |
// Process each item of interest. |
| 128 |
// |
| 129 |
for (TreeItem treeItem : itemsOfInterest) |
| 130 |
{ |
| 131 |
// Add it to the first list. |
| 132 |
prioritizedItems.get(0).add(treeItem); |
| 133 |
|
| 134 |
// Populate the list of each parent, walking up the tree. |
| 135 |
// The count keeps track of which list to populate during each iteration to an parent. |
| 136 |
// |
| 137 |
int count = 1; |
| 138 |
for (TreeItem parentItem = treeItem.getParentItem(), childItem = treeItem;; childItem = parentItem, parentItem = parentItem.getParentItem(), ++count) |
| 139 |
{ |
| 140 |
// If there is no list at the count yet, add one, otherwise reuse it. |
| 141 |
// |
| 142 |
List<TreeItem> list; |
| 143 |
if (count >= prioritizedItems.size()) |
| 144 |
{ |
| 145 |
list = new ArrayList<TreeItem>(); |
| 146 |
prioritizedItems.add(list); |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
list = prioritizedItems.get(count); |
| 151 |
} |
| 152 |
|
| 153 |
// The siblings are either the items of the parent, or the root items. |
| 154 |
// |
| 155 |
List<TreeItem> siblingItems; |
| 156 |
if (parentItem != null) |
| 157 |
{ |
| 158 |
siblingItems = Arrays.asList(parentItem.getItems()); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
siblingItems = rootItems; |
| 163 |
} |
| 164 |
|
| 165 |
// Add all the siblings after the child item to the list. |
| 166 |
// |
| 167 |
int index = siblingItems.indexOf(childItem); |
| 168 |
for (int i = index + 1, size = siblingItems.size(); i < size; ++i) |
| 169 |
{ |
| 170 |
// Ignore siblings that don't have items (aren't expandable). |
| 171 |
// |
| 172 |
TreeItem siblingItem = siblingItems.get(i); |
| 173 |
if (siblingItem.getItemCount() > 0) |
| 174 |
{ |
| 175 |
list.add(siblingItem); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// Add all the siblings before the child item to the list in reverse order. |
| 180 |
// |
| 181 |
for (int i = index - 1; i >= 0; --i) |
| 182 |
{ |
| 183 |
// Ignore siblings that don't have items (aren't expandable). |
| 184 |
TreeItem siblingItem = siblingItems.get(i); |
| 185 |
if (siblingItem.getItemCount() > 0) |
| 186 |
{ |
| 187 |
list.add(siblingItem); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// When we've reached the root siblings, so we're done. |
| 192 |
// |
| 193 |
if (parentItem == null) |
| 194 |
{ |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Add all the items to a set, in prioritized order. |
| 201 |
// |
| 202 |
final Set<TreeItem> itemsToBeExpanded = new LinkedHashSet<TreeItem>(); |
| 203 |
for (List<TreeItem> list : prioritizedItems) |
| 204 |
{ |
| 205 |
itemsToBeExpanded.addAll(list); |
| 206 |
} |
| 207 |
|
| 208 |
// Do the expansion while showing a busy cursor. |
| 209 |
// |
| 210 |
BusyIndicator.showWhile(tree.getDisplay(), new Runnable() |
| 211 |
{ |
| 212 |
public void run() |
| 213 |
{ |
| 214 |
try |
| 215 |
{ |
| 216 |
// Don't paint until we're done. |
| 217 |
// |
| 218 |
tree.setRedraw(false); |
| 219 |
|
| 220 |
// Create a queue for prioritized processing. |
| 221 |
// |
| 222 |
LinkedList<TreeItem> queue = new LinkedList<TreeItem>(itemsToBeExpanded); |
| 223 |
|
| 224 |
// Only process for a fixed period of time. |
| 225 |
// |
| 226 |
long start = System.currentTimeMillis(); |
| 227 |
while (!queue.isEmpty()) |
| 228 |
{ |
| 229 |
TreeItem treeItem = queue.poll(); |
| 230 |
|
| 231 |
// Force the creation of children, and then expand the item, if it's not already expanded. |
| 232 |
// |
| 233 |
if (!treeItem.getExpanded()) |
| 234 |
{ |
| 235 |
Event expandEvent = new Event(); |
| 236 |
expandEvent.item = treeItem; |
| 237 |
tree.notifyListeners(SWT.Expand, expandEvent); |
| 238 |
treeItem.setExpanded(true); |
| 239 |
} |
| 240 |
|
| 241 |
// Add the children in reverse order for processing in the queue. |
| 242 |
// |
| 243 |
TreeItem[] childItems = treeItem.getItems(); |
| 244 |
for (int i = childItems.length - 1; i >= 0; --i) |
| 245 |
{ |
| 246 |
TreeItem childItem = childItems[i]; |
| 247 |
|
| 248 |
// If it's not a placeholder item and we haven't already added this item to the queue, |
| 249 |
// then add it at the front for high priority processing. |
| 250 |
// |
| 251 |
if (childItem.getData() != null && itemsToBeExpanded.add(childItem)) |
| 252 |
{ |
| 253 |
queue.addFirst(childItem); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// If we've been busy for more than timeout milliseconds, stop expanding. |
| 258 |
// |
| 259 |
if (System.currentTimeMillis() - start > timeout) |
| 260 |
{ |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
finally |
| 266 |
{ |
| 267 |
// Scroll to the previous top item. |
| 268 |
// |
| 269 |
tree.setTopItem(topItem); |
| 270 |
if (selection.length != 0) |
| 271 |
{ |
| 272 |
// Force the selection to scroll into view. |
| 273 |
// |
| 274 |
tree.setSelection(selection); |
| 275 |
} |
| 276 |
|
| 277 |
// Enable drawing again. |
| 278 |
// |
| 279 |
tree.setRedraw(true); |
| 280 |
} |
| 281 |
} |
| 282 |
}); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Sets the current active workbench part. |
| 287 |
* The action is enabled only if the workbench part is a {@link IViewerProvider} that yields a {@link TreeViewer}. |
| 288 |
* @param workbenchPart the current active workbench part. |
| 289 |
* @see EditingDomainActionBarContributor#activate() |
| 290 |
* @see EditingDomainActionBarContributor#deactivate() |
| 291 |
*/ |
| 292 |
public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) |
| 293 |
{ |
| 294 |
if (workbenchPart instanceof IViewerProvider) |
| 295 |
{ |
| 296 |
viewerProvider = (IViewerProvider)workbenchPart; |
| 297 |
setEnabled(viewerProvider.getViewer() instanceof TreeViewer); |
| 298 |
} |
| 299 |
else |
| 300 |
{ |
| 301 |
viewerProvider = null; |
| 302 |
setEnabled(false); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Updates the action. |
| 308 |
* The action is enabled only if the workbench part is a {@link IViewerProvider} that yields a {@link TreeViewer}. |
| 309 |
* |
| 310 |
* @see EditingDomainActionBarContributor#update() |
| 311 |
*/ |
| 312 |
public void update() |
| 313 |
{ |
| 314 |
setEnabled(viewerProvider != null && viewerProvider.getViewer() instanceof TreeViewer); |
| 315 |
} |
| 316 |
} |