📄 consoleapplet.java
字号:
package net.sf.yacas;/*Documentation for the applet, starting it here, but has to move to main docs:just random thoughts for now.1) typing 'restart' on the command line restarts the system2) You can perform initialization calls by adding parameters "initN" in the html code, where N is a number from 1 upwards, which have to be in consecutive order.3) add to the history with "historyN" parameters to the applet<B>Note:</B> to allow supporting copy-pasting from and to this applet, you need to enableaccess to the clip board on your computer. On Unix-style computers this can be done by appending the following lines to the file ~/.java.policy (create the file if it doesnot exist yet):<p><TT>grant codeBase "http://www.xs4all.nl/~apinkus/*" {permission java.awt.AWTPermission "accessClipboard";};</TT><p>You can then copy with CTRL-c and paste with CTRL-v.*/import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.ClipboardOwner;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.Toolkit;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.io.*;import java.net.*;public class ConsoleApplet extends Applet implements KeyListener, FocusListener, ClipboardOwner{ AppletOutput out; /// Applet initialization public void init() { setBackground(Color.white); setLayout (null); addKeyListener(this); addFocusListener(this);// addMouseListener(this); out = new AppletOutput(this); ResetInput(); String hintsfilename = getDocumentBase().toString() + ".hints"; LoadHints(hintsfilename); } boolean focusGained = false; public void focusGained(FocusEvent evt) { focusGained = true; inputDirty = true; outputDirty = true; repaint(); } public void focusLost(FocusEvent evt) { } public void lostOwnership(Clipboard clipboard, Transferable contents) { } LispOutput stdoutput = null; CYacas yacas = null; StringBuffer outp = new StringBuffer(); public void start() { if (yacasLogo == null) { try { String fname = getDocumentBase().toString(); int ind = fname.lastIndexOf("/"); if (ind >0) { fname = fname.substring(0,ind+1)+"yacas.gif"; yacasLogo = getImage(new URL(fname)); } } catch (Exception e) { } } stdoutput = new StringOutput(outp); yacas = new CYacas(stdoutput); yacas.env.iCurrentInput = new CachedStdFileInput(yacas.env.iInputStatus); if (yacasLogo != null) { lines[currentLine] = new ImageLine(yacasLogo,this); currentLine = (currentLine+1)%nrLines; } { Font font = new Font("helvetica", Font.PLAIN, 12); Color c = new Color(96,96,96); AddLineStatic(100, "","", font, c); AddLineStatic(100, "","", font, c); AddLineStatic(100, "","This is Yacas version '" + CVersion.VERSION + "'.", font, c); AddLineStatic(100, "","Running from location '" + getDocumentBase() + "'.", font, c); AddLineStatic(100, "","Yacas is Free Software--Free as in Freedom--so you can redistribute Yacas or", font, c); AddLineStatic(100, "","modify it under certain conditions. Yacas comes with ABSOLUTELY NO WARRANTY.", font, c); AddLineStatic(100, "","See the GNU General Public License (GPL) for the full conditions.", font, c);//TODO fixme AddLineStatic(100, "","Type ?license or ?licence to see the GPL; type ?warranty for warranty info.", font, c); AddLineStatic(100, "","See http://yacas.sf.net for more information and documentation on Yacas.", font, c); AddLineStatic(100, "","Numeric mode: \""+BigNumber.NumericLibraryName()+"\"\n", font, c);//TODO fixme AddLineStatic(100, "","To exit Yacas, enter Exit(); or quit or Ctrl-c.\n", font, c); AddLineStatic(100, "","", font, c); AddLineStatic(100, "","Type '?', '??' or 'help' for help, or type '?function' for help on a function.\n", font, c); AddLineStatic(100, "","Type 'restart' to restart Yacas, or 'cls' to clear screen.\n", font, c); AddLineStatic(100, "","To see example commands, keep typing 'Example();'\n", font, c); } { String docbase = getDocumentBase().toString(); if (docbase.substring(0,4).equals("file")) { int pos = docbase.lastIndexOf("/"); String zipFileName = docbase.substring(0,pos+1)+"scripts.zip"; try { // java.util.zip.ZipFile z = new java.util.zip.ZipFile(new File(new java.net.URI("file:/Users/ayalpinkus/projects/JavaYacas/tempscripts.zip"))); java.util.zip.ZipFile z = new java.util.zip.ZipFile(new File(new java.net.URI(zipFileName))); LispStandard.zipFile = z;// out.println("Succeeded in finding "+zipFileName); } catch(Exception e) { out.println("Failed to find scripts.zip"); out.println(""+zipFileName+" : \n"); out.println(e.toString()); // return; } } if (docbase.startsWith("http")) { //jar:http://www.xs4all.nl/~apinkus/scripts.zip!/ int pos = docbase.lastIndexOf("/"); String scriptBase = "jar:"+ docbase.substring(0,pos+1)+"scripts.zip!/";// AddLineStatic(100, ""," '" + scriptBase + "'.", font, Color.red); yacas.Evaluate("DefaultDirectory(\""+scriptBase+"\");"); } } try {// PerformRequest("Connected: version of engine is ","Atom(Version())"); out.println(""); } catch (Exception e) { out.println(e); } int i; i=1; while (true) { String argn = "init"+i; String s = getParameter(argn); if (s == null) break; s = unescape(s); yacas.Evaluate(s); i++; } i=1; while (true) { String argn = "history"+i; String s = getParameter(argn); if (s == null) break; s = unescape(s); AppendHistoryLine(s);//TODO remove history[currentHistoryLine] = s;//TODO remove currentHistoryLine++; i++; } ResetInput(); } public void stop() { } public void AppendHistoryLine(String line) { //TODO optimize! We need to wrap around the history buffer, this is inefficient. if (currentHistoryLine == nrHistoryLines) { int i; for (i=0;i<currentHistoryLine-1;i++) { history[i] = history[i+1]; } currentHistoryLine--; } history[currentHistoryLine] = line; currentHistoryLine++; } private String unescape(String s) { StringBuffer buf = new StringBuffer(); int i,nr=s.length(); for(i=0;i<nr;i++) { if (s.charAt(i) == '\'' && s.charAt(i+1) == '\'') { buf.append('\"'); i++; } else { buf.append(s.charAt(i)); } } return buf.toString(); } public void ResetInput() { if (inputLine.length()>0) if (inputLine.charAt(inputLine.length()-1) != '\\') gatheredMultiLine = ""; inputLine = ""; cursorPos = 0; historyBrowse = currentHistoryLine; inputDirty = true; } /// Applet destruction public void destroy() { } public void keyPressed(KeyEvent e) { processKeyEvent(e); } public void keyTyped(KeyEvent e) {// processKeyEvent(e); } public void keyReleased(KeyEvent e) {// processKeyEvent(e); } public void setClipboardContents( String aString ) { StringSelection stringSelection = new StringSelection( aString ); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents( stringSelection, this ); } public String getClipboardContents() { String result = ""; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //odd: the Object param of getContents is not currently used Transferable contents = clipboard.getContents(null); boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor); if ( hasTransferableText ) { try { result = (String)contents.getTransferData(DataFlavor.stringFlavor); } catch (java.awt.datatransfer.UnsupportedFlavorException ex) { //highly unlikely since we are using a standard DataFlavor System.out.println(ex); } catch (IOException ex) { System.out.println(ex); } } return result; } protected void processKeyEvent(KeyEvent e) { inputDirty = true; if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) { if (KeyEvent.KEY_PRESSED != e.getID()) return; if (e.getKeyCode() == (int)'C') { //out.println("Copy"); setClipboardContents( gatheredMultiLine+inputLine ); } else if (e.getKeyCode() == (int)'V') { try { String toInsert = getClipboardContents(); if (toInsert != null) { int cr = toInsert.indexOf('\n'); while (cr >= 0) { inputLine = inputLine+toInsert.substring(0,cr); toInsert = toInsert.substring(cr+1,toInsert.length()); cr = toInsert.indexOf('\n');//System.out.println(""); AppendHistoryLine(inputLine);//TODO remove history[currentHistoryLine] = inputLine;//TODO remove currentHistoryLine++; AddLinesStatic(48,inputPrompt,inputLine); if (inputLine.charAt(inputLine.length()-1) == '\\') gatheredMultiLine = gatheredMultiLine + inputLine.substring(0,inputLine.length()-1); else PerformRequest("Out> ",gatheredMultiLine+inputLine); ResetInput(); } inputLine = inputLine+toInsert; RefreshHintWindow(); repaint(); return; } // out.println("Paste");// out.println(toInsert); } catch (Exception ex) { } } else { return; } } if (KeyEvent.KEY_PRESSED == e.getID()) { if (e.VK_SHIFT == e.getKeyCode()) {return;} if (e.VK_CONTROL == e.getKeyCode()) {return;} if (e.VK_ALT == e.getKeyCode()) {return;} else if (e.VK_HOME == e.getKeyCode()) { cursorPos = 0; }/*Does not seem to work? else if (e.VK_COPY == e.getKeyCode()) { System.out.println("COPY"); } else if (e.VK_PASTE == e.getKeyCode()) { System.out.println("PASTE"); }*/ else if (e.VK_END == e.getKeyCode()) { cursorPos = inputLine.length(); } else if (e.VK_LEFT == e.getKeyCode()) { if (cursorPos>0) { cursorPos--; RefreshHintWindow(); repaint(); return; } } else if (e.VK_BACK_SPACE == e.getKeyCode()) { if (cursorPos>0) { cursorPos--; inputLine = new StringBuffer(inputLine).delete(cursorPos,cursorPos+1).toString(); RefreshHintWindow(); repaint(); return; } } else if (e.VK_ESCAPE == e.getKeyCode()) { if (hintWindow != null) { hintWindow = null; } else { ResetInput(); } repaint(); return; } else if (e.VK_UP == e.getKeyCode()) { { String prefix = inputLine.substring(0,cursorPos); int i = historyBrowse - 1; while (i > 0) { if (history[i].startsWith(prefix)) break; i--; } if (i >= 0 && i != historyBrowse && history[i].startsWith(prefix)) { historyBrowse = i; inputLine = history[historyBrowse]; } } } else if (e.VK_DOWN == e.getKeyCode()) { { String prefix = inputLine.substring(0,cursorPos); int i = historyBrowse + 1; while (i < currentHistoryLine) { if (history[i].startsWith(prefix)) break; i++; } if (i < currentHistoryLine && history[i].startsWith(prefix)) { historyBrowse = i; inputLine = history[historyBrowse]; } else { int pos = cursorPos; ResetInput(); inputLine = prefix; cursorPos = pos; } } } else if (e.VK_RIGHT == e.getKeyCode()) { if (cursorPos<inputLine.length()) { cursorPos++; RefreshHintWindow(); repaint(); return; } } else if (e.VK_ENTER == e.getKeyCode()) { AppendHistoryLine(inputLine);//TODO remove history[currentHistoryLine] = inputLine;//TODO remove currentHistoryLine++; AddLinesStatic(48,inputPrompt,inputLine); if (inputLine.charAt(inputLine.length()-1) == '\\') gatheredMultiLine = gatheredMultiLine + inputLine.substring(0,inputLine.length()-1); else PerformRequest("Out> ",gatheredMultiLine+inputLine); ResetInput(); RefreshHintWindow(); repaint(0); } else { int c = (int)e.getKeyChar(); if (c>=32 && c < 128) { inputLine = new StringBuffer(inputLine).insert(cursorPos,e.getKeyChar()).toString(); cursorPos++; RefreshHintWindow(); } } inputDirty=true; repaint();//0,getHeight()-2*fontHeight,getWidth(),2*fontHeight); } } void PerformRequest(String outputPrompt,String inputLine) { boolean succeed = false; if (inputLine.equals("restart")) { stop(); int i; for (i=0;i<nrLines;i++) lines[i] = null; outputDirty = true; start(); return; } else if (inputLine.equals("cls")) { int i; for (i=0;i<nrLines;i++) lines[i] = null; outputDirty = true; succeed = true; } else if (inputLine.equals("help") || inputLine.equals("?") || inputLine.equals("??")) { try { getAppletContext().showDocument( new URL("http://yacas.sourceforge.net/manindex.html"),"help"); succeed = true; } catch (Exception e) { } } else if (inputLine.charAt(0) == '?') { try { getAppletContext().showDocument( new URL("http://yacas.sourceforge.net/ref.html#"+inputLine.substring(1,inputLine.length())),"help"); succeed = true; } catch (Exception e) { } } else { outp.delete(0,outp.length()); String response = yacas.Evaluate(inputLine); if (outp.length() > 0) { AddLinesStatic(48,"",outp.toString()); } if (yacas.iError != null) { AddLinesStatic(48,"Error> ",yacas.iError); } AddLinesStatic(48, outputPrompt,response); succeed = true; } { if (!succeed) { out.println("Request failed");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -