KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntClasspathPage


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 package org.eclipse.ant.internal.ui.preferences;
12
13
14 import java.io.File JavaDoc;
15 import java.io.FilenameFilter JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.eclipse.ant.core.AntCorePlugin;
22 import org.eclipse.ant.core.AntCorePreferences;
23 import org.eclipse.ant.core.IAntClasspathEntry;
24 import org.eclipse.ant.internal.core.AntClasspathEntry;
25 import org.eclipse.ant.internal.ui.AntUIPlugin;
26 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.TabFolder;
34 import org.eclipse.swt.widgets.TabItem;
35 import org.eclipse.ui.PlatformUI;
36
37 /**
38  * Sub-page that allows the user to enter custom classpaths
39  * to be used when running Ant build files.
40  */

41 public class AntClasspathPage implements IAntBlockContainer {
42
43     private AntClasspathBlock fAntClasspathBlock= new AntClasspathBlock();
44     private AntRuntimePreferencePage fPreferencePage;
45     private ClasspathModel fModel;
46     
47     /**
48      * Creates an instance.
49      */

50     public AntClasspathPage(AntRuntimePreferencePage preferencePage) {
51         fPreferencePage = preferencePage;
52     }
53     
54     /**
55      * Returns the specified user classpath entries
56      *
57      * @return set of user classpath entries
58      */

59     protected IAntClasspathEntry[] getAdditionalEntries() {
60         return fModel.getEntries(ClasspathModel.GLOBAL_USER);
61     }
62     
63     /**
64      * Returns the specified ant home classpath entries
65      */

66     protected IAntClasspathEntry[] getAntHomeEntries() {
67         return fModel.getEntries(ClasspathModel.ANT_HOME);
68     }
69     
70     /**
71      * Returns the contributed classpath entries
72      */

73     protected IAntClasspathEntry[] getContributedEntries() {
74         return fModel.getEntries(ClasspathModel.CONTRIBUTED);
75     }
76     
77     protected String JavaDoc getAntHome() {
78         return fAntClasspathBlock.getAntHome();
79     }
80     
81     /**
82      * Sets the contents of the tables on this page.
83      */

84     protected void initialize() {
85         
86         AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences();
87         createClasspathModel();
88         fAntClasspathBlock.initializeAntHome(prefs.getAntHome());
89         fAntClasspathBlock.setInput(fModel);
90         
91         fPreferencePage.setErrorMessage(null);
92         fPreferencePage.setValid(true);
93     }
94     
95     protected void createClasspathModel() {
96         fModel= new ClasspathModel();
97         AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences();
98         fModel.setAntHomeEntries(prefs.getAntHomeClasspathEntries());
99         fModel.setGlobalEntries(prefs.getAdditionalClasspathEntries());
100         fModel.setContributedEntries(prefs.getContributedClasspathEntries());
101     }
102     
103     protected void performDefaults() {
104         AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences();
105         fModel= new ClasspathModel();
106         fModel.setAntHomeEntries(prefs.getDefaultAntHomeEntries());
107         List JavaDoc additionalEntries= getDefaultAdditionalEntries();
108         if (additionalEntries != null) {
109             fModel.setGlobalEntries((IAntClasspathEntry[]) additionalEntries.toArray(new IAntClasspathEntry[additionalEntries.size()]));
110         } else {
111             fModel.setGlobalEntries(new IAntClasspathEntry[0]);
112         }
113         fModel.setContributedEntries(prefs.getContributedClasspathEntries());
114         fAntClasspathBlock.initializeAntHome(prefs.getDefaultAntHome());
115         fAntClasspathBlock.setInput(fModel);
116         update();
117     }
118     
119     private List JavaDoc getDefaultAdditionalEntries() {
120         IAntClasspathEntry toolsJarEntry= AntCorePlugin.getPlugin().getPreferences().getToolsJarEntry();
121         //TODO should use AntCorePreferences.getUserLibraries when promoted to API post 3.1
122
File JavaDoc libDir= new File JavaDoc(System.getProperty("user.home"), ".ant" + File.separatorChar + "lib"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
URL JavaDoc[] urls= null;
124         try {
125             urls= getLocationURLs(libDir);
126         } catch (MalformedURLException JavaDoc e) {
127             AntUIPlugin.log(e);
128             return new ArrayList JavaDoc(0);
129         }
130         
131         List JavaDoc entries= new ArrayList JavaDoc(urls.length);
132         for (int i = 0; i < urls.length; i++) {
133             AntClasspathEntry entry= new AntClasspathEntry(urls[i]);
134             entries.add(entry);
135         }
136         if (toolsJarEntry != null) {
137             entries.add(toolsJarEntry);
138         }
139         return entries;
140     }
141     
142     private URL JavaDoc[] getLocationURLs(File JavaDoc location) throws MalformedURLException JavaDoc {
143          final String JavaDoc extension= ".jar"; //$NON-NLS-1$
144
URL JavaDoc[] urls = new URL JavaDoc[0];
145          
146          if (!location.exists()) {
147              return urls;
148          }
149          
150          if (!location.isDirectory()) {
151              urls = new URL JavaDoc[1];
152              String JavaDoc path = location.getPath();
153              if (path.toLowerCase().endsWith(extension)) {
154                  urls[0] = location.toURL();
155              }
156              return urls;
157          }
158          
159          File JavaDoc[] matches = location.listFiles(
160              new FilenameFilter JavaDoc() {
161                  public boolean accept(File JavaDoc dir, String JavaDoc name) {
162                     return name.toLowerCase().endsWith(extension);
163                  }
164              });
165          
166          urls = new URL JavaDoc[matches.length];
167          for (int i = 0; i < matches.length; ++i) {
168              urls[i] = matches[i].toURL();
169          }
170          return urls;
171      }
172     
173     /**
174      * Creates the tab item that contains this sub-page.
175      */

176     protected TabItem createTabItem(TabFolder folder) {
177         TabItem item = new TabItem(folder, SWT.NONE);
178         item.setText(AntPreferencesMessages.AntClasspathPage_title);
179         item.setImage(fAntClasspathBlock.getClasspathImage());
180         item.setData(this);
181         item.setControl(createContents(folder));
182         return item;
183     }
184     
185     /**
186      * Creates this page's controls
187      */

188     protected Composite createContents(Composite parent) {
189         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IAntUIHelpContextIds.ANT_CLASSPATH_PAGE);
190         Font font = parent.getFont();
191         
192         Composite top = new Composite(parent, SWT.NONE);
193         top.setFont(font);
194         
195         GridLayout layout = new GridLayout();
196         layout.numColumns = 2;
197         layout.marginHeight = 2;
198         layout.marginWidth = 2;
199         top.setLayout(layout);
200
201         top.setLayoutData(new GridData(GridData.FILL_BOTH));
202
203         fAntClasspathBlock.setContainer(this);
204         fAntClasspathBlock.createContents(top);
205         
206         return top;
207     }
208     
209     /* (non-Javadoc)
210      * @see org.eclipse.ant.internal.ui.preferences.IAntBlockContainer#update()
211      */

212     public void update() {
213         if (fAntClasspathBlock.isValidated()){
214             return;
215         }
216         setMessage(null);
217         setErrorMessage(null);
218         boolean valid= fAntClasspathBlock.validateAntHome();
219     
220         if (valid) {
221             valid= fAntClasspathBlock.validateToolsJAR();
222         }
223         
224         fPreferencePage.setValid(valid);
225     }
226     
227     /* (non-Javadoc)
228      * @see org.eclipse.ant.internal.ui.preferences.IAntBlockContainer#setMessage(java.lang.String)
229      */

230     public void setMessage(String JavaDoc message) {
231         fPreferencePage.setMessage(message);
232     }
233     
234     /* (non-Javadoc)
235      * @see org.eclipse.ant.internal.ui.preferences.IAntBlockContainer#setErrorMessage(java.lang.String)
236      */

237     public void setErrorMessage(String JavaDoc message) {
238         fPreferencePage.setErrorMessage(message);
239     }
240     
241     /* (non-Javadoc)
242      * @see org.eclipse.ant.internal.ui.preferences.IAntBlockContainer#createPushButton(org.eclipse.swt.widgets.Composite, java.lang.String)
243      */

244     public Button createPushButton(Composite parent, String JavaDoc buttonText) {
245         Button button = new Button(parent, SWT.PUSH);
246         button.setFont(parent.getFont());
247         button.setText(buttonText);
248         fPreferencePage.setButtonLayoutData(button);
249         return button;
250     }
251 }
252
Popular Tags