1   /*
2    * JIntellitype ----------------- Copyright 2005-2006 Emil A. Lefkof III
3    * 
4    * I always give it my best shot to make a program useful and solid, but remeber
5    * that there is absolutely no warranty for using this program as stated in the
6    * following terms:
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   */
20  package com.melloware;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Event;
24  import java.awt.Frame;
25  import java.awt.GraphicsEnvironment;
26  import java.awt.Point;
27  import java.awt.Rectangle;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  
31  import javax.swing.JButton;
32  import javax.swing.JFrame;
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextArea;
36  import javax.swing.border.EtchedBorder;
37  
38  import com.melloware.jintellitype.HotkeyListener;
39  import com.melloware.jintellitype.IntellitypeListener;
40  import com.melloware.jintellitype.JIntellitype;
41  
42  /**
43   * Swing based test application to test all the functions of the JIntellitype
44   * library.
45   * <p>
46   * Copyright (c) 2006 Melloware, Inc. <http://www.melloware.com>
47   * @author Emil A. Lefkof III <elefkof@ksmpartners.com>
48   * @version 1.0
49   */
50  public class JIntellitypeTester extends JFrame implements HotkeyListener, IntellitypeListener {
51  
52     private static JIntellitypeTester mainFrame;
53     private static final int WINDOWS_A = 88;
54     private static final int ALT_SHIFT_B = 89;
55     private static final int CTRL_SHIFT_C = 90;
56     private static final int PRINT_SCREEN = 91;
57     private static final int F9 = 92;
58     private static final int F12 = 93;
59     private JButton btnRegisterHotKey = new JButton();
60     private JButton btnUnregisterHotKey = new JButton();
61     private JPanel bottomPanel = new JPanel();
62     private JPanel mainPanel = new JPanel();
63     private JPanel topPanel = new JPanel();
64     private JScrollPane scrollPane = new JScrollPane();
65     private JTextArea textArea = new JTextArea();
66  
67     /**
68      * Creates new form.
69      */
70     public JIntellitypeTester() {
71        initComponents();
72     }
73  
74     /**
75      * Main method to launch this application.
76      * <p>
77      * @param args any command line arguments
78      */
79     public static void main(String[] args) {
80        // first check to see if an instance of this application is already
81        // running, use the name of the window title of this JFrame for checking
82        if (JIntellitype.checkInstanceAlreadyRunning("JIntellitype Test Application")) {
83           System.exit(1);
84        }
85        
86        // next check to make sure JIntellitype DLL can be found and we are on 
87        // a Windows operating System
88        if (!JIntellitype.isJIntellitypeSupported()) {
89           System.exit(1);
90        }
91  
92        mainFrame = new JIntellitypeTester();
93        mainFrame.setTitle("JIntellitype Test Application");
94        center(mainFrame);
95        mainFrame.setVisible(true);
96        mainFrame.initJIntellitype();
97     }
98  
99     /*
100     * (non-Javadoc)
101     * @see com.melloware.jintellitype.HotkeyListener#onHotKey(int)
102     */
103    public void onHotKey(int aIdentifier) {
104       output("WM_HOTKEY message received " + Integer.toString(aIdentifier));
105    }
106 
107    /*
108     * (non-Javadoc)
109     * @see com.melloware.jintellitype.IntellitypeListener#onIntellitype(int)
110     */
111    public void onIntellitype(int aCommand) {
112 
113       switch (aCommand) {
114       case JIntellitype.APPCOMMAND_BROWSER_BACKWARD:
115          output("BROWSER_BACKWARD message received " + Integer.toString(aCommand));
116          break;
117       case JIntellitype.APPCOMMAND_BROWSER_FAVOURITES:
118          output("BROWSER_FAVOURITES message received " + Integer.toString(aCommand));
119          break;
120       case JIntellitype.APPCOMMAND_BROWSER_FORWARD:
121          output("BROWSER_FORWARD message received " + Integer.toString(aCommand));
122          break;
123       case JIntellitype.APPCOMMAND_BROWSER_HOME:
124          output("BROWSER_HOME message received " + Integer.toString(aCommand));
125          break;
126       case JIntellitype.APPCOMMAND_BROWSER_REFRESH:
127          output("BROWSER_REFRESH message received " + Integer.toString(aCommand));
128          break;
129       case JIntellitype.APPCOMMAND_BROWSER_SEARCH:
130          output("BROWSER_SEARCH message received " + Integer.toString(aCommand));
131          break;
132       case JIntellitype.APPCOMMAND_BROWSER_STOP:
133          output("BROWSER_STOP message received " + Integer.toString(aCommand));
134          break;
135       case JIntellitype.APPCOMMAND_LAUNCH_APP1:
136          output("LAUNCH_APP1 message received " + Integer.toString(aCommand));
137          break;
138       case JIntellitype.APPCOMMAND_LAUNCH_APP2:
139          output("LAUNCH_APP2 message received " + Integer.toString(aCommand));
140          break;
141       case JIntellitype.APPCOMMAND_LAUNCH_MAIL:
142          output("LAUNCH_MAIL message received " + Integer.toString(aCommand));
143          break;
144       case JIntellitype.APPCOMMAND_MEDIA_NEXTTRACK:
145          output("MEDIA_NEXTTRACK message received " + Integer.toString(aCommand));
146          break;
147       case JIntellitype.APPCOMMAND_MEDIA_PLAY_PAUSE:
148          output("MEDIA_PLAY_PAUSE message received " + Integer.toString(aCommand));
149          break;
150       case JIntellitype.APPCOMMAND_MEDIA_PREVIOUSTRACK:
151          output("MEDIA_PREVIOUSTRACK message received " + Integer.toString(aCommand));
152          break;
153       case JIntellitype.APPCOMMAND_MEDIA_STOP:
154          output("MEDIA_STOP message received " + Integer.toString(aCommand));
155          break;
156       case JIntellitype.APPCOMMAND_VOLUME_DOWN:
157          output("VOLUME_DOWN message received " + Integer.toString(aCommand));
158          break;
159       case JIntellitype.APPCOMMAND_VOLUME_UP:
160          output("VOLUME_UP message received " + Integer.toString(aCommand));
161          break;
162       case JIntellitype.APPCOMMAND_VOLUME_MUTE:
163          output("VOLUME_MUTE message received " + Integer.toString(aCommand));
164          break;
165       default:
166          output("Undefined INTELLITYPE message caught " + Integer.toString(aCommand));
167          break;
168       }
169    }
170 
171    /**
172     * Centers window on desktop.
173     * <p>
174     * @param aFrame the Frame to center
175     */
176    private static void center(JFrame aFrame) {
177       final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
178       final Point centerPoint = ge.getCenterPoint();
179       final Rectangle bounds = ge.getMaximumWindowBounds();
180       final int w = Math.min(aFrame.getWidth(), bounds.width);
181       final int h = Math.min(aFrame.getHeight(), bounds.height);
182       final int x = centerPoint.x - (w / 2);
183       final int y = centerPoint.y - (h / 2);
184       aFrame.setBounds(x, y, w, h);
185       if ((w == bounds.width) && (h == bounds.height)) {
186          aFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
187       }
188       aFrame.validate();
189    }
190 
191    /**
192     * Method to register a hotkey using the RegisterHotKey Windows API call.
193     * <p>
194     * @param aEvent the ActionEvent fired.
195     */
196    private void btnRegisterHotKey_actionPerformed(ActionEvent aEvent) {
197       // assign the WINDOWS+A key to the unique id 88 for identification
198       JIntellitype.getInstance().registerHotKey(WINDOWS_A, JIntellitype.MOD_WIN, (int) 'A');
199       JIntellitype.getInstance().registerHotKey(ALT_SHIFT_B, JIntellitype.MOD_ALT + JIntellitype.MOD_SHIFT, (int) 'B');
200       JIntellitype.getInstance().registerSwingHotKey(CTRL_SHIFT_C, Event.CTRL_MASK + Event.SHIFT_MASK, (int) 'C');
201       
202       // use a 0 for the modifier if you just want a single keystroke to be a hotkey
203       JIntellitype.getInstance().registerHotKey(PRINT_SCREEN, 0, 44);
204       JIntellitype.getInstance().registerHotKey(F9, 0, 120);
205       JIntellitype.getInstance().registerHotKey(F12, JIntellitype.MOD_ALT, 123);
206       // clear the text area
207       textArea.setText("");
208       output("RegisterHotKey WINDOWS+A was assigned uniqueID 88");
209       output("RegisterHotKey ALT+SHIFT+B was assigned uniqueID 89");
210       output("RegisterHotKey CTRL+SHIFT+C was assigned uniqueID 90");
211       output("RegisterHotKey PRINT_SCREEN was assigned uniqueID 91");
212       output("RegisterHotKey F9 was assigned uniqueID 92");
213       output("RegisterHotKey F12 was assigned uniqueID 93");
214       output("Press WINDOWS+A or ALT+SHIFT+B or CTRL+SHIFT+C in another application and you will see the debug output in the textarea.");
215    }
216 
217    /**
218     * Method to unregister a hotkey using the UnregisterHotKey Windows API call.
219     * <p>
220     * @param aEvent the ActionEvent fired.
221     */
222    private void btnUnregisterHotKey_actionPerformed(ActionEvent aEvent) {
223       JIntellitype.getInstance().unregisterHotKey(WINDOWS_A);
224       JIntellitype.getInstance().unregisterHotKey(ALT_SHIFT_B);
225       JIntellitype.getInstance().unregisterHotKey(CTRL_SHIFT_C);
226       JIntellitype.getInstance().unregisterHotKey(PRINT_SCREEN);
227       JIntellitype.getInstance().unregisterHotKey(F9);
228       JIntellitype.getInstance().unregisterHotKey(F12);
229       output("UnregisterHotKey WINDOWS+A");
230       output("UnregisterHotKey ALT+SHIFT+B");
231       output("UnregisterHotKey CTRL+SHIFT+C");
232       output("UnregisterHotKey PRINT_SCREEN");
233       output("UnregisterHotKey F9");
234       output("Press WINDOWS+A or ALT+SHIFT+B in another application and you will NOT see the debug output in the textarea.");
235    }
236 
237    /**
238     * This method is called from within the constructor to initialize the form.
239     */
240    private void initComponents() {
241       mainPanel.setLayout(new BorderLayout());
242       topPanel.setBorder(new EtchedBorder(1));
243       bottomPanel.setLayout(new BorderLayout());
244       bottomPanel.setBorder(new EtchedBorder(1));
245       btnRegisterHotKey.setText("RegisterHotKey");
246       btnRegisterHotKey.addActionListener(new ActionListener() {
247          public void actionPerformed(ActionEvent e) {
248             btnRegisterHotKey_actionPerformed(e);
249          }
250       });
251       btnUnregisterHotKey.setText("UnregisterHotKey");
252       btnUnregisterHotKey.addActionListener(new ActionListener() {
253          public void actionPerformed(ActionEvent e) {
254             btnUnregisterHotKey_actionPerformed(e);
255          }
256       });
257       topPanel.add(btnRegisterHotKey);
258       topPanel.add(btnUnregisterHotKey);
259       scrollPane.getViewport().add(textArea);
260       bottomPanel.add(scrollPane, BorderLayout.CENTER);
261       mainPanel.add(topPanel, BorderLayout.NORTH);
262       mainPanel.add(bottomPanel, BorderLayout.CENTER);
263 
264       this.addWindowListener(new java.awt.event.WindowAdapter() {
265          public void windowClosing(java.awt.event.WindowEvent evt) {
266             // don't forget to clean up any resources before close
267             JIntellitype.getInstance().cleanUp();
268             System.exit(0);
269          }
270       });
271 
272       this.getContentPane().add(mainPanel);
273       this.pack();
274       this.setSize(800, 600);
275    }
276 
277    /**
278     * Initialize the JInitellitype library making sure the DLL is located.
279     * 
280     */
281    public void initJIntellitype() {
282       try {
283 
284          // initialize JIntellitype with the frame so all windows commands can
285          // be attached to this window
286          JIntellitype.getInstance().addHotKeyListener(this);
287          JIntellitype.getInstance().addIntellitypeListener(this);
288          output("JIntellitype initialized");
289       } catch (RuntimeException ex) {
290          output("Either you are not on Windows, or there is a problem with the JIntellitype library!");
291       }
292    }
293 
294    /**
295     * Send the output to the log and the text area.
296     * <p>
297     * @param text the text to output
298     */
299    private void output(String text) {
300       textArea.append(text);
301       textArea.append("\n");
302    }
303 
304 }