1 /******************************************************************************* 2 * Copyright (c) 2007, 2011 Dakshinamurthy Karra, IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Dakshinamurthy Karra (Jalian Systems) - Templates View - https://bugs.eclipse.org/bugs/show_bug.cgi?id=69581 13 * Piotr Maj <pm@jcake.com> - no access to template store and current selection - https://bugs.eclipse.org/bugs/show_bug.cgi?id=296439 14 *******************************************************************************/ 15 package org.eclipse.ui.texteditor.templates; 16 17 import org.eclipse.swt.widgets.Composite; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.Platform; 21 22 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; 23 import org.eclipse.jface.text.templates.persistence.TemplateStore; 24 25 import org.eclipse.ui.IEditorPart; 26 import org.eclipse.ui.IWorkbenchPage; 27 import org.eclipse.ui.IWorkbenchPart; 28 import org.eclipse.ui.PlatformUI; 29 import org.eclipse.ui.part.IContributedContentsView; 30 import org.eclipse.ui.part.IPage; 31 import org.eclipse.ui.part.MessagePage; 32 import org.eclipse.ui.part.PageBook; 33 import org.eclipse.ui.part.PageBookView; 34 35 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds; 36 37 38 /** 39 * The Templates view.hosts {@link ITemplatesPage}s that shows the templates for 40 * the currently active editor part. 41 * <p> 42 * When this view notices an editor being activated, it uses the Eclipse adapter 43 * mechanism to get its {@link ITemplatesPage}. Hence, editors that want to 44 * provide a templates page need to provide such an adapter: 45 * </p> 46 * 47 * <pre> 48 * Object getAdapter() { 49 * ... 50 * if (ITemplatesPage.class.equals(required)) { 51 * if (fTemplatesPage == null) 52 * fTemplatesPage= new JavaTemplatesPage(this); 53 * return fTemplatesPage; 54 * } 55 * } 56 * ... 57 * } 58 * </pre> 59 * <p> 60 * <strong>Note:</strong> This plug-in does not contribute this view. Clients 61 * that want to use this view must check whether it is available and if not, 62 * contribute this view via extension point using the specified view Id 63 * {@link #ID}: 64 * </p> 65 * 66 * <pre> 67 * <extension 68 * point="org.eclipse.ui.views"> 69 * <view 70 * name="%templatesViewName" 71 * icon="$nl$/icons/full/eview16/templates.png" 72 * category="org.eclipse.ui" 73 * class="org.eclipse.ui.texteditor.templates.TemplatesView" 74 * id="org.eclipse.ui.texteditor.TemplatesView"> 75 * </view> 76 * </extension> 77 * </pre> 78 * 79 * The <code>templates.png</code> icon can be copied from this plug-in. 80 * <p> 81 * If the editor supports a templates page, the editor instantiates and 82 * configures the page, and returns it. This page is then added to this 83 * Templates view and immediately made the current page (the Templates view 84 * needs not to be visible). If the editor does not support a templates page, 85 * the Templates view shows a special default page which makes it clear to the 86 * user that no templates are available. When the Templates view notices a 87 * different editor being activated, it flips to the editor's corresponding 88 * templates page. When the templates view notices an editor being closed, it 89 * may destroy the editor's corresponding templates page. 90 * </p> 91 * <p> 92 * This class is not intended to be instantiated or subclassed by clients. 93 * </p> 94 * 95 * @since 3.4 96 * @noinstantiate This class is not intended to be instantiated by clients. 97 */ 98 public final class TemplatesView extends PageBookView { 99 100 /** 101 * The id for this view. 102 * <p> 103 * <strong>Note:</strong> Only this id is allowed when contributing 104 * this view via extension point.</p> 105 */ 106 public static final String ID= "org.eclipse.ui.texteditor.TemplatesView"; //$NON-NLS-1$ 107 108 109 /** 110 * Creates a templates view. 111 */ TemplatesView()112 public TemplatesView() { 113 } 114 115 @Override createDefaultPage(PageBook book)116 protected IPage createDefaultPage(PageBook book) { 117 MessagePage page= new MessagePage(); 118 initPage(page); 119 page.createControl(book); 120 page.setMessage(TemplatesMessages.TemplatesView_no_templates); 121 return page; 122 } 123 124 @Override createPartControl(Composite parent)125 public void createPartControl(Composite parent) { 126 super.createPartControl(parent); 127 Assert.isTrue(ID.equals(getViewSite().getId())); // prevent from contributing this view under a different ID 128 PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook(), IAbstractTextEditorHelpContextIds.TEMPLATES_VIEW); 129 } 130 131 @Override doCreatePage(IWorkbenchPart part)132 protected PageRec doCreatePage(IWorkbenchPart part) { 133 // Try to get template page. 134 ITemplatesPage page= part.getAdapter(ITemplatesPage.class); 135 if (page == null) 136 page= Platform.getAdapterManager().getAdapter(part, ITemplatesPage.class); 137 if (page == null) 138 return null; // There is no template page 139 140 initPage(page); 141 page.createControl(getPageBook()); 142 return new PageRec(part, page); 143 } 144 145 @Override doDestroyPage(IWorkbenchPart part, PageRec rec)146 protected void doDestroyPage(IWorkbenchPart part, PageRec rec) { 147 ITemplatesPage page= (ITemplatesPage)rec.page; 148 page.dispose(); 149 rec.dispose(); 150 } 151 152 @Override getBootstrapPart()153 protected IWorkbenchPart getBootstrapPart() { 154 IWorkbenchPage page= getSite().getPage(); 155 if (page != null) 156 return page.getActiveEditor(); 157 return null; 158 } 159 160 @Override isImportant(IWorkbenchPart part)161 protected boolean isImportant(IWorkbenchPart part) { 162 //We only care about editors 163 return (part instanceof IEditorPart); 164 } 165 166 @Override partBroughtToTop(IWorkbenchPart part)167 public void partBroughtToTop(IWorkbenchPart part) { 168 partActivated(part); 169 } 170 171 @Override getAdapter(Class<T> key)172 public <T> T getAdapter(Class<T> key) { 173 if (key == IContributedContentsView.class) { 174 return key.cast((IContributedContentsView) this::getCurrentContributingPart); 175 } 176 return super.getAdapter(key); 177 } 178 179 /** 180 * Returns the template store of the current page. 181 * 182 * @return the template store, or <code>null</code> if the current page does not provide that 183 * information 184 * @since 3.6 185 */ getTemplateStore()186 public TemplateStore getTemplateStore() { 187 IPage currentPage= getCurrentPage(); 188 if (currentPage instanceof ITemplatesPageExtension) 189 return ((ITemplatesPageExtension)currentPage).getTemplateStore(); 190 return null; 191 } 192 193 /** 194 * Returns the currently selected templates. 195 * 196 * @return array of selected templates, or <code>null</code> if the current page does not 197 * provide that information 198 * @since 3.6 199 */ getSelectedTemplates()200 public TemplatePersistenceData[] getSelectedTemplates() { 201 IPage currentPage= getCurrentPage(); 202 if (currentPage instanceof ITemplatesPageExtension) 203 return ((ITemplatesPageExtension)currentPage).getSelectedTemplates(); 204 return null; 205 } 206 207 } 208