/Users/lyon/j4p/src/gui/PetriBean.java

1    /* 
2     * Created by IntelliJ IDEA. 
3     * User: lyon 
4     * Date: Feb 15, 2003 
5     * Time: 8:49:30 AM 
6     */ 
7    package gui; 
8     
9    import javax.swing.*; 
10   import java.awt.*; 
11   import java.awt.event.ActionEvent; 
12   import java.awt.event.ActionListener; 
13   import java.awt.event.InputEvent; 
14   import java.awt.event.KeyEvent; 
15    
16   public class PetriBean { 
17       private int place = 1; 
18       private String ps = null; 
19       private ActionListener al; 
20    
21       public PetriBean(ActionListener _al) { 
22           al = _al; 
23       } 
24    
25       public boolean matchSilent(AWTEvent e, MenuItem mi) { 
26           if (e.getSource() == mi) return true; 
27    
28           if (e.getSource() instanceof MenuItem) return false; 
29           if (getPs() == null) return false; 
30           if (mi.getLabel().startsWith(getPs())) return true; 
31           return false; 
32       } 
33    
34       public boolean matchSilent(AWTEvent e, JMenuItem mi) { 
35           if (e.getSource() == mi) return true; 
36    
37           if (e.getSource() instanceof JMenuItem) return false; 
38           if (getPs() == null) return false; 
39           if (mi.getText().startsWith(getPs())) return true; 
40           return false; 
41       } 
42    
43       public String getPs() { 
44           return ps; 
45       } 
46    
47       public void setPs(String ps) { 
48           this.ps = ps; 
49       } 
50    
51       private String petriMap(int c) { 
52           String s = "[" + (char) c + "]"; 
53           switch (place) { 
54               case 1: 
55                   if (isEsc(c)) { 
56                       // t1 
57                       place = 2; 
58                       return null; 
59                   } 
60                   if (isTab(c)) { 
61                       // t2 
62                       place = 4; 
63                       return null; 
64                   } 
65                   return s; 
66               case 2: 
67                   if (isTab(c)) { 
68                       // t3 
69                       place = 3; 
70                       return null; 
71                   } 
72                   if (isEsc(c)) 
73                       return null; 
74                   // t4 
75                   place = 1; 
76                   ps = "[E-" + (char) c + "]"; 
77                   return (ps); 
78               case 3: 
79                   if (isTab(c)) 
80                       return null; 
81                   if (isEsc(c)) { 
82                       // t5 
83                       place = 1; 
84                       return null; 
85                   } 
86                   // t6 
87                   place = 1; 
88                   ps = "[E-T-" + (char) c + "]"; 
89                   return (ps); 
90               case 4: 
91                   if (isEsc(c)) { 
92                       // t7 
93                       place = 1; 
94                       return null; 
95                   } 
96                   if (isTab(c)) return null; 
97                   // t8 
98                   place = 1; 
99                   ps = "[T-" + (char) c + "]"; 
100                  return (ps); 
101          } 
102          ps = s; 
103          return ps; 
104      } 
105   
106      private boolean isEsc(int c) { 
107          return (c == 27); 
108      } 
109   
110      private boolean isTab(int c) { 
111          return (c == '\t'); 
112      } 
113   
114      public void processEvent(KeyEvent e) { 
115          int charValue = e.getKeyChar(); 
116          setPs(petriMap(charValue)); 
117          al.actionPerformed(new ActionEvent(e, 
118                  ActionEvent.ACTION_PERFORMED, charValue + "")); 
119      } 
120   
121      public String mapModifiers(KeyEvent e) { 
122          String modString = 
123                  KeyEvent.getKeyModifiersText( 
124                          e.getModifiers()); 
125          String newMods = ""; 
126          if (modString.indexOf("Ctrl") != -1) newMods += "^-"; 
127          if (modString.indexOf("Command") != -1) newMods += "A-"; 
128          if (modString.indexOf("Option") != -1) newMods += "C-"; 
129          return newMods; 
130          //Command+Ctrl+Option 
131      } 
132   
133      public String getShortCut(KeyEvent e) { 
134          int modifiers = e.getModifiers(); 
135          StringBuffer buf = new StringBuffer(); 
136          if ((modifiers & InputEvent.CTRL_MASK) != 0) { 
137              buf.append("^"); 
138              buf.append("+"); 
139          } 
140          if ((modifiers & InputEvent.META_MASK) != 0) { 
141              buf.append("Meta"); 
142              buf.append("+"); 
143          } 
144          if ((modifiers & InputEvent.ALT_MASK) != 0) { 
145              buf.append("@"); 
146              buf.append("+"); 
147          } 
148          return buf.toString(); 
149      } 
150   
151      public boolean matchEvent(AWTEvent e, MenuItem mi) { 
152          boolean b = matchSilent(e, mi); 
153          if (b) { 
154              System.out.println(mi.getLabel()); 
155          } 
156          return b; 
157      } 
158   
159      public boolean matchEvent(AWTEvent e, JMenuItem mi) { 
160          boolean b = matchSilent(e, mi); 
161          if (b) { 
162              System.out.println(mi.getText()); 
163          } 
164          return b; 
165      } 
166  } 
167