KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > AntView


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Roscoe Rush - Concept and prototype implementation
10  * IBM Corporation - current implementation
11  *******************************************************************************/

12
13 package org.eclipse.ant.internal.ui.views;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
21 import org.eclipse.ant.internal.ui.model.AntElementNode;
22 import org.eclipse.ant.internal.ui.model.AntModelLabelProvider;
23 import org.eclipse.ant.internal.ui.model.AntModelProblem;
24 import org.eclipse.ant.internal.ui.model.AntProjectNode;
25 import org.eclipse.ant.internal.ui.model.AntProjectNodeProxy;
26 import org.eclipse.ant.internal.ui.model.AntTargetNode;
27 import org.eclipse.ant.internal.ui.model.InternalTargetFilter;
28 import org.eclipse.ant.internal.ui.views.actions.AddBuildFilesAction;
29 import org.eclipse.ant.internal.ui.views.actions.AntOpenWithMenu;
30 import org.eclipse.ant.internal.ui.views.actions.FilterInternalTargetsAction;
31 import org.eclipse.ant.internal.ui.views.actions.RefreshBuildFilesAction;
32 import org.eclipse.ant.internal.ui.views.actions.RemoveAllAction;
33 import org.eclipse.ant.internal.ui.views.actions.RemoveProjectAction;
34 import org.eclipse.ant.internal.ui.views.actions.RunTargetAction;
35 import org.eclipse.ant.internal.ui.views.actions.SearchForBuildFilesAction;
36 import org.eclipse.core.resources.IFile;
37 import org.eclipse.core.resources.IResource;
38 import org.eclipse.core.resources.IResourceChangeEvent;
39 import org.eclipse.core.resources.IResourceChangeListener;
40 import org.eclipse.core.resources.IResourceDelta;
41 import org.eclipse.core.resources.ResourcesPlugin;
42 import org.eclipse.core.runtime.IPath;
43 import org.eclipse.core.runtime.Path;
44 import org.eclipse.jface.action.IMenuListener;
45 import org.eclipse.jface.action.IMenuManager;
46 import org.eclipse.jface.action.IToolBarManager;
47 import org.eclipse.jface.action.MenuManager;
48 import org.eclipse.jface.action.Separator;
49 import org.eclipse.jface.viewers.DoubleClickEvent;
50 import org.eclipse.jface.viewers.IDoubleClickListener;
51 import org.eclipse.jface.viewers.ISelection;
52 import org.eclipse.jface.viewers.ISelectionChangedListener;
53 import org.eclipse.jface.viewers.IStructuredSelection;
54 import org.eclipse.jface.viewers.SelectionChangedEvent;
55 import org.eclipse.jface.viewers.StructuredSelection;
56 import org.eclipse.jface.viewers.TreeViewer;
57 import org.eclipse.jface.viewers.Viewer;
58 import org.eclipse.jface.viewers.ViewerComparator;
59 import org.eclipse.jface.viewers.ViewerFilter;
60 import org.eclipse.jface.viewers.ViewerSorter;
61 import org.eclipse.swt.SWT;
62 import org.eclipse.swt.dnd.DND;
63 import org.eclipse.swt.dnd.FileTransfer;
64 import org.eclipse.swt.dnd.Transfer;
65 import org.eclipse.swt.events.KeyAdapter;
66 import org.eclipse.swt.events.KeyEvent;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Control;
69 import org.eclipse.swt.widgets.Display;
70 import org.eclipse.swt.widgets.Menu;
71 import org.eclipse.ui.IMemento;
72 import org.eclipse.ui.IViewSite;
73 import org.eclipse.ui.IWorkbenchActionConstants;
74 import org.eclipse.ui.PartInitException;
75 import org.eclipse.ui.PlatformUI;
76 import org.eclipse.ui.XMLMemento;
77 import org.eclipse.ui.part.IShowInSource;
78 import org.eclipse.ui.part.ShowInContext;
79 import org.eclipse.ui.part.ViewPart;
80 import org.eclipse.ui.texteditor.IUpdate;
81
82 /**
83  * A view which displays a hierarchical view of ant build files and allows the
84  * user to run selected targets from those files.
85  */

86 public class AntView extends ViewPart implements IResourceChangeListener, IShowInSource {
87     
88     private Object JavaDoc[] restoredViewerInput= null;
89     private boolean filterInternalTargets= false;
90     private InternalTargetFilter fInternalTargetFilter= null;
91     /**
92      * This memento allows the Ant view to save and restore state
93      * when it is closed and opened within a session. A different
94      * memento is supplied by the platform for persistence at
95      * workbench shutdown.
96      */

97     private static IMemento fgTempMemento = null;
98
99     /**
100      * XML tag used to identify an ant project in storage
101      */

102     private static final String JavaDoc TAG_PROJECT = "project"; //$NON-NLS-1$
103
/**
104      * XML key used to store whether or not an Ant project is an error node.
105      * Persisting this data saved a huge amount of processing at startup.
106      */

107     private static final String JavaDoc KEY_ERROR = "error"; //$NON-NLS-1$
108
/**
109      * XML key used to store whether or not an Ant project is an error node.
110      * Persisting this data saved a huge amount of processing at startup.
111      */

112     private static final String JavaDoc KEY_WARNING = "warning"; //$NON-NLS-1$
113
/**
114      * XML key used to store an ant project's path
115      */

116     private static final String JavaDoc KEY_PATH = "path"; //$NON-NLS-1$
117
/**
118      * XML key used to store an ant node's name
119      */

120     private static final String JavaDoc KEY_NAME = "name"; //$NON-NLS-1$
121
/**
122      * XML key used to store an ant project's default target name
123      */

124     private static final String JavaDoc KEY_DEFAULT = "default"; //$NON-NLS-1$
125
/**
126      * XML tag used to identify the "filter internal targets" preference.
127      */

128     private static final String JavaDoc TAG_FILTER_INTERNAL_TARGETS = "filterInternalTargets"; //$NON-NLS-1$
129
/**
130      * XML key used to store the value of the "filter internal targets" preference.
131      */

132     private static final String JavaDoc KEY_VALUE = "value"; //$NON-NLS-1$
133

134     /**
135      * The tree viewer that displays the users ant projects
136      */

137     private TreeViewer projectViewer;
138     private AntViewContentProvider contentProvider;
139
140     /**
141      * Collection of <code>IUpdate</code> actions that need to update on
142      * selection changed in the project viewer.
143      */

144     private List JavaDoc updateProjectActions;
145     // Ant View Actions
146
private AddBuildFilesAction addBuildFileAction;
147     private SearchForBuildFilesAction searchForBuildFilesAction;
148     private RefreshBuildFilesAction refreshBuildFilesAction;
149     private RemoveProjectAction removeProjectAction;
150     private RemoveAllAction removeAllAction;
151     private FilterInternalTargetsAction filterInternalTargetsAction;
152     private RunTargetAction runTargetAction;
153     // Context-menu-only actions
154
private AntOpenWithMenu openWithMenu;
155
156     /**
157      * The given build file has changed. Refresh the view to pick up any
158      * structural changes.
159      */

160     private void handleBuildFileChanged(AntProjectNode project) {
161         ((AntProjectNodeProxy)project).parseBuildFile(true);
162         Display.getDefault().asyncExec(new Runnable JavaDoc() {
163             public void run() {
164                 //must do a full refresh to re-sort
165
projectViewer.refresh();
166                 //update the status line
167
handleSelectionChanged((IStructuredSelection) projectViewer.getSelection());
168             }
169         });
170     }
171
172     /* (non-Javadoc)
173      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
174      */

175     public void createPartControl(Composite parent) {
176         initializeActions();
177         createProjectViewer(parent);
178         initializeDragAndDrop();
179         fillMainToolBar();
180         if (getProjects().length > 0) {
181             // If any projects have been added to the view during startup,
182
// begin listening for resource changes
183
ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
184         }
185         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IAntUIHelpContextIds.ANT_VIEW);
186         updateProjectActions();
187     }
188     
189     private void initializeDragAndDrop() {
190         int ops = DND.DROP_COPY | DND.DROP_DEFAULT;
191         Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() };
192         TreeViewer viewer = getViewer();
193         AntViewDropAdapter adapter = new AntViewDropAdapter(this);
194         viewer.addDropSupport(ops, transfers, adapter);
195     }
196
197     /**
198      * Creates a pop-up menu on the given control
199      *
200      * @param menuControl the control with which the pop-up
201      * menu will be associated
202      */

203     private void createContextMenu(Viewer viewer) {
204         Control menuControl = viewer.getControl();
205         MenuManager menuMgr = new MenuManager("#PopUp"); //$NON-NLS-1$
206
menuMgr.setRemoveAllWhenShown(true);
207         menuMgr.addMenuListener(new IMenuListener() {
208             public void menuAboutToShow(IMenuManager mgr) {
209                 fillContextMenu(mgr);
210             }
211         });
212         Menu menu = menuMgr.createContextMenu(menuControl);
213         menuControl.setMenu(menu);
214
215         // register the context menu such that other plugins may contribute to it
216
getSite().registerContextMenu(menuMgr, viewer);
217     }
218
219     /**
220      * Adds actions to the context menu
221      *
222      * @param viewer the viewer who's menu we're configuring
223      * @param menu The menu to contribute to
224      */

225     private void fillContextMenu(IMenuManager menu) {
226         addOpenWithMenu(menu);
227         menu.add(new Separator());
228         menu.add(addBuildFileAction);
229         menu.add(removeProjectAction);
230         menu.add(removeAllAction);
231         menu.add(refreshBuildFilesAction);
232         
233         menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
234     }
235
236     private void addOpenWithMenu(IMenuManager menu) {
237         AntElementNode node= getSelectionNode();
238         if (node != null) {
239             IFile buildFile= node.getIFile();
240             if (buildFile != null) {
241                 menu.add(new Separator("group.open")); //$NON-NLS-1$
242
IMenuManager submenu= new MenuManager(AntViewMessages.AntView_1);
243                 openWithMenu.setNode(node);
244                 submenu.add(openWithMenu);
245                 menu.appendToGroup("group.open", submenu); //$NON-NLS-1$
246
}
247         }
248     }
249
250     /**
251      * Initialize the actions for this view
252      */

253     private void initializeActions() {
254         updateProjectActions= new ArrayList JavaDoc(5);
255         
256         addBuildFileAction = new AddBuildFilesAction(this);
257         
258         removeProjectAction = new RemoveProjectAction(this);
259         updateProjectActions.add(removeProjectAction);
260         
261         removeAllAction = new RemoveAllAction(this);
262         updateProjectActions.add(removeAllAction);
263         
264         runTargetAction = new RunTargetAction(this);
265         updateProjectActions.add(runTargetAction);
266         
267         searchForBuildFilesAction = new SearchForBuildFilesAction(this);
268         
269         refreshBuildFilesAction = new RefreshBuildFilesAction(this);
270         updateProjectActions.add(refreshBuildFilesAction);
271         
272         openWithMenu= new AntOpenWithMenu(this.getViewSite().getPage());
273         
274         filterInternalTargetsAction= new FilterInternalTargetsAction(this);
275     }
276
277     /**
278      * Updates the enabled state of all <code>IUpdate</code> actions associated
279      * with the project viewer.
280      */

281     private void updateProjectActions() {
282         Iterator JavaDoc iter = updateProjectActions.iterator();
283         while (iter.hasNext()) {
284             ((IUpdate) iter.next()).update();
285         }
286     }
287
288     /**
289      * Create the viewer which displays the Ant projects
290      */

291     private void createProjectViewer(Composite parent) {
292         projectViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
293         contentProvider = new AntViewContentProvider();
294         projectViewer.setContentProvider(contentProvider);
295         
296         filterInternalTargetsAction.setChecked(filterInternalTargets);
297         setFilterInternalTargets(filterInternalTargets);
298         
299         projectViewer.setLabelProvider(new AntModelLabelProvider());
300         if (fgTempMemento != null) {
301             restoreViewerInput(fgTempMemento);
302             fgTempMemento= null;
303         }
304         projectViewer.setInput(new Object JavaDoc[0]);
305         if (restoredViewerInput.length > 0) {
306             contentProvider.addAll(restoredViewerInput);
307         }
308         projectViewer.setComparator(new ViewerComparator() {
309             /* (non-Javadoc)
310              * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
311              */

312             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
313                 if (e1 instanceof AntProjectNode && e2 instanceof AntProjectNode
314                     || e1 instanceof AntTargetNode && e2 instanceof AntTargetNode) {
315                     return e1.toString().compareToIgnoreCase(e2.toString());
316                 }
317                 return 0;
318             }
319         });
320         
321         projectViewer.addSelectionChangedListener(new ISelectionChangedListener() {
322             public void selectionChanged(SelectionChangedEvent event) {
323                 handleSelectionChanged((IStructuredSelection)event.getSelection());
324             }
325         });
326         
327         projectViewer.addDoubleClickListener(new IDoubleClickListener() {
328             public void doubleClick(DoubleClickEvent event) {
329                 if (!event.getSelection().isEmpty()) {
330                     handleProjectViewerDoubleClick();
331                 }
332             }
333         });
334         
335         projectViewer.getControl().addKeyListener(new KeyAdapter() {
336             public void keyPressed(KeyEvent event) {
337                 handleProjectViewerKeyPress(event);
338             }
339         });
340         
341         createContextMenu(projectViewer);
342         getSite().setSelectionProvider(projectViewer);
343     }
344     
345     private void handleProjectViewerKeyPress(KeyEvent event) {
346         if (event.character == SWT.DEL && event.stateMask == 0) {
347             if (removeProjectAction.isEnabled()) {
348                 removeProjectAction.run();
349             }
350         } else if (event.keyCode == SWT.F5 && event.stateMask == 0) {
351             if (refreshBuildFilesAction.isEnabled()) {
352                 refreshBuildFilesAction.run();
353             }
354         }
355     }
356     
357     private void handleProjectViewerDoubleClick() {
358         AntElementNode node= getSelectionNode();
359         if (node != null) {
360             runTargetAction.run(node);
361         }
362     }
363
364     /**
365      * Updates the actions and status line for selection change in one of the
366      * viewers.
367      */

368     private void handleSelectionChanged(IStructuredSelection selection) {
369         updateProjectActions();
370         Iterator JavaDoc selectionIter = selection.iterator();
371         AntElementNode selected = null;
372         if (selectionIter.hasNext()) {
373             selected = (AntElementNode) selectionIter.next();
374         }
375         String JavaDoc messageString= null;
376         if (!selectionIter.hasNext()) {
377             if (selected != null) {
378                 String JavaDoc errorString= selected.getProblemMessage();
379                 if (errorString != null) {
380                     getViewSite().getActionBars().getStatusLineManager().setErrorMessage(errorString);
381                     return;
382                 }
383             }
384             getViewSite().getActionBars().getStatusLineManager().setErrorMessage(null);
385             messageString= getStatusLineText(selected);
386         }
387         getViewSite().getActionBars().getStatusLineManager().setMessage(messageString);
388     }
389     
390     /**
391      * Returns text appropriate for display in the workbench status line for the
392      * given node.
393      */

394     private String JavaDoc getStatusLineText(AntElementNode node) {
395         if (node instanceof AntProjectNode) {
396             AntProjectNode project = (AntProjectNode) node;
397             StringBuffer JavaDoc message= new StringBuffer JavaDoc(project.getBuildFileName());
398             String JavaDoc description= project.getDescription();
399             if (description != null && description.length() > 0) {
400                 message.append(": "); //$NON-NLS-1$
401
message.append(description);
402             }
403             return message.toString();
404         } else if (node instanceof AntTargetNode) {
405             AntTargetNode target = (AntTargetNode) node;
406             StringBuffer JavaDoc message= new StringBuffer JavaDoc();
407             Enumeration JavaDoc depends= target.getTarget().getDependencies();
408             if (depends.hasMoreElements()) {
409                 message.append(AntViewMessages.AntView_3);
410                 message.append((String JavaDoc)depends.nextElement()); // Unroll the loop to avoid trailing comma
411
while (depends.hasMoreElements()) {
412                     String JavaDoc dependancy = (String JavaDoc) depends.nextElement();
413                     message.append(',').append(dependancy);
414                 }
415                 message.append('\"');
416             }
417             String JavaDoc description= target.getTarget().getDescription();
418             if (description != null && description.length() != 0) {
419                 message.append(AntViewMessages.AntView_4);
420                 message.append(description);
421                 message.append('\"');
422             }
423             return message.toString();
424         }
425         return null;
426     }
427
428     /**
429      * Returns the tree viewer that displays the projects in this view
430      *
431      * @return TreeViewer this view's project viewer
432      */

433     public TreeViewer getViewer() {
434         return projectViewer;
435     }
436
437     /**
438      * Returns the <code>AntProjectNode</code>s currently displayed in this view.
439      *
440      * @return AntProjectNode[] the <code>ProjectNode</code>s currently displayed
441      * in this view
442      */

443     public AntProjectNode[] getProjects() {
444         Object JavaDoc[] objects= contentProvider.getElements(projectViewer.getInput());
445         AntProjectNode[] projects= new AntProjectNode[objects.length];
446         for (int i = 0; i < projects.length; i++) {
447             projects[i] = (AntProjectNode)objects[i];
448         }
449         return projects;
450     }
451
452     /**
453      * Adds the given project to the view
454      *
455      * @param project the project to add
456      */

457     public void addProject(AntProjectNode project) {
458         contentProvider.add(project);
459         projectViewer.refresh();
460         ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
461         handleSelectionChanged(new StructuredSelection(project));
462     }
463
464     /**
465      * Removes the given project from the view
466      *
467      * @param project the project to remove
468      */

469     private void removeProject(AntProjectNode project) {
470         removeProjectFromContentProvider(project);
471         projectViewer.refresh();
472         setProjectViewerSelectionAfterDeletion();
473     }
474
475     private void setProjectViewerSelectionAfterDeletion() {
476         Object JavaDoc[] children= getProjects();
477         if (children.length > 0) {
478             ViewerSorter sorter= projectViewer.getSorter();
479             sorter.sort(projectViewer, children);
480             IStructuredSelection selection= new StructuredSelection(children[0]);
481             projectViewer.setSelection(selection);
482             handleSelectionChanged(selection);
483         }
484     }
485
486     /**
487      * Removes the given list of <code>AntProjectNode</code> objects from the view.
488      * This method should be called whenever multiple projects are to be removed
489      * because this method optimizes the viewer refresh associated with removing
490      * multiple items.
491      *
492      * @param projectNodes the list of <code>ProjectNode</code> objects to
493      * remove
494      */

495     public void removeProjects(List JavaDoc projectNodes) {
496         Iterator JavaDoc iter = projectNodes.iterator();
497         while (iter.hasNext()) {
498             AntProjectNode project = (AntProjectNode) iter.next();
499             removeProjectFromContentProvider(project);
500         }
501         projectViewer.refresh();
502         setProjectViewerSelectionAfterDeletion();
503     }
504
505     /**
506      * Removes the given project node from the project content provider.
507      *
508      * @param project the project to remove
509      */

510     private void removeProjectFromContentProvider(AntProjectNode project) {
511         project.dispose();
512         contentProvider.remove(project);
513         if (getProjects().length == 0) {
514             ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
515         }
516     }
517
518     /**
519      * Removes all projects from the view
520      */

521     public void removeAllProjects() {
522         AntProjectNode[] projects= getProjects();
523         for (int i = 0; i < projects.length; i++) {
524             AntProjectNode node = projects[i];
525             node.dispose();
526         }
527         // Remove all projects
528
contentProvider.removeAll();
529         
530         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
531         updateProjectActions();
532     }
533
534     /* (non-Javadoc)
535      * @see org.eclipse.ui.IViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
536      */

537     public void init(IViewSite site, IMemento memento) throws PartInitException {
538         init(site);
539         restoreViewerInput(memento);
540         if (memento != null) {
541             IMemento child= memento.getChild(TAG_FILTER_INTERNAL_TARGETS);
542             if (child != null) {
543                 filterInternalTargets= Boolean.valueOf(child.getString(KEY_VALUE)).booleanValue();
544             }
545         }
546     }
547
548     /**
549      * Restore the viewer content that was persisted
550      *
551      * @param memento the memento containing the persisted viewer content
552      */

553     private void restoreViewerInput(IMemento memento) {
554         if (memento == null) {
555             restoredViewerInput= new Object JavaDoc[0];
556             return;
557         }
558         IMemento[] projects = memento.getChildren(TAG_PROJECT);
559         if (projects.length < 1) {
560             restoredViewerInput= new Object JavaDoc[0];
561             return;
562         }
563         List JavaDoc projectNodes = new ArrayList JavaDoc(projects.length);
564         for (int i = 0; i < projects.length; i++) {
565             IMemento projectMemento = projects[i];
566             String JavaDoc pathString = projectMemento.getString(KEY_PATH);
567             if (!ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(pathString)).exists()) {
568                 // If the file no longer exists, don't add it.
569
continue;
570             }
571             String JavaDoc nameString = projectMemento.getString(KEY_NAME);
572             String JavaDoc defaultTarget= projectMemento.getString(KEY_DEFAULT);
573             String JavaDoc errorString = projectMemento.getString(KEY_ERROR);
574             String JavaDoc warningString = projectMemento.getString(KEY_WARNING);
575
576             AntProjectNodeProxy project = null;
577             if (nameString == null) {
578                 nameString = ""; //$NON-NLS-1$
579
}
580             project = new AntProjectNodeProxy(nameString, pathString);
581             if (errorString != null && Boolean.valueOf(errorString).booleanValue()) {
582                 project.setProblemSeverity(AntModelProblem.SEVERITY_ERROR);
583             } else if (warningString != null && Boolean.valueOf(warningString).booleanValue()) {
584                 project.setProblemSeverity(AntModelProblem.SEVERITY_WARNING);
585             }
586             if (defaultTarget != null) {
587                 project.setDefaultTargetName(defaultTarget);
588             }
589             projectNodes.add(project);
590         }
591         restoredViewerInput= projectNodes.toArray(new AntProjectNode[projectNodes.size()]);
592     }
593
594     /* (non-Javadoc)
595      * @see org.eclipse.ui.IViewPart#saveState(org.eclipse.ui.IMemento)
596      */

597     public void saveState(IMemento memento) {
598         // Save the projects
599
AntProjectNode[] projects = getProjects();
600         AntProjectNode project;
601         IMemento projectMemento;
602         for (int i = 0; i < projects.length; i++) {
603             project= projects[i];
604             projectMemento = memento.createChild(TAG_PROJECT);
605             projectMemento.putString(KEY_PATH, project.getBuildFileName());
606             projectMemento.putString(KEY_NAME, project.getLabel());
607             String JavaDoc defaultTarget= project.getDefaultTargetName();
608             if (project.isErrorNode()) {
609                 projectMemento.putString(KEY_ERROR, String.valueOf(true));
610             } else {
611                 if (project.isWarningNode()) {
612                     projectMemento.putString(KEY_WARNING, String.valueOf(true));
613                 }
614                 if (defaultTarget != null) {
615                     projectMemento.putString(KEY_DEFAULT, defaultTarget);
616                 }
617                 projectMemento.putString(KEY_ERROR, String.valueOf(false));
618             }
619         }
620         IMemento filterTargets= memento.createChild(TAG_FILTER_INTERNAL_TARGETS);
621         filterTargets.putString(KEY_VALUE, isFilterInternalTargets() ? String.valueOf(true) : String.valueOf(false));
622     }
623
624     /* (non-Javadoc)
625      * @see org.eclipse.ui.IWorkbenchPart#dispose()
626      */

627     public void dispose() {
628         fgTempMemento= XMLMemento.createWriteRoot("AntViewMemento"); //$NON-NLS-1$
629
saveState(fgTempMemento);
630         super.dispose();
631         if (openWithMenu != null) {
632             openWithMenu.dispose();
633         }
634         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
635     }
636
637     /* (non-Javadoc)
638      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
639      */

640     public void resourceChanged(IResourceChangeEvent event) {
641         IResourceDelta delta = event.getDelta();
642         if (delta != null) {
643             AntProjectNode projects[]= getProjects();
644             IPath buildFilePath;
645             for (int i = 0; i < projects.length; i++) {
646                 buildFilePath= new Path(projects[i].getBuildFileName());
647                 IResourceDelta change= delta.findMember(buildFilePath);
648                 if (change != null) {
649                     handleChangeDelta(change, projects[i]);
650                 }
651             }
652         }
653     }
654     
655     /**
656      * Update the view for the given resource delta. The delta is a resource
657      * delta for the given build file in the view
658      *
659      * @param delta a delta for a build file in the view
660      * @param project the project node that has changed
661      */

662     private void handleChangeDelta(IResourceDelta delta, final AntProjectNode project) {
663         IResource resource= delta.getResource();
664         if (resource.getType() != IResource.FILE) {
665             return;
666         }
667         if (delta.getKind() == IResourceDelta.REMOVED) {
668             Display.getDefault().asyncExec(new Runnable JavaDoc() {
669                 public void run() {
670                     removeProject(project);
671                 }
672             });
673         } else if (delta.getKind() == IResourceDelta.CHANGED && (delta.getFlags() & IResourceDelta.CONTENT) != 0) {
674             handleBuildFileChanged(project);
675         }
676     }
677     
678     private void fillMainToolBar() {
679         IToolBarManager toolBarMgr= getViewSite().getActionBars().getToolBarManager();
680         toolBarMgr.removeAll();
681         
682         toolBarMgr.add(addBuildFileAction);
683         toolBarMgr.add(searchForBuildFilesAction);
684         toolBarMgr.add(filterInternalTargetsAction);
685
686         toolBarMgr.add(runTargetAction);
687         toolBarMgr.add(removeProjectAction);
688         toolBarMgr.add(removeAllAction);
689         
690         toolBarMgr.update(false);
691     }
692     
693     private AntElementNode getSelectionNode() {
694         IStructuredSelection selection= (IStructuredSelection)getViewer().getSelection();
695         if (selection.size() == 1) {
696             Object JavaDoc element= selection.getFirstElement();
697             if (element instanceof AntElementNode) {
698                 AntElementNode node= (AntElementNode) element;
699                 return node;
700             }
701         }
702         return null;
703     }
704     
705     /* (non-Javadoc)
706      * @see org.eclipse.ui.part.IShowInSource#getShowInContext()
707      */

708     public ShowInContext getShowInContext() {
709         AntElementNode node= getSelectionNode();
710         if (node != null) {
711             IFile buildFile= node.getIFile();
712             if (buildFile != null) {
713                 ISelection selection= new StructuredSelection(buildFile);
714                 return new ShowInContext(null, selection);
715             }
716         }
717         return null;
718     }
719
720     /**
721      * Returns whether internal targets are currently being filtered out of
722      * the view.
723      *
724      * @return whether or not internal targets are being filtered out
725      */

726     public boolean isFilterInternalTargets() {
727         return filterInternalTargets;
728     }
729
730     /**
731      * Sets whether internal targets should be filtered out of the view.
732      *
733      * @param filter whether or not internal targets should be filtered out
734      */

735     public void setFilterInternalTargets(boolean filter) {
736         filterInternalTargets= filter;
737         if (filter) {
738             projectViewer.addFilter(getInternalTargetsFilter());
739         } else {
740             projectViewer.removeFilter(getInternalTargetsFilter());
741         }
742     }
743     
744     private ViewerFilter getInternalTargetsFilter() {
745         if (fInternalTargetFilter == null) {
746             fInternalTargetFilter= new InternalTargetFilter();
747         }
748         return fInternalTargetFilter;
749     }
750
751     /* (non-Javadoc)
752      * @see org.eclipse.ui.IWorkbenchPart#setFocus()
753      */

754     public void setFocus() {
755         if (getViewer() != null) {
756             getViewer().getControl().setFocus();
757         }
758     }
759 }
760
Popular Tags