20 package com.generalrobotix.ui.view;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Container;
25 import java.awt.Dimension;
26 import java.awt.Frame;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.List;
33 import javax.swing.BoxLayout;
34 import javax.swing.JButton;
35 import javax.swing.JCheckBox;
36 import javax.swing.JComponent;
37 import javax.swing.JDialog;
38 import javax.swing.JLabel;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollPane;
41 import javax.swing.JTabbedPane;
42 import javax.swing.JTextArea;
43 import javax.swing.JTextField;
44 import javax.swing.SwingConstants;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
47 import javax.swing.text.JTextComponent;
49 import org.python.core.PyObject;
50 import org.python.util.PythonInterpreter;
61 @SuppressWarnings(
"serial")
64 private JDialog dialog_ =
null;
65 private JPanel jPanel_localMenu =
null;
66 private JButton jButton_previous =
null;
67 private JButton jButton_next =
null;
68 private JLabel jLabel =
null;
69 private JCheckBox jCheckBoxSequential =
null;
70 private JTextArea jTextArea =
null;
72 private JCheckBox jCheckBoxContinuous =
null;
74 private List<JTextField> currentFields_ =
new ArrayList<JTextField>();
78 private int showingStage_ = 0;
79 private int currentStage_ = 0;
80 private String command_ =
null;
81 private boolean isWaiting_ =
false;
82 private static final SimpleDateFormat dateFormat_ =
new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
83 private static final String QUITBUTTON_TITLE =
"Quit";
84 private HashMap<String, String> exceptMap =
new HashMap<String, String>();
91 interpreter_ = interpreter;
97 exceptMap.put(QUITBUTTON_TITLE,QUITBUTTON_TITLE);
100 JPanel localPanel =
new JPanel(
new BorderLayout());
101 localPanel.add(
new JScrollPane(getLocalMenuPanel()),BorderLayout.CENTER);
103 JPanel localPanelLower =
new JPanel();
104 localPanelLower.add(getPreviousButton(),
null);
105 jLabel =
new JLabel(
" 1 / 1 ");
106 localPanelLower.add(jLabel,
null);
107 localPanelLower.add(getNextButton(),
null);
108 localPanelLower.add(getJCheckBoxSequential(),
null);
109 localPanel.add(localPanelLower,BorderLayout.SOUTH);
112 JTabbedPane tabbedPane =
new JTabbedPane();
113 tabbedPane.addTab(
"Local",
null, localPanel,
null);
114 tabbedPane.addTab(
"Global",
null,
new JScrollPane(getGlobalMenuPanel()),
null);
115 tabbedPane.addTab(
"History",
null,
new JScrollPane(getJTextArea()),
null);
116 tabbedPane.addChangeListener(
new ChangeListener(){
117 public void stateChanged(ChangeEvent e){
118 JTabbedPane jtp = (JTabbedPane)e.getSource();
119 if (jtp.getSelectedIndex() != 2)
120 _clearTextComponentRecursive((Container)jtp.getSelectedComponent());
125 setLayout(
new BorderLayout());
126 add(tabbedPane,BorderLayout.CENTER);
131 void setReadyToNext(){
133 if (getJCheckBoxSequential().isSelected()){
134 if (currentStage_ < menu_.length){
135 showingStage_ = currentStage_;
140 showingStage_ = currentStage_ = menu_.length-1;
151 JPanel jpanel_globalMenu =
null;
153 getGlobalMenuPanel().removeAll();
155 JPanel jpanel =
new JPanel();
156 JButton buttonQuit =
new JButton(QUITBUTTON_TITLE);
157 buttonQuit.addActionListener(
new java.awt.event.ActionListener() {
158 public void actionPerformed(java.awt.event.ActionEvent e) {
162 jpanel.add(buttonQuit);
163 jpanel.setAlignmentX( JComponent.CENTER_ALIGNMENT );
164 getGlobalMenuPanel().add(jpanel);
166 jpanel =
new JPanel();
167 JButton buttonRetry =
new JButton(
"RETRY(from first)");
168 buttonRetry.addActionListener(
new java.awt.event.ActionListener() {
169 public void actionPerformed(java.awt.event.ActionEvent e) {
171 _setTabSelected(getLocalMenuPanel());
172 getLocalMenuPanel().setVisible(
true);
175 jpanel.add(buttonRetry);
176 jpanel.setAlignmentX( JComponent.CENTER_ALIGNMENT );
177 getGlobalMenuPanel().add(jpanel);
179 for(
int i=1;
i<menu_[0].length;
i=
i+2){
180 String m1 = menu_[0][
i-1].trim();
181 String m2 = menu_[0][
i].trim();
182 if (m2.equals(
"#label")){
183 JLabel label =
new JLabel(m1, SwingConstants.LEFT);
184 label.setAlignmentX( JComponent.CENTER_ALIGNMENT );
185 getGlobalMenuPanel().add(label);
186 }
else if(m2.equals(
"#monitor")) {
187 PyObject res = interpreter_.eval(m1);
188 JLabel label =
new JLabel(res.toString(), SwingConstants.LEFT);
189 label.setAlignmentX( JComponent.CENTER_ALIGNMENT );
190 getGlobalMenuPanel().add(label);
192 addButton(getGlobalMenuPanel(),m1,m2,
false);
198 if (jpanel_globalMenu ==
null){
199 jpanel_globalMenu =
new JPanel();
200 jpanel_globalMenu.setLayout(
new BoxLayout(jpanel_globalMenu, BoxLayout.Y_AXIS));
202 return jpanel_globalMenu;
207 getLocalMenuPanel().setVisible(
false);
208 getLocalMenuPanel().removeAll();
209 getLocalMenuPanel().setVisible(
true);
210 if (showingStage_ >= menu_.length-1){
211 showingStage_ = menu_.length-1;
212 getNextButton().setEnabled(
false);
214 getNextButton().setEnabled(
true);
216 if (showingStage_ < 2){
218 getPreviousButton().setEnabled(
false);
220 getPreviousButton().setEnabled(
true);
223 currentFields_.clear();
224 for(
int i=1;
i<menu_[showingStage_].length;
i=
i+2){
225 String m1 = menu_[showingStage_][
i-1].trim();
226 String m2 = menu_[showingStage_][
i].trim();
227 if (m2.equals(
"#label")){
228 JLabel label =
new JLabel(m1, SwingConstants.LEFT);
229 label.setAlignmentX( JComponent.CENTER_ALIGNMENT );
230 getLocalMenuPanel().add(label);
231 }
else if(m2.equals(
"#monitor")) {
232 PyObject res = interpreter_.eval(m1);
233 JLabel label =
new JLabel(res.toString(), SwingConstants.LEFT);
234 label.setAlignmentX( JComponent.CENTER_ALIGNMENT );
235 getLocalMenuPanel().add(label);
237 addButton(getLocalMenuPanel(),m1,m2,(
i<=1));
241 jLabel.setText(showingStage_+
" / "+(menu_.length-1));
243 if (command_ !=
null){
247 if (getJCheckBoxSequential().isSelected() && showingStage_ != currentStage_)
257 if (jPanel_localMenu ==
null) {
258 jPanel_localMenu =
new JPanel();
259 jPanel_localMenu.setLayout(
new BoxLayout(jPanel_localMenu, BoxLayout.Y_AXIS));
261 return jPanel_localMenu;
264 if (jButton_previous ==
null) {
265 jButton_previous =
new JButton(
"Previous");
266 jButton_previous.setToolTipText(
"show previous menu");
267 jButton_previous.addActionListener(
new java.awt.event.ActionListener() {
268 public void actionPerformed(java.awt.event.ActionEvent e) {
274 return jButton_previous;
277 if (jButton_next ==
null) {
278 jButton_next =
new JButton(
" Next ");
279 jButton_next.setToolTipText(
"show next menu");
280 jButton_next.addActionListener(
new java.awt.event.ActionListener() {
281 public void actionPerformed(java.awt.event.ActionEvent e) {
290 if (jCheckBoxSequential ==
null) {
291 jCheckBoxSequential =
new JCheckBox(
"Sequential",
true);
292 jCheckBoxSequential.setToolTipText(
"enable only current button");
293 jCheckBoxSequential.addActionListener(
new java.awt.event.ActionListener() {
294 public void actionPerformed(java.awt.event.ActionEvent e) {
295 if (jCheckBoxSequential.isSelected()){
296 showingStage_ = currentStage_;
298 getJCheckBoxContinuous().setEnabled(
true);
303 getJCheckBoxContinuous().setEnabled(
false);
308 return jCheckBoxSequential;
311 if (jCheckBoxContinuous ==
null) {
312 jCheckBoxContinuous =
new JCheckBox(
"continuous",
false);
313 jCheckBoxContinuous.setToolTipText(
"excecute continuously");
315 return jCheckBoxContinuous;
317 private JButton
addButton(JPanel pnl,
final String
name,
final String
com,
final boolean goNext){
318 JPanel jpanel =
new JPanel();
319 JButton button =
new JButton(name);
322 final String[] str = parseCommand(com);
324 if (str!=
null) len = str.length;
325 final JTextField[] fields =
new JTextField[len];
326 if (str!=
null && len > 0){
327 for (
int i=0;
i<len;
i++){
328 fields[
i] =
new JTextField();
329 fields[
i].setPreferredSize(
new Dimension(60,20));
330 jpanel.add(fields[
i]);
337 if (com.indexOf(
"#continuous")!=-1){
338 getJCheckBoxContinuous().setSelected(
true);
339 jpanel.add(getJCheckBoxContinuous());
341 getJCheckBoxContinuous().setSelected(
false);
345 button.addActionListener(
new java.awt.event.ActionListener() {
346 public void actionPerformed(java.awt.event.ActionEvent e) {
347 if (command_ ==
null){
351 if (com.indexOf(
"#appendlog") < 0){
354 if (str!=
null && str.length > 0){
355 for (
int i=0;
i<str.length;
i++){
356 char c = str[
i].charAt(1);
357 String s = fields[
i].getText();
358 if (c ==
'd' || c ==
'D'){
359 Double.parseDouble(s);
360 }
else if (c ==
'i' || c ==
'I'){
362 }
else if (c ==
't' || c ==
'T'){
365 rcom = rcom.replaceFirst(str[
i],s);
370 String comLog = dateFormat_.format(
new Date())+
" : "+name+
" : "+command_+
"\n";
371 getJTextArea().append(comLog);
373 if (getJCheckBoxSequential().isSelected() && goNext ){
382 interpreter_.exec(command_);
385 }
catch (NumberFormatException e1){
387 writer_.
write(
"MenuDialog: parse error "+e1+System.getProperty(
"line.separator"));
392 jpanel.setAlignmentX( JComponent.CENTER_ALIGNMENT );
398 StringBuffer sb =
new StringBuffer();
400 while((idx = com.indexOf(
"#",idx+1)) != -1){
401 char c = com.charAt(idx+1);
402 if (c ==
'd' || c ==
'D'||
403 c ==
'i' || c ==
'I' ||
404 c ==
't' || c ==
'T' ) {
405 sb.append(
"#"+c+
" ");
408 if (sb.toString().trim().equals(
""))
410 return sb.toString().split(
" ");
415 if (jTextArea ==
null) {
416 jTextArea =
new JTextArea();
417 jTextArea.setEditable(
false);
423 Component[] components = container.getComponents();
424 for (
int i=0;
i<components.length;
i++){
425 Component
c = components[
i];
426 if (c instanceof JTextComponent){
427 ((JTextComponent)c).setText(
"");
428 }
else if (c instanceof Container) {
429 _clearTextComponentRecursive((Container)c);
434 Component parent = c.getParent();
437 if (parent instanceof JTabbedPane){
438 JTabbedPane tabpane = (JTabbedPane)parent;
439 tabpane.setSelectedComponent(c);
442 _setTabSelected(parent);
446 public void showDialog(Frame owner, String title,
boolean modal){
447 if (dialog_ ==
null ){
448 dialog_ =
new JDialog(owner,title,modal);
450 dialog_.setSize(
new Dimension(400, 400));
451 dialog_.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
452 dialog_.setContentPane(
this);
459 dialog_.setVisible(
true);
460 currentStage_ = showingStage_ = 1;
462 currentMenuPanel_ =
this;
470 return (command_ ==
null);
476 return currentMenuPanel_;
479 if (getJCheckBoxSequential().isSelected()){
480 if (command_ !=
null)
482 return menu_[Math.min(currentStage_,menu_.length-1)][0];
484 return menu_[Math.min(showingStage_,menu_.length-1)][0];
487 if (currentStage_ >= menu_.length-1)
493 if (currentFields_ !=
null && msg !=
null){
495 while((idx = msg.indexOf(
"$",idx+1)) != -1){
496 int idx2 = msg.indexOf(
"=",idx+1);
497 String s = msg.substring(idx+1,idx2);
498 int idx3 = msg.indexOf(
" ",idx2+1);
501 String
val = msg.substring(idx2+1,idx3);
503 int i = Integer.parseInt(s.trim());
504 ((JTextField)(currentFields_.get(i))).setText(val);
506 }
catch (Exception e){
518 getJCheckBoxContinuous().setSelected(b);
521 dialog_.setVisible(
false);
523 currentMenuPanel_ =
null;
#define null
our own NULL pointer
png_infop png_charpp name
static void printErr(String s)
static void setEnableRecursive(boolean e, Container container, HashMap except)
void write(char[] cbuf, int off, int len)