1 package jspecview.java;
2 
3 import java.awt.Container;
4 import java.awt.Frame;
5 import java.awt.GraphicsEnvironment;
6 import java.awt.Window;
7 import java.io.BufferedInputStream;
8 import java.io.IOException;
9 import java.net.URL;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 import java.util.Map;
13 
14 import javajs.util.P3;
15 import javajs.util.Rdr;
16 
17 import javax.swing.JDialog;
18 import javax.swing.JOptionPane;
19 
20 import org.jmol.api.GenericFileInterface;
21 import org.jmol.api.GenericImageDialog;
22 import org.jmol.api.GenericMenuInterface;
23 import org.jmol.api.GenericMouseInterface;
24 import org.jmol.api.GenericPlatform;
25 import org.jmol.api.JmolInChI;
26 import org.jmol.api.PlatformViewer;
27 import org.jmol.i18n.GT;
28 import org.jmol.util.Font;
29 
30 import jspecview.api.JSVPanel;
31 
32 
33 import netscape.javascript.JSObject;
34 
35 
36 public class AwtPlatform implements GenericPlatform {
37 
38   PlatformViewer vwr;
39 
40   @Override
setViewer(PlatformViewer viewer, Object display)41 	public void setViewer(PlatformViewer viewer, Object display) {
42     this.vwr = viewer;
43   }
44 
45   ///// Display
46 
47   @Override
convertPointFromScreen(Object display, P3 ptTemp)48 	public void convertPointFromScreen(Object display, P3 ptTemp) {
49     Display.convertPointFromScreen(display, ptTemp);
50   }
51 
52   @Override
getFullScreenDimensions(Object display, int[] widthHeight)53 	public void getFullScreenDimensions(Object display, int[] widthHeight) {
54     Display.getFullScreenDimensions(display, widthHeight);
55   }
56 
57   @Override
getMenuPopup(String menuStructure, char type)58 	public GenericMenuInterface getMenuPopup(String menuStructure, char type) {
59   	return null;//
60   }
61 
62   @Override
hasFocus(Object display)63 	public boolean hasFocus(Object display) {
64     return Display.hasFocus(display);
65   }
66 
67   @Override
prompt(String label, String data, String[] list, boolean asButtons)68 	public String prompt(String label, String data, String[] list,
69                        boolean asButtons) {
70     return Display.prompt(label, data, list, asButtons);
71   }
72 
73   /**
74    * legacy apps will use this
75    *
76    * @param g
77    * @param size
78    */
79   @Override
renderScreenImage(Object g, Object size)80 	public void renderScreenImage(Object g, Object size) {
81     Display.renderScreenImage(vwr, g, size);
82   }
83 
84   @Override
requestFocusInWindow(Object display)85 	public void requestFocusInWindow(Object display) {
86     Display.requestFocusInWindow(display);
87   }
88 
89   @Override
repaint(Object display)90 	public void repaint(Object display) {
91     Display.repaint(display);
92   }
93 
94   @Override
setTransparentCursor(Object display)95 	public void setTransparentCursor(Object display) {
96     Display.setTransparentCursor(display);
97   }
98 
99   @Override
setCursor(int c, Object display)100 	public void setCursor(int c, Object display) {
101     Display.setCursor(c, display);
102   }
103 
104   ////// Mouse
105 
106   @Override
getMouseManager(double ignored, Object jsvp)107 	public GenericMouseInterface getMouseManager(double ignored, Object jsvp) {
108     return new Mouse((JSVPanel) jsvp);
109   }
110 
111   ////// Image
112 
113   @Override
allocateRgbImage(int windowWidth, int windowHeight, int[] pBuffer, int windowSize, boolean backgroundTransparent, boolean isImageWrite)114 	public Object allocateRgbImage(int windowWidth, int windowHeight,
115                                  int[] pBuffer, int windowSize,
116                                  boolean backgroundTransparent, boolean isImageWrite) {
117     return Image.allocateRgbImage(windowWidth, windowHeight, pBuffer, windowSize, backgroundTransparent);
118   }
119 
120   /**
121    * could be byte[] (from ZIP file) or String (local file name) or URL
122    * @param data
123    * @return image object
124    *
125    */
126   @Override
createImage(Object data)127 	public Object createImage(Object data) {
128     return Image.createImage(data);
129   }
130 
131   @Override
disposeGraphics(Object gOffscreen)132 	public void disposeGraphics(Object gOffscreen) {
133     Image.disposeGraphics(gOffscreen);
134   }
135 
136   @Override
drawImage(Object g, Object img, int x, int y, int width, int height, boolean isDTI)137 	public void drawImage(Object g, Object img, int x, int y, int width, int height, boolean isDTI) {
138     Image.drawImage(g, img, x, y, width, height);
139   }
140 
141   @Override
grabPixels(Object imageobj, int width, int height, int[] pixels, int startRow, int nRows)142 	public int[] grabPixels(Object imageobj, int width, int height, int[] pixels, int startRow, int nRows) {
143     return Image.grabPixels(imageobj, width, height, pixels, startRow, nRows);
144   }
145 
146   @Override
drawImageToBuffer(Object gOffscreen, Object imageOffscreen, Object imageobj, int width, int height, int bgcolor)147 	public int[] drawImageToBuffer(Object gOffscreen, Object imageOffscreen,
148                                  Object imageobj, int width, int height, int bgcolor) {
149     return Image.drawImageToBuffer(gOffscreen, imageOffscreen, imageobj, width, height, bgcolor);
150   }
151 
152   @Override
getTextPixels(String text, Font font3d, Object gObj, Object image, int width, int height, int ascent)153 	public int[] getTextPixels(String text, Font font3d, Object gObj,
154                              Object image, int width, int height, int ascent) {
155     return Image.getTextPixels(text, font3d, gObj, image, width, height, ascent);
156   }
157 
158   @Override
flushImage(Object imagePixelBuffer)159 	public void flushImage(Object imagePixelBuffer) {
160     Image.flush(imagePixelBuffer);
161   }
162 
163   @Override
getGraphics(Object image)164 	public Object getGraphics(Object image) {
165     return Image.getGraphics(image);
166   }
167 
168   @Override
getImageHeight(Object image)169 	public int getImageHeight(Object image) {
170     return (image == null ? -1 : Image.getHeight(image));
171   }
172 
173   @Override
getImageWidth(Object image)174 	public int getImageWidth(Object image) {
175     return (image == null ? -1 : Image.getWidth(image));
176   }
177 
178   @Override
getStaticGraphics(Object image, boolean backgroundTransparent)179 	public Object getStaticGraphics(Object image, boolean backgroundTransparent) {
180     return Image.getStaticGraphics(image, backgroundTransparent);
181   }
182 
183   @Override
newBufferedImage(Object image, int w, int h)184 	public Object newBufferedImage(Object image, int w, int h) {
185     return Image.newBufferedImage(image, w, h);
186   }
187 
188   @Override
newOffScreenImage(int w, int h)189 	public Object newOffScreenImage(int w, int h) {
190     return Image.newBufferedImage(w, h);
191   }
192 
193   @Override
waitForDisplay(Object ignored, Object image)194 	public boolean waitForDisplay(Object ignored, Object image) throws InterruptedException {
195     Image.waitForDisplay(vwr, image);
196     return true;
197   }
198 
199 
200   ///// FONT
201 
202   @Override
fontStringWidth(Font font, String text)203 	public int fontStringWidth(Font font, String text) {
204     return AwtFont.stringWidth(font.getFontMetrics(), text);
205   }
206 
207   @Override
getFontAscent(Object fontMetrics)208 	public int getFontAscent(Object fontMetrics) {
209     return AwtFont.getAscent(fontMetrics);
210   }
211 
212   @Override
getFontDescent(Object fontMetrics)213 	public int getFontDescent(Object fontMetrics) {
214     return AwtFont.getDescent(fontMetrics);
215   }
216 
217   @Override
getFontMetrics(Font font, Object graphics)218 	public Object getFontMetrics(Font font, Object graphics) {
219     return AwtFont.getFontMetrics(font, graphics);
220   }
221 
222   @Override
newFont(String fontFace, boolean isBold, boolean isItalic, float fontSize)223 	public Object newFont(String fontFace, boolean isBold, boolean isItalic, float fontSize) {
224     return AwtFont.newFont(fontFace, isBold, isItalic, fontSize);
225   }
226 
227   /// misc
228 
229   @Override
getJsObjectInfo(Object[] jsObject, String method, Object[] args)230 	public Object getJsObjectInfo(Object[] jsObject, String method, Object[] args) {
231     JSObject DOMNode = (JSObject) jsObject[0];
232     if (method == null) {
233       String namespaceURI = (String) DOMNode.getMember("namespaceURI");
234       String localName = (String) DOMNode.getMember("localName");
235       return "namespaceURI=\"" + namespaceURI + "\" localName=\"" + localName + "\"";
236     }
237     return (args == null ? DOMNode.getMember(method) : DOMNode.call(method, args));
238   }
239 
240   @Override
isHeadless()241 	public boolean isHeadless() {
242     return GraphicsEnvironment.isHeadless();
243   }
244 
245   @Override
isSingleThreaded()246 	public boolean isSingleThreaded() {
247     return false;
248   }
249 
250   @Override
notifyEndOfRendering()251 	public void notifyEndOfRendering() {
252     // N/A
253   }
254 
255   /**
256    * @param p
257    * @return The hosting frame or JDialog.
258    */
getWindow(Container p)259   static public Window getWindow(Container p) {
260     while (p != null) {
261       if (p instanceof Frame)
262         return (Frame) p;
263       else if (p instanceof JDialog)
264         return (JDialog) p;
265       else if (p instanceof JmolFrame)
266         return ((JmolFrame) p).getFrame();
267       p = p.getParent();
268     }
269     return null;
270   }
271 
272   @Override
getDateFormat(String isoType)273   public String getDateFormat(String isoType) {
274     if (isoType == null) {
275       isoType = "EEE, d MMM yyyy HH:mm:ss Z";
276     } else if (isoType.contains("8824")) {
277       return "D:" + new SimpleDateFormat("YYYYMMddHHmmssX").format(new Date())
278           + "'00'";
279     } else if (isoType.contains("8601")) {
280       return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date());
281     }
282     return new SimpleDateFormat(isoType).format(new Date());
283   }
284 
285   @Override
newFile(String name)286 	public GenericFileInterface newFile(String name) {
287     return new AwtFile(name);
288   }
289 
290   @Override
getBufferedFileInputStream(String name)291 	public Object getBufferedFileInputStream(String name) {
292     return AwtFile.getBufferedFileInputStream(name);
293   }
294 
295   @Override
getURLContents(URL url, byte[] outputBytes, String post, boolean asString)296   public Object getURLContents(URL url, byte[] outputBytes, String post,
297       boolean asString) {
298     Object ret = AwtFile.getURLContents(url, outputBytes, post);
299     try {
300       return (!asString ? ret : ret instanceof String ? ret : new String(
301           (byte[]) Rdr.getStreamAsBytes((BufferedInputStream) ret, null)));
302     } catch (IOException e) {
303       return "" + e;
304     }
305   }
306 
307 	@Override
getLocalUrl(String fileName)308 	public String getLocalUrl(String fileName) {
309 		// not used in JSpecView
310 		return null;
311 	}
312 
313 	@Override
getImageDialog(String title, Map<String, GenericImageDialog> imageMap)314 	public GenericImageDialog getImageDialog(String title,
315 			Map<String, GenericImageDialog> imageMap) {
316 		// TODO Auto-generated method stub
317 		return null;
318 	}
319 
320 	@Override
forceAsyncLoad(String filename)321 	public boolean forceAsyncLoad(String filename) {
322 		return false;
323 	}
324 
325   @Override
getInChI()326   public JmolInChI getInChI() {
327     // n/a for JSpecView
328     return null;
329   }
330 
331   @Override
confirm(String msg, String msgNo)332   public int confirm(String msg, String msgNo) {
333     int ret = JOptionPane.showConfirmDialog(null, GT.$(msg));
334     switch (ret) {
335     case JOptionPane.OK_OPTION:
336       return ret;
337     case JOptionPane.CANCEL_OPTION:
338       if (/** @j2sNative false && */
339       true)
340         return ret;
341       //$FALL-THROUGH$
342     case JOptionPane.NO_OPTION:
343     default:
344       return (msgNo != null && JOptionPane.showConfirmDialog(null,
345           GT.$(msgNo)) == JOptionPane.OK_OPTION ? JOptionPane.NO_OPTION
346               : JOptionPane.CANCEL_OPTION);
347     }
348   }
349 
350 }