1 11 package org.eclipse.pde.internal.ui.preferences; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Dictionary ; 15 import java.util.Locale ; 16 import java.util.Set ; 17 import java.util.StringTokenizer ; 18 import java.util.TreeSet ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.Preferences; 24 import org.eclipse.jdt.launching.JavaRuntime; 25 import org.eclipse.jface.dialogs.Dialog; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.pde.internal.core.ICoreConstants; 28 import org.eclipse.pde.internal.core.ModelProviderEvent; 29 import org.eclipse.pde.internal.core.PDECore; 30 import org.eclipse.pde.internal.core.PDEState; 31 import org.eclipse.pde.internal.core.PluginModelManager; 32 import org.eclipse.pde.internal.core.TargetPlatformHelper; 33 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo; 34 import org.eclipse.pde.internal.core.itarget.ITarget; 35 import org.eclipse.pde.internal.core.itarget.ITargetJRE; 36 import org.eclipse.pde.internal.ui.IHelpContextIds; 37 import org.eclipse.pde.internal.ui.PDEPlugin; 38 import org.eclipse.pde.internal.ui.PDEUIMessages; 39 import org.eclipse.pde.internal.ui.launcher.VMHelper; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.layout.GridLayout; 43 import org.eclipse.swt.widgets.Combo; 44 import org.eclipse.swt.widgets.Composite; 45 import org.eclipse.swt.widgets.Control; 46 import org.eclipse.swt.widgets.Group; 47 import org.eclipse.swt.widgets.Label; 48 import org.eclipse.ui.PlatformUI; 49 50 public class TargetEnvironmentTab { 51 private Combo fOSCombo; 52 private Combo fWSCombo; 53 private Combo fNLCombo; 54 private Combo fArchCombo; 55 56 private Preferences preferences; 57 private TreeSet fNLChoices; 58 private TreeSet fOSChoices; 59 private TreeSet fWSChoices; 60 private TreeSet fArchChoices; 61 private Combo fJRECombo; 62 63 private static boolean LOCALES_INITIALIZED = false; 64 private String fDefaultJRE; 65 private TargetPlatformPreferencePage fPage; 66 67 public TargetEnvironmentTab(TargetPlatformPreferencePage page) { 68 fPage = page; 69 preferences = PDECore.getDefault().getPluginPreferences(); 70 } 71 72 private void initializeChoices() { 73 fOSChoices = new TreeSet (); 74 String [] os = Platform.knownOSValues(); 75 for (int i = 0; i < os.length; i++) 76 fOSChoices.add(os[i]); 77 addExtraChoices(fOSChoices, preferences.getString(ICoreConstants.OS_EXTRA)); 78 79 fWSChoices = new TreeSet (); 80 String [] ws = Platform.knownWSValues(); 81 for (int i = 0; i < ws.length; i++) 82 fWSChoices.add(ws[i]); 83 addExtraChoices(fWSChoices, preferences.getString(ICoreConstants.WS_EXTRA)); 84 85 fArchChoices = new TreeSet (); 86 String [] arch = Platform.knownOSArchValues(); 87 for (int i = 0; i < arch.length; i++) 88 fArchChoices.add(arch[i]); 89 addExtraChoices(fArchChoices, preferences.getString(ICoreConstants.ARCH_EXTRA)); 90 91 fNLChoices = new TreeSet (); 92 if (LOCALES_INITIALIZED) { 93 initializeAllLocales(); 94 } else { 95 fNLChoices.add(expandLocaleName(preferences.getString(ICoreConstants.NL))); 96 } 97 } 98 99 protected void updateChoices() { 100 if (LOCALES_INITIALIZED) 101 return; 102 final String current = fNLCombo.getText(); 103 try { 104 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 105 public void run(IProgressMonitor monitor) { 106 initializeAllLocales(); 107 LOCALES_INITIALIZED = true; 108 } 109 }); 110 } catch (InvocationTargetException e) { 111 PDEPlugin.log(e); 112 } catch (InterruptedException e) { 113 PDEPlugin.log(e); 114 } 115 if (!fNLCombo.isDisposed()) { 116 fNLCombo.setItems((String [])fNLChoices.toArray(new String [fNLChoices.size()])); 117 fNLCombo.setText(current); 118 } 119 } 120 121 private void initializeAllLocales() { 122 String [] nl = getLocales(); 123 for (int i = 0; i < nl.length; i++) 124 fNLChoices.add(nl[i]); 125 addExtraChoices(fNLChoices, preferences.getString(ICoreConstants.NL_EXTRA)); 126 } 127 128 private void addExtraChoices(Set set, String preference) { 129 StringTokenizer tokenizer = new StringTokenizer (preference,","); while (tokenizer.hasMoreTokens()) { 131 set.add(tokenizer.nextToken().trim()); 132 } 133 } 134 135 public Control createContents(Composite parent) { 136 Composite container = new Composite(parent, SWT.NONE); 137 GridLayout layout = new GridLayout(); 138 layout.verticalSpacing = 15; 139 container.setLayout(layout); 140 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 141 142 createTargetEnvironmentGroup(container); 143 createJREGroup(container); 144 145 Dialog.applyDialogFont(container); 146 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.TARGET_ENVIRONMENT_PREFERENCE_PAGE); 147 return container; 148 } 149 150 private void createJREGroup(Composite container) { 151 Group group = new Group(container, SWT.NULL); 152 GridLayout layout = new GridLayout(); 153 layout.numColumns = 2; 154 group.setLayout(layout); 155 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 156 group.setText(PDEUIMessages.EnvironmentBlock_jreTitle); 157 158 Label label = new Label(group, SWT.NONE); 159 label.setText(PDEUIMessages.EnvironmentBlock_jreGroup); 160 161 fJRECombo = new Combo(group, SWT.SINGLE|SWT.READ_ONLY); 162 fJRECombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 163 fJRECombo.setItems(VMHelper.getVMInstallNames()); 164 fDefaultJRE = VMHelper.getDefaultVMInstallName(); 165 fJRECombo.setText(fDefaultJRE); 166 167 label = new Label(group, SWT.WRAP); 168 label.setText(PDEUIMessages.EnvironmentBlock_jreNote); 169 GridData gd = new GridData(); 170 gd.horizontalSpan = 2; 171 gd.horizontalIndent = 25; 172 gd.widthHint = 400; 173 label.setLayoutData(gd); 174 } 175 176 private void createTargetEnvironmentGroup(Composite container) { 177 Group group = new Group(container, SWT.NULL); 178 GridLayout layout = new GridLayout(); 179 layout.numColumns = 2; 180 group.setLayout(layout); 181 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 182 group.setText(PDEUIMessages.EnvironmentBlock_targetEnv); 183 184 initializeChoices(); 185 186 Label label = new Label(group, SWT.NULL); 187 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_os); 188 189 fOSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 190 fOSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 191 fOSCombo.setItems((String [])fOSChoices.toArray(new String [fOSChoices.size()])); 192 193 label = new Label(group, SWT.NULL); 194 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_ws); 195 196 fWSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 197 fWSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 198 fWSCombo.setItems((String [])fWSChoices.toArray(new String [fWSChoices.size()])); 199 200 label = new Label(group, SWT.NULL); 201 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_arch); 202 203 fArchCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 204 fArchCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 205 fArchCombo.setItems((String [])fArchChoices.toArray(new String [fArchChoices.size()])); 206 207 label = new Label(group, SWT.NULL); 208 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_nl); 209 210 fNLCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 211 fNLCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 212 fNLCombo.setItems((String [])fNLChoices.toArray(new String [fNLChoices.size()])); 213 214 fOSCombo.setText(preferences.getString(ICoreConstants.OS)); 215 fWSCombo.setText(preferences.getString(ICoreConstants.WS)); 216 fNLCombo.setText(expandLocaleName(preferences.getString(ICoreConstants.NL))); 217 fArchCombo.setText(preferences.getString(ICoreConstants.ARCH)); 218 } 219 220 protected void loadTargetProfile(ITarget target) { 221 loadTargetProfileEnvironment(target.getEnvironment()); 222 loadTargetProfileJRE(target.getTargetJREInfo()); 223 } 224 225 private void loadTargetProfileEnvironment(IEnvironmentInfo info) { 226 if (info == null) 227 return; 228 String os = info.getDisplayOS(); 229 String ws = info.getDisplayWS(); 230 String arch = info.getDisplayArch(); 231 String nl = info.getDisplayNL(); 232 nl = expandLocaleName(nl); 233 234 if (!os.equals("")) { if (fOSCombo.indexOf(os) == -1) 236 fOSCombo.add(os); 237 fOSCombo.setText(os); 238 } 239 240 if (!ws.equals("")) { if (fWSCombo.indexOf(ws) == -1) 242 fWSCombo.add(ws); 243 fWSCombo.setText(ws); 244 } 245 246 if (!arch.equals("")) { if (fArchCombo.indexOf(arch) == -1) 248 fArchCombo.add(arch); 249 fArchCombo.setText(arch); 250 } 251 252 if (!nl.equals("")) { if (fNLCombo.indexOf(nl) == -1) 254 fNLCombo.add(nl); 255 fNLCombo.setText(nl); 256 } 257 } 258 259 private void loadTargetProfileJRE(ITargetJRE info) { 260 if (info != null) 261 fJRECombo.setText(info.getCompatibleJRE()); 262 } 263 264 267 protected void performDefaults() { 268 fOSCombo.setText(preferences.getDefaultString(ICoreConstants.OS)); 269 fWSCombo.setText(preferences.getDefaultString(ICoreConstants.WS)); 270 fNLCombo.setText(expandLocaleName(preferences.getDefaultString(ICoreConstants.NL))); 271 fArchCombo.setText(preferences.getDefaultString(ICoreConstants.ARCH)); 272 fJRECombo.setText(VMHelper.getDefaultVMInstallName()); 273 } 274 275 public boolean performOk() { 276 applyTargetEnvironmentGroup(); 277 applyJREGroup(); 278 return true; 279 } 280 281 private void applyJREGroup() { 282 try { 283 if (!fDefaultJRE.equals(VMHelper.getDefaultVMInstallName())) 284 return; 285 286 if (!VMHelper.getDefaultVMInstallName().equals(fJRECombo.getText())) 287 JavaRuntime.setDefaultVMInstall(VMHelper.getVMInstall(fJRECombo.getText()), null); 288 } catch (CoreException e) { 289 } 290 } 291 292 private void applyTargetEnvironmentGroup() { 293 String oldOS = preferences.getString(ICoreConstants.OS); 294 String oldWS = preferences.getString(ICoreConstants.WS); 295 String oldARCH = preferences.getString(ICoreConstants.ARCH); 296 String oldNL = preferences.getString(ICoreConstants.NL); 297 boolean changed = false; 298 299 String os = fOSCombo.getText().trim(); 300 if (os.length() > 0) { 301 if (!fOSChoices.contains(os)) { 302 String value = preferences.getString(ICoreConstants.OS_EXTRA); 303 value = (value.length() > 0) ? value + "," + os : os; preferences.setValue(ICoreConstants.OS_EXTRA, value); 305 } 306 preferences.setValue(ICoreConstants.OS, os); 307 changed |= !(os.equals(oldOS)); 308 } 309 310 String ws = fWSCombo.getText().trim(); 311 if (ws.length() > 0) { 312 if (!fWSChoices.contains(ws)) { 313 String value = preferences.getString(ICoreConstants.WS_EXTRA); 314 value = (value.length() > 0) ? value + "," + ws : ws; preferences.setValue(ICoreConstants.WS_EXTRA, value); 316 } 317 preferences.setValue(ICoreConstants.WS, ws); 318 changed |= !(ws.equals(oldWS)); 319 } 320 321 String arch = fArchCombo.getText().trim(); 322 if (arch.length() > 0) { 323 if (!fArchChoices.contains(arch)) { 324 String value = preferences.getString(ICoreConstants.ARCH_EXTRA); 325 value = (value.length() > 0) ? value + "," + arch : arch; preferences.setValue(ICoreConstants.ARCH_EXTRA, value); 327 } 328 preferences.setValue(ICoreConstants.ARCH, arch); 329 changed |= !(arch.equals(oldARCH)); 330 } 331 332 String locale = fNLCombo.getText().trim(); 333 if (locale.length() > 0) { 334 if (!fNLChoices.contains(locale)) { 335 String value = preferences.getString(ICoreConstants.NL_EXTRA); 336 value = (value.length() > 0) ? value + "," + locale : locale; preferences.setValue(ICoreConstants.NL_EXTRA, value); 338 } 339 int dash = locale.indexOf("-"); if (dash != -1) 341 locale = locale.substring(0, dash); 342 locale = locale.trim(); 343 preferences.setValue(ICoreConstants.NL, locale); 344 changed |= !(locale.equals(oldNL)); 345 } 346 PDECore.getDefault().savePluginPreferences(); 347 if (changed) { 348 updateState(); 349 } 350 } 351 352 private void updateState() { 353 PDEState state = fPage.getCurrentState(); 354 String [] knownExecutionEnvironments = TargetPlatformHelper.getKnownExecutionEnvironments(); 356 Dictionary [] properties = TargetPlatformHelper.getPlatformProperties(knownExecutionEnvironments, fPage.getCurrentState()); 357 state.getState().setPlatformProperties(properties); 358 PluginModelManager manager = PDECore.getDefault().getModelManager(); 359 if (manager.getState() == state) { 362 manager.modelsChanged(new ModelProviderEvent( 363 properties, 364 ICoreConstants.ENVIRONMENT_CHANGED, 365 null, 366 null, 367 null)); 368 } 369 } 370 371 private String expandLocaleName(String name) { 372 String language = ""; String country = ""; String variant = ""; 376 StringTokenizer tokenizer = new StringTokenizer (name, "_"); if (tokenizer.hasMoreTokens()) 378 language = tokenizer.nextToken(); 379 if (tokenizer.hasMoreTokens()) 380 country = tokenizer.nextToken(); 381 if (tokenizer.hasMoreTokens()) 382 variant = tokenizer.nextToken(); 383 384 Locale locale = new Locale (language, country, variant); 385 return locale.toString() + " - " + locale.getDisplayName(); } 387 388 private static String [] getLocales() { 389 Locale [] locales = Locale.getAvailableLocales(); 390 String [] result = new String [locales.length]; 391 for (int i = 0; i < locales.length; i++) { 392 Locale locale = locales[i]; 393 StringBuffer buffer = new StringBuffer (); 394 buffer.append(locale.toString()); 395 buffer.append(" - "); buffer.append(locale.getDisplayName()); 397 result[i] = buffer.toString(); 398 } 399 return result; 400 } 401 402 } 403 | Popular Tags |