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;
10 import java.util.Properties;
11 import java.util.function.Function;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipInputStream;
15 import javax.swing.JComponent;
17 import swingjs.api.js.HTML5Applet;
19 public interface JSUtilI {
22 * The HTML5 canvas delivers [r g b a r g b a ...] which is not a Java option.
23 * The closest Java option is TYPE_4BYTE_ABGR, but that is not quite what we
24 * need. SwingJS decodes TYPE_4BYTE_HTML5 as TYPE_4BYTE_RGBA"
26 * ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
28 * int[] nBits = { 8, 8, 8, 8 };
30 * int[] bOffs = { 0, 1, 2, 3 };
32 * colorModel = new ComponentColorModel(cs, nBits, true, false,
33 * Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
35 * raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
36 * width * 4, 4, bOffs, null);
38 * Note, however, that this buffer type should only be used for direct buffer access
44 public static final int TYPE_4BYTE_HTML5 = -6;
47 * The HTML5 VIDEO element wrapped in a BufferedImage.
49 * To be extended to allow video capture?
51 public static final int TYPE_HTML5_VIDEO = Integer.MIN_VALUE;
54 * Indicate to SwingJS that the given file type is binary.
58 void addBinaryFileType(String ext);
61 * Indicate to SwingJS that we can load files using AJAX from the given domain,
62 * such as "www.stolaf.edu", because we know that CORS access has been provided.
66 void addDirectDatabaseCall(String domain);
69 * Cache or uncache data under the given path name.
72 * @param data null to remove from the cache
74 void cachePathData(String path, Object data);
77 * Get the HTML5 object corresponding to the specified Component, or the current thread if null.
79 * @param c the associated component, or null for the current thread
80 * @return HTML5 applet object
82 HTML5Applet getAppletForComponent(Component c);
85 * Get an attribute applet.foo for the applet found using getApplet(null).
90 Object getAppletAttribute(String key);
94 * Get an attribute of applet's Info map for the applet found using
95 * getApplet(null). That is, applet.__Info[InfoKey].
99 Object getAppletInfo(String infoKey);
102 * Get the code base (swingjs/j2s, probably) for the applet found using
110 * Get the document base (wherever the page is) for the applet found using
116 URL getDocumentBase();
119 * Get an attribute from the div on the page that is associated with this frame,
120 * i.e. with id frame.getName() + "-div".
123 * @param type "node" or "dim"
126 Object getEmbeddedAttribute(Component frame, String type);
129 * Get a file synchronously.
132 * @param asString true for String; false for byte[]
133 * @return byte[] or String
135 Object getFile(String path, boolean asString);
138 * Get the 秘bytes field associated with a file, but only if the File object itself has
139 * them attached, not downloading them.
144 byte[] getBytes(File f);
147 * Retrieve a HashMap consisting of whatever the application wants, but
148 * guaranteed to be unique to this app context, that is, for the applet found using
154 HashMap<?, ?> getJSContext(Object contextKey);
157 * Load a resource -- probably a core file -- if and only if a particular class
158 * has not been instantialized. We use a String here because if we used a .class
159 * object, that reference itself would simply load the class, and we want the
160 * core package to include that as well.
162 * @param resourcePath
165 void loadResourceIfClassUnknown(String resource, String className);
168 * Read all applet.__Info properties for the applet found using
169 * getApplet(null) that start with the given prefix, such as "jalview_".
170 * A null prefix retrieves all properties. Note that non-string properties will be
173 * @param prefix an application prefix, or null for all properties
174 * @param p properties to be appended to
176 void readInfoProperties(String prefix, Properties p);
179 * Set an attribute for the applet found using
180 * getApplet(null). That is, applet[key] = val.
185 void setAppletAttribute(String key, Object val);
188 * Set an attribute of applet's Info map for the applet found using
189 * getApplet(null). That is, applet.__Info[key] = val.
194 void setAppletInfo(String infoKey, Object val);
197 * Set the given File object's 秘bytes field from an InputStream or a byte[] array.
198 * If the file is a JSTempFile, then also cache those bytes.
201 * @param isOrBytes BufferedInputStream, ByteArrayInputStream, FileInputStream, or byte[]
204 boolean setFileBytes(File f, Object isOrBytes);
207 * Set the given URL object's _streamData field from an InputStream or a byte[] array.
210 * @param isOrBytes BufferedInputStream, ByteArrayInputStream, FileInputStream, or byte[]
213 boolean setURLBytes(URL url, Object isOrBytes);
216 * Same as setFileBytes.
222 boolean streamToFile(InputStream is, File outFile);
225 * Switch the flag in SwingJS to use or not use the JavaScript Map object in
226 * Hashtable, HashMap, and HashSet. Default is enabled.
229 void setJavaScriptMapObjectEnabled(boolean enabled);
233 * Open a URL in a browser tab.
236 * @param target null or specific tab, such as "_blank"
238 void displayURL(String url, String target);
241 * Retrieve cached bytes for a path (with unnormalized name)
242 * from J2S._javaFileCache.
246 * @return byte[] or null
248 byte[] getCachedBytes(String path);
251 * Attach cached bytes to a file-like object, including URL,
252 * or anything having a 秘bytes field (File, URI, Path)
253 * from J2S._javaFileCache. That is, allow two such objects
254 * to share the same underlying byte[ ] array.
257 * @param URLorURIorFile
258 * @return byte[] or null
260 byte[] addJSCachedBytes(Object URLorURIorFile);
263 * Seek an open ZipInputStream to the supplied ZipEntry, if possible.
265 * @param zis the ZipInputStream
266 * @param ze the ZipEntry
267 * @return the length of this entry, or -1 if, for whatever reason, this was not possible
269 long seekZipEntry(ZipInputStream zis, ZipEntry ze);
272 * Retrieve the byte array associated with a ZipEntry.
277 byte[] getZipBytes(ZipEntry ze);
280 * Java 9 method to read all (remaining) bytes from an InputStream. In SwingJS,
281 * this may just create a new reference to an underlying Int8Array without
286 * @throws IOException
288 byte[] readAllBytes(InputStream zis) throws IOException;
291 * Java 9 method to transfer all (remaining) bytes from an InputStream to an OutputStream.
296 * @throws IOException
298 long transferTo(InputStream is, OutputStream out) throws IOException;
301 * Retrieve any bytes already attached to this URL.
306 byte[] getURLBytes(URL url);
309 * Set a message in the lower-left-hand corner SwingJS status block.
314 void showStatus(String msg, boolean doFadeOut);
317 * Asynchronously retrieve the byte[] for a URL.
322 void getURLBytesAsync(URL url, Function<byte[], Void> whenDone);
325 * Experimental method to completely disable a Swing Component's user interface.
330 void setUIEnabled(JComponent jc, boolean enabled);