Created
March 22, 2012 12:54
-
-
Save ssorgatem/2158139 to your computer and use it in GitHub Desktop.
Intercepting Jmol console commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment