📄 helpfunctions.java
字号:
/*
* MC2 -- j2me spreadsheet
*
* Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.wapindustrial.calc;
import java.util.*;import java.io.*;//#ifndef NOGUIimport javax.microedition.lcdui.*;//#endifpublic class HelpFunctions extends FunctionModule//#ifndef NOGUIimplements CommandListener//#endif{ public static final HelpFunctions HELPFUNCTIONS = new HelpFunctions(); /** Creates a new instance of HelpFunction */ public HelpFunctions() { } static final byte[] _namesArray = { '\14','b','r','o','w','s','e','-','n','a','m','e','s', '\20','f','i','l','e','-','s','e','c','t','i','o','n','l','o','a','d', '\20','f','i','l','e','-','s','e','c','t','i','o','n','s','h','o','w' }; public static final int NAME_BROWSE_NAMES = 1208-1208; public static final int NAME_FILE_SECTIONLOAD = 1221-1208; public static final int NAME_FILE_SECTIONSHOW = 1238-1208; public static final int INDEX_BROWSE_NAMES = 0; public static final int INDEX_FILE_SECTIONLOAD = 1; public static final int INDEX_FILE_SECTIONSHOW = 2; public void initializeNames() { namesArray = _namesArray; namesArrayCount = namesArray.length; table = new ModuleName[] { new ModuleName(NAME_BROWSE_NAMES), new ModuleName(NAME_FILE_SECTIONLOAD), new ModuleName(NAME_FILE_SECTIONSHOW) }; namesCount = table.length; }//#ifndef NOGUI public LispObject evaluate(ModuleName modulename, FunctorList sexp) throws EvaluateException { int size = sexp.listSize(); int optype = modulename.offset; LispObject rez = FormulaError.ERROR; switch( optype ) { case NAME_BROWSE_NAMES: { /* (browse-names type:sheet start:short len:short) -> '(name1:string name2:string ...) */ // type: 0x01-FunctionObjects, 0x02-UserFunction, 0x04-others int type = ((ShortAtom)sexp.evaluateArg1()).value; int start = ((ShortAtom)sexp.evaluateArg2()).value; int len = ((ShortAtom)sexp.evaluateArg3()).value; int nn = 0; // FIXME browse all modules int tablesize = namesCount; Vector vv = new Vector( len ); for( int i=0; i<tablesize; i++ ) { NameObjectBase nm = table[ i ]; int nmtype = 0; if( nm.getMappedObject() instanceof UserFunction ) nmtype = 0x02; else if( nm.getMappedObject() instanceof FunctionObject ) nmtype = 0x01; else nmtype = 0x04; if( (nmtype&type) != 0 ) { // within filter criteria if( nn < start ) { nn++; continue; } if( nn >= start+len ) { break; } vv.addElement( nm ); nn++; } } tablesize = vv.size(); LispObject ql[] = new LispObject[tablesize]; for( int i=0; i<tablesize; i++ ) ql[i] = new StringAtom( ((NameObjectBase)vv.elementAt( i )).getName() ); return new QuotedList( ql ); } case NAME_FILE_SECTIONLOAD: { /* (file-sectionload file:string section:string [encoding:string]) -> sectionbody:string */ String file = sexp.getString(1); String section = sexp.getString(2); String encoding = null; if(size==4) encoding = sexp.getString(3); try { String ss = loadSection( file, section, encoding ); if( ss == null ) ss = "Section " + section + "not found"; return new StringAtom( ss ); } catch( IOException ee ) { throw new EvaluateException( ee.getMessage(), sexp ); } } case NAME_FILE_SECTIONSHOW: { /* (file-sectionshow file:string section:string ) -> nil */ String file = sexp.getString(1); String section = sexp.getString(2); encoding = null; if(size==4) encoding = sexp.getString(3); nhist = 0; setForm(file,section); } } return NIL; } private static final Command cmdOK = new Command( "Done", Command.OK, 1 ); private static final Command cmdBack = new Command( "Back", Command.BACK, 2 ); // private static final Command cmdCancel = new Command( "Cancel", Command.CANCEL, 3 ); private static String curfile; private static Section cursection = null; private static final Section history[] = new Section[20]; private static int nhist = 0; private void setForm( String file, String section ) { curfile = file; Form form = new Form( section ); try { cursection = getSection( file, section ); String body = "section not found"; if( cursection != null ) { body = loadSection( file, section, encoding ); if( cursection.links != null ) for( int i=0; i<cursection.links.size(); i++ ) { Command cmd = new Command( (String)cursection.links.elementAt(i), Command.SCREEN, 3+i ); form.addCommand( cmd ); } } form.append( body ); form.addCommand( cmdOK ); form.addCommand( cmdBack );// form.addCommand( cmdCancel ); form.setCommandListener( this ); LispTask.LispMachine.popMessage = form; CanvasHandler1.setDisplay(form); } catch( IOException ee ) { LispObject.debug( "Exception in setForm()" ); LispObject.debug( ee.getMessage() ); CanvasHandler1.setCanvas(); } } String encoding; public void commandAction( Command cmd, Displayable dd ) { if( cmd == cmdOK ) { nhist = 0; CanvasHandler1.setCanvas(); return; } if( cmd == cmdBack ) { if( nhist == 0 ) CanvasHandler1.setCanvas(); else { Section sect = history[--nhist]; setForm( curfile, sect.name ); } return; } history[nhist++] = cursection; int nlink = cmd.getPriority()-3; setForm( curfile, (String)cursection.links.elementAt(nlink) ); } // one level undo static int bufcc, offset, bufundo; static InputStream inputstream; static final int readc() { offset++; if( bufundo != -1 ) { bufcc = bufundo; bufundo = -1; return bufcc; } try { bufcc = inputstream.read(); } catch( IOException ee ) { return -1; } return bufcc; } static final void undo() { bufundo = bufcc; offset--; } // FIXME support for UTF-16 (currently only byte section names aer supported) static final synchronized Hashtable loadIndexes( InputStream is ) throws IOException { int bb; byte secname[] = new byte[40]; Hashtable indx = new Hashtable( 10 ); offset = 0; // file offset bufundo = -1; inputstream = is; while( (bb=readc()) != -1 ) { // load section if( bb != '[' ) throw new IOException( "wrong help file format" ); int nn = 0; while( (bb=readc()) != ']' ) { secname[nn++] = (byte)bb; } String ss = new String( secname, 0, nn ); if( nn == 0 ) throw new IOException( "wrong help file format" ); int startsection = offset; byte linkbyte[] = new byte[40]; Vector links = null; int ab1 = readc(); int ab2 = readc(); while( !(ab1 == '\n' && ab2 == '[') ) { if( ab1 == -1 || ab2 == -1 ) { // EOF indx.put( ss, new Section( ss, startsection, offset, links ) ); return indx; } if( ab1 == '\n' && ab2 == '@' ) { int linknn = 0; while( (ab2=readc()) != '\n' ) { linkbyte[linknn++] = (byte)ab2; } if( links == null ) links = new Vector( 1 ); links.addElement( new String( linkbyte, 0, linknn ).trim() ); } ab1 = ab2; ab2 = readc(); } undo(); // to '[' indx.put( ss, new Section( ss, startsection, offset, links ) ); } return indx; } static final Hashtable indextables = new Hashtable( 1 ); // of hashtable of indexes(int) // one help file is supposed static final Section getSection( String file, String section ) throws IOException { Hashtable indx = (Hashtable) indextables.get( file ); if( indx == null ) { InputStream is = HELPFUNCTIONS.getClass().getResourceAsStream( file ); if( is == null ) { is.close(); throw new IOException( "Help file doesn't exist" ); } indx = loadIndexes( is ); is.close(); indextables.put( file, indx ); // reopen } return (Section) indx.get( section ); } /** * Loads section from file using given encoding * @param file * @param section * @param encoding * if null - system default encoding is used * @return * @throws IOException */ static final String loadSection( String file, String section, String encoding ) throws IOException { Section sect = getSection( file, section ); if( sect == null ) return null; InputStream is = HELPFUNCTIONS.getClass().getResourceAsStream( file ); int len = sect.endsec - sect.startsec; is.skip( (long)sect.startsec ); byte buf[] = new byte[len]; is.read( buf ); is.close(); if(encoding != null) { try { return new String(buf,0,len,encoding).trim(); } catch (UnsupportedEncodingException e) { debug("cannot decode name from UTF-8"); throw new IOException(); } } else return new String(buf,0,len).trim();// return getStringUTF8( buf, 0, len ).trim(); } static class Section { String name; short startsec,endsec; Vector links; // of section Section( String _name, int _startsec, int _endsec, Vector _links ) { name = _name; startsec = (short)_startsec; endsec = (short)_endsec; links = _links; } }//#endif //#ifdef JAVA_COMPILER protected String getModuleClassName() { return "HelpFunctions"; }//#endif }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -