KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.ant.internal.ui.AntUIImages;
17 import org.eclipse.ant.internal.ui.AntUtil;
18 import org.eclipse.ant.internal.ui.IAntUIConstants;
19 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
20 import org.eclipse.ant.internal.ui.model.AntProjectNode;
21 import org.eclipse.ant.internal.ui.model.AntProjectNodeProxy;
22 import org.eclipse.ant.internal.ui.preferences.FileSelectionDialog;
23 import org.eclipse.ant.internal.ui.views.AntView;
24 import org.eclipse.core.resources.IFile;
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.swt.widgets.Display;
29 import org.eclipse.ui.PlatformUI;
30
31 /**
32  * Action that prompts the user for build files and adds the selected files to
33  * an <code>AntView</code>
34  */

35 public class AddBuildFilesAction extends Action {
36
37     private AntView view;
38
39     public AddBuildFilesAction(AntView view) {
40         super(AntViewActionMessages.AddBuildFilesAction_1, AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ADD));
41         this.view= view;
42         setToolTipText(AntViewActionMessages.AddBuildFilesAction_0);
43         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAntUIHelpContextIds.ADD_BUILDFILE_ACTION);
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.jface.action.IAction#run()
48      */

49     public void run() {
50         String JavaDoc title= AntViewActionMessages.AddBuildFilesAction_2;
51         String JavaDoc message= AntViewActionMessages.AddBuildFilesAction_4;
52         String JavaDoc filterExtension= "xml"; //$NON-NLS-1$
53
String JavaDoc filterMessage= AntViewActionMessages.AddBuildFilesAction_5;
54         
55         FileSelectionDialog dialog = new FileSelectionDialog(Display.getCurrent().getActiveShell(), getBuildFiles(), title, message, filterExtension, filterMessage);
56         dialog.open();
57         final Object JavaDoc[] result= dialog.getResult();
58         if (result == null) {
59             return;
60         }
61
62         try {
63             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
64                 public void run(IProgressMonitor monitor) {
65                     monitor.beginTask(AntViewActionMessages.AddBuildFilesAction_3, result.length);
66                     for (int i = 0; i < result.length && !monitor.isCanceled(); i++) {
67                         Object JavaDoc file = result[i];
68                         if (file instanceof IFile) {
69                             String JavaDoc buildFileName= ((IFile)file).getFullPath().toString();
70                             final AntProjectNode project= new AntProjectNodeProxy(buildFileName);
71                             project.getName();
72                             monitor.worked(1);
73                             Display.getDefault().asyncExec(new Runnable JavaDoc() {
74                                 public void run() {
75                                     view.addProject(project);
76                                 }
77                             });
78                         }
79                     }
80                 }
81             });
82         } catch (InvocationTargetException JavaDoc e) {
83         } catch (InterruptedException JavaDoc e) {
84         }
85     }
86
87     private List JavaDoc getBuildFiles() {
88         AntProjectNode[] existingProjects= view.getProjects();
89         List JavaDoc buildFiles= new ArrayList JavaDoc(existingProjects.length);
90         for (int j = 0; j < existingProjects.length; j++) {
91             AntProjectNode existingProject = existingProjects[j];
92             buildFiles.add(AntUtil.getFile(existingProject.getBuildFileName()));
93         }
94         return buildFiles;
95     }
96 }
97
Popular Tags