package uk.co.mmscomputing.device.twain; import java.util.*; public class TwainCapability implements TwainConstants{ // DAT_CAPABILITY. Used by application to get/set capability from/in a data source. /* typedef struct { TW_UINT16 Cap; // id of capability to set or get, e.g. CAP_BRIGHTNESS TW_UINT16 ConType; // TWON_ONEVALUE, _RANGE, _ENUMERATION or _ARRAY TW_HANDLE hContainer; // Handle to container of type Dat } TW_CAPABILITY, FAR * pTW_CAPABILITY; */ protected TwainSource source; protected int cap; protected byte[] buf=new byte[8]; // TW_CAPABILITY protected TwainContainer container; TwainCapability(TwainSource source,int cap)throws TwainIOException{ this.source=source; this.cap=cap; this.container=null; container=get(); } TwainCapability(TwainSource source,int cap,int mode)throws TwainIOException{ this.source=source; this.cap=cap; this.container=null; switch(mode){ case MSG_GET: container=get(); break; case MSG_GETCURRENT: container=getCurrent(); break; case MSG_GETDEFAULT: container=getDefault(); break; default: container=get(); break; } } private TwainContainer get(int msg,int contype)throws TwainIOException{ jtwain.setINT16(buf,0,cap); // Cap : set capability we are looking for jtwain.setINT16(buf,2,contype); // ConType : what container do we accept jtwain.setINT32(buf,4,0); // hContainer : source will set handle to container source.call(DG_CONTROL,DAT_CAPABILITY,msg,buf); // [1] 7-145 pp state 4 - 7; get capability container int containerType =jtwain.getINT16(buf,2); // get container type int containerPtr =jtwain.getINT32(buf,4); // ptr to native capability container byte[] container =jtwain.ngetContainer(containerType,containerPtr); // get java capability container, free native container switch(containerType){ // As of TWAIN 1.9 spec these are the only container types possible case TWON_ARRAY: return new TwainArray(cap,container); case TWON_ENUMERATION: return new TwainEnumeration(cap,container); case TWON_ONEVALUE: return new TwainOneValue(cap,container); case TWON_RANGE: return new TwainRange(cap,container); default: throw new TwainIOException(getClass().getName()+".get:\n\tUnknown container type."); } } private TwainContainer get(int msg)throws TwainIOException{ return get(msg,-1); // ConType: accept any container type } public TwainContainer get()throws TwainIOException{ container= get(MSG_GET); // [1] 7-145 state 4 - 7; get capability container return container; } public TwainContainer getCurrent()throws TwainIOException{ return get(MSG_GETCURRENT); // [1] 7-147 state 4 - 7; get current capability container } public TwainContainer getDefault()throws TwainIOException{ return get(MSG_GETDEFAULT); // [1] 7-149 state 4 - 7; get default capability container } public int querySupport()throws TwainIOException{ // what actions are allowed on this capability return get(MSG_QUERYSUPPORT,TWON_ONEVALUE).intValue(); // [1] 7-151 state 4 - 7; TWTY_INT32 flags TWQC_GET, TWQC_SET ... } public boolean querySupport(int flagMask){ try{ int flags=get(MSG_QUERYSUPPORT,TWON_ONEVALUE).intValue(); return ((flags&flagMask)!=0); }catch(TwainIOException tioe){ // if some error assume not supported System.out.println("3\b"+getClass().getName()+".querySupport:\n\t"+tioe.getMessage()); return false; } } public TwainContainer reset()throws TwainIOException{ container = get(MSG_RESET); // [1] 7-153 state 4; reset to twain or source default return container; } public TwainContainer set()throws TwainIOException{ container = set(container); return container; } private TwainContainer set(TwainContainer container)throws TwainIOException{ // use only in state 4 int containerType =container.getType(); byte[] containerBytes =container.getBytes(); int containerHandle =jtwain.nsetContainer(containerType,containerBytes); // allocate native memory and copy container data into it. try{ jtwain.setINT16(buf,0,cap); // set capability jtwain.setINT16(buf,2,containerType); // set container type jtwain.setINT32(buf,4,containerHandle); // set native container // System.err.println(container.toString()); // System.err.println(TwainContainer.toString(containerBytes)); source.call(DG_CONTROL,DAT_CAPABILITY,MSG_SET,buf); }catch(TwainResultException.CheckStatus trecs){ container=get(); }finally{ jtwain.nfree(containerHandle); // release native memory again } return container; } public Object[] getItems(){ return container.getItems();} public boolean booleanValue()throws TwainIOException{ return getCurrent().booleanValue();} public int intValue() throws TwainIOException{ return getCurrent().intValue();} public double doubleValue() throws TwainIOException{ return getCurrent().doubleValue();} public void setCurrentValue(boolean v)throws TwainIOException{setCurrentValue(new Boolean(v));} public void setCurrentValue(int v) throws TwainIOException{setCurrentValue(new Integer(v));} public void setCurrentValue(double v) throws TwainIOException{setCurrentValue(new Double(v));} public void setCurrentValue(Object val)throws TwainIOException{ container.setCurrentValue(val);set(); } public boolean booleanDefaultValue()throws TwainIOException{ return getDefault().booleanDefaultValue();} public int intDefaultValue() throws TwainIOException{ return getDefault().intDefaultValue();} public double doubleDefaultValue() throws TwainIOException{ return getDefault().doubleDefaultValue();} public void setDefaultValue(boolean v)throws TwainIOException{setDefaultValue(new Boolean(v));} public void setDefaultValue(int v) throws TwainIOException{setDefaultValue(new Integer(v));} public void setDefaultValue(double v) throws TwainIOException{setDefaultValue(new Double(v));} public void setDefaultValue(Object val)throws TwainIOException{ container.setDefaultValue(val);set(); } protected String toString(String[] strs){ String s=getClass().getName()+"\n"; Object[] items =container.getItems(); for(int i=0;i