View Javadoc

1   /**
2    * .
3    */
4   package org.sirius.client.win32.classes;
5   
6   import org.apache.axis.types.UnsignedShort;
7   import org.sirius.client.win32.Win32Client;
8   import org.sirius.client.win32.core.constants.IMenuFlag;
9   
10  /**
11   * @author Myk Kolisnyk
12   * .
13   */
14  public class Menu implements IMenuFlag {
15  
16      /**
17       * .
18       */
19      public static final int STRING_MAXLEN = 255;
20  
21      /**
22       * .
23       */
24      private Win32Client client;
25      /**
26       * .
27       */
28      private Window      owner;
29      // Menu baseMenu;
30      /**
31       * .
32       */
33      private long        hmenu;
34  
35      /**
36       * .
37       * @param parent .
38       * @param text .
39       * @throws Exception .
40       */
41      public Menu(final Menu parent, final String text) throws Exception {
42          client = parent.getClient();
43          owner = parent.getOwner();
44  
45          Menu current = parent.menu(text);
46  
47          hmenu = current.getHmenu();
48      }
49  
50      /**
51       * .
52       * @param clientValue .
53       * @param ownerValue .
54       * @throws Exception .
55       */
56      public Menu(
57              final Win32Client clientValue,
58              final Window ownerValue)
59                      throws Exception {
60          this.client = clientValue;
61          this.owner = ownerValue;
62          if (ownerValue.exists()) {
63              hmenu = client.core().window().getMenu(ownerValue.getHwnd());
64          }
65          // baseMenu = null;
66      }
67  
68      /**
69       * .
70       * @param clientValue .
71       * @param ownerValue .
72       * @param hmenuValue .
73       */
74      public Menu(final Win32Client clientValue,
75              final Window ownerValue,
76              final long hmenuValue) {
77          this.client = clientValue;
78          this.owner = ownerValue;
79          this.hmenu = hmenuValue;
80      }
81  
82      /**
83       * @return the client
84       */
85      public final Win32Client getClient() {
86          return client;
87      }
88  
89      /**
90       * @return the hmenu
91       */
92      public final long getHmenu() {
93          return hmenu;
94      }
95  
96      /**
97       * .
98       * @return .
99       * @throws Exception .
100      */
101     public final String[] getItemNames() throws Exception {
102         int count = client.core().menu().getMenuItemCount(hmenu);
103         String[] names = new String[count];
104 
105         for (int i = 0; i < count; i++) {
106             UnsignedShort[] buf = new UnsignedShort[STRING_MAXLEN];
107             client.core().menu()
108                     .getMenuString(
109                             hmenu,
110                             i,
111                             buf,
112                             STRING_MAXLEN,
113                             (int) MF_BYPOSITION
114                     );
115             byte[] text = new byte[buf.length];
116 
117             for (int j = 0; j < buf.length; j++) {
118                 text[j] = buf[j].byteValue();
119             }
120             names[i] = new String(text);
121         }
122 
123         return names;
124     }
125 
126     /**
127      * .
128      * @param position .
129      * @return .
130      * @throws Exception .
131      */
132     public final String getMenuItemText(final int position)
133             throws Exception {
134         int maxLength = STRING_MAXLEN;
135 
136         UnsignedShort[] buffer = new UnsignedShort[maxLength];
137         client.core()
138                 .menu()
139                 .getMenuString(hmenu, position, buffer, maxLength,
140                         (int) MF_BYPOSITION);
141         String result = buffer.toString();
142 
143         return result;
144     }
145 
146     /**
147      * @return the owner
148      */
149     public final Window getOwner() {
150         return owner;
151     }
152 
153     /**
154      * .
155      * @param position .
156      * @return .
157      * @throws Exception .
158      */
159     public final Menu getSubMenu(final int position) throws Exception {
160         long subMenu = client.core().menu().getSubMenu(hmenu, position);
161         return new Menu(client, owner, subMenu);
162     }
163 
164     /**
165      * .
166      * @param title .
167      * @return .
168      * @throws Exception .
169      */
170     public final MenuItem item(final String title) throws Exception {
171         String[] names = this.getItemNames();
172         int itemNum = -1;
173         for (int i = 0; i < names.length; i++) {
174             if (names[i].matches(title) || names[i].contains(title)) {
175                 itemNum = i;
176                 break;
177             }
178         }
179         MenuItem item = new MenuItem(client, owner, hmenu, itemNum);
180         return item;
181     }
182 
183     /**
184      * .
185      * @param title .
186      * @return .
187      * @throws Exception .
188      */
189     public final Menu menu(final String title) throws Exception {
190         String[] names = this.getItemNames();
191         int item = -1;
192         for (int i = 0; i < names.length; i++) {
193             if (names[i].matches(title) || names[i].contains(title)) {
194                 item = i;
195                 break;
196             }
197         }
198         long subHMenu = client.core().menu().getSubMenu(hmenu, item);
199         Menu subMenu = new Menu(client, owner, subHMenu);
200         return subMenu;
201     }
202 
203     /**
204      * .
205      * @param position .
206      * @throws Exception .
207      */
208     public final void pick(final int position) throws Exception {
209         client.core().menu().pickItem(owner.getHwnd(), hmenu, position);
210     }
211 }