KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > LineDelimiterEditor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
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 /**
37  * A class to handle editing of the Line delimiter preferences in core.
38  *
39  * @since 3.1
40  */

41 public class LineDelimiterEditor {
42
43     private Button defaultButton;
44
45     private Button otherButton;
46
47     private Combo choiceCombo;
48
49     /*
50      * The project whose preferences should be set. In some cases this class
51      * will be used to edit project preferences.
52      */

53     private IProject project;
54
55     private Group group;
56
57     /**
58      * Creates a new encoding field editor with the given preference name, label
59      * and parent.
60      *
61      * @param composite
62      * the parent of the field editor's control
63      */

64     public LineDelimiterEditor(Composite composite) {
65         this(composite, null);
66     }
67
68     /**
69      * Creates a new encoding field editor with the given preference name, label
70      * and parent.
71      *
72      * @param composite
73      * the parent of the field editor's control
74      * @param project
75      * the project to set preferences on
76      *
77      */

78     public LineDelimiterEditor(Composite composite, IProject project) {
79         this.project = project;
80         createControl(composite);
81     }
82
83     /**
84      * Creates this field editor's main control containing all of its basic
85      * controls.
86      *
87      * @param parent
88      * the parent control
89      */

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     /**
138      * Load the list items from core and update the state of the buttons to
139      * match what the preference is currently set to.
140      */

141     public void doLoad() {
142         if (choiceCombo != null) {
143             populateChoiceCombo(getChoices());
144             String JavaDoc resourcePreference = getStoredValue();
145             updateState(resourcePreference == null);
146         }
147     }
148
149     /**
150      * Initializes this field editor with the preference value from the
151      * preference store.
152      */

153     public void loadDefault() {
154         if(choiceCombo != null) {
155             updateState(true);
156         }
157     }
158
159     /**
160      * Returns the value that is currently stored for the encoding.
161      *
162      * @return the currently stored encoding
163      */

164     public String JavaDoc 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     /**
171      * Answer the <code>IScopeContext</code> for the receiver, this will be a
172      * project scope if the receiver is editing project preferences, otherwise
173      * instance scope.
174      *
175      * @return the scope context
176      */

177     private IScopeContext getScopeContext() {
178         if (project != null) {
179             return new ProjectScope(project);
180         }
181
182         return new InstanceScope();
183     }
184
185     /**
186      * Returns the default setting for the object being shown.
187      *
188      * @return the default setting for the object being shown
189      */

190     protected String JavaDoc[] getChoices() {
191         Set JavaDoc keys = Platform.knownPlatformLineSeparators().keySet();
192         String JavaDoc[] keyArray = new String JavaDoc[keys.size()];
193         keys.toArray(keyArray);
194         return keyArray;
195     }
196
197     /**
198      * Populates the list of choices combo.
199      */

200     private void populateChoiceCombo(String JavaDoc[] items) {
201         choiceCombo.setItems(items);
202
203         if (getStoredValue() == null) {
204             choiceCombo.setText(""); //$NON-NLS-1$
205
} 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     /**
224      * Select the item in the combo that matches the current preferences
225      * setting. NOTE: not handling the case where two platform line separators
226      * are defined with the same value. Assumption is that they are unique and
227      * the key will be modified to represent that. E.g. a key might be Mac OS
228      * 10/Linux if the same value is required for two platforms.
229      */

230     private void selectChoice() {
231         String JavaDoc selection = null;
232         Map JavaDoc knownValues = Platform.knownPlatformLineSeparators();
233         Set JavaDoc keys = knownValues.keySet();
234         String JavaDoc current = getStoredValue();
235         if (current != null) {
236             for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
237                 String JavaDoc element = (String JavaDoc) iter.next();
238                 if (knownValues.get(element).equals(current)) {
239                     selection = element;
240                     break;
241                 }
242             }
243         }
244         String JavaDoc[] items = choiceCombo.getItems();
245         for (int i = 0; i < items.length; i++) {
246             String JavaDoc item = items[i];
247             if (item.equals(selection)) {
248                 choiceCombo.select(i);
249                 break;
250             }
251         }
252     }
253
254     /**
255      * Store the currently selected line delimiter value in the preference
256      * store.
257      */

258     public void store() {
259         String JavaDoc val;
260         if (defaultButton.getSelection() || choiceCombo.getText().equals("")) { //$NON-NLS-1$
261
val = null;
262         } else {
263             Map JavaDoc lineSeparators = Platform.knownPlatformLineSeparators();
264             val = (String JavaDoc) 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      /**
283      * Set whether or not the controls in the field editor
284      * are enabled.
285      * @param enabled The enabled state.
286      */

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