KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import com.ibm.icu.text.MessageFormat;
14 import org.eclipse.ant.internal.ui.AntUIImages;
15 import org.eclipse.ant.internal.ui.IAntUIConstants;
16 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
17 import org.eclipse.ant.internal.ui.model.AntProjectNode;
18 import org.eclipse.ant.internal.ui.model.AntProjectNodeProxy;
19 import org.eclipse.ant.internal.ui.views.AntView;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.ui.PlatformUI;
27
28 /**
29  * This action opens a dialog to search for build files and adds the resulting
30  * projects to the ant view.
31  */

32 public class SearchForBuildFilesAction extends Action {
33     private AntView view;
34     
35     public SearchForBuildFilesAction(AntView view) {
36         super(AntViewActionMessages.SearchForBuildFilesAction_Search_1, AntUIImages.getImageDescriptor(IAntUIConstants.IMG_SEARCH));
37         setToolTipText(AntViewActionMessages.SearchForBuildFilesAction_Add_build_files_with_search_2);
38         this.view= view;
39         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAntUIHelpContextIds.SEARCH_FOR_BUILDFILES_ACTION);
40     }
41     
42     /**
43      * Opens the <code>SearchForBuildFilesDialog</code> and adds the results to
44      * the ant view.
45      */

46     public void run() {
47         SearchForBuildFilesDialog dialog= new SearchForBuildFilesDialog();
48         if (dialog.open() != Window.CANCEL) {
49             final IFile[] files= dialog.getResults();
50             final boolean includeErrorNodes= dialog.getIncludeErrorResults();
51             final AntProjectNode[] existingProjects= view.getProjects();
52             try {
53                 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
54                     public void run(IProgressMonitor monitor) {
55                         monitor.beginTask(AntViewActionMessages.SearchForBuildFilesAction_Processing_search_results_3, files.length);
56                         for (int i = 0; i < files.length && !monitor.isCanceled(); i++) {
57                             String JavaDoc buildFileName= files[i].getFullPath().toString();
58                             monitor.subTask(MessageFormat.format(AntViewActionMessages.SearchForBuildFilesAction_Adding__0__4, new String JavaDoc[] {buildFileName}));
59                             if (alreadyAdded(buildFileName)) {
60                                 // Don't parse projects that have already been added.
61
continue;
62                             }
63                             final AntProjectNodeProxy project= new AntProjectNodeProxy(buildFileName);
64                             // Force the project to be parsed so the error state is set.
65
project.parseBuildFile();
66                             monitor.worked(1);
67                             if (includeErrorNodes || !(project.isErrorNode())) {
68                                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
69                                     public void run() {
70                                         view.addProject(project);
71                                     }
72                                 });
73                             }
74                         }
75                     }
76                     /**
77                      * Returns whether or not the given build file already
78                      * exists in the ant view.
79                      */

80                     private boolean alreadyAdded(String JavaDoc buildFileName) {
81                         for (int j = 0; j < existingProjects.length; j++) {
82                             AntProjectNode existingProject = existingProjects[j];
83                             if (existingProject.getBuildFileName().equals(buildFileName)) {
84                                 return true;
85                             }
86                         }
87                         return false;
88                     }
89                 });
90             } catch (InvocationTargetException JavaDoc e) {
91             } catch (InterruptedException JavaDoc e) {
92             }
93         }
94     }
95 }
96
Popular Tags