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.IField;
010: import org.eclipse.jdt.core.IJavaElement;
011: import org.eclipse.jdt.core.IJavaProject;
012: import org.eclipse.jdt.core.IPackageFragment;
013: import org.eclipse.jdt.core.IPackageFragmentRoot;
014: import org.eclipse.jdt.core.IType;
015: import org.eclipse.jdt.core.JavaModelException;
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 FieldBehavior implements NavigatorBehavior {
029:
030: public static final String ADD_MSG = "Enter Fully Qualified Field Name";
031: private static final String SELECT_FIELD = "Select Member Field";
032: private final List m_selectedValues;
033:
034: public FieldBehavior() {
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_FIELD;
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: || element instanceof IField) {
058: return true;
059: }
060: if (element instanceof IPackageFragment) {
061: try {
062: return ((IPackageFragment) element)
063: .containsJavaResources();
064: } catch (JavaModelException jme) {/**/
065: }
066: }
067: return false;
068: }
069: };
070: }
071:
072: public ISelectionChangedListener getSelectionChangedListener(
073: final PackageNavigator nav) {
074: return new ISelectionChangedListener() {
075: public void selectionChanged(SelectionChangedEvent event) {
076: m_selectedValues.removeAll(m_selectedValues);
077: StructuredSelection selection = (StructuredSelection) event
078: .getSelection();
079: List selectedFields = new ArrayList();
080: if (!selection.isEmpty()) {
081: Object element;
082: for (Iterator i = selection.iterator(); i.hasNext();) {
083: if ((element = i.next()) instanceof IField) {
084: IField field = (IField) element;
085: m_selectedValues
086: .add(resolveFullName(field));
087: selectedFields.add(element);
088: }
089: }
090: }
091: if (selectedFields.size() > 0)
092: nav.okButtonEnabled(true);
093: else
094: nav.okButtonEnabled(false);
095: }
096: };
097: }
098:
099: private String resolveFullName(IJavaElement field) {
100: List list = new ArrayList();
101: list.add(field.getElementName());
102: IJavaElement javaPart = field.getParent();
103: while (javaPart != null) {
104: if (javaPart.getElementType() == IJavaElement.TYPE
105: || javaPart.getElementType() == IJavaElement.PACKAGE_FRAGMENT
106: || javaPart.getElementType() == IJavaElement.PACKAGE_DECLARATION) {
107: list.add(javaPart.getElementName());
108:
109: }
110: javaPart = javaPart.getParent();
111: }
112: String[] parts = (String[]) list.toArray(new String[0]);
113: StringBuffer name = new StringBuffer();
114: for (int i = parts.length - 1; i >= 1; i--) {
115: name.append(parts[i] + ".");
116: }
117: name.append(parts[0]);
118: return name.toString();
119: }
120:
121: public Object getValues() {
122: return m_selectedValues.toArray(new String[0]);
123: }
124: }
|