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.19
15: """
16: ## ---------------------------------------- demo: jython
17: """
18: _jython=/Users/sketch/book/java_useBetter_1/_jython
19:
20: cd /Users/sketch/book/java_useBetter_1/gof/Observer/observer/jython
21: JYTHONPATH=$_jython jython -i Observer.py
22: ============================================================
23: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
24: Module : Observer.py
25: Date : Tue Oct 12 12:42:45 2010
26: ============================================================
27: """
28: ## ----------------------------------------
29: from java.awt import BorderLayout
30: from java.awt import Color
31: from java.awt import Font
32: from java.awt.event import ActionListener
33: from java.awt.event import ItemEvent
34: from java.awt.event import ItemListener
35: from java.lang import System
36: from javax.swing import AbstractListModel
37: from javax.swing import Box
38: from javax.swing import BoxLayout
39: from javax.swing import ButtonGroup
40: from javax.swing import JButton
41: from javax.swing import JFrame
42: from javax.swing import JList
43: from javax.swing import JPanel
44: from javax.swing import JRadioButton
45: from javax.swing import JScrollPane
46:
47: ## ----------------------------------------
48: class Subject(object):
49: def registerInterest(self, obs): pass
50:
51: ## ----------------------------------------
52: class Watch2Windows(ActionListener, ItemListener, Subject):
53:
54: def __init__(self, frame):
55: self.observers = #//list of observing frames
56:
57: p = JPanel(
58: True,
59: layout = BorderLayout(),
60: )
61: frame.contentPane.add("Center", p)
62:
63: box = Box(BoxLayout.Y_AXIS) #//vertical box layout
64: p.add("Center", box)
65:
66: #/make all part of same button group
67: bgr = ButtonGroup()
68: for e in "Red","Green","Blue":
69: button = JRadioButton(
70: label = e,
71: itemStateChanged = ColorCommand(self),
72: )
73: setattr(self, e.lower(), button)
74: box.add(button)
75: bgr.add(button)
76:
77: #/put a Close button at the bottom of the frame
78: p1 = JPanel()
79: p.add("South", p1)
80: self.close = JButton(
81: label = "Close",
82: actionPerformed = CloseCommand(self),
83: )
84: p1.add(self.close)
85: frame.pack()
86:
87: #/---------create observers---------
88: ColorFrame(self)
89: ListFrame(self)
90:
91: def notifyObservers(self, rad):
92: #/sends text of selected button to all observers
93: color = rad.text
94: for e in self.observers:
95: e.sendNotify(color)
96:
97: def registerInterest(self, obs):
98: #/adds observer to list
99: self.observers.append(obs)
100:
101: ## ---------------------------------------- Command::ConcreteCommand
102: ## ---------------------------------------- Command::Receiver
103: class ColorCommand:
104: def __init__(self, client):
105: self.client = client
106: def __call__(self, e): #itemStateChanged
107: #/responds to radio button clicks
108: #/if the button is selected
109: if e.stateChange == ItemEvent.SELECTED:
110: self.client.notifyObservers(e.source)
111:
112: ## ---------------------------------------- Command::Receiver
113: class CloseCommand:
114: def __init__(self, client):
115: self.client = client
116: def __call__(self, e): #actionPerformed
117: #/responds to close button
118: obj = e.source
119: if obj == self.client.close:
120: System.exit(0)
121:
122: ## ----------------------------------------
123: class Observer(object):
124: ##/** notify observers a change
125: ##has taken place*/
126: def sendNotify(self, s): pass
127:
128: ## ----------------------------------------
129: class ColorFrame(JFrame, Observer):
130: color_name = "black"
131: font_ = None
132:
133: def __init__(self, s):
134: super(self.__class__,self).__init__(
135: title = "Colors",
136: bounds = (100, 100, 100, 100),
137: )
138: p = JPanel(True)
139: self.contentPane.add("Center", p)
140:
141: s.registerInterest(self)
142: self.font_ = Font("Sans Serif", Font.BOLD, 14)
143:
144: self.visible = True
145:
146: def sendNotify(self, s):
147: self.color_name = s
148: color = getattr(Color, s.lower())
149: self.background = color
150:
151: def paint(self, g):
152: g.font = self.font_
153: g.drawString(self.color_name, 20, 50)
154:
155: ## ----------------------------------------
156: class ListFrame(JFrame, Observer):
157:
158: def __init__(self, s):
159: super(self.__class__,self).__init__(
160: title = "Color List",
161: bounds = (250, 100, 100, 100),
162: )
163:
164: #/put panel into the frmae
165: p = JPanel(
166: True,
167: layout = BorderLayout(),
168: )
169: self.contentPane.add("Center", p)
170:
171: #/Tell the Subject we are interested
172: s.registerInterest(self)
173:
174: #/Create the list
175: self.listData = JListData(); #//the list model
176: list_ = JList(self.listData); #//the visual list
177: lsp = JScrollPane( #//the scroller
178: preferredSize = (100, 100),
179: )
180: lsp.viewport.add(list_)
181: p.add("Center", lsp)
182:
183: self.visible = True
184:
185: def sendNotify(self, s):
186: self.listData.addElement(s)
187:
188: ## ----------------------------------------
189: class JListData(AbstractListModel):
190:
191: def __init__(self):
192: self.data =
193:
194: def getSize(self):
195: return len(self.data)
196:
197: def getElementAt(self, index):
198: return self.data[index]
199:
200: def addElement(self, s):
201: self.data.append(s)
202: self.fireIntervalAdded(self, len(self.data)-1, len(self.data))
203:
204: ## ----------------------------------------
205: class Tips:
206: @staticmethod
207: def open():
208: frame = JFrame(
209: title = "Frame with external commands",
210: bounds = (200,200,200,200),
211: ## locationRelativeTo = None,
212: defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
213: )
214: Watch2Windows(frame)
215: frame.visible = True
216:
217: ## ----------------------------------------
218: from __init__ import *
219: tips = Tips.open
220:
221: if __name__=='__main__':
222: inform()
223: testmod()
224:
225: ## ========================================