1
/*******************************************************************************
2
* Copyright (c) 2000, 2008 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
9
* IBM Corporation - initial API and implementation
10
* Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently
11
* Sergey Prigogin, Google
12
* Anton Leherbauer (Wind River Systems)
13
*******************************************************************************/
14
package org.eclipse.cdt.internal.ui.preferences.formatter;
16
import java.util.List;
17
import java.util.Observable;
18
import java.util.Observer;
20
import org.eclipse.core.resources.IProject;
21
import org.eclipse.core.runtime.preferences.IScopeContext;
22
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.cdt.ui.CUIPlugin;
28
import org.eclipse.cdt.internal.ui.preferences.PreferencesAccess;
29
import org.eclipse.cdt.internal.ui.preferences.formatter.ProfileManager.Profile;
32
* The code formatter preference page.
34
public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock {
36
private static final String FORMATTER_DIALOG_PREFERENCE_KEY= "formatter_page"; //$NON-NLS-1$
38
private static final String DIALOGSTORE_LASTSAVELOADPATH= CUIPlugin.PLUGIN_ID + ".codeformatter.savepath"; //$NON-NLS-1$
40
private class PreviewController implements Observer {
42
public PreviewController(ProfileManager profileManager) {
43
profileManager.addObserver(this);
44
fCustomCodeFormatterBlock.addObserver(this);
45
fCodeStylePreview.setWorkingValues(profileManager.getSelected().getSettings());
46
fCodeStylePreview.update();
49
public void update(Observable o, Object arg) {
50
if (o == fCustomCodeFormatterBlock) {
51
fCodeStylePreview.setFormatterId((String)arg);
52
fCodeStylePreview.update();
55
final int value= ((Integer)arg).intValue();
57
case ProfileManager.PROFILE_CREATED_EVENT:
58
case ProfileManager.PROFILE_DELETED_EVENT:
59
case ProfileManager.SELECTION_CHANGED_EVENT:
60
case ProfileManager.SETTINGS_CHANGED_EVENT:
61
fCodeStylePreview.setWorkingValues(((ProfileManager)o).getSelected().getSettings());
62
fCodeStylePreview.update();
68
* Some C++ source code used for preview.
70
private final static String PREVIEW=
71
"/*\n* " + //$NON-NLS-1$
72
FormatterMessages.CodingStyleConfigurationBlock_preview_title +
73
"\n*/\n" + //$NON-NLS-1$
74
"#include <math.h>\n\n" + //$NON-NLS-1$
75
"class Point {" + //$NON-NLS-1$
76
"public:" + //$NON-NLS-1$
77
"Point(double x, double y) : x(x), y(y) {}" + //$NON-NLS-1$
78
"double distance(const Point& other) const;" + //$NON-NLS-1$
79
"\n\n" + //$NON-NLS-1$
80
"double x;" + //$NON-NLS-1$
81
"double y;" + //$NON-NLS-1$
83
"\n\n" + //$NON-NLS-1$
84
"double Point::distance(const Point& other) const {" + //$NON-NLS-1$
85
"double dx = x - other.x;" + //$NON-NLS-1$
86
"double dy = y - other.y;" + //$NON-NLS-1$
87
"return sqrt(dx * dx + dy * dy);" + //$NON-NLS-1$
93
protected TranslationUnitPreview fCodeStylePreview;
95
protected CustomCodeFormatterBlock fCustomCodeFormatterBlock;
97
* Create a new <code>CodeFormatterConfigurationBlock</code>.
99
public CodeFormatterConfigurationBlock(IProject project, PreferencesAccess access) {
100
super(project, access, DIALOGSTORE_LASTSAVELOADPATH);
101
fCustomCodeFormatterBlock= new CustomCodeFormatterBlock(project, access);
105
protected IProfileVersioner createProfileVersioner() {
106
return new ProfileVersioner();
110
protected ProfileStore createProfileStore(IProfileVersioner versioner) {
111
return new FormatterProfileStore(versioner);
115
protected ProfileManager createProfileManager(List<Profile> profiles, IScopeContext context, PreferencesAccess access, IProfileVersioner profileVersioner) {
116
return new FormatterProfileManager(profiles, context, access, profileVersioner);
120
protected void configurePreview(Composite composite, int numColumns, ProfileManager profileManager) {
121
fCustomCodeFormatterBlock.createContents(composite);
123
createLabel(composite, FormatterMessages.CodingStyleConfigurationBlock_preview_label_text, numColumns);
124
TranslationUnitPreview result= new TranslationUnitPreview(profileManager.getSelected().getSettings(), composite);
125
result.setFormatterId(fCustomCodeFormatterBlock.getFormatterId());
126
result.setPreviewText(PREVIEW);
127
fCodeStylePreview= result;
129
final GridData gd = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL);
130
gd.horizontalSpan = numColumns;
134
fCodeStylePreview.getControl().setLayoutData(gd);
136
new PreviewController(profileManager);
140
protected ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile) {
141
return new FormatterModifyDialog(shell, profile, profileManager, profileStore, newProfile, FORMATTER_DIALOG_PREFERENCE_KEY, DIALOGSTORE_LASTSAVELOADPATH);
145
* @see org.eclipse.cdt.internal.ui.preferences.formatter.ProfileConfigurationBlock#performApply()
148
public void performApply() {
149
if (fCustomCodeFormatterBlock != null) {
150
fCustomCodeFormatterBlock.performOk();
152
super.performApply();
156
* @see org.eclipse.cdt.internal.ui.preferences.formatter.ProfileConfigurationBlock#performDefaults()
159
public void performDefaults() {
160
if (fCustomCodeFormatterBlock != null) {
161
fCustomCodeFormatterBlock.performDefaults();
163
super.performDefaults();
167
* @see org.eclipse.cdt.internal.ui.preferences.formatter.ProfileConfigurationBlock#performOk()
170
public boolean performOk() {
171
if (fCustomCodeFormatterBlock != null) {
172
fCustomCodeFormatterBlock.performOk();
174
return super.performOk();