# SettingsPanel.py : Panel containing fields and selectors for changing
#                    the microscope's state.        
#
# Distributed under the terms described in the LICENCE file.
#
# Developed by akuchlin@mems-exchange.org
#


__revision__ = "$Id: SettingsPanel.py,v 1.24 2002/01/15 17:39:37 akuchlin Exp $"

from java import awt
from javax import swing
from java.applet import Applet

import gui_util
import string

class SettingsPanel(swing.JPanel,
                    awt.event.AdjustmentListener,
                    awt.event.ItemListener,
                    awt.event.TextListener):
    def __init__ (self, client):
	swing.JPanel.__init__(self)

        self.client = client

	# Create subpanel for the coordinate display
	self.setLayout(awt.GridBagLayout())
	self.x_field = swing.JTextField(5)
	self.y_field = swing.JTextField(5)
	gui_util.constrain(self, swing.JLabel("X:"), 0,0)
	gui_util.constrain(self, self.x_field, 1,0)	
	gui_util.constrain(self, swing.JLabel("Y:"), 0,1)
	gui_util.constrain(self, self.y_field, 1,1)	

	self.mag_field = swing.JComboBox()
	for mag in ["5X", "10X", "20X", "50X", "100X"]:
	    self.mag_field.addItem(mag)

	self.mode_field = swing.JComboBox()
        for mode in ['Brightfield', 'Darkfield', 'ICR']:
	    self.mode_field.addItem(mode)

	self.aperture_field = swing.JComboBox()
        for aperture in ['1', '2', '3', '4', '5']:
	    self.aperture_field.addItem(aperture)

        self.light_field = swing.JScrollBar(swing.JScrollBar.HORIZONTAL,
                                            0, 1, 0, 100)
        gui_util.constrain(self, swing.JLabel('Magnification:'), 0,3, 1,1)
	gui_util.constrain(self, self.mag_field, 1,3, 2,1)
        gui_util.constrain(self, swing.JLabel('Mode:'), 0,4, 1,1)
        gui_util.constrain(self, self.mode_field, 1,4, 1,1)
        gui_util.constrain(self, swing.JLabel('Aperture:'), 0,5, 1,1)
        gui_util.constrain(self, self.aperture_field, 1,5, 1,1)
        self.light_label = swing.JLabel('Light: 000 %')
        gui_util.constrain(self, self.light_label, 0,6, 1,1)
        gui_util.constrain(self, self.light_field, 1,6, 1,1)
        button = swing.JButton("Send Microscope Settings")
        button.setActionCommand("Move")
        button.addActionListener(self.client)
	gui_util.constrain(self, button, 0,7, 2,1)

        # Add the SettingsPanel object as a listener to all events which
        # involve changing the field values.
#        self.x_field.addTextListener(self)
#        self.y_field.addTextListener(self)

        self.mag_field.addItemListener(self)
        self.mode_field.addItemListener(self)
        self.aperture_field.addItemListener(self)
        
        self.light_field.addAdjustmentListener(self)

    def textValueChanged (self, e):
        try: self.xpos = int(self.x_field.getText()) 
        except ValueError: pass
        try: self.ypos = int(self.y_field.getText()) 
        except ValueError: pass

    def itemStateChanged (self, e):
        mag_str = self.mag_field.getSelectedItem()
        self.mag = int(mag_str[:-1])
        self.mode = self.mode_field.getSelectedItem()
        aperture_str = self.aperture_field.getSelectedItem()
        self.aperture = int(aperture_str)
        
    def adjustmentValueChanged (self, e):
        # Copy all the current settings to attributes of this object.
        self.light = self.light_field.getValue()

        self.update_fields()
        
    def update_status (self):
        """Retrieve the microscope status attributes from the ImageDisplay
        object."""
        i = self.client.image_disp
        for attr in ['xpos', 'ypos', 'zpos', 'mag', 'mode',
                     'aperture', 'light',
                     ]:
            value = getattr(i, attr)
            setattr(self, attr, value)
        self.update_fields()
        
    def update_fields (self):
        """Set the fields in the SettingsPanel to their current values."""
        self.x_field.setText(str(self.xpos))
	self.y_field.setText(str(self.ypos))
        
        self.mag_field.setSelectedItem(str(self.mag)+'X')
        self.mode_field.setSelectedItem(str(self.mode))
        self.aperture_field.setSelectedItem(str(self.aperture))
	self.light_field.setValue(self.light)
        
	self.light_label.setText('Light: %3i %%' % self.light)
