~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
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;
 
15
 
 
16
import java.util.List;
 
17
import java.util.Observable;
 
18
import java.util.Observer;
 
19
 
 
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;
 
25
 
 
26
import org.eclipse.cdt.ui.CUIPlugin;
 
27
 
 
28
import org.eclipse.cdt.internal.ui.preferences.PreferencesAccess;
 
29
import org.eclipse.cdt.internal.ui.preferences.formatter.ProfileManager.Profile;
 
30
 
 
31
/**
 
32
 * The code formatter preference page. 
 
33
 */
 
34
public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock {
 
35
    
 
36
    private static final String FORMATTER_DIALOG_PREFERENCE_KEY= "formatter_page"; //$NON-NLS-1$
 
37
 
 
38
    private static final String DIALOGSTORE_LASTSAVELOADPATH= CUIPlugin.PLUGIN_ID + ".codeformatter.savepath"; //$NON-NLS-1$
 
39
 
 
40
        private class PreviewController implements Observer {
 
41
 
 
42
                public PreviewController(ProfileManager profileManager) {
 
43
                        profileManager.addObserver(this);
 
44
                        fCustomCodeFormatterBlock.addObserver(this);
 
45
                        fCodeStylePreview.setWorkingValues(profileManager.getSelected().getSettings());
 
46
                        fCodeStylePreview.update();
 
47
                }
 
48
                
 
49
                public void update(Observable o, Object arg) {
 
50
                        if (o == fCustomCodeFormatterBlock) {
 
51
                                fCodeStylePreview.setFormatterId((String)arg);
 
52
                                fCodeStylePreview.update();
 
53
                                return;
 
54
                        }
 
55
                        final int value= ((Integer)arg).intValue();
 
56
                        switch (value) {
 
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();
 
63
                        }
 
64
                }
 
65
        }
 
66
        
 
67
        /**
 
68
         * Some C++ source code used for preview.
 
69
         */
 
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$
 
82
                "};" +  //$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$
 
88
                "}"; //$NON-NLS-1$
 
89
 
 
90
        /**
 
91
         * The CPreview.
 
92
         */
 
93
        protected TranslationUnitPreview fCodeStylePreview;
 
94
        
 
95
        protected CustomCodeFormatterBlock fCustomCodeFormatterBlock;
 
96
        /**
 
97
         * Create a new <code>CodeFormatterConfigurationBlock</code>.
 
98
         */
 
99
        public CodeFormatterConfigurationBlock(IProject project, PreferencesAccess access) {
 
100
                super(project, access, DIALOGSTORE_LASTSAVELOADPATH);
 
101
                fCustomCodeFormatterBlock= new CustomCodeFormatterBlock(project, access);
 
102
        }
 
103
 
 
104
        @Override
 
105
        protected IProfileVersioner createProfileVersioner() {
 
106
            return new ProfileVersioner();
 
107
    }
 
108
        
 
109
        @Override
 
110
        protected ProfileStore createProfileStore(IProfileVersioner versioner) {
 
111
            return new FormatterProfileStore(versioner);
 
112
    }
 
113
        
 
114
        @Override
 
115
        protected ProfileManager createProfileManager(List<Profile> profiles, IScopeContext context, PreferencesAccess access, IProfileVersioner profileVersioner) {
 
116
            return new FormatterProfileManager(profiles, context, access, profileVersioner);
 
117
    }
 
118
        
 
119
        @Override
 
120
        protected void configurePreview(Composite composite, int numColumns, ProfileManager profileManager) {
 
121
                fCustomCodeFormatterBlock.createContents(composite);
 
122
 
 
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;
 
128
 
 
129
                final GridData gd = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL);
 
130
                gd.horizontalSpan = numColumns;
 
131
                gd.verticalSpan= 7;
 
132
                gd.widthHint = 0;
 
133
                gd.heightHint = 0;
 
134
                fCodeStylePreview.getControl().setLayoutData(gd);
 
135
                
 
136
                new PreviewController(profileManager);
 
137
        }
 
138
 
 
139
    @Override
 
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);
 
142
    }
 
143
 
 
144
        /*
 
145
         * @see org.eclipse.cdt.internal.ui.preferences.formatter.ProfileConfigurationBlock#performApply()
 
146
         */
 
147
        @Override
 
148
        public void performApply() {
 
149
                if (fCustomCodeFormatterBlock != null) {
 
150
                        fCustomCodeFormatterBlock.performOk();
 
151
                }
 
152
                super.performApply();
 
153
        }
 
154
 
 
155
        /*
 
156
         * @see org.eclipse.cdt.internal.ui.preferences.formatter.ProfileConfigurationBlock#performDefaults()
 
157
         */
 
158
        @Override
 
159
        public void performDefaults() {
 
160
                if (fCustomCodeFormatterBlock != null) {
 
161
                        fCustomCodeFormatterBlock.performDefaults();
 
162
                }
 
163
                super.performDefaults();
 
164
        }
 
165
 
 
166
        /*
 
167
         * @see org.eclipse.cdt.internal.ui.preferences.formatter.ProfileConfigurationBlock#performOk()
 
168
         */
 
169
        @Override
 
170
        public boolean performOk() {
 
171
                if (fCustomCodeFormatterBlock != null) {
 
172
                        fCustomCodeFormatterBlock.performOk();
 
173
                }
 
174
                return super.performOk();
 
175
        }
 
176
    
 
177
}