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.core.resources.IFile;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.core.resources.ResourcesPlugin;
010: import org.eclipse.jdt.core.IJavaElement;
011: import org.eclipse.jdt.core.IJavaProject;
012: import org.eclipse.jdt.core.JavaCore;
013: import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
014: import org.eclipse.jdt.internal.core.PackageFragmentRoot;
015: import org.eclipse.jdt.internal.ui.JavaWorkbenchAdapter;
016: import org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider;
017: import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
018: import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
019: import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
020: import org.eclipse.jdt.ui.JavaElementLabels;
021: import org.eclipse.jface.dialogs.IDialogConstants;
022: import org.eclipse.jface.dialogs.MessageDialog;
023: import org.eclipse.jface.viewers.ISelection;
024: import org.eclipse.jface.viewers.ISelectionChangedListener;
025: import org.eclipse.jface.viewers.StructuredSelection;
026: import org.eclipse.jface.viewers.TreeViewer;
027: import org.eclipse.jface.viewers.ViewerSorter;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.events.MouseAdapter;
030: import org.eclipse.swt.events.MouseEvent;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Shell;
036: import org.eclipse.swt.widgets.Tree;
037: import org.eclipse.swt.widgets.TreeItem;
038: import org.eclipse.ui.model.IWorkbenchAdapter;
039: import org.eclipse.ui.model.WorkbenchContentProvider;
040: import org.terracotta.ui.util.SWTUtil;
041:
042: import com.tc.util.event.EventMulticaster;
043: import com.tc.util.event.UpdateEvent;
044: import com.tc.util.event.UpdateEventListener;
045:
046: import java.util.ArrayList;
047: import java.util.List;
048:
049: public class PackageNavigator extends MessageDialog {
050:
051: private final Shell m_parentShell;
052: private final IProject m_project;
053: private final EventMulticaster m_valueListener;
054: private final NavigatorBehavior m_behavior;
055: private Layout m_layout;
056:
057: public PackageNavigator(Shell shell, String title,
058: IProject project, NavigatorBehavior behavior) {
059: super (shell, title, null, null, MessageDialog.NONE,
060: new String[] { IDialogConstants.OK_LABEL,
061: IDialogConstants.CANCEL_LABEL }, 0);
062: setShellStyle(getShellStyle() | SWT.RESIZE);
063: this .m_parentShell = shell;
064: this .m_project = project;
065: this .m_valueListener = new EventMulticaster();
066: this .m_behavior = behavior;
067: }
068:
069: protected void configureShell(Shell shell) {
070: super .configureShell(shell);
071: shell.setSize(400, 300);
072: if (m_parentShell != null) {
073: SWTUtil.placeDialogInCenter(m_parentShell, shell);
074: }
075: }
076:
077: protected Control createDialogArea(Composite parent) {
078: parent.setLayout(new GridLayout());
079: return super .createDialogArea(parent);
080: }
081:
082: protected Control createButtonBar(Composite parent) {
083: Composite comp = (Composite) super .createButtonBar(parent);
084: comp.setLayout(new GridLayout(2, false));
085: getButton(getDefaultButtonIndex()).setEnabled(false);
086: return comp;
087: }
088:
089: void okButtonEnabled(boolean enable) {
090: getButton(getDefaultButtonIndex()).setEnabled(enable);
091: }
092:
093: void enableSelection(boolean enable,
094: ISelectionChangedListener listener) {
095: if (enable)
096: m_layout.m_viewer.addSelectionChangedListener(listener);
097: else
098: m_layout.m_viewer.removeSelectionChangedListener(listener);
099: }
100:
101: protected Control createCustomArea(Composite parent) {
102: m_layout = new Layout(parent, m_behavior.style());
103: m_layout.m_viewer
104: .setContentProvider(new JavaHierarchyContentProvider());
105: m_layout.m_viewer.setLabelProvider(new JavaUILabelProvider(
106: AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS
107: | JavaElementLabels.P_COMPRESSED
108: | JavaElementLabels.ALL_CATEGORY,
109: AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
110: | JavaElementImageProvider.SMALL_ICONS));
111: m_layout.m_viewer.setSorter(new ViewerSorter());
112: IJavaProject jproj = JavaCore.create(m_project);
113: m_layout.m_viewer.setInput(JavaCore.create(ResourcesPlugin
114: .getWorkspace().getRoot()));
115: m_layout.m_viewer.addSelectionChangedListener(m_behavior
116: .getSelectionChangedListener(this ));
117: m_layout.m_viewer.addFilter(m_behavior.getFilter(jproj));
118:
119: return parent;
120: }
121:
122: protected void buttonPressed(int buttonId) {
123: tearDown();
124: if (buttonId == IDialogConstants.OK_ID) {
125: m_valueListener.fireUpdateEvent(new UpdateEvent(m_behavior
126: .getValues()));
127: }
128: super .buttonPressed(buttonId);
129: }
130:
131: public void addValueListener(UpdateEventListener listener) {
132: m_valueListener.addListener(listener);
133: }
134:
135: private void tearDown() {
136: m_layout.m_viewer.getTree().setRedraw(false);
137: m_layout.m_viewer.getTree().setEnabled(false);
138: m_layout.m_viewer.getTree().removeAll();
139: }
140:
141: // --------------------------------------------------------------------------------
142:
143: private static class JavaHierarchyContentProvider extends
144: PackageExplorerContentProvider {
145: private final WorkbenchContentProvider m_workbench;
146:
147: public JavaHierarchyContentProvider() {
148: super (true);
149: setIsFlatLayout(true);
150: m_workbench = new WorkbenchContentProvider() {
151: protected IWorkbenchAdapter getAdapter(Object element) {
152: return new JavaWorkbenchAdapter() {
153: public Object[] getChildren(Object parentElement) {
154: List subset = new ArrayList();
155: Object[] children = super
156: .getChildren(parentElement);
157: for (int i = 0; i < children.length; i++) {
158: if (!(children[i] instanceof PackageFragmentRoot && !(children[i] instanceof JarPackageFragmentRoot))) {
159: subset.add(children[i]);
160: }
161: }
162: return subset.toArray();
163: }
164: };
165: }
166: };
167: }
168:
169: public Object[] getChildren(Object parentElement) {
170: List subset = new ArrayList();
171: Object[] children = super .getChildren(parentElement);
172: for (int i = 0; i < children.length; i++) {
173: if (children[i] instanceof JarPackageFragmentRoot) {
174: Object[] workbenchElements = m_workbench
175: .getChildren(((IJavaElement) children[i])
176: .getParent());
177: return workbenchElements;
178: }
179: if (children[i]
180: .getClass()
181: .getName()
182: .equals(
183: "org.eclipse.jdt.internal.core.JarPackageFragment")) {
184: Object[] workbenchElements = m_workbench
185: .getChildren(parentElement);
186: return workbenchElements;
187: }
188: subset.add(children[i]);
189: }
190: return subset.toArray();
191: }
192: }
193:
194: // --------------------------------------------------------------------------------
195:
196: private class Layout {
197: final TreeViewer m_viewer;
198:
199: private Layout(Composite parent, int style) {
200: this .m_viewer = new TreeViewer(parent, style | SWT.BORDER);
201: m_viewer.setAutoExpandLevel(2);
202: m_viewer.getTree().setLayoutData(
203: new GridData(GridData.FILL_BOTH));
204: m_viewer.getTree().addMouseListener(new MouseAdapter() {
205: public void mouseDoubleClick(MouseEvent e) {
206: ISelection sel = m_viewer.getSelection();
207:
208: if (sel instanceof StructuredSelection
209: && !sel.isEmpty()) {
210: Object o = ((StructuredSelection) sel)
211: .getFirstElement();
212:
213: if (o instanceof IFile) {
214: buttonPressed(IDialogConstants.OK_ID);
215: } else {
216: Tree tree = (Tree) e.widget;
217: TreeItem[] selection = tree.getSelection();
218:
219: if (selection.length > 0) {
220: TreeItem item = selection[0];
221: item.setExpanded(!item.getExpanded());
222: m_viewer.refresh();
223: }
224: }
225: }
226: }
227: });
228: }
229: }
230: }
|