Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 77831 Details for
Bug 69581
[templates] Templates view
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
TemplatesView - implemented suggested improvements
templatesview.patch (text/plain), 111.16 KB, created by
dakshinamurthy.karra
on 2007-09-06 13:08:10 EDT
(
hide
)
Description:
TemplatesView - implemented suggested improvements
Filename:
MIME Type:
Creator:
dakshinamurthy.karra
Created:
2007-09-06 13:08:10 EDT
Size:
111.16 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.ui.workbench >Index: Eclipse UI/org/eclipse/ui/IPageLayout.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/IPageLayout.java,v >retrieving revision 1.33 >diff -u -r1.33 IPageLayout.java >--- Eclipse UI/org/eclipse/ui/IPageLayout.java 26 Jun 2007 17:35:10 -0000 1.33 >+++ Eclipse UI/org/eclipse/ui/IPageLayout.java 6 Sep 2007 16:47:44 -0000 >@@ -10,6 +10,7 @@ > * Chris Gross <schtoo@schtoo.com> > * - Fix for 99155 - allow standalone view placeholders > * Chris Gross chris.gross@us.ibm.com Bug 107443 >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 > *******************************************************************************/ > package org.eclipse.ui; > >@@ -92,6 +93,11 @@ > public static String ID_OUTLINE = "org.eclipse.ui.views.ContentOutline"; //$NON-NLS-1$ > > /** >+ * The view id for the workbench's Templates standard component. >+ */ >+ public static String ID_TEMPLATES = "org.eclipse.ui.views.Templates"; //$NON-NLS-1$ >+ >+ /** > * The view id for the workbench's Bookmark Navigator standard component. > */ > public static String ID_BOOKMARKS = "org.eclipse.ui.views.BookmarkView"; //$NON-NLS-1$ >#P org.eclipse.ui.views >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.views/META-INF/MANIFEST.MF,v >retrieving revision 1.9 >diff -u -r1.9 MANIFEST.MF >--- META-INF/MANIFEST.MF 7 Sep 2006 19:17:20 -0000 1.9 >+++ META-INF/MANIFEST.MF 6 Sep 2007 16:47:53 -0000 >@@ -11,7 +11,8 @@ > org.eclipse.ui.internal.views.contentoutline;x-internal:=true, > org.eclipse.ui.internal.views.properties;x-internal:=true, > org.eclipse.ui.views.contentoutline, >- org.eclipse.ui.views.properties >+ org.eclipse.ui.views.properties, >+ org.eclipse.ui.views.templates > Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.2.0,4.0.0)", > org.eclipse.help;bundle-version="[3.2.0,4.0.0)", > org.eclipse.ui;bundle-version="[3.2.0,4.0.0)" >Index: plugin.properties >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.views/plugin.properties,v >retrieving revision 1.13 >diff -u -r1.13 plugin.properties >--- plugin.properties 8 May 2006 20:53:18 -0000 1.13 >+++ plugin.properties 6 Sep 2007 16:47:53 -0000 >@@ -7,10 +7,11 @@ > # > # Contributors: > # IBM Corporation - initial API and implementation >+# Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 > ############################################################################### > pluginName= Views > providerName= Eclipse.org > > Views.PropertySheet = Properties > Views.ContentOutline = Outline >- >+Views.Templates = Templates >Index: plugin.xml >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.views/plugin.xml,v >retrieving revision 1.26 >diff -u -r1.26 plugin.xml >--- plugin.xml 23 Feb 2006 19:35:46 -0000 1.26 >+++ plugin.xml 6 Sep 2007 16:47:53 -0000 >@@ -18,6 +18,13 @@ > class="org.eclipse.ui.views.contentoutline.ContentOutline" > id="org.eclipse.ui.views.ContentOutline"> > </view> >+ <view >+ name="%Views.Templates" >+ icon="$nl$/icons/full/eview16/templates.gif" >+ category="org.eclipse.ui" >+ class="org.eclipse.ui.views.templates.TemplatesView" >+ id="org.eclipse.ui.views.Templates"> >+ </view> > </extension> > > </plugin> >Index: src/org/eclipse/ui/views/templates/TemplatesView.java >=================================================================== >RCS file: src/org/eclipse/ui/views/templates/TemplatesView.java >diff -N src/org/eclipse/ui/views/templates/TemplatesView.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/views/templates/TemplatesView.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,184 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.views.templates; >+ >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.ui.IEditorPart; >+import org.eclipse.ui.IWorkbenchPage; >+import org.eclipse.ui.IWorkbenchPart; >+import org.eclipse.ui.PlatformUI; >+import org.eclipse.ui.internal.views.ViewsPlugin; >+import org.eclipse.ui.internal.views.templates.TemplatesMessages; >+import org.eclipse.ui.part.IContributedContentsView; >+import org.eclipse.ui.part.IPage; >+import org.eclipse.ui.part.MessagePage; >+import org.eclipse.ui.part.PageBook; >+import org.eclipse.ui.part.PageBookView; >+ >+/** >+ * Main class for the Templates View. >+ * <p> >+ * This standard view has id <code>"org.eclipse.ui.views.TemplatesView"</code>. >+ * </p> >+ * When a <b>templates view</b> notices an editor being activated, it >+ * asks the editor whether it has a <b>template page</b> to include >+ * in the templates view. This is done using <code>getAdapter</code>: >+ * <pre> >+ * IEditorPart editor = ...; >+ * ITemplatePage templatePage = (ITemplatePage) editor.getAdapter(ITemplatePage.class); >+ * if (templatePage != null) { >+ * // editor wishes to contribute templatePage to templates view >+ * } >+ * </pre> >+ * If the editor supports a templates page, the editor instantiates >+ * and configures the page, and returns it. This page is then added to the >+ * templates view (a pagebook which presents one page at a time) and >+ * immediately made the current page (the templates view need not be >+ * visible). If the editor does not support a template page, the templates >+ * view shows a special default page which makes it clear to the user >+ * that the templates view is disengaged. When the templates view notices a >+ * different editor being activated, it flips to the editor's corresponding >+ * template page. When the templates view notices an editor being >+ * closed, it may destroy the editor's corresponding content outline page. >+ * </p> >+ * <p> >+ * The workbench will automatically instantiate this class when a templates >+ * view is needed for a workbench window. This class was not intended >+ * to be instantiated or subclassed by clients. >+ * </p> >+ * >+ * @since 3.4 >+ */ >+public class TemplatesView extends PageBookView { >+ /** >+ * The plugin prefix. >+ */ >+ public static final String PREFIX = PlatformUI.PLUGIN_ID + "."; //$NON-NLS-1$ >+ >+ /** >+ * Help context id used for the templates view >+ */ >+ public static final String TEMPLATES_VIEW_HELP_CONTEXT_ID = PREFIX >+ + "templates_view_context";//$NON-NLS-1$ >+ >+ /** >+ * Message to show on the default page. >+ */ >+ private static final String DEFAULT_TEXT = TemplatesMessages.Templates_defaultMessage; >+ >+ /** >+ * Creates a templates view >+ */ >+ public TemplatesView() { >+ super(); >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on PageBookView. >+ */ >+ protected IPage createDefaultPage(PageBook book) { >+ MessagePage page = new MessagePage(); >+ initPage(page); >+ page.createControl(book); >+ page.setMessage(DEFAULT_TEXT); >+ return page; >+ } >+ >+ /** >+ * The <code>PageBookView</code> implementation of this <code>IWorkbenchPart</code> >+ * method creates a <code>PageBook</code> control with its default page showing. >+ */ >+ public void createPartControl(Composite parent) { >+ super.createPartControl(parent); >+ PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook(), >+ TEMPLATES_VIEW_HELP_CONTEXT_ID); >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on PageBookView. >+ */ >+ protected PageRec doCreatePage(IWorkbenchPart part) { >+ // Try to get template page. >+ Object obj = ViewsPlugin.getAdapter(part, ITemplatesPage.class, false); >+ if (obj instanceof ITemplatesPage) { >+ ITemplatesPage page = (ITemplatesPage) obj; >+ initPage(page); >+ page.createControl(getPageBook()); >+ return new PageRec(part, page); >+ } >+ // There is no template page >+ return null; >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on PageBookView. >+ */ >+ protected void doDestroyPage(IWorkbenchPart part, PageRec rec) { >+ ITemplatesPage page = (ITemplatesPage) rec.page; >+ page.dispose(); >+ rec.dispose(); >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on IAdaptable. >+ */ >+ public Object getAdapter(Class key) { >+ if (key == IContributedContentsView.class) { >+ return new IContributedContentsView() { >+ public IWorkbenchPart getContributingPart() { >+ return getContributingEditor(); >+ } >+ }; >+ } >+ return super.getAdapter(key); >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on PageBookView. >+ */ >+ protected IWorkbenchPart getBootstrapPart() { >+ IWorkbenchPage page = getSite().getPage(); >+ if (page != null) { >+ return page.getActiveEditor(); >+ } >+ >+ return null; >+ } >+ >+ /** >+ * Returns the editor which contributed the current >+ * page to this view. >+ * >+ * @return the editor which contributed the current page >+ * or <code>null</code> if no editor contributed the current page >+ */ >+ private IWorkbenchPart getContributingEditor() { >+ return getCurrentContributingPart(); >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on PageBookView. >+ * We only want to track editors. >+ */ >+ protected boolean isImportant(IWorkbenchPart part) { >+ //We only care about editors >+ return (part instanceof IEditorPart); >+ } >+ >+ /* (non-Javadoc) >+ * Method declared on IViewPart. >+ * Treat this the same as part activation. >+ */ >+ public void partBroughtToTop(IWorkbenchPart part) { >+ partActivated(part); >+ } >+} >Index: src/org/eclipse/ui/internal/views/templates/TemplatesMessages.java >=================================================================== >RCS file: src/org/eclipse/ui/internal/views/templates/TemplatesMessages.java >diff -N src/org/eclipse/ui/internal/views/templates/TemplatesMessages.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/internal/views/templates/TemplatesMessages.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,33 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.internal.views.templates; >+ >+import org.eclipse.osgi.util.NLS; >+ >+/** >+ * TemplatesMessages is the message class for the messages used in the templates view. >+ */ >+public class TemplatesMessages extends NLS { >+ private static final String BUNDLE_NAME = "org.eclipse.ui.internal.views.templates.TemplatesMessages"; //$NON-NLS-1$ >+ >+ /** >+ * Default message for an empty templates view >+ */ >+ public static String Templates_defaultMessage; >+ static { >+ // initialize resource bundle >+ NLS.initializeMessages(BUNDLE_NAME, TemplatesMessages.class); >+ } >+ >+ private TemplatesMessages() { >+ } >+} >Index: src/org/eclipse/ui/internal/views/templates/TemplatesMessages.properties >=================================================================== >RCS file: src/org/eclipse/ui/internal/views/templates/TemplatesMessages.properties >diff -N src/org/eclipse/ui/internal/views/templates/TemplatesMessages.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/internal/views/templates/TemplatesMessages.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,11 @@ >+############################################################################### >+# Copyright (c) 2000, 2007 Dakshinamurthy Karra and others. >+# All rights reserved. This program and the accompanying materials >+# are made available under the terms of the Eclipse Public License v1.0 >+# which accompanies this distribution, and is available at >+# http://www.eclipse.org/legal/epl-v10.html >+# >+# Contributors: >+# Dakshinamurthy Karra - initial API and implementation >+############################################################################### >+Templates_defaultMessage=Templates are not available. >Index: src/org/eclipse/ui/views/templates/ITemplatesPage.java >=================================================================== >RCS file: src/org/eclipse/ui/views/templates/ITemplatesPage.java >diff -N src/org/eclipse/ui/views/templates/ITemplatesPage.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/views/templates/ITemplatesPage.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,25 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.views.templates; >+ >+import org.eclipse.ui.part.IPageBookViewPage; >+ >+/** >+ * Interface for a template page. This interface defines >+ * the minimum requirement for pages within the templates view, namely >+ * they must be pages (implement <code>IPageBookViewPage</code>). >+ * >+ * @see TemplatesView >+ * @since 3.4 >+ */ >+public interface ITemplatesPage extends IPageBookViewPage { >+} >#P org.eclipse.ui.workbench.texteditor >Index: META-INF/MANIFEST.MF >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/META-INF/MANIFEST.MF,v >retrieving revision 1.19 >diff -u -r1.19 MANIFEST.MF >--- META-INF/MANIFEST.MF 6 Aug 2007 13:19:00 -0000 1.19 >+++ META-INF/MANIFEST.MF 6 Sep 2007 16:47:56 -0000 >@@ -19,7 +19,8 @@ > org.eclipse.ui.texteditor.quickdiff, > org.eclipse.ui.texteditor.rulers, > org.eclipse.ui.texteditor.spelling, >- org.eclipse.ui.texteditor.templates >+ org.eclipse.ui.texteditor.templates, >+ org.eclipse.ui.texteditor.templates.view > Require-Bundle: > org.eclipse.core.runtime;bundle-version="[3.2.0,4.0.0)", > org.eclipse.core.expressions;bundle-version="[3.2.0,4.0.0)", >Index: src/org/eclipse/ui/texteditor/templates/TemplatePreferencePage.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/TemplatePreferencePage.java,v >retrieving revision 1.55 >diff -u -r1.55 TemplatePreferencePage.java >--- src/org/eclipse/ui/texteditor/templates/TemplatePreferencePage.java 6 Aug 2007 13:19:01 -0000 1.55 >+++ src/org/eclipse/ui/texteditor/templates/TemplatePreferencePage.java 6 Sep 2007 16:48:27 -0000 >@@ -7,6 +7,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 > *******************************************************************************/ > package org.eclipse.ui.texteditor.templates; > >@@ -134,7 +135,7 @@ > * > * @since 3.3 > */ >- protected static class EditTemplateDialog extends StatusDialog { >+ public static class EditTemplateDialog extends StatusDialog { > > private class TextViewerAction extends Action implements IUpdate { > >Index: src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.java >=================================================================== >RCS file: src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.java >diff -N src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,61 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.texteditor.templates.view; >+ >+import org.eclipse.osgi.util.NLS; >+ >+/** >+ * Helper class to get NLSed messages. >+ * >+ * @since 3.0 >+ */ >+final class TemplatesPageMessages extends NLS { >+ >+ private static final String BUNDLE_NAME= TemplatesPageMessages.class.getName(); >+ private TemplatesPageMessages() { >+ // Do not instantiate >+ } >+ >+ // TemplatesView >+ public static String TemplatesPage_column_context; >+ public static String TemplatesPage_column_description; >+ public static String TemplatesPage_column_name; >+ public static String TemplatesPage_insert; >+ public static String TemplatesPage_edit; >+ public static String TemplatesPage_copy; >+ public static String TemplatesPage_paste; >+ public static String TemplatesPage_preference_page; >+ public static String TemplatesPage_link_to_editor; >+ public static String TemplatesPage_collapse_all; >+ public static String TemplatesPage_new; >+ public static String TemplatesPage_remove; >+ public static String TemplatesPage_insert_tooltip; >+ public static String TemplatesPage_edit_tooltip; >+ public static String TemplatesPage_preference_page_tooltip; >+ public static String TemplatesPage_link_to_editor_tooltip; >+ public static String TemplatesPage_collapse_all_tooltip; >+ public static String TemplatesPage_new_tooltip; >+ public static String TemplatesPage_remove_tooltip; >+ public static String TemplatesPage_preview; >+ public static String TemplatesPage_question_create_new_message; >+ public static String TemplatesPage_question_create_new_title; >+ public static String TemplatesPage_save_error_message; >+ public static String TemplatesPage_snippet; >+ public static String TemplatesPage_paste_description; >+ public static String TemplatesPage_remove_message; >+ public static String TemplatesPage_remove_title; >+ >+ static { >+ NLS.initializeMessages(BUNDLE_NAME, TemplatesPageMessages.class); >+ } >+ >+} >Index: src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.properties >=================================================================== >RCS file: src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.properties >diff -N src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/texteditor/templates/view/TemplatesPageMessages.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,39 @@ >+############################################################################### >+# Copyright (c) 2007 Dakshinamurthy Karra and others. >+# All rights reserved. This program and the accompanying materials >+# are made available under the terms of the Eclipse Public License v1.0 >+# which accompanies this distribution, and is available at >+# http://www.eclipse.org/legal/epl-v10.html >+# >+# Contributors: >+# Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+############################################################################### >+ >+# templates page (view) >+TemplatesPage_collapse_all=Collapse All >+TemplatesPage_column_context=Context >+TemplatesPage_column_description=Description >+TemplatesPage_save_error_message=Error Saving the Templates >+TemplatesPage_column_name=Name >+TemplatesPage_edit=&Edit... >+TemplatesPage_insert=&Insert... >+TemplatesPage_copy=Copy >+TemplatesPage_paste=Paste >+TemplatesPage_preference_page=&Preferences... >+TemplatesPage_paste_description=Pasted from editor >+TemplatesPage_link_to_editor=Link with Editor >+TemplatesPage_new=&New... >+TemplatesPage_preview=Preview >+TemplatesPage_snippet=snippet >+TemplatesPage_question_create_new_message= The name of the template has been changed. Click 'Yes' to create an additional template with the new name or 'No' to rename the existing one. >+TemplatesPage_remove_title=Removing Template{0} >+TemplatesPage_remove_message=Are you sure you want to remove the selected template{0}? >+TemplatesPage_question_create_new_title= Edit Template >+TemplatesPage_remove=&Remove >+TemplatesPage_insert_tooltip=Insert into Editor >+TemplatesPage_edit_tooltip=Edit the Template >+TemplatesPage_preference_page_tooltip=Show Preference Page >+TemplatesPage_link_to_editor_tooltip=Link with Editor >+TemplatesPage_collapse_all_tooltip=Collapse All >+TemplatesPage_new_tooltip=Create a New Template >+TemplatesPage_remove_tooltip=Remove the Template >Index: src/org/eclipse/ui/texteditor/templates/view/TemplatesPageImages.java >=================================================================== >RCS file: src/org/eclipse/ui/texteditor/templates/view/TemplatesPageImages.java >diff -N src/org/eclipse/ui/texteditor/templates/view/TemplatesPageImages.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/texteditor/templates/view/TemplatesPageImages.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,161 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.texteditor.templates.view; >+ >+import java.net.URL; >+ >+import org.eclipse.core.runtime.FileLocator; >+import org.eclipse.core.runtime.Path; >+import org.eclipse.core.runtime.Platform; >+import org.eclipse.jface.resource.ImageDescriptor; >+import org.eclipse.jface.resource.ImageRegistry; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.ui.internal.texteditor.TextEditorPlugin; >+import org.osgi.framework.Bundle; >+ >+/** >+ * The images provided by the texteditor plugin >+ * >+ * @since 3.4 >+ */ >+public class TemplatesPageImages { >+ >+ static final String PREFIX_ELCL = TextEditorPlugin.PLUGIN_ID + ".elcl."; //$NON-NLS-1$ >+ static final String PREFIX_DLCL = TextEditorPlugin.PLUGIN_ID + ".dlcl."; //$NON-NLS-1$ >+ static final String PREFIX_OBJ = TextEditorPlugin.PLUGIN_ID + ".obj."; //$NON-NLS-1$ >+ >+ static final String IMG_ELCL_TEMPLATE_NEW = PREFIX_ELCL + "new_template.gif"; //$NON-NLS-1$ >+ static final String IMG_ELCL_TEMPLATE_DELETE = PREFIX_ELCL + "delete_template.gif"; //$NON-NLS-1$ >+ static final String IMG_ELCL_TEMPLATE_EDIT = PREFIX_ELCL + "edit_template.gif"; //$NON-NLS-1$ >+ static final String IMG_ELCL_TEMPLATE_INSERT = PREFIX_ELCL + "insert_template.gif"; //$NON-NLS-1$ >+ static final String IMG_ELCL_TEMPLATE_LINK = PREFIX_ELCL + "link_to_editor.gif"; //$NON-NLS-1$ >+ static final String IMG_ELCL_TEMPLATE_COLLAPSE_ALL = PREFIX_ELCL + "collapseall.gif"; //$NON-NLS-1$ >+ static final String IMG_DLCL_TEMPLATE_DELETE = PREFIX_DLCL + "delete_template.gif"; //$NON-NLS-1$ >+ static final String IMG_DLCL_TEMPLATE_EDIT = PREFIX_DLCL + "edit_template.gif"; //$NON-NLS-1$ >+ static final String IMG_DLCL_TEMPLATE_INSERT = PREFIX_DLCL + "insert_template.gif"; //$NON-NLS-1$ >+ >+ static final String IMG_OBJ_PREVIEW = PREFIX_OBJ + "preview.gif"; //$NON-NLS-1$ >+ static final String IMG_OBJ_CONTEXT = PREFIX_OBJ + "context.gif"; //$NON-NLS-1$ >+ static final String IMG_OBJ_TEMPLATE = PREFIX_OBJ + "template_obj.gif"; //$NON-NLS-1$ >+ >+ /** >+ * The image registry containing <code>Image</code>s. >+ */ >+ private static ImageRegistry imageRegistry; >+ >+ private static String ICONS_PATH = "$nl$/icons/full/"; //$NON-NLS-1$ >+ >+ // Use IPath and toOSString to build the names to ensure they have the >+ // slashes correct >+ private final static String ELCL = ICONS_PATH + "elcl16/"; //$NON-NLS-1$ >+ private final static String DLCL = ICONS_PATH + "dlcl16/"; //$NON-NLS-1$ >+ private final static String OBJ = ICONS_PATH + "obj16/"; //$NON-NLS-1$ >+ >+ /** >+ * Declare all images >+ */ >+ private static void declareImages() { >+ // Ant Editor images >+ declareRegistryImage(IMG_ELCL_TEMPLATE_NEW, ELCL + "new_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_ELCL_TEMPLATE_INSERT, ELCL + "insert_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_DLCL_TEMPLATE_INSERT, DLCL + "insert_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_ELCL_TEMPLATE_DELETE, ELCL + "delete_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_DLCL_TEMPLATE_DELETE, DLCL + "delete_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_ELCL_TEMPLATE_EDIT, ELCL + "edit_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_DLCL_TEMPLATE_EDIT, DLCL + "edit_template.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_ELCL_TEMPLATE_LINK, ELCL + "link_to_editor.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_ELCL_TEMPLATE_COLLAPSE_ALL, ELCL + "collapseall.gif"); //$NON-NLS-1$ >+ >+ declareRegistryImage(IMG_OBJ_PREVIEW, OBJ + "preview.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_OBJ_CONTEXT, OBJ + "context.gif"); //$NON-NLS-1$ >+ declareRegistryImage(IMG_OBJ_TEMPLATE, OBJ + "template_obj.gif"); //$NON-NLS-1$ >+ } >+ >+ /** >+ * Declare an Image in the registry table. >+ * >+ * @param key >+ * The key to use when registering the image >+ * @param path >+ * The path where the image can be found. This path is relative >+ * to where this plugin class is found (i.e. typically the >+ * packages directory) >+ */ >+ private final static void declareRegistryImage(String key, String path) { >+ ImageDescriptor desc = ImageDescriptor.getMissingImageDescriptor(); >+ Bundle bundle = Platform.getBundle(TextEditorPlugin.PLUGIN_ID); >+ URL url = null; >+ if (bundle != null) { >+ url = FileLocator.find(bundle, new Path(path), null); >+ desc = ImageDescriptor.createFromURL(url); >+ } >+ imageRegistry.put(key, desc); >+ } >+ >+ /** >+ * Returns the ImageRegistry. >+ * >+ * @return image registry >+ */ >+ public static ImageRegistry getImageRegistry() { >+ if (imageRegistry == null) { >+ initializeImageRegistry(); >+ } >+ return imageRegistry; >+ } >+ >+ /** >+ * Initialize the image registry by declaring all of the required graphics. >+ * This involves creating JFace image descriptors describing how to >+ * create/find the image should it be needed. The image is not actually >+ * allocated until requested. >+ * >+ * Prefix conventions Wizard Banners WIZBAN_ Preference Banners PREF_BAN_ >+ * Property Page Banners PROPBAN_ Color toolbar CTOOL_ Enable toolbar ETOOL_ >+ * Disable toolbar DTOOL_ Local enabled toolbar ELCL_ Local Disable toolbar >+ * DLCL_ Object large OBJL_ Object small OBJS_ View VIEW_ Product images >+ * PROD_ Misc images MISC_ >+ * >+ * Where are the images? The images (typically gifs) are found in the same >+ * location as this plugin class. This may mean the same package directory >+ * as the package holding this class. The images are declared using >+ * this.getClass() to ensure they are looked up via this plugin class. >+ * >+ * @return the image registry >+ * @see org.eclipse.jface.resource.ImageRegistry >+ */ >+ public static ImageRegistry initializeImageRegistry() { >+ imageRegistry = TextEditorPlugin.getDefault().getImageRegistry(); >+ declareImages(); >+ return imageRegistry; >+ } >+ >+ /** >+ * Returns the <code>Image<code> identified by the given key, >+ * or <code>null</code> if it does not exist. >+ * @param key >+ * @return the image >+ */ >+ public static Image getImage(String key) { >+ return getImageRegistry().get(key); >+ } >+ >+ /** >+ * Returns the <code>ImageDescriptor<code> identified by the given key, >+ * or <code>null</code> if it does not exist. >+ * @param key >+ * @return the image descriptor >+ */ >+ public static ImageDescriptor getImageDescriptor(String key) { >+ return getImageRegistry().getDescriptor(key); >+ } >+} >Index: src/org/eclipse/ui/texteditor/templates/view/TemplateTransfer.java >=================================================================== >RCS file: src/org/eclipse/ui/texteditor/templates/view/TemplateTransfer.java >diff -N src/org/eclipse/ui/texteditor/templates/view/TemplateTransfer.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/texteditor/templates/view/TemplateTransfer.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,181 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.texteditor.templates.view; >+ >+import java.io.ByteArrayInputStream; >+import java.io.ByteArrayOutputStream; >+import java.io.DataInputStream; >+import java.io.DataOutputStream; >+import java.io.IOException; >+import java.util.ArrayList; >+ >+import org.eclipse.jface.text.templates.Template; >+import org.eclipse.swt.dnd.ByteArrayTransfer; >+import org.eclipse.swt.dnd.TransferData; >+ >+/** >+ * Transfer type used for clip board and DnD operations for template objects >+ * >+ * @see TemplatesPage >+ * >+ * @since 3.4 >+ */ >+public class TemplateTransfer extends ByteArrayTransfer { >+ >+ private static TemplateTransfer instance; >+ >+ private static final String LOCAL_NAME = TemplateTransfer.class.getName() >+ + System.currentTimeMillis(); >+ private static final int LOCAL_TYPE = registerType(LOCAL_NAME); >+ >+ private static String[] names = null; >+ private static int types[] = null; >+ >+ /** >+ * @return the registered Transfer instance >+ */ >+ public static TemplateTransfer getInstance() { >+ if (instance == null) { >+ instance = new TemplateTransfer(); >+ types = new int[] { LOCAL_TYPE }; >+ names = new String[] { LOCAL_NAME }; >+ } >+ return instance; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.Transfer#getTypeIds() >+ */ >+ protected int[] getTypeIds() { >+ return types; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.Transfer#getTypeNames() >+ */ >+ protected String[] getTypeNames() { >+ return names; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, >+ * org.eclipse.swt.dnd.TransferData) >+ */ >+ protected void javaToNative(Object object, TransferData transferData) { >+ if (object == null || !(object instanceof Template[]) >+ || !isSupportedType(transferData)) >+ return; >+ ByteArrayOutputStream out = new ByteArrayOutputStream(); >+ DataOutputStream dataOut = new DataOutputStream(out); >+ Template[] templates = (Template[]) object; >+ for (int i = 0; i < templates.length; i++) { >+ writeTemplate(dataOut, templates[i]); >+ } >+ byte[] byteArray = out.toByteArray(); >+ try { >+ dataOut.close(); >+ } catch (IOException e) { >+ } >+ super.javaToNative(byteArray, transferData); >+ } >+ >+ /** >+ * Write a template to the output stream >+ * >+ * @param dataOut >+ * @param template >+ */ >+ private void writeTemplate(DataOutputStream dataOut, Template template) { >+ try { >+ writeString(dataOut, template.getName()); >+ writeString(dataOut, template.getDescription()); >+ writeString(dataOut, template.getContextTypeId()); >+ writeString(dataOut, template.getPattern()); >+ dataOut.writeBoolean(template.isAutoInsertable()); >+ } catch (IOException e) { >+ } >+ } >+ >+ /** >+ * Write a string to the output stream >+ * >+ * @param dataOut >+ * @param name >+ * @throws IOException >+ */ >+ private void writeString(DataOutputStream dataOut, String name) >+ throws IOException { >+ byte[] bytes = name.getBytes(); >+ dataOut.writeInt(bytes.length); >+ dataOut.write(bytes); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(org.eclipse.swt.dnd.TransferData) >+ */ >+ protected Object nativeToJava(TransferData transferData) { >+ byte[] bytes = (byte[]) super.nativeToJava(transferData); >+ if (bytes == null) >+ return null; >+ ArrayList listTemplates = new ArrayList(); >+ ByteArrayInputStream in = new ByteArrayInputStream(bytes); >+ DataInputStream dataIn = new DataInputStream(in); >+ try { >+ while (dataIn.available() > 0) { >+ Template template = readTempate(dataIn); >+ if (template == null) >+ break; >+ listTemplates.add(template); >+ } >+ } catch (IOException e) { >+ } finally { >+ try { >+ dataIn.close(); >+ } catch (IOException e) { >+ } >+ } >+ return listTemplates.toArray(new Template[listTemplates.size()]); >+ } >+ >+ /** >+ * Read a template from the data input stream. >+ * >+ * @param dataIn >+ * @return the template >+ * @throws IOException >+ */ >+ private Template readTempate(DataInputStream dataIn) throws IOException { >+ return new Template(readString(dataIn), readString(dataIn), >+ readString(dataIn), readString(dataIn), dataIn.readBoolean()); >+ } >+ >+ /** >+ * Read a string from the data input stream >+ * >+ * @param dataIn >+ * @return the string >+ * @throws IOException >+ */ >+ private String readString(DataInputStream dataIn) throws IOException { >+ byte[] bytes = new byte[dataIn.readInt()]; >+ dataIn.read(bytes); >+ return new String(bytes); >+ } >+} >Index: src/org/eclipse/ui/texteditor/templates/view/TemplatesPage.java >=================================================================== >RCS file: src/org/eclipse/ui/texteditor/templates/view/TemplatesPage.java >diff -N src/org/eclipse/ui/texteditor/templates/view/TemplatesPage.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/texteditor/templates/view/TemplatesPage.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,1371 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.texteditor.templates.view; >+ >+import java.io.IOException; >+import java.util.ArrayList; >+import java.util.Arrays; >+import java.util.Iterator; >+import java.util.List; >+ >+import org.eclipse.jface.action.Action; >+import org.eclipse.jface.action.IAction; >+import org.eclipse.jface.action.IMenuListener; >+import org.eclipse.jface.action.IMenuManager; >+import org.eclipse.jface.action.IToolBarManager; >+import org.eclipse.jface.action.MenuManager; >+import org.eclipse.jface.action.Separator; >+import org.eclipse.jface.dialogs.MessageDialog; >+import org.eclipse.jface.layout.TreeColumnLayout; >+import org.eclipse.jface.preference.IPreferenceStore; >+import org.eclipse.jface.resource.JFaceResources; >+import org.eclipse.jface.text.templates.ContextTypeRegistry; >+import org.eclipse.jface.text.templates.Template; >+import org.eclipse.jface.text.templates.TemplateContextType; >+import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; >+import org.eclipse.jface.text.templates.persistence.TemplateStore; >+import org.eclipse.jface.util.IPropertyChangeListener; >+import org.eclipse.jface.util.PropertyChangeEvent; >+import org.eclipse.jface.viewers.ColumnPixelData; >+import org.eclipse.jface.viewers.DoubleClickEvent; >+import org.eclipse.jface.viewers.IDoubleClickListener; >+import org.eclipse.jface.viewers.ISelectionChangedListener; >+import org.eclipse.jface.viewers.IStructuredSelection; >+import org.eclipse.jface.viewers.ITableLabelProvider; >+import org.eclipse.jface.viewers.ITreeContentProvider; >+import org.eclipse.jface.viewers.LabelProvider; >+import org.eclipse.jface.viewers.SelectionChangedEvent; >+import org.eclipse.jface.viewers.StructuredSelection; >+import org.eclipse.jface.viewers.TreeViewer; >+import org.eclipse.jface.viewers.Viewer; >+import org.eclipse.jface.viewers.ViewerComparator; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.custom.CLabel; >+import org.eclipse.swt.custom.SashForm; >+import org.eclipse.swt.custom.ViewForm; >+import org.eclipse.swt.dnd.Clipboard; >+import org.eclipse.swt.dnd.DND; >+import org.eclipse.swt.dnd.DragSourceAdapter; >+import org.eclipse.swt.dnd.DragSourceEvent; >+import org.eclipse.swt.dnd.DropTargetAdapter; >+import org.eclipse.swt.dnd.DropTargetEvent; >+import org.eclipse.swt.dnd.Transfer; >+import org.eclipse.swt.events.ControlEvent; >+import org.eclipse.swt.events.ControlListener; >+import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.layout.GridData; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Menu; >+import org.eclipse.swt.widgets.Shell; >+import org.eclipse.swt.widgets.Tree; >+import org.eclipse.swt.widgets.TreeColumn; >+import org.eclipse.swt.widgets.TreeItem; >+import org.eclipse.ui.IActionBars; >+import org.eclipse.ui.IWorkbenchActionConstants; >+import org.eclipse.ui.IWorkbenchPartSite; >+import org.eclipse.ui.actions.ActionFactory; >+import org.eclipse.ui.dialogs.PreferencesUtil; >+import org.eclipse.ui.dnd.IDragAndDropService; >+import org.eclipse.ui.internal.texteditor.NLSUtility; >+import org.eclipse.ui.internal.texteditor.TextEditorPlugin; >+import org.eclipse.ui.part.Page; >+import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; >+ >+/** >+ * An abstract base class for template pages. >+ * <p> >+ * Clients who are defining an editor may elect to provide a corresponding >+ * templates page. This templates page will be presented to the user via the >+ * Templates View (the user decides whether their workbench window contains this >+ * view) whenever that editor is active. This class should be subclassed by >+ * clients. >+ * </p> >+ * <p> >+ * Internally, a TemplatesPage uses the template store to display different >+ * categories. A link to editor mode on the templates page allows to filtering >+ * of the categories to only that are supported in this context. >+ * </p> >+ * >+ * @since 3.4 >+ */ >+public abstract class TemplatesPage extends Page { >+ >+ /** >+ * Sashform size >+ */ >+ private static final String SASH_SIZE_PREF_ID = TextEditorPlugin.PLUGIN_ID >+ + ".templates.templatesPage.sashSize"; //$NON-NLS-1$ >+ /** >+ * Tree columns widths >+ */ >+ private static final String COLUMN_NAME_WIDTH_PREF_ID = TextEditorPlugin.PLUGIN_ID >+ + ".templates.templatesPage.nameWidth"; //$NON-NLS-1$ >+ private static final String COLUMN_DESCRIPTION_WIDTH_PREF_ID = TextEditorPlugin.PLUGIN_ID >+ + ".templates.templatesPage.descriptionWidth"; //$NON-NLS-1$ >+ /** >+ * Link to editor action setting >+ */ >+ private static final String LINK_ACTION_PREF_ID = TextEditorPlugin.PLUGIN_ID >+ + ".templates.templatesPage.linkAction"; //$NON-NLS-1$ >+ /** >+ * Show disabled actions setting >+ */ >+ private static final String SHOW_DISABLED_ACTION_PREF_ID = TextEditorPlugin.PLUGIN_ID >+ + ".templates.templatesPage.showDisabledAction"; //$NON-NLS-1$ >+ /** >+ * The ID for the popup menu for this templates page >+ */ >+ private static final String POPUP_MENU_ID = "org.eclipse.ui.texteditor.templates.PopupMenu"; //$NON-NLS-1$ >+ >+ /** >+ * Default image for a template >+ */ >+ private static final Image DEFAULT_TEMPLATE_IMAGE = TemplatesPageImages >+ .getImage(TemplatesPageImages.IMG_OBJ_TEMPLATE); >+ /** >+ * Image for the context >+ */ >+ private static final Image CONTEXT_IMAGE = TemplatesPageImages >+ .getImage(TemplatesPageImages.IMG_OBJ_CONTEXT); >+ >+ /** >+ * Drop support for the editor linked to this page. When a user drops a >+ * template into the active editor, the template is applied at the drop >+ * position. >+ */ >+ private final class EditorDropTarget extends DropTargetAdapter { >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#dragEnter(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void dragEnter(DropTargetEvent event) { >+ event.detail = DND.DROP_COPY; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void dragOperationChanged(DropTargetEvent event) { >+ event.detail = DND.DROP_COPY; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#dragOver(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void dragOver(DropTargetEvent event) { >+ event.feedback |= DND.FEEDBACK_SCROLL; >+ event.detail = DND.DROP_NONE; >+ if (isEditorModifiable() >+ && isValidTemplateForPosition(getSelectedTemplate(), >+ new Point(event.x, event.y))) >+ event.detail = DND.DROP_COPY; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#drop(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void drop(DropTargetEvent event) { >+ insertTemplate(getSelectedTemplate(), new Point(event.x, event.y)); >+ } >+ } >+ >+ /** >+ * Comparator for the viewer. Sorts the templates by name and then >+ * description and context types by names. >+ */ >+ private static final class TemplateViewerComparator extends ViewerComparator { >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, >+ * java.lang.Object, java.lang.Object) >+ */ >+ public int compare(Viewer viewer, Object object1, Object object2) { >+ if ((object1 instanceof TemplatePersistenceData) >+ && (object2 instanceof TemplatePersistenceData)) { >+ Template left = ((TemplatePersistenceData) object1).getTemplate(); >+ Template right = ((TemplatePersistenceData) object2).getTemplate(); >+ int result = left.getName().compareToIgnoreCase(right.getName()); >+ if (result != 0) >+ return result; >+ return left.getDescription().compareToIgnoreCase(right.getDescription()); >+ } >+ if ((object1 instanceof TemplateContextType) >+ && (object2 instanceof TemplateContextType)) { >+ return ((TemplateContextType) object1).getName().compareToIgnoreCase( >+ ((TemplateContextType) object1).getName()); >+ } >+ return super.compare(viewer, object1, object2); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ViewerComparator#isSorterProperty(java.lang.Object, >+ * java.lang.String) >+ */ >+ public boolean isSorterProperty(Object element, String property) { >+ return true; >+ } >+ } >+ >+ /** >+ * Label provider for templates. >+ */ >+ private class TemplateLabelProvider extends LabelProvider implements ITableLabelProvider { >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, >+ * int) >+ */ >+ public Image getColumnImage(Object element, int columnIndex) { >+ if (columnIndex != 0) >+ return null; >+ if (element instanceof TemplateContextType) >+ return CONTEXT_IMAGE; >+ return getImageForTemplate(((TemplatePersistenceData) element).getTemplate()); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, >+ * int) >+ */ >+ public String getColumnText(Object element, int columnIndex) { >+ if (element instanceof TemplatePersistenceData) >+ return getTemplateColumnText((TemplatePersistenceData) element, columnIndex); >+ return getContextColumnText((TemplateContextType) element, columnIndex); >+ } >+ >+ private String getTemplateColumnText(TemplatePersistenceData data, int columnIndex) { >+ switch (columnIndex) { >+ case 0: >+ return data.getTemplate().getName(); >+ case 1: >+ return data.getTemplate().getDescription(); >+ default: >+ return ""; //$NON-NLS-1$ >+ } >+ } >+ >+ private String getContextColumnText(TemplateContextType contextType, int columnIndex) { >+ switch (columnIndex) { >+ case 0: >+ return contextType.getName(); >+ default: >+ return ""; //$NON-NLS-1$ >+ } >+ } >+ } >+ >+ /** >+ * Content provider for templates. Provides all the enabled templates >+ * defined for this editor. >+ */ >+ private class TemplatesContentProvider implements ITreeContentProvider { >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) >+ */ >+ public Object[] getChildren(Object parentElement) { >+ if (parentElement instanceof TemplatePersistenceData) >+ return new Object[0]; >+ else if (parentElement instanceof TemplateContextType) { >+ TemplateContextType contextType = (TemplateContextType) parentElement; >+ return getTemplates(contextType.getId()); >+ } >+ return null; >+ } >+ >+ private TemplatePersistenceData[] getTemplates(String contextId) { >+ List templateList = new ArrayList(); >+ TemplatePersistenceData[] datas = getTemplateStore().getTemplateData(false); >+ for (int i = 0; i < datas.length; i++) { >+ if (datas[i].isEnabled() >+ && datas[i].getTemplate().getContextTypeId().equals(contextId)) >+ templateList.add(datas[i]); >+ } >+ return (TemplatePersistenceData[]) templateList >+ .toArray(new TemplatePersistenceData[templateList.size()]); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) >+ */ >+ public Object getParent(Object element) { >+ if (element instanceof TemplatePersistenceData) { >+ TemplatePersistenceData templateData = (TemplatePersistenceData) element; >+ return getContextTypeRegistry().getContextType( >+ templateData.getTemplate().getContextTypeId()); >+ } >+ return null; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) >+ */ >+ public boolean hasChildren(Object element) { >+ if (element instanceof TemplateContextType) >+ return true; >+ return false; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) >+ */ >+ public Object[] getElements(Object inputElement) { >+ List contextTypes = new ArrayList(); >+ >+ for (Iterator iterator = getContextTypeRegistry().contextTypes(); iterator.hasNext();) { >+ TemplateContextType contextType = (TemplateContextType) iterator.next(); >+ if (isActiveContext(contextType) || !fLinkWithEditor) >+ contextTypes.add(contextType); >+ } >+ return contextTypes.toArray(new TemplateContextType[contextTypes.size()]); >+ } >+ >+ private boolean isActiveContext(TemplateContextType contextType) { >+ return fActiveTypes == null || fActiveTypes.contains(contextType.getId()); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.IContentProvider#dispose() >+ */ >+ public void dispose() { >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, >+ * java.lang.Object, java.lang.Object) >+ */ >+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { >+ } >+ } >+ >+ /** >+ * Listener to monitor changes to template store >+ */ >+ private IPropertyChangeListener fTemplateChangeListener; >+ >+ /** >+ * Control for this pagebook view >+ */ >+ private SashForm fControl; >+ >+ /* Actions */ >+ private Action fInsertAction; >+ private Action fAddAction; >+ private Action fEditAction; >+ private Action fRemoveAction; >+ private Action fLinkWithEditorAction; >+ private Action fCollapseAllAction; >+ private Action fPreferencePageAction; >+ >+ /* Clipboard actions */ >+ private Action fPasteAction; >+ private Action fCopyAction; >+ >+ /* Options and values saved in preference store */ >+ private boolean fLinkWithEditor; >+ private int fSashSize; >+ private int fNameWidth; >+ private int fDescriptionWidth; >+ >+ /** Currently selected templates and type */ >+ private ArrayList fSelectedTemplateList = new ArrayList(); >+ private TemplateContextType fSelectedType; >+ >+ /** Current active context types for the editor */ >+ private List fActiveTypes; >+ >+ /* Preference stores */ >+ /** >+ * Preference store to store the dialog setting for this page >+ */ >+ private IPreferenceStore fPreferenceStore; >+ >+ /* Controls */ >+ private Tree fTemplatesTree; >+ private TreeViewer fTreeViewer; >+ private Menu fContextMenu; >+ >+ /** >+ * The template that is being dragged >+ */ >+ private TemplatePersistenceData fDnDTemplate; >+ >+ /** >+ * Creates a new template view page. >+ */ >+ protected TemplatesPage() { >+ super(); >+ setupPreferenceStore(); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.ui.part.Page#createControl(org.eclipse.swt.widgets.Composite) >+ */ >+ public void createControl(Composite ancestor) { >+ setupActions(); >+ >+ fControl = new SashForm(ancestor, SWT.VERTICAL); >+ >+ createTemplateTree(); >+ setupPatternViewer(fControl); >+ >+ hookContextMenu(); >+ initializeDND(); >+ updateButtons(); >+ >+ fControl.setWeights(new int[] { fSashSize, 100 - fSashSize }); >+ fTemplateChangeListener = new IPropertyChangeListener() { >+ public void propertyChange(PropertyChangeEvent event) { >+ refresh(); >+ } >+ }; >+ getTemplatePreferenceStore().addPropertyChangeListener(fTemplateChangeListener); >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.ui.part.Page#setFocus() >+ */ >+ public void setFocus() { >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.ui.part.Page#getControl() >+ */ >+ public Control getControl() { >+ return fControl; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.ui.part.Page#dispose() >+ */ >+ public void dispose() { >+ if (fContextMenu != null && !fContextMenu.isDisposed()) >+ fContextMenu.dispose(); >+ if (fTemplateChangeListener != null) >+ getTemplatePreferenceStore().removePropertyChangeListener(fTemplateChangeListener); >+ super.dispose(); >+ } >+ >+ /** >+ * Get the shell >+ * >+ * @return the shell for this view site >+ */ >+ protected Shell getShell() { >+ return getSite().getShell(); >+ } >+ >+ /** >+ * The caret position in the editor has moved into a new context type. It is >+ * the subclasses responsibility to see that this is called only when needed >+ * by keeping track of editor contents (eg. partitions). >+ * >+ * @param ids >+ */ >+ protected void updateContextTypes(String[] ids) { >+ fActiveTypes = Arrays.asList(ids); >+ if (fLinkWithEditorAction != null && fLinkWithEditor) >+ refresh(); >+ } >+ >+ /** >+ * Setup the editor site as a drop target. Should be invoked by the >+ * subclasses for the D&D to work with the editor. >+ * >+ * @param site >+ * @param viewer >+ */ >+ protected void setupEditorDropTarget(IWorkbenchPartSite site, Control viewer) { >+ IDragAndDropService dndService = (IDragAndDropService) site >+ .getService(IDragAndDropService.class); >+ EditorDropTarget editorDropTarget = new EditorDropTarget(); >+ dndService.addMergedDropTarget(viewer, DND.DROP_COPY, new Transfer[] { TemplateTransfer >+ .getInstance() }, editorDropTarget); >+ } >+ >+ /** >+ * Convert the clipboard contents into a template >+ * >+ * @param clipBoard >+ * @return the template or null if contents are not valid >+ */ >+ protected Template getTemplateFromClipboard(Clipboard clipBoard) { >+ Template[] contents = (Template[]) clipBoard.getContents(TemplateTransfer.getInstance()); >+ if (contents != null) >+ return new Template(contents[0].getName(), contents[0].getDescription(), >+ getContextTypeId(), contents[0].getPattern(), true); >+ return null; >+ } >+ >+ /** >+ * Get the image to be used for the given template. Clients can override to >+ * provide a different image. >+ * >+ * @param template >+ * @return handle to the image >+ */ >+ protected Image getImageForTemplate(Template template) { >+ return DEFAULT_TEMPLATE_IMAGE; >+ } >+ >+ /** >+ * Subclasses should override and facilitate inserting the template code >+ * into the active editor. >+ * >+ * @param template >+ * @param position >+ */ >+ abstract protected void insertTemplate(Template template, Point position); >+ >+ /** >+ * Get the context type registry for the associated editor. >+ * >+ * @return the contextTypeRegistry >+ */ >+ abstract protected ContextTypeRegistry getContextTypeRegistry(); >+ >+ /** >+ * Get the template store for the associated editor >+ * >+ * @return the template store >+ */ >+ abstract protected TemplateStore getTemplateStore(); >+ >+ /** >+ * Get the preference store used to create the template store returned by >+ * {@link TemplatesPage#getTemplateStore()}. >+ * >+ * @return the preference store >+ */ >+ abstract protected IPreferenceStore getTemplatePreferenceStore(); >+ >+ /** >+ * Check whether the template is valid for the given drop position >+ * >+ * @param template >+ * @param position >+ * @return true if the template is valid >+ */ >+ abstract protected boolean isValidTemplateForPosition(Template template, Point position); >+ >+ /** >+ * Check whether the linked editor is modifiable >+ * >+ * @return true if the editor is modifiable >+ */ >+ abstract protected boolean isEditorModifiable(); >+ >+ /** >+ * Get the pattern data from the clipboard. This data will be stored for the >+ * template. >+ * >+ * @param clipBoard >+ * @return pattern data or null if the clipboard does not have valid content >+ */ >+ abstract protected String getTemplateDataFromClipboard(Clipboard clipBoard); >+ >+ /** >+ * Get the preference page ID for the templates for the given editor. >+ * >+ * @return id of the preference page, null if none exists >+ */ >+ abstract protected String getPreferencePageId(); >+ >+ /** >+ * Creates the edit dialog. Subclasses may override this method to provide a >+ * custom dialog. >+ * >+ * @param template >+ * the template being edited >+ * @param edit >+ * whether the dialog should be editable >+ * @param isNameModifiable >+ * whether the template name may be modified >+ * @return the created or modified template, or <code>null</code> if the >+ * editing failed >+ */ >+ abstract protected Template editTemplate(Template template, boolean edit, >+ boolean isNameModifiable); >+ >+ /** >+ * Get the pattern viewer. >+ * >+ * @param parent >+ * @return the viewer >+ */ >+ abstract protected Viewer createPatternViewer(Composite parent); >+ >+ /** >+ * Get the transfers supported by this editor. >+ * >+ * @return array of transfers supported >+ */ >+ abstract protected Transfer[] getTransfers(); >+ >+ /** >+ * Create the template pattern from a DnD operation. >+ * >+ * @param transfer >+ * @param data >+ * @return pattern or null if the transfer is not supported >+ */ >+ abstract protected String getTemplateDataFromDrop(Transfer transfer, Object data); >+ >+ /** >+ * Update the pattern viewer to show the current template. >+ * >+ * @param template >+ */ >+ abstract protected void updatePatternViewer(Template template); >+ >+ /** >+ * Setup the preference store >+ */ >+ private void setupPreferenceStore() { >+ fPreferenceStore = TextEditorPlugin.getDefault().getPreferenceStore(); >+ fPreferenceStore.setDefault(LINK_ACTION_PREF_ID, true); >+ fPreferenceStore.setDefault(SHOW_DISABLED_ACTION_PREF_ID, false); >+ fPreferenceStore.setDefault(SASH_SIZE_PREF_ID, 80); >+ fLinkWithEditor = fPreferenceStore.getBoolean(LINK_ACTION_PREF_ID); >+ fSashSize = fPreferenceStore.getInt(SASH_SIZE_PREF_ID); >+ } >+ >+ /** >+ * Setup the menu, context menu and toolbar actions. >+ */ >+ private void setupActions() { >+ createActions(); >+ IActionBars actionBars = getSite().getActionBars(); >+ >+ actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), fPasteAction); >+ fPasteAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.PASTE); >+ fPasteAction.setText(TemplatesPageMessages.TemplatesPage_paste); >+ actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), fCopyAction); >+ fCopyAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY); >+ fCopyAction.setText(TemplatesPageMessages.TemplatesPage_copy); >+ fillToolbar(actionBars); >+ fillMenu(actionBars); >+ } >+ >+ /** >+ * Create all the actions >+ */ >+ private void createActions() { >+ fInsertAction = new Action(TemplatesPageMessages.TemplatesPage_insert) { >+ public void run() { >+ insertTemplate(getSelectedTemplate(), null); >+ } >+ }; >+ fInsertAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_ELCL_TEMPLATE_INSERT)); >+ fInsertAction.setDisabledImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_DLCL_TEMPLATE_INSERT)); >+ fInsertAction.setToolTipText(TemplatesPageMessages.TemplatesPage_insert_tooltip); >+ >+ fAddAction = new Action(TemplatesPageMessages.TemplatesPage_new) { >+ public void run() { >+ addTemplate(); >+ } >+ }; >+ fAddAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_ELCL_TEMPLATE_NEW)); >+ fAddAction.setToolTipText(TemplatesPageMessages.TemplatesPage_new_tooltip); >+ >+ fEditAction = new Action(TemplatesPageMessages.TemplatesPage_edit) { >+ public void run() { >+ editTemplate(); >+ } >+ }; >+ fEditAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_ELCL_TEMPLATE_EDIT)); >+ fEditAction.setDisabledImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_DLCL_TEMPLATE_EDIT)); >+ fEditAction.setToolTipText(TemplatesPageMessages.TemplatesPage_edit_tooltip); >+ >+ fRemoveAction = new Action(TemplatesPageMessages.TemplatesPage_remove) { >+ public void run() { >+ removeTemplate(); >+ } >+ }; >+ fRemoveAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_DLCL_TEMPLATE_DELETE)); >+ fRemoveAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_ELCL_TEMPLATE_DELETE)); >+ fRemoveAction.setToolTipText(TemplatesPageMessages.TemplatesPage_remove_tooltip); >+ >+ fLinkWithEditorAction = new Action(TemplatesPageMessages.TemplatesPage_link_to_editor, >+ IAction.AS_CHECK_BOX) { >+ public void run() { >+ fLinkWithEditor = fLinkWithEditorAction.isChecked(); >+ fPreferenceStore.setValue(LINK_ACTION_PREF_ID, fLinkWithEditor); >+ refresh(); >+ } >+ }; >+ fLinkWithEditorAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_ELCL_TEMPLATE_LINK)); >+ fLinkWithEditorAction.setChecked(fPreferenceStore.getBoolean(LINK_ACTION_PREF_ID)); >+ fLinkWithEditorAction >+ .setToolTipText(TemplatesPageMessages.TemplatesPage_link_to_editor_tooltip); >+ fCollapseAllAction = new Action(TemplatesPageMessages.TemplatesPage_collapse_all) { >+ public void run() { >+ fTreeViewer.collapseAll(); >+ } >+ }; >+ fCollapseAllAction.setImageDescriptor(TemplatesPageImages >+ .getImageDescriptor(TemplatesPageImages.IMG_ELCL_TEMPLATE_COLLAPSE_ALL)); >+ fCollapseAllAction >+ .setToolTipText(TemplatesPageMessages.TemplatesPage_collapse_all_tooltip); >+ >+ if (getPreferencePageId() != null) { >+ fPreferencePageAction = new Action( >+ TemplatesPageMessages.TemplatesPage_preference_page) { >+ public void run() { >+ showPreferencePage(); >+ } >+ }; >+ fPreferencePageAction >+ .setToolTipText(TemplatesPageMessages.TemplatesPage_preference_page_tooltip); >+ } >+ >+ fPasteAction = new Action() { >+ public void run() { >+ final Template template = readClipboardContents(); >+ if (template != null) >+ getShell().getDisplay().asyncExec(new Runnable() { >+ public void run() { >+ addTemplate(template); >+ } >+ }); >+ } >+ >+ private Template readClipboardContents() { >+ Clipboard clipBoard = new Clipboard(getShell().getDisplay()); >+ String pattern = getTemplateDataFromClipboard(clipBoard); >+ if (pattern != null) >+ return new Template(createTemplateName(), >+ TemplatesPageMessages.TemplatesPage_paste_description, >+ getContextTypeId(), pattern, true); >+ return getTemplateFromClipboard(clipBoard); >+ } >+ }; >+ >+ fCopyAction = new Action() { >+ public void run() { >+ if (!isSingleTemplateSelected()) >+ return; >+ Template[] templates = new Template[] { getSelectedTemplate() }; >+ Clipboard clipBoard = new Clipboard(getShell().getDisplay()); >+ clipBoard.setContents(new Object[] { templates }, new Transfer[] { TemplateTransfer >+ .getInstance() }); >+ } >+ }; >+ } >+ >+ /** >+ * Fill the view menu >+ * >+ * @param actionBars >+ */ >+ private void fillMenu(IActionBars actionBars) { >+ IMenuManager menuManager = actionBars.getMenuManager(); >+ >+ if (fPreferencePageAction != null) { >+ menuManager.add(fPreferencePageAction); >+ menuManager.add(new Separator()); >+ } >+ >+ menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); >+ } >+ >+ /** >+ * Fill the toolbar >+ * >+ * @param actionBars >+ */ >+ private void fillToolbar(IActionBars actionBars) { >+ IToolBarManager toolBarManager = actionBars.getToolBarManager(); >+ toolBarManager.add(fInsertAction); >+ toolBarManager.add(fAddAction); >+ toolBarManager.add(fEditAction); >+ toolBarManager.add(fRemoveAction); >+ >+ toolBarManager.add(new Separator()); >+ >+ toolBarManager.add(fLinkWithEditorAction); >+ toolBarManager.add(fCollapseAllAction); >+ } >+ >+ /** >+ * Hookup the context menu >+ */ >+ private void hookContextMenu() { >+ MenuManager menuMgr = new MenuManager(POPUP_MENU_ID); >+ menuMgr.setRemoveAllWhenShown(true); >+ menuMgr.addMenuListener(new IMenuListener() { >+ public void menuAboutToShow(IMenuManager manager) { >+ fillContextMenu(manager); >+ } >+ }); >+ fContextMenu = menuMgr.createContextMenu(fTreeViewer.getControl()); >+ fTreeViewer.getControl().setMenu(fContextMenu); >+ getSite().registerContextMenu(POPUP_MENU_ID, menuMgr, fTreeViewer); >+ } >+ >+ /** >+ * Fill the context menu items >+ * >+ * @param manager >+ */ >+ private void fillContextMenu(IMenuManager manager) { >+ manager.add(fInsertAction); >+ manager.add(new Separator()); >+ manager.add(fAddAction); >+ manager.add(fEditAction); >+ manager.add(fRemoveAction); >+ manager.add(new Separator()); >+ manager.add(fCopyAction); >+ manager.add(fPasteAction); >+ // Other plug-ins can contribute there actions here >+ manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); >+ } >+ >+ /** >+ * Create the tree to display templates >+ */ >+ private void createTemplateTree() { >+ Composite treeComposite = new Composite(fControl, SWT.NONE); >+ GridData data = new GridData(GridData.FILL_BOTH); >+ treeComposite.setLayoutData(data); >+ >+ TreeColumnLayout columnLayout = new TreeColumnLayout(); >+ treeComposite.setLayout(columnLayout); >+ fTemplatesTree = new Tree(treeComposite, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI >+ | SWT.FULL_SELECTION); >+ fTemplatesTree.setHeaderVisible(true); >+ fTemplatesTree.setLinesVisible(true); >+ >+ GC gc = new GC(getShell()); >+ gc.setFont(JFaceResources.getDialogFont()); >+ >+ TreeColumn columnName = new TreeColumn(fTemplatesTree, SWT.NONE); >+ columnName.setText(TemplatesPageMessages.TemplatesPage_column_name); >+ int minWidth = fPreferenceStore.getInt(COLUMN_NAME_WIDTH_PREF_ID); >+ if (minWidth == 0) >+ minWidth = gc.stringExtent("XXXXXXXXXXXXXXX").x; //$NON-NLS-1$ >+ columnLayout.setColumnData(columnName, new ColumnPixelData(minWidth, false)); >+ columnName.addControlListener(new ControlListener() { >+ public void controlMoved(ControlEvent e) { >+ } >+ >+ public void controlResized(ControlEvent e) { >+ fNameWidth = ((TreeColumn) e.getSource()).getWidth(); >+ fPreferenceStore.setValue(COLUMN_NAME_WIDTH_PREF_ID, fNameWidth); >+ } >+ }); >+ >+ TreeColumn columnDescription = new TreeColumn(fTemplatesTree, SWT.NONE); >+ columnDescription.setText(TemplatesPageMessages.TemplatesPage_column_description); >+ minWidth = fPreferenceStore.getInt(COLUMN_DESCRIPTION_WIDTH_PREF_ID); >+ if (minWidth == 0) >+ minWidth = gc.stringExtent("XXXXXXXXXXXXXXXXXXXXXXXXX").x; //$NON-NLS-1$ >+ columnLayout.setColumnData(columnDescription, new ColumnPixelData(minWidth, false)); >+ columnDescription.addControlListener(new ControlListener() { >+ public void controlMoved(ControlEvent e) { >+ } >+ >+ public void controlResized(ControlEvent e) { >+ fDescriptionWidth = ((TreeColumn) e.getSource()).getWidth(); >+ fPreferenceStore.setValue(COLUMN_DESCRIPTION_WIDTH_PREF_ID, fDescriptionWidth); >+ } >+ }); >+ >+ gc.dispose(); >+ >+ createTreeViewer(); >+ } >+ >+ /** >+ * Create the tree viewer and setup the providers >+ */ >+ private void createTreeViewer() { >+ fTreeViewer = new TreeViewer(fTemplatesTree); >+ fTreeViewer.setLabelProvider(new TemplateLabelProvider()); >+ fTreeViewer.setContentProvider(new TemplatesContentProvider()); >+ >+ fTreeViewer.setComparator(new TemplateViewerComparator()); >+ fTreeViewer.setInput(getTemplatePreferenceStore()); >+ fTreeViewer.addDoubleClickListener(new IDoubleClickListener() { >+ public void doubleClick(DoubleClickEvent e) { >+ updateSelectedItems(); >+ insertTemplate(getSelectedTemplate(), null); >+ } >+ }); >+ >+ fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() { >+ public void selectionChanged(SelectionChangedEvent e) { >+ updateSelectedItems(); >+ updateButtons(); >+ } >+ }); >+ fTreeViewer.expandAll(); >+ } >+ >+ /** >+ * Setup the pattern viewer >+ * >+ * @param parent >+ */ >+ private void setupPatternViewer(Composite parent) { >+ ViewForm viewForm = new ViewForm(parent, SWT.NONE); >+ viewForm.setBorderVisible(false); >+ CLabel previewLabel = new CLabel(viewForm, SWT.NONE); >+ previewLabel.setText(TemplatesPageMessages.TemplatesPage_preview); >+ previewLabel.setImage(TemplatesPageImages.getImage(TemplatesPageImages.IMG_OBJ_PREVIEW)); >+ viewForm.setTopLeft(previewLabel); >+ >+ Viewer viewer = createPatternViewer(viewForm); >+ viewForm.setContent(viewer.getControl()); >+ viewForm.addControlListener(new ControlListener() { >+ public void controlMoved(ControlEvent e) { >+ } >+ >+ public void controlResized(ControlEvent e) { >+ int[] weights = fControl.getWeights(); >+ fSashSize = (int) (weights[0] * 100.0 / (weights[0] + weights[1])); >+ fPreferenceStore.setValue(SASH_SIZE_PREF_ID, fSashSize); >+ } >+ }); >+ } >+ >+ /** >+ * Update the selected items. The templates are stored in >+ * fSelectedTemplateList and the context type is stored in fSelectedType. >+ * >+ * If all selected templates do not belong to the same context type the type >+ * selection is set to null. >+ */ >+ private void updateSelectedItems() { >+ IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); >+ >+ fSelectedTemplateList.clear(); >+ fSelectedType = null; >+ >+ if (selection.size() == 1) { >+ Object item = selection.getFirstElement(); >+ if (item instanceof TemplateContextType) >+ fSelectedType = (TemplateContextType) item; >+ else >+ fSelectedTemplateList.add(item); >+ } else if (selection.size() > 1) { >+ for (Iterator iterator = selection.iterator(); iterator.hasNext();) { >+ Object item = iterator.next(); >+ if (item instanceof TemplateContextType) { >+ fSelectedTemplateList.clear(); >+ break; >+ } >+ fSelectedTemplateList.add(item); >+ } >+ } >+ if (!fSelectedTemplateList.isEmpty()) { >+ String contextTypeId = ((TemplatePersistenceData) fSelectedTemplateList.get(0)) >+ .getTemplate().getContextTypeId(); >+ fSelectedType = getContextTypeRegistry().getContextType(contextTypeId); >+ for (Iterator iterator = fSelectedTemplateList.iterator(); iterator.hasNext();) { >+ TemplatePersistenceData template = (TemplatePersistenceData) iterator.next(); >+ if (!contextTypeId.equals(template.getTemplate().getContextTypeId())) { >+ fSelectedType = null; >+ break; >+ } >+ } >+ } >+ updatePatternViewer(getSelectedTemplate()); >+ } >+ >+ /** >+ * Is a single template selected. >+ * >+ * @return true if a single template is selected >+ */ >+ private boolean isSingleTemplateSelected() { >+ return fSelectedTemplateList.size() == 1; >+ } >+ >+ /** >+ * Get the selected template >+ * >+ * @return the template or null if no/>1 templates are selected. >+ */ >+ private Template getSelectedTemplate() { >+ if (isSingleTemplateSelected()) >+ return ((TemplatePersistenceData) fSelectedTemplateList.get(0)).getTemplate(); >+ return null; >+ } >+ >+ /** >+ * Get the persistence data element of the selected template >+ * >+ * @return template data or null if no/>1 templates are selected >+ */ >+ private TemplatePersistenceData getSelectedTemplateData() { >+ if (isSingleTemplateSelected()) >+ return ((TemplatePersistenceData) fSelectedTemplateList.get(0)); >+ return null; >+ } >+ >+ /** >+ * Show the preference page. The action is enabled only if >+ * getPreferencePageId() returns non-null. >+ */ >+ private void showPreferencePage() { >+ PreferencesUtil.createPreferenceDialogOn(getShell(), getPreferencePageId(), null, null) >+ .open(); >+ } >+ >+ /** >+ * Update the state of the buttons >+ */ >+ private void updateButtons() { >+ fCopyAction.setEnabled(isSingleTemplateSelected()); >+ fInsertAction.setEnabled(isSingleTemplateSelected()); >+ fEditAction.setEnabled(isSingleTemplateSelected()); >+ fRemoveAction.setEnabled((fSelectedTemplateList.size() != 0)); >+ } >+ >+ /** >+ * Add a template >+ */ >+ private void addTemplate() { >+ String id = getContextTypeId(); >+ if (id != null) { >+ Template template = new Template("", "", id, "", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ >+ >+ addTemplate(template); >+ } >+ } >+ >+ /** >+ * Get contextType of the selected template >+ * >+ * @return contextType of the selected template or the first from the >+ * registry if no templates are selected >+ */ >+ private String getContextTypeId() { >+ if (fSelectedType != null) >+ return fSelectedType.getId(); >+ Iterator it = getContextTypeRegistry().contextTypes(); >+ if (it.hasNext()) >+ return ((TemplateContextType) it.next()).getId(); >+ return null; >+ } >+ >+ /** >+ * Add a template. The dialog is filled with the values from the given >+ * template. >+ * >+ * @param template >+ */ >+ private void addTemplate(Template template) { >+ Template newTemplate; >+ newTemplate = editTemplate(template, false, true); >+ if (newTemplate != null) { >+ TemplatePersistenceData data = new TemplatePersistenceData(newTemplate, true); >+ getTemplateStore().add(data); >+ saveTemplateStore(); >+ fSelectedTemplateList.clear(); >+ fSelectedTemplateList.add(data); >+ changeSelection(); >+ } >+ } >+ >+ /** >+ * Save the template store >+ */ >+ private void saveTemplateStore() { >+ try { >+ getTemplateStore().save(); >+ } catch (IOException e) { >+ e.printStackTrace(); >+ MessageDialog.openError(getShell(), >+ TemplatesPageMessages.TemplatesPage_save_error_message, e.getMessage()); >+ } >+ } >+ >+ /** >+ * Change selection >+ */ >+ private void changeSelection() { >+ fTreeViewer.refresh(); >+ if (isSingleTemplateSelected()) >+ fTreeViewer.setSelection(new StructuredSelection(fSelectedTemplateList.get(0)), true); >+ else >+ fTreeViewer.setSelection(new StructuredSelection()); >+ updateSelectedItems(); >+ } >+ >+ /** >+ * Edit the selected template >+ */ >+ private void editTemplate() { >+ Template oldTemplate = getSelectedTemplate(); >+ Template newTemplate = editTemplate(new Template(oldTemplate), true, true); >+ if (newTemplate != null) { >+ if (!newTemplate.getName().equals(oldTemplate.getName()) >+ && MessageDialog.openQuestion(getShell(), >+ TemplatesPageMessages.TemplatesPage_question_create_new_title, >+ TemplatesPageMessages.TemplatesPage_question_create_new_message)) { >+ fSelectedTemplateList.clear(); >+ TemplatePersistenceData templateData = new TemplatePersistenceData(newTemplate, >+ true); >+ fSelectedTemplateList.add(templateData); >+ getTemplateStore().add(templateData); >+ } else { >+ ((TemplatePersistenceData) fSelectedTemplateList.get(0)).setTemplate(newTemplate); >+ } >+ changeSelection(); >+ } >+ saveTemplateStore(); >+ } >+ >+ /** >+ * Move the selected template from one context to another >+ * >+ * @param modifiedTemplate >+ */ >+ private void moveTemplate(Template modifiedTemplate) { >+ Template newTemplate = editTemplate(modifiedTemplate, true, true); >+ if (newTemplate != null) { >+ fDnDTemplate.setTemplate(newTemplate); >+ changeSelection(); >+ } >+ saveTemplateStore(); >+ } >+ >+ /** >+ * Remove one or more selected templates >+ */ >+ private void removeTemplate() { >+ String suffix = fSelectedTemplateList.size() == 1 ? "" : "s"; //$NON-NLS-1$ //$NON-NLS-2$ >+ String title = NLSUtility.format(TemplatesPageMessages.TemplatesPage_remove_title, >+ new Object[] { suffix }); >+ String message = NLSUtility.format(TemplatesPageMessages.TemplatesPage_remove_message, >+ new Object[] { suffix }); >+ if (!MessageDialog.openQuestion(getShell(), title, message)) >+ return; >+ for (Iterator iterator = fSelectedTemplateList.iterator(); iterator.hasNext();) { >+ TemplatePersistenceData data = (TemplatePersistenceData) iterator.next(); >+ getTemplateStore().delete(data); >+ } >+ saveTemplateStore(); >+ fSelectedTemplateList.clear(); >+ changeSelection(); >+ } >+ >+ /** >+ * Initialize drag and drop the template items >+ */ >+ private void initializeDND() { >+ DragSourceAdapter dragListener = new DragSourceAdapter() { >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DragSourceAdapter#dragStart(org.eclipse.swt.dnd.DragSourceEvent) >+ */ >+ public void dragStart(DragSourceEvent event) { >+ if (!isSingleTemplateSelected()) { >+ event.doit = false; >+ } else { >+ fDnDTemplate = getSelectedTemplateData(); >+ } >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DragSourceAdapter#dragSetData(org.eclipse.swt.dnd.DragSourceEvent) >+ */ >+ public void dragSetData(DragSourceEvent event) { >+ if (isSingleTemplateSelected() >+ && TemplateTransfer.getInstance().isSupportedType(event.dataType)) { >+ event.data = new Template[] { getSelectedTemplate() }; >+ } >+ } >+ }; >+ fTreeViewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { TemplateTransfer >+ .getInstance() }, dragListener); >+ DropTargetAdapter dropListener = new DropTargetAdapter() { >+ Transfer[] transfers = getTransfers(); >+ TemplateTransfer templateTransfer = TemplateTransfer.getInstance(); >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#dragEnter(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void dragEnter(DropTargetEvent event) { >+ if (event.detail == DND.DROP_DEFAULT) >+ event.detail = DND.DROP_COPY; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#dragOperationChanged(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void dragOperationChanged(DropTargetEvent event) { >+ if (event.detail == DND.DROP_DEFAULT) >+ event.detail = DND.DROP_COPY; >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#dragOver(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void dragOver(DropTargetEvent event) { >+ event.feedback |= DND.FEEDBACK_SCROLL; >+ if (event.item == null) { >+ event.detail = DND.DROP_NONE; >+ return; >+ } >+ int index = 0; >+ boolean isTemplateTransfer = false; >+ while (index < event.dataTypes.length) { >+ int i = 0; >+ while (i < transfers.length) { >+ if (transfers[i].isSupportedType(event.dataTypes[index])) >+ break; >+ i++; >+ } >+ if (i < transfers.length) >+ break; >+ if (templateTransfer.isSupportedType(event.dataTypes[index])) { >+ isTemplateTransfer = true; >+ break; >+ } >+ index++; >+ } >+ if (index < event.dataTypes.length) { >+ event.currentDataType = event.dataTypes[index]; >+ if (event.detail == DND.DROP_DEFAULT || !isTemplateTransfer) >+ event.detail = DND.DROP_COPY; >+ return; >+ } >+ } >+ >+ /* >+ * (non-Javadoc) >+ * >+ * @see org.eclipse.swt.dnd.DropTargetAdapter#drop(org.eclipse.swt.dnd.DropTargetEvent) >+ */ >+ public void drop(DropTargetEvent event) { >+ if (event.item == null) >+ return; >+ Object object = ((TreeItem) event.item).getData(); >+ String contextId; >+ if (object instanceof TemplateContextType) >+ contextId = ((TemplateContextType) object).getId(); >+ else >+ contextId = ((TemplatePersistenceData) object).getTemplate().getContextTypeId(); >+ for (int i = 0; i < transfers.length; i++) { >+ if (transfers[i].isSupportedType(event.currentDataType)) { >+ String text = getTemplateDataFromDrop(transfers[i], event.data); >+ final Template template = new Template(createTemplateName(), >+ TemplatesPageMessages.TemplatesPage_paste_description, >+ contextId, text, true); >+ getShell().getDisplay().asyncExec(new Runnable() { >+ public void run() { >+ addTemplate(template); >+ } >+ }); >+ return; >+ } >+ } >+ if (templateTransfer.isSupportedType(event.currentDataType)) { >+ Template[] templates = (Template[]) event.data; >+ Template t = templates[0]; >+ final Template template = new Template(t.getName(), t.getDescription(), >+ contextId, t.getPattern(), true); >+ final int dropType = event.detail; >+ getShell().getDisplay().asyncExec(new Runnable() { >+ public void run() { >+ if (dropType == DND.DROP_COPY) >+ addTemplate(template); >+ else >+ moveTemplate(template); >+ } >+ }); >+ } >+ } >+ }; >+ Transfer[] transfers = new Transfer[getTransfers().length + 1]; >+ System.arraycopy(getTransfers(), 0, transfers, 0, getTransfers().length); >+ transfers[getTransfers().length] = TemplateTransfer.getInstance(); >+ fTreeViewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, dropListener); >+ } >+ >+ /** >+ * Create a template name >+ * >+ * @return a new template name >+ */ >+ private String createTemplateName() { >+ for (int i = 1; i < Integer.MAX_VALUE; i++) { >+ String name = TemplatesPageMessages.TemplatesPage_snippet + i; >+ if (getTemplateStore().findTemplate(name) == null) >+ return name; >+ } >+ return null; >+ } >+ >+ /** >+ * Refresh the template tree contents >+ */ >+ private void refresh() { >+ fTreeViewer.refresh(); >+ fTreeViewer.expandAll(); >+ } >+} >Index: src/org/eclipse/ui/texteditor/templates/view/TextEditorTemplatesPage.java >=================================================================== >RCS file: src/org/eclipse/ui/texteditor/templates/view/TextEditorTemplatesPage.java >diff -N src/org/eclipse/ui/texteditor/templates/view/TextEditorTemplatesPage.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/ui/texteditor/templates/view/TextEditorTemplatesPage.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,347 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.ui.texteditor.templates.view; >+ >+import org.eclipse.jface.action.Action; >+import org.eclipse.jface.action.IAction; >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.Document; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextViewerExtension5; >+import org.eclipse.jface.text.source.ISourceViewer; >+import org.eclipse.jface.text.source.SourceViewer; >+import org.eclipse.jface.text.source.SourceViewerConfiguration; >+import org.eclipse.jface.text.templates.Template; >+import org.eclipse.jface.viewers.IPostSelectionProvider; >+import org.eclipse.jface.viewers.ISelectionChangedListener; >+import org.eclipse.jface.viewers.ISelectionProvider; >+import org.eclipse.jface.viewers.SelectionChangedEvent; >+import org.eclipse.jface.viewers.Viewer; >+import org.eclipse.jface.window.Window; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.custom.StyledText; >+import org.eclipse.swt.dnd.Clipboard; >+import org.eclipse.swt.dnd.TextTransfer; >+import org.eclipse.swt.dnd.Transfer; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+import org.eclipse.swt.widgets.Event; >+import org.eclipse.ui.texteditor.AbstractTextEditor; >+import org.eclipse.ui.texteditor.ITextEditorActionConstants; >+import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; >+import org.eclipse.ui.texteditor.templates.TemplatePreferencePage.EditTemplateDialog; >+ >+/** >+ * A TemplatesPage implementation for Editors based on AbstractTextEditor. >+ * >+ * @since 3.4 >+ */ >+public abstract class TextEditorTemplatesPage extends TemplatesPage { >+ /** >+ * A post selection changed listener for the editor. Depending on the caret position >+ * updates the templates >+ */ >+ private final class SelectionChangedListener implements ISelectionChangedListener { >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent) >+ */ >+ public void selectionChanged(SelectionChangedEvent event) { >+ String[] contextTypes = getContextTypes() ; >+ if (needUpdate(contextTypes)) { >+ fCurrentContextTypes = contextTypes ; >+ updateContextTypes(fCurrentContextTypes); >+ return ; >+ } >+ } >+ >+ /** >+ * Check whether an update of the TemplatesPage is needed >+ * >+ * @param contextTypes >+ * @return true if update is needed >+ */ >+ private boolean needUpdate(String[] contextTypes) { >+ return fCurrentContextTypes == null || fCurrentContextTypes.length != contextTypes.length || >+ contextTypeChanged(contextTypes); >+ } >+ >+ /** >+ * Check whether there is any change in the context types needed >+ * >+ * @param contextTypes >+ * @return true if any of the context types changed >+ */ >+ private boolean contextTypeChanged(String[] contextTypes) { >+ for (int i = 0; i < contextTypes.length; i++) { >+ if (!contextTypes[i].equals(fCurrentContextTypes[i])) >+ return false ; >+ } >+ return true; >+ } >+ } >+ >+ /** >+ * The editor >+ */ >+ private final AbstractTextEditor fEditor; >+ /** >+ * The SourceViewer attached to this editor >+ */ >+ private final ISourceViewer fViewer; >+ /** >+ * The pattern viewer to be used with this view >+ */ >+ private SourceViewer fPatternViewer; >+ >+ /* Caches to improve performance while dropping a template */ >+ private int fCachedOffset; >+ private boolean fCachedResult; >+ private Point fCachedPosition; >+ >+ /** >+ * The current context types >+ */ >+ private String[] fCurrentContextTypes; >+ /** >+ * The selection changed listener to monitor the editor selections >+ */ >+ private SelectionChangedListener fSelectionChangedListener; >+ >+ /** >+ * Paste action support >+ */ >+ private IAction fOldPasteAction; >+ private IAction fPasteAction; >+ >+ /** >+ * Create a new TemplatesPage >+ * >+ * @param editor >+ * @param viewer >+ */ >+ public TextEditorTemplatesPage(AbstractTextEditor editor, ISourceViewer viewer) { >+ super(); >+ fEditor= editor; >+ fViewer= viewer; >+ setupEditorDropTarget(fEditor.getSite(), (Control) fEditor.getAdapter(Control.class)); >+ setupSelectionProvider(); >+ setupPasteOperation(); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#dispose() >+ */ >+ public void dispose() { >+ ISelectionProvider selectionProvider = fViewer.getSelectionProvider(); >+ if (selectionProvider instanceof IPostSelectionProvider) >+ ((IPostSelectionProvider)selectionProvider).removePostSelectionChangedListener(fSelectionChangedListener); >+ else >+ selectionProvider.removeSelectionChangedListener(fSelectionChangedListener); >+ fEditor.setAction(ITextEditorActionConstants.PASTE, fOldPasteAction); >+ super.dispose(); >+ } >+ >+ /** >+ * Get the editor associated with this view >+ * >+ * @return the editor >+ */ >+ protected AbstractTextEditor getEditor() { >+ return fEditor ; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#isValidTemplateForPosition(org.eclipse.jface.text.templates.Template, org.eclipse.swt.graphics.Point) >+ */ >+ protected boolean isValidTemplateForPosition(Template template, Point position) { >+ StyledText textWidget= (StyledText) fEditor.getAdapter(Control.class); >+ IDocument document= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); >+ try { >+ if (position.equals(fCachedPosition)) >+ return fCachedResult; >+ fCachedPosition= position; >+ int offset= getOffset(document, textWidget, textWidget.toControl(position.x, position.y)); >+ if (fCachedOffset == offset) >+ return fCachedResult; >+ fCachedOffset= offset; >+ if (isValidTemplate(document, template, offset, 0)) >+ return fCachedResult= true; >+ } catch (BadLocationException e) { >+ } >+ return fCachedResult= false; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#isEditorModifiable() >+ */ >+ protected boolean isEditorModifiable() { >+ return fEditor.isEditorInputModifiable(); >+ } >+ >+ /** >+ * Creates, configures and returns a source viewer to present the template >+ * pattern on the templates page. Clients may override to provide a custom >+ * source viewer featuring e.g. syntax coloring. >+ * >+ * @param parent >+ * the parent control >+ * @return a configured source viewer >+ */ >+ protected Viewer createPatternViewer(Composite parent) { >+ if (parent == null) >+ return fPatternViewer ; >+ fPatternViewer = new SourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL >+ | SWT.H_SCROLL); >+ SourceViewerConfiguration configuration = new SourceViewerConfiguration(); >+ fPatternViewer.configure(configuration); >+ IDocument document = new Document(); >+ fPatternViewer.setDocument(document); >+ fPatternViewer.setEditable(false); >+ return fPatternViewer; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#updatePatternViewer(org.eclipse.jface.text.templates.Template) >+ */ >+ protected void updatePatternViewer(Template template) { >+ if (template != null) >+ fPatternViewer.getDocument().set(template.getPattern()); >+ else >+ fPatternViewer.getDocument().set(""); //$NON-NLS-1$ >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getTemplateDataFromClipboard(org.eclipse.swt.dnd.Clipboard) >+ */ >+ protected String getTemplateDataFromClipboard(Clipboard clipBoard) { >+ String pattern = ((String) clipBoard.getContents(TextTransfer.getInstance())); >+ if (pattern != null) >+ return pattern.replaceAll("\\$", "\\$\\$"); //$NON-NLS-1$//$NON-NLS-2$ >+ return null ; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getTemplateDataFromDrop(org.eclipse.swt.dnd.Transfer, java.lang.Object) >+ */ >+ protected String getTemplateDataFromDrop(Transfer transfer, Object data) { >+ if (transfer instanceof TextTransfer) >+ return ((String)data).replaceAll("\\$", "\\$\\$"); //$NON-NLS-1$//$NON-NLS-2$ >+ return null; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getTransfers() >+ */ >+ protected Transfer[] getTransfers() { >+ return new Transfer[] { TextTransfer.getInstance() }; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#editTemplate(org.eclipse.jface.text.templates.Template, boolean, boolean) >+ */ >+ protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) { >+ EditTemplateDialog dialog = new EditTemplateDialog(getShell(), template, edit, >+ isNameModifiable, getContextTypeRegistry()); >+ if (dialog.open() == Window.OK) { >+ return dialog.getTemplate(); >+ } >+ return null; >+ } >+ >+ /** >+ * Get the context types supported at the current caret position of the editor >+ * >+ * @return the ids of the context types >+ */ >+ protected abstract String[] getContextTypes() ; >+ >+ /** >+ * Check whether the given template is valid for the document at the given position >+ * >+ * @param document >+ * @param template >+ * @param offset >+ * @param length >+ * @return true if the template is valid >+ */ >+ protected abstract boolean isValidTemplate(IDocument document, Template template, int offset, int length); >+ >+ /** >+ * Setup the paste operation >+ * >+ * We get the editors Paste operation and sets up a new operation that checks for the clipboard contents for >+ * TemplateTransfer data. >+ */ >+ private void setupPasteOperation() { >+ fOldPasteAction = fEditor.getAction(ITextEditorActionConstants.PASTE); >+ fPasteAction = new Action(TemplatesPageMessages.TemplatesPage_paste) { >+ public void run() { >+ Clipboard clipBoard = new Clipboard(getShell().getDisplay()); >+ Template template = getTemplateFromClipboard(clipBoard); >+ if (template != null) >+ insertTemplate(template, null); >+ else >+ fOldPasteAction.run(); >+ } >+ public void runWithEvent(Event event) { >+ Clipboard clipBoard = new Clipboard(getShell().getDisplay()); >+ Template template = getTemplateFromClipboard(clipBoard); >+ if (template != null) >+ insertTemplate(template, null); >+ else >+ fOldPasteAction.runWithEvent(event); >+ } >+ }; >+ fPasteAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.PASTE); >+ fEditor.setAction(ITextEditorActionConstants.PASTE, fPasteAction); >+ } >+ >+ /** >+ * Setup a selection listener to monitor the editor >+ */ >+ private void setupSelectionProvider() { >+ ISelectionProvider selectionProvider = fViewer.getSelectionProvider(); >+ fSelectionChangedListener = new SelectionChangedListener(); >+ if (selectionProvider instanceof IPostSelectionProvider) >+ ((IPostSelectionProvider)selectionProvider).addPostSelectionChangedListener(fSelectionChangedListener); >+ else >+ selectionProvider.addSelectionChangedListener(fSelectionChangedListener); >+ } >+ >+ /** >+ * Get the document relative offset from the textwidget relative point >+ * >+ * @param document >+ * @param textWidget >+ * @param point >+ * @return the offset >+ * @throws BadLocationException >+ */ >+ private int getOffset(IDocument document, StyledText textWidget, Point point) throws BadLocationException { >+ int widgetCaret= fViewer.getTextWidget().getCaretOffset(); >+ if (fViewer instanceof ITextViewerExtension5) { >+ ITextViewerExtension5 ext= (ITextViewerExtension5) fViewer; >+ try { >+ return ext.widgetOffset2ModelOffset(textWidget.getOffsetAtLocation(point)); >+ } catch (IllegalArgumentException e) { >+ int docLineIndex= ext.widgetLine2ModelLine(textWidget.getLineIndex(point.y)); >+ String lineDelimiter= document.getLineDelimiter(docLineIndex); >+ int delimLength= lineDelimiter == null ? 0 : lineDelimiter.length(); >+ return document.getLineOffset(docLineIndex) + document.getLineLength(docLineIndex) - delimLength; >+ } >+ } >+ IRegion visible= fViewer.getVisibleRegion(); >+ return widgetCaret + visible.getOffset(); >+ } >+} >#P org.eclipse.jdt.ui >Index: ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java,v >retrieving revision 1.434 >diff -u -r1.434 JavaEditor.java >--- ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java 28 Aug 2007 13:45:47 -0000 1.434 >+++ ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java 6 Sep 2007 16:49:16 -0000 >@@ -7,6 +7,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 > *******************************************************************************/ > package org.eclipse.jdt.internal.ui.javaeditor; > >@@ -142,6 +143,7 @@ > > import org.eclipse.ui.views.contentoutline.ContentOutline; > import org.eclipse.ui.views.contentoutline.IContentOutlinePage; >+import org.eclipse.ui.views.templates.ITemplatesPage; > > import org.eclipse.jdt.core.IClassFile; > import org.eclipse.jdt.core.ICompilationUnit; >@@ -197,6 +199,7 @@ > import org.eclipse.jdt.internal.ui.javaeditor.selectionactions.StructureSelectNextAction; > import org.eclipse.jdt.internal.ui.javaeditor.selectionactions.StructureSelectPreviousAction; > import org.eclipse.jdt.internal.ui.javaeditor.selectionactions.StructureSelectionAction; >+import org.eclipse.jdt.internal.ui.preferences.JavaTemplatesPage; > import org.eclipse.jdt.internal.ui.search.BreakContinueTargetFinder; > import org.eclipse.jdt.internal.ui.search.ExceptionOccurrencesFinder; > import org.eclipse.jdt.internal.ui.search.ImplementOccurrencesFinder; >@@ -1336,6 +1339,12 @@ > protected JavaOutlinePage fOutlinePage; > /** Outliner context menu Id */ > protected String fOutlinerContextMenuId; >+ /** The templates page >+ * >+ * @since 3.4 >+ */ >+ private JavaTemplatesPage fTemplatesPage; >+ > /** > * The editor selection changed listener. > * >@@ -1791,6 +1800,10 @@ > return fOutlinePage; > } > >+ if (required.equals(ITemplatesPage.class)) { >+ return getTemplatesPage(); >+ } >+ > if (IEncodingSupport.class.equals(required)) > return fEncodingSupport; > >@@ -1846,6 +1859,17 @@ > } > > /** >+ * @since 3.4 >+ */ >+ private JavaTemplatesPage getTemplatesPage() { >+ if (fTemplatesPage == null) { >+ fTemplatesPage= new JavaTemplatesPage(this); >+ >+ } >+ return fTemplatesPage; >+ } >+ >+ /** > * React to changed selection. > * > * @since 3.0 >Index: ui/org/eclipse/jdt/internal/ui/JavaPerspectiveFactory.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/JavaPerspectiveFactory.java,v >retrieving revision 1.44 >diff -u -r1.44 JavaPerspectiveFactory.java >--- ui/org/eclipse/jdt/internal/ui/JavaPerspectiveFactory.java 29 May 2007 18:41:36 -0000 1.44 >+++ ui/org/eclipse/jdt/internal/ui/JavaPerspectiveFactory.java 6 Sep 2007 16:48:30 -0000 >@@ -7,6 +7,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 > *******************************************************************************/ > package org.eclipse.jdt.internal.ui; > >@@ -45,7 +46,9 @@ > outputfolder.addPlaceholder(IPageLayout.ID_BOOKMARKS); > outputfolder.addPlaceholder(IProgressConstants.PROGRESS_VIEW_ID); > >- layout.addView(IPageLayout.ID_OUTLINE, IPageLayout.RIGHT, (float)0.75, editorArea); >+ IFolderLayout outlineFolder = layout.createFolder("right", IPageLayout.RIGHT, (float)0.75, editorArea); //$NON-NLS-1$ >+ outlineFolder.addView(IPageLayout.ID_OUTLINE); >+ outlineFolder.addView(IPageLayout.ID_TEMPLATES); > > layout.addActionSet(IDebugUIConstants.LAUNCH_ACTION_SET); > layout.addActionSet(JavaUI.ID_ACTION_SET); >@@ -66,6 +69,7 @@ > > // views - standard workbench > layout.addShowViewShortcut(IPageLayout.ID_OUTLINE); >+ layout.addShowViewShortcut(IPageLayout.ID_TEMPLATES); > layout.addShowViewShortcut(IPageLayout.ID_PROBLEM_VIEW); > layout.addShowViewShortcut(IPageLayout.ID_RES_NAV); > layout.addShowViewShortcut(IPageLayout.ID_TASK_LIST); >Index: ui/org/eclipse/jdt/internal/ui/preferences/JavaTemplatesPage.java >=================================================================== >RCS file: ui/org/eclipse/jdt/internal/ui/preferences/JavaTemplatesPage.java >diff -N ui/org/eclipse/jdt/internal/ui/preferences/JavaTemplatesPage.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ ui/org/eclipse/jdt/internal/ui/preferences/JavaTemplatesPage.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,392 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 Dakshinamurthy Karra and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 >+ *******************************************************************************/ >+ >+package org.eclipse.jdt.internal.ui.preferences; >+ >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.graphics.Font; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.graphics.Point; >+import org.eclipse.swt.layout.GridData; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Control; >+ >+import org.eclipse.jface.preference.IPreferenceStore; >+import org.eclipse.jface.resource.JFaceResources; >+import org.eclipse.jface.viewers.Viewer; >+import org.eclipse.jface.window.Window; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.Document; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITextViewerExtension; >+import org.eclipse.jface.text.Position; >+import org.eclipse.jface.text.Region; >+import org.eclipse.jface.text.TextSelection; >+import org.eclipse.jface.text.TextUtilities; >+import org.eclipse.jface.text.source.ISourceViewer; >+import org.eclipse.jface.text.templates.ContextTypeRegistry; >+import org.eclipse.jface.text.templates.DocumentTemplateContext; >+import org.eclipse.jface.text.templates.Template; >+import org.eclipse.jface.text.templates.TemplateContextType; >+import org.eclipse.jface.text.templates.persistence.TemplateStore; >+ >+import org.eclipse.ui.texteditor.templates.view.TextEditorTemplatesPage; >+ >+import org.eclipse.ui.views.templates.ITemplatesPage; >+ >+import org.eclipse.jdt.core.ICompilationUnit; >+ >+import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType; >+import org.eclipse.jdt.internal.corext.template.java.JavaContext; >+import org.eclipse.jdt.internal.corext.template.java.JavaContextType; >+import org.eclipse.jdt.internal.corext.template.java.JavaDocContext; >+import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType; >+import org.eclipse.jdt.internal.corext.template.java.SWTContextType; >+ >+import org.eclipse.jdt.ui.PreferenceConstants; >+import org.eclipse.jdt.ui.text.IJavaPartitions; >+import org.eclipse.jdt.ui.text.JavaTextTools; >+ >+import org.eclipse.jdt.internal.ui.JavaPlugin; >+import org.eclipse.jdt.internal.ui.JavaPluginImages; >+import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; >+import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; >+import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; >+import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration; >+import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal; >+import org.eclipse.jdt.internal.ui.text.template.preferences.TemplateVariableProcessor; >+ >+/** >+ * An implementation of TemplatesPage for JavaEditor >+ */ >+public class JavaTemplatesPage extends TextEditorTemplatesPage implements ITemplatesPage { >+ >+ private static final String PREFERENCE_PAGE_ID= "org.eclipse.jdt.ui.preferences.JavaTemplatePreferencePage"; //$NON-NLS-1$ >+ private static final TemplateStore TEMPLATE_STORE= JavaPlugin.getDefault().getTemplateStore(); >+ private static final IPreferenceStore PREFERENCE_STORE= JavaPlugin.getDefault().getPreferenceStore(); >+ private static final ContextTypeRegistry TEMPLATE_CONTEXT_REGISTRY= JavaPlugin.getDefault().getTemplateContextRegistry(); >+ >+ private TemplateVariableProcessor fTemplateProcessor; >+ private JavaSourceViewer fPatternViewer; >+ >+ /** >+ * Create a new TemplatesPage for the JavaEditor >+ * >+ * @param javaEditor >+ */ >+ public JavaTemplatesPage(JavaEditor javaEditor) { >+ super(javaEditor, javaEditor.getViewer()); >+ fTemplateProcessor= new TemplateVariableProcessor(); >+ updateContextTypes(getContextTypes()); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#insertTemplate(org.eclipse.jface.text.templates.Template, org.eclipse.swt.graphics.Point) >+ */ >+ protected void insertTemplate(Template template, Point dropPosition) { >+ if (!getJavaEditor().validateEditorInputState()) >+ return; >+ ISourceViewer contextViewer= getJavaEditor().getViewer(); >+ ITextSelection textSelection= (ITextSelection) contextViewer.getSelectionProvider().getSelection(); >+ IDocument document= getJavaEditor().getDocumentProvider().getDocument(getJavaEditor().getEditorInput()); >+ if (!isValidTemplate(document, template, textSelection.getOffset(), textSelection.getLength())) >+ return; >+ beginCompoundChange(contextViewer); >+ /* >+ * The Editor checks whether a completion for a word exists before it allows for the template to be >+ * applied. We pickup the current text at the selection position and replace it with the first char >+ * of the template name for this to succeed. >+ * Another advantage by this method is that the template replaces the selected text provided the >+ * selection by itself is not used in the template pattern. >+ */ >+ String savedText; >+ try { >+ savedText= document.get(textSelection.getOffset(), textSelection.getLength()); >+ if (savedText.length() == 0) { >+ String prefix= getIdentifierPart(document, template, textSelection.getOffset(), textSelection.getLength()); >+ if (prefix.length() > 0 && !template.getName().startsWith(prefix.toString())) { >+ return; >+ } >+ if (prefix.length() > 0) { >+ contextViewer.setSelectedRange(textSelection.getOffset() - prefix.length(), prefix.length()); >+ textSelection= (ITextSelection) contextViewer.getSelectionProvider().getSelection(); >+ } >+ } >+ document.replace(textSelection.getOffset(), textSelection.getLength(), template.getName().substring(0, 1)); >+ } catch (BadLocationException e) { >+ endCompoundChange(contextViewer); >+ return; >+ } >+ Position position= new Position(textSelection.getOffset() + 1, 0); >+ Region region= new Region(textSelection.getOffset() + 1, 0); >+ contextViewer.getSelectionProvider().setSelection(new TextSelection(textSelection.getOffset(), 1)); >+ ICompilationUnit compilationUnit= (ICompilationUnit) EditorUtility.getEditorInputJavaElement(getJavaEditor(), true); >+ >+ TemplateContextType type= getContextTypeRegistry().getContextType(template.getContextTypeId()); >+ DocumentTemplateContext context= ((CompilationUnitContextType) type).createContext(document, position, compilationUnit); >+ context.setVariable("selection", savedText); //$NON-NLS-1$ >+ if (context.getKey().length() == 0) { >+ try { >+ document.replace(textSelection.getOffset(), 1, savedText); >+ } catch (BadLocationException e) { >+ endCompoundChange(contextViewer); >+ return; >+ } >+ } >+ TemplateProposal proposal= new TemplateProposal(template, context, region, null); >+ getJavaEditor().getSite().getPage().activate(getJavaEditor()); >+ proposal.apply(getJavaEditor().getViewer(), ' ', 0, region.getOffset()); >+ endCompoundChange(contextViewer); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getContextTypeRegistry() >+ */ >+ protected ContextTypeRegistry getContextTypeRegistry() { >+ return TEMPLATE_CONTEXT_REGISTRY; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getTemplatePreferenceStore() >+ */ >+ protected IPreferenceStore getTemplatePreferenceStore() { >+ return PREFERENCE_STORE; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getTemplateStore() >+ */ >+ protected TemplateStore getTemplateStore() { >+ return TEMPLATE_STORE; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TextEditorTemplatesPage#isValidTemplate(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.templates.Template, int, int) >+ */ >+ protected boolean isValidTemplate(IDocument document, Template template, int offset, int length) { >+ try { >+ String[] contextIds= getContextIds(document, offset); >+ for (int i= 0; i < contextIds.length; i++) { >+ if (contextIds[i].equals(template.getContextTypeId())) { >+ DocumentTemplateContext context= getContext(document, template, offset, length); >+ return context.canEvaluate(template) || isTemplateAllowed(context, template); >+ } >+ } >+ } catch (BadLocationException e) { >+ } >+ return false; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TextEditorTemplatesPage#createPatternViewer(org.eclipse.swt.widgets.Composite) >+ */ >+ protected Viewer createPatternViewer(Composite parent) { >+ if (parent == null) >+ return fPatternViewer ; >+ IDocument document= new Document(); >+ JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools(); >+ tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING); >+ IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore(); >+ fPatternViewer= new JavaSourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, store); >+ SimpleJavaSourceViewerConfiguration configuration= new SimpleJavaSourceViewerConfiguration(tools.getColorManager(), store, null, IJavaPartitions.JAVA_PARTITIONING, false); >+ fPatternViewer.configure(configuration); >+ fPatternViewer.setEditable(false); >+ fPatternViewer.setDocument(document); >+ >+ Font font= JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT); >+ fPatternViewer.getTextWidget().setFont(font); >+ new JavaSourcePreviewerUpdater(fPatternViewer, configuration, store); >+ >+ Control control= fPatternViewer.getControl(); >+ GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL); >+ control.setLayoutData(data); >+ >+ fPatternViewer.setEditable(false); >+ return fPatternViewer; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getImageForTemplate(org.eclipse.jface.text.templates.Template) >+ */ >+ protected Image getImageForTemplate(Template template) { >+ if (template.getContextTypeId().equals(SWTContextType.ID)) >+ return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_SWT_TEMPLATE); >+ return JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TextEditorTemplatesPage#editTemplate(org.eclipse.jface.text.templates.Template, boolean, boolean) >+ */ >+ protected Template editTemplate(Template template, boolean edit, boolean isNameModifiable) { >+ EditTemplateDialog dialog= new EditTemplateDialog(getShell(), template, edit, isNameModifiable, getContextTypeRegistry()); >+ if (dialog.open() == Window.OK) { >+ return dialog.getTemplate(); >+ } >+ return null; >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TextEditorTemplatesPage#getContextTypes() >+ */ >+ protected String[] getContextTypes() { >+ Point selectedRange= getJavaEditor().getViewer().getSelectedRange(); >+ int offset= selectedRange.x + selectedRange.y; >+ try { >+ IDocument document= getJavaEditor().getDocumentProvider().getDocument(getJavaEditor().getEditorInput()); >+ return getContextIds(document, offset); >+ } catch (BadLocationException e) { >+ return new String[0]; >+ } >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TextEditorTemplatesPage#updatePatternViewer(org.eclipse.jface.text.templates.Template) >+ */ >+ protected void updatePatternViewer(Template template) { >+ if (template == null) { >+ fPatternViewer.getDocument().set(""); //$NON-NLS-1$ >+ return ; >+ } >+ String contextId= template.getContextTypeId(); >+ TemplateContextType type= getContextTypeRegistry().getContextType(contextId); >+ fTemplateProcessor.setContextType(type); >+ >+ IDocument doc= fPatternViewer.getDocument(); >+ >+ String start= null; >+ if ("javadoc".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(); >+ fPatternViewer.setDocument(doc, startLen, doc.getLength() - startLen); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.ui.texteditor.templates.TemplatesPage#getPreferencePageId() >+ */ >+ protected String getPreferencePageId() { >+ return PREFERENCE_PAGE_ID; >+ } >+ >+ /** >+ * Helper function to return the JavaEditor >+ * >+ * @return the editor >+ */ >+ private JavaEditor getJavaEditor() { >+ return (JavaEditor)getEditor() ; >+ } >+ >+ /** >+ * Undomanager - end compound change >+ * @param viewer >+ */ >+ private void endCompoundChange(ISourceViewer viewer) { >+ if (viewer instanceof ITextViewerExtension) >+ ((ITextViewerExtension) viewer).getRewriteTarget().endCompoundChange(); >+ } >+ >+ /** >+ * Undomanager - begin a compound change >+ * @param viewer >+ */ >+ private void beginCompoundChange(ISourceViewer viewer) { >+ if (viewer instanceof ITextViewerExtension) >+ ((ITextViewerExtension) viewer).getRewriteTarget().beginCompoundChange(); >+ } >+ >+ /** >+ * Check whether the template is allowed eventhough the context can't evaluate it. This is needed because >+ * the Dropping of a template is more lenient than ctl-space invoked code assist. >+ * >+ * @param context >+ * @param template >+ * @return true if the template is allowed >+ */ >+ private boolean isTemplateAllowed(DocumentTemplateContext context, Template template) { >+ int offset; >+ try { >+ if (template.getContextTypeId().equals(JavaDocContextType.ID)) { >+ return (offset= context.getCompletionOffset()) > 0 && Character.isWhitespace(context.getDocument().getChar(offset - 1)); >+ } else { >+ return ((offset= context.getCompletionOffset()) > 0 && !isTemplateNamePart(context.getDocument().getChar(offset - 1))); >+ } >+ } catch (BadLocationException e) { >+ } >+ return false; >+ } >+ >+ /** >+ * Checks whether the character is a valid character in Java template names >+ * >+ * @param ch >+ * @return true or false >+ */ >+ private boolean isTemplateNamePart(char ch) { >+ return !Character.isWhitespace(ch) && ch != '(' && ch != ')' && ch != '{' && ch != '}' && ch != ';'; >+ } >+ >+ /** >+ * Get context >+ * >+ * @param document >+ * @param template >+ * @param offset >+ * @param length >+ * @return the context >+ */ >+ private DocumentTemplateContext getContext(IDocument document, Template template, final int offset, int length) { >+ DocumentTemplateContext context; >+ if (template.getContextTypeId().equals(JavaDocContextType.ID)) { >+ context= new JavaDocContext(getContextTypeRegistry().getContextType(template.getContextTypeId()), document, new Position(offset, length), (ICompilationUnit) EditorUtility >+ .getEditorInputJavaElement(getJavaEditor(), true)); >+ } else { >+ context= new JavaContext(getContextTypeRegistry().getContextType(template.getContextTypeId()), document, new Position(offset, length), (ICompilationUnit) EditorUtility.getEditorInputJavaElement( >+ getJavaEditor(), true)); >+ } >+ return context; >+ } >+ >+ /** >+ * Get the active contexts for the given position in the document >+ * >+ * @param document >+ * @param offset >+ * @return an array of valid context id >+ * @throws BadLocationException >+ */ >+ private String[] getContextIds(IDocument document, int offset) throws BadLocationException { >+ String partition= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, true); >+ String[] ids= new String[] { JavaContextType.ID, SWTContextType.ID }; >+ if (partition.equals(IJavaPartitions.JAVA_DOC)) >+ ids= new String[] { JavaDocContextType.ID }; >+ return ids; >+ } >+ >+ /** >+ * Get the java identifier terminated at the given offset >+ * >+ * @param document >+ * @param template >+ * @param offset >+ * @param length >+ * @return the identifier part >+ * @throws BadLocationException >+ */ >+ private String getIdentifierPart(IDocument document, Template template, int offset, int length) throws BadLocationException { >+ return getContext(document, template, offset, length).getKey(); >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 69581
:
74439
|
74440
|
74444
|
74505
|
74784
|
74917
|
75026
|
75067
|
75300
|
75416
|
75417
|
75435
|
75441
|
75769
|
75791
|
75794
|
76655
|
77830
|
77831
|
78536
|
78538
|
79074
|
79118
|
79120
|
79124
|
80754