001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.graphics.Font;
014: import org.eclipse.swt.graphics.Point;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Control;
018:
019: import org.eclipse.jface.preference.IPreferenceStore;
020: import org.eclipse.jface.resource.JFaceResources;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.jface.window.Window;
023:
024: import org.eclipse.jface.text.Document;
025: import org.eclipse.jface.text.IDocument;
026: import org.eclipse.jface.text.source.SourceViewer;
027: import org.eclipse.jface.text.templates.Template;
028: import org.eclipse.jface.text.templates.TemplateContextType;
029: import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
030:
031: import org.eclipse.ui.IWorkbenchPreferencePage;
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
034:
035: import org.eclipse.jdt.ui.PreferenceConstants;
036: import org.eclipse.jdt.ui.text.IJavaPartitions;
037: import org.eclipse.jdt.ui.text.JavaTextTools;
038:
039: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
040: import org.eclipse.jdt.internal.ui.JavaPlugin;
041: import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
042: import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
043: import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor;
044:
045: public class JavaTemplatePreferencePage extends TemplatePreferencePage
046: implements IWorkbenchPreferencePage {
047:
048: private TemplateVariableProcessor fTemplateProcessor;
049:
050: public JavaTemplatePreferencePage() {
051: setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
052: setTemplateStore(JavaPlugin.getDefault().getTemplateStore());
053: setContextTypeRegistry(JavaPlugin.getDefault()
054: .getTemplateContextRegistry());
055: fTemplateProcessor = new TemplateVariableProcessor();
056: }
057:
058: /*
059: * @see PreferencePage#createControl(Composite)
060: */
061: public void createControl(Composite parent) {
062: super .createControl(parent);
063: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
064: IJavaHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
065: }
066:
067: /*
068: * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createContents(org.eclipse.swt.widgets.Composite)
069: * @since 3.3
070: */
071: protected Control createContents(Composite ancestor) {
072: ScrolledPageContent scrolled = new ScrolledPageContent(
073: ancestor, SWT.H_SCROLL | SWT.V_SCROLL);
074: scrolled.setExpandHorizontal(true);
075: scrolled.setExpandVertical(true);
076:
077: Control control = super .createContents(scrolled);
078:
079: scrolled.setContent(control);
080: final Point size = control
081: .computeSize(SWT.DEFAULT, SWT.DEFAULT);
082: scrolled.setMinSize(size.x, size.y);
083:
084: return scrolled;
085: }
086:
087: /*
088: * @see org.eclipse.jface.preference.IPreferencePage#performOk()
089: */
090: public boolean performOk() {
091: boolean ok = super .performOk();
092:
093: JavaPlugin.getDefault().savePluginPreferences();
094:
095: return ok;
096: }
097:
098: /*
099: * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
100: */
101: protected String getFormatterPreferenceKey() {
102: return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
103: }
104:
105: /*
106: * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createTemplateEditDialog2(org.eclipse.jface.text.templates.Template, boolean, boolean)
107: */
108: protected Template editTemplate(Template template, boolean edit,
109: boolean isNameModifiable) {
110: org.eclipse.jdt.internal.ui.preferences.EditTemplateDialog dialog = new org.eclipse.jdt.internal.ui.preferences.EditTemplateDialog(
111: getShell(), template, edit, isNameModifiable,
112: getContextTypeRegistry());
113: if (dialog.open() == Window.OK) {
114: return dialog.getTemplate();
115: }
116: return null;
117: }
118:
119: /*
120: * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
121: */
122: protected SourceViewer createViewer(Composite parent) {
123: IDocument document = new Document();
124: JavaTextTools tools = JavaPlugin.getDefault()
125: .getJavaTextTools();
126: tools.setupJavaDocumentPartitioner(document,
127: IJavaPartitions.JAVA_PARTITIONING);
128: IPreferenceStore store = JavaPlugin.getDefault()
129: .getCombinedPreferenceStore();
130: SourceViewer viewer = new JavaSourceViewer(parent, null, null,
131: false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
132: SimpleJavaSourceViewerConfiguration configuration = new SimpleJavaSourceViewerConfiguration(
133: tools.getColorManager(), store, null,
134: IJavaPartitions.JAVA_PARTITIONING, false);
135: viewer.configure(configuration);
136: viewer.setEditable(false);
137: viewer.setDocument(document);
138:
139: Font font = JFaceResources
140: .getFont(PreferenceConstants.EDITOR_TEXT_FONT);
141: viewer.getTextWidget().setFont(font);
142: new JavaSourcePreviewerUpdater(viewer, configuration, store);
143:
144: Control control = viewer.getControl();
145: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
146: | GridData.FILL_VERTICAL);
147: control.setLayoutData(data);
148:
149: return viewer;
150: }
151:
152: /*
153: * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
154: */
155: protected void updateViewerInput() {
156: IStructuredSelection selection = (IStructuredSelection) getTableViewer()
157: .getSelection();
158: SourceViewer viewer = getViewer();
159:
160: if (selection.size() == 1
161: && selection.getFirstElement() instanceof TemplatePersistenceData) {
162: TemplatePersistenceData data = (TemplatePersistenceData) selection
163: .getFirstElement();
164: Template template = data.getTemplate();
165: String contextId = template.getContextTypeId();
166: TemplateContextType type = JavaPlugin.getDefault()
167: .getTemplateContextRegistry().getContextType(
168: contextId);
169: fTemplateProcessor.setContextType(type);
170:
171: IDocument doc = viewer.getDocument();
172:
173: String start = null;
174: if ("javadoc".equals(contextId)) { //$NON-NLS-1$
175: start = "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
176: } else
177: start = ""; //$NON-NLS-1$
178:
179: doc.set(start + template.getPattern());
180: int startLen = start.length();
181: viewer.setDocument(doc, startLen, doc.getLength()
182: - startLen);
183:
184: } else {
185: viewer.getDocument().set(""); //$NON-NLS-1$
186: }
187: }
188: }
|