public
Last active

Intercepting Jmol console commands

  • Download Gist
customcommand.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#-*- coding:utf-8 -*-
#
# Copyright 2012 AdriƠ Cereto MassaguƩ <adrian.cereto@urv.cat>
# This is the jython version of Jmol's Integration.java.
# It is under public domain
"""
An example of integrating the Jmol viewer into a jython application, with optional console.
"""
from java.lang.System import exit
 
from java.awt import BorderLayout , Container, Dimension, Graphics
from java.awt.event import WindowAdapter
 
from javax.swing import JFrame, JPanel
 
from org.jmol.adapter.smarter import SmarterJmolAdapter
from org.jmol.api import JmolViewer
from org.jmol.util import Logger
from org.openscience.jmol.app.jmolpanel import AppConsole
 
strScript = "delay; move 360 0 0 0 0 0 0 0 4;"
 
def main():
"""
Demonstrates a simple way to include an optional console along with the applet.
"""
frame = JFrame("Hello", size = (410, 700), defaultCloseOperation = JFrame.EXIT_ON_CLOSE)
contentPane = frame.getContentPane()
jmolPanel = JmolPanel(preferredSize = Dimension(400, 400))
# main panel -- Jmol panel on top
panel = JPanel(layout = BorderLayout())
panel.add(jmolPanel)
# main panel -- console panel on bottom
panel2 = JPanel(layout = BorderLayout(), preferredSize = Dimension(400, 300))
console = Console(jmolPanel.viewer, panel2,"History State Clear")
# You can use a different JmolStatusListener or JmolCallbackListener interface
# if you want to, but AppConsole itself should take care of any console-related callbacks
jmolPanel.viewer.jmolCallbackListener = console
panel.add("South", panel2)
contentPane.add(panel)
jmolPanel.viewer.evalString("Function myCommand () {}")
jmolPanel.viewer.evalString("set syncscript on")
frame.visible = True
# sample start-up script
strError = jmolPanel.viewer.openFile("http://chemapps.stolaf.edu/jmol/docs/examples-11/data/caffeine.xyz")
if strError == None:
jmolPanel.viewer.evalString(strScript)
else:
Logger.error(strError)
 
class Console(AppConsole):
def notifyEnabled(self, callbacktype):
print callbacktype
if str(callbacktype) == 'SYNC':
return True
return AppConsole.notifyEnabled(self, callbacktype)
 
def notifyCallback(self, callbacktype, data):
print 'Callback is ' + str(callbacktype)
print 'data is ' + str(data)
if str(callbacktype) == 'SYNC':
print 'Script is:'
print data[1]
if len(data[1].split('##')) >=3:
command = data[1].split('##')[0]
if 'mycommand' in command.lower():
self.sendConsoleEcho("mycommand has been executed!")
else:
AppConsole.notifyCallback(self, callbacktype, data)
 
class JmolPanel(JPanel):
def __init__(self, preferredSize = None):
self.preferredSize = preferredSize
self.currentSize = Dimension()
self.viewer = JmolViewer.allocateViewer(self, SmarterJmolAdapter(), None, None, None, None, None)
 
def paint(self, g):
self.getSize(self.currentSize)
self.viewer.renderScreenImage(g, self.currentSize.width, self.currentSize.height)
 
if __name__ == "__main__":
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.