00001 from javax.swing import * 00002 from javax.swing.filechooser import FileFilter 00003 from java.lang import * 00004 from java.awt import * 00005 import datetime 00006 import __builtin__ 00007 00008 wimf = None 00009 def waitInputConfirm(msg): 00010 if wimf: 00011 f = wimf.f 00012 else: 00013 f = None 00014 ret = JOptionPane.showConfirmDialog(f, msg, "waitInputConfirm", 00015 JOptionPane.OK_CANCEL_OPTION) 00016 if ret == 2: 00017 raise StandardError, "script is canceled" 00018 return True 00019 00020 def waitInputSelect(msg): 00021 if wimf: 00022 f = wimf.f 00023 else: 00024 f = None 00025 ret = JOptionPane.showConfirmDialog(f, msg, "waitInputSelect", 00026 JOptionPane.YES_NO_CANCEL_OPTION) 00027 if ret == 0: 00028 return True 00029 elif ret == 1: 00030 return False 00031 elif ret == 2: 00032 raise StandardError, "script is canceled" 00033 00034 class posFilter(FileFilter): 00035 def accept(self, f): 00036 if f.isDirectory(): 00037 return True 00038 s = f.toString() 00039 ss = s.split(".") 00040 ext = ss[len(ss)-1] 00041 return ext == "pos" 00042 00043 class commandPanel: 00044 def __init__(self, frame, label, cmd, isFirst): 00045 self.frame = frame 00046 self.panel = JPanel() 00047 self.cmd = cmd 00048 self.isFirst = isFirst 00049 self.bCmd = JButton(label, actionPerformed=self.processEvent) 00050 self.panel.add(self.bCmd) 00051 self.args = [] 00052 self.nstr = cmd.count("#T") 00053 self.nint = cmd.count("#I") 00054 self.ndbl = cmd.count("#D") 00055 if self.nstr: 00056 for i in range(self.nstr): 00057 tf = JTextField(15) 00058 self.args.append(tf) 00059 self.panel.add(tf) 00060 bc = JButton("select", actionPerformed=self.selectFile) 00061 self.panel.add(bc) 00062 elif self.nint: 00063 for i in range(self.nint): 00064 tf = JTextField(3) 00065 self.args.append(tf) 00066 self.panel.add(tf) 00067 elif self.ndbl: 00068 for i in range(self.ndbl): 00069 tf = JTextField(3) 00070 self.args.append(tf) 00071 self.panel.add(tf) 00072 if cmd.count("#continuous"): 00073 self.cbContinuous = JCheckBox("continuous", actionPerformed=self.toggleContinuous) 00074 self.cbContinuous.setSelected(True) 00075 self.panel.add(self.cbContinuous) 00076 self.continuous = True 00077 else: 00078 self.cbContinuous = None 00079 self.continuous = False 00080 return 00081 def toggleContinuous(self,event): 00082 self.continuous = self.cbContinuous.isSelected() 00083 return 00084 def selectFile(self, event): 00085 jfc = JFileChooser(".") 00086 pf = posFilter() 00087 jfc.addChoosableFileFilter(pf) 00088 ret = jfc.showOpenDialog(None) 00089 if ret == 0: 00090 path = jfc.getSelectedFile().toString() 00091 a = path.split("/") 00092 file = a[len(a)-1] 00093 base = file.split(".")[0] 00094 self.args[0].setText(base) 00095 def processEvent(self, event): 00096 cmd = self.cmd 00097 if self.nstr: 00098 for i in range(self.nstr): 00099 cmd = cmd.replace("#T", self.args[i].getText(), 1) 00100 elif self.nint: 00101 for i in range(self.nint): 00102 cmd = cmd.replace("#I", self.args[i].getText(), 1) 00103 elif self.ndbl: 00104 for i in range(self.ndbl): 00105 cmd = cmd.replace("#D", self.args[i].getText(), 1) 00106 self.frame.addHistory(cmd) 00107 exec(cmd) 00108 if self.isFirst and self.frame.isSequential(): 00109 self.frame.advancePage() 00110 return 00111 00112 class waitInputMenuFrame: 00113 def __init__(self, menu): 00114 self.f = JFrame("waitInputMenu") 00115 self.f.setSize(400, 600) 00116 self.tabPane = JTabbedPane(JTabbedPane.TOP) 00117 00118 self.lmenu = menu[1:] 00119 self.page = 0 00120 self.pageExec = 0 00121 self.color = Color(238,238,238) 00122 panel1 = JPanel() 00123 panel1.setLayout(BorderLayout()) 00124 self.localMenuPanel = JPanel() 00125 self.localMenuPanel.setLayout(BoxLayout(self.localMenuPanel, 00126 BoxLayout.Y_AXIS)) 00127 panel1.add(self.localMenuPanel, BorderLayout.NORTH) 00128 p = JPanel() 00129 p.setLayout(GridLayout(1,4)) 00130 self.lPage = JLabel() 00131 p.add(self.lPage) 00132 self.bPrev = JButton("Prev", actionPerformed=self.prevPage) 00133 p.add(self.bPrev) 00134 self.bNext = JButton("Next", actionPerformed=self.nextPage) 00135 p.add(self.bNext) 00136 self.bSkip = JButton("Skip", actionPerformed=self.advancePage) 00137 p.add(self.bSkip) 00138 self.cbSequential = JCheckBox("sequential", actionPerformed=self.setupPage) 00139 self.cbSequential.setSelected(True) 00140 p.add(self.cbSequential) 00141 panel1.add(p, BorderLayout.SOUTH) 00142 self.setupPage() 00143 self.tabPane.addTab("Local", JScrollPane(panel1)) 00144 00145 gmenu = menu[0] 00146 panel2 = JPanel() 00147 p = JPanel() 00148 panel2.setLayout(BoxLayout(panel2, BoxLayout.Y_AXIS)) 00149 bQuit = JButton("Quit", actionPerformed=exit) 00150 p.add(bQuit) 00151 panel2.add(p) 00152 p = JPanel() 00153 bRestart = JButton("Restart", actionPerformed=self.restart) 00154 p.add(bRestart) 00155 panel2.add(p) 00156 for i in range(len(gmenu)/2): 00157 label = gmenu[i*2] 00158 content = gmenu[i*2+1] 00159 if content == "#label": 00160 p = JPanel() 00161 l = JLabel(label) 00162 p.add(l) 00163 else: 00164 cp = commandPanel(self, label, content, False) 00165 p = cp.panel 00166 panel2.add(p) 00167 self.tabPane.addTab("Global", JScrollPane(panel2)) 00168 00169 panel3 = JPanel() 00170 self.taHistory = JTextArea() 00171 sp = JScrollPane(self.taHistory) 00172 panel3.setLayout(BorderLayout()) 00173 panel3.add(sp, BorderLayout.CENTER) 00174 self.tabPane.addTab("History", panel3) 00175 00176 self.f.add(self.tabPane) 00177 self.f.setVisible(True) 00178 def restart(self, event): 00179 self.pageExec = 0 00180 self.page = 0 00181 self.setupPage() 00182 self.tabPane.setSelectedIndex(0) 00183 return 00184 def isSequential(self): 00185 return self.cbSequential.isSelected() 00186 def setupPage(self, event=None): 00187 self.localMenuPanel.removeAll() 00188 pmenu = self.lmenu[self.page] 00189 for i in range(len(pmenu)/2): 00190 cp = commandPanel(self, pmenu[i*2], pmenu[i*2+1], i==0) 00191 if self.page != self.pageExec and self.isSequential(): 00192 cp.bCmd.setEnabled(False) 00193 else: 00194 cp.bCmd.setEnabled(True) 00195 self.localMenuPanel.add(cp.panel) 00196 if cp.continuous and self.page == self.pageExec: 00197 cp.processEvent(event) 00198 self.f.setVisible(True) 00199 self.lPage.setText(str(self.page+1)+"/"+str(len(self.lmenu))) 00200 if self.page == 0: 00201 self.bPrev.setEnabled(False) 00202 else: 00203 self.bPrev.setEnabled(True) 00204 if self.page == len(self.lmenu)-1: 00205 self.bNext.setEnabled(False) 00206 self.bSkip.setEnabled(False) 00207 else: 00208 self.bNext.setEnabled(True) 00209 self.bSkip.setEnabled(True) 00210 for c in self.localMenuPanel.getComponents(): 00211 c.setBackground(self.color) 00212 return 00213 def advancePage(self, event=None): 00214 if self.pageExec < len(self.lmenu)-1: 00215 self.pageExec += 1 00216 self.page += 1 00217 self.setupPage() 00218 elif self.pageExec == len(self.lmenu)-1: 00219 self.tabPane.setSelectedIndex(1) 00220 return 00221 def nextPage(self, event): 00222 self.page += 1 00223 self.setupPage() 00224 return 00225 def prevPage(self, event): 00226 self.page -= 1 00227 self.setupPage() 00228 return 00229 def addHistory(self,msg): 00230 dstr = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S") 00231 str = dstr+":"+msg+"\n" 00232 self.taHistory.append(str) 00233 return 00234 00235 def waitInputMenu(menu): 00236 global wimf 00237 wimf = waitInputMenuFrame(menu) 00238 00239 def setMenuColor(c): 00240 wimf.color = c 00241 for comp in wimf.localMenuPanel.getComponents(): 00242 comp.setBackground(c) 00243 00244 00245 __builtin__.waitInputConfirm = waitInputConfirm 00246 __builtin__.waitInputSelect = waitInputSelect 00247 __builtin__.waitInputMenu = waitInputMenu