swing/JButton/buttonDemo/jython/ButtonDemo.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.13
 15: """
 16: ## ---------------------------------------- demo: jython
 17: """
 18: _jython=/Users/sketch/book/java_useBetter_1/_jython
 19: 
 20: cd /Users/sketch/book/java_useBetter_1/swing/JButton/buttonDemo/jython
 21: 
 22: JYTHONPATH=$_jython jython -i ButtonDemo.py
 23: 
 24: ============================================================
 25: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26) 
 26: Module : ButtonDemo.py
 27: Date   : Tue Sep 14 06:44:24 2010
 28: ============================================================
 29: """
 30: ## ----------------------------------------
 31: ##package components;
 32: 
 33: from java.awt.event import KeyEvent
 34: from javax.swing import AbstractButton
 35: from javax.swing import ImageIcon
 36: from javax.swing import JButton
 37: from javax.swing import JFrame
 38: from javax.swing import JPanel
 39: 
 40: from os.path import dirname
 41: 
 42: ## ----------------------------------------
 43: class ButtonDemo(JPanel):
 44: 
 45:     def __init__(self, master, *args, **keys):
 46:         master.contentPane = self
 47: 
 48:         path = "%s/_images"%dirname(argv[-1])
 49:         path = "_images"
 50: 
 51:         e = CommandButton(
 52:             "Disable middle button",
 53:             icon = self.createImageIcon("%s/right.gif"%path),
 54:             toolTipText = "Click this button to disable the middle button.",
 55:             verticalTextPosition = AbstractButton.CENTER,
 56:             horizontalTextPosition = AbstractButton.LEADING,
 57:             actionPerformed = self,
 58:             command = DisableCommand(self),
 59:             )
 60:         e.setMnemonic(KeyEvent.VK_D)
 61:         self.disableButton = e
 62: 
 63:         e = JButton(
 64:             "Middle button",
 65:             self.createImageIcon("%s/middle.gif"%path),
 66:             verticalTextPosition = AbstractButton.BOTTOM,
 67:             horizontalTextPosition = AbstractButton.CENTER,
 68:             toolTipText = "This middle button does nothing when you click it.",
 69:             )
 70:         e.setMnemonic(KeyEvent.VK_M)
 71:         self.middleButton = e
 72: 
 73:         #/Use the default text position of CENTER, TRAILING (RIGHT).
 74:         e = CommandButton(
 75:             "Enable middle button",
 76:             icon = self.createImageIcon("%s/left.gif"%path),
 77:             toolTipText = "Click this button to enable the middle button.",
 78:             enabled = False,
 79:             actionPerformed = self,
 80:             command = EnableCommand(self),
 81:             )
 82:         e.setMnemonic(KeyEvent.VK_E)
 83:         self.enableButton = e
 84: 
 85:         #/Add Components to this container, using the default FlowLayout.
 86:         for e in [
 87:             self.disableButton,
 88:             self.middleButton,
 89:             self.enableButton,
 90:         ]:
 91:             self.add(e)
 92: 
 93:     def __call__(self, e):
 94:         e.source.command()
 95: 
 96:     #/** Returns an ImageIcon, or null if the path was invalid. */
 97:     def createImageIcon(self, path):
 98:         return ImageIcon(path)
 99: 
100:     ## ----------------------------------------
101:     @staticmethod
102:     def createAndShowGUI():
103:         frame = JFrame(
104:             title = "ButtonDemo",
105:             defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
106:             locationRelativeTo = None,
107:             )
108: ##        newContentPane.setOpaque(true); //content panes must be opaque
109:         ButtonDemo(frame)
110:         frame.pack()
111:         frame.visible = True
112:         pass
113: 
114: ## ----------------------------------------
115: class CommandButton(JButton):               # Command::Invoker
116:     pass
117: 
118: ## ----------------------------------------
119: class Command:                              # Command::Command
120:     def __init__(self, client):
121:         self.client = client
122:         
123:     def __call__(self):
124:         raise NotImplementedError, self.__class__.__name__
125: 
126: class DisableCommand(Command):              # Command::ConcreteCommand
127:     def __call__(self):
128:         self.client.disableButton.enabled = False
129:         self.client.middleButton.enabled = False
130:         self.client.enableButton.enabled = True
131: 
132: class EnableCommand(Command):               # Command::ConcreteCommand
133:     def __call__(self):
134:         self.client.disableButton.enabled = True
135:         self.client.middleButton.enabled = True
136:         self.client.enableButton.enabled = False
137: 
138: ## ----------------------------------------
139: from __init__ import *
140: tips = ButtonDemo.createAndShowGUI
141: 
142: if __name__=='__main__':
143:     inform()
144:     testmod()
145: 
146: ## ========================================