KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntCodeFormatterPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.ant.internal.ui.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.ant.internal.ui.AntSourceViewerConfiguration;
18 import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences;
19 import org.eclipse.ant.internal.ui.editor.formatter.XmlFormatter;
20 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.resource.JFaceResources;
23 import org.eclipse.jface.text.Document;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.source.SourceViewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.graphics.Font;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.ui.editors.text.EditorsUI;
35 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
36
37 /*
38  * The page to configure the code formatter options.
39  */

40 public class AntCodeFormatterPreferencePage extends AbstractAntEditorPreferencePage {
41     
42     private SourceViewer fPreviewViewer;
43     private AntPreviewerUpdater fPreviewerUpdater;
44     
45     protected OverlayPreferenceStore createOverlayStore() {
46         List JavaDoc overlayKeys= new ArrayList JavaDoc();
47         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AntEditorPreferenceConstants.FORMATTER_WRAP_LONG));
48         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AntEditorPreferenceConstants.FORMATTER_ALIGN));
49         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AntEditorPreferenceConstants.FORMATTER_MAX_LINE_LENGTH));
50         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AntEditorPreferenceConstants.FORMATTER_TAB_CHAR));
51         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, AntEditorPreferenceConstants.FORMATTER_TAB_SIZE));
52         
53         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
54         overlayKeys.toArray(keys);
55         return new OverlayPreferenceStore(getPreferenceStore(), keys);
56     }
57     
58     /*
59      * @see PreferencePage#createControl(Composite)
60      */

61     public void createControl(Composite parent) {
62         super.createControl(parent);
63         //TODO set help
64
//WorkbenchHelp.setHelp(getControl(), "ANT_FORMATTER_PREFERENCE_PAGE");
65
}
66
67     protected Control createContents(Composite parent) {
68         initializeDialogUnits(parent);
69         getOverlayStore().load();
70         getOverlayStore().start();
71         int numColumns= 2;
72         Composite result= new Composite(parent, SWT.NONE);
73         GridLayout layout= new GridLayout();
74         layout.marginHeight= 0;
75         layout.marginWidth= 0;
76         result.setLayout(layout);
77         
78         Group indentationGroup= createGroup(numColumns, result, AntPreferencesMessages.AntCodeFormatterPreferencePage_0);
79         
80         String JavaDoc labelText= AntPreferencesMessages.AntCodeFormatterPreferencePage_1;
81         String JavaDoc[] errorMessages= new String JavaDoc[]{AntPreferencesMessages.AntCodeFormatterPreferencePage_2, AntPreferencesMessages.AntCodeFormatterPreferencePage_3};
82         addTextField(indentationGroup, labelText, AntEditorPreferenceConstants.FORMATTER_TAB_SIZE, 3, 0, errorMessages);
83         
84         labelText= AntPreferencesMessages.AntCodeFormatterPreferencePage_4;
85         addCheckBox(indentationGroup, labelText, AntEditorPreferenceConstants.FORMATTER_TAB_CHAR, 1);
86         
87         Group wrappingGroup= createGroup(numColumns, result, AntPreferencesMessages.AntCodeFormatterPreferencePage_6);
88         labelText= AntPreferencesMessages.AntCodeFormatterPreferencePage_7;
89         errorMessages= new String JavaDoc[]{AntPreferencesMessages.AntCodeFormatterPreferencePage_8, AntPreferencesMessages.AntCodeFormatterPreferencePage_9};
90         addTextField(wrappingGroup, labelText, AntEditorPreferenceConstants.FORMATTER_MAX_LINE_LENGTH, 3, 0, errorMessages);
91         labelText= AntPreferencesMessages.AntCodeFormatterPreferencePage_10;
92         addCheckBox(wrappingGroup, labelText, AntEditorPreferenceConstants.FORMATTER_WRAP_LONG, 1);
93         labelText= AntPreferencesMessages.AntCodeFormatterPreferencePage_5;
94         addCheckBox(wrappingGroup, labelText, AntEditorPreferenceConstants.FORMATTER_ALIGN, 1);
95         
96         
97         Label label= new Label(result, SWT.LEFT);
98         label.setText(AntPreferencesMessages.AntEditorPreferencePage_9);
99         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
100         
101         Control previewer= createPreviewer(result);
102         GridData gd= new GridData(GridData.FILL_BOTH);
103         gd.widthHint= convertWidthInCharsToPixels(20);
104         gd.heightHint= convertHeightInCharsToPixels(5);
105         previewer.setLayoutData(gd);
106         
107         initializeFields();
108         
109         applyDialogFont(result);
110     
111         return result;
112     }
113     
114     /**
115      * Convenience method to create a group
116      */

117     private Group createGroup(int numColumns, Composite parent, String JavaDoc text ) {
118         final Group group= new Group(parent, SWT.NONE);
119         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
120         gd.horizontalSpan= numColumns;
121         gd.widthHint= 0;
122         group.setLayoutData(gd);
123         group.setFont(parent.getFont());
124         
125         final GridLayout layout= new GridLayout(numColumns, false);
126         group.setLayout(layout);
127         group.setText(text);
128         return group;
129     }
130     
131     private Control createPreviewer(Composite parent) {
132         fPreviewViewer = new SourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
133         
134         AntSourceViewerConfiguration configuration = new AntSourceViewerConfiguration();
135     
136         fPreviewViewer.configure(configuration);
137         fPreviewViewer.setEditable(false);
138         Font font= JFaceResources.getFont(JFaceResources.TEXT_FONT);
139         fPreviewViewer.getTextWidget().setFont(font);
140         
141         IPreferenceStore store= new ChainedPreferenceStore(new IPreferenceStore[] { getOverlayStore(), EditorsUI.getPreferenceStore() });
142         fPreviewerUpdater= new AntPreviewerUpdater(fPreviewViewer, configuration, store);
143         
144         String JavaDoc content= loadPreviewContentFromFile("FormatPreviewCode.txt"); //$NON-NLS-1$
145
content= formatContent(content, store);
146         IDocument document = new Document(content);
147         new AntDocumentSetupParticipant().setup(document);
148         fPreviewViewer.setDocument(document);
149         
150         return fPreviewViewer.getControl();
151     }
152
153     private String JavaDoc formatContent(String JavaDoc content, IPreferenceStore preferenceStore) {
154         FormattingPreferences prefs= new FormattingPreferences();
155         prefs.setPreferenceStore(preferenceStore);
156         return XmlFormatter.format(content, prefs);
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.ant.internal.ui.preferences.AbstractAntEditorPreferencePage#handleDefaults()
161      */

162     protected void handleDefaults() {
163     }
164     
165     /* (non-Javadoc)
166      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
167      */

168     public void dispose() {
169         super.dispose();
170         if (fPreviewerUpdater != null) {
171             fPreviewerUpdater.dispose();
172         }
173     }
174 }
Popular Tags