1 /* $RCSfile$
2  * $Author jonathan gutow$
3  * $Date Aug 5, 2007 9:19:06 AM $
4  * $Revision$
5  * Updated Dec. 2015 by Angel Herraez
6  * valid for JSmol
7  *
8  * Copyright (C) 2005-2016  The Jmol Development Team
9  *
10  * Contact: jmol-developers@lists.sf.net
11  *
12  *  This library is free software; you can redistribute it and/or
13  *  modify it under the terms of the GNU Lesser General Public
14  *  License as published by the Free Software Foundation; either
15  *  version 2.1 of the License, or (at your option) any later version.
16  *
17  *  This library is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  Lesser General Public License for more details.
21  *
22  *  You should have received a copy of the GNU Lesser General Public
23  *  License along with this library; if not, write to the Free Software
24  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25  *  02110-1301, USA.
26  */
27 package org.openscience.jmol.app.webexport;
28 
29 import javajs.util.PT;
30 
31 import javax.swing.JFileChooser;
32 import javax.swing.JLabel;
33 import javax.swing.JPanel;
34 import javax.swing.JSpinner;
35 import javax.swing.SpinnerNumberModel;
36 
37 import org.jmol.i18n.GT;
38 import org.jmol.viewer.Viewer;
39 
40 class ScriptButtons extends WebPanel {
41 
ScriptButtons(Viewer vwr, JFileChooser fc, WebPanel[] webPanels, int panelIndex)42   ScriptButtons(Viewer vwr, JFileChooser fc, WebPanel[] webPanels,
43       int panelIndex) {
44     super(vwr, fc, webPanels, panelIndex);
45     panelName = "script_btn";
46     listLabel = GT.$("These names will be used for button labels");
47     //description = "Create a web page containing a text and button pane that scrolls next to a resizable Jmol applet";
48   }
49 
50   @Override
appletParamPanel()51   JPanel appletParamPanel() {
52     SpinnerNumberModel appletSizeModel = new SpinnerNumberModel(
53         WebExport.getScriptButtonPercent(), //initial value
54         20, //min
55         100, //max
56         5); //step size
57     appletSizeSpinnerP = new JSpinner(appletSizeModel);
58     //panel to hold spinner and label
59     JPanel appletSizePPanel = new JPanel();
60     appletSizePPanel.add(new JLabel(GT.$("% of window for JSmol width:")));
61     appletSizePPanel.add(appletSizeSpinnerP);
62     return (appletSizePPanel);
63   }
64 
65   @Override
fixHtml(String html)66   String fixHtml(String html) {
67     int size = ((SpinnerNumberModel) (appletSizeSpinnerP.getModel()))
68         .getNumber().intValue();
69     int appletheightpercent = 100;
70     int nbuttons = getInstanceList().getModel().getSize();
71     if (!allSelectedWidgets().isEmpty())
72       appletheightpercent = 85;
73     html = PT.rep(html, "@WIDTHPERCENT@", "" + size);
74     html = PT.rep(html, "@LEFTPERCENT@", "" + (100 - size));
75     html = PT.rep(html, "@NBUTTONS@", "" + nbuttons);
76     html = PT.rep(html, "@HEIGHT@", "" + appletheightpercent);
77     html = PT.rep(html, "@BOTTOMPERCENT@", "" + (100 - appletheightpercent));
78     return html;
79   }
80 
81   @Override
getAppletDefs(int i, String html, StringBuilder appletDefs, JmolInstance instance)82   String getAppletDefs(int i, String html, StringBuilder appletDefs,
83                        JmolInstance instance) {
84     /* The widgets are built as hidden divs in the scrolling region,
85       upon page load their html is copied into variables,
86       and then when a button is pressed its set of widgets is read from the variable
87       and filled into the common widget area below the JSmol panel.
88       Note: widget code must be removed from the original place to avoid duplicate IDs.
89     */
90     String name = instance.name;
91     String buttonname = instance.javaname;
92     String widgetDefs = "";
93     int row = 0;
94     if (!instance.whichWidgets.isEmpty()) {
95       if (instance.whichWidgets.get(3)) {
96         //special:  widgetList[3] is AnimationWidget, taller than the others, put it to the right
97         widgetDefs += "<div class=\"widgetItemAnim\"> "
98             + theWidgets.widgetList[3].getJavaScript(0, instance) + "</div>";
99       }
100       widgetDefs += "<table><tbody><tr>";
101       for (int j = 0; j < nWidgets; j++) {
102         if (j == 3) {
103           continue;
104         }
105         if (instance.whichWidgets.get(j)) {
106           if (row == 3) {
107             widgetDefs += "</tr><tr>";
108             row = 0;
109           }
110           widgetDefs += "<td class=\"widgetItemScBtn\">"
111               + theWidgets.widgetList[j].getJavaScript(0, instance)
112               //does nothing? .replace("'", "\'")
113               + "</td>";
114           row = row + 1;
115         }
116       }
117       widgetDefs += "</tr></tbody></table>";
118     }
119     if (i == 0) {
120       html = PT.rep(html, "@APPLETNAME0@", GT.escapeHTML(buttonname));
121     }
122     String s = htmlAppletTemplate;
123     s = PT.rep(s, "@APPLETNAME0@", GT.escapeHTML(buttonname));
124     s = PT.rep(s, "@NAME@", "&#x201C;" + GT.escapeHTML(name) + "&#x201D;");
125     s = PT.rep(s, "@LABEL@", GT.escapeHTML(name));
126     s = PT.rep(s, "@I@", "" + i);
127     s = PT.rep(s, "@WIDGETSTR@", widgetDefs);
128     appletDefs.append(s);
129     return html;
130   }
131 }
132