1 /* $RCSfile$
2  * $Author: hansonr $
3  * $Date: 2019-11-23 22:17:53 -0600 (Sat, 23 Nov 2019) $
4  * $Revision: 22005 $
5  *
6  * Copyright (C) 2002-2005  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2.1 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 package org.openscience.jmol.app.jmolpanel;
25 
26 import java.io.BufferedReader;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.net.URL;
32 import java.util.Hashtable;
33 import java.util.Map;
34 
35 import javajs.util.PT;
36 
37 import javax.swing.JButton;
38 import javax.swing.JMenu;
39 import javax.swing.JMenuItem;
40 import javax.swing.AbstractButton;
41 import javax.swing.JCheckBoxMenuItem;
42 import javax.swing.JRadioButtonMenuItem;
43 import javax.swing.JCheckBox;
44 
45 import org.jmol.console.GenericConsole;
46 import org.jmol.console.JmolButton;
47 import org.jmol.console.KeyJMenu;
48 import org.jmol.console.KeyJMenuItem;
49 import org.jmol.console.KeyJCheckBox;
50 import org.jmol.console.KeyJCheckBoxMenuItem;
51 import org.jmol.console.KeyJRadioButtonMenuItem;
52 import org.jmol.i18n.GT;
53 import org.jmol.viewer.JC;
54 
55 public class GuiMap {
56 
57   private static Object language;
58 
59   Map<String, Object> map = new Hashtable<String, Object>();
60 
61   protected Map<String, String> labels;
62 
63   // keys here refer to keys listed in org.openscience.jmol.Properties.Jmol-resources.properties
64   // actions are either defined there, as xxxScript=, or by
65   // Actions created in DisplayPanel.java
66 
setupLabels()67   private void setupLabels() {
68     labels = new Hashtable<String, String>();
69     labels.put("macros", GT.$("&Macros"));
70     labels.put("idfileMenu", GT.$("&File"));
71     labels.put("file", GT.$("&File"));
72     labels.put("newwin", GT.$("&New"));
73     labels.put("open", GT.$("&Open"));
74     labels.put("openTip", GT.$("Open a file."));
75     labels.put("openurl", GT.$("Open &URL"));
76     labels.put("openpdb", GT.$("&Get PDB"));
77     labels.put("openmol", GT.$("Get &MOL"));
78     labels.put("reloadScript", GT.$("&Reload"));
79 
80     labels.put("spectrumMenu", "&Spectra");
81 
82     labels.put("openJSpecViewScript", "JSpecView");
83     labels.put("simulate1HSpectrumScript", "Simulated 1H Spectrum");
84     labels.put("simulate13CSpectrumScript", "Simulated 13C Spectrum");
85 
86     labels.put("editor", GT.$("Scrip&t Editor...")); // new %t 11.7.45
87     labels.put("console", GT.$("Conso&le..."));
88     labels.put("jconsole", GT.$("Jmol Java &Console"));
89     labels.put("atomsetchooser", GT.$("AtomSet&Chooser..."));
90     labels.put("saveas", GT.$("&Save As..."));
91     labels.put("exportMenu", GT.$("&Export"));
92     labels.put("export", GT.$("Export &Image..."));
93     labels.put("exportTip", GT.$("Save current view as an image."));
94     labels.put("toweb", GT.$("Export to &Web Page..."));
95     labels.put("towebTip", GT.$("Export one or more views to a web page."));
96     labels.put("povray", GT.$("Render in POV-&Ray..."));
97     labels.put("povrayTip", GT.$("Render in POV-Ray"));
98     labels.put("write", GT.$("Write &State..."));
99     labels.put("writeTip", GT.$("Save current view as a Jmol state script."));
100     labels.put("print", GT.$("&Print..."));
101     labels.put("printTip", GT.$("Print view."));
102     labels.put("close", GT.$("&Close"));
103     labels.put("exit", GT.$("E&xit"));
104     labels.put("recentFiles", GT.$("Recent &Files..."));
105     labels.put("edit", GT.$("&Edit"));
106     // labels.put("makecrystal", GT. _("Make crystal..."));
107     labels.put("selectall", GT.$("Select &All"));
108     labels.put("deselectall", GT.$("Deselect All"));
109     labels.put("copyImage", GT.$("Copy &Image"));
110     labels.put("copyScript", GT.$("Copy Script"));
111     labels.put("prefs", GT.$("Pr&eferences..."));
112     labels.put("pasteClipboard", GT.$("&Paste"));
113     labels.put("editSelectAllScript", GT.$("Select &All"));
114     labels.put("selectMenu", GT.$("&Select"));
115     labels.put("selectMenuText", GT.$("&Select"));
116     labels.put("selectAllScript", GT.$("&All"));
117     labels.put("selectNoneScript", GT.$("&None"));
118     labels.put("selectHydrogenScript", GT.$("Hydrogen"));
119     labels.put("selectCarbonScript", GT.$("Carbon"));
120     labels.put("selectNitrogenScript", GT.$("Nitrogen"));
121     labels.put("selectOxygenScript", GT.$("Oxygen"));
122     labels.put("selectPhosphorusScript", GT.$("Phosphorus"));
123     labels.put("selectSulfurScript", GT.$("Sulfur"));
124     labels.put("selectAminoScript", GT.$("Amino"));
125     labels.put("selectNucleicScript", GT.$("Nucleic"));
126     labels.put("selectWaterScript", GT.$("Water"));
127     labels.put("selectHeteroScript", GT.$("Hetero"));
128     labels.put("display", GT.$("&Display"));
129     labels.put("atomMenu", GT.$("&Atom"));
130     labels.put("atomNoneScript", GT.$("&None"));
131     labels.put("atom15Script", GT.o(GT.$("{0}% van der Waals"), "15"));
132     labels.put("atom20Script", GT.o(GT.$("{0}% van der Waals"), "20"));
133     labels.put("atom25Script", GT.o(GT.$("{0}% van der Waals"), "25"));
134     labels.put("atom100Script", GT.o(GT.$("{0}% van der Waals"), "100"));
135     labels.put("bondMenu", GT.$("&Bond"));
136     labels.put("bondNoneScript", GT.$("&None"));
137     labels.put("bondWireframeScript", GT.$("&Wireframe"));
138     labels.put("bond100Script", GT.o(GT.$("{0} \u00C5"), "0.10"));
139     labels.put("bond150Script", GT.o(GT.$("{0} \u00C5"), "0.15"));
140     labels.put("bond200Script", GT.o(GT.$("{0} \u00C5"), "0.20"));
141     labels.put("labelMenu", GT.$("&Label"));
142     labels.put("labelNoneScript", GT.$("&None"));
143     labels.put("labelSymbolScript", GT.$("&Symbol"));
144     labels.put("labelNameScript", GT.$("&Name"));
145     labels.put("labelNumberScript", GT.$("&Number"));
146     labels.put("labelCenteredScript", GT.$("&Centered"));
147     labels.put("labelUpperRightScript", GT.$("&Upper right"));
148     labels.put("vectorMenu", GT.$("&Vector"));
149     labels.put("vectorOffScript", GT.$("&None"));
150     labels.put("vectorOnScript", GT.$("&On"));
151     labels.put("vector3Script", GT.o(GT.$("{0} pixels"), "3"));
152     labels.put("vector005Script", GT.o(GT.$("{0} \u00C5"), "0.05"));
153     labels.put("vector01Script", GT.o(GT.$("{0} \u00C5"), "0.1"));
154     labels.put("vectorScale02Script", GT.o(GT.$("Scale {0}"), "0.2"));
155     labels.put("vectorScale05Script", GT.o(GT.$("Scale {0}"), "0.5"));
156     labels.put("vectorScale1Script", GT.o(GT.$("Scale {0}"), "1"));
157     labels.put("vectorScale2Script", GT.o(GT.$("Scale {0}"), "2"));
158     labels.put("vectorScale5Script", GT.o(GT.$("Scale {0}"), "5"));
159     labels.put("zoomMenu", GT.$("&Zoom"));
160     labels.put("zoom100Script", GT.o(GT.$("{0}%"), "100"));
161     labels.put("zoom150Script", GT.o(GT.$("{0}%"), "150"));
162     labels.put("zoom200Script", GT.o(GT.$("{0}%"), "200"));
163     labels.put("zoom400Script", GT.o(GT.$("{0}%"), "400"));
164     labels.put("zoom800Script", GT.o(GT.$("{0}%"), "800"));
165     labels.put("perspectiveCheck", GT.$("&Perspective Depth"));
166     labels.put("axesCheck", GT.$("A&xes"));
167     labels.put("boundboxCheck", GT.$("B&ounding Box"));
168     labels.put("hydrogensCheck", GT.$("&Hydrogens"));
169     labels.put("vectorsCheck", GT.$("V&ectors"));
170     labels.put("measurementsCheck", GT.$("&Measurements"));
171     labels.put("resize", GT.$("Resi&ze"));
172     labels.put("view", GT.$("&View"));
173     labels.put("front", GT.$("&Front"));
174     labels.put("top", GT.$("&Top"));
175     labels.put("bottom", GT.$("&Bottom"));
176     labels.put("right", GT.$("&Right"));
177     labels.put("left", GT.$("&Left"));
178     labels.put("axisaScript", GT.$("Axis a"));
179     labels.put("axisbScript", GT.$("Axis b"));
180     labels.put("axiscScript", GT.$("Axis c"));
181     labels.put("axisxScript", GT.$("Axis x"));
182     labels.put("axisyScript", GT.$("Axis y"));
183     labels.put("axiszScript", GT.$("Axis z"));
184     labels.put("transform", GT.$("Tr&ansform..."));
185     labels.put("definecenter", GT.$("Define &Center"));
186     labels.put("tools", GT.$("&Tools"));
187     labels.put("gauss", GT.$("&Gaussian..."));
188     labels.put("viewMeasurementTable", GT.$("&Measurements") + "...");
189     labels.put("distanceUnitsMenu", GT.$("Distance &Units"));
190     labels.put("distanceNanometersScript", GT.$("&Nanometers 1E-9"));
191     labels.put("distanceAngstromsScript", GT.$("&Angstroms 1E-10"));
192     labels.put("distancePicometersScript", GT.$("&Picometers 1E-12"));
193     labels.put("distanceHzScript", GT.$("&Hz (NMR J-coupling)"));
194     labels.put("animateMenu", GT.$("&Animate..."));
195     labels.put("vibrateMenu", GT.$("&Vibrate..."));
196     // these three are not implemented:
197     labels.put("graph", GT.$("&Graph..."));
198     labels.put("chemicalShifts", GT.$("Calculate chemical &shifts..."));
199     labels.put("crystprop", GT.$("&Crystal Properties"));
200     //
201     labels.put("animateOnceScript", GT.$("&Once"));
202     labels.put("animateLoopScript", GT.$("&Loop"));
203     labels.put("animatePalindromeScript", GT.$("P&alindrome"));
204     labels.put("animateStopScript", GT.$("&Stop animation"));
205     labels.put("animateRewindScript", GT.$("&Rewind to first frame"));
206     labels.put("animateRewindScriptTip", GT.$("Rewind to first frame"));
207     labels.put("animateNextScript", GT.$("Go to &next frame"));
208     labels.put("animateNextScriptTip", GT.$("Go to next frame"));
209     labels.put("animatePrevScript", GT.$("Go to &previous frame"));
210     labels.put("animatePrevScriptTip", GT.$("Go to previous frame"));
211     labels.put("animateAllScript", GT.$("All &frames"));
212     labels.put("animateAllScriptTip", GT.$("All frames"));
213     labels.put("animateLastScript", GT.$("Go to &last frame"));
214     labels.put("animateLastScriptTip", GT.$("Go to last frame"));
215     labels.put("vibrateStartScript", GT.$("Start &vibration"));
216     labels.put("vibrateStopScript", GT.$("&Stop vibration"));
217     labels.put("vibrateRewindScript", GT.$("&First frequency"));
218     labels.put("vibrateNextScript", GT.$("&Next frequency"));
219     labels.put("vibratePrevScript", GT.$("&Previous frequency"));
220     labels.put("surfaceTool", GT.$("SurfaceTool..."));
221     labels.put("surfaceToolTip", GT.$("Control Display of Surfaces"));
222     labels.put("help", GT.$("&Help"));
223     labels.put("about", GT.$("About Jmol"));
224     labels.put("uguide", GT.$("Jmol Wiki"));
225     labels.put("whatsnew", GT.$("What's New"));
226     labels.put("credits", GT.$("Credits"));
227     labels.put("Prefs.showHydrogens", GT.$("Hydrogens"));
228     labels.put("Prefs.showMeasurements", GT.$("Measurements"));
229     labels.put("Prefs.perspectiveDepth", GT.$("Perspective Depth"));
230     labels.put("Prefs.showAxes", GT.$("Axes"));
231     labels.put("Prefs.showBoundingBox", GT.$("Bounding Box"));
232     labels.put("Prefs.axesOrientationRasmol",
233         GT.$("RasMol/Chime compatible axes orientation/rotations"));
234     labels.put("Prefs.openFilePreview",
235         GT.$("File Preview (requires restarting Jmol)"));
236     labels.put("Prefs.clearHistory",
237         GT.$("Clear history (requires restarting Jmol)"));
238     labels.put("Prefs.largeFont", GT.$("Large Console Font"));
239     labels.put("Prefs.isLabelAtomColor", GT.$("Use Atom Color"));
240     labels.put("Prefs.isBondAtomColor", GT.$("Use Atom Color"));
241     labels.put("rotateScriptTip", GT.$("Rotate molecule."));
242     labels.put("pickScriptTip",
243         GT.$("Select a set of atoms using SHIFT-LEFT-DRAG."));
244     labels
245         .put("pickMeasureScriptTip", GT.$("Click atoms to measure distances"));
246     labels.put("pickCenterScriptTip", GT.$("Click an atom to center on it"));
247     labels
248         .put(
249             "pickLabelScriptTip",
250             GT.$("click an atom to toggle label;DOUBLE-Click a label to set; drag to move"));
251     labels.put("homeTip", GT.$("Return molecule to home position."));
252     labels.put("modelkitScriptTip", GT.$("Open the model kit."));
253     labels.put("JavaConsole.Clear", GT.$("Clear"));
254     labels.put("plugins", GT.$("&Plugins"));
255 
256     moreLabels(labels);
257 
258   }
259 
260   /**
261    * Add more labels if desired
262    *
263    * @param labels
264    */
moreLabels(Map<String, String> labels)265   protected void moreLabels(Map<String, String> labels) {
266     //labels.put("plugins", GT.$("&Plugins"));
267   }
268 
getLabel(String key)269   public String getLabel(String key) {
270     if (labels == null)
271       setupLabels();
272     String s = labels.get(key);
273     if (s == null || s.length() == 0) {
274       System.err.println("GUI key? " + key);
275       return key;
276     }
277     return s;
278   }
279 
newJMenu(String key)280   public JMenu newJMenu(String key) {
281     return new KeyJMenu(key, getLabel(key), map);
282   }
283 
newJMenuItem(String key)284   public JMenuItem newJMenuItem(String key) {
285     return new KeyJMenuItem(key, getLabel(key), map);
286   }
287 
newJCheckBoxMenuItem(String key, boolean isChecked)288   public JCheckBoxMenuItem newJCheckBoxMenuItem(String key, boolean isChecked) {
289     return new KeyJCheckBoxMenuItem(key, getLabel(key), map, isChecked);
290   }
291 
newJRadioButtonMenuItem(String key)292   public JRadioButtonMenuItem newJRadioButtonMenuItem(String key) {
293     return new KeyJRadioButtonMenuItem(key, getLabel(key), map);
294   }
295 
newJCheckBox(String key, boolean isChecked)296   public JCheckBox newJCheckBox(String key, boolean isChecked) {
297     return new KeyJCheckBox(key, getLabel(key), map, isChecked);
298   }
299 
newJButton(String key)300   public JButton newJButton(String key) {
301     JButton jb = new JmolButton(getLabel(key));
302     map.put(key, jb);
303     return jb;
304   }
305 
get(String key)306   public Object get(String key) {
307     return map.get(key);
308   }
309 
setSelected(String key, boolean b)310   public void setSelected(String key, boolean b) {
311     ((AbstractButton) get(key)).setSelected(b);
312   }
313 
setEnabled(String key, boolean b)314   public void setEnabled(String key, boolean b) {
315     ((AbstractButton) get(key)).setEnabled(b);
316   }
317 
updateLabels()318   public void updateLabels() {
319     boolean doTranslate = GT.setDoTranslate(true);
320     setupLabels();
321     GenericConsole.setAbstractButtonLabels(map, labels);
322     GT.setDoTranslate(doTranslate);
323   }
324 
translate(String str)325   public static String translate(String str) {
326     if (translations == null || !GT.getLanguage().equals(language))
327       setTranslations();
328     language = GT.getLanguage();
329     for (int i = 0; i < translations.length; i += 2) {
330       String t = translations[i];
331       if (str.indexOf(t) >= 0) {
332         String s = translations[i + 1];
333         if (s.equals("see Jmol-resources.properties"))
334           s = JmolResourceHandler.getStringX(t);
335         str = PT.rep(str, t, s);
336       }
337     }
338     return str;
339   }
340 
getResource(Object object, String fileName)341   public static URL getResource(Object object, String fileName) {
342     return getResource(object, fileName, true);
343   }
344 
getHtmlResource(Object object, String root)345   public static URL getHtmlResource(Object object, String root) {
346     String lang = GT.getLanguage();
347     String fileName = root + "_" + lang + ".html";
348     URL url = getResource(object, fileName, false);
349     if (url == null && lang.length() == 5) {
350       fileName = root + "_" + lang.substring(0, 2) + ".html";
351       url = getResource(object, fileName, false);
352     }
353     if (url == null) {
354       fileName = root + ".html";
355       url = getResource(object, fileName, true);
356     }
357     return url;
358   }
359 
360   /**
361    * @param object
362    *        UNUSED
363    * @param fileName
364    * @param flagError
365    * @return URL
366    */
getResource(Object object, String fileName, boolean flagError)367   public static URL getResource(Object object, String fileName,
368                                 boolean flagError) {
369     URL url = null;
370     if (fileName.indexOf("/org/") > 0)
371       fileName = fileName.substring(fileName.indexOf("/org/") + 1);
372     if (!fileName.contains("/"))
373       fileName = "org/openscience/jmol/app/webexport/html/" + fileName;
374     try {
375       if ((url = ClassLoader.getSystemResource(fileName)) == null && flagError)
376         System.err.println("Couldn't find file: " + fileName);
377     } catch (Exception e) {
378       System.err.println("Exception " + e.getMessage() + " in getResource "
379           + fileName);
380     }
381     return url;
382   }
383 
getResourceString(Object object, String name)384   public static String getResourceString(Object object, String name)
385       throws IOException {
386     URL url = (name.indexOf(".") >= 0 ? getResource(object, name)
387         : getHtmlResource(object, name));
388     if (url == null) {
389       throw new FileNotFoundException("Error loading resource " + name);
390     }
391     StringBuilder sb = new StringBuilder();
392     try {
393       //turns out from the Jar file
394       // it's a sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream
395       // and within Eclipse it's a BufferedInputStream
396       //LogPanel.log(name + " : " + url.getContent().toString());
397       BufferedReader br = new BufferedReader(new InputStreamReader(
398           (InputStream) url.getContent()));
399       String line;
400       while ((line = br.readLine()) != null)
401         sb.append(line).append("\n");
402       br.close();
403     } catch (Exception e) {
404       System.out.println(e.getMessage());
405     }
406     return translate(sb.toString());
407   }
408 
409   private static String[] translations;
410 
411   /**
412    * allows for web page material to be internationalized, inserting
413    * language-specific code, as for WebExport, or by inserting boiler-plate
414    * information, as for About_xx.html
415    *
416    */
setTranslations()417   private static void setTranslations() {
418     // for all templates and JmolPopIn.js
419     translations = new String[] {
420         "GT_JmolPopIn.js_TOGETA3DMODEL",
421         GT.escapeHTML(GT.o(
422             GT.$("To get a 3-D model you can manipulate, click {0}here{1}. Download time may be significant the first time the applet is loaded."),
423             new String[] { "<a href=\"HREF\">", "</a>" })),
424 
425         "GT_pop_in_template.html_INSERTTITLE",
426         GT.escapeHTML(GT.$("Insert the page TITLE here.")),
427         "GT_pop_in_template.html_INSERTINTRO",
428         GT.escapeHTML(GT.$("Insert the page INTRODUCTION here.")),
429 
430         "GT_pop_in_template2.html_INSERTCAPTION",
431         GT.escapeHTML(GT.o(
432             GT.$("CLICK IMAGE TO ACTIVATE 3D <br/> Insert a caption for {0} here."),
433             "@NAME@")),
434         "GT_pop_in_template2.html_INSERTADDITIONAL",
435         GT.escapeHTML(GT.o(
436             GT.$("Insert additional explanatory text here. Long text will wrap around Jmol model {0}."),
437             "@NAME@")),
438 
439         "GT_script_btn_template.html_INSERT",
440         GT.escapeHTML(GT.$("Insert your TITLE and INTRODUCTION here.")),
441         "GT_script_btn_template.html_LOADING",
442         GT.escapeHTML(GT.o(
443             GT.$("Once the molecule file is fully loaded, the image at right will become live.  At that time the \"activate 3-D\" icon {0} will disappear."),
444             new String[] { "<img id=\"make_live_icon\" src=\"\" height=\"15px\" />" })),
445         "GT_script_btn_template.html_VIEWAGAIN",
446         GT.escapeHTML(GT
447             .$("You may look at any of these intermediate views again by clicking on the appropriate button.")),
448         "GT_script_btn_template.html_JAVACAPABLE",
449         GT.escapeHTML(GT
450             .$("If your browser/OS combination is Java capable, you will get snappier performance if you <a href=\"?use=JAVA\">use Java</a>")),
451         "GT_script_btn_template2.html_BUTTONINFO",
452         GT.escapeHTML(GT.o(
453             GT.$("The button {0} will appear below.  Insert information for {0} here and below."),
454             "@NAME@")),
455         "GT_script_btn_template2.html_MORE",
456         GT.escapeHTML(GT.o(GT.$("Insert more information for {0} here."),
457             "@NAME@")), "About.html#version",
458         "<p><b>Jmol " + JC.version + " (" + JC.date + ")</b></p>",
459         "About.html#splash", "see Jmol-resources.properties",
460         "About.html#weblinks", "see Jmol-resources.properties",
461         "About.html#libraries", "see Jmol-resources.properties" };
462   }
463 
464 }
465