KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > PDETemplateSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.wizards.templates;
13
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.ResourceBundle JavaDoc;
18
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.pde.core.plugin.IPluginBase;
21 import org.eclipse.pde.internal.core.TargetPlatform;
22 import org.eclipse.pde.internal.core.plugin.PluginBase;
23 import org.eclipse.pde.internal.ui.PDEPlugin;
24 import org.eclipse.pde.ui.templates.OptionTemplateSection;
25 import org.osgi.framework.Bundle;
26
27 public abstract class PDETemplateSection extends OptionTemplateSection {
28
29     protected ResourceBundle JavaDoc getPluginResourceBundle() {
30         Bundle JavaDoc bundle = Platform.getBundle(PDEPlugin.getPluginId());
31         return Platform.getResourceBundle(bundle);
32     }
33     
34     protected URL JavaDoc getInstallURL() {
35         return PDEPlugin.getDefault().getInstallURL();
36     }
37     
38     public URL JavaDoc getTemplateLocation() {
39         try {
40             String JavaDoc[] candidates = getDirectoryCandidates();
41             for (int i = 0; i < candidates.length; i++) {
42                 if (PDEPlugin.getDefault().getBundle().getEntry(candidates[i]) != null) {
43                     URL JavaDoc candidate = new URL JavaDoc(getInstallURL(), candidates[i]);
44                     return candidate;
45                 }
46             }
47         } catch (MalformedURLException JavaDoc e) {
48         }
49         return null;
50     }
51
52     private String JavaDoc[] getDirectoryCandidates() {
53         String JavaDoc version = getVersion(model.getPluginBase());
54         if ("3.0".equals(version)) //$NON-NLS-1$
55
return new String JavaDoc[] { "templates_3.0" + "/" + getSectionId() + "/" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
56
if ("3.1".equals(version) || "3.2".equals(version)) //$NON-NLS-1$ //$NON-NLS-2$
57
return new String JavaDoc[] {
58                     "templates_3.2" + "/" + getSectionId() + "/", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
59
"templates_3.1" + "/" + getSectionId() + "/", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
60
"templates_3.0" + "/" + getSectionId() + "/" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
61
return new String JavaDoc[] { "templates" + "/" + getSectionId() + "/" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
62
}
63     
64     private String JavaDoc getVersion(IPluginBase plugin) {
65         // workaround to not introduce new API for IPluginBase
66
if (plugin instanceof PluginBase)
67             return ((PluginBase)plugin).getTargetVersion();
68         return TargetPlatform.getTargetVersionString();
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.pde.ui.templates.ITemplateSection#getFoldersToInclude()
73      */

74     public String JavaDoc[] getNewFiles() {
75         return new String JavaDoc[0];
76     }
77     
78     protected String JavaDoc getFormattedPackageName(String JavaDoc id){
79         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
80         for (int i = 0; i < id.length(); i++) {
81             char ch = id.charAt(i);
82             if (buffer.length() == 0) {
83                 if (Character.isJavaIdentifierStart(ch))
84                     buffer.append(Character.toLowerCase(ch));
85             } else {
86                 if (Character.isJavaIdentifierPart(ch) || ch == '.')
87                     buffer.append(ch);
88             }
89         }
90         return buffer.toString().toLowerCase(Locale.ENGLISH);
91     }
92 }
93
Popular Tags