Back to index

moin  1.9.0~rc2
CommandMenu.java
Go to the documentation of this file.
00001 /*
00002  * @(#)CommandMenu.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.util;
00007 
00008 import java.awt.*;
00009 import java.util.*;
00010 
00018 import java.awt.event.ActionListener;
00019 import java.awt.event.ActionEvent;
00020 
00021 public  class CommandMenu
00022         extends Menu implements ActionListener {
00023 
00024     private Vector   fCommands;
00025 
00026     public CommandMenu(String name) {
00027         super(name);
00028         fCommands = new Vector(10);
00029     }
00030 
00035     public synchronized void add(Command command) {
00036         MenuItem m = new MenuItem(command.name());
00037         m.addActionListener(this);
00038         add(m);
00039         fCommands.addElement(command);
00040     }
00041 
00046     public synchronized void add(Command command, MenuShortcut shortcut) {
00047         MenuItem m = new MenuItem(command.name(), shortcut);
00048         m.setName(command.name());
00049         m.addActionListener(this);
00050         add(m);
00051         fCommands.addElement(command);
00052     }
00053 
00054     public synchronized void remove(Command command) {
00055         System.out.println("not implemented");
00056     }
00057 
00058     public synchronized void remove(MenuItem item) {
00059         System.out.println("not implemented");
00060     }
00061 
00065     public synchronized void enable(String name, boolean state) {
00066         for (int i = 0; i < getItemCount(); i++) {
00067             MenuItem item = getItem(i);
00068             if (name.equals(item.getLabel())) {
00069                 item.setEnabled(state);
00070                 return;
00071             }
00072         }
00073     }
00074 
00075     public synchronized void checkEnabled() {
00076         int j = 0;
00077         for (int i = 0; i < getItemCount(); i++) {
00078             MenuItem item = getItem(i);
00079             // ignore separators
00080             // a separator has a hyphen as its label
00081             if (item.getLabel().equals("-"))
00082                 continue;
00083             Command cmd = (Command)fCommands.elementAt(j);
00084             item.setEnabled(cmd.isExecutable());
00085             j++;
00086         }
00087     }
00088 
00092     public void actionPerformed(ActionEvent e) {
00093         int j = 0;
00094         Object source = e.getSource();
00095         for (int i = 0; i < getItemCount(); i++) {
00096             MenuItem item = getItem(i);
00097             // ignore separators
00098             // a separator has a hyphen as its label
00099             if (item.getLabel().equals("-"))
00100                 continue;
00101             if (source == item) {
00102                 Command cmd = (Command)fCommands.elementAt(j);
00103                 cmd.execute();
00104                 break;
00105             }
00106             j++;
00107         }
00108     }
00109 }
00110 
00111