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 109745 Details for
Bug 239979
[MidletTemplates]: MIDlets templates
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
patch for this bug V1
midletTemplate.patch (text/plain), 109.84 KB, created by
Feng(Marvin) Wang
on 2008-08-12 00:39:11 EDT
(
hide
)
Description:
patch for this bug V1
Filename:
MIME Type:
Creator:
Feng(Marvin) Wang
Created:
2008-08-12 00:39:11 EDT
Size:
109.84 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.mtj.ui >Index: schema/midletTemplateWizard.exsd >=================================================================== >--- schema/midletTemplateWizard.exsd (revision 0) >+++ schema/midletTemplateWizard.exsd (revision 0) >@@ -0,0 +1,163 @@ >+<?xml version='1.0' encoding='UTF-8'?> >+<!-- Schema file written by PDE --> >+<schema targetNamespace="org.eclipse.mtj.ui"> >+<annotation> >+ <appInfo> >+ <meta.schema plugin="org.eclipse.mtj.ui" id="midletTemplateWizard" name="Midlet Template Wizard"/> >+ </appInfo> >+ <documentation> >+ Provides an extension point for registering an Midlet Template Wizard. User can create a Midlet from a Midlet Template Wizard. >+ </documentation> >+ </annotation> >+ >+ <element name="extension"> >+ <complexType> >+ <sequence minOccurs="1" maxOccurs="unbounded"> >+ <element ref="wizard" minOccurs="1" maxOccurs="unbounded"/> >+ </sequence> >+ <attribute name="point" type="string" use="required"> >+ <annotation> >+ <documentation> >+ >+ </documentation> >+ </annotation> >+ </attribute> >+ <attribute name="id" type="string"> >+ <annotation> >+ <documentation> >+ >+ </documentation> >+ </annotation> >+ </attribute> >+ <attribute name="name" type="string"> >+ <annotation> >+ <documentation> >+ >+ </documentation> >+ <appInfo> >+ <meta.attribute translatable="true"/> >+ </appInfo> >+ </annotation> >+ </attribute> >+ </complexType> >+ </element> >+ >+ <element name="wizard"> >+ <complexType> >+ <sequence> >+ <element ref="description" minOccurs="0" maxOccurs="1"/> >+ </sequence> >+ <attribute name="id" type="string" use="required"> >+ <annotation> >+ <documentation> >+ a unique name that will be used to identify this wizard. >+ </documentation> >+ </annotation> >+ </attribute> >+ <attribute name="name" type="string" use="required"> >+ <annotation> >+ <documentation> >+ a translatable name that will be used in UI representation >+of this wizard. >+ </documentation> >+ <appInfo> >+ <meta.attribute translatable="true"/> >+ </appInfo> >+ </annotation> >+ </attribute> >+ <attribute name="class" type="string" use="required"> >+ <annotation> >+ <documentation> >+ a fully qualified name of a class. The class must extends <samp>org.eclipse.mtj.ui.template.MidletTemplateWizard</samp> >+ </documentation> >+ <appInfo> >+ <meta.attribute kind="java" basedOn="org.eclipse.mtj.ui.template.MidletTemplateWizard:"/> >+ </appInfo> >+ </annotation> >+ </attribute> >+ <attribute name="icon" type="string"> >+ <annotation> >+ <documentation> >+ a relative path of an icon that will be used to visually >+represent the wizard. >+ </documentation> >+ <appInfo> >+ <meta.attribute kind="resource"/> >+ </appInfo> >+ </annotation> >+ </attribute> >+ </complexType> >+ </element> >+ >+ <element name="description" type="string"> >+ <annotation> >+ <appInfo> >+ <meta.element translatable="true"/> >+ </appInfo> >+ <documentation> >+ Short description of this wizard. >+ </documentation> >+ </annotation> >+ </element> >+ >+ <annotation> >+ <appInfo> >+ <meta.section type="since"/> >+ </appInfo> >+ <documentation> >+ [Enter the first release in which this extension point appears.] >+ </documentation> >+ </annotation> >+ >+ <annotation> >+ <appInfo> >+ <meta.section type="examples"/> >+ </appInfo> >+ <documentation> >+ <extension >+ point="org.eclipse.mtj.ui.midletTemplateWizard"> >+ <wizard >+ class="org.eclipse.mtj.examples.midlettemplate.helloworld.HelloWorldMidletTemplateWizard" >+ icon="icons/class_obj_green.gif" >+ id="org.eclipse.mtj.ui.templateWizard.helloWorld" >+ name="Hello World Midlet Template"> >+ <description> >+ Hello world Midlet template. This template is just a example template which demonstrate how to create a Midlet template. You may create your own template by mimicing this example. >+ </description> >+ </wizard> >+ </extension> >+ </documentation> >+ </annotation> >+ >+ <annotation> >+ <appInfo> >+ <meta.section type="apiInfo"/> >+ </appInfo> >+ <documentation> >+ The class attribute specified a class must extend <samp>org.eclipse.mtj.ui.template.MidletTemplateWizard</samp> class. >+ </documentation> >+ </annotation> >+ >+ <annotation> >+ <appInfo> >+ <meta.section type="implementation"/> >+ </appInfo> >+ <documentation> >+ [Enter information about supplied implementation of this extension point.] >+ </documentation> >+ </annotation> >+ >+ <annotation> >+ <appInfo> >+ <meta.section type="copyright"/> >+ </appInfo> >+ <documentation> >+ Copyright (c) 2008 Sybase Inc. and others.<br> >+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 <a >+href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a> >+ </documentation> >+ </annotation> >+ >+</schema> >Index: plugin.xml >=================================================================== >--- plugin.xml (revision 345) >+++ plugin.xml (working copy) >@@ -4,6 +4,7 @@ > <extension-point id="deviceEditor" name="Device Editor" schema="schema/org_eclipse_mtj_ui_deviceEditor.exsd"/> > <extension-point id="jadAttributes" name="JAD Attributes" schema="schema/jadAttributes.exsd"/> > <extension-point id="jadEditorPages" name="JAD Editor Pages" schema="schema/jadEditorPages.exsd"/> >+ <extension-point id="midletTemplateWizard" name="Midlet Template Wizard" schema="schema/midletTemplateWizard.exsd"/> > <extension > point="org.eclipse.ui.newWizards"> > <category >@@ -35,6 +36,17 @@ > %wizard.newMidlet.description > </description> > </wizard> >+ <wizard >+ category="mtj.category" >+ class="org.eclipse.mtj.ui.internal.wizards.template.NewMidletFromTemplateWizard" >+ finalPerspective="org.eclipse.jdt.ui.JavaPerspective" >+ icon="icons/newclass_wiz_green.gif" >+ id="org.eclipse.mtj.ui.wizards.NewMidletFromTemplateWizard" >+ name="%wizard.newTemplateMidlet.label"> >+ <description> >+ %wizard.newTemplateMidlet.description >+ </description> >+ </wizard> > </extension> > <extension > point="org.eclipse.ui.importWizards"> >Index: META-INF/MANIFEST.MF >=================================================================== >--- META-INF/MANIFEST.MF (revision 345) >+++ META-INF/MANIFEST.MF (working copy) >@@ -41,4 +41,5 @@ > Bundle-ManifestVersion: 2 > Export-Package: org.eclipse.mtj.ui.devices, > org.eclipse.mtj.ui.jadEditor, >+ org.eclipse.mtj.ui.template, > org.eclipse.mtj.ui.viewers >Index: plugin.properties >=================================================================== >--- plugin.properties (revision 345) >+++ plugin.properties (working copy) >@@ -61,6 +61,9 @@ > wizard.newMidlet.label=Java ME MIDlet > wizard.newMidlet.description=Create a Java ME MIDlet > >+wizard.newTemplateMidlet.label=Java ME MIDlet from Template >+wizard.newTemplateMidlet.description=Create a Java ME MIDlet from a Template >+ > importWizards.EclipseMEProject.label=Import EclipseME Project > importWizards.EclipseMEProject.description=Select a directory to search for existing EclipseME projects. > # >Index: src/org/eclipse/mtj/ui/template/MidletTemplate.java >=================================================================== >--- src/org/eclipse/mtj/ui/template/MidletTemplate.java (revision 0) >+++ src/org/eclipse/mtj/ui/template/MidletTemplate.java (revision 0) >@@ -0,0 +1,147 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+package org.eclipse.mtj.ui.template; >+ >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.mtj.ui.internal.template.TemplateMessages; >+ >+/** >+ * You can subclass this class to create your own template class. >+ * >+ * @author wangf >+ * >+ */ >+public abstract class MidletTemplate { >+ /** >+ * The name of the folder that contains Midlet template. We recommend you >+ * create a folder with this name under project root, and put your templates >+ * into it. For example, your template path may like this: >+ * /%PROJECT_ROOT%/templates/helloWorldTemplate/ >+ */ >+ public static final String TEMPLATE_FOLDER = "templates"; >+ /** >+ * The name of the folder which located in the template folder. All your >+ * Java Source template should be put into this folder. The folder name MUST >+ * be "java". For example, your java source template folder may like this: >+ * /%PROJECT_ROOT%/templates/helloWorldTemplate/java/ >+ */ >+ public static final String JAVA_FOLDER = "java"; >+ /** >+ * The name of the folder which located in the template folder. All your >+ * resource file should be put into this folder. The folder name MUST be >+ * "resource". For example, your resource file folder may like this: >+ * /%PROJECT_ROOT%/templates/helloWorldTemplate/resource/ >+ */ >+ public static final String RESOURCE_FOLDER = "resource"; >+ /** >+ * Every java source template need a package declaration. The package >+ * declaration appear like this: >+ * <p> >+ * package $packageName$; >+ * </p> >+ * $packageName$ will be replaced with the real package name. >+ * >+ */ >+ public static final String OPTION_PACKAGE = "packageName"; >+ /** >+ * The list store all template options. >+ */ >+ private List<TemplateOption> options; >+ >+ public MidletTemplate() { >+ options = new ArrayList<TemplateOption>(); >+ // The default value of "packageName" option is >+ // {@link TemplateMessages.Option_Package_defaultValue}. >+ // You may change the value in the subclass is you don't like it. >+ options.add(new StringOption(OPTION_PACKAGE, >+ TemplateMessages.Option_Package_label, >+ TemplateMessages.Option_Package_defaultValue)); >+ } >+ >+ /** >+ * Get the substitute string for template option. It's default behavior is >+ * to return TemplateOption.getValue().toString(). >+ * >+ * @param optionName >+ * - the name of the template option. >+ * @return - the substitute string of the template option. >+ */ >+ public String getSubstituteStringFor(String optionName) { >+ TemplateOption option = getOption(optionName); >+ if (option != null) { >+ return option.getValue().toString(); >+ } else { >+ return ""; >+ } >+ } >+ >+ /** >+ * Get a list contains all the options. >+ * >+ * @return >+ */ >+ public List<TemplateOption> getOptions() { >+ return options; >+ } >+ >+ /** >+ * Get a option by it's name. >+ * >+ * @param optionName >+ * @return >+ */ >+ public TemplateOption getOption(String optionName) { >+ List<TemplateOption> options = getOptions(); >+ int i = options.indexOf(new StringOption(optionName, null, null)); >+ if (i >= 0) { >+ TemplateOption option = options.get(i); >+ return option; >+ } else { >+ return null; >+ } >+ } >+ >+ /** >+ * Add a template option. >+ * >+ * @param option >+ */ >+ protected void addOption(TemplateOption option) { >+ options.add(option); >+ } >+ >+ /** >+ * Return the path string of the Midlet icon. This path will be added in to >+ * the JAD file. For example, if this method return a String >+ * "/icons/myIcon.gif", your Midlet declaration in the JAD may like this: >+ * <p> >+ * <code>MIDlet-1: MyTemplateMidlet,/icons/myIcon.gif,myj2meapp.helloworld.MyTemplateMidlet</code> >+ * </p> >+ * You may override this method to provide a icon for your template Midlet. >+ * >+ * @return - a string stand for the path of the icon for the Midlet >+ */ >+ protected String getMidletIconPath() { >+ return ""; >+ } >+ >+ /** >+ * Get the name of the Midlet template. This name will display as the title >+ * of the wizard page. >+ * >+ * @return >+ */ >+ public abstract String getName(); >+ >+} >Index: src/org/eclipse/mtj/ui/template/MidletTemplateWizard.java >=================================================================== >--- src/org/eclipse/mtj/ui/template/MidletTemplateWizard.java (revision 0) >+++ src/org/eclipse/mtj/ui/template/MidletTemplateWizard.java (revision 0) >@@ -0,0 +1,686 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+package org.eclipse.mtj.ui.template; >+ >+import java.io.File; >+import java.io.FileInputStream; >+import java.io.FileNotFoundException; >+import java.io.FileOutputStream; >+import java.io.IOException; >+import java.io.InputStream; >+import java.io.InputStreamReader; >+import java.io.OutputStreamWriter; >+import java.io.Reader; >+import java.io.UnsupportedEncodingException; >+import java.lang.reflect.InvocationTargetException; >+import java.net.URL; >+import java.util.Enumeration; >+import java.util.HashMap; >+import java.util.Map; >+import java.util.regex.Matcher; >+import java.util.regex.Pattern; >+import java.util.zip.ZipEntry; >+import java.util.zip.ZipFile; >+ >+import org.eclipse.core.resources.IFile; >+import org.eclipse.core.resources.IResource; >+import org.eclipse.core.resources.IWorkspaceRoot; >+import org.eclipse.core.resources.ResourcesPlugin; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.FileLocator; >+import org.eclipse.core.runtime.IPath; >+import org.eclipse.core.runtime.IProgressMonitor; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.Path; >+import org.eclipse.core.runtime.SafeRunner; >+import org.eclipse.jdt.core.ICompilationUnit; >+import org.eclipse.jdt.core.IJavaElement; >+import org.eclipse.jdt.core.IJavaProject; >+import org.eclipse.jdt.core.IPackageFragmentRoot; >+import org.eclipse.jdt.core.IType; >+import org.eclipse.jdt.core.JavaCore; >+import org.eclipse.jface.operation.IRunnableWithProgress; >+import org.eclipse.jface.wizard.Wizard; >+import org.eclipse.mtj.core.internal.MTJCorePlugin; >+import org.eclipse.mtj.core.internal.utils.Utils; >+import org.eclipse.mtj.core.model.jad.ApplicationDescriptor; >+import org.eclipse.mtj.core.model.project.IMidletSuiteProject; >+import org.eclipse.mtj.core.model.project.MidletSuiteFactory; >+import org.eclipse.mtj.ui.internal.template.MidletTemplateWizardPage; >+import org.eclipse.mtj.ui.internal.template.TemplateMessages; >+import org.eclipse.mtj.ui.internal.utils.LogAndDisplaySafeRunnable; >+import org.eclipse.mtj.ui.internal.wizards.template.TemplateWizardMessages; >+ >+/** >+ * You should subclass this class to create your MidletTemplateWizard. You >+ * should >+ * <p> >+ * 1. Provide a MidletTemplate by override {@link #createTemplate()} method >+ * <p> >+ * 2. Provide the URL of your template by override >+ * {@link #getTemplateFolderURL()} >+ * <p> >+ * Typically you do not need add any more page to this wizard. >+ * >+ * @author wangf >+ * >+ */ >+public abstract class MidletTemplateWizard extends Wizard { >+ >+ private MidletTemplate template; >+ private Pattern SUBSTITUTION_PATTERN = Pattern.compile("\\$.*\\$"); >+ /** >+ * The Java source folder of your MTJ Midlet Suite project. You can choose >+ * it on the wizard page. >+ */ >+ private IPackageFragmentRoot javaRoot; >+ /** >+ * The resource folder of your MTJ Midlet Suite project. You can choose it >+ * on the wizard page. >+ */ >+ private IPackageFragmentRoot resourceRoot; >+ >+ public MidletTemplateWizard() { >+ this.template = createTemplate(); >+ setWindowTitle(TemplateMessages.MidletTemplateWizard_title); >+ setNeedsProgressMonitor(true); >+ } >+ >+ @Override >+ public void addPages() { >+ MidletTemplateWizardPage page = new MidletTemplateWizardPage(template); >+ addPage(page); >+ } >+ >+ @Override >+ public boolean performFinish() { >+ boolean completed = true; >+ >+ IRunnableWithProgress runable = getCreateMidletOperation(); >+ try { >+ getContainer().run(false, true, runable); >+ } catch (InvocationTargetException e) { >+ MTJCorePlugin.log(IStatus.ERROR, e); >+ completed = false; >+ } catch (InterruptedException e) { >+ MTJCorePlugin.log(IStatus.ERROR, e); >+ completed = false; >+ } >+ >+ return completed; >+ } >+ >+ /** >+ * Set the Java source folder of your MTJ Midlet Suite project. User can >+ * choose the folder on the wizard page. >+ * >+ * @param javaRoot >+ */ >+ public void setJavaRoot(IPackageFragmentRoot javaRoot) { >+ this.javaRoot = javaRoot; >+ } >+ >+ /** >+ * Set the resource folder of your MTJ Midlet Suite project. User can choose >+ * it on the wizard page. >+ * >+ * @param resourceRoot >+ */ >+ public void setResourceRoot(IPackageFragmentRoot resourceRoot) { >+ this.resourceRoot = resourceRoot; >+ } >+ >+ /** >+ * Return a IRunnableWithProgress that will run in the >+ * {@link #performFinish()} method. This runnable create the Midlet from >+ * template. >+ * >+ * @return >+ */ >+ private IRunnableWithProgress getCreateMidletOperation() { >+ IRunnableWithProgress runnable = null; >+ >+ URL templateFolderURL = getTemplateFolderURL(); >+ try { >+ templateFolderURL = FileLocator.resolve(templateFolderURL); >+ templateFolderURL = FileLocator.toFileURL(templateFolderURL); >+ } catch (IOException e) { >+ return null; >+ } >+ if ("file".equals(templateFolderURL.getProtocol())) { //$NON-NLS-1$ >+ runnable = getDirCopyRunable(templateFolderURL); >+ } else if ("jar".equals(templateFolderURL.getProtocol())) { //$NON-NLS-1$ >+ runnable = getZipCopyRunable(templateFolderURL); >+ } >+ >+ return runnable; >+ } >+ >+ /** >+ * Get a IRunnableWithProgress which perform the operation that copy >+ * templates from a jar file. >+ * >+ * @param jarURL >+ * - the URL of the Jar file which contains template. >+ * @return >+ */ >+ private IRunnableWithProgress getZipCopyRunable(URL jarURL) { >+ final String file = jarURL.getFile(); >+ final int exclamation = file.indexOf('!'); >+ if (exclamation < 0) { >+ return null; >+ } >+ IRunnableWithProgress runnable = new IRunnableWithProgress() { >+ >+ public void run(IProgressMonitor monitor) >+ throws InvocationTargetException, InterruptedException { >+ SafeRunner.run(new LogAndDisplaySafeRunnable(getShell(), >+ TemplateWizardMessages.ErrorDialogTitle_duringCopy) { >+ >+ public void run() throws Exception { >+ URL fileUrl = new URL(file.substring(0, exclamation)); >+ File pluginJar = new File(fileUrl.getFile()); >+ String templateDir = file.substring(exclamation + 1); >+ IPath templatePath = new Path(templateDir); >+ ZipFile zipFile = null; >+ try { >+ zipFile = new ZipFile(pluginJar); >+ generateFilesFromZip(zipFile, templatePath); >+ } finally { >+ if (zipFile != null) { >+ zipFile.close(); >+ } >+ } >+ } >+ >+ }); >+ >+ } >+ >+ }; >+ return runnable; >+ } >+ >+ /** >+ * Generate Midlet files including Java source code and resource files from >+ * jar file. >+ * >+ * @param zipFile >+ * @param templatePath >+ * @throws CoreException >+ * @throws IOException >+ */ >+ private void generateFilesFromZip(ZipFile zipFile, IPath templatePath) >+ throws IOException, CoreException { >+ Map<IPath, ZipEntry>[] entryMaps = collectJavaAndResourceEntries( >+ zipFile, templatePath); >+ >+ Map<IPath, ZipEntry> javaEntries = entryMaps[0]; >+ copyAndModifyJavaTemplateFromZip(zipFile, javaEntries); >+ >+ Map<IPath, ZipEntry> resourceEntries = entryMaps[1]; >+ copyResoueceFromZip(zipFile, resourceEntries); >+ } >+ >+ /** >+ * Return a array contains 2 maps. The first map contains all the Java >+ * source file template ZIP entries. The second map contains all the >+ * resource file ZIP entries. >+ * >+ * @param zipFile >+ * @param templatePath >+ * @return >+ */ >+ private Map<IPath, ZipEntry>[] collectJavaAndResourceEntries( >+ ZipFile zipFile, IPath templatePath) { >+ IPath javaPath = templatePath.append(MidletTemplate.JAVA_FOLDER); >+ IPath resourcePath = templatePath >+ .append(MidletTemplate.RESOURCE_FOLDER); >+ >+ Map<IPath, ZipEntry> javaEntries = new HashMap<IPath, ZipEntry>(); >+ Map<IPath, ZipEntry> resourceEntries = new HashMap<IPath, ZipEntry>(); >+ >+ for (Enumeration<?> zipEntries = zipFile.entries(); zipEntries >+ .hasMoreElements();) { >+ ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement(); >+ IPath entryPath = new Path(zipEntry.getName()); >+ if (javaPath.isPrefixOf(entryPath) && !zipEntry.isDirectory()) { >+ IPath key = entryPath.removeFirstSegments(javaPath >+ .segmentCount()); >+ javaEntries.put(key, zipEntry); >+ } else if (resourcePath.isPrefixOf(entryPath) >+ && !zipEntry.isDirectory()) { >+ IPath key = entryPath.removeFirstSegments(resourcePath >+ .segmentCount()); >+ resourceEntries.put(key, zipEntry); >+ } >+ } >+ @SuppressWarnings("unchecked") >+ // Do NOT change the sequence of the 2 element >+ Map<IPath, ZipEntry>[] entryMaps = new Map[] { javaEntries, >+ resourceEntries }; >+ return entryMaps; >+ } >+ >+ /** >+ * Get a IRunnableWithProgress which perform the operation that copy >+ * templates from a folder. >+ * >+ * @param templateFolderURL >+ * - The URL of the folder which contains template. >+ * @return >+ */ >+ private IRunnableWithProgress getDirCopyRunable(URL templateFolderURL) { >+ File templateDirectory = new File(templateFolderURL.getFile()); >+ if (!templateDirectory.exists()) >+ return null; >+ final File javaTemplateDir = new File(templateDirectory, >+ MidletTemplate.JAVA_FOLDER); >+ final File resourceFileDir = new File(templateDirectory, >+ MidletTemplate.RESOURCE_FOLDER); >+ >+ IRunnableWithProgress runnable = new IRunnableWithProgress() { >+ >+ public void run(final IProgressMonitor monitor) >+ throws InvocationTargetException, InterruptedException { >+ SafeRunner.run(new LogAndDisplaySafeRunnable(getShell(), >+ TemplateWizardMessages.ErrorDialogTitle_duringCopy) { >+ >+ public void run() throws Exception { >+ // Do copy >+ copyAndModifyJavaTemplateFromDir(javaTemplateDir, >+ getMidletProjectPackageFolder(), monitor); >+ copyResourceFromDir(resourceFileDir, monitor); >+ // refresh >+ javaRoot.getResource().refreshLocal( >+ IResource.DEPTH_INFINITE, monitor); >+ resourceRoot.getResource().refreshLocal( >+ IResource.DEPTH_INFINITE, monitor); >+ } >+ >+ }); >+ } >+ >+ }; >+ return runnable; >+ } >+ >+ /** >+ * Copy & substitute template string of all Java template ZIP entries from >+ * jar file. And then add Midlet declaration into JAD. >+ * >+ * @param zipFile >+ * - Jar file contains templates. >+ * @param javaEntries >+ * - a map contains all the Java template ZIP entries. >+ * @throws UnsupportedEncodingException >+ * @throws IOException >+ * @throws FileNotFoundException >+ * @throws CoreException >+ */ >+ private void copyAndModifyJavaTemplateFromZip(ZipFile zipFile, >+ Map<IPath, ZipEntry> javaEntries) >+ throws UnsupportedEncodingException, IOException, >+ FileNotFoundException, CoreException { >+ for (IPath key : javaEntries.keySet()) { >+ // Get StringBuffer before template substituting >+ StringBuffer sb = getSbBeforeSubstituteTemplates(zipFile >+ .getInputStream(javaEntries.get(key))); >+ // Get StringBuffer before template substituting >+ sb = getSbAfterSubstituteTemplates(sb); >+ // Get the absolute path of the destination file. >+ IPath destFilePath = getMidletProjectPackagePath().append(key); >+ String destFileName = substituteJavaTemplateFileName(destFilePath >+ .lastSegment()); >+ destFilePath = destFilePath.removeLastSegments(1).append( >+ destFileName); >+ // Do file writing >+ writeFile(new File(destFilePath.toOSString()), sb); >+ // Must refresh local before add Midlet declaration to JAD >+ javaRoot.getResource().refreshLocal(IResource.DEPTH_INFINITE, null); >+ // Add Midlet declaration to JAD >+ addMidletToJAD(destFilePath.toFile(), null); >+ } >+ } >+ >+ /** >+ * Copy & substitute template string of all Java template files from a >+ * directory contains templates. And then add Midlet declaration into JAD. >+ * >+ * @param source >+ * @param destination >+ * @param monitor >+ * @throws IOException >+ * @throws CoreException >+ */ >+ private void copyAndModifyJavaTemplateFromDir(File source, >+ File destination, IProgressMonitor monitor) throws IOException, >+ CoreException { >+ if (source.exists() && source.isDirectory()) { >+ destination.mkdirs(); >+ >+ File[] sources = source.listFiles(); >+ for (int i = 0; i < sources.length; i++) { >+ File currentSrc = sources[i]; >+ String currentDestName = substituteJavaTemplateFileName(currentSrc >+ .getName()); >+ File currentDest = new File(destination, currentDestName); >+ >+ if (currentSrc.isDirectory()) { >+ copyAndModifyJavaTemplateFromDir(currentSrc, currentDest, >+ monitor); >+ } else { >+ StringBuffer sb = getSbBeforeSubstituteTemplates(new FileInputStream( >+ currentSrc)); >+ sb = getSbAfterSubstituteTemplates(sb); >+ writeFile(currentDest, sb); >+ // Must refresh local before add Midlet declaration to JAD >+ javaRoot.getResource().refreshLocal( >+ IResource.DEPTH_INFINITE, monitor); >+ // Add Midlet declaration to JAD >+ addMidletToJAD(currentDest, monitor); >+ } >+ } >+ } >+ } >+ >+ /** >+ * Copy all of the resource files from Jar file to MTJ Midlet suite project >+ * resource folder. >+ * >+ * @param zipFile >+ * - the Jar file contains templates >+ * @param resourceEntries >+ * - a map contains all the resource ZIP entries. >+ * @throws IOException >+ * @throws CoreException >+ */ >+ private void copyResoueceFromZip(ZipFile zipFile, >+ Map<IPath, ZipEntry> resourceEntries) throws IOException, >+ CoreException { >+ for (IPath key : resourceEntries.keySet()) { >+ InputStream in = zipFile.getInputStream(resourceEntries.get(key)); >+ IPath destFilePath = getMidletProjectResourcePath().append(key); >+ writeFile(destFilePath, in); >+ resourceRoot.getResource().refreshLocal(IResource.DEPTH_INFINITE, >+ null); >+ } >+ } >+ >+ /** >+ * Copy all of the resource files from template resource directory to MTJ >+ * Midlet suite project resource folder. >+ * >+ * @param resourceDir >+ * - Directory contains template resource files. >+ * @param monitor >+ * @throws IOException >+ */ >+ private void copyResourceFromDir(File resourceDir, IProgressMonitor monitor) >+ throws IOException { >+ Utils.copyDirectory(resourceDir, getMidletProjectResourceDir(), null); >+ } >+ >+ /** >+ * Get the Java package folder of the MTJ Midlet Suite Project. >+ * >+ * @return >+ */ >+ private File getMidletProjectPackageFolder() { >+ IPath packagePath = getMidletProjectPackagePath(); >+ File packageDir = packagePath.toFile(); >+ return packageDir; >+ } >+ >+ /** >+ * Get the Java package path of the MTJ Midlet Suite Project. >+ * >+ * @return >+ */ >+ private IPath getMidletProjectPackagePath() { >+ IPath srcPath = getMidletProjectSrcPath(); >+ String packageName = template >+ .getSubstituteStringFor(MidletTemplate.OPTION_PACKAGE); >+ IPath packagePath = srcPath.append(packageName.replace(".", "/"));//$NON-NLS-1$ >+ return packagePath; >+ } >+ >+ /** >+ * Get the path of the MTJ Midlet Suite project Source folder. >+ * >+ * @return >+ */ >+ private IPath getMidletProjectSrcPath() { >+ IPath srcPath = null; >+ srcPath = javaRoot.getResource().getLocation(); >+ return srcPath; >+ } >+ >+ /** >+ * Get the MTJ Midlet Suite project resource folder. >+ * >+ * @return >+ */ >+ private File getMidletProjectResourceDir() { >+ IPath resourcePath = getMidletProjectResourcePath(); >+ return resourcePath.toFile(); >+ } >+ >+ /** >+ * Get the MTJ Midlet Suite project resource path. >+ * >+ * @return >+ */ >+ private IPath getMidletProjectResourcePath() { >+ IPath resourcePath = resourceRoot.getResource().getLocation(); >+ return resourcePath; >+ } >+ >+ /** >+ * Write a StringBuffer to a file. >+ * >+ * @param destFile >+ * - the destination file. >+ * @param sb >+ * - the StringBuffer to be written to the destination file. >+ * @throws FileNotFoundException >+ * @throws UnsupportedEncodingException >+ * @throws IOException >+ */ >+ private void writeFile(File destFile, StringBuffer sb) >+ throws FileNotFoundException, UnsupportedEncodingException, >+ IOException { >+ // Make necessary Dirs >+ IPath path = new Path(destFile.getPath()); >+ path.removeLastSegments(1).toFile().mkdirs(); >+ // Do write >+ FileOutputStream fos = new FileOutputStream(destFile); >+ OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8"); >+ writer.write(sb.toString()); >+ writer.close(); >+ } >+ >+ /** >+ * Write the content from a InputStream to a file. >+ * >+ * @param destPath >+ * - the path of the destination file. >+ * @param in >+ * - the ImputSteam whose content will be written to the >+ * destination file. >+ * @throws IOException >+ */ >+ private void writeFile(IPath destPath, InputStream in) throws IOException { >+ // Make necessary Dirs >+ destPath.removeLastSegments(1).toFile().mkdirs(); >+ // Do write >+ FileOutputStream out = new FileOutputStream(destPath.toFile()); >+ Utils.copyInputToOutput(in, out); >+ } >+ >+ /** >+ * Add the created Midlet declaration to JAD file. >+ * >+ * @param javaSourceFile >+ * - the Java source file just generated from a template. >+ * @param monitor >+ * @throws IOException >+ * @throws CoreException >+ */ >+ private void addMidletToJAD(File javaSourceFile, IProgressMonitor monitor) >+ throws IOException, CoreException { >+ // create ICompilationUnit from File >+ String path = javaSourceFile.getAbsolutePath(); >+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); >+ IFile javaSrcFile = root.getFileForLocation(new Path(path)); >+ ICompilationUnit cu = (ICompilationUnit) JavaCore.create(javaSrcFile); >+ if (cu == null) { >+ return; >+ } >+ // find Midlets and add them to JAD >+ IType[] types = cu.getAllTypes(); >+ for (IType type : types) { >+ if (Utils.isMidlet(type, monitor)) { >+ writeMidletDeclarationToJAD(type, monitor); >+ } >+ } >+ } >+ >+ /** >+ * Add Midlet declaration to JAD file. >+ * >+ * @param midlet >+ * @param monitor >+ * >+ * @throws IOException >+ * @throws CoreException >+ */ >+ private void writeMidletDeclarationToJAD(IType midlet, >+ IProgressMonitor monitor) throws IOException, CoreException { >+ // Pull out the midlet suite that the type was created in >+ IJavaProject javaProject = (IJavaProject) midlet >+ .getAncestor(IJavaElement.JAVA_PROJECT); >+ IMidletSuiteProject midletSuite = MidletSuiteFactory >+ .getMidletSuiteProject(javaProject); >+ >+ ApplicationDescriptor desc = midletSuite.getApplicationDescriptor(); >+ if (desc != null) { >+ ApplicationDescriptor.MidletDefinition def = new ApplicationDescriptor.MidletDefinition( >+ desc.getMidletCount() + 1, midlet.getElementName(), >+ template.getMidletIconPath(), midlet >+ .getFullyQualifiedName()); >+ desc.addMidletDefinition(def); >+ desc.store(); >+ >+ IFile jadFile = midletSuite.getApplicationDescriptorFile(); >+ jadFile.refreshLocal(IResource.DEPTH_ONE, monitor); >+ } >+ } >+ >+ /** >+ * The Java template file name may need to be substituted. >+ * >+ * @param fileName >+ * @return >+ */ >+ private String substituteJavaTemplateFileName(String fileName) { >+ StringBuffer sb = getSbAfterSubstituteTemplates(new StringBuffer( >+ fileName)); >+ return sb.toString(); >+ } >+ >+ /** >+ * Substitute all the template options. Before substituting, A StringBuffer >+ * may appear as "abc-$toBeSubstituted$-xyz"; after substituting, the >+ * StringBuffer will be like this: "abc-substituted-xyz". >+ * <p> >+ * "$toBeSubstituted$" is the template option, "substituted" is the value of >+ * the template option. >+ * >+ * @param sb >+ * @return >+ */ >+ private StringBuffer getSbAfterSubstituteTemplates(StringBuffer sb) { >+ // Replace template values >+ int offset = 0; >+ Matcher matcher = SUBSTITUTION_PATTERN.matcher(sb); >+ >+ while (matcher.find(offset)) { >+ // The matched string may like this: $toBeSubstituted$ >+ String referencedValue = matcher.group(); >+ // The name of the template option may like this: toBeSubstituted >+ String optionName = referencedValue.substring(1, referencedValue >+ .length() - 1); >+ // The string used to substitute the template option string. >+ String resolvedValue = template.getSubstituteStringFor(optionName); >+ // do substitute >+ sb.replace(matcher.start(), matcher.end(), resolvedValue); >+ // Figure out the new offset, based on the replaced >+ // string length >+ offset = matcher.start() + resolvedValue.length(); >+ } >+ return sb; >+ } >+ >+ /** >+ * Get a StringBuffer from a InputStream. >+ * >+ * @param is >+ * @return >+ * @throws UnsupportedEncodingException >+ * @throws IOException >+ */ >+ private StringBuffer getSbBeforeSubstituteTemplates(InputStream is) >+ throws UnsupportedEncodingException, IOException { >+ // Read in the template >+ int charsRead; >+ char[] buffer = new char[1024]; >+ >+ Reader reader = new InputStreamReader(is, "UTF-8");//$NON-NLS-N$ >+ StringBuffer sb = new StringBuffer(); >+ while ((charsRead = reader.read(buffer)) != -1) { >+ sb.append(buffer, 0, charsRead); >+ } >+ >+ is.close(); >+ return sb; >+ } >+ >+ /** >+ * Get the URL of the template folder. You should implement this method in >+ * your subclass. Typically, your method may appear like this: >+ * <p> >+ * <code>@Override</code> >+ * <p> >+ * <code>protected URL getTemplateFolderURL() {</code> >+ * <p> >+ * <code> URL templateFolderURL = getClass().getResource(</code> >+ * <p> >+ * <code> "/" + MidletTemplate.TEMPLATE_FOLDER + "/helloWorld");</code> >+ * <p> >+ * <code>return templateFolderURL;</code> >+ * <p> >+ * <code><p>}</code> >+ * <p> >+ * >+ * @return - the URL of the folder contains your template. >+ */ >+ protected abstract URL getTemplateFolderURL(); >+ >+ /** >+ * Create and return a subclass of {@link MidletTemplate}. The subclass will >+ * be used to substitute template strings in your template. >+ * >+ * @return - a MidletTemplate >+ */ >+ protected abstract MidletTemplate createTemplate(); >+} >Index: src/org/eclipse/mtj/ui/template/StringOption.java >=================================================================== >--- src/org/eclipse/mtj/ui/template/StringOption.java (revision 0) >+++ src/org/eclipse/mtj/ui/template/StringOption.java (revision 0) >@@ -0,0 +1,70 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.template; >+ >+import org.eclipse.mtj.ui.internal.template.MidletTemplateWizardPage; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.events.ModifyEvent; >+import org.eclipse.swt.events.ModifyListener; >+import org.eclipse.swt.layout.GridData; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Text; >+ >+/** >+ * This class is used to provide a template option that contains a string value. >+ * When you add a StringOption to your {@link MidletTemplate}, the >+ * {@link MidletTemplateWizardPage} will appear contains one more string field >+ * that allow the user to change the default value of this StringOption. >+ * >+ * @author wangf >+ * >+ */ >+public class StringOption extends TemplateOption { >+ /** >+ * @see TemplateOption#TemplateOption(String, String, Object) >+ * @param name >+ * @param label >+ * @param defaultValue >+ */ >+ public StringOption(String name, String label, String defaultValue) { >+ super(name, label, defaultValue); >+ } >+ >+ /** >+ * Create a single line Text control with a label. The control will display >+ * on the {@link MidletTemplateWizardPage}. User can change the default >+ * option value by set the text of Text control. >+ */ >+ @Override >+ public void createControl(Composite container, int span) { >+ // Create the label of the Text. >+ createLabel(container); >+ // Create the text. >+ final Text text = new Text(container, SWT.SINGLE | SWT.BORDER); >+ if (getValue() != null) { >+ text.setText(getValue().toString()); >+ } >+ GridData gd = new GridData(GridData.FILL_HORIZONTAL); >+ gd.horizontalSpan = span - 1; >+ text.setLayoutData(gd); >+ >+ text.addModifyListener(new ModifyListener() { >+ >+ public void modifyText(ModifyEvent e) { >+ StringOption.this.setValue(text.getText()); >+ } >+ >+ }); >+ } >+ >+} >Index: src/org/eclipse/mtj/ui/template/TemplateOption.java >=================================================================== >--- src/org/eclipse/mtj/ui/template/TemplateOption.java (revision 0) >+++ src/org/eclipse/mtj/ui/template/TemplateOption.java (revision 0) >@@ -0,0 +1,129 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.template; >+ >+import org.eclipse.mtj.ui.internal.template.MidletTemplateWizardPage; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Label; >+ >+/** >+ * This class is a base class of template options. >+ * >+ * @author wangf >+ * >+ */ >+public abstract class TemplateOption { >+ private String name; >+ private String label; >+ private Object value; >+ >+ /** >+ * The constructor. >+ * >+ * @param name >+ * - the name of the template option. For example, you create a >+ * TemplateOption for a template key string $toBeSubstituted$, >+ * the name of this TemplateOption must be "toBeSubstituted" >+ * @param label >+ * - the label of the template option. It will appear as a label >+ * on the UI {@link MidletTemplateWizardPage}. >+ * @param value >+ * - the default value of the TemplateOption. By default(if you >+ * don't override >+ * {@link MidletTemplate#getSubstituteStringFor(String optionName)} >+ * method), value.toString() will be used as the substitute >+ * string of this TemplateOption. >+ */ >+ public TemplateOption(String name, String label, Object value) { >+ this.name = name; >+ this.label = label; >+ this.value = value; >+ } >+ >+ /** >+ * create a label and set the text to the "label" field variable of this >+ * class. >+ * >+ * @param parent >+ * @return >+ */ >+ protected Label createLabel(Composite parent) { >+ Label label = new Label(parent, SWT.NULL); >+ label.setText(getLabel()); >+ return label; >+ } >+ >+ /** >+ * Create a control for this template option which will be displayed on >+ * {@link MidletTemplateWizardPage} >+ * >+ * @param container >+ * @param span >+ */ >+ public abstract void createControl(Composite container, int span); >+ >+ @Override >+ public int hashCode() { >+ final int prime = 31; >+ int result = 1; >+ result = prime * result + ((name == null) ? 0 : name.hashCode()); >+ return result; >+ } >+ >+ /** >+ * If the name of two objects equal with each other, we assume the objects >+ * equal with each other. Do NOT change the equals behavior in the subclass. >+ */ >+ @Override >+ public boolean equals(Object obj) { >+ if (this == obj) >+ return true; >+ if (obj == null) >+ return false; >+ if (getClass() != obj.getClass()) >+ return false; >+ TemplateOption other = (TemplateOption) obj; >+ if (name == null) { >+ if (other.name != null) >+ return false; >+ } else if (!name.equals(other.name)) >+ return false; >+ return true; >+ } >+ >+ public String getName() { >+ return name; >+ } >+ >+ public void setName(String name) { >+ this.name = name; >+ } >+ >+ public String getLabel() { >+ return label; >+ } >+ >+ public void setLabel(String label) { >+ this.label = label; >+ } >+ >+ public Object getValue() { >+ return value; >+ } >+ >+ public void setValue(Object value) { >+ this.value = value; >+ } >+ >+} >Index: src/org/eclipse/mtj/ui/MtjPerspectiveFactory.java >=================================================================== >--- src/org/eclipse/mtj/ui/MtjPerspectiveFactory.java (revision 345) >+++ src/org/eclipse/mtj/ui/MtjPerspectiveFactory.java (working copy) >@@ -25,94 +25,97 @@ > */ > public class MtjPerspectiveFactory implements IPerspectiveFactory { > >- private IPageLayout layout; >+ private IPageLayout layout; > >- /* >- * (non-Javadoc) >- * >- * @see org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui.IPageLayout) >- */ >- public void createInitialLayout(IPageLayout layout) { >- this.layout = layout; >- addViews(); >- addActionSets(); >- addNewWizardShortcuts(); >- addPerspectiveShortcuts(); >- addViewShortcuts(); >- } >+ /* >+ * (non-Javadoc) >+ * >+ * @see >+ * org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui >+ * .IPageLayout) >+ */ >+ public void createInitialLayout(IPageLayout layout) { >+ this.layout = layout; >+ addViews(); >+ addActionSets(); >+ addNewWizardShortcuts(); >+ addPerspectiveShortcuts(); >+ addViewShortcuts(); >+ } > >- private void addPerspectiveShortcuts() { >- layout.addPerspectiveShortcut(JavaUI.ID_PERSPECTIVE); >- layout.addPerspectiveShortcut(IDebugUIConstants.ID_DEBUG_PERSPECTIVE); >- } >+ private void addPerspectiveShortcuts() { >+ layout.addPerspectiveShortcut(JavaUI.ID_PERSPECTIVE); >+ layout.addPerspectiveShortcut(IDebugUIConstants.ID_DEBUG_PERSPECTIVE); >+ } > >- private void addViews() { >- // Add Package Explorer, Navigator and Hierarchy views to left >- IFolderLayout topLeft = layout.createFolder("topLeft", //$NON-NLS-1$ >- IPageLayout.LEFT, 0.25f, layout.getEditorArea()); >- topLeft.addView(JavaUI.ID_PACKAGES); >- topLeft.addView(IPageLayout.ID_RES_NAV); >- topLeft.addView(JavaUI.ID_TYPE_HIERARCHY); >- // Add Problems, Javadoc and Tasks views to bottom >- IFolderLayout bottom = layout.createFolder("bottomRight", //$NON-NLS-1$ >- IPageLayout.BOTTOM, 0.75f, layout.getEditorArea()); >- bottom.addView(IPageLayout.ID_PROBLEM_VIEW); >- bottom.addView(JavaUI.ID_JAVADOC_VIEW); >- bottom.addView(IPageLayout.ID_TASK_LIST); >- // Add Outline view to right >- layout.addView(IPageLayout.ID_OUTLINE, IPageLayout.RIGHT, 0.75f, layout >- .getEditorArea()); >- } >+ private void addViews() { >+ // Add Package Explorer, Navigator and Hierarchy views to left >+ IFolderLayout topLeft = layout.createFolder("topLeft", //$NON-NLS-1$ >+ IPageLayout.LEFT, 0.25f, layout.getEditorArea()); >+ topLeft.addView(JavaUI.ID_PACKAGES); >+ topLeft.addView(IPageLayout.ID_RES_NAV); >+ topLeft.addView(JavaUI.ID_TYPE_HIERARCHY); >+ // Add Problems, Javadoc and Tasks views to bottom >+ IFolderLayout bottom = layout.createFolder("bottomRight", //$NON-NLS-1$ >+ IPageLayout.BOTTOM, 0.75f, layout.getEditorArea()); >+ bottom.addView(IPageLayout.ID_PROBLEM_VIEW); >+ bottom.addView(JavaUI.ID_JAVADOC_VIEW); >+ bottom.addView(IPageLayout.ID_TASK_LIST); >+ // Add Outline view to right >+ layout.addView(IPageLayout.ID_OUTLINE, IPageLayout.RIGHT, 0.75f, layout >+ .getEditorArea()); >+ } > >- private void addActionSets() { >- layout.addActionSet(IDebugUIConstants.LAUNCH_ACTION_SET); >- layout.addActionSet(JavaUI.ID_ACTION_SET); >- layout.addActionSet(JavaUI.ID_ELEMENT_CREATION_ACTION_SET); >- layout.addActionSet(IPageLayout.ID_NAVIGATE_ACTION_SET); >- } >+ private void addActionSets() { >+ layout.addActionSet(IDebugUIConstants.LAUNCH_ACTION_SET); >+ layout.addActionSet(JavaUI.ID_ACTION_SET); >+ layout.addActionSet(JavaUI.ID_ELEMENT_CREATION_ACTION_SET); >+ layout.addActionSet(IPageLayout.ID_NAVIGATE_ACTION_SET); >+ } > >- private void addNewWizardShortcuts() { >- layout >- .addNewWizardShortcut("org.eclipse.mtj.ui.wizards.NewJ2MEProjectWizard"); //$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.mtj.ui.wizards.NewMidletWizard"); //$NON-NLS-1$ >+ private void addNewWizardShortcuts() { >+ layout >+ .addNewWizardShortcut("org.eclipse.mtj.ui.wizards.NewJ2MEProjectWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.mtj.ui.wizards.NewMidletWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.mtj.ui.wizards.NewMidletFromTemplateWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewPackageCreationWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewClassCreationWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewInterfaceCreationWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewSourceFolderCreationWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewSnippetFileCreationWizard"); //$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewJavaWorkingSetWizard"); //$NON-NLS-1$ >+ layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.folder");//$NON-NLS-1$ >+ layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.file");//$NON-NLS-1$ >+ layout >+ .addNewWizardShortcut("org.eclipse.ui.editors.wizards.UntitledTextFileWizard");//$NON-NLS-1$ >+ } > >- layout >- .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewPackageCreationWizard"); //$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewClassCreationWizard"); //$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewInterfaceCreationWizard"); //$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewSourceFolderCreationWizard"); //$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewSnippetFileCreationWizard"); //$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.jdt.ui.wizards.NewJavaWorkingSetWizard"); //$NON-NLS-1$ >- layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.folder");//$NON-NLS-1$ >- layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.file");//$NON-NLS-1$ >- layout >- .addNewWizardShortcut("org.eclipse.ui.editors.wizards.UntitledTextFileWizard");//$NON-NLS-1$ >- } >+ private void addViewShortcuts() { >+ // views - error log >+ layout.addShowViewShortcut("org.eclipse.pde.runtime.LogView"); //$NON-NLS-1$ > >- private void addViewShortcuts() { >- // views - error log >- layout.addShowViewShortcut("org.eclipse.pde.runtime.LogView"); //$NON-NLS-1$ >+ // views - java >+ layout.addShowViewShortcut(JavaUI.ID_PACKAGES); >+ layout.addShowViewShortcut(JavaUI.ID_TYPE_HIERARCHY); >+ layout.addShowViewShortcut(JavaUI.ID_SOURCE_VIEW); >+ layout.addShowViewShortcut(JavaUI.ID_JAVADOC_VIEW); > >- // views - java >- layout.addShowViewShortcut(JavaUI.ID_PACKAGES); >- layout.addShowViewShortcut(JavaUI.ID_TYPE_HIERARCHY); >- layout.addShowViewShortcut(JavaUI.ID_SOURCE_VIEW); >- layout.addShowViewShortcut(JavaUI.ID_JAVADOC_VIEW); >+ // views - debugging >+ layout.addShowViewShortcut(IConsoleConstants.ID_CONSOLE_VIEW); > >- // views - debugging >- layout.addShowViewShortcut(IConsoleConstants.ID_CONSOLE_VIEW); >- >- // views - standard workbench >- layout.addShowViewShortcut(IPageLayout.ID_OUTLINE); >- layout.addShowViewShortcut(IPageLayout.ID_PROBLEM_VIEW); >- layout.addShowViewShortcut(IPageLayout.ID_RES_NAV); >- layout.addShowViewShortcut(IPageLayout.ID_TASK_LIST); >- layout.addShowViewShortcut(IProgressConstants.PROGRESS_VIEW_ID); >- } >+ // views - standard workbench >+ layout.addShowViewShortcut(IPageLayout.ID_OUTLINE); >+ layout.addShowViewShortcut(IPageLayout.ID_PROBLEM_VIEW); >+ layout.addShowViewShortcut(IPageLayout.ID_RES_NAV); >+ layout.addShowViewShortcut(IPageLayout.ID_TASK_LIST); >+ layout.addShowViewShortcut(IProgressConstants.PROGRESS_VIEW_ID); >+ } > } >Index: src/org/eclipse/mtj/ui/internal/wizards/importer/eclipseme/StatusUtil.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/importer/eclipseme/StatusUtil.java (revision 345) >+++ src/org/eclipse/mtj/ui/internal/wizards/importer/eclipseme/StatusUtil.java (working copy) >@@ -1,65 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2000, 2008 IBM Corporation 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: >- * IBM Corporation - initial API and implementation >- *******************************************************************************/ >-/******************************************************************************* >- * Copyright (c) 2008 Sybase Inc. 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: >- * Feng Wang (Sybase) - Copy from >- * org.eclipse.ui.internal.wizards.datatransfer.DataTransferMessages, >- * which located in org.eclipse.ui.ide plug-in. Remove unused methods. >- *******************************************************************************/ >-package org.eclipse.mtj.ui.internal.wizards.importer.eclipseme; >- >-import org.eclipse.core.runtime.IStatus; >-import org.eclipse.core.runtime.Status; >-import org.eclipse.mtj.ui.IMTJUIConstants; >- >-/** >- * Utility class to create status objects. >- * >- * PRIVATE This class is an internal implementation class and should not be >- * referenced or sub-classed outside of the workbench >- * >- */ >-public class StatusUtil { >- /** >- * This method must not be called outside the workbench. >- * >- * Utility method for creating status. >- * >- * @param severity >- * @param message >- * @param exception >- * @return {@link IStatus} >- */ >- public static IStatus newStatus(int severity, String message, >- Throwable exception) { >- >- String statusMessage = message; >- if (message == null || message.trim().length() == 0) { >- if (exception == null) { >- throw new IllegalArgumentException(); >- } else if (exception.getMessage() == null) { >- statusMessage = exception.toString(); >- } else { >- statusMessage = exception.getMessage(); >- } >- } >- >- return new Status(severity, IMTJUIConstants.PLUGIN_ID, severity, >- statusMessage, exception); >- } >-} >Index: src/org/eclipse/mtj/ui/internal/wizards/importer/eclipseme/ImportEclipseMEProjectWizardPage.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/importer/eclipseme/ImportEclipseMEProjectWizardPage.java (revision 345) >+++ src/org/eclipse/mtj/ui/internal/wizards/importer/eclipseme/ImportEclipseMEProjectWizardPage.java (working copy) >@@ -92,6 +92,7 @@ > import org.eclipse.mtj.core.model.project.MidletSuiteFactory; > import org.eclipse.mtj.core.model.project.impl.MidletSuiteProject; > import org.eclipse.mtj.ui.IMTJUIConstants; >+import org.eclipse.mtj.ui.internal.wizards.StatusUtil; > import org.eclipse.osgi.util.NLS; > import org.eclipse.swt.SWT; > import org.eclipse.swt.events.FocusAdapter; >Index: src/org/eclipse/mtj/ui/internal/wizards/StatusUtil.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/StatusUtil.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/StatusUtil.java (revision 0) >@@ -0,0 +1,169 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2007 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - Copy from >+ * org.eclipse.jdt.internal.ui.dialogs.StatusUtil and modified. >+ *******************************************************************************/ >+package org.eclipse.mtj.ui.internal.wizards; >+ >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.Status; >+ >+import org.eclipse.jface.dialogs.DialogPage; >+import org.eclipse.jface.dialogs.IMessageProvider; >+import org.eclipse.mtj.ui.IMTJUIConstants; >+ >+/** >+ * A utility class to work with IStatus. >+ */ >+public class StatusUtil { >+ /** >+ * This method must not be called outside MTJ. >+ * >+ * Utility method for creating status. >+ * >+ * @param severity >+ * @param message >+ * @param exception >+ * @return {@link IStatus} >+ */ >+ public static IStatus newStatus(int severity, String message, >+ Throwable exception) { >+ >+ String statusMessage = message; >+ if (message == null || message.trim().length() == 0) { >+ if (exception == null) { >+ throw new IllegalArgumentException(); >+ } else if (exception.getMessage() == null) { >+ statusMessage = exception.toString(); >+ } else { >+ statusMessage = exception.getMessage(); >+ } >+ } >+ >+ return new Status(severity, IMTJUIConstants.PLUGIN_ID, severity, >+ statusMessage, exception); >+ } >+ >+ /** >+ * This method must not be called outside MTJ. >+ * >+ * Utility method for creating status. >+ * >+ * @param severity >+ * @param message >+ * @return {@link IStatus} >+ */ >+ public static IStatus newStatus(int severity, String message) { >+ return new Status(severity, IMTJUIConstants.PLUGIN_ID, message); >+ } >+ >+ /** >+ * Compares two instances of <code>IStatus</code>. The more severe is >+ * returned: An error is more severe than a warning, and a warning is more >+ * severe than OK. If the two status have the same severity, the second is >+ * returned. >+ * >+ * @param s1 >+ * first status >+ * @param s2 >+ * second status >+ * @return the more severe status >+ */ >+ public static IStatus getMoreSevere(IStatus s1, IStatus s2) { >+ if (s1.getSeverity() > s2.getSeverity()) { >+ return s1; >+ } else { >+ return s2; >+ } >+ } >+ >+ /** >+ * Finds the most severe status from a array of status. An error is more >+ * severe than a warning, and a warning is more severe than OK. >+ * >+ * @param statuses >+ * an array of status >+ * @return the most severe status >+ */ >+ public static IStatus getMostSevere(IStatus[] statuses) { >+ IStatus max = null; >+ for (int i = 0; i < statuses.length; i++) { >+ IStatus curr = statuses[i]; >+ if (curr.matches(IStatus.ERROR)) { >+ return curr; >+ } >+ if (max == null || curr.getSeverity() > max.getSeverity()) { >+ max = curr; >+ } >+ } >+ return max; >+ } >+ >+ /** >+ * Decide if all the status are acceptable. If one of the status's severity >+ * is greater than IStatus.WARNING, it will return false. >+ * >+ * @param statuses >+ * - A array of status to be decided if all of them are >+ * acceptable. >+ * @return - If all the statuses are acceptable. >+ */ >+ public static boolean allAreAcceptable(IStatus[] statuses) { >+ IStatus mostSevere = getMostSevere(statuses); >+ if (mostSevere.getSeverity() <= IStatus.WARNING) { >+ return true; >+ } else { >+ return false; >+ } >+ } >+ >+ /** >+ * Applies the status to the status line of a dialog page. >+ * >+ * @param page >+ * the dialog page >+ * @param status >+ * the status to apply >+ */ >+ public static void applyToStatusLine(DialogPage page, IStatus status) { >+ String message = status.getMessage(); >+ if (message != null && message.length() == 0) { >+ message = null; >+ } >+ switch (status.getSeverity()) { >+ case IStatus.OK: >+ page.setMessage(message, IMessageProvider.NONE); >+ page.setErrorMessage(null); >+ break; >+ case IStatus.WARNING: >+ page.setMessage(message, IMessageProvider.WARNING); >+ page.setErrorMessage(null); >+ break; >+ case IStatus.INFO: >+ page.setMessage(message, IMessageProvider.INFORMATION); >+ page.setErrorMessage(null); >+ break; >+ default: >+ page.setMessage(null); >+ page.setErrorMessage(message); >+ break; >+ } >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/wizards/template/MidletTemplatesSelectionPage.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/template/MidletTemplatesSelectionPage.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/template/MidletTemplatesSelectionPage.java (revision 0) >@@ -0,0 +1,619 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+package org.eclipse.mtj.ui.internal.wizards.template; >+ >+import java.util.List; >+ >+import org.eclipse.core.resources.IProject; >+import org.eclipse.core.resources.IResource; >+import org.eclipse.core.resources.ResourcesPlugin; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IAdaptable; >+import org.eclipse.core.runtime.IPath; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.Path; >+import org.eclipse.jdt.core.IJavaElement; >+import org.eclipse.jdt.core.IJavaModel; >+import org.eclipse.jdt.core.IJavaProject; >+import org.eclipse.jdt.core.IPackageFragmentRoot; >+import org.eclipse.jdt.core.JavaCore; >+import org.eclipse.jdt.core.JavaModelException; >+import org.eclipse.jdt.ui.JavaElementComparator; >+import org.eclipse.jdt.ui.JavaElementLabelProvider; >+import org.eclipse.jdt.ui.StandardJavaElementContentProvider; >+import org.eclipse.jface.dialogs.Dialog; >+import org.eclipse.jface.resource.ImageDescriptor; >+import org.eclipse.jface.viewers.ILabelProvider; >+import org.eclipse.jface.viewers.ISelectionChangedListener; >+import org.eclipse.jface.viewers.IStructuredContentProvider; >+import org.eclipse.jface.viewers.IStructuredSelection; >+import org.eclipse.jface.viewers.LabelProvider; >+import org.eclipse.jface.viewers.SelectionChangedEvent; >+import org.eclipse.jface.viewers.TableViewer; >+import org.eclipse.jface.viewers.Viewer; >+import org.eclipse.jface.viewers.ViewerFilter; >+import org.eclipse.jface.window.Window; >+import org.eclipse.jface.wizard.IWizardNode; >+import org.eclipse.jface.wizard.Wizard; >+import org.eclipse.jface.wizard.WizardSelectionPage; >+import org.eclipse.mtj.core.IMTJCoreConstants; >+import org.eclipse.mtj.core.internal.MTJCorePlugin; >+import org.eclipse.mtj.ui.internal.template.TemplateWizardProxy; >+import org.eclipse.mtj.ui.internal.template.TemplateWizardRegistry; >+import org.eclipse.mtj.ui.internal.template.WizardNode; >+import org.eclipse.mtj.ui.internal.wizards.StatusUtil; >+import org.eclipse.mtj.ui.template.MidletTemplateWizard; >+import org.eclipse.osgi.util.NLS; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.custom.SashForm; >+import org.eclipse.swt.events.ModifyEvent; >+import org.eclipse.swt.events.ModifyListener; >+import org.eclipse.swt.events.SelectionEvent; >+import org.eclipse.swt.events.SelectionListener; >+import org.eclipse.swt.graphics.Image; >+import org.eclipse.swt.layout.GridData; >+import org.eclipse.swt.layout.GridLayout; >+import org.eclipse.swt.widgets.Button; >+import org.eclipse.swt.widgets.Composite; >+import org.eclipse.swt.widgets.Label; >+import org.eclipse.swt.widgets.Text; >+import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; >+ >+/** >+ * We use this wizard page for the user to select a Midlet template >+ * >+ * @author wangf >+ * >+ */ >+public class MidletTemplatesSelectionPage extends WizardSelectionPage implements >+ ISelectionChangedListener { >+ >+ private TableViewer templateSelectionViewer; >+ private Text description; >+ /** >+ * The Java source folder of the MTJ Midlet Suite project. >+ */ >+ private IPackageFragmentRoot currentJavaRoot; >+ /** >+ * The resource folder of the MTJ Midlet Suite project. >+ */ >+ private IPackageFragmentRoot currentResourceRoot; >+ private Text javaSourceFolderText; >+ private Text resourceFolderText; >+ private IStatus javaSourceFolderStatus = getErrorStatus(""); >+ private IStatus resourceFolderStatus = getErrorStatus(""); >+ >+ public MidletTemplatesSelectionPage(String pageName) { >+ super(pageName); >+ setTitle(TemplateWizardMessages.WizardTemplateSelectionPage_title); >+ setDescription(TemplateWizardMessages.WizardTemplateSelectionPage_desc); >+ } >+ >+ // ----------------------- begin initialize--------------------------- >+ /** >+ * Initialize Java Source Folder and Resource Folder. Since we have no way >+ * to know which source folder is Java Source Folder and which source folder >+ * is Resource Folder, we just take the first source folder as Java Source >+ * folder and take the second folder as the Resource Folder. If there is >+ * only one source folder of the MTJ Midlet Suite project, we set Java >+ * Source Folder and Resource Folder the same one. >+ */ >+ private void initSourceFolders() { >+ IJavaElement initialJavaElement = getInitialJavaElement(); >+ if (initialJavaElement == null) { >+ return; >+ } >+ try { >+ IJavaProject jproject = initialJavaElement.getJavaProject(); >+ if (jproject != null) { >+ if (jproject.exists()) { >+ IPackageFragmentRoot[] sourceRoots = new IPackageFragmentRoot[2]; >+ int sourceRootsAmount = 0; >+ IPackageFragmentRoot[] roots = jproject >+ .getPackageFragmentRoots(); >+ // Try to get two source folders >+ for (int i = 0; i < roots.length; i++) { >+ if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { >+ sourceRoots[sourceRootsAmount] = roots[i]; >+ sourceRootsAmount++; >+ if (sourceRootsAmount == 2) { >+ break; >+ } >+ } >+ } >+ // Initialize Java Source Folder and Resource Folder >+ if (sourceRootsAmount == 2) { >+ // two source folders >+ currentJavaRoot = sourceRoots[0]; >+ currentResourceRoot = sourceRoots[1]; >+ } else if (sourceRootsAmount == 1) { >+ // one source folder >+ currentJavaRoot = sourceRoots[0]; >+ currentResourceRoot = sourceRoots[0]; >+ } else if (sourceRootsAmount == 0) { >+ // project root as source folder >+ sourceRoots[0] = jproject >+ .getPackageFragmentRoot(jproject.getResource()); >+ currentJavaRoot = sourceRoots[0]; >+ currentResourceRoot = sourceRoots[0]; >+ } >+ } >+ >+ } >+ } catch (JavaModelException e) { >+ MTJCorePlugin.log(IStatus.ERROR, e); >+ } >+ // initialize the UI >+ javaSourceFolderText.setText(getSourceFolderName(currentJavaRoot)); >+ resourceFolderText.setText(getSourceFolderName(currentResourceRoot)); >+ } >+ >+ /** >+ * Get the initial Java Element. It is the Java element that user selected >+ * from the Package Explorer or Navigator. >+ * >+ * @return >+ */ >+ private IJavaElement getInitialJavaElement() { >+ IJavaElement javaElement = null; >+ NewMidletFromTemplateWizard wizard = (NewMidletFromTemplateWizard) MidletTemplatesSelectionPage.this >+ .getWizard(); >+ javaElement = getJavaElementFromSelection(wizard.getSelection()); >+ return javaElement; >+ } >+ >+ /** >+ * Get Java element from selection. >+ * >+ * @param selection >+ * @return >+ */ >+ protected IJavaElement getJavaElementFromSelection( >+ IStructuredSelection selection) { >+ IJavaElement jelem = null; >+ if (selection != null && !selection.isEmpty()) { >+ Object selectedElement = selection.getFirstElement(); >+ if (selectedElement instanceof IAdaptable) { >+ IAdaptable adaptable = (IAdaptable) selectedElement; >+ >+ jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class); >+ if (jelem == null) { >+ IResource resource = (IResource) adaptable >+ .getAdapter(IResource.class); >+ if (resource != null >+ && resource.getType() != IResource.ROOT) { >+ while (jelem == null >+ && resource.getType() != IResource.PROJECT) { >+ resource = resource.getParent(); >+ jelem = (IJavaElement) resource >+ .getAdapter(IJavaElement.class); >+ } >+ if (jelem == null) { >+ jelem = JavaCore.create(resource); // java project >+ } >+ } >+ } >+ } >+ } >+ >+ return jelem; >+ } >+ >+ /** >+ * Get the name of source folder. >+ * >+ * @param root - >+ * Java source folder or resource folder of MTJ Midlet Suite >+ * project. >+ * @return >+ */ >+ private String getSourceFolderName(IPackageFragmentRoot root) { >+ // IPackageFragmentRoot root = getSourceFolder(); >+ String folderName = (root == null) ? "" : root.getPath().makeRelative().toString(); //$NON-NLS-1$ >+ return folderName; >+ } >+ >+ // ----------------------- end initialize--------------------------- >+ >+ // ----------------------- begin UI creation--------------------------- >+ public void createControl(Composite parent) { >+ // The container >+ Composite container = new Composite(parent, SWT.NONE); >+ GridLayout layout = new GridLayout(); >+ layout.verticalSpacing = 10; >+ container.setLayout(layout); >+ container.setLayoutData(new GridData(GridData.FILL_BOTH)); >+ // Create the source folder choosing section >+ createSourceFolderSection(container); >+ // The "Available Templates" label >+ Label templatesLabel = new Label(container, SWT.NONE); >+ templatesLabel >+ .setText(TemplateWizardMessages.WizardTemplateSelectionPage_templates); >+ // the SashForm contains Templates TableViewer and Description Text >+ SashForm sashForm = new SashForm(container, SWT.HORIZONTAL); >+ GridData gd = new GridData(GridData.FILL_BOTH); >+ gd.widthHint = 300; >+ sashForm.setLayoutData(gd); >+ // Create template TableViewer >+ createTemplateViewer(sashForm); >+ // Description of the template >+ description = new Text(sashForm, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY >+ | SWT.BORDER); >+ >+ Dialog.applyDialogFont(container); >+ setControl(container); >+ } >+ >+ /** >+ * Create the source folder choosing section >+ * >+ * @param parent >+ */ >+ private void createSourceFolderSection(Composite parent) { >+ Composite container = new Composite(parent, SWT.NONE); >+ GridLayout layout = new GridLayout(3, false); >+ container.setLayout(layout); >+ GridData gd = new GridData(GridData.FILL_HORIZONTAL); >+ container.setLayoutData(gd); >+ >+ Label sourceFolderLabel = new Label(container, SWT.NONE); >+ sourceFolderLabel >+ .setText(TemplateWizardMessages.WizardTemplateSelectionPage_JavaRoot); >+ gd = new GridData(); >+ sourceFolderLabel.setLayoutData(gd); >+ >+ javaSourceFolderText = new Text(container, SWT.SINGLE | SWT.BORDER); >+ gd = new GridData(GridData.FILL_HORIZONTAL); >+ javaSourceFolderText.setLayoutData(gd); >+ javaSourceFolderText.addModifyListener(new ModifyListener() { >+ >+ public void modifyText(ModifyEvent e) { >+ javaSourceFolderStatus = validateProjectNatureAndSourceFolder(javaSourceFolderText >+ .getText()); >+ // validation >+ validateAndDisPlayStatus(); >+ } >+ >+ }); >+ >+ Button javaSrcFolderBrowseButton = new Button(container, SWT.PUSH); >+ javaSrcFolderBrowseButton >+ .setText(TemplateWizardMessages.WizardTemplateSelectionPage_SourceFolderBrowse); >+ javaSrcFolderBrowseButton.addSelectionListener(new SelectionListener() { >+ public void widgetSelected(SelectionEvent e) { >+ chooseJavaSourceFolder(); >+ } >+ >+ public void widgetDefaultSelected(SelectionEvent e) { >+ chooseJavaSourceFolder(); >+ } >+ }); >+ >+ Label resourceFolderLabel = new Label(container, SWT.NONE); >+ resourceFolderLabel >+ .setText(TemplateWizardMessages.WizardTemplateSelectionPage_ResourceRoot); >+ gd = new GridData(); >+ resourceFolderLabel.setLayoutData(gd); >+ >+ resourceFolderText = new Text(container, SWT.SINGLE | SWT.BORDER); >+ gd = new GridData(GridData.FILL_HORIZONTAL); >+ resourceFolderText.setLayoutData(gd); >+ resourceFolderText.addModifyListener(new ModifyListener() { >+ >+ public void modifyText(ModifyEvent e) { >+ resourceFolderStatus = validateProjectNatureAndSourceFolder(resourceFolderText >+ .getText()); >+ // validation >+ validateAndDisPlayStatus(); >+ } >+ >+ }); >+ >+ Button rsourceFolderBrowseButton = new Button(container, SWT.PUSH); >+ rsourceFolderBrowseButton >+ .setText(TemplateWizardMessages.WizardTemplateSelectionPage_SourceFolderBrowse); >+ rsourceFolderBrowseButton.addSelectionListener(new SelectionListener() { >+ public void widgetSelected(SelectionEvent e) { >+ chooseResourceFolder(); >+ } >+ >+ public void widgetDefaultSelected(SelectionEvent e) { >+ chooseResourceFolder(); >+ } >+ }); >+ // Initialize source folders >+ initSourceFolders(); >+ } >+ >+ /** >+ * Create a table viewer to diaplay all the templates. >+ * >+ * @param sashForm >+ */ >+ private void createTemplateViewer(SashForm sashForm) { >+ // Template list >+ templateSelectionViewer = new TableViewer(sashForm, SWT.BORDER); >+ templateSelectionViewer >+ .setContentProvider(new IStructuredContentProvider() { >+ >+ @SuppressWarnings("unchecked") >+ public Object[] getElements(Object inputElement) { >+ List<TemplateWizardProxy> templates = (List<TemplateWizardProxy>) inputElement; >+ return templates >+ .toArray(new TemplateWizardProxy[templates >+ .size()]); >+ } >+ >+ public void dispose() { >+ } >+ >+ public void inputChanged(Viewer viewer, Object oldInput, >+ Object newInput) { >+ } >+ >+ }); >+ templateSelectionViewer.setLabelProvider(new LabelProvider() { >+ @Override >+ public Image getImage(Object element) { >+ TemplateWizardProxy proxy = (TemplateWizardProxy) element; >+ ImageDescriptor descriptor = proxy.getIcon(); >+ return descriptor == null ? null : descriptor.createImage(); >+ } >+ }); >+ // Get all the template wizards >+ List<TemplateWizardProxy> templates = TemplateWizardRegistry >+ .getTemplateWizardProxies(); >+ // Fill template viewer with template wizards >+ templateSelectionViewer.setInput(templates); >+ // Add listener >+ templateSelectionViewer.addSelectionChangedListener(this); >+ } >+ >+ // ----------------------- end UI creation--------------------------- >+ >+ // ----------------------- begin event handling--------------------------- >+ /** >+ * User choose Java Source Folder >+ */ >+ private void chooseJavaSourceFolder() { >+ IPackageFragmentRoot root = chooseContainer(currentJavaRoot); >+ if (root != null) { >+ currentJavaRoot = root; >+ javaSourceFolderText.setText(getSourceFolderName(root)); >+ } >+ } >+ >+ /** >+ * User choose Resource Folder >+ */ >+ private void chooseResourceFolder() { >+ IPackageFragmentRoot root = chooseContainer(currentResourceRoot); >+ if (root != null) { >+ currentResourceRoot = root; >+ resourceFolderText.setText(getSourceFolderName(root)); >+ } >+ } >+ >+ /** >+ * Opens a selection dialog that allows to select a source container. >+ * >+ * @return returns the selected package fragment root or <code>null</code> >+ * if the dialog has been canceled. The caller typically sets the >+ * result to the container input field. >+ */ >+ private IPackageFragmentRoot chooseContainer( >+ IPackageFragmentRoot currentSelection) { >+ Class<?>[] acceptedClasses = new Class[] { IPackageFragmentRoot.class, >+ IJavaProject.class }; >+ TypedElementSelectionValidator validator = new TypedElementSelectionValidator( >+ acceptedClasses, false) { >+ public boolean isSelectedValid(Object element) { >+ try { >+ if (element instanceof IJavaProject) { >+ IJavaProject jproject = (IJavaProject) element; >+ IPath path = jproject.getProject().getFullPath(); >+ return (jproject.findPackageFragmentRoot(path) != null); >+ } else if (element instanceof IPackageFragmentRoot) { >+ return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE); >+ } >+ return true; >+ } catch (JavaModelException e) { >+ MTJCorePlugin.log(IStatus.ERROR, e); >+ } >+ return false; >+ } >+ }; >+ >+ acceptedClasses = new Class[] { IJavaModel.class, >+ IPackageFragmentRoot.class, IJavaProject.class }; >+ ViewerFilter filter = new TypedViewerFilter(acceptedClasses) { >+ public boolean select(Viewer viewer, Object parent, Object element) { >+ if (element instanceof IPackageFragmentRoot) { >+ try { >+ return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE); >+ } catch (JavaModelException e) { >+ MTJCorePlugin.log(IStatus.ERROR, e); >+ return false; >+ } >+ } >+ return super.select(viewer, parent, element); >+ } >+ }; >+ >+ StandardJavaElementContentProvider provider = new StandardJavaElementContentProvider(); >+ ILabelProvider labelProvider = new JavaElementLabelProvider( >+ JavaElementLabelProvider.SHOW_DEFAULT); >+ ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog( >+ getShell(), labelProvider, provider); >+ dialog.setValidator(validator); >+ dialog.setComparator(new JavaElementComparator()); >+ dialog >+ .setTitle(TemplateWizardMessages.MewMidletFromTemplateWizard_ChooseSourceContainerDialog_title); >+ dialog >+ .setMessage(TemplateWizardMessages.MewMidletFromTemplateWizard_ChooseSourceContainerDialog_description); >+ dialog.addFilter(filter); >+ dialog.setInput(JavaCore.create(ResourcesPlugin.getWorkspace() >+ .getRoot())); >+ dialog.setInitialSelection(currentSelection); >+ dialog.setHelpAvailable(false); >+ >+ if (dialog.open() == Window.OK) { >+ Object element = dialog.getFirstResult(); >+ if (element instanceof IJavaProject) { >+ IJavaProject jproject = (IJavaProject) element; >+ return jproject.getPackageFragmentRoot(jproject.getProject()); >+ } else if (element instanceof IPackageFragmentRoot) { >+ return (IPackageFragmentRoot) element; >+ } >+ return null; >+ } >+ return null; >+ } >+ >+ public void selectionChanged(SelectionChangedEvent event) { >+ // create and set WizardNode >+ IStructuredSelection selection = (IStructuredSelection) event >+ .getSelection(); >+ TemplateWizardProxy templateWizardProxy = (TemplateWizardProxy) selection >+ .getFirstElement(); >+ if (templateWizardProxy == null) { >+ setSelectedNode(null); >+ return; >+ } >+ // set wizard node >+ IWizardNode wizardNode = createWizardNode(templateWizardProxy); >+ setSelectedNode(wizardNode); >+ // Set the description of the selected template >+ description.setText(templateWizardProxy.getDescription()); >+ } >+ >+ /** >+ * Create wizard node from TemplateWizardProxy >+ * >+ * @param wizardProxy >+ * @return >+ */ >+ protected IWizardNode createWizardNode(final TemplateWizardProxy wizardProxy) { >+ return new WizardNode() { >+ >+ @Override >+ protected Wizard createWizard() throws CoreException { >+ MidletTemplateWizard midletTemplateWizard = wizardProxy >+ .createWizard(currentJavaRoot, currentResourceRoot); >+ return midletTemplateWizard; >+ } >+ >+ }; >+ } >+ >+ // ----------------------- end event handling--------------------------- >+ >+ // ------begin validation -------- >+ /* >+ * Do validation once the page is visible. >+ */ >+ @Override >+ public void setVisible(boolean visible) { >+ super.setVisible(visible); >+ validateAndDisPlayStatus(); >+ } >+ >+ @Override >+ public boolean canFlipToNextPage() { >+ return super.canFlipToNextPage() && isPageComplete(); >+ } >+ >+ /** >+ * Validate if the Source Folder is valid >+ * >+ * @param sourceFolderPath >+ * @return >+ */ >+ private IStatus validateProjectNatureAndSourceFolder(String sourceFolderPath) { >+ IPath path = new Path(sourceFolderPath); >+ IResource resource = ResourcesPlugin.getWorkspace().getRoot() >+ .findMember(path); >+ >+ if (resource == null) { >+ return getErrorStatus(sourceFolderPath); >+ } >+ >+ IProject project = resource.getProject(); >+ IJavaElement javaElement = JavaCore.create(resource); >+ >+ try { >+ // validate project nature >+ if (!project.hasNature(IMTJCoreConstants.J2ME_NATURE_ID)) { >+ return StatusUtil.newStatus(IStatus.ERROR, NLS.bind( >+ TemplateWizardMessages.Validation_NotMTJProject, >+ project.getName())); >+ } >+ // validate source folder >+ if (javaElement instanceof IPackageFragmentRoot) { >+ IPackageFragmentRoot root = (IPackageFragmentRoot) javaElement; >+ if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { >+ return getOKStatus(); >+ } else { >+ return getErrorStatus(sourceFolderPath); >+ } >+ } else if (javaElement instanceof IJavaProject) { >+ IJavaProject jproject = (IJavaProject) javaElement; >+ IPath projPath = jproject.getProject().getFullPath(); >+ if (jproject.findPackageFragmentRoot(projPath) != null) { >+ return getOKStatus(); >+ } else { >+ return getErrorStatus(sourceFolderPath); >+ } >+ } else { >+ return getErrorStatus(sourceFolderPath); >+ } >+ } catch (Exception e) { >+ MTJCorePlugin.log(IStatus.WARNING, e); >+ return getOKStatus(); >+ } >+ } >+ >+ /** >+ * Return a error status. >+ * >+ * @param sourceFolderPath >+ * @return >+ */ >+ private IStatus getErrorStatus(String sourceFolderPath) { >+ return StatusUtil.newStatus(IStatus.ERROR, NLS.bind( >+ TemplateWizardMessages.Validation_NotSourceFolder, >+ sourceFolderPath)); >+ } >+ >+ /** >+ * Return a OK status. >+ * >+ * @return >+ */ >+ private IStatus getOKStatus() { >+ return StatusUtil.newStatus(IStatus.OK, >+ TemplateWizardMessages.WizardTemplateSelectionPage_desc); >+ } >+ >+ /** >+ * Decide if the page is complete and display the validation result message. >+ */ >+ private void validateAndDisPlayStatus() { >+ IStatus[] statuses = { javaSourceFolderStatus, resourceFolderStatus }; >+ IStatus currentStatus = StatusUtil.getMostSevere(statuses); >+ // Set if the page is complete >+ setPageComplete(StatusUtil.allAreAcceptable(statuses)); >+ // display status on page >+ StatusUtil.applyToStatusLine(this, currentStatus); >+ } >+ // ------end validation -------- >+} >Index: src/org/eclipse/mtj/ui/internal/wizards/template/NewMidletFromTemplateWizard.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/template/NewMidletFromTemplateWizard.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/template/NewMidletFromTemplateWizard.java (revision 0) >@@ -0,0 +1,67 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.wizards.template; >+ >+import org.eclipse.jface.viewers.IStructuredSelection; >+import org.eclipse.jface.wizard.Wizard; >+import org.eclipse.ui.INewWizard; >+import org.eclipse.ui.IWorkbench; >+ >+/** >+ * Wizard for creating Midlet form template >+ * >+ * @author wangf >+ * >+ */ >+public class NewMidletFromTemplateWizard extends Wizard implements INewWizard { >+ /** >+ * We will use the selection to retrieve source folders. >+ */ >+ private IStructuredSelection selection; >+ private MidletTemplatesSelectionPage templateListSelectionPage; >+ >+ public NewMidletFromTemplateWizard() { >+ setWindowTitle(TemplateWizardMessages.MewMidletFromTemplateWizard_windowTitle); >+ setNeedsProgressMonitor(true); >+ setForcePreviousAndNextButtons(true); >+ } >+ >+ public void addPages() { >+ templateListSelectionPage = new MidletTemplatesSelectionPage( >+ "templateListSelectionPage"); >+ addPage(templateListSelectionPage); >+ >+ } >+ >+ /** >+ * This wizard is a wizards selection wizard, so it should always return >+ * false. >+ */ >+ @Override >+ public boolean canFinish() { >+ return false; >+ } >+ >+ @Override >+ public boolean performFinish() { >+ return false; >+ } >+ >+ public void init(IWorkbench workbench, IStructuredSelection selection) { >+ this.selection = selection; >+ } >+ >+ public IStructuredSelection getSelection() { >+ return selection; >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/wizards/template/TemplateWizardMessages.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/template/TemplateWizardMessages.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/template/TemplateWizardMessages.java (revision 0) >@@ -0,0 +1,50 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.wizards.template; >+ >+import org.eclipse.osgi.util.NLS; >+ >+public class TemplateWizardMessages extends NLS { >+ private static final String BUNDLE_NAME = "org.eclipse.mtj.ui.internal.wizards.template.messages";//$NON-NLS-N$ >+ >+ public static String WizardTemplateSelectionPage_title; >+ public static String WizardTemplateSelectionPage_desc; >+ >+ public static String WizardTemplateSelectionPage_templates; >+ >+ public static String MewMidletFromTemplateWizard_windowTitle; >+ >+ public static String MewMidletFromTemplateWizard_error_title; >+ >+ public static String MewMidletFromTemplateWizard_error_nothingSelected; >+ >+ public static String MewMidletFromTemplateWizard_ChooseSourceContainerDialog_title; >+ >+ public static String MewMidletFromTemplateWizard_ChooseSourceContainerDialog_description; >+ >+ public static String Validation_NotSourceFolder; >+ >+ public static String ErrorDialogTitle_duringCopy; >+ >+ public static String WizardTemplateSelectionPage_JavaRoot; >+ >+ public static String WizardTemplateSelectionPage_SourceFolderBrowse; >+ >+ public static String WizardTemplateSelectionPage_ResourceRoot; >+ >+ public static String Validation_NotMTJProject; >+ >+ static { >+ NLS.initializeMessages(BUNDLE_NAME, TemplateWizardMessages.class); >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/wizards/template/TypedElementSelectionValidator.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/template/TypedElementSelectionValidator.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/template/TypedElementSelectionValidator.java (revision 0) >@@ -0,0 +1,128 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2008 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - Copy from {@link org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator} >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.wizards.template; >+ >+import java.util.Collection; >+ >+import org.eclipse.core.runtime.Assert; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.core.runtime.Status; >+import org.eclipse.mtj.ui.IMTJUIConstants; >+import org.eclipse.ui.dialogs.ISelectionStatusValidator; >+ >+/** >+ * Implementation of a <code>ISelectionValidator</code> to validate the type of >+ * an element. Empty selections are not accepted. >+ */ >+public class TypedElementSelectionValidator implements >+ ISelectionStatusValidator { >+ >+ private IStatus fgErrorStatus = new Status(IStatus.ERROR, >+ IMTJUIConstants.PLUGIN_ID, ""); //$NON-NLS-1$ >+ private IStatus fgOKStatus = new Status(IStatus.OK, >+ IMTJUIConstants.PLUGIN_ID, "");//$NON-NLS-1$ >+ >+ private Class<?>[] fAcceptedTypes; >+ private boolean fAllowMultipleSelection; >+ private Collection<?> fRejectedElements; >+ >+ /** >+ * @param acceptedTypes >+ * The types accepted by the validator >+ * @param allowMultipleSelection >+ * If set to <code>true</code>, the validator allows multiple >+ * selection. >+ */ >+ public TypedElementSelectionValidator(Class<?>[] acceptedTypes, >+ boolean allowMultipleSelection) { >+ this(acceptedTypes, allowMultipleSelection, null); >+ } >+ >+ /** >+ * @param acceptedTypes >+ * The types accepted by the validator >+ * @param allowMultipleSelection >+ * If set to <code>true</code>, the validator allows multiple >+ * selection. >+ * @param rejectedElements >+ * A list of elements that are not accepted >+ */ >+ public TypedElementSelectionValidator(Class<?>[] acceptedTypes, >+ boolean allowMultipleSelection, Collection<?> rejectedElements) { >+ Assert.isNotNull(acceptedTypes); >+ fAcceptedTypes = acceptedTypes; >+ fAllowMultipleSelection = allowMultipleSelection; >+ fRejectedElements = rejectedElements; >+ } >+ >+ /* >+ * @see org.eclipse.ui.dialogs.ISelectionValidator#isValid(java.lang.Object) >+ */ >+ public IStatus validate(Object[] elements) { >+ if (isValid(elements)) { >+ return fgOKStatus; >+ } >+ return fgErrorStatus; >+ } >+ >+ private boolean isOfAcceptedType(Object o) { >+ for (int i = 0; i < fAcceptedTypes.length; i++) { >+ if (fAcceptedTypes[i].isInstance(o)) { >+ return true; >+ } >+ } >+ return false; >+ } >+ >+ private boolean isRejectedElement(Object elem) { >+ return (fRejectedElements != null) && fRejectedElements.contains(elem); >+ } >+ >+ /** >+ * @param elem >+ * the element to test >+ * @return returns if the selected element is valid >+ */ >+ protected boolean isSelectedValid(Object elem) { >+ return true; >+ } >+ >+ private boolean isValid(Object[] selection) { >+ if (selection.length == 0) { >+ return false; >+ } >+ >+ if (!fAllowMultipleSelection && selection.length != 1) { >+ return false; >+ } >+ >+ for (int i = 0; i < selection.length; i++) { >+ Object o = selection[i]; >+ if (!isOfAcceptedType(o) || isRejectedElement(o) >+ || !isSelectedValid(o)) { >+ return false; >+ } >+ } >+ return true; >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/wizards/template/TypedViewerFilter.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/template/TypedViewerFilter.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/template/TypedViewerFilter.java (revision 0) >@@ -0,0 +1,83 @@ >+/******************************************************************************* >+ * Copyright (c) 2000, 2006 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - Copy from {@link org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter} >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.wizards.template; >+ >+import org.eclipse.core.runtime.Assert; >+ >+import org.eclipse.jface.viewers.Viewer; >+import org.eclipse.jface.viewers.ViewerFilter; >+ >+/** >+ * Viewer filter used in selection dialogs. >+ */ >+public class TypedViewerFilter extends ViewerFilter { >+ >+ private Class<?>[] fAcceptedTypes; >+ private Object[] fRejectedElements; >+ >+ /** >+ * Creates a filter that only allows elements of gives types. >+ * >+ * @param acceptedTypes >+ * The types of accepted elements >+ */ >+ public TypedViewerFilter(Class<?>[] acceptedTypes) { >+ this(acceptedTypes, null); >+ } >+ >+ /** >+ * Creates a filter that only allows elements of gives types, but not from a >+ * list of rejected elements. >+ * >+ * @param acceptedTypes >+ * Accepted elements must be of this types >+ * @param rejectedElements >+ * Element equals to the rejected elements are filtered out >+ */ >+ public TypedViewerFilter(Class<?>[] acceptedTypes, Object[] rejectedElements) { >+ Assert.isNotNull(acceptedTypes); >+ fAcceptedTypes = acceptedTypes; >+ fRejectedElements = rejectedElements; >+ } >+ >+ /** >+ * @see ViewerFilter#select(org.eclipse.jface.viewers.Viewer, >+ * java.lang.Object, java.lang.Object) >+ */ >+ public boolean select(Viewer viewer, Object parentElement, Object element) { >+ if (fRejectedElements != null) { >+ for (int i = 0; i < fRejectedElements.length; i++) { >+ if (element.equals(fRejectedElements[i])) { >+ return false; >+ } >+ } >+ } >+ for (int i = 0; i < fAcceptedTypes.length; i++) { >+ if (fAcceptedTypes[i].isInstance(element)) { >+ return true; >+ } >+ } >+ return false; >+ } >+ >+} >Index: src/org/eclipse/mtj/ui/internal/wizards/template/messages.properties >=================================================================== >--- src/org/eclipse/mtj/ui/internal/wizards/template/messages.properties (revision 0) >+++ src/org/eclipse/mtj/ui/internal/wizards/template/messages.properties (revision 0) >@@ -0,0 +1,29 @@ >+############################################################################### >+# Copyright (c) 2008 Sybase Inc. 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: >+# Feng Wang (Sybase) - initial implementation >+############################################################################### >+WizardTemplateSelectionPage_title = Templates >+WizardTemplateSelectionPage_desc = Select one of the available templates to generate a Midlet. >+WizardTemplateSelectionPage_templates = &Available Templates: >+WizardTemplateSelectionPage_JavaRoot = Java Source Folder: >+WizardTemplateSelectionPage_ResourceRoot = Resource Folder: >+WizardTemplateSelectionPage_SourceFolderBrowse = Browse... >+ >+MewMidletFromTemplateWizard_windowTitle = New Midlet with Templates >+MewMidletFromTemplateWizard_error_title = Error >+MewMidletFromTemplateWizard_error_nothingSelected = You should select a project >+ >+MewMidletFromTemplateWizard_ChooseSourceContainerDialog_title = Source Folder Selection >+MewMidletFromTemplateWizard_ChooseSourceContainerDialog_description = &Choose a source folder: >+ >+Validation_NotSourceFolder=Folder ''{0}'' is not a source folder >+Validation_NotMTJProject=Project ''{0}'' is not a MTJ Midlet Suite project >+ >+ErrorDialogTitle_duringCopy="copy files"\ No newline at end of file >Index: src/org/eclipse/mtj/ui/internal/template/MidletTemplateWizardPage.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/template/MidletTemplateWizardPage.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/template/MidletTemplateWizardPage.java (revision 0) >@@ -0,0 +1,49 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.template; >+ >+import org.eclipse.jface.dialogs.Dialog; >+import org.eclipse.jface.wizard.WizardPage; >+import org.eclipse.mtj.ui.template.MidletTemplate; >+import org.eclipse.mtj.ui.template.TemplateOption; >+import org.eclipse.swt.SWT; >+import org.eclipse.swt.layout.GridLayout; >+import org.eclipse.swt.widgets.Composite; >+ >+public class MidletTemplateWizardPage extends WizardPage { >+ >+ private MidletTemplate template; >+ >+ public MidletTemplateWizardPage(MidletTemplate template) { >+ super(""); >+ this.template = template; >+ setTitle(template.getName()); >+ setDescription(TemplateMessages.MidletTemplateWizardPage_desc); >+ } >+ >+ public void createControl(Composite parent) { >+ Composite container = new Composite(parent, SWT.NULL); >+ GridLayout layout = new GridLayout(); >+ layout.numColumns = 2; >+ layout.verticalSpacing = 9; >+ container.setLayout(layout); >+ // Create control for each template option >+ for (TemplateOption option : template.getOptions()) { >+ option.createControl(container, 2); >+ } >+ >+ setControl(container); >+ Dialog.applyDialogFont(container); >+ } >+ >+} >Index: src/org/eclipse/mtj/ui/internal/template/TemplateMessages.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/template/TemplateMessages.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/template/TemplateMessages.java (revision 0) >@@ -0,0 +1,33 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.template; >+ >+import org.eclipse.osgi.util.NLS; >+ >+public class TemplateMessages extends NLS { >+ private static final String BUNDLE_NAME = "org.eclipse.mtj.ui.internal.template.messages";//$NON-NLS-N$ >+ >+ public static String MidletTemplateWizard_title; >+ >+ public static String MidletTemplateWizardPage_desc; >+ >+ public static String Option_Package_label; >+ >+ public static String Option_Package_defaultValue; >+ >+ public static String Template_descriptionNotAvailable; >+ >+ static { >+ NLS.initializeMessages(BUNDLE_NAME, TemplateMessages.class); >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/template/TemplateWizardProxy.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/template/TemplateWizardProxy.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/template/TemplateWizardProxy.java (revision 0) >@@ -0,0 +1,170 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.template; >+ >+import java.util.MissingResourceException; >+import java.util.ResourceBundle; >+ >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IConfigurationElement; >+import org.eclipse.core.runtime.Platform; >+import org.eclipse.jdt.core.IPackageFragmentRoot; >+import org.eclipse.jface.resource.ImageDescriptor; >+import org.eclipse.mtj.ui.template.MidletTemplateWizard; >+import org.eclipse.ui.plugin.AbstractUIPlugin; >+import org.osgi.framework.Bundle; >+ >+/** >+ * This class is a proxy of the real template wizard. >+ * >+ * @author wangf >+ * >+ */ >+public class TemplateWizardProxy { >+ >+ public static final String ATT_NAME = "name"; //$NON-NLS-1$ >+ public static final String TAG_DESCRIPTION = "description"; //$NON-NLS-1$ >+ public static final String ATT_ID = "id"; //$NON-NLS-1$ >+ public static final String ATT_CLASS = "class"; //$NON-NLS-1$ >+ public static final String ATT_ICON = "icon"; >+ >+ private IConfigurationElement wizardElement; >+ >+ private String description; >+ >+ private ImageDescriptor imageDescriptor; >+ >+ public TemplateWizardProxy(IConfigurationElement e) { >+ this.wizardElement = e; >+ } >+ >+ /** >+ * This method is used by UI to display the Midlet template name. >+ */ >+ @Override >+ public String toString() { >+ return wizardElement.getAttribute(ATT_NAME); >+ } >+ >+ /** >+ * Create MidletTemplateWizard from the IConfigurationElement >+ * >+ * @param javaRoot - >+ * the Java source folder of the MTJ Midlet suite project. User >+ * can choose this folder on the wizard page. >+ * @param resourceRoot - >+ * the resource folder of the MTJ Midlet suite project. User can >+ * choose this folder on the wizard page. >+ * @return >+ * @throws CoreException >+ */ >+ public MidletTemplateWizard createWizard(IPackageFragmentRoot javaRoot, >+ IPackageFragmentRoot resourceRoot) throws CoreException { >+ // Create the wizard >+ MidletTemplateWizard midletTemplateWizard = (MidletTemplateWizard) wizardElement >+ .createExecutableExtension(ATT_CLASS); >+ // Set the Java source folder and resource folder >+ midletTemplateWizard.setJavaRoot(javaRoot); >+ midletTemplateWizard.setResourceRoot(resourceRoot); >+ >+ return midletTemplateWizard; >+ } >+ >+ /** >+ * Get the ImageDescriptor of the wizard. >+ * >+ * @return >+ */ >+ public ImageDescriptor getIcon() { >+ if (imageDescriptor == null) { >+ String iconName = wizardElement.getAttribute(ATT_ICON); >+ if (iconName == null) { >+ return null; >+ } >+ imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin( >+ wizardElement.getNamespaceIdentifier(), iconName); >+ } >+ return imageDescriptor; >+ } >+ >+ /** >+ * Get the description of the wizard. >+ * >+ * @return >+ */ >+ public String getDescription() { >+ if (description == null) { >+ IConfigurationElement[] children = wizardElement >+ .getChildren(TAG_DESCRIPTION); >+ if (children.length > 0) { >+ description = expandDescription(children[0].getValue()); >+ } else { >+ description = TemplateMessages.Template_descriptionNotAvailable; >+ } >+ } >+ return description; >+ } >+ >+ /** >+ * We allow replacement variables in description values as well. This is to >+ * allow extension template description reuse in project template wizards. >+ * Tokens in form '%token%' will be evaluated against the contributing >+ * plug-in's resource bundle. As before, to have '%' in the description, one >+ * need to add '%%'. >+ */ >+ private String expandDescription(String source) { >+ if (source == null || source.length() == 0) >+ return source; >+ if (source.indexOf('%') == -1) >+ return source; >+ >+ Bundle bundle = Platform.getBundle(wizardElement >+ .getNamespaceIdentifier()); >+ if (bundle == null) >+ return source; >+ >+ ResourceBundle resourceBundle = Platform.getResourceBundle(bundle); >+ if (resourceBundle == null) >+ return source; >+ StringBuffer buf = new StringBuffer(); >+ boolean keyMode = false; >+ int keyStartIndex = -1; >+ for (int i = 0; i < source.length(); i++) { >+ char c = source.charAt(i); >+ if (c == '%') { >+ char c2 = source.charAt(i + 1); >+ if (c2 == '%') { >+ i++; >+ buf.append('%'); >+ continue; >+ } >+ if (keyMode) { >+ keyMode = false; >+ String key = source.substring(keyStartIndex, i); >+ String value = key; >+ try { >+ value = resourceBundle.getString(key); >+ } catch (MissingResourceException e) { >+ } >+ buf.append(value); >+ } else { >+ keyStartIndex = i + 1; >+ keyMode = true; >+ } >+ } else if (!keyMode) { >+ buf.append(c); >+ } >+ } >+ return buf.toString(); >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/template/TemplateWizardRegistry.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/template/TemplateWizardRegistry.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/template/TemplateWizardRegistry.java (revision 0) >@@ -0,0 +1,55 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.template; >+ >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.core.runtime.IConfigurationElement; >+import org.eclipse.core.runtime.IExtensionRegistry; >+import org.eclipse.core.runtime.Platform; >+import org.eclipse.mtj.ui.internal.MTJUIPlugin; >+ >+/** >+ * This class is used to retrieve all of the "midletTemplateWizard" extensions. >+ * >+ * @author wangf >+ * >+ */ >+public class TemplateWizardRegistry { >+ public static final String MIDLET_TEMPLATE_WIZARD = "midletTemplateWizard"; >+ >+ private TemplateWizardRegistry() { >+ } >+ >+ /** >+ * Get all of the extensions of org.eclipse.mtj.ui.midletTemplateWizard >+ * extension point. >+ * >+ * @return - a list of TemplateWizardProxy >+ */ >+ public static List<TemplateWizardProxy> getTemplateWizardProxies() { >+ List<TemplateWizardProxy> proxies = new ArrayList<TemplateWizardProxy>(); >+ >+ IExtensionRegistry registry = Platform.getExtensionRegistry(); >+ String pluginId = MTJUIPlugin.getDefault().getBundle() >+ .getSymbolicName(); >+ IConfigurationElement[] elements = registry >+ .getConfigurationElementsFor(pluginId, MIDLET_TEMPLATE_WIZARD); >+ for (IConfigurationElement e : elements) { >+ TemplateWizardProxy proxy = new TemplateWizardProxy(e); >+ proxies.add(proxy); >+ } >+ return proxies; >+ } >+} >Index: src/org/eclipse/mtj/ui/internal/template/WizardNode.java >=================================================================== >--- src/org/eclipse/mtj/ui/internal/template/WizardNode.java (revision 0) >+++ src/org/eclipse/mtj/ui/internal/template/WizardNode.java (revision 0) >@@ -0,0 +1,58 @@ >+/******************************************************************************* >+ * Copyright (c) 2008 Sybase Inc. 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: >+ * Feng Wang (Sybase) - initial implementation >+ *******************************************************************************/ >+ >+package org.eclipse.mtj.ui.internal.template; >+ >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IStatus; >+import org.eclipse.jface.wizard.IWizard; >+import org.eclipse.jface.wizard.IWizardNode; >+import org.eclipse.jface.wizard.Wizard; >+import org.eclipse.mtj.core.internal.MTJCorePlugin; >+import org.eclipse.swt.graphics.Point; >+ >+public abstract class WizardNode implements IWizardNode { >+ >+ private IWizard wizard; >+ >+ public void dispose() { >+ if (wizard != null) { >+ wizard.dispose(); >+ wizard = null; >+ } >+ } >+ >+ public Point getExtent() { >+ return new Point(-1, -1); >+ } >+ >+ public IWizard getWizard() { >+ if (wizard != null) { >+ return wizard; // we've already created it >+ } >+ try { >+ wizard = createWizard(); >+ } catch (CoreException e) { >+ MTJCorePlugin.log(IStatus.ERROR, e); >+ // may be convenient for template wizard extension developer. >+ e.printStackTrace(); >+ } // create instance of target wizard >+ return wizard; >+ } >+ >+ protected abstract Wizard createWizard() throws CoreException; >+ >+ public boolean isContentCreated() { >+ return wizard != null; >+ } >+ >+} >Index: src/org/eclipse/mtj/ui/internal/template/messages.properties >=================================================================== >--- src/org/eclipse/mtj/ui/internal/template/messages.properties (revision 0) >+++ src/org/eclipse/mtj/ui/internal/template/messages.properties (revision 0) >@@ -0,0 +1,17 @@ >+############################################################################### >+# Copyright (c) 2008 Sybase Inc. 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: >+# Feng Wang (Sybase) - initial implementation >+############################################################################### >+MidletTemplateWizard_title = New Midlet with Templates >+MidletTemplateWizardPage_desc=You may customize the following fields >+Option_Package_label=Package name: >+Option_Package_defaultValue=mtj.template >+ >+Template_descriptionNotAvailable=Description of the template is not available.\ No newline at end of file
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 239979
:
109745
|
109746
|
109967