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.69
15: """
16: ## ---------------------------------------- bash
17: """
18: stty erase ^H
19: PS1="$ "
20: alias jython="/Users/sketch/home_Jython/_release/jython2.5.0/bin/jython"
21: """
22: ## ---------------------------------------- Java/Jython
23: """
24: alias java="/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java"
25: alias javac="/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/javac"
26:
27: JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home"
28: JYTHON_HOME_FALLBACK="/Users/sketch/jython2.5b3"
29: """
30: ## ---------------------------------------- JFC/Swing
31: """
32: http://java.sun.com/docs/books/tutorial/uiswing/examples/events/BeeperProject/src/events/Beeper.java
33: """
34: ## ---------------------------------------- demo: java
35: """
36: $ cd ~/download/demo_Jython/java.sun.com/docs-books-tutorial-uiswing-example/
37: $ cd events-BeeperProject/src/
38: $
39: $ javac events/Beeper.java
40: $ ls -l events/
41: total 24
42: -rw-r--r-- 1 sketch staff 414 3 12 01:09 Beeper$1.class
43: -rw-r--r-- 1 sketch staff 1589 3 12 01:09 Beeper.class
44: -rw-r--r--@ 1 sketch staff 3357 1 28 19:00 Beeper.java
45: $ java events/Beeper
46: $
47: """
48: ## ---------------------------------------- demo: jython
49: """
50: $ cd ~/PyTour/Jython/color_chart/
51: $ jython -i chart_swing.py
52:
53: """
54: ## ----------------------------------------
55: from javax.swing import JFrame
56:
57: def tips():
58: frame = JFrame(
59: title = "component: javax.swing",
60: size = (300,210),
61: )
62: TIPS(master=frame)
63:
64: frame.visible = True
65:
66: ## ----------------------------------------
67: from java.awt import BorderLayout
68: from java.awt import Color
69: from javax.swing import JPanel
70: from javax.swing import JTabbedPane
71:
72: class TIPS():
73: def __init__(self, master):
74: for name, layout in {
75: "control": "CENTER",
76: "canvas" : "WEST" ,
77: }.items():
78: comp = getattr(self, "create_%s"%name)(
79: master = master,
80: layout = getattr(BorderLayout, layout),
81: )
82: setattr(self, name, comp)
83:
84: ## ----------------------------------------
85: def create_control(self, master, layout):
86:
87: def stateChanged(event):
88: self.tab = event.source.selectedComponent
89:
90: pane = JTabbedPane(
91: stateChanged = stateChanged,
92: )
93: master.add(pane, layout)
94:
95: items = (
96: "JButton",
97: "JList",
98: "JSlider",
99: "JTextArea",
100: "JRadioButton",
101: "JCheckBox",
102: "JTextField",
103: "JComboBox",
104: "JSpinner",
105: )
106: items = sorted(items)
107: tabs = [eval("Tab_%s(Command(self))"%e) for e in items]
108:
109: self.pairs = {}
110: for title, tab in zip(items, tabs):
111: container = tab.addIn(title, pane)
112: self.pairs[container] = tab
113: return
114:
115: def create_canvas(self, master, layout):
116: comp = JPanel(
117: background = Color.white,
118: preferredSize = (100,100),
119: )
120: master.add(comp, layout)
121: return comp
122:
123: ## ----------------------------------------
124: def update(self, source=None, name=None, value=None):
125: if value:
126: self.canvas.background = value
127: return
128: container = self.pairs[self.tab]
129: if not name:
130: name = getattr(source, container.selector)
131: self.canvas.background = getattr(container.species, name)
132:
133: ## ----------------------------------------
134: class Command(object):
135: def __init__(self, client):
136: self.client = client
137:
138: def __call__(self, event):
139: self.client.update(event.source)
140:
141: ## ----------------------------------------
142: from pawt import colors
143:
144: class TabbedComponent(object):
145: selector = None
146: items = "red", "green", "blue", "cyan", "magenta", "yellow",
147:
148: def addIn(self, title, tabbedPane):
149: tabbedPane.addTab(title, self.container)
150: return self.container
151:
152: ## ---------------------------------------- JButton
153: from javax.swing import JButton
154:
155: class Tab_JButton(TabbedComponent):
156: species = Color
157: selector = "text"
158:
159: def __init__(self, command):
160: self.container = self.create(command)
161:
162: def create(self, command):
163: container = JPanel()
164:
165: for e in self.items:
166: comp = JButton(
167: text = e,
168: actionPerformed = command,
169: )
170: container.add(comp)
171: return container
172:
173: ## ---------------------------------------- JCheckBox
174: from javax.swing import JCheckBox
175:
176: class Tab_JCheckBox(TabbedComponent):
177: species = Color
178: selector = None
179: items = "red", "green", "blue",
180: seq = "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white",
181:
182: def __init__(self, command):
183: self.container = self.create(command)
184: self.command = command
185:
186: def create(self, command):
187: container = JPanel()
188:
189: for i,e in enumerate(self.items):
190: comp = JCheckBox(
191: text = e,
192: actionCommand = str(2**i),
193: actionPerformed = self,
194: )
195: container.add(comp)
196: return container
197:
198: def __call__(self, event):
199: index = sum(int(e.actionCommand)
200: for e in self.container.components if e.selected)
201: self.command.client.update(name=self.seq[index])
202:
203: ## ---------------------------------------- JComboBox
204: from javax.swing import DefaultComboBoxModel
205: from javax.swing import JComboBox
206: from java.awt.event import ActionListener
207:
208: class Tab_JComboBox(TabbedComponent, ActionListener):
209: species = colors
210: selector = "selectedItem"
211:
212: def __init__(self, command):
213: self.model = self.create_Model()
214: self.container = self.create(command)
215: self.command = command
216:
217: def create_Model(self):
218: model = DefaultComboBoxModel()
219:
220: items = [e for e in dir(colors) if not e.startswith("_")]
221: for e in items:
222: model.addElement(e)
223: return model
224:
225: def create(self, command):
226: container = JPanel()
227: comp = JComboBox(
228: model = self.model,
229: #### actionPerformed = command, #addActionListener
230: )
231: comp.addActionListener(self)
232: container.add(comp)
233: return container
234:
235: #### def __call__(self, event):
236: def actionPerformed(self, event):
237: name = getattr(event.source, self.selector)
238: self.command.client.update(name=name)
239:
240: ## ---------------------------------------- JList
241: from javax.swing import DefaultListModel
242: from javax.swing import JList
243: from javax.swing import JScrollPane
244:
245: class Tab_JList(TabbedComponent):
246: species = colors
247: selector = "selectedValue"
248:
249: def __init__(self, command):
250: self.model = self.create_Model()
251: self.container = self.create(command)
252:
253: def create_Model(self):
254: model = DefaultListModel()
255:
256: items = [e for e in dir(colors) if not e.startswith("_")]
257: for e in items:
258: model.addElement(e)
259: return model
260:
261: def create(self, command):
262: comp = JList(
263: model = self.model,
264: valueChanged = command,
265: )
266: container = JScrollPane(comp)
267: return container
268:
269: ## ---------------------------------------- JRadioButton
270: from javax.swing import JRadioButton
271: from javax.swing import ButtonGroup
272:
273: class Tab_JRadioButton(TabbedComponent):
274: species = Color
275: selector = "actionCommand"
276:
277: def __init__(self, command):
278: self.container = self.create(command)
279:
280: def create(self, command):
281: container = JPanel()
282: group = ButtonGroup()
283:
284: for e in self.items:
285: comp = JRadioButton(
286: text = e,
287: actionCommand = e,
288: actionPerformed = command,
289: )
290: container.add(comp)
291: group.add(comp)
292: return container
293:
294: ## ---------------------------------------- JSlider
295: from javax.swing import JSlider
296:
297: class Tab_JSlider(TabbedComponent):
298: species = Color
299: selector = "text"
300: items = "red", "green", "blue",
301:
302: def __init__(self, command):
303: self.container = self.create(command)
304: self.command = command
305:
306: def create(self, command):
307: container = JPanel()
308:
309: for e in self.items:
310: comp = JSlider(
311: maximum = 255,
312: minimum = 0,
313: paintTicks = True,
314: majorTickSpacing = 32,
315: minorTickSpacing = 8,
316: stateChanged = self, #addChangeListener
317: )
318: container.add(comp)
319: return container
320:
321: def __call__(self, event):
322: s = [e.value for e in self.container.components]
323: self.command.client.update(value = Color(*s))
324:
325: ## ---------------------------------------- JSpinner
326: from javax.swing import SpinnerListModel
327: from javax.swing import JSpinner
328:
329: class Tab_JSpinner(TabbedComponent):
330: species = Color
331: selector = "value"
332:
333: def __init__(self, command):
334: self.model = self.create_Model()
335: self.container = self.create(command)
336:
337: def create_Model(self):
338: return SpinnerListModel(self.items)
339:
340: def create(self, command):
341: container = JPanel()
342: comp = JSpinner(
343: model = self.model,
344: #### columns = max(len(e) for e in self.items),
345: stateChanged = command, #addChangeListener
346: )
347: container.add(comp)
348: return container
349:
350: ## ---------------------------------------- JTextArea
351: from javax.swing import JTextArea
352:
353: class Tab_JTextArea(TabbedComponent):
354: species = colors
355: selector = "selectedText"
356:
357: def __init__(self, command):
358: self.container = self.create(command)
359:
360: def create(self, command):
361: items = [e for e in dir(colors) if not e.startswith("_")]
362: text = ", ".join(items)
363:
364: comp = JTextArea(
365: columns = 20,
366: rows = 8,
367: lineWrap = True,
368: wrapStyleWord = True,
369: text = text,
370: )
371: container = JScrollPane(comp)
372: return container
373:
374: ## ---------------------------------------- JTextField
375: from javax.swing import JTextField
376:
377: class Tab_JTextField(TabbedComponent):
378: species = colors
379: selector = "text"
380:
381: def __init__(self, command):
382: self.container = self.create(command)
383:
384: def create(self, command):
385: text = "red"
386:
387: container = JPanel()
388: comp = JTextField(
389: columns = 12,
390: text = text,
391: actionPerformed = command,
392: )
393: container.add(comp)
394: return container
395:
396: ## ----------------------------------------
397: ## ----------------------------------------
398: from time import ctime
399: from sys import argv, version
400:
401: def inform(n=60):
402: print("="*n)
403: print("Version: %s"%version.split("\n")[0])
404: print("Module : %s"%argv[0].split("/")[-1])
405: print("Date : %s"%ctime())
406: print("="*n)
407:
408: ## ----------------------------------------
409: from doctest import testmod
410:
411: if __name__=='__main__':
412: inform()
413: testmod()
414:
415: ## ========================================
416: