KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > CodeFormatterConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.preferences.formatter;
13
14 import java.util.List JavaDoc;
15 import java.util.Observable JavaDoc;
16 import java.util.Observer JavaDoc;
17
18 import org.eclipse.core.runtime.preferences.IScopeContext;
19
20 import org.eclipse.core.resources.IProject;
21
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.jdt.ui.JavaUI;
27
28 import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
29 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
30
31
32
33 /**
34  * The code formatter preference page.
35  */

36
37 public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock {
38     
39     private static final String JavaDoc FORMATTER_DIALOG_PREFERENCE_KEY= "formatter_page"; //$NON-NLS-1$
40

41     private static final String JavaDoc DIALOGSTORE_LASTSAVELOADPATH= JavaUI.ID_PLUGIN + ".codeformatter"; //$NON-NLS-1$
42

43     /**
44      * Some Java source code used for preview.
45      */

46     protected final String JavaDoc PREVIEW= "/**\n* " + //$NON-NLS-1$
47
FormatterMessages.CodingStyleConfigurationBlock_preview_title +
48             "\n*/\n\n" + //$NON-NLS-1$
49
"package mypackage; import java.util.LinkedList; public class MyIntStack {" + //$NON-NLS-1$
50
"private final LinkedList fStack;" + //$NON-NLS-1$
51
"public MyIntStack(){fStack= new LinkedList();}" + //$NON-NLS-1$
52
"public int pop(){return ((Integer)fStack.removeFirst()).intValue();}" + //$NON-NLS-1$
53
"public void push(int elem){fStack.addFirst(new Integer(elem));}" + //$NON-NLS-1$
54
"public boolean isEmpty() {return fStack.isEmpty();}" + //$NON-NLS-1$
55
"}"; //$NON-NLS-1$
56

57     private class PreviewController implements Observer JavaDoc {
58
59         public PreviewController(ProfileManager profileManager) {
60             profileManager.addObserver(this);
61             fJavaPreview.setWorkingValues(profileManager.getSelected().getSettings());
62             fJavaPreview.update();
63         }
64
65         public void update(Observable JavaDoc o, Object JavaDoc arg) {
66             final int value= ((Integer JavaDoc)arg).intValue();
67             switch (value) {
68             case ProfileManager.PROFILE_CREATED_EVENT:
69             case ProfileManager.PROFILE_DELETED_EVENT:
70             case ProfileManager.SELECTION_CHANGED_EVENT:
71             case ProfileManager.SETTINGS_CHANGED_EVENT:
72                 fJavaPreview.setWorkingValues(((ProfileManager)o).getSelected().getSettings());
73                 fJavaPreview.update();
74             }
75         }
76
77     }
78
79     
80     /**
81      * The JavaPreview.
82      */

83     private JavaPreview fJavaPreview;
84
85     /**
86      * Create a new <code>CodeFormatterConfigurationBlock</code>.
87      */

88     public CodeFormatterConfigurationBlock(IProject project, PreferencesAccess access) {
89         super(project, access, DIALOGSTORE_LASTSAVELOADPATH);
90     }
91
92     protected IProfileVersioner createProfileVersioner() {
93         return new ProfileVersioner();
94     }
95     
96     protected ProfileStore createProfileStore(IProfileVersioner versioner) {
97         return new FormatterProfileStore(versioner);
98     }
99     
100     protected ProfileManager createProfileManager(List JavaDoc profiles, IScopeContext context, PreferencesAccess access, IProfileVersioner profileVersioner) {
101         return new FormatterProfileManager(profiles, context, access, profileVersioner);
102     }
103     
104     protected void configurePreview(Composite composite, int numColumns, ProfileManager profileManager) {
105         createLabel(composite, FormatterMessages.CodingStyleConfigurationBlock_preview_label_text, numColumns);
106         CompilationUnitPreview result= new CompilationUnitPreview(profileManager.getSelected().getSettings(), composite);
107         result.setPreviewText(PREVIEW);
108         fJavaPreview= result;
109
110         final GridData gd = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL);
111         gd.horizontalSpan = numColumns;
112         gd.verticalSpan= 7;
113         gd.widthHint = 0;
114         gd.heightHint = 0;
115         fJavaPreview.getControl().setLayoutData(gd);
116         
117         new PreviewController(profileManager);
118     }
119
120     
121     protected ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile) {
122         return new FormatterModifyDialog(shell, profile, profileManager, profileStore, newProfile, FORMATTER_DIALOG_PREFERENCE_KEY, DIALOGSTORE_LASTSAVELOADPATH);
123     }
124 }
125
Popular Tags