1 11 package org.eclipse.pde.internal.ui.editor.target; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Locale ; 15 import java.util.StringTokenizer ; 16 import java.util.TreeSet ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.jface.operation.IRunnableWithProgress; 21 import org.eclipse.pde.core.IModelChangedEvent; 22 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo; 23 import org.eclipse.pde.internal.core.itarget.ITarget; 24 import org.eclipse.pde.internal.core.itarget.ITargetModel; 25 import org.eclipse.pde.internal.ui.PDEPlugin; 26 import org.eclipse.pde.internal.ui.PDEUIMessages; 27 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 28 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 29 import org.eclipse.pde.internal.ui.editor.PDESection; 30 import org.eclipse.pde.internal.ui.parts.ComboPart; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.custom.CCombo; 33 import org.eclipse.swt.events.FocusAdapter; 34 import org.eclipse.swt.events.FocusEvent; 35 import org.eclipse.swt.events.ModifyEvent; 36 import org.eclipse.swt.events.ModifyListener; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.widgets.Combo; 40 import org.eclipse.swt.widgets.Composite; 41 import org.eclipse.swt.widgets.Control; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.forms.IFormColors; 45 import org.eclipse.ui.forms.widgets.FormToolkit; 46 import org.eclipse.ui.forms.widgets.Section; 47 48 public class EnvironmentSection extends PDESection { 49 50 private ComboPart fOSCombo; 51 private ComboPart fWSCombo; 52 private ComboPart fNLCombo; 53 private ComboPart fArchCombo; 54 55 private TreeSet fNLChoices; 56 private TreeSet fOSChoices; 57 private TreeSet fWSChoices; 58 private TreeSet fArchChoices; 59 private boolean LOCALES_INITIALIZED = false; 60 61 public EnvironmentSection(PDEFormPage page, Composite parent) { 62 super(page, parent, Section.DESCRIPTION); 63 createClient(getSection(), page.getEditor().getToolkit()); 64 } 65 66 protected void createClient(Section section, FormToolkit toolkit) { 67 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 68 section.setText(PDEUIMessages.EnvironmentSection_title); 69 section.setDescription(PDEUIMessages.EnvironmentSection_description); 70 GridData data = new GridData(GridData.FILL_HORIZONTAL); 71 data.verticalAlignment = SWT.TOP; 72 data.horizontalSpan = 2; 73 section.setLayoutData(data); 74 75 Composite client = toolkit.createComposite(section); 76 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(true, 2)); 77 client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 78 79 Composite left = toolkit.createComposite(client); 80 left.setLayout(new GridLayout(2, false)); 81 GridLayout layout= FormLayoutFactory.createClearGridLayout(false, 2); 82 layout.horizontalSpacing = layout.verticalSpacing = 5; 83 left.setLayout(layout); 84 left.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 85 86 IEnvironmentInfo orgEnv = getEnvironment(); 87 initializeChoices(orgEnv); 88 89 Label label = toolkit.createLabel(left, PDEUIMessages.EnvironmentSection_operationSystem); 90 label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 91 92 fOSCombo = new ComboPart(); 93 fOSCombo.createControl(left, toolkit, SWT.SINGLE | SWT.BORDER ); 94 fOSCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 95 fOSCombo.setItems((String [])fOSChoices.toArray(new String [fOSChoices.size()])); 96 97 label = toolkit.createLabel(left, PDEUIMessages.EnvironmentSection_windowingSystem); 98 label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 99 100 fWSCombo = new ComboPart(); 101 fWSCombo.createControl(left, toolkit, SWT.SINGLE | SWT.BORDER); 102 fWSCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 103 fWSCombo.setItems((String [])fWSChoices.toArray(new String [fWSChoices.size()])); 104 105 Composite right = toolkit.createComposite(client); 106 layout= FormLayoutFactory.createClearGridLayout(false, 2); 107 layout.verticalSpacing = layout.horizontalSpacing = 5; 108 right.setLayout(layout); 109 right.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 110 111 label = toolkit.createLabel(right, PDEUIMessages.EnvironmentSection_architecture); 112 label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 113 114 fArchCombo = new ComboPart(); 115 fArchCombo.createControl(right, toolkit, SWT.SINGLE | SWT.BORDER); 116 fArchCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 117 fArchCombo.setItems((String [])fArchChoices.toArray(new String [fArchChoices.size()])); 118 119 120 label = toolkit.createLabel(right, PDEUIMessages.EnvironmentSection_locale); 121 label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 122 123 fNLCombo = new ComboPart(); 124 fNLCombo.createControl(right, toolkit, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL); 125 fNLCombo.getControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 126 fNLCombo.setItems((String [])fNLChoices.toArray(new String [fNLChoices.size()])); 127 128 refresh(); 129 130 fOSCombo.addModifyListener(new ModifyListener() { 131 public void modifyText(ModifyEvent e) { 132 getEnvironment().setOS(getText(fOSCombo)); 133 } 134 }); 135 fWSCombo.addModifyListener(new ModifyListener() { 136 public void modifyText(ModifyEvent e) { 137 getEnvironment().setWS(getText(fWSCombo)); 138 } 139 }); 140 fArchCombo.addModifyListener(new ModifyListener() { 141 public void modifyText(ModifyEvent e) { 142 getEnvironment().setArch(getText(fArchCombo)); 143 } 144 }); 145 fNLCombo.getControl().addFocusListener(new FocusAdapter() { 146 public void focusGained(FocusEvent event) { 147 if (!LOCALES_INITIALIZED) { 149 try { 150 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 151 public void run(IProgressMonitor monitor) { 152 initializeAllLocales(); 153 LOCALES_INITIALIZED = true; 154 } 155 }); 156 } catch (InvocationTargetException e) { 157 PDEPlugin.log(e); 158 } catch (InterruptedException e) { 159 PDEPlugin.log(e); 160 } 161 } 162 163 if (fNLCombo.getItemCount() < 3) { 167 String current = fNLCombo.getSelection(); 168 if (!fNLCombo.getControl().isDisposed()) { 169 fNLCombo.setItems((String [])fNLChoices.toArray(new String [fNLChoices.size()])); 170 fNLCombo.setText(current); 171 } 172 173 fNLCombo.addModifyListener(new ModifyListener() { 174 public void modifyText(ModifyEvent e) { 175 String value = getText(fNLCombo); 176 int index = value.indexOf("-"); if (index > 0) 178 value = value.substring(0, index); 179 getEnvironment().setNL(value.trim()); 180 } 181 }); 182 } 183 184 } 185 }); 186 187 toolkit.paintBordersFor(client); 188 section.setClient(client); 189 190 getModel().addModelChangedListener(this); 192 } 193 194 197 public void modelChanged(IModelChangedEvent e) { 198 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 200 handleModelEventWorldChanged(e); 201 } 202 } 203 204 207 private void handleModelEventWorldChanged(IModelChangedEvent event) { 208 refresh(); 210 } 211 212 215 public void dispose() { 216 ITargetModel model = getModel(); 217 if (model != null) { 218 model.removeModelChangedListener(this); 219 } 220 super.dispose(); 221 } 222 223 private void initializeChoices(IEnvironmentInfo orgEnv) { 224 fOSChoices = new TreeSet (); 225 String [] os = Platform.knownOSValues(); 226 for (int i = 0; i < os.length; i++) 227 fOSChoices.add(os[i]); 228 fOSChoices.add(""); String fileValue = orgEnv.getOS(); 230 if (fileValue != null) 231 fOSChoices.add(fileValue); 232 233 fWSChoices = new TreeSet (); 234 String [] ws = Platform.knownWSValues(); 235 for (int i = 0; i < ws.length; i++) 236 fWSChoices.add(ws[i]); 237 fWSChoices.add(""); fileValue = orgEnv.getWS(); 239 if (fileValue != null) 240 fWSChoices.add(fileValue); 241 242 fArchChoices = new TreeSet (); 243 String [] arch = Platform.knownOSArchValues(); 244 for (int i = 0; i < arch.length; i++) 245 fArchChoices.add(arch[i]); 246 fArchChoices.add(""); fileValue = orgEnv.getArch(); 248 if (fileValue != null) 249 fArchChoices.add(fileValue); 250 251 fNLChoices = new TreeSet (); 252 fNLChoices.add(""); } 254 255 private void initializeAllLocales() { 256 String [] nl = getLocales(); 257 for (int i = 0; i < nl.length; i++) 258 fNLChoices.add(nl[i]); 259 String fileValue = getEnvironment().getNL(); 260 if (fileValue != null) 261 fNLChoices.add(expandLocaleName(fileValue)); 262 LOCALES_INITIALIZED = true; 263 } 264 265 private static String [] getLocales() { 266 Locale [] locales = Locale.getAvailableLocales(); 267 String [] result = new String [locales.length]; 268 for (int i = 0; i < locales.length; i++) { 269 Locale locale = locales[i]; 270 StringBuffer buffer = new StringBuffer (); 271 buffer.append(locale.toString()); 272 buffer.append(" - "); buffer.append(locale.getDisplayName()); 274 result[i] = buffer.toString(); 275 } 276 return result; 277 } 278 279 private String expandLocaleName(String name) { 280 String language = ""; String country = ""; String variant = ""; 284 StringTokenizer tokenizer = new StringTokenizer (name, "_"); if (tokenizer.hasMoreTokens()) 286 language = tokenizer.nextToken(); 287 if (tokenizer.hasMoreTokens()) 288 country = tokenizer.nextToken(); 289 if (tokenizer.hasMoreTokens()) 290 variant = tokenizer.nextToken(); 291 292 Locale locale = new Locale (language, country, variant); 293 return locale.toString() + " - " + locale.getDisplayName(); } 295 296 private String getText(ComboPart combo) { 297 Control control = combo.getControl(); 298 if (control instanceof Combo) 299 return ((Combo) control).getText(); 300 return ((CCombo) control).getText(); 301 } 302 303 private IEnvironmentInfo getEnvironment() { 304 IEnvironmentInfo info = getTarget().getEnvironment(); 305 if (info == null) { 306 info = getModel().getFactory().createEnvironment(); 307 getTarget().setEnvironment(info); 308 } 309 return info; 310 } 311 312 private ITarget getTarget() { 313 return getModel().getTarget(); 314 } 315 316 private ITargetModel getModel() { 317 return (ITargetModel)getPage().getPDEEditor().getAggregateModel(); 318 } 319 320 public void refresh() { 321 IEnvironmentInfo orgEnv = getEnvironment(); 322 String presetValue = (orgEnv.getOS() == null) ? "" : orgEnv.getOS(); fOSCombo.setText(presetValue); 324 presetValue = (orgEnv.getWS() == null) ? "" : orgEnv.getWS(); fWSCombo.setText(presetValue); 326 presetValue = (orgEnv.getArch() == null) ? "" : orgEnv.getArch(); fArchCombo.setText(presetValue); 328 presetValue = (orgEnv.getNL() == null) ? "" : expandLocaleName(orgEnv.getNL()); fNLCombo.setText(presetValue); 330 331 super.refresh(); 332 } 333 334 protected void updateChoices() { 335 if (LOCALES_INITIALIZED) 336 return; 337 if (getModel() == null) 339 return; 340 new Thread (new Runnable () { 342 public void run() { 343 initializeAllLocales(); 344 } 345 }).start(); 346 } 347 348 } 349 | Popular Tags |