1 11 package org.eclipse.ant.internal.ui.preferences; 12 13 14 import java.io.File ; 15 import java.io.FilenameFilter ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 import java.util.List ; 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 41 public class AntClasspathPage implements IAntBlockContainer { 42 43 private AntClasspathBlock fAntClasspathBlock= new AntClasspathBlock(); 44 private AntRuntimePreferencePage fPreferencePage; 45 private ClasspathModel fModel; 46 47 50 public AntClasspathPage(AntRuntimePreferencePage preferencePage) { 51 fPreferencePage = preferencePage; 52 } 53 54 59 protected IAntClasspathEntry[] getAdditionalEntries() { 60 return fModel.getEntries(ClasspathModel.GLOBAL_USER); 61 } 62 63 66 protected IAntClasspathEntry[] getAntHomeEntries() { 67 return fModel.getEntries(ClasspathModel.ANT_HOME); 68 } 69 70 73 protected IAntClasspathEntry[] getContributedEntries() { 74 return fModel.getEntries(ClasspathModel.CONTRIBUTED); 75 } 76 77 protected String getAntHome() { 78 return fAntClasspathBlock.getAntHome(); 79 } 80 81 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 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 getDefaultAdditionalEntries() { 120 IAntClasspathEntry toolsJarEntry= AntCorePlugin.getPlugin().getPreferences().getToolsJarEntry(); 121 File libDir= new File (System.getProperty("user.home"), ".ant" + File.separatorChar + "lib"); URL [] urls= null; 124 try { 125 urls= getLocationURLs(libDir); 126 } catch (MalformedURLException e) { 127 AntUIPlugin.log(e); 128 return new ArrayList (0); 129 } 130 131 List entries= new ArrayList (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 [] getLocationURLs(File location) throws MalformedURLException { 143 final String extension= ".jar"; URL [] urls = new URL [0]; 145 146 if (!location.exists()) { 147 return urls; 148 } 149 150 if (!location.isDirectory()) { 151 urls = new URL [1]; 152 String path = location.getPath(); 153 if (path.toLowerCase().endsWith(extension)) { 154 urls[0] = location.toURL(); 155 } 156 return urls; 157 } 158 159 File [] matches = location.listFiles( 160 new FilenameFilter () { 161 public boolean accept(File dir, String name) { 162 return name.toLowerCase().endsWith(extension); 163 } 164 }); 165 166 urls = new URL [matches.length]; 167 for (int i = 0; i < matches.length; ++i) { 168 urls[i] = matches[i].toURL(); 169 } 170 return urls; 171 } 172 173 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 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 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 230 public void setMessage(String message) { 231 fPreferencePage.setMessage(message); 232 } 233 234 237 public void setErrorMessage(String message) { 238 fPreferencePage.setErrorMessage(message); 239 } 240 241 244 public Button createPushButton(Composite parent, String 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 |