1 11 package org.eclipse.pde.internal.ui.preferences; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Locale ; 15 import java.util.Set ; 16 import java.util.StringTokenizer ; 17 import java.util.TreeSet ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.Preferences; 23 import org.eclipse.jdt.launching.JavaRuntime; 24 import org.eclipse.jface.dialogs.Dialog; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.pde.internal.core.ICoreConstants; 27 import org.eclipse.pde.internal.core.IEnvironmentVariables; 28 import org.eclipse.pde.internal.core.PDECore; 29 import org.eclipse.pde.internal.ui.IHelpContextIds; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.PDEUIMessages; 32 import org.eclipse.pde.internal.ui.launcher.LauncherUtils; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Combo; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Group; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.ui.PlatformUI; 42 43 public class EnvironmentBlock implements IEnvironmentVariables { 44 private Combo fOSCombo; 45 private Combo fWSCombo; 46 private Combo fNLCombo; 47 private Combo fArchCombo; 48 49 private Preferences preferences; 50 private TreeSet fNLChoices; 51 private TreeSet fOSChoices; 52 private TreeSet fWSChoices; 53 private TreeSet fArchChoices; 54 private Combo fJRECombo; 55 56 private static boolean LOCALES_INITIALIZED = false; 57 private String fDefaultJRE; 58 59 public EnvironmentBlock() { 60 preferences = PDECore.getDefault().getPluginPreferences(); 61 } 62 63 private void initializeChoices() { 64 fOSChoices = new TreeSet (); 65 String [] os = Platform.knownOSValues(); 66 for (int i = 0; i < os.length; i++) 67 fOSChoices.add(os[i]); 68 addExtraChoices(fOSChoices, preferences.getString(OS_EXTRA)); 69 70 fWSChoices = new TreeSet (); 71 String [] ws = Platform.knownWSValues(); 72 for (int i = 0; i < ws.length; i++) 73 fWSChoices.add(ws[i]); 74 addExtraChoices(fWSChoices, preferences.getString(WS_EXTRA)); 75 76 fArchChoices = new TreeSet (); 77 String [] arch = Platform.knownOSArchValues(); 78 for (int i = 0; i < arch.length; i++) 79 fArchChoices.add(arch[i]); 80 addExtraChoices(fArchChoices, preferences.getString(ARCH_EXTRA)); 81 82 fNLChoices = new TreeSet (); 83 if (LOCALES_INITIALIZED) { 84 initializeAllLocales(); 85 } else { 86 fNLChoices.add(expandLocaleName(preferences.getString(NL))); 87 } 88 } 89 90 protected void updateChoices() { 91 if (LOCALES_INITIALIZED) 92 return; 93 final String current = fNLCombo.getText(); 94 try { 95 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 96 public void run(IProgressMonitor monitor) { 97 initializeAllLocales(); 98 LOCALES_INITIALIZED = true; 99 } 100 }); 101 } catch (InvocationTargetException e) { 102 PDEPlugin.log(e); 103 } catch (InterruptedException e) { 104 PDEPlugin.log(e); 105 } 106 fNLCombo.setItems((String [])fNLChoices.toArray(new String [fNLChoices.size()])); 107 fNLCombo.setText(current); 108 } 109 110 private void initializeAllLocales() { 111 String [] nl = getLocales(); 112 for (int i = 0; i < nl.length; i++) 113 fNLChoices.add(nl[i]); 114 addExtraChoices(fNLChoices, preferences.getString(NL_EXTRA)); 115 } 116 117 private void addExtraChoices(Set set, String preference) { 118 StringTokenizer tokenizer = new StringTokenizer (preference); 119 while (tokenizer.hasMoreTokens()) { 120 set.add(tokenizer.nextToken().trim()); 121 } 122 } 123 124 public Control createContents(Composite parent) { 125 Composite container = new Composite(parent, SWT.NONE); 126 GridLayout layout = new GridLayout(); 127 layout.verticalSpacing = 15; 128 container.setLayout(layout); 129 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 130 131 createTargetEnvironmentGroup(container); 132 createJREGroup(container); 133 134 Dialog.applyDialogFont(container); 135 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.TARGET_ENVIRONMENT_PREFERENCE_PAGE); 136 return container; 137 } 138 139 private void createJREGroup(Composite container) { 140 Group group = new Group(container, SWT.NULL); 141 GridLayout layout = new GridLayout(); 142 layout.numColumns = 2; 143 group.setLayout(layout); 144 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 145 group.setText(PDEUIMessages.EnvironmentBlock_jreTitle); 146 147 Label label = new Label(group, SWT.NONE); 148 label.setText(PDEUIMessages.EnvironmentBlock_jreGroup); 150 fJRECombo = new Combo(group, SWT.SINGLE|SWT.READ_ONLY); 151 fJRECombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 152 fJRECombo.setItems(LauncherUtils.getVMInstallNames()); 153 fDefaultJRE = LauncherUtils.getDefaultVMInstallName(); 154 fJRECombo.setText(fDefaultJRE); 155 156 label = new Label(group, SWT.WRAP); 157 label.setText(PDEUIMessages.EnvironmentBlock_jreNote); GridData gd = new GridData(); 159 gd.horizontalSpan = 2; 160 gd.horizontalIndent = 25; 161 gd.widthHint = 400; 162 label.setLayoutData(gd); 163 } 164 165 private void createTargetEnvironmentGroup(Composite container) { 166 Group group = new Group(container, SWT.NULL); 167 GridLayout layout = new GridLayout(); 168 layout.numColumns = 2; 169 group.setLayout(layout); 170 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 171 group.setText(PDEUIMessages.EnvironmentBlock_targetEnv); 173 initializeChoices(); 174 175 Label label = new Label(group, SWT.NULL); 176 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_os); 177 178 fOSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 179 fOSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 180 fOSCombo.setItems((String [])fOSChoices.toArray(new String [fOSChoices.size()])); 181 182 label = new Label(group, SWT.NULL); 183 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_ws); 184 185 fWSCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 186 fWSCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 187 fWSCombo.setItems((String [])fWSChoices.toArray(new String [fWSChoices.size()])); 188 189 label = new Label(group, SWT.NULL); 190 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_arch); 191 192 fArchCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 193 fArchCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 194 fArchCombo.setItems((String [])fArchChoices.toArray(new String [fArchChoices.size()])); 195 196 label = new Label(group, SWT.NULL); 197 label.setText(PDEUIMessages.Preferences_TargetEnvironmentPage_nl); 198 199 fNLCombo = new Combo(group, SWT.SINGLE | SWT.BORDER); 200 fNLCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 201 fNLCombo.setItems((String [])fNLChoices.toArray(new String [fNLChoices.size()])); 202 203 fOSCombo.setText(preferences.getString(OS)); 204 fWSCombo.setText(preferences.getString(WS)); 205 fNLCombo.setText(expandLocaleName(preferences.getString(NL))); 206 fArchCombo.setText(preferences.getString(ARCH)); 207 } 208 209 212 protected void performDefaults() { 213 fOSCombo.setText(preferences.getDefaultString(OS)); 214 fWSCombo.setText(preferences.getDefaultString(WS)); 215 fNLCombo.setText(expandLocaleName(preferences.getDefaultString(NL))); 216 fArchCombo.setText(preferences.getDefaultString(ARCH)); 217 fJRECombo.setText(LauncherUtils.getDefaultVMInstallName()); 218 } 219 220 public boolean performOk() { 221 applySelfHostingMode(); 222 applyTargetEnvironmentGroup(); 223 applyJREGroup(); 224 return true; 225 } 226 227 private void applyJREGroup() { 228 try { 229 if (!fDefaultJRE.equals(LauncherUtils.getDefaultVMInstallName())) 230 return; 231 232 if (!LauncherUtils.getDefaultVMInstallName().equals(fJRECombo.getText())) 233 JavaRuntime.setDefaultVMInstall(LauncherUtils.getVMInstall(fJRECombo.getText()), null); 234 } catch (CoreException e) { 235 } 236 } 237 238 private void applySelfHostingMode() { 239 preferences.setValue(ICoreConstants.STRICT_MODE, false); 240 } 241 242 private void applyTargetEnvironmentGroup() { 243 String os = fOSCombo.getText().trim(); 244 if (os.length() > 0) { 245 if (!fOSChoices.contains(os)) { 246 String value = preferences.getString(OS_EXTRA); 247 value = (value.length() > 0) ? value + "," + os : os; preferences.setValue(OS_EXTRA, value); 249 } 250 preferences.setValue(OS, os); 251 } 252 253 String ws = fWSCombo.getText().trim(); 254 if (ws.length() > 0) { 255 if (!fWSChoices.contains(ws)) { 256 String value = preferences.getString(WS_EXTRA); 257 value = (value.length() > 0) ? value + "," + ws : ws; preferences.setValue(WS_EXTRA, value); 259 } 260 preferences.setValue(WS, ws); 261 } 262 263 String arch = fArchCombo.getText().trim(); 264 if (arch.length() > 0) { 265 if (!fArchChoices.contains(arch)) { 266 String value = preferences.getString(ARCH_EXTRA); 267 value = (value.length() > 0) ? value + "," + arch : arch; preferences.setValue(ARCH_EXTRA, value); 269 } 270 preferences.setValue(ARCH, arch); 271 } 272 273 String locale = fNLCombo.getText().trim(); 274 if (locale.length() > 0) { 275 if (!fNLChoices.contains(locale)) { 276 String value = preferences.getString(NL_EXTRA); 277 value = (value.length() > 0) ? value + "," + locale : locale; preferences.setValue(NL_EXTRA, value); 279 } 280 int dash = locale.indexOf("-"); if (dash != -1) 282 locale = locale.substring(0, dash); 283 locale = locale.trim(); 284 preferences.setValue(NL, locale); 285 } 286 PDECore.getDefault().savePluginPreferences(); 287 } 288 289 290 private String expandLocaleName(String name) { 291 String language = ""; String country = ""; String variant = ""; 295 StringTokenizer tokenizer = new StringTokenizer (name, "_"); if (tokenizer.hasMoreTokens()) 297 language = tokenizer.nextToken(); 298 if (tokenizer.hasMoreTokens()) 299 country = tokenizer.nextToken(); 300 if (tokenizer.hasMoreTokens()) 301 variant = tokenizer.nextToken(); 302 303 Locale locale = new Locale (language, country, variant); 304 return locale.toString() + " - " + locale.getDisplayName(); } 306 307 private static String [] getLocales() { 308 Locale [] locales = Locale.getAvailableLocales(); 309 String [] result = new String [locales.length]; 310 for (int i = 0; i < locales.length; i++) { 311 Locale locale = locales[i]; 312 StringBuffer buffer = new StringBuffer (); 313 buffer.append(locale.toString()); 314 buffer.append(" - "); buffer.append(locale.getDisplayName()); 316 result[i] = buffer.toString(); 317 } 318 return result; 319 } 320 } 321 | Popular Tags |