KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > templates > AntTemplatePreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 John-Mason P. Shackelford 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  * John-Mason P. Shackelford - initial API and implementation
10  * IBM Corporation - bug fixes
11  *******************************************************************************/

12 package org.eclipse.ant.internal.ui.editor.templates;
13
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.ant.internal.ui.editor.formatter.FormattingPreferences;
16 import org.eclipse.ant.internal.ui.editor.formatter.XmlFormatter;
17 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant;
18 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.text.Document;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.source.SourceViewer;
23 import org.eclipse.jface.text.source.SourceViewerConfiguration;
24 import org.eclipse.jface.text.templates.Template;
25 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
31
32 /**
33  * @see org.eclipse.jface.preference.PreferencePage
34  */

35 public class AntTemplatePreferencePage extends TemplatePreferencePage {
36
37     private FormattingPreferences fFormattingPreferences= new FormattingPreferences();
38     
39     public AntTemplatePreferencePage() {
40         setPreferenceStore(AntUIPlugin.getDefault().getPreferenceStore());
41         setTemplateStore(AntTemplateAccess.getDefault().getTemplateStore());
42         setContextTypeRegistry(AntTemplateAccess.getDefault().getContextTypeRegistry());
43     }
44
45     /* (non-Javadoc)
46      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
47      */

48     public boolean performOk() {
49           boolean ok = super.performOk();
50           AntUIPlugin.getDefault().savePluginPreferences();
51           return ok;
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
58      */

59     protected SourceViewer createViewer(Composite parent) {
60         SourceViewer viewer = new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
61           
62         SourceViewerConfiguration configuration = new AntTemplateViewerConfiguration();
63         IDocument document = new Document();
64         new AntDocumentSetupParticipant().setup(document);
65         viewer.configure(configuration);
66         viewer.setDocument(document);
67         viewer.setEditable(false);
68         Font font= JFaceResources.getFont(JFaceResources.TEXT_FONT);
69         viewer.getTextWidget().setFont(font);
70                 
71         return viewer;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
76      */

77     protected String JavaDoc getFormatterPreferenceKey() {
78         return AntEditorPreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
79     }
80     
81     /*
82      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
83      */

84     protected void updateViewerInput() {
85         IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
86         SourceViewer viewer= getViewer();
87         
88         if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
89             TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
90             Template template= data.getTemplate();
91             if (AntUIPlugin.getDefault().getPreferenceStore().getBoolean(getFormatterPreferenceKey())) {
92                 String JavaDoc formatted= XmlFormatter.format(template.getPattern(), fFormattingPreferences);
93                 viewer.getDocument().set(formatted);
94             } else {
95                 viewer.getDocument().set(template.getPattern());
96             }
97         } else {
98             viewer.getDocument().set(""); //$NON-NLS-1$
99
}
100     }
101     /* (non-Javadoc)
102      * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#isShowFormatterSetting()
103      */

104     protected boolean isShowFormatterSetting() {
105         return false;
106     }
107 }
Popular Tags