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 {
23 * The HTML5 canvas delivers [r g b a r g b a ...] which is not a Java option.
24 * The closest Java option is TYPE_4BYTE_ABGR, but that is not quite what we
25 * need. SwingJS decodes TYPE_4BYTE_HTML5 as TYPE_4BYTE_RGBA"
27 * ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
29 * int[] nBits = { 8, 8, 8, 8 };
31 * int[] bOffs = { 0, 1, 2, 3 };
33 * colorModel = new ComponentColorModel(cs, nBits, true, false,
34 * Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
36 * raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
37 * width * 4, 4, bOffs, null);
39 * Note, however, that this buffer type should only be used for direct buffer access
45 public static final int TYPE_4BYTE_HTML5 = -6;
48 * The HTML5 VIDEO element wrapped in a BufferedImage.
50 * To be extended to allow video capture?
52 public static final int TYPE_HTML5_VIDEO = Integer.MIN_VALUE;
55 * Indicate to SwingJS that the given file type is binary.
59 void addBinaryFileType(String ext);
62 * Indicate to SwingJS that we can load files using AJAX from the given domain,
63 * such as "www.stolaf.edu", because we know that CORS access has been provided.
67 void addDirectDatabaseCall(String domain);
70 * Cache or uncache data under the given path name.
73 * @param data null to remove from the cache
75 void cachePathData(String path, Object data);
78 * Get the HTML5 object corresponding to the specified Component, or the current thread if null.
80 * @param c the associated component, or null for the current thread
81 * @return HTML5 applet object
83 HTML5Applet getAppletForComponent(Component c);
86 * Get an attribute applet.foo for the applet found using getApplet(null).
91 Object getAppletAttribute(String key);
95 * Get the applet's __Info map or an attribute of that map for the applet found using
96 * getApplet(null). That is, applet.__Info or applet.__Info[InfoKey].
98 * @param infoKey if null, return the full __Info map
100 Object getAppletInfo(String infoKey);
103 * Get the code base (swingjs/j2s, probably) for the applet found using
111 * Get the document base (wherever the page is) for the applet found using
117 URL getDocumentBase();
120 * Get an attribute from the div on the page that is associated with this frame,
121 * i.e. with id frame.getName() + "-div".
124 * @param type "node" or "dim"
127 Object getEmbeddedAttribute(Component frame, String type);
130 * Get a file synchronously.
133 * @param asString true for String; false for byte[]
134 * @return byte[] or String
136 Object getFile(String path, boolean asString);
139 * Get the 秘bytes field associated with a file, but only if the File object itself has
140 * them attached, not downloading them.
145 byte[] getBytes(File f);
148 * Retrieve a HashMap consisting of whatever the application wants, but
149 * guaranteed to be unique to this app context, that is, for the applet found using
155 HashMap<?, ?> getJSContext(Object contextKey);
158 * Load a resource -- probably a core file -- if and only if a particular class
159 * has not been instantialized. We use a String here because if we used a .class
160 * object, that reference itself would simply load the class, and we want the
161 * core package to include that as well.
163 * @param resourcePath
166 void loadResourceIfClassUnknown(String resource, String className);
169 * Read all applet.__Info properties for the applet found using
170 * getApplet(null) that start with the given prefix, such as "jalview_".
171 * A null prefix retrieves all properties. Note that non-string properties will be
174 * @param prefix an application prefix, or null for all properties
175 * @param p properties to be appended to
177 void readInfoProperties(String prefix, Properties p);
180 * Set an attribute for the applet found using
181 * getApplet(null). That is, applet[key] = val.
186 void setAppletAttribute(String key, Object val);
189 * Set an attribute of applet's Info map for the applet found using
190 * getApplet(null). That is, applet.__Info[key] = val.
195 void setAppletInfo(String infoKey, Object val);
198 * Set the given File object's 秘bytes field from an InputStream or a byte[] array.
199 * If the file is a JSTempFile, then also cache those bytes.
202 * @param isOrBytes BufferedInputStream, ByteArrayInputStream, FileInputStream, or byte[]
205 boolean setFileBytes(File f, Object isOrBytes);
208 * Set the given URL object's _streamData field from an InputStream or a byte[] array.
211 * @param isOrBytes BufferedInputStream, ByteArrayInputStream, FileInputStream, or byte[]
214 boolean setURLBytes(URL url, Object isOrBytes);
217 * Same as setFileBytes.
223 boolean streamToFile(InputStream is, File outFile);
226 * Switch the flag in SwingJS to use or not use the JavaScript Map object in
227 * Hashtable, HashMap, and HashSet. Default is enabled.
230 void setJavaScriptMapObjectEnabled(boolean enabled);
234 * Open a URL in a browser tab.
237 * @param target null or specific tab, such as "_blank"
239 void displayURL(String url, String target);
242 * Retrieve cached bytes for a path (with unnormalized name)
243 * from J2S._javaFileCache.
247 * @return byte[] or null
249 byte[] getCachedBytes(String path);
252 * Attach cached bytes to a file-like object, including URL,
253 * or anything having a 秘bytes field (File, URI, Path)
254 * from J2S._javaFileCache. That is, allow two such objects
255 * to share the same underlying byte[ ] array.
258 * @param URLorURIorFile
259 * @return byte[] or null
261 byte[] addJSCachedBytes(Object URLorURIorFile);
264 * Seek an open ZipInputStream to the supplied ZipEntry, if possible.
266 * @param zis the ZipInputStream
267 * @param ze the ZipEntry
268 * @return the length of this entry, or -1 if, for whatever reason, this was not possible
270 long seekZipEntry(ZipInputStream zis, ZipEntry ze);
273 * Retrieve the byte array associated with a ZipEntry.
278 byte[] getZipBytes(ZipEntry ze);
281 * Java 9 method to read all (remaining) bytes from an InputStream. In SwingJS,
282 * this may just create a new reference to an underlying Int8Array without
287 * @throws IOException
289 byte[] readAllBytes(InputStream zis) throws IOException;
292 * Java 9 method to transfer all (remaining) bytes from an InputStream to an OutputStream.
297 * @throws IOException
299 long transferTo(InputStream is, OutputStream out) throws IOException;
302 * Retrieve any bytes already attached to this URL.
307 byte[] getURLBytes(URL url);
310 * Set a message in the lower-left-hand corner SwingJS status block.
315 void showStatus(String msg, boolean doFadeOut);
318 * Asynchronously retrieve the byte[] for a URL.
323 void getURLBytesAsync(URL url, Function<byte[], Void> whenDone);
326 * Experimental method to completely disable a Swing Component's user interface.
331 void setUIEnabled(JComponent jc, boolean enabled);
337 * @param format a javax.sound.sampled.AudioFormat
340 void playAudio(byte[] buffer, Object format) throws Exception;
343 * For either an applet or an application, get the ORIGINAL __Info as a Map that
344 * has a full set up lower-case keys along with whatever non-all-lower-case keys
345 * provided at start-up.
349 Map<String, Object> getAppletInfoAsMap();
352 void setAppClass(Object j);