3 import java.awt.Component;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
9 import java.util.HashMap;
11 import java.util.Properties;
12 import java.util.function.Function;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
16 import javax.swing.JComponent;
18 import swingjs.api.js.HTML5Applet;
20 public interface JSUtilI
24 * The HTML5 canvas delivers [r g b a r g b a ...] which is not a Java option.
25 * The closest Java option is TYPE_4BYTE_ABGR, but that is not quite what we
26 * need. SwingJS decodes TYPE_4BYTE_HTML5 as TYPE_4BYTE_RGBA"
28 * ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
30 * int[] nBits = { 8, 8, 8, 8 };
32 * int[] bOffs = { 0, 1, 2, 3 };
34 * colorModel = new ComponentColorModel(cs, nBits, true, false,
35 * Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
37 * raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
38 * width * 4, 4, bOffs, null);
40 * Note, however, that this buffer type should only be used for direct buffer access
46 public static final int TYPE_4BYTE_HTML5 = -6;
49 * The HTML5 VIDEO element wrapped in a BufferedImage.
51 * To be extended to allow video capture?
53 public static final int TYPE_HTML5_VIDEO = Integer.MIN_VALUE;
56 * Indicate to SwingJS that the given file type is binary.
60 void addBinaryFileType(String ext);
63 * Indicate to SwingJS that we can load files using AJAX from the given domain,
64 * such as "www.stolaf.edu", because we know that CORS access has been provided.
68 void addDirectDatabaseCall(String domain);
71 * Cache or uncache data under the given path name.
74 * @param data null to remove from the cache
76 void cachePathData(String path, Object data);
79 * Get the HTML5 object corresponding to the specified Component, or the current thread if null.
81 * @param c the associated component, or null for the current thread
82 * @return HTML5 applet object
84 HTML5Applet getAppletForComponent(Component c);
87 * Get an attribute applet.foo for the applet found using getApplet(null).
92 Object getAppletAttribute(String key);
96 * Get the applet's __Info map or an attribute of that map for the applet found using
97 * getApplet(null). That is, applet.__Info or applet.__Info[InfoKey].
99 * @param infoKey if null, return the full __Info map
101 Object getAppletInfo(String infoKey);
104 * Get the code base (swingjs/j2s, probably) for the applet found using
112 * Get the document base (wherever the page is) for the applet found using
118 URL getDocumentBase();
121 * Get an attribute from the div on the page that is associated with this frame,
122 * i.e. with id frame.getName() + "-div".
125 * @param type "node" or "dim"
128 Object getEmbeddedAttribute(Component frame, String type);
131 * Get a file synchronously.
134 * @param asString true for String; false for byte[]
135 * @return byte[] or String
137 Object getFile(String path, boolean asString);
140 * Get the 秘bytes field associated with a file, but only if the File object itself has
141 * them attached, not downloading them.
146 byte[] getBytes(File f);
149 * Retrieve a HashMap consisting of whatever the application wants, but
150 * guaranteed to be unique to this app context, that is, for the applet found using
156 HashMap<?, ?> getJSContext(Object contextKey);
159 * Load a resource -- probably a core file -- if and only if a particular class
160 * has not been instantialized. We use a String here because if we used a .class
161 * object, that reference itself would simply load the class, and we want the
162 * core package to include that as well.
164 * @param resourcePath
167 void loadResourceIfClassUnknown(String resource, String className);
170 * Read all applet.__Info properties for the applet found using
171 * getApplet(null) that start with the given prefix, such as "jalview_".
172 * A null prefix retrieves all properties. Note that non-string properties will be
175 * @param prefix an application prefix, or null for all properties
176 * @param p properties to be appended to
178 void readInfoProperties(String prefix, Properties p);
181 * Set an attribute for the applet found using
182 * getApplet(null). That is, applet[key] = val.
187 void setAppletAttribute(String key, Object val);
190 * Set an attribute of applet's Info map for the applet found using
191 * getApplet(null). That is, applet.__Info[key] = val.
196 void setAppletInfo(String infoKey, Object val);
199 * Set the given File object's 秘bytes field from an InputStream or a byte[] array.
200 * If the file is a JSTempFile, then also cache those bytes.
203 * @param isOrBytes BufferedInputStream, ByteArrayInputStream, FileInputStream, or byte[]
206 boolean setFileBytes(File f, Object isOrBytes);
209 * Set the given URL object's _streamData field from an InputStream or a byte[] array.
212 * @param isOrBytes BufferedInputStream, ByteArrayInputStream, FileInputStream, or byte[]
215 boolean setURLBytes(URL url, Object isOrBytes);
218 * Same as setFileBytes.
224 boolean streamToFile(InputStream is, File outFile);
227 * Switch the flag in SwingJS to use or not use the JavaScript Map object in
228 * Hashtable, HashMap, and HashSet. Default is enabled.
231 void setJavaScriptMapObjectEnabled(boolean enabled);
235 * Open a URL in a browser tab.
238 * @param target null or specific tab, such as "_blank"
240 void displayURL(String url, String target);
243 * Retrieve cached bytes for a path (with unnormalized name)
244 * from J2S._javaFileCache.
248 * @return byte[] or null
250 byte[] getCachedBytes(String path);
253 * Attach cached bytes to a file-like object, including URL,
254 * or anything having a 秘bytes field (File, URI, Path)
255 * from J2S._javaFileCache. That is, allow two such objects
256 * to share the same underlying byte[ ] array.
259 * @param URLorURIorFile
260 * @return byte[] or null
262 byte[] addJSCachedBytes(Object URLorURIorFile);
265 * Seek an open ZipInputStream to the supplied ZipEntry, if possible.
267 * @param zis the ZipInputStream
268 * @param ze the ZipEntry
269 * @return the length of this entry, or -1 if, for whatever reason, this was not possible
271 long seekZipEntry(ZipInputStream zis, ZipEntry ze);
274 * Retrieve the byte array associated with a ZipEntry.
279 byte[] getZipBytes(ZipEntry ze);
282 * Java 9 method to read all (remaining) bytes from an InputStream. In SwingJS,
283 * this may just create a new reference to an underlying Int8Array without
288 * @throws IOException
290 byte[] readAllBytes(InputStream zis) throws IOException;
293 * Java 9 method to transfer all (remaining) bytes from an InputStream to an OutputStream.
298 * @throws IOException
300 long transferTo(InputStream is, OutputStream out) throws IOException;
303 * Retrieve any bytes already attached to this URL.
308 byte[] getURLBytes(URL url);
311 * Set a message in the lower-left-hand corner SwingJS status block.
316 void showStatus(String msg, boolean doFadeOut);
319 * Asynchronously retrieve the byte[] for a URL.
324 void getURLBytesAsync(URL url, Function<byte[], Void> whenDone);
327 * Experimental method to completely disable a Swing Component's user interface.
332 void setUIEnabled(JComponent jc, boolean enabled);
338 * @param format a javax.sound.sampled.AudioFormat
341 void playAudio(byte[] buffer, Object format) throws Exception;
344 * For either an applet or an application, get the ORIGINAL __Info as a Map that
345 * has a full set up lower-case keys along with whatever non-all-lower-case keys
346 * provided at start-up.
350 Map<String, Object> getAppletInfoAsMap();
353 void setAppClass(Object j);