KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > actions > RefreshBuildFilesAction


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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.views.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import com.ibm.icu.text.MessageFormat;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18 import org.eclipse.ant.internal.ui.AntUIImages;
19 import org.eclipse.ant.internal.ui.IAntUIConstants;
20 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
21 import org.eclipse.ant.internal.ui.model.AntProjectNode;
22 import org.eclipse.ant.internal.ui.model.AntProjectNodeProxy;
23 import org.eclipse.ant.internal.ui.model.AntTargetNode;
24 import org.eclipse.ant.internal.ui.views.AntView;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.texteditor.IUpdate;
31
32 /**
33  * Action which refreshes the selected buildfiles in the Ant view
34  */

35 public class RefreshBuildFilesAction extends Action implements IUpdate {
36
37     private AntView fView;
38
39     /**
40      * Creates a new <code>RefreshBuildFilesAction</code> which will refresh buildfiles
41      * in the given Ant view.
42      * @param view the Ant view whose selection this action will use when
43      * determining which buildfiles to refresh.
44      */

45     public RefreshBuildFilesAction(AntView view) {
46         super(AntViewActionMessages.RefreshBuildFilesAction_Refresh_Buildfiles_1, AntUIImages.getImageDescriptor(IAntUIConstants.IMG_REFRESH));
47         setToolTipText(AntViewActionMessages.RefreshBuildFilesAction_Refresh_Buildfiles_1);
48         fView = view;
49         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAntUIHelpContextIds.REFRESH_BUILDFILE_ACTION);
50     }
51
52     /**
53      * Refreshes the selected buildfiles (or all buildfiles if none selected) in the Ant view
54      */

55     public void run() {
56         final Set JavaDoc projects= getSelectedProjects();
57         if (projects.isEmpty()) {
58             // If no selection, add all
59
AntProjectNode[] allProjects= fView.getProjects();
60             for (int i = 0; i < allProjects.length; i++) {
61                 projects.add(allProjects[i]);
62             }
63         }
64         final Iterator JavaDoc iter= projects.iterator();
65         if (!iter.hasNext()) {
66             return;
67         }
68         
69         try {
70             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
71                 public void run(IProgressMonitor monitor) {
72                     monitor.beginTask(AntViewActionMessages.RefreshBuildFilesAction_Refreshing_buildfiles_3, projects.size());
73                     AntProjectNodeProxy project;
74                     while (iter.hasNext()) {
75                         project= (AntProjectNodeProxy) iter.next();
76                         monitor.subTask(MessageFormat.format(AntViewActionMessages.RefreshBuildFilesAction_Refreshing__0__4, new String JavaDoc[] {project.getBuildFileName()}));
77                         project.parseBuildFile(true);
78                         monitor.worked(1);
79                     }
80                 }
81             });
82         } catch (InvocationTargetException JavaDoc e) {
83         } catch (InterruptedException JavaDoc e) {
84         }
85         fView.getViewer().refresh();
86     }
87
88     /**
89      * Returns the selected project nodes to refresh
90      *
91      * @return Set the selected <code>ProjectNode</code>s to refresh.
92      */

93     private Set JavaDoc getSelectedProjects() {
94         IStructuredSelection selection = (IStructuredSelection) fView.getViewer().getSelection();
95         HashSet JavaDoc set= new HashSet JavaDoc();
96         Iterator JavaDoc iter = selection.iterator();
97         Object JavaDoc data;
98         while (iter.hasNext()) {
99             data= iter.next();
100             if (data instanceof AntProjectNode) {
101                 set.add(data);
102             } else if (data instanceof AntTargetNode) {
103                 set.add(((AntTargetNode) data).getProjectNode());
104             }
105         }
106         return set;
107     }
108
109     /**
110      * Updates the enablement of this action based on the user's selection
111      */

112     public void update() {
113         setEnabled(fView.getProjects().length > 0);
114     }
115
116 }
117
Popular Tags