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 static final int SEMICOLON = 94;
60     private JButton btnRegisterHotKey = new JButton();
61     private JButton btnUnregisterHotKey = new JButton();
62     private JPanel bottomPanel = new JPanel();
63     private JPanel mainPanel = new JPanel();
64     private JPanel topPanel = new JPanel();
65     private JScrollPane scrollPane = new JScrollPane();
66     private JTextArea textArea = new JTextArea();
67  
68     /**
69      * Creates new form.
70      */
71     public JIntellitypeTester() {
72        initComponents();
73     }
74  
75     /**
76      * Main method to launch this application.
77      * <p>
78      * @param args any command line arguments
79      */
80     public static void main(String[] args) {
81        // first check to see if an instance of this application is already
82        // running, use the name of the window title of this JFrame for checking
83        if (JIntellitype.checkInstanceAlreadyRunning("JIntellitype Test Application")) {
84           System.exit(1);
85        }
86        
87        // next check to make sure JIntellitype DLL can be found and we are on 
88        // a Windows operating System
89        if (!JIntellitype.isJIntellitypeSupported()) {
90           System.exit(1);
91        }
92  
93        mainFrame = new JIntellitypeTester();
94        mainFrame.setTitle("JIntellitype Test Application");
95        center(mainFrame);
96        mainFrame.setVisible(true);
97        mainFrame.initJIntellitype();
98     }
99  
100    /*
101     * (non-Javadoc)
102     * @see com.melloware.jintellitype.HotkeyListener#onHotKey(int)
103     */
104    public void onHotKey(int aIdentifier) {
105       output("WM_HOTKEY message received " + Integer.toString(aIdentifier));
106    }
107 
108    /*
109     * (non-Javadoc)
110     * @see com.melloware.jintellitype.IntellitypeListener#onIntellitype(int)
111     */
112    public void onIntellitype(int aCommand) {
113 
114       switch (aCommand) {
115       case JIntellitype.APPCOMMAND_BROWSER_BACKWARD:
116          output("BROWSER_BACKWARD message received " + Integer.toString(aCommand));
117          break;
118       case JIntellitype.APPCOMMAND_BROWSER_FAVOURITES:
119          output("BROWSER_FAVOURITES message received " + Integer.toString(aCommand));
120          break;
121       case JIntellitype.APPCOMMAND_BROWSER_FORWARD:
122          output("BROWSER_FORWARD message received " + Integer.toString(aCommand));
123          break;
124       case JIntellitype.APPCOMMAND_BROWSER_HOME:
125          output("BROWSER_HOME message received " + Integer.toString(aCommand));
126          break;
127       case JIntellitype.APPCOMMAND_BROWSER_REFRESH:
128          output("BROWSER_REFRESH message received " + Integer.toString(aCommand));
129          break;
130       case JIntellitype.APPCOMMAND_BROWSER_SEARCH:
131          output("BROWSER_SEARCH message received " + Integer.toString(aCommand));
132          break;
133       case JIntellitype.APPCOMMAND_BROWSER_STOP:
134          output("BROWSER_STOP message received " + Integer.toString(aCommand));
135          break;
136       case JIntellitype.APPCOMMAND_LAUNCH_APP1:
137          output("LAUNCH_APP1 message received " + Integer.toString(aCommand));
138          break;
139       case JIntellitype.APPCOMMAND_LAUNCH_APP2:
140          output("LAUNCH_APP2 message received " + Integer.toString(aCommand));
141          break;
142       case JIntellitype.APPCOMMAND_LAUNCH_MAIL:
143          output("LAUNCH_MAIL message received " + Integer.toString(aCommand));
144          break;
145       case JIntellitype.APPCOMMAND_MEDIA_NEXTTRACK:
146          output("MEDIA_NEXTTRACK message received " + Integer.toString(aCommand));
147          break;
148       case JIntellitype.APPCOMMAND_MEDIA_PLAY_PAUSE:
149          output("MEDIA_PLAY_PAUSE message received " + Integer.toString(aCommand));
150          break;
151       case JIntellitype.APPCOMMAND_MEDIA_PREVIOUSTRACK:
152          output("MEDIA_PREVIOUSTRACK message received " + Integer.toString(aCommand));
153          break;
154       case JIntellitype.APPCOMMAND_MEDIA_STOP:
155          output("MEDIA_STOP message received " + Integer.toString(aCommand));
156          break;
157       case JIntellitype.APPCOMMAND_VOLUME_DOWN:
158          output("VOLUME_DOWN message received " + Integer.toString(aCommand));
159          break;
160       case JIntellitype.APPCOMMAND_VOLUME_UP:
161          output("VOLUME_UP message received " + Integer.toString(aCommand));
162          break;
163       case JIntellitype.APPCOMMAND_VOLUME_MUTE:
164          output("VOLUME_MUTE message received " + Integer.toString(aCommand));
165          break;
166       default:
167          output("Undefined INTELLITYPE message caught " + Integer.toString(aCommand));
168          break;
169       }
170    }
171 
172    /**
173     * Centers window on desktop.
174     * <p>
175     * @param aFrame the Frame to center
176     */
177    private static void center(JFrame aFrame) {
178       final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
179       final Point centerPoint = ge.getCenterPoint();
180       final Rectangle bounds = ge.getMaximumWindowBounds();
181       final int w = Math.min(aFrame.getWidth(), bounds.width);
182       final int h = Math.min(aFrame.getHeight(), bounds.height);
183       final int x = centerPoint.x - (w / 2);
184       final int y = centerPoint.y - (h / 2);
185       aFrame.setBounds(x, y, w, h);
186       if ((w == bounds.width) && (h == bounds.height)) {
187          aFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
188       }
189       aFrame.validate();
190    }
191 
192    /**
193     * Method to register a hotkey using the RegisterHotKey Windows API call.
194     * <p>
195     * @param aEvent the ActionEvent fired.
196     */
197    private void btnRegisterHotKey_actionPerformed(ActionEvent aEvent) {
198       // assign the WINDOWS+A key to the unique id 88 for identification
199       JIntellitype.getInstance().registerHotKey(WINDOWS_A, JIntellitype.MOD_WIN, (int) 'A');
200       JIntellitype.getInstance().registerHotKey(ALT_SHIFT_B, JIntellitype.MOD_ALT + JIntellitype.MOD_SHIFT, (int) 'B');
201       JIntellitype.getInstance().registerSwingHotKey(CTRL_SHIFT_C, Event.CTRL_MASK + Event.SHIFT_MASK, (int) 'C');
202       
203       // use a 0 for the modifier if you just want a single keystroke to be a hotkey
204       JIntellitype.getInstance().registerHotKey(PRINT_SCREEN, 0, 44);
205       JIntellitype.getInstance().registerHotKey(F9, 0, 120);
206       JIntellitype.getInstance().registerHotKey(F12, JIntellitype.MOD_ALT, 123);
207       JIntellitype.getInstance().registerHotKey(SEMICOLON, 0, 186);
208       // clear the text area
209       textArea.setText("");
210       output("RegisterHotKey WINDOWS+A was assigned uniqueID 88");
211       output("RegisterHotKey ALT+SHIFT+B was assigned uniqueID 89");
212       output("RegisterHotKey CTRL+SHIFT+C was assigned uniqueID 90");
213       output("RegisterHotKey PRINT_SCREEN was assigned uniqueID 91");
214       output("RegisterHotKey F9 was assigned uniqueID 92");
215       output("RegisterHotKey F12 was assigned uniqueID 93");
216       output("RegisterHotKey SEMICOLON was assigned uniqueID 94");
217       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.");
218    }
219 
220    /**
221     * Method to unregister a hotkey using the UnregisterHotKey Windows API call.
222     * <p>
223     * @param aEvent the ActionEvent fired.
224     */
225    private void btnUnregisterHotKey_actionPerformed(ActionEvent aEvent) {
226       JIntellitype.getInstance().unregisterHotKey(WINDOWS_A);
227       JIntellitype.getInstance().unregisterHotKey(ALT_SHIFT_B);
228       JIntellitype.getInstance().unregisterHotKey(CTRL_SHIFT_C);
229       JIntellitype.getInstance().unregisterHotKey(PRINT_SCREEN);
230       JIntellitype.getInstance().unregisterHotKey(F9);
231       JIntellitype.getInstance().unregisterHotKey(F12);
232       JIntellitype.getInstance().unregisterHotKey(SEMICOLON);
233       output("UnregisterHotKey WINDOWS+A");
234       output("UnregisterHotKey ALT+SHIFT+B");
235       output("UnregisterHotKey CTRL+SHIFT+C");
236       output("UnregisterHotKey PRINT_SCREEN");
237       output("UnregisterHotKey F9");
238       output("UnregisterHotKey F12");
239       output("UnregisterHotKey SEMICOLON");
240       output("Press WINDOWS+A or ALT+SHIFT+B in another application and you will NOT see the debug output in the textarea.");
241    }
242 
243    /**
244     * This method is called from within the constructor to initialize the form.
245     */
246    private void initComponents() {
247       mainPanel.setLayout(new BorderLayout());
248       topPanel.setBorder(new EtchedBorder(1));
249       bottomPanel.setLayout(new BorderLayout());
250       bottomPanel.setBorder(new EtchedBorder(1));
251       btnRegisterHotKey.setText("RegisterHotKey");
252       btnRegisterHotKey.addActionListener(new ActionListener() {
253          public void actionPerformed(ActionEvent e) {
254             btnRegisterHotKey_actionPerformed(e);
255          }
256       });
257       btnUnregisterHotKey.setText("UnregisterHotKey");
258       btnUnregisterHotKey.addActionListener(new ActionListener() {
259          public void actionPerformed(ActionEvent e) {
260             btnUnregisterHotKey_actionPerformed(e);
261          }
262       });
263       topPanel.add(btnRegisterHotKey);
264       topPanel.add(btnUnregisterHotKey);
265       scrollPane.getViewport().add(textArea);
266       bottomPanel.add(scrollPane, BorderLayout.CENTER);
267       mainPanel.add(topPanel, BorderLayout.NORTH);
268       mainPanel.add(bottomPanel, BorderLayout.CENTER);
269 
270       this.addWindowListener(new java.awt.event.WindowAdapter() {
271          public void windowClosing(java.awt.event.WindowEvent evt) {
272             // don't forget to clean up any resources before close
273             JIntellitype.getInstance().cleanUp();
274             System.exit(0);
275          }
276       });
277 
278       this.getContentPane().add(mainPanel);
279       this.pack();
280       this.setSize(800, 600);
281    }
282 
283    /**
284     * Initialize the JInitellitype library making sure the DLL is located.
285     * 
286     */
287    public void initJIntellitype() {
288       try {
289 
290          // initialize JIntellitype with the frame so all windows commands can
291          // be attached to this window
292          JIntellitype.getInstance().addHotKeyListener(this);
293          JIntellitype.getInstance().addIntellitypeListener(this);
294          output("JIntellitype initialized");
295       } catch (RuntimeException ex) {
296          output("Either you are not on Windows, or there is a problem with the JIntellitype library!");
297       }
298    }
299 
300    /**
301     * Send the output to the log and the text area.
302     * <p>
303     * @param text the text to output
304     */
305    private void output(String text) {
306       textArea.append(text);
307       textArea.append("\n");
308    }
309 
310 }