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.07
15: """
16: ## ---------------------------------------- demo: jython
17: """
18: _jython=/Users/sketch/book/java_useBetter_1/_jython
19:
20: cd /Users/sketch/book/java_useBetter_1/swing/JToolBar/toolBarDemo/jython
21: JYTHONPATH=$_jython jython -i ToolBarDemo.py
22: ============================================================
23: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
24: Module : ToolBarDemo.py
25: Date : Fri Oct 1 20:16:06 2010
26: ============================================================
27: """
28: ## ----------------------------------------
29: ##package components;
30:
31: from java.awt import BorderLayout
32: from java.awt.event import ActionListener
33: from javax.swing import JButton
34: from javax.swing import JFrame
35: from javax.swing import JPanel
36: from javax.swing import JScrollPane
37: from javax.swing import JTextArea
38: from javax.swing import JToolBar
39: from javax.swing import ImageIcon
40:
41: ## ----------------------------------------
42: class ToolBarDemo(JPanel, ActionListener):
43:
44: def __init__(self, *args):
45: super(self.__class__,self).__init__(
46: layout = BorderLayout(),
47: preferredSize = (450, 130),
48: )
49:
50: #/Create the toolbar.
51: toolBar = JToolBar("Still draggable")
52: self.addButtons(toolBar)
53:
54: #/Create the text area used for output. Request
55: #/enough space for 5 rows and 30 columns.
56: self.textArea = JTextArea(
57: rows = 5,
58: columns = 30,
59: editable = False,
60: )
61: scrollPane = JScrollPane(self.textArea)
62:
63: #/Lay out the main panel.
64: self.add(toolBar, BorderLayout.PAGE_START)
65: self.add(scrollPane, BorderLayout.CENTER)
66:
67: def addButtons(self, toolBar):
68: s = [
69: ( "Back24",
70: PreviousCommand, # first button
71: "Back to previous something-or-other",
72: "Previous",
73: ),
74: ( "Up24",
75: UpCommand, # second button
76: "Up to something-or-other",
77: "Up",
78: ),
79: ( "Forward24",
80: NextCommand, # third button
81: "Forward to something-or-other",
82: "Next",
83: ),
84: ]
85: for imageName, command, toolTipText, altText in s:
86: button = self.makeNavigationButton(
87: imageName, command, toolTipText, altText)
88: toolBar.add(button)
89:
90: def makeNavigationButton(self,
91: imageName, command, toolTipText, altText
92: ):
93: #/Look for the image.
94: imageName = "_images/%s.gif"%imageName
95:
96: #/Create and initialize the button.
97: button = CommandButton(
98: icon = ImageIcon(imageName, altText),
99: toolTipText = toolTipText,
100: actionPerformed = self,
101: command = command(),
102: )
103: return button
104:
105: def __call__(self, e):
106: #/ Handle each button.
107: s = "If this were a real app,\n it would have "
108: self.displayResult(s+e.source.command())
109:
110: def displayResult(self, actionDescription):
111: self.textArea.append(actionDescription + "\n")
112: self.textArea.caretPosition = self.textArea.document.length
113:
114: ## ----------------------------------------
115: @staticmethod
116: def createAndShowGUI():
117: ##//Create and set up the window.
118: frame = JFrame(
119: title = "ToolBarDemo",
120: locationRelativeTo = None,
121: defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
122: )
123:
124: ##//Add content to the window.
125: frame.add(ToolBarDemo())
126:
127: ##//Display the window.
128: frame.pack()
129: frame.visible = True
130:
131: ## ---------------------------------------- Command::Invoker
132: class CommandButton(JButton):
133: pass
134:
135: ## ---------------------------------------- Command::Command
136: class Command:
137: def __call__(self):
138: raise NotImplementedError, self.__class__.__name__
139:
140: ## ---------------------------------------- Command::ConcreteCommand
141: class PreviousCommand(Command):
142: #/ first button clicked
143: def __call__(self):
144: return "taken you to the previous ."
145:
146: ## ---------------------------------------- Command::ConcreteCommand
147: class UpCommand(Command):
148: #/ second button clicked
149: def __call__(self):
150: return "taken you up one level to ."
151:
152: ## ---------------------------------------- Command::ConcreteCommand
153: class NextCommand(Command):
154: #/ third button clicked
155: def __call__(self):
156: return "taken you to the next ."
157:
158: ## ----------------------------------------
159: from __init__ import *
160: tips = ToolBarDemo.createAndShowGUI
161:
162: if __name__=='__main__':
163: inform()
164: testmod()
165:
166: ## ========================================