1
/*******************************************************************************
2
* Copyright (c) 2000, 2008 QNX Software Systems 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
9
* QNX Software Systems - Initial API and implementation
10
* Sergey Prigogin, Google
11
* Anton Leherbauer (Wind River Systems)
12
*******************************************************************************/
14
package org.eclipse.cdt.internal.ui.preferences.formatter;
16
import java.util.HashMap;
17
import java.util.Iterator;
19
import java.util.Observable;
21
import org.eclipse.core.resources.IProject;
22
import org.eclipse.core.runtime.IConfigurationElement;
23
import org.eclipse.core.runtime.IExtension;
24
import org.eclipse.core.runtime.IExtensionPoint;
25
import org.eclipse.core.runtime.Platform;
26
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
27
import org.eclipse.core.runtime.preferences.IScopeContext;
28
import org.eclipse.jface.resource.JFaceResources;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.events.SelectionAdapter;
31
import org.eclipse.swt.events.SelectionEvent;
32
import org.eclipse.swt.layout.GridData;
33
import org.eclipse.swt.widgets.Combo;
34
import org.eclipse.swt.widgets.Composite;
35
import org.eclipse.swt.widgets.Control;
36
import org.eclipse.ui.PlatformUI;
38
import org.eclipse.cdt.core.CCorePlugin;
39
import org.eclipse.cdt.core.CCorePreferenceConstants;
40
import org.eclipse.cdt.ui.CUIPlugin;
41
import org.eclipse.cdt.utils.ui.controls.ControlFactory;
43
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
44
import org.eclipse.cdt.internal.ui.preferences.PreferencesAccess;
47
* Allows to choose the formatter in a combo box.
48
* If no formatter is contributed, nothing is shown.
50
public class CustomCodeFormatterBlock extends Observable {
52
private HashMap<String, String> idMap = new HashMap<String, String>();
53
private IEclipsePreferences fPrefs;
54
private String fDefaultFormatterId;
55
private Combo fFormatterCombo;
56
private static final String ATTR_NAME = "name"; //$NON-NLS-1$
57
private static final String ATTR_ID = "id"; //$NON-NLS-1$
58
private static final String DEFAULT = FormatterMessages.CustomCodeFormatterBlock_default_formatter;
60
public CustomCodeFormatterBlock(IProject project, PreferencesAccess access) {
61
final IScopeContext scope;
62
final IEclipsePreferences defaults;
63
if (project != null) {
64
scope= access.getProjectScope(project);
65
defaults= access.getInstanceScope().getNode(CCorePlugin.PLUGIN_ID);
67
scope= access.getInstanceScope();
68
defaults= access.getDefaultScope().getNode(CCorePlugin.PLUGIN_ID);
70
fPrefs= scope.getNode(CCorePlugin.PLUGIN_ID);
71
fDefaultFormatterId= defaults.get(CCorePreferenceConstants.CODE_FORMATTER, null);
72
if (fDefaultFormatterId == null) {
73
// backward compatibility: use UI prefs
74
IEclipsePreferences instance= access.getInstanceScope().getNode(CUIPlugin.PLUGIN_ID);
75
fDefaultFormatterId= instance.get(CCorePreferenceConstants.CODE_FORMATTER, null);
76
if (fDefaultFormatterId != null) {
77
instance.remove(CCorePreferenceConstants.CODE_FORMATTER);
78
if (project != null) {
79
defaults.put(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId);
83
initializeFormatters();
86
public void performOk() {
87
if (fFormatterCombo == null) {
90
String text = fFormatterCombo.getText();
91
String formatterId = idMap.get(text);
92
if (formatterId != null && !formatterId.equals(fDefaultFormatterId)) {
93
fPrefs.put(CCorePreferenceConstants.CODE_FORMATTER, formatterId);
95
// simply reset to the default one.
100
public void performDefaults() {
101
fPrefs.remove(CCorePreferenceConstants.CODE_FORMATTER);
103
if (fFormatterCombo == null) {
106
fFormatterCombo.clearSelection();
107
fFormatterCombo.setText(DEFAULT);
108
Iterator<Map.Entry<String,String>> iterator = idMap.entrySet().iterator();
109
while (iterator.hasNext()) {
110
Map.Entry<String,String> entry = iterator.next();
111
String val = entry.getValue();
112
if (val != null && val.equals(fDefaultFormatterId)) {
113
fFormatterCombo.setText(entry.getKey());
116
handleFormatterChanged();
121
* Get the currently selected formatter id.
123
* @return the selected formatter id or <code>null</code> if the default is selected.
125
public String getFormatterId() {
126
if (fFormatterCombo == null) {
127
return fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId);
129
String formatterId= idMap.get(fFormatterCombo.getText());
134
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
136
public Control createContents(Composite parent) {
137
if (getNumberOfAvailableFormatters() == 0) {
140
Composite composite = ControlFactory.createGroup(parent, FormatterMessages.CustomCodeFormatterBlock_formatter_name, 1);
141
((GridData)composite.getLayoutData()).horizontalSpan = 5;
143
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ICHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
145
fFormatterCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
146
fFormatterCombo.setFont(parent.getFont());
147
fFormatterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
148
fFormatterCombo.addSelectionListener(new SelectionAdapter() {
150
public void widgetSelected(SelectionEvent e) {
151
handleFormatterChanged();
154
Iterator<String> items = idMap.keySet().iterator();
155
while (items.hasNext()) {
156
fFormatterCombo.add(items.next());
159
final String noteTitle= FormatterMessages.CustomCodeFormatterBlock_formatter_note;
160
final String noteMessage= FormatterMessages.CustomCodeFormatterBlock_contributed_formatter_warning;
161
ControlFactory.createNoteComposite(JFaceResources.getDialogFont(), composite, noteTitle, noteMessage);
168
private void handleFormatterChanged() {
170
String formatterId= getFormatterId();
171
notifyObservers(formatterId);
174
private void initDefault() {
175
boolean init = false;
176
String formatterID= fPrefs.get(CCorePreferenceConstants.CODE_FORMATTER, fDefaultFormatterId);
177
if (formatterID != null) {
178
Iterator<Map.Entry<String,String>> iterator = idMap.entrySet().iterator();
179
while (iterator.hasNext()) {
180
Map.Entry<String, String> entry = iterator.next();
181
String val = entry.getValue();
182
if (val != null && val.equals(formatterID)) {
183
fFormatterCombo.setText(entry.getKey());
190
fFormatterCombo.setText(DEFAULT);
194
private void initializeFormatters() {
195
idMap = new HashMap<String, String>();
196
idMap.put(DEFAULT, CCorePreferenceConstants.DEFAULT_CODE_FORMATTER);
197
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(CCorePlugin.PLUGIN_ID, CCorePlugin.FORMATTER_EXTPOINT_ID);
199
IExtension[] exts = point.getExtensions();
200
for (IExtension ext : exts) {
201
IConfigurationElement[] elements = ext.getConfigurationElements();
202
for (int j = 0; j < elements.length; ++j) {
203
String name = elements[j].getAttribute(ATTR_NAME);
204
String id= elements[j].getAttribute(ATTR_ID);
211
private final int getNumberOfAvailableFormatters() {
212
return idMap.size() - 1;