| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.authoring.ui.providers; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Collection; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.Map; |
| |
| import org.eclipse.emf.common.notify.AdapterFactory; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.edit.provider.ITreeItemContentProvider; |
| import org.eclipse.emf.edit.provider.IViewerNotification; |
| import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
| import org.eclipse.epf.library.edit.FeatureValueWrapperItemProvider; |
| import org.eclipse.epf.library.edit.category.CustomCategoryItemProvider; |
| import org.eclipse.epf.library.edit.navigator.ConfigurationsItemProvider; |
| import org.eclipse.epf.library.edit.navigator.PluginUIPackagesItemProvider; |
| import org.eclipse.epf.library.edit.util.TngUtil; |
| import org.eclipse.epf.library.services.SafeUpdateController; |
| import org.eclipse.epf.library.util.LibraryUtil; |
| import org.eclipse.epf.uma.ContentPackage; |
| import org.eclipse.epf.uma.CustomCategory; |
| import org.eclipse.epf.uma.MethodLibrary; |
| import org.eclipse.epf.uma.MethodPlugin; |
| import org.eclipse.jface.viewers.CheckboxTreeViewer; |
| import org.eclipse.jface.viewers.Viewer; |
| |
| /** |
| * Content provider for Custom Category view of configuration page |
| * |
| * @author Jeff Hardy |
| */ |
| |
| public class CustomCategoryContentProvider extends AdapterFactoryContentProvider { |
| |
| // there is a problem in the passed in AdapterFactory |
| // it can't find the correct parent from the child |
| // need to build a map of child to it's parent if the child is a UI |
| // folder |
| // clear the map when the input is set |
| Map<Object, Object> childUIParentMap = new HashMap<Object, Object>(); |
| |
| /** |
| * Create an instance |
| * @param adapterFactory |
| */ |
| public CustomCategoryContentProvider(AdapterFactory adapterFactory) { |
| super(adapterFactory); |
| } |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#getChildren(java.lang.Object) |
| */ |
| public Object[] getChildren(Object parentElement) { |
| Collection<Object> children = new ArrayList<Object>(); |
| if (parentElement instanceof MethodPlugin) { |
| // get root CC of plugin |
| CustomCategory rootCC = (TngUtil.getRootCustomCategory((MethodPlugin)parentElement)); |
| children.addAll(Arrays.asList(super.getChildren(rootCC))); |
| children = unwrapFVWIPs(children); |
| children = filterCCElements(children); |
| } else if (parentElement instanceof PluginUIPackagesItemProvider) { |
| children.addAll(Arrays.asList(super.getChildren(parentElement))); |
| children = filterPlugin(children); |
| } else if (parentElement instanceof CustomCategory) { |
| children.addAll(Arrays.asList(super.getChildren(parentElement))); |
| children = unwrapFVWIPs(children); |
| children = filterCCElements(children); |
| } else if (parentElement instanceof FeatureValueWrapperItemProvider) { |
| children.addAll(Arrays.asList(super.getChildren(parentElement))); |
| children = unwrapFVWIPs(children); |
| children = filterCCElements(children); |
| } else if (parentElement instanceof MethodLibrary) { |
| children.addAll(Arrays.asList(getElements(parentElement))); |
| } |
| createChildParentMap(parentElement, children); |
| return children.toArray(); |
| } |
| |
| private void createChildParentMap(Object parent, Collection children) { |
| if (children == null || children.size() == 0) { |
| return; |
| } |
| for (Iterator iter = children.iterator();iter.hasNext();) { |
| Object child = iter.next(); |
| // if the parent is a UI folder, map the child to the parent |
| // so that we can get the parent later |
| // if ( !(parent instanceof MethodElement) ) |
| { |
| childUIParentMap.put(child, parent); |
| } |
| } |
| } |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#getElements(java.lang.Object) |
| */ |
| public Object[] getElements(Object inputElement) { |
| Collection<Object> elements = new ArrayList<Object>(Arrays.asList(super.getElements(inputElement))); |
| elements = filterPlugin(elements); |
| return elements.toArray(); |
| } |
| |
| private Collection<Object> filterPlugin(Collection<Object> elements) { |
| for (Iterator iter = elements.iterator();iter.hasNext();) { |
| Object element = iter.next(); |
| if (element instanceof MethodPlugin) { |
| // filter out plugins that have no CC's |
| if (!hasCCs((MethodPlugin)element)) { |
| iter.remove(); |
| } |
| } else if (element instanceof PluginUIPackagesItemProvider) { |
| // filter out plugin packages that have no plugins with CC's |
| if (!hasCCs((PluginUIPackagesItemProvider)element)) { |
| iter.remove(); |
| } |
| } else if (element instanceof ConfigurationsItemProvider) { |
| // remove "Configurations" folder |
| iter.remove(); |
| } |
| } |
| return elements; |
| } |
| |
| private Collection<Object> filterCCElements(Collection<Object> elements) { |
| for (Iterator iter = elements.iterator();iter.hasNext();) { |
| Object element = iter.next(); |
| element = TngUtil.unwrap(element); |
| if (!(element instanceof CustomCategory)) { |
| // filter out non CC's |
| iter.remove(); |
| } |
| } |
| return elements; |
| } |
| |
| private Collection<Object> unwrapFVWIPs(Collection<Object> elements) { |
| Collection<Object> result = new ArrayList<Object>(); |
| for (Iterator iter = elements.iterator();iter.hasNext();) { |
| Object element = iter.next(); |
| element = TngUtil.unwrap(element); |
| if (element instanceof CustomCategory) { |
| result.add(getTreeItemForCC((CustomCategory)element)); |
| } |
| } |
| return result; |
| |
| } |
| |
| |
| private boolean hasCCs(MethodPlugin plugin) { |
| CustomCategory rootCC = TngUtil.getRootCustomCategory(plugin); |
| if (rootCC.getCategorizedElements().isEmpty()) { |
| return false; |
| } else { |
| return true; |
| } |
| } |
| |
| private boolean hasCCs(PluginUIPackagesItemProvider pluginPackage) { |
| Collection<MethodPlugin> plugins = pluginPackage.getPlugins(); |
| boolean hasPluginsWithCCs = false; |
| for (Iterator<MethodPlugin> pluginIter = plugins.iterator();pluginIter.hasNext();) { |
| MethodPlugin plugin = pluginIter.next(); |
| // get root CC of plugin |
| CustomCategory rootCC = (TngUtil.getRootCustomCategory(plugin)); |
| if (!rootCC.getCategorizedElements().isEmpty()) { |
| hasPluginsWithCCs = true; |
| break; |
| } |
| } |
| if (hasPluginsWithCCs) { |
| return true; |
| } else { |
| return false; |
| } |
| |
| } |
| |
| // |
| // private boolean isUIFolder(Object e) { |
| // return (e instanceof ContentItemProvider |
| // || e instanceof ProcessesItemProvider || e instanceof MethodPackagesItemProvider || |
| // e instanceof PluginUIPackagesItemProvider); |
| // } |
| // |
| // public Object getUITargetElement(Object e) { |
| // if (e instanceof CustomCategory) { |
| // return ((CustomCategory) e).eContainer(); |
| // } else if (e instanceof MethodElement) { |
| // return e; |
| // } else if (e instanceof ContentItemProvider) { |
| // return ((ContentItemProvider) e).getParent(null); |
| // } else if (e instanceof ProcessesItemProvider) { |
| // return ((ProcessesItemProvider) e).getParent(null); |
| // } else if (e instanceof ItemProviderAdapter) { |
| // Object target = ((ItemProviderAdapter) e).getTarget(); |
| // if (target != null && target instanceof MethodElement) { |
| // return target; |
| // } |
| // } |
| // |
| // return null; |
| // } |
| // |
| // private Object[] getValidItems(Object parent, Object[] elements) { |
| // if (elements == null || elements.length == 0) { |
| // return elements; |
| // } |
| // |
| // List pkgs = new ArrayList(); |
| // for (int i = 0; i < elements.length; i++) { |
| // Object e = LibraryUtil.unwrap(elements[i]); |
| // boolean uiFolder = isUIFolder(e); |
| // if (uiFolder || e instanceof MethodPackage |
| // || e instanceof MethodPlugin) { |
| // pkgs.add(e); |
| // |
| // if (uiFolder) { |
| // uiFolders.add(e); |
| // } |
| // |
| // // if the parent is a UI folder, map the child to the parent |
| // // so that we can get the parent later |
| // // if ( !(parent instanceof MethodElement) ) |
| // { |
| // childUIParentMap.put(e, parent); |
| // } |
| // } else { |
| // ; // System.out.println("Ignored: " + e); |
| // } |
| // } |
| // |
| // return pkgs.toArray(); |
| // } |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#getParent(java.lang.Object) |
| */ |
| public Object getParent(Object element) { |
| if (childUIParentMap.containsKey(element)) { |
| return childUIParentMap.get(element); |
| } else { |
| // try to determine parent |
| Object parent = super.getParent(element); |
| if (parent instanceof ContentPackage && |
| element instanceof CustomCategory) { |
| return LibraryUtil.getMethodPlugin((CustomCategory)element); |
| } else if (parent instanceof CustomCategory) { |
| parent = getTreeItemForCC((CustomCategory)parent); |
| } |
| // else if (parent instanceof FeatureValueWrapperItemProvider) { |
| // parent = TngUtil.unwrap(parent); |
| // } |
| return parent; |
| } |
| } |
| |
| public Object getTreeItemForCC(CustomCategory cc) { |
| // get ITreeContentProvider adapater |
| CustomCategoryItemProvider adapter = (CustomCategoryItemProvider) getAdapterFactory().adapt(cc, ITreeItemContentProvider.class); |
| for (Iterator iter = adapter.getNotifyChangedListeners().iterator(); iter.hasNext();) { |
| Object listener = iter.next(); |
| if (listener instanceof FeatureValueWrapperItemProvider |
| && TngUtil.unwrap(listener) == cc) { |
| return (FeatureValueWrapperItemProvider)listener; |
| } |
| } |
| return cc; |
| } |
| |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#hasChildren(java.lang.Object) |
| */ |
| public boolean hasChildren(Object element) { |
| Object[] children = getChildren(element); |
| return (children != null && children.length > 0); |
| } |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) |
| */ |
| public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| super.inputChanged(viewer, oldInput, newInput); |
| childUIParentMap.clear(); |
| } |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#notifyChanged(org.eclipse.emf.common.notify.Notification) |
| */ |
| public void notifyChanged(Notification notification) { |
| super.notifyChanged(notification); |
| |
| if ((notification instanceof IViewerNotification)) { |
| final IViewerNotification vnt = (IViewerNotification) notification; |
| final Object element = vnt.getElement(); |
| final CheckboxTreeViewer ctrl = ((CheckboxTreeViewer) super.viewer); |
| if (element != null && (vnt.getEventType() == Notification.ADD |
| || vnt.getEventType() == Notification.SET)) { |
| |
| SafeUpdateController.syncExec(new Runnable() { |
| public void run() { |
| ctrl.refresh(); |
| } |
| }); |
| } |
| } |
| } |
| |
| /** |
| * @see org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider#dispose() |
| */ |
| public void dispose() { |
| super.dispose(); |
| childUIParentMap.clear(); |
| } |
| } |