📄 structuredviewer.java
字号:
* @param background */ public void setBackground(Color background) { this.background = background; } /** * Set the font. * @param font */ public void setFont(Font font) { this.font = font; } /** * Set the foreground color. * @param foreground */ public void setForeground(Color foreground) { this.foreground = foreground; } } /** * The safe runnable used to update an item. */ class UpdateItemSafeRunnable extends SafeRunnable { private Widget widget; private Object element; private boolean fullMap; UpdateItemSafeRunnable(Widget widget, Object element, boolean fullMap) { this.widget = widget; this.element = element; this.fullMap = fullMap; } public void run() { doUpdateItem(widget, element, fullMap); } } /** * Creates a structured element viewer. The viewer has no input, no content * provider, a default label provider, no sorter, and no filters. */ protected StructuredViewer() { // do nothing } /** * Adds a listener for double-clicks in this viewer. Has no effect if an * identical listener is already registered. * * @param listener * a double-click listener */ public void addDoubleClickListener(IDoubleClickListener listener) { doubleClickListeners.add(listener); } /** * Adds a listener for selection-open in this viewer. Has no effect if an * identical listener is already registered. * * @param listener * a double-click listener */ public void addOpenListener(IOpenListener listener) { openListeners.add(listener); } /* * (non-Javadoc) Method declared on IPostSelectionProvider. */ public void addPostSelectionChangedListener(ISelectionChangedListener listener) { postSelectionChangedListeners.add(listener); } /** * Adds support for dragging items out of this viewer via a user * drag-and-drop operation. * * @param operations * a bitwise OR of the supported drag and drop operation types ( * <code>DROP_COPY</code>,<code>DROP_LINK</code>, and * <code>DROP_MOVE</code>) * @param transferTypes * the transfer types that are supported by the drag operation * @param listener * the callback that will be invoked to set the drag data and to * cleanup after the drag and drop operation finishes * @see org.eclipse.swt.dnd.DND */ public void addDragSupport(int operations, Transfer[] transferTypes, DragSourceListener listener) { Control myControl = getControl(); final DragSource dragSource = new DragSource(myControl, operations); dragSource.setTransfer(transferTypes); dragSource.addDragListener(listener); } /** * Adds support for dropping items into this viewer via a user drag-and-drop * operation. * * @param operations * a bitwise OR of the supported drag and drop operation types ( * <code>DROP_COPY</code>,<code>DROP_LINK</code>, and * <code>DROP_MOVE</code>) * @param transferTypes * the transfer types that are supported by the drop operation * @param listener * the callback that will be invoked after the drag and drop * operation finishes * @see org.eclipse.swt.dnd.DND */ public void addDropSupport(int operations, Transfer[] transferTypes, final DropTargetListener listener) { Control control = getControl(); DropTarget dropTarget = new DropTarget(control, operations); dropTarget.setTransfer(transferTypes); dropTarget.addDropListener(listener); } /** * Adds the given filter to this viewer, and triggers refiltering and * resorting of the elements. * * @param filter * a viewer filter */ public void addFilter(ViewerFilter filter) { if (filters == null) { filters = new ArrayList(); } filters.add(filter); refresh(); } /** * Asserts that the given array of elements is itself non- <code>null</code> * and contains no <code>null</code> elements. * * @param elements * the array to check */ protected void assertElementsNotNull(Object[] elements) { Assert.isNotNull(elements); for (int i = 0, n = elements.length; i < n; ++i) { Assert.isNotNull(elements[i]); } } /** * Associates the given element with the given widget. Sets the given item's * data to be the element, and maps the element to the item in the element * map (if enabled). * * @param element * the element * @param item * the widget */ protected void associate(Object element, Item item) { Object data = item.getData(); if (data != element) { if (data != null) { disassociate(item); } item.setData(element); } // Always map the element, even if data == element, // since unmapAllElements() can leave the map inconsistent // See bug 2741 for details. mapElement(element, item); } /** * Disassociates the given SWT item from its corresponding element. Sets the * item's data to <code>null</code> and removes the element from the * element map (if enabled). * * @param item * the widget */ protected void disassociate(Item item) { Object element = item.getData(); Assert.isNotNull(element); //Clear the map before we clear the data unmapElement(element, item); item.setData(null); } /** * Returns the widget in this viewer's control which represents the given * element if it is the viewer's input. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param element * @return the corresponding widget, or <code>null</code> if none */ protected abstract Widget doFindInputItem(Object element); /** * Returns the widget in this viewer's control which represent the given * element. This method searchs all the children of the input element. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param element * @return the corresponding widget, or <code>null</code> if none */ protected abstract Widget doFindItem(Object element); /** * Copies the attributes of the given element into the given SWT item. The * element map is updated according to the value of <code>fullMap</code>. * If <code>fullMap</code> is <code>true</code> then the current mapping * from element to widgets is removed and the new mapping is added. If * fullmap is <code>false</code> then only the new map gets installed. * Installing only the new map is necessary in cases where only the order of * elements changes but not the set of elements. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param item * @param element element * @param fullMap * <code>true</code> if mappings are added and removed, and * <code>false</code> if only the new map gets installed */ protected abstract void doUpdateItem(Widget item, Object element, boolean fullMap); /** * Compares two elements for equality. Uses the element comparer if one has * been set, otherwise uses the default <code>equals</code> method on the * elements themselves. * * @param elementA * the first element * @param elementB * the second element * @return whether elementA is equal to elementB */ protected boolean equals(Object elementA, Object elementB) { if (comparer == null) { return elementA == null ? elementB == null : elementA.equals(elementB); } else { return elementA == null ? elementB == null : comparer.equals(elementA, elementB); } } /** * Returns the result of running the given elements through the filters. * * @param elements * the elements to filter * @return only the elements which all filters accept */ protected Object[] filter(Object[] elements) { if (filters != null) { ArrayList filtered = new ArrayList(elements.length); Object root = getRoot(); for (int i = 0; i < elements.length; i++) { boolean add = true; for (int j = 0; j < filters.size(); j++) { add = ((ViewerFilter) filters.get(j)).select(this, root, elements[i]); if (!add) { break; } } if (add) { filtered.add(elements[i]); } } return filtered.toArray(); } return elements; } /** * Finds the widget which represents the given element. * <p> * The default implementation of this method tries first to find the widget * for the given element assuming that it is the viewer's input; this is * done by calling <code>doFindInputItem</code>. If it is not found * there, it is looked up in the internal element map provided that this * feature has been enabled. If the element map is disabled, the widget is * found via <code>doFindInputItem</code>. * </p> * * @param element * the element * @return the corresponding widget, or <code>null</code> if none */ protected final Widget findItem(Object element) { Widget[] result = findItems(element); return result.length == 0 ? null : result[0]; } /** * Finds the widgets which represent the given element. The returned array * must not be changed by clients; it might change upon calling other * methods on this viewer. * <p> * This method was introduced to support multiple equal elements in a viewer * (@see {@link AbstractTreeViewer}). Multiple equal elements are only * supported if the element map is enabled by calling * {@link #setUseHashlookup(boolean)} and passing <code>true</code>. * </p> * <p> * The default implementation of this method tries first to find the widget * for the given element assuming that it is the viewer's input; this is * done by calling <code>doFindInputItem</code>. If it is not found * there, the widgets are looked up in the internal element map provided * that this feature has been enabled. If the element map is disabled, the * widget is found via <code>doFindInputItem</code>. * </p> * * @param element * the element * @return the corresponding widgets * * @since 3.2 */ protected final Widget[] findItems(Object element) { Widget result = doFindInputItem(element); if (result != null) { return new Widget[] { result }; } // if we have an element map use it, otherwise search for the item. if (usingElementMap()) { Object widgetOrWidgets = elementMap.get(element); if (widgetOrWidgets==null) { return NO_WIDGETS; } else if (widgetOrWidgets instanceof Widget) { return new Widget[] {(Widget) widgetOrWidgets}; } else { return (Widget[])widgetOrWidgets; } } result = doFindItem(element); return result == null ? NO_WIDGETS : new Widget[] { result }; } /** * Notifies any double-click listeners that a double-click has been * received. Only listeners registered at the time this method is called are * notified. * * @param event * a double-click event * * @see IDoubleClickListener#doubleClick */ protected void fireDoubleClick(final DoubleClickEvent event) { Object[] listeners = doubleClickListeners.getListeners(); for (int i = 0; i < listeners.length; ++i) { final IDoubleClickListener l = (IDoubleClickListener) listeners[i]; SafeRunnable.run(new SafeRunnable() { public void run() { l.doubleClick(event); } }); } } /** * Notifies any open event listeners that a open event has been received. * Only listeners registered at the time this method is called are notified. * * @param event * a double-click event * * @see IOpenListener#open(OpenEvent) */ protected void fireOpen(final OpenEvent event) { Object[] listeners = openListeners.getListeners(); for (int i = 0; i < listeners.length; ++i) { final IOpenListener l = (IOpenListener) listeners[i]; SafeRunnable.run(new SafeRunnable() { public void run() { l.open(event); } }); } } /** * Notifies any post selection listeners that a post selection event has * been received. Only listeners registered at the time this method is * called are notified. * * @param event * a selection changed event * * @see #addPostSelectionChangedListener(ISelectionChangedListener) */ protected void firePostSelectionChanged(final SelectionChangedEvent event) { Object[] listeners = postSelectionChangedListeners.getListeners(); for (int i = 0; i < listeners.length; ++i) { final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; SafeRunnable.run(new SafeRunnable() { public void run() { l.selectionChanged(event); } }); } } /** * Returns the comparer to use for comparing elements, or * <code>null</code> if none has been set. If specified, * the viewer uses this to compare and hash elements rather
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -