001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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 java.util.Map;
013:
014: import org.eclipse.core.runtime.IStatus;
015:
016: import org.eclipse.core.resources.IProject;
017:
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020:
021: import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
022:
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
025:
026: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
027: import org.eclipse.jdt.internal.ui.JavaPlugin;
028: import org.eclipse.jdt.internal.ui.dialogs.StatusUtil;
029:
030: /*
031: * The page to configure the code templates.
032: */
033: public class CodeTemplatePreferencePage extends
034: PropertyAndPreferencePage {
035:
036: public static final String PREF_ID = "org.eclipse.jdt.ui.preferences.CodeTemplatePreferencePage"; //$NON-NLS-1$
037: public static final String PROP_ID = "org.eclipse.jdt.ui.propertyPages.CodeTemplatePreferencePage"; //$NON-NLS-1$
038:
039: public static final String DATA_SELECT_TEMPLATE = "CodeTemplatePreferencePage.select_template"; //$NON-NLS-1$
040:
041: private CodeTemplateBlock fCodeTemplateConfigurationBlock;
042:
043: public CodeTemplatePreferencePage() {
044: setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
045: //setDescription(PreferencesMessages.getString("CodeTemplatesPreferencePage.description")); //$NON-NLS-1$
046:
047: // only used when page is shown programatically
048: setTitle(PreferencesMessages.CodeTemplatesPreferencePage_title);
049: }
050:
051: /*
052: * @see PreferencePage#createControl(Composite)
053: */
054: public void createControl(Composite parent) {
055: IWorkbenchPreferenceContainer container = (IWorkbenchPreferenceContainer) getContainer();
056: fCodeTemplateConfigurationBlock = new CodeTemplateBlock(
057: getNewStatusChangedListener(), getProject(), container);
058:
059: super .createControl(parent);
060: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
061: IJavaHelpContextIds.CODE_TEMPLATES_PREFERENCE_PAGE);
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage#createPreferenceContent(org.eclipse.swt.widgets.Composite)
066: */
067: protected Control createPreferenceContent(Composite composite) {
068: return fCodeTemplateConfigurationBlock
069: .createContents(composite);
070: }
071:
072: /* (non-Javadoc)
073: * @see org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage#enableProjectSpecificSettings(boolean)
074: */
075: protected void enableProjectSpecificSettings(
076: boolean useProjectSpecificSettings) {
077: super .enableProjectSpecificSettings(useProjectSpecificSettings);
078: if (fCodeTemplateConfigurationBlock != null) {
079: fCodeTemplateConfigurationBlock
080: .useProjectSpecificSettings(useProjectSpecificSettings);
081: }
082: }
083:
084: /*
085: * @see IPreferencePage#performOk()
086: */
087: public boolean performOk() {
088: if (fCodeTemplateConfigurationBlock != null) {
089: return fCodeTemplateConfigurationBlock
090: .performOk(useProjectSettings());
091: }
092: return true;
093: }
094:
095: /*
096: * @see PreferencePage#performDefaults()
097: */
098: protected void performDefaults() {
099: super .performDefaults();
100: if (fCodeTemplateConfigurationBlock != null) {
101: fCodeTemplateConfigurationBlock.performDefaults();
102: }
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.jface.dialogs.DialogPage#dispose()
107: */
108: public void dispose() {
109: if (fCodeTemplateConfigurationBlock != null) {
110: fCodeTemplateConfigurationBlock.dispose();
111: }
112: super .dispose();
113: }
114:
115: /* (non-Javadoc)
116: * @see org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener#statusChanged(org.eclipse.core.runtime.IStatus)
117: */
118: public void statusChanged(IStatus status) {
119: setValid(!status.matches(IStatus.ERROR));
120: StatusUtil.applyToStatusLine(this , status);
121: }
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.jface.preference.IPreferencePage#performCancel()
125: */
126: public boolean performCancel() {
127: if (fCodeTemplateConfigurationBlock != null) {
128: fCodeTemplateConfigurationBlock.performCancel();
129: }
130: return super .performCancel();
131: }
132:
133: /* (non-Javadoc)
134: * @see org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage#hasProjectSpecificOptions(org.eclipse.core.resources.IProject)
135: */
136: protected boolean hasProjectSpecificOptions(IProject project) {
137: return fCodeTemplateConfigurationBlock
138: .hasProjectSpecificOptions(project);
139: }
140:
141: /* (non-Javadoc)
142: * @see org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage#getPreferencePageID()
143: */
144: protected String getPreferencePageID() {
145: return PREF_ID;
146: }
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.jdt.internal.ui.preferences.PropertyAndPreferencePage#getPropertyPageID()
150: */
151: protected String getPropertyPageID() {
152: return PROP_ID;
153: }
154:
155: /*
156: * @see org.eclipse.jface.preference.PreferencePage#applyData(java.lang.Object)
157: */
158: public void applyData(Object data) {
159: if (data instanceof Map) {
160: Object id = ((Map) data).get(DATA_SELECT_TEMPLATE);
161: if (id instanceof String) {
162: final TemplatePersistenceData[] templates = fCodeTemplateConfigurationBlock.fTemplateStore
163: .getTemplateData();
164: TemplatePersistenceData template = null;
165: for (int index = 0; index < templates.length; index++) {
166: template = templates[index];
167: if (template.getId().equals(id)) {
168: fCodeTemplateConfigurationBlock
169: .postSetSelection(template);
170: break;
171: }
172: }
173: }
174: }
175: super.applyData(data);
176: }
177: }
|