jython/waitInput.py
Go to the documentation of this file.
1 from javax.swing import *
2 from javax.swing.filechooser import FileFilter
3 from java.lang import *
4 from java.awt import *
5 import datetime
6 import __builtin__
7 
8 wimf = None
9 def waitInputConfirm(msg):
10  if wimf:
11  f = wimf.f
12  else:
13  f = None
14  ret = JOptionPane.showConfirmDialog(f, msg, "waitInputConfirm",
15  JOptionPane.OK_CANCEL_OPTION)
16  if ret == 2:
17  raise StandardError, "script is canceled"
18  return True
19 
20 def waitInputSelect(msg):
21  if wimf:
22  f = wimf.f
23  else:
24  f = None
25  ret = JOptionPane.showConfirmDialog(f, msg, "waitInputSelect",
26  JOptionPane.YES_NO_CANCEL_OPTION)
27  if ret == 0:
28  return True
29  elif ret == 1:
30  return False
31  elif ret == 2:
32  raise StandardError, "script is canceled"
33 
34 class posFilter(FileFilter):
35  def accept(self, f):
36  if f.isDirectory():
37  return True
38  s = f.toString()
39  ss = s.split(".")
40  ext = ss[len(ss)-1]
41  return ext == "pos"
42 
44  def __init__(self, frame, label, cmd, isFirst):
45  self.frame = frame
46  self.panel = JPanel()
47  self.cmd = cmd
48  self.isFirst = isFirst
49  self.bCmd = JButton(label, actionPerformed=self.processEvent)
50  self.panel.add(self.bCmd)
51  self.args = []
52  self.nstr = cmd.count("#T")
53  self.nint = cmd.count("#I")
54  self.ndbl = cmd.count("#D")
55  if self.nstr:
56  for i in range(self.nstr):
57  tf = JTextField(15)
58  self.args.append(tf)
59  self.panel.add(tf)
60  bc = JButton("select", actionPerformed=self.selectFile)
61  self.panel.add(bc)
62  elif self.nint:
63  for i in range(self.nint):
64  tf = JTextField(3)
65  self.args.append(tf)
66  self.panel.add(tf)
67  elif self.ndbl:
68  for i in range(self.ndbl):
69  tf = JTextField(3)
70  self.args.append(tf)
71  self.panel.add(tf)
72  if cmd.count("#continuous"):
73  self.cbContinuous = JCheckBox("continuous", actionPerformed=self.toggleContinuous)
74  self.cbContinuous.setSelected(True)
75  self.panel.add(self.cbContinuous)
76  self.continuous = True
77  else:
78  self.cbContinuous = None
79  self.continuous = False
80  return
81  def toggleContinuous(self,event):
82  self.continuous = self.cbContinuous.isSelected()
83  return
84  def selectFile(self, event):
85  jfc = JFileChooser(".")
86  pf = posFilter()
87  jfc.addChoosableFileFilter(pf)
88  ret = jfc.showOpenDialog(None)
89  if ret == 0:
90  path = jfc.getSelectedFile().toString()
91  a = path.split("/")
92  file = a[len(a)-1]
93  base = file.split(".")[0]
94  self.args[0].setText(base)
95  def processEvent(self, event):
96  cmd = self.cmd
97  if self.nstr:
98  for i in range(self.nstr):
99  cmd = cmd.replace("#T", self.args[i].getText(), 1)
100  elif self.nint:
101  for i in range(self.nint):
102  cmd = cmd.replace("#I", self.args[i].getText(), 1)
103  elif self.ndbl:
104  for i in range(self.ndbl):
105  cmd = cmd.replace("#D", self.args[i].getText(), 1)
106  self.frame.addHistory(cmd)
107  exec(cmd)
108  if self.isFirst and self.frame.isSequential():
109  self.frame.advancePage()
110  return
111 
113  def __init__(self, menu):
114  self.f = JFrame("waitInputMenu")
115  self.f.setSize(400, 600)
116  self.tabPane = JTabbedPane(JTabbedPane.TOP)
117 
118  self.lmenu = menu[1:]
119  self.page = 0
120  self.pageExec = 0
121  self.color = Color(238,238,238)
122  panel1 = JPanel()
123  panel1.setLayout(BorderLayout())
124  self.localMenuPanel = JPanel()
125  self.localMenuPanel.setLayout(BoxLayout(self.localMenuPanel,
126  BoxLayout.Y_AXIS))
127  panel1.add(self.localMenuPanel, BorderLayout.NORTH)
128  p = JPanel()
129  p.setLayout(GridLayout(1,4))
130  self.lPage = JLabel()
131  p.add(self.lPage)
132  self.bPrev = JButton("Prev", actionPerformed=self.prevPage)
133  p.add(self.bPrev)
134  self.bNext = JButton("Next", actionPerformed=self.nextPage)
135  p.add(self.bNext)
136  self.bSkip = JButton("Skip", actionPerformed=self.advancePage)
137  p.add(self.bSkip)
138  self.cbSequential = JCheckBox("sequential", actionPerformed=self.setupPage)
139  self.cbSequential.setSelected(True)
140  p.add(self.cbSequential)
141  panel1.add(p, BorderLayout.SOUTH)
142  self.setupPage()
143  self.tabPane.addTab("Local", JScrollPane(panel1))
144 
145  gmenu = menu[0]
146  panel2 = JPanel()
147  p = JPanel()
148  panel2.setLayout(BoxLayout(panel2, BoxLayout.Y_AXIS))
149  bQuit = JButton("Quit", actionPerformed=exit)
150  p.add(bQuit)
151  panel2.add(p)
152  p = JPanel()
153  bRestart = JButton("Restart", actionPerformed=self.restart)
154  p.add(bRestart)
155  panel2.add(p)
156  for i in range(len(gmenu)/2):
157  label = gmenu[i*2]
158  content = gmenu[i*2+1]
159  if content == "#label":
160  p = JPanel()
161  l = JLabel(label)
162  p.add(l)
163  else:
164  cp = commandPanel(self, label, content, False)
165  p = cp.panel
166  panel2.add(p)
167  self.tabPane.addTab("Global", JScrollPane(panel2))
168 
169  panel3 = JPanel()
170  self.taHistory = JTextArea()
171  sp = JScrollPane(self.taHistory)
172  panel3.setLayout(BorderLayout())
173  panel3.add(sp, BorderLayout.CENTER)
174  self.tabPane.addTab("History", panel3)
175 
176  self.f.add(self.tabPane)
177  self.f.setVisible(True)
178  def restart(self, event):
179  self.pageExec = 0
180  self.page = 0
181  self.setupPage()
182  self.tabPane.setSelectedIndex(0)
183  return
184  def isSequential(self):
185  return self.cbSequential.isSelected()
186  def setupPage(self, event=None):
187  self.localMenuPanel.removeAll()
188  pmenu = self.lmenu[self.page]
189  for i in range(len(pmenu)/2):
190  cp = commandPanel(self, pmenu[i*2], pmenu[i*2+1], i==0)
191  if self.page != self.pageExec and self.isSequential():
192  cp.bCmd.setEnabled(False)
193  else:
194  cp.bCmd.setEnabled(True)
195  self.localMenuPanel.add(cp.panel)
196  if cp.continuous and self.page == self.pageExec:
197  cp.processEvent(event)
198  self.f.setVisible(True)
199  self.lPage.setText(str(self.page+1)+"/"+str(len(self.lmenu)))
200  if self.page == 0:
201  self.bPrev.setEnabled(False)
202  else:
203  self.bPrev.setEnabled(True)
204  if self.page == len(self.lmenu)-1:
205  self.bNext.setEnabled(False)
206  self.bSkip.setEnabled(False)
207  else:
208  self.bNext.setEnabled(True)
209  self.bSkip.setEnabled(True)
210  for c in self.localMenuPanel.getComponents():
211  c.setBackground(self.color)
212  return
213  def advancePage(self, event=None):
214  if self.pageExec < len(self.lmenu)-1:
215  self.pageExec += 1
216  self.page += 1
217  self.setupPage()
218  elif self.pageExec == len(self.lmenu)-1:
219  self.tabPane.setSelectedIndex(1)
220  return
221  def nextPage(self, event):
222  self.page += 1
223  self.setupPage()
224  return
225  def prevPage(self, event):
226  self.page -= 1
227  self.setupPage()
228  return
229  def addHistory(self,msg):
230  dstr = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
231  str = dstr+":"+msg+"\n"
232  self.taHistory.append(str)
233  return
234 
235 def waitInputMenu(menu):
236  global wimf
237  wimf = waitInputMenuFrame(menu)
238 
240  wimf.color = c
241  for comp in wimf.localMenuPanel.getComponents():
242  comp.setBackground(c)
243 
244 
245 __builtin__.waitInputConfirm = waitInputConfirm
246 __builtin__.waitInputSelect = waitInputSelect
247 __builtin__.waitInputMenu = waitInputMenu
def waitInputConfirm(msg)
def toggleContinuous(self, event)
def setupPage(self, event=None)
def __init__(self, frame, label, cmd, isFirst)
def waitInputSelect(msg)
def selectFile(self, event)
def advancePage(self, event=None)
def setMenuColor(c)
def processEvent(self, event)
def waitInputMenu(menu)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51