1 /* $RCSfile$
2  * $Author: hansonr $
3  * $Date: 2010-05-11 15:47:18 -0500 (Tue, 11 May 2010) $
4  * $Revision: 13064 $
5  *
6  * Copyright (C) 2000-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 jspecview.popup;
25 
26 import javajs.util.Lst;
27 import javajs.util.PT;
28 import jspecview.api.JSVPanel;
29 import jspecview.common.JSVersion;
30 import jspecview.common.JSViewer;
31 import jspecview.common.PanelData;
32 import jspecview.common.Spectrum;
33 
34 import org.jmol.api.SC;
35 import org.jmol.popup.GenericPopup;
36 import org.jmol.popup.PopupResource;
37 
38 abstract public class JSVGenericPopup extends GenericPopup {
39 
40   // list is saved in http://www.stolaf.edu/academics/chemapps/jmol/docs/misc
41   protected final static boolean dumpList = false;
42   protected final static int UPDATE_NEVER = -1;
43   private final static int UPDATE_ALL = 0;
44   private final static int UPDATE_CONFIG = 1;
45   private final static int UPDATE_SHOW = 2;
46 
47   protected JSViewer vwr;
48   protected int updateMode;
49 
50   // private int itemMax = 25;
51   // private int titleWidthMax = 20;
52 
53   private Lst<String> cnmrPeaks;
54   private Lst<String> hnmrPeaks;
55   private int aboutComputedMenuBaseCount;
56   private boolean allowMenu;
57   private boolean zoomEnabled;
58 
JSVGenericPopup()59   public JSVGenericPopup() {
60     // required by reflection
61   }
62 
initialize(JSViewer viewer, PopupResource bundle, String title)63   protected void initialize(JSViewer viewer, PopupResource bundle, String title) {
64     this.vwr = viewer;
65     initSwing(title, bundle, viewer.getApplet(), viewer.isJS, viewer.isSigned,
66         false);
67   }
68 
69   // //// JmolPopupInterface methods //////
70 
71   // private final static int MENUITEM_HEIGHT = 20;
72 
73   @Override
jpiDispose()74   public void jpiDispose() {
75     helper.menuClearListeners(popupMenu);
76     popupMenu = thisPopup = null;
77   }
78 
79   @Override
jpiGetMenuAsObject()80   public Object jpiGetMenuAsObject() {
81     return popupMenu;
82   }
83 
84   @Override
jpiShow(int x, int y)85   public void jpiShow(int x, int y) {
86     // main entry point from Viewer
87     // called via JmolPopupInterface
88     show(x, y, false);
89     appRestorePopupMenu();
90     menuShowPopup(popupMenu, thisx, thisy);
91   }
92 
93   @Override
jpiUpdateComputedMenus()94   public void jpiUpdateComputedMenus() {
95     if (updateMode == UPDATE_NEVER)
96       return;
97     updateMode = UPDATE_ALL;
98     getViewerData();
99     updateFileMenu();
100     updateFileTypeDependentMenus();
101     updateMode = UPDATE_CONFIG;
102     updateAboutSubmenu();
103   }
104 
105   // /////// protected methods //////////
106 
107   @Override
appCheckItem(String item, SC newMenu)108   protected void appCheckItem(String item, SC newMenu) {
109     // no special items in JSV menu
110   }
111 
112   @Override
appFixLabel(String label)113   protected String appFixLabel(String label) {
114     if (label.startsWith("_"))
115       label = label.substring(label.indexOf("_", 2) + 1);
116     else if (label.equals("VERSION"))
117       label = JSVersion.VERSION;
118     label = PT.rep(label, "JAVA", "");
119     label = PT.rep(label, "CB", "");
120     label = PT.rep(label, "Menu", "");
121     label = PT.rep(label, "_", " ");
122     return label;
123   }
124 
125   @Override
getScriptForCallback(SC source, String id, String script)126   protected String getScriptForCallback(SC source, String id, String script) {
127     return script;
128   }
129 
130   @Override
appGetMenuAsString(String title)131   public String appGetMenuAsString(String title) {
132     return (new JSVPopupResourceBundle()).getMenuAsText(title);
133   }
134 
135   @Override
appGetBooleanProperty(String name)136   protected boolean appGetBooleanProperty(String name) {
137     return false; // not implemented in JSV
138   }
139 
140   @Override
appRunSpecialCheckBox(SC item, String basename, String what, boolean TF)141   protected boolean appRunSpecialCheckBox(SC item, String basename,
142                                           String what, boolean TF) {
143     // n/a
144     return false;
145   }
146 
147   @Override
appRestorePopupMenu()148   protected void appRestorePopupMenu() {
149     thisPopup = popupMenu;
150   }
151 
152   @Override
appRunScript(String script)153   protected void appRunScript(String script) {
154     vwr.runScript(script);
155   }
156 
157   @Override
appUpdateForShow()158   protected void appUpdateForShow() {
159     thisJsvp = vwr.selectedPanel;
160     setEnables(thisJsvp);
161     if (updateMode == UPDATE_NEVER)
162       return;
163     getViewerData();
164     updateMode = UPDATE_SHOW;
165     updateSpectraMenu();
166     updateAboutSubmenu();
167   }
168 
169   @Override
appUpdateSpecialCheckBoxValue(SC item, String what, boolean TF)170   protected void appUpdateSpecialCheckBoxValue(SC item, String what, boolean TF) {
171     // not used
172   }
173 
174   // /////// private methods /////////
175 
getViewerData()176   private void getViewerData() {
177     // cnmrPeaks = (JmolList<String>) modelInfo.get("jdxAtomSelect_13CNMR");
178     // hnmrPeaks = (JmolList<String>) modelInfo.get("jdxAtomSelect_1HNMR");
179   }
180 
updateFileTypeDependentMenus()181   private void updateFileTypeDependentMenus() {
182     // not used
183   }
184 
updateFileMenu()185   private void updateFileMenu() {
186     Object menu = htMenus.get("fileMenu");
187     if (menu == null)
188       return;
189   }
190 
updateSpectraMenu()191   private void updateSpectraMenu() {
192     SC menuh = htMenus.get("hnmrMenu");
193     SC menuc = htMenus.get("cnmrMenu");
194     if (menuh != null)
195       menuRemoveAll(menuh, 0);
196     if (menuc != null)
197       menuRemoveAll(menuc, 0);
198     SC menu = htMenus.get("spectraMenu");
199     if (menu == null)
200       return;
201     menuRemoveAll(menu, 0);
202     // yes binary | not logical || here -- need to try to set both
203     boolean isOK = setSpectraMenu(menuh, hnmrPeaks)
204         | setSpectraMenu(menuc, cnmrPeaks);
205     if (isOK) {
206       if (menuh != null)
207         menuAddSubMenu(menu, menuh);
208       if (menuc != null)
209         menuAddSubMenu(menu, menuc);
210     }
211     menuEnable(menu, isOK);
212   }
213 
setSpectraMenu(SC menu, Lst<String> peaks)214   private boolean setSpectraMenu(SC menu, Lst<String> peaks) {
215     if (menu == null)
216       return false;
217     menuEnable(menu, false);
218     int n = (peaks == null ? 0 : peaks.size());
219     if (n == 0)
220       return false;
221     for (int i = 0; i < n; i++) {
222       String peak = peaks.get(i);
223       String title = PT.getQuotedAttribute(peak, "title");
224       String atoms = PT.getQuotedAttribute(peak, "atoms");
225       if (atoms != null)
226         menuCreateItem(menu, title,
227             "select visible & (@" + PT.rep(atoms, ",", " or @") + ")", "Focus"
228                 + i);
229     }
230     menuEnable(menu, true);
231     return true;
232   }
233 
updateAboutSubmenu()234   private void updateAboutSubmenu() {
235     SC menu = htMenus.get("aboutComputedMenu");
236     if (menu == null)
237       return;
238     menuRemoveAll(menu, aboutComputedMenuBaseCount);
239   }
240 
241 
setEnabled(boolean allowMenu, boolean zoomEnabled)242   public void setEnabled(boolean allowMenu, boolean zoomEnabled) {
243     this.allowMenu = allowMenu;
244     this.zoomEnabled = zoomEnabled;
245     enableMenus();
246   }
247 
enableMenus()248   private void enableMenus() {
249     // all except About and Zoom disabled
250     setItemEnabled("_SIGNED_FileMenu", allowMenu);
251     setItemEnabled("ViewMenu", pd != null && allowMenu);
252     setItemEnabled("Open_File...", allowMenu);
253     setItemEnabled("Open_Simulation...", allowMenu);
254     setItemEnabled("Open_URL...", allowMenu);
255     setItemEnabled("Save_AsMenu", pd != null && allowMenu);
256     setItemEnabled("Export_AsMenu", pd != null && allowMenu);
257     setItemEnabled("Append_File...", pd != null && allowMenu);
258     setItemEnabled("Append_Simulation...", pd != null && allowMenu);
259     setItemEnabled("Append_URL...", pd != null && allowMenu);
260     setItemEnabled("Views...", pd != null && allowMenu);
261     setItemEnabled("Script", allowMenu);
262     setItemEnabled("Print...", pd != null && allowMenu);
263     setItemEnabled("ZoomMenu", pd != null && zoomEnabled);
264   }
265 
266   private PanelData pd;
267   protected JSVPanel thisJsvp;
268 
setEnables(JSVPanel jsvp)269   private void setEnables(JSVPanel jsvp) {
270     pd = (jsvp == null ? null : jsvp.getPanelData());
271     Spectrum spec0 = (pd == null ? null : pd.getSpectrum());
272     boolean isOverlaid = pd != null && pd.isShowAllStacked();
273     boolean isSingle = pd != null && pd.haveSelectedSpectrum();
274 
275     setItemEnabled("Integration", pd != null && pd.getSpectrum().canIntegrate());
276     setItemEnabled("Measurements", true);
277     //pd != null && pd.hasCurrentMeasurements(AType.Measurements));
278     setItemEnabled("Peaks", pd != null && pd.getSpectrum().is1D());
279 
280     setItemEnabled("Predicted_Solution_Colour_(fitted)",
281         isSingle && spec0.canShowSolutionColor());
282     setItemEnabled("Predicted_Solution_Colour_(interpolated)", isSingle
283         && spec0.canShowSolutionColor());
284     setItemEnabled("Toggle_Trans/Abs", isSingle && spec0.canConvertTransAbs());
285     setItemEnabled("Show_Overlay_Key", isOverlaid
286         && pd.getNumberOfGraphSets() == 1);
287     setItemEnabled("Overlay_Offset...", isOverlaid);
288     setItemEnabled("JDXMenu", pd != null && spec0.canSaveAsJDX());
289     setItemEnabled("Export_AsMenu", pd != null);
290     enableMenus();
291   }
292 
setItemEnabled(String key, boolean TF)293   private void setItemEnabled(String key, boolean TF) {
294     menuEnable(htMenus.get(key), TF);
295   }
296 
setSelected(String key, boolean TF)297   public void setSelected(String key, boolean TF) {
298     SC item = htMenus.get(key);
299     if (item == null || item.isSelected() == TF)
300       return;
301     menuEnable(item, false);
302     item.setSelected(TF);
303     menuEnable(item, true);
304   }
305 
306 // not implemented -- see   ScriptToken.COMPOUNDMENUON()
307 //  /**
308 //   * @param panelNodes
309 //   * @param allowCompoundMenu
310 //   */
311 //  public void setCompoundMenu(Lst<PanelNode> panelNodes,
312 //                              boolean allowCompoundMenu) {
313 //    // TODO Auto-generated method stub
314 //
315 //  }
316 
317 
318   @Override
getUnknownCheckBoxScriptToRun(SC item, String name, String what, boolean TF)319   protected String getUnknownCheckBoxScriptToRun(SC item, String name,
320                                                  String what, boolean TF) {
321     // not used in JSV
322     return null;
323   }
324 
325 }
326