01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package org.terracotta.dso.editors.chooser;
06:
07: import org.eclipse.core.resources.IFile;
08: import org.eclipse.core.resources.IFolder;
09: import org.eclipse.jdt.core.IJavaModel;
10: import org.eclipse.jdt.core.IJavaProject;
11: import org.eclipse.jdt.core.IPackageFragment;
12: import org.eclipse.jdt.core.IPackageFragmentRoot;
13: import org.eclipse.jface.viewers.ISelectionChangedListener;
14: import org.eclipse.jface.viewers.SelectionChangedEvent;
15: import org.eclipse.jface.viewers.StructuredSelection;
16: import org.eclipse.jface.viewers.Viewer;
17: import org.eclipse.jface.viewers.ViewerFilter;
18: import org.eclipse.swt.SWT;
19:
20: public class FileBehavior implements NavigatorBehavior {
21:
22: private static final String SELECT_FILE = "Select File";
23: protected String m_selectedValue;
24:
25: public int style() {
26: return SWT.SINGLE;
27: }
28:
29: public String getTitle() {
30: return SELECT_FILE;
31: }
32:
33: public ViewerFilter getFilter(final IJavaProject javaProject) {
34: return new ViewerFilter() {
35: public boolean select(Viewer viewer, Object parentElement,
36: Object element) {
37: return filterSelect(viewer, parentElement, element);
38: }
39: };
40: }
41:
42: protected boolean filterSelect(Viewer viewer, Object parentElement,
43: Object element) {
44: if (element instanceof IJavaModel)
45: return true;
46: if (element instanceof IJavaProject)
47: return true;
48: if (element instanceof IFolder)
49: return true;
50: if (element instanceof IFile)
51: return true;
52: if (element instanceof IPackageFragment)
53: return true;
54: if (element instanceof IPackageFragmentRoot)
55: return true;
56: return false;
57: }
58:
59: public ISelectionChangedListener getSelectionChangedListener(
60: final PackageNavigator nav) {
61: return new ISelectionChangedListener() {
62: public void selectionChanged(SelectionChangedEvent event) {
63: nav.okButtonEnabled(true);
64: StructuredSelection selection = (StructuredSelection) event
65: .getSelection();
66: if (!selection.isEmpty()) {
67: Object element = selection.getFirstElement();
68: if (element != null) {
69: if (element instanceof IJavaProject
70: || element instanceof IFolder) {
71: nav.okButtonEnabled(false);
72: } else if (element instanceof IFile) {
73: IFile file = (IFile) element;
74: m_selectedValue = file
75: .getProjectRelativePath()
76: .toString();
77: nav.okButtonEnabled(true);
78: }
79: }
80: }
81: }
82: };
83: }
84:
85: public Object getValues() {
86: return m_selectedValue;
87: }
88: }
|