001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.views;
006:
007: import org.eclipse.core.resources.IFile;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.core.resources.IResource;
010: import org.eclipse.core.resources.IResourceChangeEvent;
011: import org.eclipse.core.resources.IResourceChangeListener;
012: import org.eclipse.core.resources.IResourceDelta;
013: import org.eclipse.core.resources.IResourceDeltaVisitor;
014: import org.eclipse.core.resources.ResourcesPlugin;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.jdt.core.IField;
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.IJavaProject;
020: import org.eclipse.jdt.core.IMember;
021: import org.eclipse.jdt.core.IMethod;
022: import org.eclipse.jdt.core.IType;
023: import org.eclipse.jdt.core.JavaCore;
024: import org.eclipse.jdt.core.JavaModelException;
025: import org.eclipse.jdt.internal.ui.JavaPlugin;
026: import org.eclipse.jdt.internal.ui.dnd.DelegatingDropAdapter;
027: import org.eclipse.jdt.internal.ui.util.SelectionUtil;
028: import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
029: import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater;
030: import org.eclipse.jdt.ui.IContextMenuConstants;
031: import org.eclipse.jface.action.Action;
032: import org.eclipse.jface.action.IMenuListener;
033: import org.eclipse.jface.action.IMenuManager;
034: import org.eclipse.jface.action.IStatusLineManager;
035: import org.eclipse.jface.action.IToolBarManager;
036: import org.eclipse.jface.util.TransferDropTargetListener;
037: import org.eclipse.jface.viewers.DoubleClickEvent;
038: import org.eclipse.jface.viewers.IDoubleClickListener;
039: import org.eclipse.jface.viewers.ISelection;
040: import org.eclipse.jface.viewers.ISelectionChangedListener;
041: import org.eclipse.jface.viewers.SelectionChangedEvent;
042: import org.eclipse.jface.viewers.StructuredSelection;
043: import org.eclipse.jface.viewers.StructuredViewer;
044: import org.eclipse.swt.SWT;
045: import org.eclipse.swt.dnd.DND;
046: import org.eclipse.swt.dnd.DropTarget;
047: import org.eclipse.swt.dnd.Transfer;
048: import org.eclipse.swt.events.KeyAdapter;
049: import org.eclipse.swt.events.KeyEvent;
050: import org.eclipse.swt.events.KeyListener;
051: import org.eclipse.swt.widgets.Composite;
052: import org.eclipse.swt.widgets.Display;
053: import org.eclipse.ui.IActionBars;
054: import org.eclipse.ui.IPartListener;
055: import org.eclipse.ui.IWorkbenchPart;
056: import org.eclipse.ui.IWorkbenchWindow;
057: import org.eclipse.ui.PlatformUI;
058: import org.eclipse.ui.actions.ActionContext;
059: import org.eclipse.ui.actions.ActionFactory;
060: import org.eclipse.ui.part.ViewPart;
061: import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
062: import org.terracotta.dso.ConfigurationAdapter;
063: import org.terracotta.dso.ConfigurationHelper;
064: import org.terracotta.dso.JdtUtils;
065: import org.terracotta.dso.MultiChangeSignaller;
066: import org.terracotta.dso.TcPlugin;
067: import org.terracotta.dso.actions.ActionUtil;
068:
069: import com.terracottatech.config.QualifiedClassName;
070: import com.terracottatech.config.QualifiedFieldName;
071: import com.terracottatech.config.TcConfigDocument.TcConfig;
072:
073: public class ConfigViewPart extends ViewPart implements
074: ISelectionChangedListener, IPartListener, IMenuListener,
075: IDoubleClickListener, IResourceChangeListener,
076: IResourceDeltaVisitor {
077: public static final String ID_CONFIG_VIEW_PART = "org.terracotta.dso.ui.views.configView";
078: private static TcPlugin fPlugin = TcPlugin.getDefault();
079: ConfigViewer fConfigViewer;
080: ConfigRefreshAction fRefreshAction;
081: DeleteAction fDeleteAction;
082: IncludeActionGroup fIncludeActionGroup;
083: LockActionGroup fLockActionGroup;
084: private ConfigSelectionProvider fSelectionProviderMediator;
085: IJavaProject m_javaProject;
086: private ConfigAdapter m_configAdapter;
087:
088: private static String REFRESH = ActionFactory.REFRESH.getId();
089: private static String DELETE = ActionFactory.DELETE.getId();
090:
091: public ConfigViewPart() {
092: super ();
093: fPlugin
094: .addConfigurationListener(m_configAdapter = new ConfigAdapter());
095: }
096:
097: public void createPartControl(Composite parent) {
098: createViewer(parent);
099: initDragAndDrop(parent);
100:
101: fSelectionProviderMediator = new ConfigSelectionProvider(
102: new StructuredViewer[] { fConfigViewer });
103: IStatusLineManager slManager = getViewSite().getActionBars()
104: .getStatusLineManager();
105: fSelectionProviderMediator
106: .addSelectionChangedListener(new StatusBarUpdater(
107: slManager));
108: getSite().setSelectionProvider(fSelectionProviderMediator);
109:
110: fConfigViewer.initContextMenu(this , getSite(),
111: fSelectionProviderMediator);
112: fConfigViewer.addDoubleClickListener(this );
113:
114: makeActions();
115: fillViewMenu();
116: getSite().getPage().addPartListener(this );
117: ResourcesPlugin.getWorkspace().addResourceChangeListener(this );
118: }
119:
120: public void menuAboutToShow(IMenuManager menu) {
121: fillConfigViewerContextMenu(menu);
122: }
123:
124: private void createViewer(Composite parent) {
125: fConfigViewer = new ConfigViewer(parent, this );
126:
127: fConfigViewer.addKeyListener(createKeyListener());
128: fConfigViewer.addSelectionChangedListener(this );
129: }
130:
131: private void initDragAndDrop(Composite parent) {
132: addDropAdapters(fConfigViewer);
133:
134: DropTarget dropTarget = new DropTarget(parent, DND.DROP_LINK
135: | DND.DROP_DEFAULT);
136: dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer
137: .getInstance() });
138: dropTarget.addDropListener(new ConfigTransferDropAdapter(this ,
139: fConfigViewer));
140: }
141:
142: private void addDropAdapters(StructuredViewer viewer) {
143: Transfer[] transfers = new Transfer[] { LocalSelectionTransfer
144: .getInstance() };
145: int ops = DND.DROP_LINK | DND.DROP_DEFAULT;
146:
147: TransferDropTargetListener[] dropListeners = new TransferDropTargetListener[] { new ConfigTransferDropAdapter(
148: this , viewer) };
149: viewer.addDropSupport(ops, transfers,
150: new DelegatingDropAdapter(dropListeners));
151: }
152:
153: protected void fillConfigViewerContextMenu(IMenuManager menu) {
154: JavaPlugin.createStandardGroups(menu);
155:
156: menu.appendToGroup(IContextMenuConstants.GROUP_SHOW,
157: fRefreshAction);
158: if (fDeleteAction.canActionBeAdded()) {
159: menu.appendToGroup(IContextMenuConstants.GROUP_SHOW,
160: fDeleteAction);
161: }
162:
163: fIncludeActionGroup
164: .setContext(new ActionContext(getSelection()));
165: fIncludeActionGroup.fillContextMenu(menu);
166: fIncludeActionGroup.setContext(null);
167:
168: fLockActionGroup.setContext(new ActionContext(getSelection()));
169: fLockActionGroup.fillContextMenu(menu);
170: fLockActionGroup.setContext(null);
171: }
172:
173: public void setConfig(final TcConfig config) {
174: getSite().getShell().getDisplay().asyncExec(new Runnable() {
175: public void run() {
176: String configPath = "";
177:
178: if (config == null) {
179: m_javaProject = null;
180: } else if (m_javaProject != null) {
181: IProject project = m_javaProject.getProject();
182: IFile configFile = TcPlugin.getDefault()
183: .getConfigurationFile(project);
184:
185: if (configFile != null) {
186: configPath = project.getName()
187: + IPath.SEPARATOR
188: + configFile.getProjectRelativePath()
189: .toOSString();
190: }
191: }
192:
193: setTitleToolTip(configPath);
194: setContentDescription(configPath);
195:
196: fConfigViewer.setConfig(config);
197: fRefreshAction.setEnabled(config != null);
198: fDeleteAction.setEnabled(config != null
199: && fDeleteAction.canActionBeAdded());
200: }
201: });
202: }
203:
204: private void updateView() {
205: fConfigViewer.refresh();
206: fConfigViewer.expandAll();
207: }
208:
209: private KeyListener createKeyListener() {
210: KeyListener keyListener = new KeyAdapter() {
211: public void keyReleased(KeyEvent event) {
212: handleKeyEvent(event);
213: }
214: };
215:
216: return keyListener;
217: }
218:
219: protected void handleKeyEvent(KeyEvent event) {
220: if (event.stateMask == 0) {
221: if (event.keyCode == SWT.F5) {
222: if ((fRefreshAction != null)
223: && fRefreshAction.isEnabled()) {
224: fRefreshAction.run();
225: return;
226: }
227: } else if (event.keyCode == Action.findKeyCode("DELETE")) {
228: if ((fDeleteAction != null)
229: && fDeleteAction.isEnabled()) {
230: fDeleteAction.run();
231: return;
232: }
233: }
234:
235: }
236: }
237:
238: private void makeActions() {
239: fRefreshAction = new ConfigRefreshAction(this );
240: fDeleteAction = new DeleteAction(this );
241: fIncludeActionGroup = new IncludeActionGroup(this );
242: fLockActionGroup = new LockActionGroup(this );
243: }
244:
245: private void fillViewMenu() {
246: IActionBars actionBars = getViewSite().getActionBars();
247: IMenuManager viewMenu = actionBars.getMenuManager();
248: IToolBarManager toolBar = actionBars.getToolBarManager();
249:
250: actionBars.setGlobalActionHandler(REFRESH, fRefreshAction);
251: actionBars.setGlobalActionHandler(DELETE, fDeleteAction);
252:
253: toolBar.add(fRefreshAction);
254: toolBar.add(fDeleteAction);
255:
256: viewMenu.add(fRefreshAction);
257: viewMenu.add(fDeleteAction);
258:
259: fIncludeActionGroup.fillActionBars(actionBars);
260: fLockActionGroup.fillActionBars(actionBars);
261: }
262:
263: public void refresh() {
264: updateView();
265: }
266:
267: void removeSelectedItem() {
268: ISelection sel = getSelection();
269:
270: if (!sel.isEmpty()) {
271: if (sel instanceof StructuredSelection) {
272: IProject project = m_javaProject.getProject();
273: StructuredSelection ss = (StructuredSelection) sel;
274: Object[] objects = ss.toArray();
275: MultiChangeSignaller signaller = new MultiChangeSignaller();
276:
277: for (int i = objects.length - 1; i >= 0; i--) {
278: Object obj = objects[i];
279:
280: if (obj instanceof RootWrapper) {
281: RootWrapper wrapper = (RootWrapper) obj;
282: wrapper.remove();
283: signaller.rootsChanged = true;
284: } else if (obj instanceof LockWrapper) {
285: LockWrapper wrapper = (LockWrapper) obj;
286: wrapper.remove();
287: if (wrapper instanceof NamedLockWrapper) {
288: signaller.namedLocksChanged = true;
289: } else {
290: signaller.autolocksChanged = true;
291: }
292: } else if (obj instanceof BootClassWrapper) {
293: BootClassWrapper wrapper = (BootClassWrapper) obj;
294: wrapper.remove();
295: signaller.bootClassesChanged = true;
296: } else if (obj instanceof TransientFieldWrapper) {
297: TransientFieldWrapper wrapper = (TransientFieldWrapper) obj;
298: wrapper.remove();
299: signaller.transientFieldsChanged = true;
300: } else if (obj instanceof DistributedMethodWrapper) {
301: DistributedMethodWrapper wrapper = (DistributedMethodWrapper) obj;
302: wrapper.remove();
303: signaller.distributedMethodsChanged = true;
304: } else if (obj instanceof IncludeWrapper) {
305: IncludeWrapper wrapper = (IncludeWrapper) obj;
306: wrapper.remove();
307: signaller.includeRulesChanged = true;
308: } else if (obj instanceof ExcludeWrapper) {
309: ExcludeWrapper wrapper = (ExcludeWrapper) obj;
310: wrapper.remove();
311: signaller.excludeRulesChanged = true;
312: }
313: }
314: if (fPlugin.getConfigurationEditor(project) == null) {
315: fPlugin
316: .removeConfigurationListener(m_configAdapter);
317: fPlugin.saveConfiguration(project);
318: fPlugin.addConfigurationListener(m_configAdapter);
319: }
320: signaller.signal(project);
321: }
322: }
323: }
324:
325: void addRoot(IField field) {
326: addRoots(new IField[] { field });
327: }
328:
329: void addRoots(IField[] fields) {
330: if (m_javaProject != null) {
331: IProject project = m_javaProject.getProject();
332: ConfigurationHelper configHelper = fPlugin
333: .getConfigurationHelper(project);
334: MultiChangeSignaller signaller = new MultiChangeSignaller();
335:
336: for (int i = 0; i < fields.length; i++) {
337: configHelper.ensureRoot(fields[i], signaller);
338: }
339: signaller.signal(project);
340: }
341: }
342:
343: void addInclude(IJavaElement element) {
344: if (m_javaProject != null) {
345: IProject project = m_javaProject.getProject();
346: ConfigurationHelper configHelper = fPlugin
347: .getConfigurationHelper(project);
348: configHelper.ensureAdaptable(element);
349: }
350: }
351:
352: void addIncludes(IJavaElement[] elements) {
353: if (m_javaProject != null) {
354: IProject project = m_javaProject.getProject();
355: ConfigurationHelper configHelper = fPlugin
356: .getConfigurationHelper(project);
357:
358: for (int i = 0; i < elements.length; i++) {
359: configHelper.ensureAdaptable(elements[i]);
360: }
361: }
362: }
363:
364: void setIncludeExpression(String classExpression) {
365: ISelection selection = getSelection();
366: Object element = SelectionUtil.getSingleElement(selection);
367:
368: if (element instanceof IncludeWrapper) {
369: IncludeWrapper wrapper = (IncludeWrapper) element;
370: wrapper.setClassExpression(classExpression);
371:
372: IProject project = m_javaProject.getProject();
373: fPlugin.removeConfigurationListener(m_configAdapter);
374: fPlugin.fireIncludeRuleChanged(project, wrapper.getIndex());
375: if (fPlugin.getConfigurationEditor(project) == null) {
376: fPlugin.saveConfiguration(project);
377: }
378: fPlugin.addConfigurationListener(m_configAdapter);
379:
380: fConfigViewer.update(element, null);
381: }
382: }
383:
384: void setHonorTransient(boolean honor) {
385: ISelection selection = getSelection();
386: Object element = SelectionUtil.getSingleElement(selection);
387:
388: if (element instanceof IncludeWrapper) {
389: IncludeWrapper wrapper = (IncludeWrapper) element;
390: wrapper.setHonorTransient(honor);
391:
392: IProject project = m_javaProject.getProject();
393: fPlugin.removeConfigurationListener(m_configAdapter);
394: fPlugin.fireIncludeRuleChanged(project, wrapper.getIndex());
395: if (fPlugin.getConfigurationEditor(project) == null) {
396: fPlugin.saveConfiguration(project);
397: }
398: fPlugin.addConfigurationListener(m_configAdapter);
399: }
400: }
401:
402: void setOnLoad(OnLoadAction action, String handler) {
403: ISelection selection = getSelection();
404: Object element = SelectionUtil.getSingleElement(selection);
405:
406: if (element instanceof IncludeWrapper) {
407: IncludeWrapper wrapper = (IncludeWrapper) element;
408:
409: if (action.isNoop()) {
410: wrapper.unsetOnLoad();
411: } else if (action.isExecute()) {
412: wrapper.setOnLoadExecute(handler);
413: } else if (action.isMethod()) {
414: wrapper.setOnLoadMethod(handler);
415: }
416:
417: IProject project = m_javaProject.getProject();
418: fPlugin.removeConfigurationListener(m_configAdapter);
419: fPlugin.fireIncludeRuleChanged(project, wrapper.getIndex());
420: if (fPlugin.getConfigurationEditor(project) == null) {
421: fPlugin.saveConfiguration(project);
422: }
423: fPlugin.addConfigurationListener(m_configAdapter);
424: }
425: }
426:
427: void addExclude(IJavaElement element) {
428: if (m_javaProject != null) {
429: IProject project = m_javaProject.getProject();
430: ConfigurationHelper configHelper = fPlugin
431: .getConfigurationHelper(project);
432: configHelper.ensureExcluded(element);
433: }
434: }
435:
436: void addExcludes(IJavaElement[] elements) {
437: if (m_javaProject != null) {
438: IProject project = m_javaProject.getProject();
439: ConfigurationHelper configHelper = fPlugin
440: .getConfigurationHelper(project);
441:
442: for (int i = 0; i < elements.length; i++) {
443: configHelper.ensureExcluded(elements[i]);
444: }
445: }
446: }
447:
448: void setLockExpression(String classExpression) {
449: ISelection selection = getSelection();
450: Object element = SelectionUtil.getSingleElement(selection);
451:
452: if (element instanceof LockWrapper) {
453: LockWrapper wrapper = (LockWrapper) element;
454: wrapper.setMethodExpression(classExpression);
455:
456: IProject project = m_javaProject.getProject();
457: fPlugin.removeConfigurationListener(m_configAdapter);
458: if (wrapper instanceof NamedLockWrapper) {
459: fPlugin.fireNamedLockChanged(project, wrapper
460: .getIndex());
461: } else {
462: fPlugin
463: .fireAutolockChanged(project, wrapper
464: .getIndex());
465: }
466: if (fPlugin.getConfigurationEditor(project) == null) {
467: fPlugin.saveConfiguration(project);
468: }
469: fPlugin.addConfigurationListener(m_configAdapter);
470:
471: fConfigViewer.update(element, null);
472: }
473: }
474:
475: void setLockLevel(LockLevelAction action) {
476: ISelection selection = getSelection();
477: Object element = SelectionUtil.getSingleElement(selection);
478:
479: if (element instanceof LockWrapper) {
480: LockWrapper wrapper = (LockWrapper) element;
481:
482: wrapper.setLevel(action.getLevel());
483:
484: IProject project = m_javaProject.getProject();
485: fPlugin.removeConfigurationListener(m_configAdapter);
486: if (wrapper instanceof NamedLockWrapper) {
487: fPlugin.fireNamedLockChanged(project, wrapper
488: .getIndex());
489: } else {
490: fPlugin
491: .fireAutolockChanged(project, wrapper
492: .getIndex());
493: }
494: if (fPlugin.getConfigurationEditor(project) == null) {
495: fPlugin.saveConfiguration(project);
496: }
497: fPlugin.addConfigurationListener(m_configAdapter);
498: }
499: }
500:
501: void setAutoSynchronized(boolean autoSynchronized) {
502: ISelection selection = getSelection();
503: Object element = SelectionUtil.getSingleElement(selection);
504:
505: if (element instanceof AutolockWrapper) {
506: AutolockWrapper wrapper = (AutolockWrapper) element;
507:
508: if (autoSynchronized)
509: wrapper.setAutoSynchronized(autoSynchronized);
510: else
511: wrapper.unsetAutoSynchronized();
512:
513: IProject project = m_javaProject.getProject();
514: fPlugin.removeConfigurationListener(m_configAdapter);
515: fPlugin.fireAutolockChanged(project, wrapper.getIndex());
516:
517: if (fPlugin.getConfigurationEditor(project) == null) {
518: fPlugin.saveConfiguration(project);
519: }
520: fPlugin.addConfigurationListener(m_configAdapter);
521: }
522: }
523:
524: void addTransientField(IField field) {
525: addTransientFields(new IField[] { field });
526: }
527:
528: void addTransientFields(IField[] fields) {
529: if (m_javaProject != null) {
530: IProject project = m_javaProject.getProject();
531: ConfigurationHelper configHelper = fPlugin
532: .getConfigurationHelper(project);
533: MultiChangeSignaller signaller = new MultiChangeSignaller();
534:
535: for (int i = 0; i < fields.length; i++) {
536: configHelper.ensureTransient(fields[i], signaller);
537: }
538: signaller.signal(project);
539: }
540: }
541:
542: void addDistributedMethod(IMethod method) {
543: addDistributedMethods(new IMethod[] { method });
544: }
545:
546: void addDistributedMethods(IMethod[] methods) {
547: if (m_javaProject != null) {
548: IProject project = m_javaProject.getProject();
549: ConfigurationHelper configHelper = fPlugin
550: .getConfigurationHelper(project);
551: MultiChangeSignaller signaller = new MultiChangeSignaller();
552:
553: for (int i = 0; i < methods.length; i++) {
554: configHelper.ensureDistributedMethod(methods[i],
555: signaller);
556: }
557: signaller.signal(project);
558: }
559: }
560:
561: void addAdditionalBootJarClass(IType type) {
562: addAdditionalBootJarClasses(new IType[] { type });
563: }
564:
565: void addAdditionalBootJarClasses(IType[] types) {
566: if (m_javaProject != null) {
567: IProject project = m_javaProject.getProject();
568: ConfigurationHelper configHelper = fPlugin
569: .getConfigurationHelper(project);
570: MultiChangeSignaller signaller = new MultiChangeSignaller();
571:
572: for (int i = 0; i < types.length; i++) {
573: configHelper.ensureBootJarClass(types[i], signaller);
574: }
575: signaller.signal(project);
576: }
577: }
578:
579: void addAutolock(IJavaElement element) {
580: addAutolocks(new IJavaElement[] { element });
581: }
582:
583: void addAutolocks(IJavaElement[] elements) {
584: if (m_javaProject != null) {
585: IProject project = m_javaProject.getProject();
586: ConfigurationHelper configHelper = fPlugin
587: .getConfigurationHelper(project);
588: MultiChangeSignaller signaller = new MultiChangeSignaller();
589:
590: for (int i = 0; i < elements.length; i++) {
591: configHelper.ensureAutolocked(elements[i], signaller);
592: }
593: signaller.signal(project);
594: }
595: }
596:
597: void addNamedLock(IJavaElement element) {
598: addNamedLocks(new IJavaElement[] { element });
599: }
600:
601: void addNamedLocks(IJavaElement[] elements) {
602: if (m_javaProject != null) {
603: IProject project = m_javaProject.getProject();
604: ConfigurationHelper configHelper = fPlugin
605: .getConfigurationHelper(project);
606: MultiChangeSignaller signaller = new MultiChangeSignaller();
607:
608: for (int i = 0; i < elements.length; i++) {
609: configHelper.ensureNameLocked(elements[i], signaller);
610: }
611: signaller.signal(project);
612: }
613: }
614:
615: public void setFocus() {
616: fConfigViewer.setFocus();
617: }
618:
619: IType getType(String typeName) {
620: if (m_javaProject != null) {
621: try {
622: return JdtUtils.findType(m_javaProject, typeName);
623: } catch (JavaModelException jme) {/**/
624: }
625: }
626: return null;
627: }
628:
629: IField getField(String fieldName) {
630: if (fieldName != null && m_javaProject != null) {
631: IProject project = m_javaProject.getProject();
632: ConfigurationHelper configHelper = fPlugin
633: .getConfigurationHelper(project);
634: return configHelper.getField(fieldName);
635: }
636:
637: return null;
638: }
639:
640: public void selectionChanged(SelectionChangedEvent event) {
641: fDeleteAction.setEnabled(fDeleteAction.canActionBeAdded());
642: }
643:
644: public void dispose() {
645: ResourcesPlugin.getWorkspace().removeResourceChangeListener(
646: this );
647: if (fIncludeActionGroup != null) {
648: fIncludeActionGroup.dispose();
649: }
650: if (fLockActionGroup != null) {
651: fLockActionGroup.dispose();
652: }
653: fPlugin.removeConfigurationListener(m_configAdapter);
654: getViewSite().getPage().removePartListener(this );
655: super .dispose();
656: }
657:
658: public TcConfig getConfig() {
659: if (m_javaProject != null) {
660: return fPlugin.getConfiguration(m_javaProject.getProject());
661: }
662: return null;
663: }
664:
665: protected ISelection getSelection() {
666: StructuredViewer viewerInFocus = fSelectionProviderMediator
667: .getViewerInFocus();
668: if (viewerInFocus != null) {
669: return viewerInFocus.getSelection();
670: }
671: return StructuredSelection.EMPTY;
672: }
673:
674: private static class ConfigSelectionProvider extends
675: SelectionProviderMediator {
676: public ConfigSelectionProvider(StructuredViewer[] viewers) {
677: super (viewers, null);
678: }
679:
680: public ISelection getSelection() {
681: ISelection selection = super .getSelection();
682: if (false && !selection.isEmpty()) {
683: return ConfigUI.convertSelection(selection);
684: }
685: return selection;
686: }
687: }
688:
689: private void initFromJavaProject(IJavaProject javaProject) {
690: if (javaProject == null || !javaProject.equals(m_javaProject)) {
691: TcConfig config = null;
692:
693: if ((m_javaProject = javaProject) != null) {
694: IProject project = javaProject.getProject();
695:
696: if (TcPlugin.getDefault().hasTerracottaNature(project)) {
697: config = fPlugin.getConfiguration(project);
698: }
699: }
700: setConfig(config);
701: }
702: }
703:
704: public void partActivated(IWorkbenchPart part) {
705: if (part != this ) {
706: IWorkbenchWindow window = part.getSite()
707: .getWorkbenchWindow();
708:
709: if (window != null) {
710: ISelection selection = window.getSelectionService()
711: .getSelection();
712:
713: if (selection != null) {
714: initFromJavaProject(ActionUtil
715: .locateSelectedJavaProject(selection));
716: } else {
717: //setConfig(null);
718: }
719: }
720: }
721: }
722:
723: public void partBroughtToTop(IWorkbenchPart part) {/**/
724: }
725:
726: public void partClosed(IWorkbenchPart part) {/**/
727: }
728:
729: public void partDeactivated(IWorkbenchPart part) {/**/
730: }
731:
732: public void partOpened(IWorkbenchPart part) {
733: if (part == this ) {
734: IWorkbenchWindow window = part.getSite()
735: .getWorkbenchWindow();
736:
737: if (window != null) {
738: ISelection selection = window.getSelectionService()
739: .getSelection();
740:
741: if (selection != null) {
742: initFromJavaProject(ActionUtil
743: .locateSelectedJavaProject(selection));
744: } else {
745: //setConfig(null);
746: }
747: }
748: }
749: }
750:
751: class ConfigAdapter extends ConfigurationAdapter {
752: public void configurationChanged(IProject project) {
753: if (TcPlugin.getDefault().hasTerracottaNature(project)) {
754: if (m_javaProject != null
755: && m_javaProject.getProject().equals(project)) {
756: m_javaProject = JavaCore.create(project);
757: setConfig(fPlugin.getConfiguration(project));
758: }
759: } else {
760: setConfig(null);
761: }
762: }
763:
764: public void rootChanged(IProject project, int index) {
765: fConfigViewer.refreshRoot(index);
766: }
767:
768: public void rootsChanged(IProject project) {
769: fConfigViewer.refreshRoots();
770: }
771:
772: public void distributedMethodsChanged(IProject project) {
773: fConfigViewer.refreshDistributedMethods();
774: }
775:
776: public void distributedMethodChanged(IProject project, int index) {
777: fConfigViewer.refreshDistributedMethod(index);
778: }
779:
780: public void bootClassesChanged(IProject project) {
781: fConfigViewer.refreshBootClasses();
782: }
783:
784: public void bootClassChanged(IProject project, int index) {
785: fConfigViewer.refreshBootClass(index);
786: }
787:
788: public void transientFieldsChanged(IProject project) {
789: fConfigViewer.refreshTransientFields();
790: }
791:
792: public void transientFieldChanged(IProject project, int index) {
793: fConfigViewer.refreshTransientField(index);
794: }
795:
796: public void namedLockChanged(IProject project, int index) {
797: fConfigViewer.refreshNamedLock(index);
798: }
799:
800: public void namedLocksChanged(IProject project) {
801: fConfigViewer.refreshNamedLocks();
802: }
803:
804: public void autolockChanged(IProject project, int index) {
805: fConfigViewer.refreshAutolock(index);
806: }
807:
808: public void autolocksChanged(IProject project) {
809: fConfigViewer.refreshAutolocks();
810: }
811:
812: public void includeRuleChanged(IProject project, int index) {
813: fConfigViewer.refreshIncludeRule(index);
814: }
815:
816: public void includeRulesChanged(IProject project) {
817: fConfigViewer.refreshInstrumentationRules();
818: }
819:
820: public void excludeRuleChanged(IProject project, int index) {
821: fConfigViewer.refreshExcludeRule(index);
822: }
823:
824: public void excludeRulesChanged(IProject project) {
825: fConfigViewer.refreshInstrumentationRules();
826: }
827:
828: public void instrumentationRulesChanged(IProject project) {
829: fConfigViewer.refreshInstrumentationRules();
830: }
831: }
832:
833: public void doubleClick(DoubleClickEvent event) {
834: ISelection sel = event.getSelection();
835:
836: if (!sel.isEmpty()) {
837: if (sel instanceof StructuredSelection) {
838: StructuredSelection ss = (StructuredSelection) sel;
839:
840: if (ss.size() == 1) {
841: Object obj = ss.getFirstElement();
842: IMember member = null;
843:
844: if (obj instanceof RootWrapper) {
845: member = getField(((RootWrapper) obj)
846: .getFieldName());
847: } else if (obj instanceof QualifiedFieldName) {
848: member = getField(((QualifiedFieldName) obj)
849: .getStringValue());
850: } else if (obj instanceof QualifiedClassName) {
851: member = getType(((QualifiedClassName) obj)
852: .getStringValue());
853: } else if (obj instanceof BootClassWrapper) {
854: member = getType(((BootClassWrapper) obj)
855: .getClassName());
856: } else if (obj instanceof TransientFieldWrapper) {
857: member = getField(((TransientFieldWrapper) obj)
858: .getFieldName());
859: }
860:
861: if (member != null) {
862: ConfigUI.jumpToMember(member);
863: }
864: }
865: }
866: }
867: }
868:
869: public boolean visit(IResourceDelta delta) {
870: if (fConfigViewer == null
871: || fConfigViewer.getTree().isDisposed()
872: || PlatformUI.getWorkbench().isClosing()) {
873: return false;
874: }
875:
876: final IProject project;
877: if ((project = isAffected(delta)) != null) {
878: Display.getDefault().asyncExec(new Runnable() {
879: public void run() {
880: m_javaProject = JavaCore.create(project);
881: setConfig(fPlugin.getConfiguration(project));
882: }
883: });
884: return false;
885: }
886:
887: return true;
888: }
889:
890: private IProject isAffected(IResourceDelta delta) {
891: IResource res = delta.getResource();
892: IProject project = null;
893:
894: if (res instanceof IProject) {
895: if (delta.getKind() == IResourceDelta.ADDED
896: || (delta.getFlags() & IResourceDelta.DESCRIPTION) != 0) {
897: project = (IProject) delta.getResource();
898: return project; //TcPlugin.getDefault().hasTerracottaNature(project) ? project : null;
899: }
900: }
901:
902: IResourceDelta[] children = delta.getAffectedChildren();
903: for (int i = 0; i < children.length; i++) {
904: if ((project = isAffected(children[i])) != null) {
905: return project;
906: }
907: }
908:
909: return null;
910: }
911:
912: public void resourceChanged(final IResourceChangeEvent event) {
913: switch (event.getType()) {
914: case IResourceChangeEvent.POST_CHANGE:
915: try {
916: event.getDelta().accept(this );
917: } catch (CoreException ce) {
918: ce.printStackTrace();
919: }
920: break;
921: case IResourceChangeEvent.PRE_DELETE:
922: case IResourceChangeEvent.PRE_CLOSE: {
923: setConfig(null);
924: break;
925: }
926: }
927: }
928: }
|