MenuDialog.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * MenuDialog.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro Kawasumi (General Robotix, Inc.)
17  * Created on 2005/01/31
18  */
19 
20 package com.generalrobotix.ui.view;
21 
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;
32 
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;
48 
49 import org.python.core.PyObject;
50 import org.python.util.PythonInterpreter;
51 
61 @SuppressWarnings("serial")
62 public class MenuDialog extends JPanel {
63  private static MenuDialog currentMenuPanel_ = null;
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;
71 
72  private JCheckBox jCheckBoxContinuous = null;
73  //private JButton currentGoNextButton_ = null;
74  private List<JTextField> currentFields_ = new ArrayList<JTextField>();
75  //private boolean isClickedButtonGoNext_ = false;
76 
77  private String[][] menu_;
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>();
85  private String message_;
86  private PythonInterpreter interpreter_;
88 
89  public MenuDialog(String[][] src, PythonInterpreter interpreter, String message, StyledTextWriter writer){
90  menu_ = src;
91  interpreter_ = interpreter;
92  message_ = message;
93  writer_ = writer;
94  initialize();
95  }
96  private void initialize() {
97  exceptMap.put(QUITBUTTON_TITLE,QUITBUTTON_TITLE);
98  // set panel configuration
99  // local panel
100  JPanel localPanel = new JPanel(new BorderLayout());
101  localPanel.add(new JScrollPane(getLocalMenuPanel()),BorderLayout.CENTER);
102 
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);
110 
111  // main tab
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());
121  }
122  });
123 
124  // content pane
125  setLayout(new BorderLayout());
126  add(tabbedPane,BorderLayout.CENTER);
127 
128  isWaiting_ = false;
129  }
130 
131  void setReadyToNext(){
132  command_ = null;
133  if (getJCheckBoxSequential().isSelected()){
134  if (currentStage_ < menu_.length){
135  showingStage_ = currentStage_;
136  // boolean doClick = (getJCheckBoxContinuous().isSelected()) &&
137  // isClickedButtonGoNext_;
138  showLocalMenu();
139  } else {
140  showingStage_ = currentStage_ = menu_.length-1;
141  showLocalMenu();
142  // _setTabSelected(getGlobalMenuPanel());
143  // getGlobalMenuPanel().setVisible(true);
144  }
145  } else {
146  showLocalMenu();
147  }
148  }
149 
150  // Global Menu Panel -------------------------
151  JPanel jpanel_globalMenu = null;
152  private void showGlobalMenu(){
153  getGlobalMenuPanel().removeAll();
154 
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) {
159  exit();
160  }
161  });
162  jpanel.add(buttonQuit);
163  jpanel.setAlignmentX( JComponent.CENTER_ALIGNMENT );
164  getGlobalMenuPanel().add(jpanel);
165 
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) {
170  refresh();
171  _setTabSelected(getLocalMenuPanel());
172  getLocalMenuPanel().setVisible(true);
173  }
174  });
175  jpanel.add(buttonRetry);
176  jpanel.setAlignmentX( JComponent.CENTER_ALIGNMENT );
177  getGlobalMenuPanel().add(jpanel);
178 
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);
191  }else {
192  addButton(getGlobalMenuPanel(),m1,m2,false);
193  }
194  }
195  }
196 
197  private JPanel getGlobalMenuPanel() {
198  if (jpanel_globalMenu == null){
199  jpanel_globalMenu = new JPanel();
200  jpanel_globalMenu.setLayout(new BoxLayout(jpanel_globalMenu, BoxLayout.Y_AXIS));
201  }
202  return jpanel_globalMenu;
203  }
204 
205  // Local Menu Panel -------------------------
206  private void showLocalMenu(){
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);
213  } else {
214  getNextButton().setEnabled(true);
215  }
216  if (showingStage_ < 2){
217  showingStage_ = 1;
218  getPreviousButton().setEnabled(false);
219  } else {
220  getPreviousButton().setEnabled(true);
221  }
222 
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);
236  }else {
237  addButton(getLocalMenuPanel(),m1,m2,(i<=1));
238  }
239  }
240 
241  jLabel.setText(showingStage_+" / "+(menu_.length-1));
242 
243  if (command_ != null){
244  GrxGuiUtil.setEnableRecursive(false,getLocalMenuPanel(),null);
245  GrxGuiUtil.setEnableRecursive(false,getGlobalMenuPanel(),exceptMap);
246  } else {
247  if (getJCheckBoxSequential().isSelected() && showingStage_ != currentStage_)
248  GrxGuiUtil.setEnableRecursive(false,getLocalMenuPanel(),null);
249  GrxGuiUtil.setEnableRecursive(true,getGlobalMenuPanel(),null);
250  }
251  // getLocalMenuPanel().add(javax.swing.Box.createVerticalGlue());
252  //getLocalMenuPanel().setVisible(true);
253  //setVisible(true);
254  }
255 
256  private JPanel getLocalMenuPanel() {
257  if (jPanel_localMenu == null) {
258  jPanel_localMenu = new JPanel();
259  jPanel_localMenu.setLayout(new BoxLayout(jPanel_localMenu, BoxLayout.Y_AXIS));
260  }
261  return jPanel_localMenu;
262  }
263  private JButton getPreviousButton() {
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) {
269  showingStage_--;
270  showLocalMenu();
271  }
272  });
273  }
274  return jButton_previous;
275  }
276  private JButton getNextButton() {
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) {
282  showingStage_++;
283  showLocalMenu();
284  }
285  });
286  }
287  return jButton_next;
288  }
289  private JCheckBox getJCheckBoxSequential() {
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_;
297  showLocalMenu();
298  getJCheckBoxContinuous().setEnabled(true);
299  } else {
300  if (isIdle()){
301  GrxGuiUtil.setEnableRecursive(true,getLocalMenuPanel(),null);
302  }
303  getJCheckBoxContinuous().setEnabled(false);
304  }
305  }
306  });
307  }
308  return jCheckBoxSequential;
309  }
310  private JCheckBox getJCheckBoxContinuous() {
311  if (jCheckBoxContinuous == null) {
312  jCheckBoxContinuous = new JCheckBox("continuous",false);
313  jCheckBoxContinuous.setToolTipText("excecute continuously");
314  }
315  return jCheckBoxContinuous;
316  }
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);
320  jpanel.add(button);
321 
322  final String[] str = parseCommand(com);
323  int len = 1;
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]);
331  //currentFields_.add(fields[i]);
332  }
333  }
334 
335  if (goNext){
336  //currentGoNextButton_ = button;
337  if (com.indexOf("#continuous")!=-1){
338  getJCheckBoxContinuous().setSelected(true);
339  jpanel.add(getJCheckBoxContinuous());
340  } else {
341  getJCheckBoxContinuous().setSelected(false);
342  }
343  }
344 
345  button.addActionListener(new java.awt.event.ActionListener() {
346  public void actionPerformed(java.awt.event.ActionEvent e) {
347  if (command_ == null){
348  String rcom = com;
349  //DebugUtil.print("command:"+rcom);
350  try{
351  if (com.indexOf("#appendlog") < 0){
352  //BodyStatPanel.getInstance().saveData();
353  }
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'){
361  Integer.parseInt(s);
362  } else if (c == 't' || c == 'T'){
363  s = "\'"+s+"\'";
364  }
365  rcom = rcom.replaceFirst(str[i],s);
366  }
367  //DebugUtil.print("replaced command:"+rcom);
368  }
369  command_ = rcom;
370  String comLog = dateFormat_.format(new Date())+" : "+name+" : "+command_+"\n";
371  getJTextArea().append(comLog);
372 
373  if (getJCheckBoxSequential().isSelected() && goNext ){ // && getJCheckBoxContinuous().isSelected()){
374  // isClickedButtonGoNext_ = true;
375  currentStage_++;
376  } else {
377  // isClickedButtonGoNext_ = false;
378  }
379  GrxGuiUtil.setEnableRecursive(false,getLocalMenuPanel(),null);
380  GrxGuiUtil.setEnableRecursive(false,getGlobalMenuPanel(),exceptMap);
381  //clearTextComponent((Container)(getJTabbedPane().getSelectedComponent()));
382  interpreter_.exec(command_);
383  setReadyToNext();
384  //showMessage(message_);
385  } catch (NumberFormatException e1){
386  GrxDebugUtil.printErr("MenuDialog: parse error");
387  writer_.write("MenuDialog: parse error "+e1+System.getProperty("line.separator"));
388  }
389  }
390  }
391  });
392  jpanel.setAlignmentX( JComponent.CENTER_ALIGNMENT );
393  pnl.add(jpanel);
394  return button;
395  }
396 
397  private String[] parseCommand(String com){
398  StringBuffer sb = new StringBuffer();
399  int idx = -1;
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+" ");
406  }
407  }
408  if (sb.toString().trim().equals(""))
409  return null;
410  return sb.toString().split(" ");
411  }
412 
413  // Log Panel------------------------------------
414  private JTextArea getJTextArea() {
415  if (jTextArea == null) {
416  jTextArea = new JTextArea();
417  jTextArea.setEditable(false);
418  }
419  return jTextArea;
420  }
421 
422  private static void _clearTextComponentRecursive(Container container){
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);
430  }
431  }
432  }
433  private static void _setTabSelected(Component c){
434  Component parent = c.getParent();
435  if (parent == null)
436  return;
437  if (parent instanceof JTabbedPane){
438  JTabbedPane tabpane = (JTabbedPane)parent;
439  tabpane.setSelectedComponent(c);
440  return;
441  }
442  _setTabSelected(parent);
443  }
444  // Public Methods
445  //第一引数をJFrameからawtのFrameに変更
446  public void showDialog(Frame owner, String title, boolean modal){
447  if (dialog_ == null ){
448  dialog_ = new JDialog(owner,title,modal);
449  //GrxGuiUtil.setWindowConfig("menudialog",dialog_,new Dimension(300,400));
450  dialog_.setSize(new Dimension(400, 400));
451  dialog_.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
452  dialog_.setContentPane(this);
453  }
454 
455  refresh();
456 
457  }
458  private void refresh(){
459  dialog_.setVisible(true);
460  currentStage_ = showingStage_ = 1;
461  isWaiting_ = true;
462  currentMenuPanel_ = this;
463  showGlobalMenu();
464  showLocalMenu();
465  }
466  public boolean isWaiting(){
467  return isWaiting_;
468  }
469  public boolean isIdle(){
470  return (command_ == null);
471  }
472  public String getCommand(){
473  return command_;
474  }
476  return currentMenuPanel_;
477  }
478  public String getCurrentMenuItem(){
479  if (getJCheckBoxSequential().isSelected()){
480  if (command_ != null)
481  return command_;
482  return menu_[Math.min(currentStage_,menu_.length-1)][0];
483  }
484  return menu_[Math.min(showingStage_,menu_.length-1)][0];
485  }
486  public boolean isAllDone(){
487  if (currentStage_ >= menu_.length-1)
488  return true;
489  return false;
490  }
491  public void showMessage(String msg){
492  int idx = -1;
493  if (currentFields_ != null && msg != null){
494  //DebugUtil.print("setMessage:"+msg);
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);
499  if (idx3 < 0)
500  idx3 = msg.length();
501  String val = msg.substring(idx2+1,idx3);
502  try {
503  int i = Integer.parseInt(s.trim());
504  ((JTextField)(currentFields_.get(i))).setText(val);
505  //DebugUtil.print("setMessage:$"+i+"="+val);
506  } catch (Exception e){
507  GrxDebugUtil.printErr("MenuDialog.setMessage():",e);
508  }
509  idx = idx2;
510  }
511  }
512  }
513  public void setMessage(String message){
514  message_ = message;
515  }
516 
517  public void setContinuous(boolean b){
518  getJCheckBoxContinuous().setSelected(b);
519  }
520  public void exit(){
521  dialog_.setVisible(false);
522  isWaiting_ = false;
523  currentMenuPanel_ = null;
524  }
525 }
int c
Definition: autoplay.py:16
void showDialog(Frame owner, String title, boolean modal)
#define null
our own NULL pointer
Definition: IceTypes.h:57
def refresh()
Definition: rbimporter.py:36
static void _clearTextComponentRecursive(Container container)
MenuDialog(String[][] src, PythonInterpreter interpreter, String message, StyledTextWriter writer)
Definition: MenuDialog.java:89
png_infop png_charpp name
Definition: png.h:2382
static void _setTabSelected(Component c)
png_uint_32 i
Definition: png.h:2735
long b
Definition: jpegint.h:371
static MenuDialog getCurrentMenuDialog()
int val
Definition: jpeglib.h:956
JButton addButton(JPanel pnl, final String name, final String com, final boolean goNext)
static void setEnableRecursive(boolean e, Container container, HashMap except)
org


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:24