1 11 package org.eclipse.ui.internal.ide; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.ProjectScope; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 21 import org.eclipse.core.runtime.preferences.IScopeContext; 22 import org.eclipse.core.runtime.preferences.InstanceScope; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.graphics.Font; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.layout.GridLayout; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Combo; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Group; 34 import org.osgi.service.prefs.BackingStoreException; 35 36 41 public class LineDelimiterEditor { 42 43 private Button defaultButton; 44 45 private Button otherButton; 46 47 private Combo choiceCombo; 48 49 53 private IProject project; 54 55 private Group group; 56 57 64 public LineDelimiterEditor(Composite composite) { 65 this(composite, null); 66 } 67 68 78 public LineDelimiterEditor(Composite composite, IProject project) { 79 this.project = project; 80 createControl(composite); 81 } 82 83 90 protected void createControl(Composite parent) { 91 Font font = parent.getFont(); 92 group = new Group(parent, SWT.NONE); 93 GridData data = new GridData(GridData.FILL_HORIZONTAL); 94 group.setLayoutData(data); 95 GridLayout layout = new GridLayout(); 96 layout.numColumns = 2; 97 group.setLayout(layout); 98 group.setText(IDEWorkbenchMessages.IDEWorkspacePreference_fileLineDelimiter); 99 group.setFont(font); 100 101 SelectionAdapter buttonListener = new SelectionAdapter() { 102 public void widgetSelected(SelectionEvent e) { 103 if (e.widget.equals(defaultButton)) { 104 updateState(true); 105 } else { 106 updateState(false); 107 } 108 } 109 }; 110 111 defaultButton = new Button(group, SWT.RADIO); 112 if (project == null) { 113 defaultButton.setText(IDEWorkbenchMessages.IDEWorkspacePreference_defaultLineDelim); 114 } else { 115 defaultButton.setText(IDEWorkbenchMessages.IDEWorkspacePreference_defaultLineDelimProj); 116 } 117 118 data = new GridData(); 119 data.horizontalSpan = 2; 120 defaultButton.setLayoutData(data); 121 defaultButton.addSelectionListener(buttonListener); 122 defaultButton.setFont(font); 123 124 otherButton = new Button(group, SWT.RADIO); 125 otherButton 126 .setText(IDEWorkbenchMessages.IDEWorkspacePreference_otherLineDelim); 127 otherButton.addSelectionListener(buttonListener); 128 otherButton.setFont(font); 129 130 choiceCombo = new Combo(group, SWT.NONE | SWT.READ_ONLY); 131 data = new GridData(); 132 choiceCombo.setFont(font); 133 choiceCombo.setLayoutData(data); 134 populateChoiceCombo(getChoices()); 135 } 136 137 141 public void doLoad() { 142 if (choiceCombo != null) { 143 populateChoiceCombo(getChoices()); 144 String resourcePreference = getStoredValue(); 145 updateState(resourcePreference == null); 146 } 147 } 148 149 153 public void loadDefault() { 154 if(choiceCombo != null) { 155 updateState(true); 156 } 157 } 158 159 164 public String getStoredValue() { 165 IScopeContext[] scopeContext = new IScopeContext[] { getScopeContext() }; 166 IEclipsePreferences node = scopeContext[0].getNode(Platform.PI_RUNTIME); 167 return node.get(Platform.PREF_LINE_SEPARATOR, null); 168 } 169 170 177 private IScopeContext getScopeContext() { 178 if (project != null) { 179 return new ProjectScope(project); 180 } 181 182 return new InstanceScope(); 183 } 184 185 190 protected String [] getChoices() { 191 Set keys = Platform.knownPlatformLineSeparators().keySet(); 192 String [] keyArray = new String [keys.size()]; 193 keys.toArray(keyArray); 194 return keyArray; 195 } 196 197 200 private void populateChoiceCombo(String [] items) { 201 choiceCombo.setItems(items); 202 203 if (getStoredValue() == null) { 204 choiceCombo.setText(""); } else { 206 selectChoice(); 207 } 208 } 209 210 private void updateState(boolean useDefault) { 211 if (useDefault) { 212 defaultButton.setSelection(true); 213 otherButton.setSelection(false); 214 choiceCombo.setEnabled(false); 215 } else { 216 defaultButton.setSelection(false); 217 otherButton.setSelection(true); 218 choiceCombo.setEnabled(true); 219 selectChoice(); 220 } 221 } 222 223 230 private void selectChoice() { 231 String selection = null; 232 Map knownValues = Platform.knownPlatformLineSeparators(); 233 Set keys = knownValues.keySet(); 234 String current = getStoredValue(); 235 if (current != null) { 236 for (Iterator iter = keys.iterator(); iter.hasNext();) { 237 String element = (String ) iter.next(); 238 if (knownValues.get(element).equals(current)) { 239 selection = element; 240 break; 241 } 242 } 243 } 244 String [] items = choiceCombo.getItems(); 245 for (int i = 0; i < items.length; i++) { 246 String item = items[i]; 247 if (item.equals(selection)) { 248 choiceCombo.select(i); 249 break; 250 } 251 } 252 } 253 254 258 public void store() { 259 String val; 260 if (defaultButton.getSelection() || choiceCombo.getText().equals("")) { val = null; 262 } else { 263 Map lineSeparators = Platform.knownPlatformLineSeparators(); 264 val = (String ) lineSeparators.get(choiceCombo.getText()); 265 } 266 267 IEclipsePreferences node = getScopeContext().getNode( 268 Platform.PI_RUNTIME); 269 if (val == null) { 270 node.remove(Platform.PREF_LINE_SEPARATOR); 271 } else { 272 node.put(Platform.PREF_LINE_SEPARATOR, val); 273 } 274 try { 275 node.flush(); 276 } catch (BackingStoreException e) { 277 IDEWorkbenchPlugin.log(e.getMessage(), e); 278 } 279 280 } 281 282 287 public void setEnabled(boolean enabled) { 288 group.setEnabled(enabled); 289 Control [] children = group.getChildren(); 290 for (int i = 0; i < children.length; i++) { 291 children[i].setEnabled(enabled); 292 293 } 294 } 295 296 } 297 | Popular Tags |