swing/JComboBox/comboBoxDemo/jython/ComboBoxDemo.py

  1: #! /usr/bin/env python
  2: # coding: utf-8
  3: ## ----------------------------------------
  4: ##
  5: ## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
  6: ##
  7: ## ----------------------------------------
  8: #...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
  9: """
 10: >>> tips()
 11: 
 12: >>> ## ----------------------------------------
 13: >>> None
 14: version: #1.0.23
 15: """
 16: ## ---------------------------------------- demo: jython
 17: """
 18: _jython=/Users/sketch/book/java_useBetter_1/_jython
 19: 
 20: cd /Users/sketch/book/java_useBetter_1/swing/JComboBox/comboBoxDemo/jython/
 21: JYTHONPATH=$_jython jython -i ComboBoxDemo.py
 22: ============================================================
 23: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26) 
 24: Module : ComboBoxDemo.py
 25: Date   : Thu Oct  7 15:21:09 2010
 26: ============================================================
 27: """
 28: ## ----------------------------------------
 29: ##package components;
 30: 
 31: from java.awt import BorderLayout
 32: from java.awt import Font
 33: from java.awt.event import ActionListener
 34: from java.lang import System
 35: from javax.swing import BorderFactory
 36: from javax.swing import ImageIcon
 37: from javax.swing import JComboBox
 38: from javax.swing import JFrame
 39: from javax.swing import JLabel
 40: from javax.swing import JPanel
 41: 
 42: from os.path import exists
 43: 
 44: ## ----------------------------------------
 45: class ComboBoxDemo(JPanel, ActionListener):
 46:     
 47:     def __init__(self, master):
 48:         super(self.__class__,self).__init__(BorderLayout())
 49:         self.opaque = True
 50: 
 51:         petStrings = "Bird","Cat","Dog","Rabbit","Pig",
 52: 
 53:         ## ---------------------------------------- Command::Invoker
 54:         class CommandComboBox(JComboBox):
 55:             def __init__(self, listdata, command, **keys):
 56:                 super(self.__class__,self).__init__(listdata, **keys)
 57:                 self.command = command
 58:                 self.addActionListener(self)
 59:                 
 60:             def actionPerformed(self, e):
 61:                 self.command(e)
 62:                 
 63:         ## ---------------------------------------- Command::Command
 64:         #** Listens to the combo box. */
 65:         class Command:
 66:             def __init__(self, client):
 67:                 self.client = client
 68:                 
 69:             def __call__(self, e):
 70:                 cb = e.source
 71:                 petName = cb.selectedItem
 72:                 self.client.updateLabel(petName)
 73: 
 74:         #/Create the combo box, select the item at index 4.
 75:         #/Indices start at 0, so 4 specifies the pig.
 76:         self.petList = CommandComboBox(
 77:             listdata = petStrings,
 78:             selectedIndex = 4,
 79:             command = Command(self),
 80:             )
 81:         print "self.petList.list:: %s"%self.petList.list
 82: 
 83:         #/Set up the picture.
 84:         self.picture = JLabel(
 85:             horizontalAlignment = JLabel.CENTER,
 86:             border = BorderFactory.createEmptyBorder(10,0,0,0),
 87:             )
 88:         self.picture.font = self.picture.font.deriveFont(Font.ITALIC)
 89:         self.updateLabel(petStrings[self.petList.selectedIndex])
 90: 
 91:         #/The preferred size is hard-coded to be the width of the
 92:         #/widest image and the height of the tallest image + the border.
 93:         #/A real program would compute this.
 94:         self.picture.preferredSize = 177,122+10
 95:         
 96:         #/Lay out the demo.
 97:         self.add(self.petList, BorderLayout.PAGE_START)
 98:         self.add(self.picture, BorderLayout.PAGE_END)
 99:         self.border = BorderFactory.createEmptyBorder(20,20,20,20)
100:         
101:         #/Display the window.
102:         master.contentPane = self
103:         master.pack()
104:         
105:     def updateLabel(self, name):
106:         icon = self.createImageIcon("_images/%s.gif"%name)
107:         self.picture.icon = icon
108:         self.picture.toolTipText = "A drawing of a " + name.lower()
109:         self.picture.text = "Image not found" if icon == None else None
110:         
111:     #** Returns an ImageIcon, or null if the path was invalid. */
112:     @staticmethod
113:     def createImageIcon(path):
114:         imgURL = path if exists(path) else None
115:         if imgURL != None:
116:             return ImageIcon(imgURL)
117:         else:
118:             System.err.println("Couldn't find file: " + path)
119:             return None
120:         
121:     """
122:      * Create the GUI and show it.  For thread safety,
123:      * this method should be invoked from the
124:      * event dispatch thread.
125:     """
126:     @staticmethod
127:     def createAndShowGUI():
128:         System.out.println("Couldn't find file: @")
129:         
130:         #/Create and set up the window.
131:         frame = JFrame(
132:             title = "ComboBoxDemo",
133:             locationRelativeTo = None,
134:             defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
135:             )
136:         
137:         #/Create and set up the content pane.
138:         ComboBoxDemo(frame)
139:         
140:         ##Display the window.
141:         frame.visible = True
142:         
143: ## ----------------------------------------
144: from __init__ import *
145: tips = ComboBoxDemo.createAndShowGUI
146: 
147: if __name__=='__main__':
148:     inform()
149:     testmod()
150: 
151: ## ========================================