📄 structuredviewer.java
字号:
* than the elements' own equals and hashCode methods. * * @return the comparer to use for comparing elements or * <code>null</code> */ public IElementComparer getComparer() { return comparer; } /** * Returns the filtered array of children of the given element. The * resulting array must not be modified, as it may come directly from the * model's internal state. * * @param parent * the parent element * @return a filtered array of child elements */ protected Object[] getFilteredChildren(Object parent) { Object[] result = getRawChildren(parent); if (filters != null) { for (Iterator iter = filters.iterator(); iter.hasNext();) { ViewerFilter f = (ViewerFilter) iter.next(); result = f.filter(this, parent, result); } } return result; } /** * Returns this viewer's filters. * * @return an array of viewer filters */ public ViewerFilter[] getFilters() { if (filters == null) { return new ViewerFilter[0]; } ViewerFilter[] result = new ViewerFilter[filters.size()]; filters.toArray(result); return result; } /** * Returns the item at the given display-relative coordinates, or * <code>null</code> if there is no item at that location. * <p> * The default implementation of this method returns <code>null</code>. * </p> * * @param x * horizontal coordinate * @param y * vertical coordinate * @return the item, or <code>null</code> if there is no item at the given * coordinates */ protected Item getItem(int x, int y) { return null; } /** * Returns the children of the given parent without sorting and filtering * them. The resulting array must not be modified, as it may come directly * from the model's internal state. * <p> * Returns an empty array if the given parent is <code>null</code>. * </p> * * @param parent * the parent element * @return the child elements */ protected Object[] getRawChildren(Object parent) { Object[] result = null; if (parent != null) { IStructuredContentProvider cp = (IStructuredContentProvider) getContentProvider(); if (cp != null) { result = cp.getElements(parent); assertElementsNotNull(result); } } return (result != null) ? result : new Object[0]; } /** * Returns the root element. * <p> * The default implementation of this framework method forwards to * <code>getInput</code>. Override if the root element is different from * the viewer's input element. * </p> * * @return the root element, or <code>null</code> if none */ protected Object getRoot() { return getInput(); } /** * The <code>StructuredViewer</code> implementation of this method returns * the result as an <code>IStructuredSelection</code>. * <p> * Subclasses do not typically override this method, but implement * <code>getSelectionFromWidget(List)</code> instead. * <p> * @return ISelection */ public ISelection getSelection() { Control control = getControl(); if (control == null || control.isDisposed()) { return StructuredSelection.EMPTY; } List list = getSelectionFromWidget(); return new StructuredSelection(list); } /** * Retrieves the selection, as a <code>List</code>, from the underlying * widget. * * @return the list of selected elements */ protected abstract List getSelectionFromWidget(); /** * Returns the sorted and filtered set of children of the given element. The * resulting array must not be modified, as it may come directly from the * model's internal state. * * @param parent * the parent element * @return a sorted and filtered array of child elements */ protected Object[] getSortedChildren(Object parent) { Object[] result = getFilteredChildren(parent); if (sorter != null) { // be sure we're not modifying the original array from the model result = (Object[]) result.clone(); sorter.sort(this, result); } return result; } /** * Returns this viewer's sorter, or <code>null</code> if it does not have * one. If this viewer has a comparator that was set via * <code>setComparator(ViewerComparator)</code> then this method will return * <code>null</code> if the comparator is not an instance of ViewerSorter. * <p> * It is recommended to use <code>getComparator()</code> instead. * </p> * * @return a viewer sorter, or <code>null</code> if none or if the comparator is * not an instance of ViewerSorter */ public ViewerSorter getSorter() { if (sorter instanceof ViewerSorter) return (ViewerSorter)sorter; return null; } /** * Return this viewer's comparator used to sort elements. * This method should be used instead of <code>getSorter()</code>. * * @return a viewer comparator, or <code>null</code> if none * * @since 3.2 */ public ViewerComparator getComparator(){ return sorter; } /** * Handles a double-click select event from the widget. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param event * the SWT selection event */ protected void handleDoubleSelect(SelectionEvent event) { // This method is reimplemented in AbstractTreeViewer to fix bug 108102. // handle case where an earlier selection listener disposed the control. Control control = getControl(); if (control != null && !control.isDisposed()) { // If the double-clicked element can be obtained from the event, use it // otherwise get it from the control. Some controls like List do // not have the notion of item. // For details, see bug 90161 [Navigator] DefaultSelecting folders shouldn't always expand first one ISelection selection; if (event.item != null && event.item.getData() != null) { selection = new StructuredSelection(event.item.getData()); } else { selection = getSelection(); updateSelection(selection); } fireDoubleClick(new DoubleClickEvent(this, selection)); } } /** * Handles an open event from the OpenStrategy. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param event * the SWT selection event */ protected void handleOpen(SelectionEvent event) { Control control = getControl(); if (control != null && !control.isDisposed()) { ISelection selection = getSelection(); fireOpen(new OpenEvent(this, selection)); } } /** * Handles an invalid selection. * <p> * This framework method is called if a model change picked up by a viewer * results in an invalid selection. For instance if an element contained in * the selection has been removed from the viewer, the viewer is free to * either remove the element from the selection or to pick another element * as its new selection. The default implementation of this method calls * <code>updateSelection</code>. Subclasses may override it to implement * a different strategy for picking a new selection when the old selection * becomes invalid. * </p> * * @param invalidSelection * the selection before the viewer was updated * @param newSelection * the selection after the update, or <code>null</code> if none */ protected void handleInvalidSelection(ISelection invalidSelection, ISelection newSelection) { updateSelection(newSelection); SelectionChangedEvent event = new SelectionChangedEvent(this, newSelection); firePostSelectionChanged(event); } /** * The <code>StructuredViewer</code> implementation of this * <code>ContentViewer</code> method calls <code>update</code> if the * event specifies that the label of a given element has changed, otherwise * it calls super. Subclasses may reimplement or extend. * </p> * @param event the event that generated this update */ protected void handleLabelProviderChanged(LabelProviderChangedEvent event) { Object[] elements = event.getElements(); if (elements != null) { update(elements, null); } else { super.handleLabelProviderChanged(event); } } /** * Handles a select event from the widget. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param event * the SWT selection event */ protected void handleSelect(SelectionEvent event) { // handle case where an earlier selection listener disposed the control. Control control = getControl(); if (control != null && !control.isDisposed()) { updateSelection(getSelection()); } } /** * Handles a post select event from the widget. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param e the SWT selection event */ protected void handlePostSelect(SelectionEvent e) { SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection()); firePostSelectionChanged(event); } /* * (non-Javadoc) Method declared on Viewer. */ protected void hookControl(Control control) { super.hookControl(control); OpenStrategy handler = new OpenStrategy(control); handler.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { handleSelect(e); } public void widgetDefaultSelected(SelectionEvent e) { handleDoubleSelect(e); } }); handler.addPostSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { handlePostSelect(e); } }); handler.addOpenListener(new IOpenEventListener() { public void handleOpen(SelectionEvent e) { StructuredViewer.this.handleOpen(e); } }); } /** * Returns whether this viewer has any filters. * @return boolean */ protected boolean hasFilters() { return filters != null && filters.size() > 0; } /** * Refreshes this viewer starting at the given element. * * @param element * the element */ protected abstract void internalRefresh(Object element); /** * Refreshes this viewer starting at the given element. Labels are updated * as described in <code>refresh(boolean updateLabels)</code>. * <p> * The default implementation simply calls * <code>internalRefresh(element)</code>, ignoring * <code>updateLabels</code>. * <p> * If this method is overridden to do the actual refresh, then * <code>internalRefresh(Object element)</code> should simply call * <code>internalRefresh(element, true)</code>. * * @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 */ protected void internalRefresh(Object element, boolean updateLabels) { internalRefresh(element); } /** * Adds the element item pair to the element map. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param element * the element * @param item * the corresponding widget */ protected void mapElement(Object element, Widget item) { if (elementMap != null) { Object widgetOrWidgets = elementMap.get(element); if (widgetOrWidgets == null) { elementMap.put(element, item); } else if (widgetOrWidgets instanceof Widget) { if (widgetOrWidgets != item) { elementMap.put(element, new Widget[] { (Widget) widgetOrWidgets, item }); } } else { Widget[] widgets = (Widget[]) widgetOrWidgets; int indexOfItem = Arrays.asList(widgets).indexOf(item); if (indexOfItem == -1) { int length = widgets.length; System.arraycopy(widgets, 0, widgets = new Widget[length + 1], 0, length); widgets[length] = item; elementMap.put(element, widgets); } } } } /** * Determines whether a change to the given property of the given element * would require refiltering and/or resorting. * <p> * This method is internal to the framework; subclassers should not call * this method. * </p> * * @param element * the element * @param property * the property * @return <code>true</code> if refiltering is required, and * <code>false</code> otherwise */ protected boolean needsRefilter(Object element, String property) { if (sorter != null && sorter.isSorterProperty(element, property)) { return true; } if (filters != null) { for (int i = 0, n = filters.size(); i < n; ++i) { ViewerFilter filter = (ViewerFilter) filters.get(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -