InternationalInputApp.java

Go to the documentation of this file.
00001 // (C) 2003 by Dominique Unruh. GPL 00002 00003 import java.applet.*; 00004 import javax.swing.*; 00005 import javax.swing.event.*; 00006 import java.net.*; 00007 import java.util.*; 00008 import java.io.*; 00009 import java.awt.*; 00010 import java.awt.event.*; 00011 import javax.jnlp.*; 00012 00013 public class InternationalInputApp 00014 extends JApplet { 00015 public static final int MAX_LOG_SIZE = 10000; 00016 public static final int LOG_CUT_SIZE = 9000; 00017 public static final String FRAME_TITLE = "International Input"; 00018 00019 InternationalInput internationalInput; 00020 StringBuffer log = new StringBuffer(); 00021 00022 public static class AppletInternationalInputContext 00023 extends NullInternationalInputContext { 00024 00025 InternationalInputApp applet = null; 00026 00028 public AppletInternationalInputContext(InternationalInputApp _applet) { 00029 applet = _applet; } 00034 public void showInfo(String info, boolean log) { 00035 if (log) { 00036 applet.log.append(info); 00037 applet.log.append("\n"); 00038 if (applet.log.length()>MAX_LOG_SIZE) 00039 applet.log.delete(0,applet.log.length()-LOG_CUT_SIZE); 00040 } 00041 applet.showStatus(info); } 00043 public URL getDocumentBase() { return applet.getDocumentBase(); }; 00048 public void showDocument(URL url, boolean top) { 00049 applet.getAppletContext().showDocument 00050 (url, top?"intinp-top":"copy"); } 00054 public void addMenuBar(JMenuBar menuBar) { 00055 applet.setJMenuBar(menuBar); } 00063 public Properties getProperties(String names[]) { 00064 Properties props = new Properties(); 00065 for (int i=0; i<names.length; i++) { 00066 String val = applet.getParameter(names[i]); 00067 if (val!=null) props.setProperty(names[i],val); 00068 } 00069 return props; 00070 } 00071 00072 public boolean isExitAllowed() { return false; } 00073 }; 00074 00075 public static class StandAloneInternationalInputContext 00076 extends NullInternationalInputContext { 00077 JFrame frame = null; 00078 JLabel statusBar = null; 00079 JFrame browserFrame = null; 00080 JEditorPane browser = null; 00081 00082 public StandAloneInternationalInputContext(JFrame _frame, JLabel _sb) { 00083 frame = _frame; statusBar = _sb; } 00084 00089 public Properties getProperties(String names[]) { 00090 return System.getProperties(); 00091 } 00092 00094 public URL getDocumentBase() { 00095 try { 00096 String cwd = new File("").toURL().toString(); 00097 if (!cwd.endsWith("/")) cwd += "/"; 00098 return new URL(cwd); 00099 } catch (MalformedURLException e) { 00100 throw new UnsupportedOperationException 00101 ("Could not convert current dir to URL: "+e.getMessage()); 00102 } 00103 } 00107 public void addMenuBar(JMenuBar menuBar) { 00108 frame.setJMenuBar(menuBar); 00109 } 00114 public void showDocument(URL url, boolean top) { 00115 try { 00116 createBrowser().setPage(url); 00117 } catch (IOException e) { 00118 e.printStackTrace(); 00119 throw new RuntimeException(e.toString()); 00120 } 00121 } 00122 00126 public void showDocumentFromString(String source) { 00127 JEditorPane browser = createBrowser(); 00128 browser.setContentType("text/html"); 00129 browser.setText(source); 00130 } 00131 00132 JEditorPane createBrowser() { 00133 if (browserFrame!=null) { 00134 browserFrame.setVisible(true); 00135 browserFrame.setState(Frame.NORMAL); 00136 return browser; 00137 } 00138 00139 browserFrame = new JFrame("International Input - Browser"); 00140 browserFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 00141 browser = new JEditorPane(); 00142 Font oldFont = browser.getFont(); 00143 String tryFonts[] = { "Arial Unicode MS", "Code2000", 00144 "Bitstream Cyberbit" }; 00145 for (int i=0; i<tryFonts.length; i++) { 00146 try { 00147 Font newFont = new Font(tryFonts[i], 00148 Font.PLAIN,oldFont.getSize()); 00149 if (newFont.getFamily().equals(tryFonts[i])) { 00150 browser.setFont(newFont); 00151 break; 00152 } 00153 } catch (Throwable e) { e.printStackTrace(); }; 00154 } 00155 00156 JScrollPane scroll = new JScrollPane(); 00157 scroll.setViewportView(browser); 00158 browser.setEditable(false); 00159 browser.addHyperlinkListener(new HyperlinkListener() { 00160 public void hyperlinkUpdate(HyperlinkEvent e) { 00161 JEditorPane ed = (JEditorPane)e.getSource(); 00162 try { 00163 if (e.getEventType()==HyperlinkEvent.EventType.ACTIVATED) 00164 ed.setPage(e.getURL()); 00165 } catch (IOException ex) { 00166 ed.setContentType("text/plain"); 00167 ed.setText(ex.toString()); 00168 ex.printStackTrace(); 00169 } 00170 } 00171 }); 00172 browserFrame.getContentPane().add(scroll); 00173 browserFrame.setSize(800,600); 00174 browserFrame.setVisible(true); 00175 return browser; 00176 } 00181 public void showInfo(String info, boolean log) { 00182 if (log) System.out.println(info); 00183 if (info.length()==0) info = " "; 00184 statusBar.setText(info); 00185 } 00186 00187 public void setTitleInfo(String info) { 00188 if (info==null) 00189 frame.setTitle(FRAME_TITLE); 00190 else 00191 frame.setTitle(FRAME_TITLE+" - "+info); 00192 } 00193 00194 public boolean isExitAllowed() { return true; } 00195 } 00196 00197 public static class WebStartInternationalInputContext 00198 extends StandAloneInternationalInputContext { 00199 BasicService basicService; 00200 public WebStartInternationalInputContext(JFrame _frame, JLabel _sb) { 00201 super(_frame,_sb); 00202 try { 00203 basicService = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService"); 00204 } catch (UnavailableServiceException ex) {}; 00205 } 00206 public Properties getProperties(String names[]) { 00207 Properties prop = System.getProperties(); 00208 String config = prop.getProperty("config","config.inp.gz"); 00209 if (config.indexOf(":") == -1) 00210 config = getClass().getClassLoader().getResource(config).toString(); 00211 prop.setProperty("config",config); 00212 return prop; 00213 } 00214 public URL getDocumentBase() 00215 throws UnsupportedOperationException { 00216 if (basicService==null) 00217 throw new UnsupportedOperationException 00218 ("Can't determine code base: No BasicService"); 00219 return basicService.getCodeBase(); 00220 } 00221 public void showDocument(URL url, boolean top) { 00222 if (basicService==null) { super.showDocument(url,top); return; }; 00223 if (!basicService.isWebBrowserSupported()) { 00224 super.showDocument(url,top); return; }; 00225 basicService.showDocument(url); 00226 } 00227 } 00228 00229 public static String getenv(String name) { 00230 if (!System.getProperty("file.separator").equals("/")) return null; 00231 try { 00232 Process pr = Runtime.getRuntime().exec(new String[] { "sh", "-c", "echo \"$"+name+"\"" }); 00233 00234 BufferedReader r = new BufferedReader 00235 (new InputStreamReader 00236 (pr.getInputStream())); 00237 String val = r.readLine(); 00238 pr.destroy(); 00239 return val; 00240 } catch (IOException e) { 00241 e.printStackTrace(); 00242 return null; 00243 } 00244 } 00245 00246 public static void initHttpClient() { 00247 String proxy = System.getProperty("http.proxyHost"); 00248 if (proxy==null) 00249 proxy = System.getProperty("http.proxyHost"); 00250 while (proxy==null) { 00251 String proxyEnv = getenv("http_proxy"); 00252 String errmsg = "Environment variable http_proxy has unidentifiable value "+proxyEnv+":"; 00253 if (proxyEnv==null) break; 00254 if (!proxyEnv.startsWith("http://")) { 00255 System.err.println(errmsg); 00256 System.err.println("Does not begin with http://"); 00257 break; 00258 } 00259 proxyEnv = proxyEnv.substring(7); 00260 int idx = proxyEnv.indexOf(':'); 00261 if (idx == -1 || idx==proxyEnv.length()) { 00262 System.err.println(errmsg); 00263 System.err.println("No : found after host"); 00264 break; 00265 } 00266 proxy = proxyEnv.substring(0,idx); 00267 int idx2 = proxyEnv.indexOf('/'); 00268 if (idx2==-1) idx2 = proxyEnv.length(); 00269 int proxyPort; 00270 try { 00271 proxyPort = Integer.parseInt(proxyEnv.substring(idx+1,idx2)); 00272 } catch (NumberFormatException e) { 00273 System.err.println(errmsg); 00274 System.err.println("No integer after : (found "+proxyEnv.substring(idx,idx2)+" there)"); 00275 break; 00276 } 00277 System.setProperty("http.proxyHost",proxy); 00278 System.setProperty("http.proxyPort",Integer.toString(proxyPort)); 00279 break; 00280 } 00281 String agent = System.getProperty("http.agent"); 00282 if (agent==null) 00283 System.setProperty("http.agent","International Input"); 00284 } 00285 00286 public static void main(String args[]) { 00287 boolean webStart = false; 00288 try { 00289 Class.forName("javax.jnlp.ServiceManager"); 00290 System.out.println("Using Java Web-Start"); 00291 webStart = true; 00292 } catch (Throwable th) {}; 00293 try { 00294 if (!webStart) initHttpClient(); 00295 } catch (SecurityException ex) { 00296 ex.printStackTrace(); 00297 } 00298 JFrame frame = new JFrame(FRAME_TITLE); 00299 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 00300 JPanel statusPane = new JPanel(new GridLayout(1,1)); 00301 JLabel statusBar = new JLabel("International Input loaded."); 00302 statusPane.add(statusBar); 00303 InternationalInputContext context; 00304 if (!webStart) 00305 context = new StandAloneInternationalInputContext(frame,statusBar); 00306 else 00307 context = new WebStartInternationalInputContext(frame,statusBar); 00308 InternationalInput intinp = new InternationalInput(context); 00309 frame.getContentPane().add("Center",intinp); 00310 frame.getContentPane().add("South",statusBar); 00311 00312 frame.pack(); 00313 frame.setSize(400,400); 00314 if (webStart || System.getProperty("nogui")==null) 00315 frame.setVisible(true); 00316 else 00317 System.out.println("No GUI"); 00318 for (int i=0; i<args.length; i++) { 00319 intinp.loadConfig(args[i]); } 00320 intinp.ready(); 00321 } 00322 00323 public void init() { 00324 internationalInput = new InternationalInput 00325 (new AppletInternationalInputContext(this)); 00326 getContentPane().add(internationalInput); 00327 validate(); 00328 internationalInput.ready(); 00329 } 00330 00331 public void stop() { 00332 Todo.reset(); 00333 } 00334 00340 public String copyText() { 00341 return internationalInput.copyText(true); 00342 } 00347 public void setClipboardContent(String data) { 00348 internationalInput.setClipboardContent(data); 00349 } 00354 /* public boolean hasChangedSinceMirror() { 00355 return internationalInput.hasChangedSinceMirror(); 00356 }*/ 00361 /*public String mirror() { 00362 return internationalInput.mirror(); 00363 }*/ 00367 public void noButtons() { 00368 internationalInput.noButtons(); 00369 } 00376 public void clearText() { 00377 new Todo(internationalInput) { 00378 public void action() { 00379 ((InternationalInput)arg1).clearText(); }}; 00380 } 00385 public String getDocumentation(String group) { 00386 return internationalInput.getDocumentation(group); 00387 } 00388 00389 /* 00393 /*public String getScreenName(String group) { 00394 return internationalInput.getScreenName(group); 00395 }*/ 00396 /* 00399 /* public String lastActivatedGroup() { 00400 return internationalInput.lastActivatedGroup(); 00401 }*/ 00402 00409 public void loadConfig(String config) { 00410 new Todo(internationalInput,config) { 00411 public void action() throws IOException { 00412 ((InternationalInput)arg1). 00413 loadConfig((String)arg2); 00414 }}; 00415 } 00422 public void loadConfigString(String config) throws IOException { 00423 new Todo(internationalInput,config) { 00424 public void action() throws IOException { 00425 ((InternationalInput)arg1). 00426 loadConfigString((String)arg2); 00427 }}; 00428 } 00429 00430 public String getLog() { 00431 String context = new String(log); 00432 log.setLength(0); 00433 return context; 00434 } 00435 00436 public void pasteText(boolean temporary) { 00437 new Todo(internationalInput,new Boolean(temporary)) { 00438 public void action() throws IOException { 00439 ((InternationalInput)arg1).pasteText 00440 (((Boolean)arg2).booleanValue()); 00441 }}; 00442 } 00443 public String getCurrentDocumentation() { 00444 return internationalInput.getCurrentDocumentation(); } 00445 }

Generated on Sun Aug 15 11:56:53 2004 for International Input by doxygen 1.3.7