📄 structuredviewer.java
字号:
if (filter.isFilterProperty(element, property)) { return true; } } } return false; } /** * Returns a new hashtable using the given capacity and this viewer's element comparer. * * @param capacity the initial capacity of the hashtable * @return a new hashtable * * @since 3.0 */ CustomHashtable newHashtable(int capacity) { return new CustomHashtable(capacity, getComparer()); } /** * Attempts to preserves the current selection across a run of the given * code. * <p> * The default implementation of this method: * <ul> * <li>discovers the old selection (via <code>getSelection</code>)</li> * <li>runs the given runnable</li> * <li>attempts to restore the old selection (using * <code>setSelectionToWidget</code></li> * <li>rediscovers the resulting selection (via <code>getSelection</code>) * </li> * <li>calls <code>handleInvalidSelection</code> if the selection did not * take</li> * <li>calls <code>postUpdateHook</code></li> * </ul> * </p> * * @param updateCode * the code to run */ protected void preservingSelection(Runnable updateCode) { ISelection oldSelection = null; try { // preserve selection oldSelection = getSelection(); inChange = restoreSelection = true; // perform the update updateCode.run(); } finally { inChange = false; // restore selection if (restoreSelection) { setSelectionToWidget(oldSelection, false); } // send out notification if old and new differ ISelection newSelection = getSelection(); if (!newSelection.equals(oldSelection)) { handleInvalidSelection(oldSelection, newSelection); } } } /* * Non-Javadoc. Method declared on Viewer. */ public void refresh() { refresh(getRoot()); } /** * Refreshes this viewer with information freshly obtained from this * viewer's model. If <code>updateLabels</code> is <code>true</code> * then labels for otherwise unaffected elements are updated as well. * Otherwise, it assumes labels for existing elements are unchanged, and * labels are only obtained as needed (for example, for new elements). * <p> * Calling <code>refresh(true)</code> has the same effect as * <code>refresh()</code>. * <p> * Note that the implementation may still obtain labels for existing * elements even if <code>updateLabels</code> is false. The intent is * simply to allow optimization where possible. * * @param updateLabels * <code>true</code> to update labels for existing elements, * <code>false</code> to only update labels as needed, assuming * that labels for existing elements are unchanged. * * @since 2.0 */ public void refresh(boolean updateLabels) { refresh(getRoot(), updateLabels); } /** * Refreshes this viewer starting with the given element. * <p> * Unlike the <code>update</code> methods, this handles structural changes * to the given element (e.g. addition or removal of children). If only the * given element needs updating, it is more efficient to use the * <code>update</code> methods. * </p> * * @param element * the element */ public void refresh(final Object element) { preservingSelection(new Runnable() { public void run() { internalRefresh(element); } }); } /** * Refreshes this viewer starting with the given element. Labels are updated * as described in <code>refresh(boolean updateLabels)</code>. * <p> * Unlike the <code>update</code> methods, this handles structural changes * to the given element (e.g. addition or removal of children). If only the * given element needs updating, it is more efficient to use the * <code>update</code> methods. * </p> * * @param element * the element * @param updateLabels * <code>true</code> to update labels for existing elements, * <code>false</code> to only update labels as needed, assuming * that labels for existing elements are unchanged. * * @since 2.0 */ public void refresh(final Object element, final boolean updateLabels) { preservingSelection(new Runnable() { public void run() { internalRefresh(element, updateLabels); } }); } /** * * Refreshes the given TableItem with the given element. Calls * <code>doUpdateItem(..., false)</code>. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * @param widget * the widget * @param element * the element */ protected final void refreshItem(Widget widget, Object element) { SafeRunnable.run(new UpdateItemSafeRunnable(widget, element, true)); } /** * Removes the given open listener from this viewer. Has no affect if an * identical listener is not registered. * * @param listener * a double-click listener */ public void removeOpenListener(IOpenListener listener) { openListeners.remove(listener); } /* * (non-Javadoc) Method declared on IPostSelectionProvider. */ public void removePostSelectionChangedListener(ISelectionChangedListener listener) { postSelectionChangedListeners.remove(listener); } /** * Removes the given double-click listener from this viewer. Has no affect * if an identical listener is not registered. * * @param listener * a double-click listener */ public void removeDoubleClickListener(IDoubleClickListener listener) { doubleClickListeners.remove(listener); } /** * Removes the given filter from this viewer, and triggers refiltering and * resorting of the elements if required. Has no effect if the identical * filter is not registered. * * @param filter * a viewer filter */ public void removeFilter(ViewerFilter filter) { Assert.isNotNull(filter); if (filters != null) { // Note: can't use List.remove(Object). Use identity comparison // instead. for (Iterator i = filters.iterator(); i.hasNext();) { Object o = i.next(); if (o == filter) { i.remove(); refresh(); if (filters.size() == 0) { filters = null; } return; } } } } /** * Discards this viewer's filters and triggers refiltering and resorting of * the elements. */ public void resetFilters() { if (filters != null) { filters = null; refresh(); } } /** * Ensures that the given element is visible, scrolling the viewer if * necessary. The selection is unchanged. * * @param element * the element to reveal */ public abstract void reveal(Object element); /* * (non-Javadoc) * @see org.eclipse.jface.viewers.ContentViewer#setContentProvider(org.eclipse.jface.viewers.IContentProvider) */ public void setContentProvider(IContentProvider provider) { assertContentProviderType(provider); super.setContentProvider(provider); } /** * Assert that the content provider is of one of the * supported types. * @param provider */ protected void assertContentProviderType(IContentProvider provider) { Assert.isTrue(provider instanceof IStructuredContentProvider); } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.Viewer#setInput(java.lang.Object) */ public final void setInput(Object input) { try { // fInChange= true; unmapAllElements(); super.setInput(input); } finally { // fInChange= false; } } /* * (non-Javadoc) * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean) */ public void setSelection(ISelection selection, boolean reveal) { /** * <p> * If the new selection differs from the current selection the hook * <code>updateSelection</code> is called. * </p> * <p> * If <code>setSelection</code> is called from within * <code>preserveSelection</code>, the call to * <code>updateSelection</code> is delayed until the end of * <code>preserveSelection</code>. * </p> * <p> * Subclasses do not typically override this method, but implement * <code>setSelectionToWidget</code> instead. * </p> */ Control control = getControl(); if (control == null || control.isDisposed()) { return; } if (!inChange) { setSelectionToWidget(selection, reveal); ISelection sel = getSelection(); updateSelection(sel); firePostSelectionChanged(new SelectionChangedEvent(this, sel)); } else { restoreSelection = false; setSelectionToWidget(selection, reveal); } } /** * Parlays the given list of selected elements into selections on this * viewer's control. * <p> * Subclasses should override to set their selection based on the given list * of elements. * </p> * * @param l * list of selected elements (element type: <code>Object</code>) * or <code>null</code> if the selection is to be cleared * @param reveal * <code>true</code> if the selection is to be made visible, * and <code>false</code> otherwise */ protected abstract void setSelectionToWidget(List l, boolean reveal); /** * Converts the selection to a <code>List</code> and calls * <code>setSelectionToWidget(List, boolean)</code>. The selection is * expected to be an <code>IStructuredSelection</code> of elements. If * not, the selection is cleared. * <p> * Subclasses do not typically override this method, but implement * <code>setSelectionToWidget(List, boolean)</code> instead. * * @param selection * an IStructuredSelection of elements * @param reveal * <code>true</code> to reveal the first element in the * selection, or <code>false</code> otherwise */ protected void setSelectionToWidget(ISelection selection, boolean reveal) { if (selection instanceof IStructuredSelection) { setSelectionToWidget(((IStructuredSelection) selection).toList(), reveal); } else { setSelectionToWidget((List) null, reveal); } } /** * Sets this viewer's sorter and triggers refiltering and resorting of this * viewer's element. Passing <code>null</code> turns sorting off. * <p> * It is recommended to use <code>setComparator()</code> instead. * </p> * * @param sorter * a viewer sorter, or <code>null</code> if none */ public void setSorter(ViewerSorter sorter) { if (this.sorter != sorter) { this.sorter = sorter; refresh(); } } /** * Sets this viewer's comparator to be used for sorting elements, and triggers refiltering and * resorting of this viewer's element. <code>null</code> turns sorting off. * To get the viewer's comparator, call <code>getComparator()</code>. * <p> * IMPORTANT: This method was introduced in 3.2. If a reference to this viewer object * is passed to clients who call <code>getSorter()<code>, null may be returned from * from that method even though the viewer is sorting its elements using the * viewer's comparator. * </p> * * @param comparator a viewer comparator, or <code>null</code> if none * * @since 3.2 */ public void setComparator(ViewerComparator comparator){ if (this.sorter != comparator){ this.sorter = comparator; refresh(); } } /** * Configures whether this structured viewer uses an internal hash table to * speeds up the mapping between elements and SWT items. This must be called * before the viewer is given an input (via <code>setInput</code>). * * @param enable * <code>true</code> to enable hash lookup, and * <code>false</code> to disable it */ public void setUseHashlookup(boolean enable) { Assert.isTrue(getInput() == null, "Can only enable the hash look up before input has been set");//$NON-NLS-1$ if (enable) { elementMap = newHashtable(CustomHashtable.DEFAULT_CAPACITY); } else { elementMap = null; } } /** * Sets the comparer to use for comparing elements, or <code>null</code> * to use the default <code>equals</code> and <code>hashCode</code> * methods on the elements themselves. * * @param comparer * the comparer to use for comparing elements or * <code>null</code> */ public void setComparer(IElementComparer comparer) { this.comparer = comparer; if (elementMap != null) { elementMap = new CustomHashtable(elementMap, comparer); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -