001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.editors.chooser;
006:
007: import org.eclipse.jdt.core.IClassFile;
008: import org.eclipse.jdt.core.ICompilationUnit;
009: import org.eclipse.jdt.core.IJavaProject;
010: import org.eclipse.jdt.core.IPackageFragment;
011: import org.eclipse.jdt.core.IPackageFragmentRoot;
012: import org.eclipse.jdt.core.IType;
013: import org.eclipse.jdt.core.JavaModelException;
014: import org.eclipse.jdt.internal.core.BinaryType;
015: import org.eclipse.jdt.internal.core.SourceType;
016: import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.SelectionChangedEvent;
019: import org.eclipse.jface.viewers.StructuredSelection;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.jface.viewers.ViewerFilter;
022: import org.eclipse.swt.SWT;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: public final class ClassBehavior implements NavigatorBehavior {
029:
030: public static final String ADD_MSG = "Enter Fully Qualified Class Name";
031: private static final String SELECT_CLASS = "Select Class";
032: private final List m_selectedValues;
033:
034: public ClassBehavior() {
035: this .m_selectedValues = new ArrayList();
036: }
037:
038: public int style() {
039: return SWT.MULTI;
040: }
041:
042: public String getTitle() {
043: return SELECT_CLASS;
044: }
045:
046: public ViewerFilter getFilter(final IJavaProject javaProject) {
047: return new ViewerFilter() {
048: public boolean select(Viewer viewer, Object parentElement,
049: Object element) {
050: if (element instanceof IJavaProject)
051: return true;
052: if (element instanceof ClassPathContainer
053: || element instanceof ICompilationUnit
054: || element instanceof IType
055: || element instanceof IPackageFragmentRoot
056: || element instanceof IClassFile) {
057: return true;
058: }
059: if (element instanceof IPackageFragment) {
060: try {
061: return ((IPackageFragment) element)
062: .containsJavaResources();
063: } catch (JavaModelException jme) {/**/
064: }
065: }
066: return false;
067: }
068: };
069: }
070:
071: public ISelectionChangedListener getSelectionChangedListener(
072: final PackageNavigator nav) {
073: return new ISelectionChangedListener() {
074: public void selectionChanged(SelectionChangedEvent event) {
075: m_selectedValues.removeAll(m_selectedValues);
076: StructuredSelection selection = (StructuredSelection) event
077: .getSelection();
078: List selectedClasses = new ArrayList();
079: if (!selection.isEmpty()) {
080: Object element;
081: for (Iterator i = selection.iterator(); i.hasNext();) {
082: element = i.next();
083: if (element instanceof SourceType
084: || element instanceof BinaryType) {
085: IType clazz = (IType) element;
086: m_selectedValues.add(clazz
087: .getFullyQualifiedName());
088: selectedClasses.add(element);
089: }
090: }
091: }
092: if (selectedClasses.size() > 0)
093: nav.okButtonEnabled(true);
094: else
095: nav.okButtonEnabled(false);
096: }
097: };
098: }
099:
100: public Object getValues() {
101: return m_selectedValues.toArray(new String[0]);
102: }
103: }
|