001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package net.refractions.udig.internal.ui.operations;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: import net.refractions.udig.ui.operations.OpAction;
018:
019: import org.eclipse.jface.viewers.ITreeContentProvider;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.ui.activities.WorkbenchActivityHelper;
022:
023: /**
024: * Provides content for viewers that wish to show Operations.
025: */
026: public class OperationContentProvider implements ITreeContentProvider {
027:
028: /**
029: * Child cache. Map from Object->Object[]. Our hasChildren() method is
030: * expensive so it's better to cache the results of getChildren().
031: */
032: private Map childMap = new HashMap();
033:
034: /**
035: * Create a new instance of the ViewContentProvider.
036: */
037: public OperationContentProvider() {
038: //no-op
039: }
040:
041: /*
042: * (non-Javadoc)
043: *
044: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
045: */
046: public void dispose() {
047: childMap.clear();
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
054: */
055: public Object[] getChildren(Object element) {
056: Object[] children = (Object[]) childMap.get(element);
057: if (children == null) {
058: children = createChildren(element);
059: childMap.put(element, children);
060: }
061: return children;
062: }
063:
064: /**
065: * Does the actual work of getChildren.
066: *
067: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
068: */
069: private Object[] createChildren(Object element) {
070: if (element instanceof OperationMenuFactory) {
071: OperationMenuFactory opMenuFactory = (OperationMenuFactory) element;
072: Map<String, OperationCategory> categoriesMap = opMenuFactory
073: .getCategories();
074:
075: ArrayList filtered = new ArrayList();
076: ;
077: for (OperationCategory category : categoriesMap.values()) {
078: if (!hasChildren(category))
079: continue;
080:
081: filtered.add(category);
082: }
083: filtered.addAll(opMenuFactory.getActions());
084: Object[] topLevelElements = filtered
085: .toArray(new Object[filtered.size()]);
086:
087: // if there is only one category, return it's children directly
088: if (topLevelElements.length == 1) {
089: return getChildren(topLevelElements[0]);
090: }
091: return topLevelElements;
092: } else if (element instanceof OperationCategory) {
093: List<OpAction> actionList = ((OperationCategory) element)
094: .getActions();
095: OpAction[] actions = actionList
096: .toArray(new OpAction[actionList.size()]);
097: if (actions != null) {
098: ArrayList filtered = new ArrayList();
099: for (int i = 0; i < actions.length; i++) {
100: OpAction action = actions[i];
101: if (!action.isEnabled()
102: || WorkbenchActivityHelper
103: .filterItem(action))
104: continue;
105: filtered.add(action);
106: }
107: return filtered.toArray();
108: }
109: }
110:
111: return new Object[0];
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
118: */
119: public Object[] getElements(Object element) {
120: return getChildren(element);
121: }
122:
123: /*
124: * (non-Javadoc)
125: *
126: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
127: */
128: public Object getParent(Object element) {
129: return null;
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
136: */
137: public boolean hasChildren(java.lang.Object element) {
138: if (element instanceof OperationMenuFactory)
139: return true;
140: else if (element instanceof OperationCategory) {
141: if (getChildren(element).length > 0)
142: return true;
143: }
144: return false;
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
151: * java.lang.Object, java.lang.Object)
152: */
153: public void inputChanged(Viewer viewer, Object oldInput,
154: Object newInput) {
155: childMap.clear();
156: }
157: }
|