KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.DisposeEvent;
23 import org.eclipse.swt.events.DisposeListener;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Shell;
29
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.jface.viewers.StructuredSelection;
34
35 import org.eclipse.ui.forms.events.HyperlinkAdapter;
36 import org.eclipse.ui.forms.events.HyperlinkEvent;
37 import org.eclipse.ui.forms.widgets.FormText;
38 import org.eclipse.ui.forms.widgets.FormToolkit;
39 import org.eclipse.ui.forms.widgets.TableWrapData;
40 import org.eclipse.ui.forms.widgets.TableWrapLayout;
41
42 import org.eclipse.jdt.core.IPackageFragmentRoot;
43 import org.eclipse.jdt.core.JavaModelException;
44
45 import org.eclipse.jdt.internal.corext.util.Messages;
46
47 import org.eclipse.jdt.internal.ui.JavaPlugin;
48 import org.eclipse.jdt.internal.ui.preferences.ScrolledPageContent;
49 import org.eclipse.jdt.internal.ui.util.PixelConverter;
50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
51
52 /**
53  * Displays a set of available links to modify or adjust the project.
54  * The links contain a short description about the consequences of
55  * this action.
56  *
57  * The content depends on the selection made on the project.
58  * If selection changes, then the <code>HintTextGroup</code> will be
59  * notified through the <code>IPackageExplorerActionListener</code> interface.
60  */

61 public final class HintTextGroup implements ISelectionChangedListener {
62         
63     private Composite fTopComposite;
64     private DialogPackageExplorerActionGroup fActionGroup;
65     private List JavaDoc fNewFolders;
66     private HashMap JavaDoc fImageMap;
67     
68     public HintTextGroup() {
69         fNewFolders= new ArrayList JavaDoc();
70         fImageMap= new HashMap JavaDoc();
71     }
72     
73     public Composite createControl(Composite parent) {
74         fTopComposite= new Composite(parent, SWT.NONE);
75         fTopComposite.setFont(parent.getFont());
76         
77         GridData gridData= new GridData(GridData.FILL_BOTH);
78         PixelConverter converter= new PixelConverter(parent);
79         gridData.heightHint= converter.convertHeightInCharsToPixels(12);
80         gridData.widthHint= converter.convertWidthInCharsToPixels(25);
81         GridLayout gridLayout= new GridLayout();
82         gridLayout.marginWidth= 0;//-converter.convertWidthInCharsToPixels(2);
83
gridLayout.marginHeight= 0;//= -4;
84
fTopComposite.setLayout(gridLayout);
85         fTopComposite.setLayoutData(gridData);
86         fTopComposite.setData(null);
87         fTopComposite.addDisposeListener(new DisposeListener() {
88             public void widgetDisposed(DisposeEvent e) {
89                 Collection JavaDoc collection= fImageMap.values();
90                 Iterator JavaDoc iterator= collection.iterator();
91                 while(iterator.hasNext()) {
92                     Image image= (Image)iterator.next();
93                     image.dispose();
94                 }
95             }
96         });
97         return fTopComposite;
98     }
99     
100     private Shell getShell() {
101         return JavaPlugin.getActiveWorkbenchShell();
102     }
103     
104     /**
105      * An action group managing the actions needed by
106      * the <code>HintTextGroup</code>.
107      *
108      * Note: This method has to be called on initialization.
109      * Calling this method in the constructor is not
110      * possible because the actions might need a reference to
111      * this class.
112      *
113      * @param actionGroup the action group containing the necessary
114      * actions
115      *
116      * @see DialogPackageExplorerActionGroup
117      */

118     public void setActionGroup(DialogPackageExplorerActionGroup actionGroup) {
119         fActionGroup= actionGroup;
120     }
121     
122     /**
123      * Creates a form text.
124      *
125      * @param parent the parent to put the form text on
126      * @param text the form text to be displayed
127      * @return the created form text
128      *
129      * @see FormToolkit#createFormText(org.eclipse.swt.widgets.Composite, boolean)
130      */

131     private FormText createFormText(Composite parent, String JavaDoc text) {
132         FormToolkit toolkit= new FormToolkit(getShell().getDisplay());
133         try {
134             FormText formText= toolkit.createFormText(parent, true);
135             formText.setFont(parent.getFont());
136             try {
137                 formText.setText(text, true, false);
138             } catch (IllegalArgumentException JavaDoc e) {
139                 formText.setText(e.getMessage(), false, false);
140                 JavaPlugin.log(e);
141             }
142             formText.marginHeight= 2;
143             formText.marginWidth= 0;
144             formText.setBackground(null);
145             formText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
146             return formText;
147         } finally {
148             toolkit.dispose();
149         }
150     }
151     
152     /**
153      * Create a label with a hyperlink and a picture.
154      *
155      * @param parent the parent widget of the label
156      * @param text the text of the label
157      * @param action the action to be executed if the hyperlink is activated
158      */

159     private void createLabel(Composite parent, String JavaDoc text, final BuildpathModifierAction action) {
160         FormText formText= createFormText(parent, text);
161         Image image= (Image)fImageMap.get(action.getId());
162         if (image == null) {
163             image= action.getImageDescriptor().createImage();
164             fImageMap.put(action.getId(), image);
165         }
166         formText.setImage("defaultImage", image); //$NON-NLS-1$
167
formText.addHyperlinkListener(new HyperlinkAdapter() {
168
169             public void linkActivated(HyperlinkEvent e) {
170                 action.run();
171             }
172             
173         });
174     }
175     
176     /**
177      * Handle folder creation. This includes:
178      * <li>Set the selection of the <code>fPackageExplorer</code>
179      * to the result object, unless the result object is <code>
180      * null</code></li>
181      * <li>Add the created folder to the list of new folders</li>
182      * <li>Adjust the text of <code>fOutputLocationField</code>
183      * and add the project's new output location to the list of
184      * new folders, if necessary
185      *
186      * In this case, the list consists only of one element on which the
187      * new folder has been created
188      *
189      * @param result a list with only one element to be selected by the
190      * <code>fPackageExplorer</code>, or an empty list if creation was
191      * aborted
192      */

193     void handleFolderCreation(List JavaDoc result) {
194         if (result.size() == 1) {
195             try {
196                 fNewFolders.add(((IPackageFragmentRoot)result.get(0)).getCorrespondingResource());
197             } catch (JavaModelException e) {
198                 JavaPlugin.log(e);
199             }
200         }
201     }
202     
203     public List JavaDoc getCreatedResources() {
204         return fNewFolders;
205     }
206         
207     public void resetCreatedResources() {
208         fNewFolders.clear();
209     }
210     
211     /**
212      * {@inheritDoc}
213      */

214     public void selectionChanged(SelectionChangedEvent event) {
215         if (event.getSelection() instanceof StructuredSelection) {
216             handlePostSelectionChange((StructuredSelection)event.getSelection());
217         } else {
218             handlePostSelectionChange(StructuredSelection.EMPTY);
219         }
220     }
221
222     private void handlePostSelectionChange(StructuredSelection selection) {
223         
224         BuildpathModifierAction[] actions= fActionGroup.getHintTextGroupActions();
225         String JavaDoc[] descriptions= new String JavaDoc[actions.length];
226         for (int i= 0; i < actions.length; i++) {
227             descriptions[i]= actions[i].getDetailedDescription();
228         }
229         
230         // Get the child composite of the top composite
231
Composite childComposite= (Composite)fTopComposite.getData();
232         
233         // Dispose old composite (if necessary)
234
if (childComposite != null && childComposite.getParent() != null)
235             childComposite.getParent().dispose();
236       
237         PixelConverter converter= new PixelConverter(fTopComposite);
238         
239         // Create new composite
240
ScrolledPageContent spc= new ScrolledPageContent(fTopComposite, SWT.V_SCROLL);
241         spc.getVerticalBar().setIncrement(5);
242         
243         GridData gridData= new GridData(GridData.FILL_BOTH);
244         
245         gridData.heightHint= converter.convertHeightInCharsToPixels(12);
246         gridData.widthHint= converter.convertWidthInCharsToPixels(25);
247         spc.setLayoutData(gridData);
248         
249         childComposite= spc.getBody();
250         TableWrapLayout tableWrapLayout= new TableWrapLayout();
251         tableWrapLayout.leftMargin= 0;
252         tableWrapLayout.rightMargin= 0;
253         childComposite.setLayout(tableWrapLayout);
254         gridData= new GridData(GridData.FILL_BOTH);
255         gridData.heightHint= converter.convertHeightInCharsToPixels(12);
256         gridData.widthHint= converter.convertWidthInCharsToPixels(25);
257         childComposite.setLayoutData(gridData);
258         
259         fTopComposite.setData(childComposite);
260         
261         if (noContextHelpAvailable(actions)) {
262             String JavaDoc noAction= noAction(selection);
263             createFormText(childComposite, Messages.format(NewWizardMessages.HintTextGroup_NoAction, noAction));
264             fTopComposite.layout(true);
265             return;
266         }
267         
268         for (int i= 0; i < actions.length; i++) {
269             createLabel(childComposite, descriptions[i], actions[i]);
270         }
271         
272         fTopComposite.layout(true);
273     }
274     
275     private String JavaDoc noAction(ISelection selection) {
276         if (selection instanceof StructuredSelection) {
277             return noAction(((StructuredSelection)selection).toList());
278         } else {
279             return noAction(Collections.EMPTY_LIST);
280         }
281     }
282
283     private String JavaDoc noAction(List JavaDoc selectedElements) {
284         if (selectedElements.size() == 0)
285             return NewWizardMessages.PackageExplorerActionGroup_NoAction_NullSelection;
286         
287         if (selectedElements.size() == 1)
288             return NewWizardMessages.PackageExplorerActionGroup_NoAction_NoReason;
289         
290         return NewWizardMessages.PackageExplorerActionGroup_NoAction_MultiSelection;
291     }
292         
293     /**
294      * Check if for the current type of selection, no context specific actions can
295      * be applied. Note: this does not mean, that there are NO actions available at all.<p>
296      *
297      * For example: if the default package is selected, there is no specific action for this kind
298      * of selection as no operations are allowed on the default package. Nevertheless, the
299      * <code>PackageExplorerActionEvent</code> will return at least one action that allows to
300      * link to an existing folder in the file system, but this operation is always available
301      * and does not add any supporting information to the current selection. Therefore,
302      * it can be filtered and the correct answer to the user is that there is no specific
303      * action for the default package.
304      *
305      * @param actions an array of provided actions
306      * @return <code>true</code> if there is at least one action that allows context
307      * sensitive operations, <code>false</code> otherwise.
308      */

309     private boolean noContextHelpAvailable(BuildpathModifierAction[] actions) {
310         if (actions.length == 0)
311             return true;
312         if (actions.length == 1) {
313             int id= Integer.parseInt(actions[0].getId());
314             if (id == BuildpathModifierAction.CREATE_LINK)
315                 return true;
316         }
317         if (actions.length == 2) {
318             int idLink= Integer.parseInt(actions[0].getId());
319             int idReset= Integer.parseInt(actions[1].getId());
320             if (idReset == BuildpathModifierAction.RESET_ALL &&
321                 idLink == BuildpathModifierAction.CREATE_LINK)
322                 return true;
323         }
324         return false;
325     }
326 }
327
Popular Tags