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.17a
15: """
16: ## ---------------------------------------- demo: jython
17: """
18: _jython=/Users/sketch/book/java_useBetter_1/_jython
19:
20: cd /Users/sketch/book/java_useBetter_1/gof/Visitor/visitor/jython/
21: JYTHONPATH=$_jython jython -i Visitor.py
22: ============================================================
23: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
24: Module : Visitor.py
25: Date : Tue Sep 28 04:17:46 2010
26: ============================================================
27: """
28: ## ----------------------------------------
29: from java.awt import *
30: from java.awt.event import *
31: from javax.swing import *
32: from javax.swing.event import *
33:
34: ## ---------------------------------------- Visitor::ObjectStructure
35: class VacationDisplay(ActionListener):
36:
37: def __init__(self, frame):
38: def listPane():
39: self.empList = \
40: pane = JawtList(10)
41: return pane
42:
43: def boxPane():
44: self.total = \
45: JTextField(
46: 5,
47: horizontalAlignment = JTextField.CENTER,
48: text = " ",
49: background = Color.white,
50: )
51: self.bonus_total = \
52: JTextField(
53: 5,
54: horizontalAlignment = JTextField.CENTER,
55: )
56: vacation = \
57: JButton(
58: "Vacations",
59: actionPerformed = self,
60: )
61:
62: pane = Box(BoxLayout.Y_AXIS)
63: for e in [
64: self.total,
65: Box.createVerticalStrut(10),
66: self.bonus_total,
67: Box.createVerticalStrut(10),
68: vacation,
69: Box.createVerticalStrut(50),
70: ]:
71: pane.add(e)
72: return pane
73:
74: def controlPane():
75: pane = JPanel(
76: layout = GridLayout(1,2),
77: )
78: pane.add(listPane())
79: pane.add(boxPane())
80: return pane
81:
82: frame.contentPane.add(controlPane())
83: self.createEmployees()
84:
85: ## ----------------------------------------
86: def createEmployees(self):
87: self.employees = [
88: Employee("Susan Bear", 55000, 12, 1),
89: Employee("Adam Gehr", 150000, 9, 0),
90: Employee("Fred Harris", 50000, 15, 2),
91: Employee("David Oakley", 57000, 12, 2),
92: Employee("Larry Thomas", 100000, 20, 6),
93: ]
94: self.employees.append(Boss(
95: "Leslie Susi", 175000, 16, 4,
96: bonusDays = 12,
97: ))
98: self.employees.append(Boss(
99: "Laurence Byerly", 35000, 17, 6,
100: bonusDays = 17,
101: ))
102: for e in self.employees:
103: self.empList.add_(e.name)
104:
105: ## ----------------------------------------
106: def __call__(self, e): # actionPerformed
107: vac = VacationVisitor()
108: bvac = BVacationVisitor()
109: for e in self.employees:
110: e.accept(vac)
111: e.accept(bvac)
112: self.total.text = str(vac. total_days)
113: self.bonus_total.text = str(bvac.total_days)
114:
115: ## ----------------------------------------
116: @staticmethod
117: def open():
118: frame = JFrame(
119: title = "Vacation Display",
120: size = (300, 200),
121: locationRelativeTo = None,
122: defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
123: )
124: comp = VacationDisplay(frame)
125: frame.visible = True
126:
127: ## ----------------------------------------
128: class JawtList(JScrollPane, ListSelectionListener):
129: """
130: ; this is a simple adapter class to
131: ; convert List awt methods to Swing methods
132: """
133: def __init__(self, rows): # rows ?
134: class JListData(AbstractListModel):
135: def __init__(self):
136: self.data = []
137: def __getitem__(self,index):
138: return self.data[index]
139: def getSize(self):
140: return len(self.data)
141: def getElementAt(self,index):
142: return self[index]
143: def addElement(self,s):
144: self.data.append(s)
145: self.fireIntervalAdded(self, len(self.data)-1, len(self.data))
146:
147: self.listContents = JListData()
148: self.listWindow = JList(self.listContents)
149: self.viewport.add(self.listWindow)
150:
151: def add_(self, s):
152: self.listContents.addElement(s)
153: self.validate()
154:
155: ## ---------------------------------------- Visitor::Element
156: class Element(object):
157: def accept(self, v):
158: getattr(v, "visit%s"%self.__class__.__name__)(self)
159:
160: ## ---------------------------------------- Visitor::ConcreteElement
161: class Employee(Element):
162: def __init__(self, name, salary, vacdays, sickdays):
163: self.name = name
164: self.salary = salary
165: self.vacDays = vacdays
166: self.sickDays = sickdays
167:
168: ## ---------------------------------------- Visitor::ConcreteElement
169: class Boss(Employee):
170: def __init__(self, name, salary, vacdays, sickdays, bonusDays):
171: super(self.__class__,self).__init__(
172: name, salary, vacdays, sickdays)
173: self.bonusDays = bonusDays
174:
175: ## ---------------------------------------- Visitor::Visitor
176: class Visitor:
177: def visitEmployee(self, emp):
178: raise NotImplementedError
179: def visitBoss(self, emp):
180: raise NotImplementedError
181:
182: ## ---------------------------------------- Visitor::ConcreteVisitor
183: class VacationVisitor(Visitor):
184: def __init__(self):
185: self.total_days = 0
186:
187: def visitEmployee(self, emp):
188: self.total_days += emp.vacDays
189:
190: def visitBoss(self, boss):
191: self.total_days += boss.vacDays
192:
193: ## ---------------------------------------- Visitor::ConcreteVisitor
194: class BVacationVisitor(Visitor):
195: def __init__(self):
196: self.total_days = 0
197:
198: def visitEmployee(self, emp):
199: self.total_days += emp.vacDays
200:
201: def visitBoss(self, boss):
202: self.total_days += boss.vacDays
203: self.total_days += boss.bonusDays
204:
205: ## ----------------------------------------
206: from __init__ import *
207: tips = VacationDisplay.open
208:
209: if __name__=='__main__':
210: inform()
211: testmod()
212:
213: ## ========================================