KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21
22 import org.eclipse.core.resources.IFolder;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.SashForm;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31
32 import org.eclipse.jface.action.ToolBarManager;
33 import org.eclipse.jface.operation.IRunnableContext;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.viewers.StructuredSelection;
36
37 import org.eclipse.ui.forms.events.ExpansionAdapter;
38 import org.eclipse.ui.forms.events.ExpansionEvent;
39 import org.eclipse.ui.forms.widgets.ExpandableComposite;
40
41 import org.eclipse.jdt.core.IClasspathEntry;
42 import org.eclipse.jdt.core.IJavaProject;
43 import org.eclipse.jdt.core.IPackageFragmentRoot;
44 import org.eclipse.jdt.core.JavaCore;
45 import org.eclipse.jdt.core.JavaModelException;
46
47 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
48 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
49 import org.eclipse.jdt.internal.corext.buildpath.IBuildpathModifierListener;
50
51 import org.eclipse.jdt.internal.ui.JavaPlugin;
52 import org.eclipse.jdt.internal.ui.preferences.ScrolledPageContent;
53 import org.eclipse.jdt.internal.ui.util.PixelConverter;
54 import org.eclipse.jdt.internal.ui.util.ViewerPane;
55 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
56 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage;
57 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock;
58 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
59 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElementAttribute;
60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
61 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
62 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
63 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField;
64 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
65 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
66
67 public class NewSourceContainerWorkbookPage extends BuildPathBasePage implements IBuildpathModifierListener {
68     
69     public static final String JavaDoc OPEN_SETTING= "org.eclipse.jdt.internal.ui.wizards.buildpaths.NewSourceContainerPage.openSetting"; //$NON-NLS-1$
70

71     private ListDialogField fClassPathList;
72     private HintTextGroup fHintTextGroup;
73     private DialogPackageExplorer fPackageExplorer;
74     private SelectionButtonDialogField fUseFolderOutputs;
75     private final StringDialogField fOutputLocationField;
76     private DialogPackageExplorerActionGroup fActionGroup;
77     
78     private IJavaProject fJavaProject;
79
80     private final IRunnableContext fContext;
81
82     private final BuildPathsBlock fBuildPathsBlock;
83
84
85     /**
86      * Constructor of the <code>NewSourceContainerWorkbookPage</code> which consists of
87      * a tree representing the project, a toolbar with the available actions, an area
88      * containing hyperlinks that perform the same actions as those in the toolbar but
89      * additionally with some short description.
90      *
91      * @param classPathList
92      * @param outputLocationField
93      * @param context a runnable context, can be <code>null</code>
94      * @param buildPathsBlock
95      */

96     public NewSourceContainerWorkbookPage(ListDialogField classPathList, StringDialogField outputLocationField, IRunnableContext context, BuildPathsBlock buildPathsBlock) {
97         fClassPathList= classPathList;
98         fOutputLocationField= outputLocationField;
99         fContext= context;
100         fBuildPathsBlock= buildPathsBlock;
101     
102         fUseFolderOutputs= new SelectionButtonDialogField(SWT.CHECK);
103         fUseFolderOutputs.setSelection(false);
104         fUseFolderOutputs.setLabelText(NewWizardMessages.SourceContainerWorkbookPage_folders_check);
105         
106         fPackageExplorer= new DialogPackageExplorer();
107         fHintTextGroup= new HintTextGroup();
108      }
109     
110     /**
111      * Initialize the controls displaying
112      * the content of the java project and saving
113      * the '.classpath' and '.project' file.
114      *
115      * Must be called before initializing the
116      * controls using <code>getControl(Composite)</code>.
117      *
118      * @param javaProject the current java project
119      */

120     public void init(IJavaProject javaProject) {
121         fJavaProject= javaProject;
122         fPackageExplorer.addPostSelectionChangedListener(fHintTextGroup);
123         fActionGroup.getResetAllAction().setBreakPoint(javaProject);
124
125         if (Display.getCurrent() != null) {
126             doUpdateUI();
127         } else {
128             Display.getDefault().asyncExec(new Runnable JavaDoc() {
129                 public void run() {
130                     doUpdateUI();
131                 }
132             });
133         }
134     }
135     
136     private void doUpdateUI() {
137         fPackageExplorer.setInput(fJavaProject);
138         
139         boolean useFolderOutputs= false;
140         List JavaDoc cpelements= fClassPathList.getElements();
141         for (int i= 0; i < cpelements.size() && !useFolderOutputs; i++) {
142             CPListElement cpe= (CPListElement) cpelements.get(i);
143             if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
144                 if (cpe.getAttribute(CPListElement.OUTPUT) != null) {
145                     useFolderOutputs= true;
146                 }
147             }
148         }
149         fUseFolderOutputs.setSelection(useFolderOutputs);
150     }
151     
152     public void dispose() {
153         if (fActionGroup != null) {
154             fActionGroup.removeBuildpathModifierListener(this);
155             fActionGroup= null;
156         }
157         fPackageExplorer.removePostSelectionChangedListener(fHintTextGroup);
158         fPackageExplorer.dispose();
159     }
160      
161     /**
162      * Initializes controls and return composite containing
163      * these controls.
164      *
165      * Before calling this method, make sure to have
166      * initialized this instance with a java project
167      * using <code>init(IJavaProject)</code>.
168      *
169      * @param parent the parent composite
170      * @return composite containing controls
171      *
172      * @see #init(IJavaProject)
173      */

174     public Control getControl(Composite parent) {
175         final int[] sashWeight= {60};
176         final IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
177         preferenceStore.setDefault(OPEN_SETTING, true);
178         
179         // ScrolledPageContent is needed for resizing on expand the expandable composite
180
ScrolledPageContent scrolledContent = new ScrolledPageContent(parent);
181         Composite body= scrolledContent.getBody();
182         body.setLayout(new GridLayout());
183         
184         final SashForm sashForm= new SashForm(body, SWT.VERTICAL | SWT.NONE);
185         sashForm.setFont(sashForm.getFont());
186         
187         ViewerPane pane= new ViewerPane(sashForm, SWT.BORDER | SWT.FLAT);
188         pane.setContent(fPackageExplorer.createControl(pane));
189         fPackageExplorer.setContentProvider();
190         
191         final ExpandableComposite excomposite= new ExpandableComposite(sashForm, SWT.NONE, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT);
192         excomposite.setFont(sashForm.getFont());
193         excomposite.setText(NewWizardMessages.NewSourceContainerWorkbookPage_HintTextGroup_title);
194         final boolean isExpanded= preferenceStore.getBoolean(OPEN_SETTING);
195         excomposite.setExpanded(isExpanded);
196         excomposite.addExpansionListener(new ExpansionAdapter() {
197                        public void expansionStateChanged(ExpansionEvent e) {
198                            ScrolledPageContent parentScrolledComposite= getParentScrolledComposite(excomposite);
199                            if (parentScrolledComposite != null) {
200                               boolean expanded= excomposite.isExpanded();
201                               parentScrolledComposite.reflow(true);
202                               adjustSashForm(sashWeight, sashForm, expanded);
203                               preferenceStore.setValue(OPEN_SETTING, expanded);
204                            }
205                        }
206                  });
207         
208         excomposite.setClient(fHintTextGroup.createControl(excomposite));
209         fUseFolderOutputs.doFillIntoGrid(body, 1);
210         
211         fActionGroup= new DialogPackageExplorerActionGroup(fHintTextGroup, fContext, fPackageExplorer, this);
212         fActionGroup.addBuildpathModifierListener(this);
213            
214         
215         fUseFolderOutputs.setDialogFieldListener(new IDialogFieldListener() {
216             public void dialogFieldChanged(DialogField field) {
217                 boolean isUseFolders= fUseFolderOutputs.isSelected();
218                 if (!isUseFolders) {
219                     ResetAllOutputFoldersAction action= new ResetAllOutputFoldersAction(fContext, fJavaProject, fPackageExplorer) {
220                         public void run() {
221                             commitDefaultOutputFolder();
222                             super.run();
223                         }
224                     };
225                     action.addBuildpathModifierListener(NewSourceContainerWorkbookPage.this);
226                     action.run();
227                 }
228                 fPackageExplorer.showOutputFolders(isUseFolders);
229             }
230         });
231         
232         Composite outputLocation= new Composite(body, SWT.NONE);
233         outputLocation.setLayout(new GridLayout(2, false));
234         outputLocation.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
235         
236         LayoutUtil.doDefaultLayout(outputLocation, new DialogField[] {fOutputLocationField }, true, SWT.DEFAULT, SWT.DEFAULT);
237         LayoutUtil.setHorizontalGrabbing(fOutputLocationField.getTextControl(null));
238         
239         // Create toolbar with actions on the left
240
ToolBarManager tbm= fActionGroup.createLeftToolBarManager(pane);
241         pane.setTopCenter(null);
242         pane.setTopLeft(tbm.getControl());
243         
244         // Create toolbar with help on the right
245
tbm= fActionGroup.createLeftToolBar(pane);
246         pane.setTopRight(tbm.getControl());
247         
248         fHintTextGroup.setActionGroup(fActionGroup);
249         fPackageExplorer.setActionGroup(fActionGroup);
250         
251         sashForm.setWeights(new int[] {60, 40});
252         adjustSashForm(sashWeight, sashForm, excomposite.isExpanded());
253         GridData gd= new GridData(GridData.FILL_BOTH);
254         PixelConverter converter= new PixelConverter(parent);
255         gd.heightHint= converter.convertHeightInCharsToPixels(20);
256         sashForm.setLayoutData(gd);
257         
258         parent.layout(true);
259
260         return scrolledContent;
261     }
262     
263     /**
264      * Adjust the size of the sash form.
265      *
266      * @param sashWeight the weight to be read or written
267      * @param sashForm the sash form to apply the new weights to
268      * @param isExpanded <code>true</code> if the expandable composite is
269      * expanded, <code>false</code> otherwise
270      */

271     private void adjustSashForm(int[] sashWeight, SashForm sashForm, boolean isExpanded) {
272         if (isExpanded) {
273             int upperWeight= sashWeight[0];
274             sashForm.setWeights(new int[]{upperWeight, 100 - upperWeight});
275         }
276         else {
277             // TODO Dividing by 10 because of https://bugs.eclipse.org/bugs/show_bug.cgi?id=81939
278
sashWeight[0]= sashForm.getWeights()[0] / 10;
279             sashForm.setWeights(new int[]{95, 5});
280         }
281         sashForm.layout(true);
282     }
283     
284     /**
285      * Get the scrolled page content of the given control by
286      * traversing the parents.
287      *
288      * @param control the control to get the scrolled page content for
289      * @return the scrolled page content or <code>null</code> if none found
290      */

291     private ScrolledPageContent getParentScrolledComposite(Control control) {
292        Control parent= control.getParent();
293        while (!(parent instanceof ScrolledPageContent)) {
294            parent= parent.getParent();
295        }
296        if (parent instanceof ScrolledPageContent) {
297            return (ScrolledPageContent) parent;
298        }
299        return null;
300    }
301     
302     /* (non-Javadoc)
303      * @see org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage#getSelection()
304      */

305     public List JavaDoc getSelection() {
306         List JavaDoc selectedList= new ArrayList JavaDoc();
307         
308         IJavaProject project= fJavaProject;
309         try {
310             List JavaDoc list= ((StructuredSelection)fPackageExplorer.getSelection()).toList();
311             List JavaDoc existingEntries= ClasspathModifier.getExistingEntries(project);
312         
313             for(int i= 0; i < list.size(); i++) {
314                 Object JavaDoc obj= list.get(i);
315                 if (obj instanceof IPackageFragmentRoot) {
316                     IPackageFragmentRoot element= (IPackageFragmentRoot)obj;
317                     CPListElement cpElement= ClasspathModifier.getClasspathEntry(existingEntries, element);
318                     selectedList.add(cpElement);
319                 }
320                 else if (obj instanceof IJavaProject) {
321                     IClasspathEntry entry= ClasspathModifier.getClasspathEntryFor(project.getPath(), project, IClasspathEntry.CPE_SOURCE);
322                     if (entry == null)
323                         continue;
324                     CPListElement cpElement= CPListElement.createFromExisting(entry, project);
325                     selectedList.add(cpElement);
326                 }
327             }
328         } catch (JavaModelException e) {
329             return new ArrayList JavaDoc();
330         }
331         return selectedList;
332     }
333
334     /* (non-Javadoc)
335      * @see org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage#setSelection(java.util.List)
336      */

337     public void setSelection(List JavaDoc selection, boolean expand) {
338         // page switch
339

340         if (selection.size() == 0)
341             return;
342         
343         List JavaDoc cpEntries= new ArrayList JavaDoc();
344         
345         for (int i= 0; i < selection.size(); i++) {
346             Object JavaDoc obj= selection.get(i);
347             if (obj instanceof CPListElement) {
348                 CPListElement element= (CPListElement) obj;
349                 if (element.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
350                     cpEntries.add(element);
351                 }
352             } else if (obj instanceof CPListElementAttribute) {
353                 CPListElementAttribute attribute= (CPListElementAttribute)obj;
354                 CPListElement element= attribute.getParent();
355                 if (element.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
356                     cpEntries.add(element);
357                 }
358             }
359         }
360         
361         // refresh classpath
362
List JavaDoc list= fClassPathList.getElements();
363         IClasspathEntry[] entries= new IClasspathEntry[list.size()];
364         for(int i= 0; i < list.size(); i++) {
365             CPListElement entry= (CPListElement) list.get(i);
366             entries[i]= entry.getClasspathEntry();
367         }
368         try {
369             fJavaProject.setRawClasspath(entries, null);
370         } catch (JavaModelException e) {
371             JavaPlugin.log(e);
372         }
373         
374         fPackageExplorer.setSelection(cpEntries);
375     }
376
377     /* (non-Javadoc)
378      * @see org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathBasePage#isEntryKind(int)
379      */

380     public boolean isEntryKind(int kind) {
381         return kind == IClasspathEntry.CPE_SOURCE;
382     }
383     
384     /**
385      * Update <code>fClassPathList</code>.
386      * @param delta describing the changes to the buildpath
387      */

388     public void buildpathChanged(BuildpathDelta delta) {
389         fClassPathList.setElements(Arrays.asList(delta.getNewEntries()));
390         
391         try {
392             fOutputLocationField.setText(fJavaProject.getOutputLocation().makeRelative().toString());
393         } catch (JavaModelException e) {
394             JavaPlugin.log(e);
395         }
396     }
397
398     public void commitDefaultOutputFolder() {
399         if (!fBuildPathsBlock.isOKStatus())
400             return;
401         try {
402             IPath path= new Path(fOutputLocationField.getText()).makeAbsolute();
403             IPath outputLocation= fJavaProject.getOutputLocation();
404             if (path.equals(outputLocation))
405                 return;
406             
407             if (!outputLocation.equals(fJavaProject.getPath())) {
408                 IFolder folder= fJavaProject.getProject().getWorkspace().getRoot().getFolder(outputLocation);
409                 if (folder.exists() && JavaCore.create(folder) == null) {
410                     folder.delete(true, null);
411                 }
412             }
413             fJavaProject.setOutputLocation(path, null);
414         } catch (JavaModelException e) {
415             JavaPlugin.log(e);
416         } catch (CoreException e) {
417             JavaPlugin.log(e);
418         }
419     }
420
421     /**
422      * {@inheritDoc}
423      */

424     public void setFocus() {
425         fPackageExplorer.getViewerControl().setFocus();
426     }
427
428     public IJavaProject getJavaProject() {
429         return fJavaProject;
430     }
431 }
432
Popular Tags