001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide;
011:
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.ProjectScope;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
020: import org.eclipse.core.runtime.preferences.IScopeContext;
021: import org.eclipse.core.runtime.preferences.InstanceScope;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.SelectionAdapter;
024: import org.eclipse.swt.events.SelectionEvent;
025: import org.eclipse.swt.graphics.Font;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Button;
029: import org.eclipse.swt.widgets.Combo;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Group;
033: import org.osgi.service.prefs.BackingStoreException;
034:
035: /**
036: * A class to handle editing of the Line delimiter preferences in core.
037: *
038: * @since 3.1
039: */
040: public class LineDelimiterEditor {
041:
042: private Button defaultButton;
043:
044: private Button otherButton;
045:
046: private Combo choiceCombo;
047:
048: /*
049: * The project whose preferences should be set. In some cases this class
050: * will be used to edit project preferences.
051: */
052: private IProject project;
053:
054: private Group group;
055:
056: /**
057: * Creates a new encoding field editor with the given preference name, label
058: * and parent.
059: *
060: * @param composite
061: * the parent of the field editor's control
062: */
063: public LineDelimiterEditor(Composite composite) {
064: this (composite, null);
065: }
066:
067: /**
068: * Creates a new encoding field editor with the given preference name, label
069: * and parent.
070: *
071: * @param composite
072: * the parent of the field editor's control
073: * @param project
074: * the project to set preferences on
075: *
076: */
077: public LineDelimiterEditor(Composite composite, IProject project) {
078: this .project = project;
079: createControl(composite);
080: }
081:
082: /**
083: * Creates this field editor's main control containing all of its basic
084: * controls.
085: *
086: * @param parent
087: * the parent control
088: */
089: protected void createControl(Composite parent) {
090: Font font = parent.getFont();
091: group = new Group(parent, SWT.NONE);
092: GridData data = new GridData(GridData.FILL_HORIZONTAL);
093: group.setLayoutData(data);
094: GridLayout layout = new GridLayout();
095: layout.numColumns = 2;
096: group.setLayout(layout);
097: group
098: .setText(IDEWorkbenchMessages.IDEWorkspacePreference_fileLineDelimiter);
099: 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
114: .setText(IDEWorkbenchMessages.IDEWorkspacePreference_defaultLineDelim);
115: } else {
116: defaultButton
117: .setText(IDEWorkbenchMessages.IDEWorkspacePreference_defaultLineDelimProj);
118: }
119:
120: data = new GridData();
121: data.horizontalSpan = 2;
122: defaultButton.setLayoutData(data);
123: defaultButton.addSelectionListener(buttonListener);
124: defaultButton.setFont(font);
125:
126: otherButton = new Button(group, SWT.RADIO);
127: otherButton
128: .setText(IDEWorkbenchMessages.IDEWorkspacePreference_otherLineDelim);
129: otherButton.addSelectionListener(buttonListener);
130: otherButton.setFont(font);
131:
132: choiceCombo = new Combo(group, SWT.NONE | SWT.READ_ONLY);
133: data = new GridData();
134: choiceCombo.setFont(font);
135: choiceCombo.setLayoutData(data);
136: populateChoiceCombo(getChoices());
137: }
138:
139: /**
140: * Load the list items from core and update the state of the buttons to
141: * match what the preference is currently set to.
142: */
143: public void doLoad() {
144: if (choiceCombo != null) {
145: populateChoiceCombo(getChoices());
146: String resourcePreference = getStoredValue();
147: updateState(resourcePreference == null);
148: }
149: }
150:
151: /**
152: * Initializes this field editor with the preference value from the
153: * preference store.
154: */
155: public void loadDefault() {
156: if (choiceCombo != null) {
157: updateState(true);
158: }
159: }
160:
161: /**
162: * Returns the value that is currently stored for the encoding.
163: *
164: * @return the currently stored encoding
165: */
166: public String getStoredValue() {
167: IScopeContext[] scopeContext = new IScopeContext[] { getScopeContext() };
168: IEclipsePreferences node = scopeContext[0]
169: .getNode(Platform.PI_RUNTIME);
170: return node.get(Platform.PREF_LINE_SEPARATOR, null);
171: }
172:
173: /**
174: * Answer the <code>IScopeContext</code> for the receiver, this will be a
175: * project scope if the receiver is editing project preferences, otherwise
176: * instance scope.
177: *
178: * @return the scope context
179: */
180: private IScopeContext getScopeContext() {
181: if (project != null) {
182: return new ProjectScope(project);
183: }
184:
185: return new InstanceScope();
186: }
187:
188: /**
189: * Returns the default setting for the object being shown.
190: *
191: * @return the default setting for the object being shown
192: */
193: protected String[] getChoices() {
194: Set keys = Platform.knownPlatformLineSeparators().keySet();
195: String[] keyArray = new String[keys.size()];
196: keys.toArray(keyArray);
197: return keyArray;
198: }
199:
200: /**
201: * Populates the list of choices combo.
202: */
203: private void populateChoiceCombo(String[] items) {
204: choiceCombo.setItems(items);
205:
206: if (getStoredValue() == null) {
207: choiceCombo.setText(""); //$NON-NLS-1$
208: } else {
209: selectChoice();
210: }
211: }
212:
213: private void updateState(boolean useDefault) {
214: if (useDefault) {
215: defaultButton.setSelection(true);
216: otherButton.setSelection(false);
217: choiceCombo.setEnabled(false);
218: } else {
219: defaultButton.setSelection(false);
220: otherButton.setSelection(true);
221: choiceCombo.setEnabled(true);
222: selectChoice();
223: }
224: }
225:
226: /**
227: * Select the item in the combo that matches the current preferences
228: * setting. NOTE: not handling the case where two platform line separators
229: * are defined with the same value. Assumption is that they are unique and
230: * the key will be modified to represent that. E.g. a key might be Mac OS
231: * 10/Linux if the same value is required for two platforms.
232: */
233: private void selectChoice() {
234: String selection = null;
235: Map knownValues = Platform.knownPlatformLineSeparators();
236: Set keys = knownValues.keySet();
237: String current = getStoredValue();
238: if (current != null) {
239: for (Iterator iter = keys.iterator(); iter.hasNext();) {
240: String element = (String) iter.next();
241: if (knownValues.get(element).equals(current)) {
242: selection = element;
243: break;
244: }
245: }
246: }
247: String[] items = choiceCombo.getItems();
248: for (int i = 0; i < items.length; i++) {
249: String item = items[i];
250: if (item.equals(selection)) {
251: choiceCombo.select(i);
252: break;
253: }
254: }
255: }
256:
257: /**
258: * Store the currently selected line delimiter value in the preference
259: * store.
260: */
261: public void store() {
262: String val;
263: if (defaultButton.getSelection()
264: || choiceCombo.getText().equals("")) { //$NON-NLS-1$
265: val = null;
266: } else {
267: Map lineSeparators = Platform.knownPlatformLineSeparators();
268: val = (String) lineSeparators.get(choiceCombo.getText());
269: }
270:
271: IEclipsePreferences node = getScopeContext().getNode(
272: Platform.PI_RUNTIME);
273: if (val == null) {
274: node.remove(Platform.PREF_LINE_SEPARATOR);
275: } else {
276: node.put(Platform.PREF_LINE_SEPARATOR, val);
277: }
278: try {
279: node.flush();
280: } catch (BackingStoreException e) {
281: IDEWorkbenchPlugin.log(e.getMessage(), e);
282: }
283:
284: }
285:
286: /**
287: * Set whether or not the controls in the field editor
288: * are enabled.
289: * @param enabled The enabled state.
290: */
291: public void setEnabled(boolean enabled) {
292: group.setEnabled(enabled);
293: Control[] children = group.getChildren();
294: for (int i = 0; i < children.length; i++) {
295: children[i].setEnabled(enabled);
296:
297: }
298: }
299:
300: }
|