org.rubypeople.rdt.internal.ui.preferences.RubyTemplatePreferencePage.java Source code

Java tutorial

Introduction

Here is the source code for org.rubypeople.rdt.internal.ui.preferences.RubyTemplatePreferencePage.java

Source

/*******************************************************************************
 * Copyright (c) 2000, 2004  John-Mason P. Shackelford and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *       John-Mason P. Shackelford - initial API and implementation
 *     IBM Corporation - bug fixes
 *******************************************************************************/
package org.rubypeople.rdt.internal.ui.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.texteditor.templates.TemplatePreferencePage;
import org.rubypeople.rdt.internal.ui.RubyPlugin;
import org.rubypeople.rdt.internal.ui.rubyeditor.RubySourceViewer;
import org.rubypeople.rdt.internal.ui.text.IRubyPartitions;
import org.rubypeople.rdt.internal.ui.text.SimpleRubySourceViewerConfiguration;
import org.rubypeople.rdt.internal.ui.text.template.contentassist.RubyTemplateAccess;
import org.rubypeople.rdt.ui.PreferenceConstants;
import org.rubypeople.rdt.ui.text.RubyTextTools;

/**
 * @see org.eclipse.jface.preference.PreferencePage
 */
public class RubyTemplatePreferencePage extends TemplatePreferencePage {

    public RubyTemplatePreferencePage() {
        setPreferenceStore(RubyPlugin.getDefault().getPreferenceStore());
        setTemplateStore(RubyTemplateAccess.getDefault().getTemplateStore());
        setContextTypeRegistry(RubyTemplateAccess.getDefault().getContextTypeRegistry());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.IPreferencePage#performOk()
     */
    public boolean performOk() {
        boolean ok = super.performOk();
        RubyPlugin.getDefault().savePluginPreferences();
        return ok;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#createViewer(org.eclipse.swt.widgets.Composite)
     */
    protected SourceViewer createViewer(Composite parent) {
        IDocument document = new Document();
        RubyTextTools tools = RubyPlugin.getDefault().getRubyTextTools();
        tools.setupRubyDocumentPartitioner(document, IRubyPartitions.RUBY_PARTITIONING);
        IPreferenceStore store = RubyPlugin.getDefault().getCombinedPreferenceStore();
        SourceViewer viewer = new RubySourceViewer(parent, null, null, false,
                SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store);
        SimpleRubySourceViewerConfiguration configuration = new SimpleRubySourceViewerConfiguration(
                tools.getColorManager(), store, null, IRubyPartitions.RUBY_PARTITIONING, false);
        viewer.configure(configuration);
        viewer.setEditable(false);
        viewer.setDocument(document);

        Font font = JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT);
        viewer.getTextWidget().setFont(font);
        new RubySourcePreviewerUpdater(viewer, configuration, store);

        Control control = viewer.getControl();
        GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
        control.setLayoutData(data);

        return viewer;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#getFormatterPreferenceKey()
     */
    protected String getFormatterPreferenceKey() {
        return PreferenceConstants.TEMPLATES_USE_CODEFORMATTER;
    }

    /*
     * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#updateViewerInput()
     */
    protected void updateViewerInput() {
        IStructuredSelection selection = (IStructuredSelection) getTableViewer().getSelection();
        SourceViewer viewer = getViewer();

        if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
            TemplatePersistenceData data = (TemplatePersistenceData) selection.getFirstElement();
            Template template = data.getTemplate();
            String contextId = template.getContextTypeId();

            IDocument doc = viewer.getDocument();

            String start = null;
            if ("rdoc".equals(contextId)) { //$NON-NLS-1$
                start = "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
            } else
                start = ""; //$NON-NLS-1$

            doc.set(start + template.getPattern());
            int startLen = start.length();
            viewer.setDocument(doc, startLen, doc.getLength() - startLen);

        } else {
            viewer.getDocument().set(""); //$NON-NLS-1$
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.texteditor.templates.TemplatePreferencePage#isShowFormatterSetting()
     */
    protected boolean isShowFormatterSetting() {
        return false;
    }
}