Source Code Cross Referenced for BrowserControl.java in  » Test-Coverage » salome-tmf » salomeTMF_plug » helpgui » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Test Coverage » salome tmf » salomeTMF_plug.helpgui.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Based on Steven Spencer's Java Tip in JavaWorld:
003:         * http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
004:         */
005:
006:        package salomeTMF_plug.helpgui.util;
007:
008:        import java.io.IOException;
009:        import java.net.URL;
010:
011:        /**
012:         * A simple, static class to display an URL in the system browser.
013:         * 
014:         * Under Windows, this will bring up the default browser, usually either
015:         * Netscape or Microsoft IE. The default browser is determined by the OS. This
016:         * has been tested under: Windows 95/98/NT/2000.
017:         * 
018:         * Under MacOS, this will bring up the default browser. The default browser is
019:         * determined by the OS. This has been tested under: n/a
020:         * 
021:         * In other cases (and under Unix), the system browser is hard-coded to be
022:         * 'netscape'. Netscape must be in your PATH for this to work. This has been
023:         * tested with the following platforms: AIX, HP-UX and Solaris.
024:         * 
025:         * Examples: * BrowserControl.displayURL("http://www.javaworld.com")
026:         * 
027:         * BrowserControl.displayURL("file://c:\\docs\\index.html")
028:         * 
029:         * BrowserContorl.displayURL("file:///user/joe/index.html");
030:         * 
031:         * Note - you must include the url type -- either "http://" or "file://".
032:         */
033:        public class BrowserControl {
034:
035:            /**
036:             * Display an URL in the system browser. If you want to display a file, you
037:             * must include the absolute path name.
038:             * 
039:             * @param url
040:             *            the document's url (the url must start with either "http://"
041:             *            or "file://").
042:             */
043:            public static boolean displayURL(String url) {
044:
045:                // Opening a browser, even when running sandbox-restricted
046:                // in JavaWebStart.
047:                System.out.println("Try to open url : " + url);
048:                try {
049:                    Class serManClass = Class
050:                            .forName("javax.jnlp.ServiceManager");
051:                    Class basSerClass = Class
052:                            .forName("javax.jnlp.BasicService");
053:                    Class[] stringParam = { String.class };
054:                    Class[] urlParam = { URL.class };
055:
056:                    Object basicService = serManClass.getMethod("lookup",
057:                            stringParam).invoke(serManClass,
058:                            new Object[] { "javax.jnlp.BasicService" });
059:                    basSerClass.getMethod("showDocument", urlParam).invoke(
060:                            basicService, new Object[] { new URL(url) });
061:
062:                    return true;
063:                } catch (Exception e) {
064:                    // Not running in JavaWebStart or service is not supported.
065:                    // We continue with the methods below ...
066:                }
067:
068:                //String[] cmd = null;
069:
070:                switch (getPlatform()) {
071:                case (WIN_ID):
072:                    return runCmdLine(replaceToken(WIN_CMDLINE, URLTOKEN, url));
073:                case (MAC_ID):
074:                    return runCmdLine(replaceToken(MAC_CMDLINE, URLTOKEN, url));
075:                default:
076:                    for (int i = 0; i < OTHER_CMDLINES.length; i++) {
077:                        if (runCmdLine(replaceToken(OTHER_CMDLINES[i],
078:                                URLTOKEN, url), replaceToken(
079:                                OTHER_FALLBACKS[i], URLTOKEN, url)))
080:                            return true;
081:                    }
082:                }
083:
084:                return false;
085:            }
086:
087:            /**
088:             * Try to determine whether this application is running under Windows or
089:             * some other platform by examing the "os.name" property.
090:             * 
091:             * @return the ID of the platform
092:             */
093:            private static int getPlatform() {
094:                String os = System.getProperty("os.name");
095:                if (os != null && os.startsWith(WIN_PREFIX))
096:                    return WIN_ID;
097:                if (os != null && os.startsWith(MAC_PREFIX))
098:                    return MAC_ID;
099:                return OTHER_ID;
100:            }
101:
102:            /*private static String connectStringArray(String[] a) {
103:            	if (a == null)
104:            		return null;
105:
106:            	String s = "";
107:            	for (int i = 0; i < a.length; i++) {
108:            		if (i > 0)
109:            			s += " ";
110:            		s += a[i];
111:            	}
112:
113:            	return s;
114:            }*/
115:
116:            private static String[] replaceToken(String[] target, String token,
117:                    String replacement) {
118:                if (null == target)
119:                    return null;
120:                String[] result = new String[target.length];
121:
122:                for (int i = 0; i < target.length; i++)
123:                    result[i] = target[i].replaceAll(token, replacement);
124:
125:                return result;
126:            }
127:
128:            private static boolean runCmdLine(String[] cmdLine) {
129:                return runCmdLine(cmdLine, null);
130:            }
131:
132:            private static boolean runCmdLine(String[] cmdLine,
133:                    String[] fallBackCmdLine) {
134:                try {
135:
136:                    /*
137:                     * System.out.println( "Trying to invoke browser, cmd='" +
138:                     * connectStringArray(cmdLine) + "' ... ");
139:                     */
140:                    Process p = Runtime.getRuntime().exec(cmdLine);
141:
142:                    if (null != fallBackCmdLine) {
143:                        // wait for exit code -- if it's 0, command worked,
144:                        // otherwise we need to start fallBackCmdLine.
145:                        int exitCode = p.waitFor();
146:                        if (exitCode != 0) {
147:                            /*
148:                             * System.out.println(exitCode); System.out.println();
149:                             */
150:
151:                            /*
152:                             * System.out.println( "Trying to invoke browser, cmd='" +
153:                             * connectStringArray(fallBackCmdLine) + "' ...");
154:                             */
155:                            Runtime.getRuntime().exec(fallBackCmdLine);
156:
157:                        }
158:                    }
159:
160:                    System.out.println();
161:                    return true;
162:
163:                } catch (InterruptedException e) {
164:                    System.out.println("Caught: " + e);
165:                } catch (IOException e) {
166:                    System.out.println("Caught: " + e);
167:                }
168:
169:                return false;
170:            }
171:
172:            // This token is a placeholder for the actual URL
173:            private static final String URLTOKEN = "%URLTOKEN%";
174:
175:            // Used to identify the windows platform.
176:            private static final int WIN_ID = 1;
177:
178:            // Used to discover the windows platform.
179:            private static final String WIN_PREFIX = "Windows";
180:
181:            // The default system browser under windows.
182:            // Once upon a time:
183:            //   for 'Windows 9' and 'Windows M': start
184:            //   for 'Windows': cmd /c start
185:            private static final String[] WIN_CMDLINE = { "rundll32",
186:                    "url.dll,FileProtocolHandler", URLTOKEN };
187:
188:            // Used to identify the mac platform.
189:            private static final int MAC_ID = 2;
190:
191:            // Used to discover the mac platform.
192:            private static final String MAC_PREFIX = "Mac";
193:
194:            // The default system browser under mac.
195:            private static final String[] MAC_CMDLINE = { "open", URLTOKEN };
196:
197:            // Used to identify the mac platform.
198:            private static final int OTHER_ID = -1;
199:
200:            private static final String[][] OTHER_CMDLINES = {
201:
202:            // The first guess for a browser under other systems (and unix):
203:                    // Remote controlling firefox
204:                    // (http://www.mozilla.org/unix/remote.html)
205:                    { "firefox", "-remote",
206:                            "openURL(" + URLTOKEN + ",new-window)" },
207:
208:                    //	Remote controlling mozilla
209:                    // (http://www.mozilla.org/unix/remote.html)
210:                    { "mozilla", "-remote",
211:                            "openURL(" + URLTOKEN + ",new-window)" },
212:                    // The second guess for a browser under other systems (and unix):
213:                    // The RedHat skript htmlview
214:                    { "htmlview", URLTOKEN },
215:
216:                    // The third guess for a browser under other systems (and unix):
217:                    // Remote controlling netscape
218:                    // (http://wp.netscape.com/newsref/std/x-remote.html)
219:                    { "netscape", "-remote", "openURL(" + URLTOKEN + ")" }
220:
221:            };
222:
223:            private static final String[][] OTHER_FALLBACKS = {
224:
225:            // Fallback for remote controlling mozilla:
226:                    //Starting up a new mozilla
227:                    { "firefox", URLTOKEN },
228:
229:                    // Starting up a new mozilla
230:                    { "mozilla", URLTOKEN },
231:
232:                    // No fallback for htmlview
233:                    null,
234:
235:                    // Fallback for remote controlling netscape:
236:                    // Starting up a new netscape
237:                    { "netscape", URLTOKEN }
238:
239:            };
240:
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.