2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.eclipse.webapp.internal.ui;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Vector;
27  
28  import org.eclipse.core.resources.IWorkspaceRoot;
29  import org.eclipse.jdt.core.IClasspathEntry;
30  import org.eclipse.jdt.core.IJavaProject;
31  import org.eclipse.jdt.internal.ui.JavaPlugin;
32  import org.eclipse.jdt.internal.ui.preferences.PreferencesMessages;
33  import org.eclipse.jdt.internal.ui.util.PixelConverter;
34  import org.eclipse.jdt.internal.ui.util.SWTUtil;
35  import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
36  import org.eclipse.jdt.internal.ui.wizards.buildpaths.LibrariesWorkbookPage;
37  import org.eclipse.jdt.internal.ui.wizards.dialogfields.CheckedListDialogField;
38  import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
39  import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
40  import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
41  import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
42  import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
43  import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
44  import org.eclipse.jface.viewers.LabelProvider;
45  import org.eclipse.swt.SWT;
46  import org.eclipse.swt.layout.GridData;
47  import org.eclipse.swt.layout.GridLayout;
48  import org.eclipse.swt.widgets.Composite;
49  import org.eclipse.swt.widgets.Control;
50  import org.eclipse.swt.widgets.FileDialog;
51  import org.eclipse.swt.widgets.Shell;
52  import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
53  
54  /**
55   * UI block which shows a list of jar entries.
56   * 
57   * @version $Id: WebAppConfigurationBlock.java 238816 2004-02-29 16:36:46Z vmassol $
58   */
59  public class WebAppConfigurationBlock
60  {
61      /**
62       * Number of chars visible in the output war location field 
63       */
64      private static final int NB_VISIBLE_CHARS = 25;
65  
66      /**
67       * Field for the output war location. 
68       */
69      private StringButtonDialogField outputField;
70  
71      /**
72       * Field for the webapp location. 
73       */
74      private StringDialogField webappDirField;
75  
76      /**
77       * UI block that shows a list of jar entries. 
78       */
79      private LibrariesWorkbookPage libraryPage;
80  
81      /**
82       * List of entries displayed by the libraryPage. 
83       */
84      private CheckedListDialogField classPathList;
85  
86      /**
87       * Java project needed for the libraryPage initialization. 
88       */
89      private IJavaProject javaProject;
90  
91      /**
92       * The shell used by dialog popups (directory and file choosers). 
93       */
94      private Shell shell;
95  
96      /**
97       * Constructor.
98       * @param theShell The shell used by dialog popups
99       *     (directory and file choosers)
100      * @param theJavaProject Java project needed for libraryPage initialization
101      * @param theOutput initial output field value
102      * @param theDir initial webapp directory value
103      * @param theEntries initial list of entries
104      */
105     public WebAppConfigurationBlock(final Shell theShell,
106         final IJavaProject theJavaProject, final String theOutput,
107         final String theDir, final IClasspathEntry[] theEntries)
108     {
109         shell = theShell;
110         javaProject = theJavaProject;
111         BuildPathAdapter adapter = new BuildPathAdapter();
112 
113         classPathList =
114             new CheckedListDialogField(null, null, new LabelProvider());
115         classPathList.setDialogFieldListener(adapter);
116         //TODO tets if everything works as expected
117         IWorkbenchPreferenceContainer root = (IWorkbenchPreferenceContainer)JavaPlugin.getWorkspace().getRoot().getParent();
118         //SWTUtil.createPreferenceLink((IWorkbenchPreferenceContainer));
119         libraryPage = new LibrariesWorkbookPage( classPathList, root);
120         outputField = new StringButtonDialogField(adapter);
121         outputField.setDialogFieldListener(adapter);
122         outputField.setLabelText(
123             WebappMessages.getString(
124                 "WebAppConfigurationBlock.outputfield.label"));
125         outputField.setButtonLabel(
126             WebappMessages.getString(
127                 "WebAppConfigurationBlock.outputfield.button.label"));
128 
129         webappDirField = new StringDialogField();
130         webappDirField.setDialogFieldListener(adapter);
131         webappDirField.setLabelText(
132             WebappMessages.getString(
133                 "WebAppConfigurationBlock.webappdirfield.label"));
134         update(theOutput, theDir, theEntries);
135     }
136 
137     /**
138      * Adapter that displatches control events.
139      */
140     private class BuildPathAdapter
141         implements IStringButtonAdapter, IDialogFieldListener
142     {
143 
144         /**
145          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.
146          *     IStringButtonAdapter#changeControlPressed(
147          *     org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
148          */
149         public final void changeControlPressed(final DialogField theField)
150         {
151             webappChangeControlPressed(theField);
152         }
153 
154         /**
155          * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.
156          *     IDialogFieldListener#dialogFieldChanged(
157          *     org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField)
158          */
159         public final void dialogFieldChanged(final DialogField theField)
160         {
161             webappDialogFieldChanged(theField);
162         }
163     }
164 
165     /**
166      * Adapter that dispatches events from Dialog fields.
167      * Possible use : validation of an entry in a dialog field.
168      * @param theField field that triggered an event.
169      */
170     private void webappDialogFieldChanged(final DialogField theField)
171     {
172         // TODO: validate entries in dialogs
173         // Do nothing.
174     }
175 
176     /**
177      * Adapter that dispatches events from StringButtonDialog fields.
178      * @param theField field that triggered an event.
179      */
180     private void webappChangeControlPressed(final DialogField theField)
181     {
182             if (theField == outputField)
183             {
184                 File output = chooseOutput();
185                 if (output != null)
186                 {
187                     outputField.setText(output.getAbsolutePath());
188                 }
189             }
190     }
191 
192     /**
193      * Displays a file chooser dialog and returns the chosen file.
194      * @return File the chosen file
195      */
196     private File chooseOutput()
197     {
198         File output = new File(outputField.getText());
199         String initPath = "";
200         String initFileName = "webapp.war";
201 
202         if (output != null)
203         {
204             if (!output.isDirectory())
205             {
206                 File dir = output.getParentFile();
207                 if (dir != null)
208                 {
209                     initPath = dir.getPath();
210                 }
211                 initFileName = output.getName();
212             }
213             else
214             {
215                 initPath = output.getPath();
216             }
217         }
218         FileDialog dialog = new FileDialog(shell);
219         dialog.setText("");
220            // PreferencesMessages.getString(
221              //   WebappMessages.getString(
222                //     "WebAppConfigurationBlock.outputchooser.label")));
223         dialog.setFileName(initFileName);
224         dialog.setFilterExtensions(new String[] {"*.war"});
225         dialog.setFilterPath(initPath);
226         String res = dialog.open();
227         if (res != null)
228         {
229             return (new File(res));
230         }
231         return null;
232     }
233 
234 
235     /**
236      * Returns the UI control for this block.
237      * @param theParent the parent control.
238      * @return Control the created control
239      */
240     public final Control createContents(final Composite theParent)
241     {
242         Composite topComp = new Composite(theParent, SWT.NONE);
243 
244         GridLayout topLayout = new GridLayout();
245         topLayout.numColumns = 3;
246         topLayout.marginWidth = 0;
247         topLayout.marginHeight = 0;
248         topComp.setLayout(topLayout);
249 
250         outputField.doFillIntoGrid(topComp, 3);
251         webappDirField.doFillIntoGrid(topComp, 3);
252 
253         PixelConverter converter = new PixelConverter(topComp);
254         LayoutUtil.setWidthHint(
255             outputField.getTextControl(null),
256             converter.convertWidthInCharsToPixels(NB_VISIBLE_CHARS));
257         LayoutUtil.setHorizontalGrabbing(outputField.getTextControl(null));
258 
259         Control libraryPageControl = libraryPage.getControl(topComp);
260         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
261         gd.horizontalSpan = 3;
262         libraryPageControl.setLayoutData(gd);
263         libraryPage.init(javaProject);
264         return topComp;
265     }
266 
267     /**
268      * Returns the text entered in the output field.
269      * @return String the text entered
270      */
271     public final String getOutput()
272     {
273         return outputField.getText();
274     }
275 
276     /**
277      * Returns the text entered in the webapp field.
278      * @return String the text entered
279      */
280     public final String getWebappDir()
281     {
282         return webappDirField.getText();
283     }
284 
285     /**
286      * Returns the array of jar entries selected in the libraryPage.
287      * @return IClasspathEntry[] the array of jar entries selected
288      */
289     public final IClasspathEntry[] getWebappClasspath()
290     {
291         Vector result = new Vector();
292         List cplist = classPathList.getElements();
293         for (int i = 0; i < cplist.size(); i++)
294         {
295             CPListElement elem = (CPListElement) cplist.get(i);
296             if (elem.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
297             {
298                 result.add(elem.getClasspathEntry());
299             }
300         }
301         return (IClasspathEntry[]) result.toArray(
302             new IClasspathEntry[result.size()]);
303     }
304 
305     /**
306      * Returns a list of jar entries contained in an array of entries.
307      * @param theClasspathEntries array of classpath entries 
308      * @return ArrayList list containing the jar entries
309      */
310     private ArrayList getExistingEntries(
311         final IClasspathEntry[] theClasspathEntries)
312     {
313         ArrayList newClassPath = new ArrayList();
314         for (int i = 0; i < theClasspathEntries.length; i++)
315         {
316             IClasspathEntry curr = theClasspathEntries[i];
317             if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
318             {
319                 try
320                 {
321                     newClassPath.add(
322                         CPListElement.createFromExisting(curr, javaProject));
323                 }
324                 catch (NullPointerException e)
325                 {
326                     // an error occured when parsing the entry
327                     // (possibly invalid entry)
328                     // We don't add it
329                 }
330             }
331         }
332         return newClassPath;
333 
334     }
335 
336     /**
337      * Refreshes the control with the given values.
338      * @param theOutput webapp output war location 
339      * @param theDir webapp directory
340      * @param theEntries jar entries for the webapp
341      */
342     public final void update(final String theOutput,
343         final String theDir, final IClasspathEntry[] theEntries)
344     {
345         outputField.setText(theOutput);
346         webappDirField.setText(theDir);
347         classPathList.setElements(getExistingEntries(theEntries));
348     }
349 
350     /**
351      * Refreshes the control.
352      */
353     public final void refresh()
354     {
355         libraryPage.init(javaProject);
356     }
357 }