KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > CreateSourceFolderAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.operation.IRunnableContext;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jface.wizard.WizardDialog;
29
30 import org.eclipse.ui.IWorkbenchSite;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.part.ISetSelectionTarget;
33
34 import org.eclipse.jdt.core.IClasspathEntry;
35 import org.eclipse.jdt.core.IJavaProject;
36
37 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
38
39 import org.eclipse.jdt.ui.PreferenceConstants;
40
41 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
42 import org.eclipse.jdt.internal.ui.JavaPluginImages;
43 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45 import org.eclipse.jdt.internal.ui.util.PixelConverter;
46 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
47 import org.eclipse.jdt.internal.ui.wizards.buildpaths.AddSourceFolderWizard;
48 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
49
50 //SelectedElements iff enabled: IJavaProject && size==1
51
public class CreateSourceFolderAction extends BuildpathModifierAction {
52
53     public CreateSourceFolderAction(IWorkbenchSite site) {
54         this(site, null, PlatformUI.getWorkbench().getProgressService());
55     }
56     
57     public CreateSourceFolderAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
58         this(null, selectionTarget, context);
59     }
60     
61     private CreateSourceFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
62         super(site, selectionTarget, BuildpathModifierAction.CREATE_FOLDER);
63         
64         setText(ActionMessages.OpenNewSourceFolderWizardAction_text2);
65         setDescription(ActionMessages.OpenNewSourceFolderWizardAction_description);
66         setToolTipText(ActionMessages.OpenNewSourceFolderWizardAction_tooltip);
67         setImageDescriptor(JavaPluginImages.DESC_TOOL_NEWPACKROOT);
68         
69         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_SOURCEFOLDER_WIZARD_ACTION);
70     }
71     
72     /**
73      * {@inheritDoc}
74      */

75     public String JavaDoc getDetailedDescription() {
76         return NewWizardMessages.PackageExplorerActionGroup_FormText_createNewSourceFolder;
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.jface.action.Action#run()
81      */

82     public void run() {
83         Shell shell= getShell();
84     
85         try {
86             IJavaProject javaProject= (IJavaProject)getSelectedElements().get(0);
87             
88             CPListElement newEntrie= new CPListElement(javaProject, IClasspathEntry.CPE_SOURCE);
89             CPListElement[] existing= CPListElement.createFromExisting(javaProject);
90             boolean isProjectSrcFolder= CPListElement.isProjectSourceFolder(existing, javaProject);
91             
92             AddSourceFolderWizard wizard= new AddSourceFolderWizard(existing, newEntrie, getOutputLocation(javaProject), false, false, false, isProjectSrcFolder, isProjectSrcFolder);
93             wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(javaProject));
94             
95             WizardDialog dialog= new WizardDialog(shell, wizard);
96             PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
97             dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
98             dialog.create();
99             int res= dialog.open();
100             if (res == Window.OK) {
101                 BuildpathDelta delta= new BuildpathDelta(getToolTipText());
102                 
103                 ArrayList JavaDoc newEntries= wizard.getExistingEntries();
104                 delta.setNewEntries((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()]));
105                 
106                 IResource resource= wizard.getCreatedElement().getCorrespondingResource();
107                 delta.addCreatedResource(resource);
108                     
109                 delta.setDefaultOutputLocation(wizard.getOutputLocation());
110                     
111                 informListeners(delta);
112                 
113                 selectAndReveal(new StructuredSelection(wizard.getCreatedElement()));
114             }
115             
116             notifyResult(res == Window.OK);
117         } catch (CoreException e) {
118             String JavaDoc title= NewWizardMessages.AbstractOpenWizardAction_createerror_title;
119             String JavaDoc message= NewWizardMessages.AbstractOpenWizardAction_createerror_message;
120             ExceptionHandler.handle(e, shell, title, message);
121         }
122     }
123
124     private IPath getOutputLocation(IJavaProject javaProject) {
125         try {
126             return javaProject.getOutputLocation();
127         } catch (CoreException e) {
128             IProject project= javaProject.getProject();
129             IPath projPath= project.getFullPath();
130             return projPath.append(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
131         }
132     }
133
134     protected boolean canHandle(IStructuredSelection selection) {
135         if (selection.size() != 1)
136             return false;
137             
138         if (!(selection.getFirstElement() instanceof IJavaProject))
139             return false;
140         
141         return true;
142     }
143 }
144
Popular Tags