gof/Command/fullCommand/jython/FullCommand.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.22
 15: """
 16: ## ---------------------------------------- demo: jython
 17: """
 18: _jython=/Users/sketch/book/java_useBetter_1/_jython
 19: 
 20: cd /Users/sketch/book/java_useBetter_1/gof/Command/fullCommand/jython/
 21: JYTHONPATH=$_jython jython -i FullCommand.py
 22: ============================================================
 23: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26) 
 24: Module : ButtonDemo.py
 25: Date   : Tue Sep 14 06:44:24 2010
 26: ============================================================
 27: """
 28: ## ----------------------------------------
 29: from java.awt import Color
 30: from java.awt import FileDialog
 31: from java.awt.event import ActionListener
 32: from java.lang import System
 33: from javax.swing import JButton
 34: from javax.swing import JFrame
 35: from javax.swing import JMenu
 36: from javax.swing import JMenuBar
 37: from javax.swing import JMenuItem
 38: from javax.swing import JPanel
 39: 
 40: ## ---------------------------------------- Command::Client
 41: class FullCommand(JPanel, ActionListener):
 42: 
 43:     def __init__(self, frame):
 44: 
 45:         def createMenuBar():
 46:             menuBar = JMenuBar()
 47:             
 48:             menu = JMenu("File")
 49:             menuBar.add(menu)
 50:             
 51:             menu.add(CmdMenu(
 52:                 text = "Open...",
 53:                 actionPerformed = self,
 54:                 command = FileCommand(frame),
 55:                 ))            
 56:             menu.add(CmdMenu(
 57:                 text = "Exit",
 58:                 actionPerformed = self,
 59:                 command = ExitCommand(),
 60:                 ))
 61: 
 62:             return menuBar
 63:         
 64:         self.add(CmdButton(
 65:             text = "Red",
 66:             actionPerformed = self,
 67:             command = RedCommand(self),
 68:             ))
 69:         
 70:         frame.JMenuBar = createMenuBar()
 71:     
 72:     def __call__(self, e):   # actionPerformed
 73:         e.source.command()
 74: 
 75:     ## ----------------------------------------
 76:     @staticmethod
 77:     def open():
 78:         frame = JFrame(
 79:             title = "Frame with external commands",
 80:             bounds = (100,100,200,100),
 81:             locationRelativeTo = None,
 82:             defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
 83:             )
 84:         comp = FullCommand(frame)
 85:         frame.contentPane.add(comp)
 86:         frame.visible = True
 87: 
 88: ## ---------------------------------------- Command::Invoker
 89: class CmdButton(JButton): pass
 90: 
 91: class CmdMenu(JMenuItem): pass
 92: 
 93: ## ---------------------------------------- Command::Command
 94: class Command:
 95:     def __call__(self):
 96:         raise NotImplementedError, self.__class__.__name__
 97: 
 98: ## ---------------------------------------- Command::ConcreteCommand
 99: ## ---------------------------------------- Command::Receiver
100: class RedCommand(Command):
101:     def __init__(self, panel):
102:         self.panel = panel
103:     def __call__(self):
104:         print "::",self.__class__.__name__
105:         self.panel.background = Color.red
106: 
107: class FileCommand(Command):
108:     def __init__(self, frame):
109:         self.frame = frame
110:     def __call__(self):
111:         print "::",self.__class__.__name__
112:         dialog = FileDialog(self.frame, "Open file")
113:         dialog.visible = True
114: 
115: class ExitCommand(Command):
116:     def __call__(self):
117:         print "::",self.__class__.__name__
118:         exit()
119: 
120: ## ----------------------------------------
121: from __init__ import *
122: tips = FullCommand.open
123: 
124: if __name__=='__main__':
125:     inform()
126:     testmod()
127: 
128: ## ========================================