KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > datatransfer > AppletUtil


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

12
13 package org.eclipse.ant.internal.ui.datatransfer;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.debug.core.ILaunch;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
24 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
25
26 /**
27  * Helper class to access private method
28  * <code>org.eclipse.jdt.internal.launching.JavaAppletLaunchConfigurationDelegate}.buildHTMLFile()</code>.
29  *
30  * <p>
31  * Source was copied and slighlty modified.
32  */

33 public class AppletUtil
34 {
35     private AppletUtil() {}
36     
37     // create instance to access helper method
38
private static AbstractJavaLaunchConfigurationDelegate fgDelegate = new AbstractJavaLaunchConfigurationDelegate()
39     {
40         public void launch(ILaunchConfiguration configuration, String JavaDoc mode,
41                 ILaunch launch, IProgressMonitor monitor) throws CoreException
42         {
43         }
44     };
45
46     /**
47      * Using the specified launch configuration, build an HTML file that
48      * specifies the applet to launch.
49      *
50      * @return the created HTML file
51      */

52     public static String JavaDoc buildHTMLFile(ILaunchConfiguration configuration) throws CoreException
53     {
54         String JavaDoc name = getMainTypeName(configuration);
55         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
56         b.append("<!--" + BuildFileCreator.WARNING + " -->" + ExportUtil.NEWLINE); //$NON-NLS-1$ //$NON-NLS-2$
57
b.append("<html>" + ExportUtil.NEWLINE); //$NON-NLS-1$
58
b.append(" <body>" + ExportUtil.NEWLINE); //$NON-NLS-1$
59
b.append(" <applet code="); //$NON-NLS-1$
60
b.append(getQuotedString(name + ".class")); //$NON-NLS-1$
61
String JavaDoc appletName = configuration.getAttribute(
62                 IJavaLaunchConfigurationConstants.ATTR_APPLET_NAME, ""); //$NON-NLS-1$
63
if (appletName.length() != 0)
64         {
65             b.append(" name=\"" + appletName + "\""); //$NON-NLS-1$ //$NON-NLS-2$
66
}
67         b.append(" width=\""); //$NON-NLS-1$
68
b.append(Integer.toString(configuration.getAttribute(
69                 IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, 200)));
70         b.append("\" height=\""); //$NON-NLS-1$
71
b.append(Integer.toString(configuration.getAttribute(
72                 IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, 200)));
73         b.append("\">" + ExportUtil.NEWLINE); //$NON-NLS-1$
74
Map JavaDoc parameters = configuration.getAttribute(
75                 IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS,
76                 new HashMap JavaDoc());
77         if (parameters.size() != 0)
78         {
79             Iterator JavaDoc iterator = parameters.entrySet().iterator();
80             while (iterator.hasNext())
81             {
82                 Map.Entry JavaDoc next = (Map.Entry JavaDoc) iterator.next();
83                 b.append(" <param name="); //$NON-NLS-1$
84
b.append(getQuotedString((String JavaDoc) next.getKey()));
85                 b.append(" value="); //$NON-NLS-1$
86
b.append(getQuotedString((String JavaDoc) next.getValue()));
87                 b.append(">" + ExportUtil.NEWLINE); //$NON-NLS-1$
88
}
89         }
90         b.append(" </applet>" + ExportUtil.NEWLINE); //$NON-NLS-1$
91
b.append(" </body>" + ExportUtil.NEWLINE); //$NON-NLS-1$
92
b.append("</html>" + ExportUtil.NEWLINE); //$NON-NLS-1$
93
return b.toString();
94     }
95
96     private static String JavaDoc getQuotedString(String JavaDoc string)
97     {
98         if (string.indexOf('"') == -1)
99         {
100             return '"' + string + '"';
101         }
102         return '\'' + string + '\'';
103     }
104
105     /**
106      * Returns the main type name specified by the given launch configuration,
107      * or <code>null</code> if none.
108      *
109      * @param configuration
110      * launch configuration
111      * @return the main type name specified by the given launch configuration,
112      * or <code>null</code> if none
113      * @exception CoreException
114      * if unable to retrieve the attribute
115      */

116     public static String JavaDoc getMainTypeName(ILaunchConfiguration configuration)
117             throws CoreException
118     {
119         return fgDelegate.getMainTypeName(configuration);
120     }
121 }
Popular Tags