Subversion Repositories svn.mios

Rev

Rev 628 | Rev 741 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)VirtualKeyboardDriver.java   beta8   2006/04/23
3
 *
4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
import java.awt.Dimension;
22
import java.awt.Toolkit;
23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
25
import java.awt.event.ComponentAdapter;
26
import java.awt.event.ComponentEvent;
27
import java.awt.event.KeyEvent;
28
import java.awt.event.WindowAdapter;
29
import java.awt.event.WindowEvent;
738 adamjking 30
import java.io.File;
628 adamjking 31
import java.util.prefs.Preferences;
32
 
33
import javax.swing.JApplet;
34
import javax.swing.JDialog;
35
import javax.swing.JFrame;
36
import javax.swing.JMenuItem;
37
import javax.swing.UIManager;
38
 
39
import org.midibox.apps.virtualkeyboard.gui.VirtualKeyboardGUI;
738 adamjking 40
import org.midibox.apps.virtualkeyboard.xml.VirtualKeyboardXML;
628 adamjking 41
import org.midibox.utils.gui.ImageLoader;
42
 
43
public class VirtualKeyboard extends JApplet {
44
 
45
    final static Preferences preferences = Preferences.userRoot().node(
46
            "org/midibox/virtualkeyboard/gui");
47
 
738 adamjking 48
    final static String configFileName = ".virtualkeyboard";
49
 
628 adamjking 50
    private org.midibox.apps.virtualkeyboard.VirtualKeyboard virtualKeyboard;
51
 
52
    private VirtualKeyboardGUI virtualKeyboardGUI;
53
 
54
    public VirtualKeyboard() {
738 adamjking 55
 
628 adamjking 56
        virtualKeyboard = new org.midibox.apps.virtualkeyboard.VirtualKeyboard();
57
 
738 adamjking 58
        loadConfigFile();
59
 
628 adamjking 60
        try {
61
            UIManager.setLookAndFeel(preferences.get("lookAndFeel", UIManager
62
                    .getCrossPlatformLookAndFeelClassName()));
63
        } catch (Exception e) {
64
            System.out.println(e.toString());
65
        }
66
 
67
        JDialog.setDefaultLookAndFeelDecorated(preferences.getBoolean(
68
                "defaultDecoratedFrames", false));
69
 
70
        JFrame.setDefaultLookAndFeelDecorated(preferences.getBoolean(
71
                "defaultDecoratedFrames", false));
72
 
73
        virtualKeyboardGUI = new VirtualKeyboardGUI(virtualKeyboard);
74
 
75
        getContentPane().add(virtualKeyboardGUI);
76
 
77
        setJMenuBar(virtualKeyboardGUI.createMenuBar());
78
    }
79
 
80
    public void init() {
738 adamjking 81
 
628 adamjking 82
        virtualKeyboardGUI.setShowConnections(preferences.getBoolean(
83
                "showConnections", true));
84
    }
85
 
738 adamjking 86
    protected void loadConfigFile() {
87
 
88
        File configFile = new File(System.getProperty("user.home"),
89
                configFileName);
90
 
91
        if (configFile.exists()) {
92
 
93
            VirtualKeyboardXML virtualKeyboardXML = new VirtualKeyboardXML(
94
                    virtualKeyboard, VirtualKeyboardXML.TAG_ROOT_ELEMENT);
95
 
96
            virtualKeyboardXML.loadXML(configFile);
97
        }
98
    }
99
 
628 adamjking 100
    public void destroy() {
738 adamjking 101
 
102
        saveConfigFile();
103
 
628 adamjking 104
        preferences.put("lookAndFeel", virtualKeyboardGUI.getLookAndFeel());
105
 
106
        preferences.putBoolean("defaultDecoratedFrames", virtualKeyboardGUI
107
                .isDefaultDecoratedFrames());
108
 
109
        preferences.putBoolean("showConnections", virtualKeyboardGUI
110
                .isShowConnections());
111
    }
112
 
738 adamjking 113
    protected void saveConfigFile() {
114
 
115
        File configFile = new File(System.getProperty("user.home"),
116
                configFileName);
117
 
118
        if (!configFile.exists()) {
119
            try {
120
                configFile.createNewFile();
121
            } catch (Exception e) {
122
                e.printStackTrace();
123
            }
124
        }
125
 
126
        if (configFile.exists()) {
127
 
128
            VirtualKeyboardXML virtualKeyboardXML = new VirtualKeyboardXML(
129
                    virtualKeyboard, VirtualKeyboardXML.TAG_ROOT_ELEMENT);
130
 
131
            virtualKeyboardXML.saveXML(configFile);
132
        }
133
    }
134
 
628 adamjking 135
    public static void exit(JFrame frame) {
738 adamjking 136
 
628 adamjking 137
        preferences.putInt("mainWindowX", frame.getX());
138
        preferences.putInt("mainWindowY", frame.getY());
738 adamjking 139
 
628 adamjking 140
        System.exit(0);
141
    }
142
 
143
    public static void main(String[] args) {
144
 
145
        final VirtualKeyboard virtualKeyboardGUIDriver = new VirtualKeyboard();
738 adamjking 146
 
628 adamjking 147
        virtualKeyboardGUIDriver.init();
148
 
149
        final JFrame frame = new JFrame("Virtual Keyboard");
738 adamjking 150
 
151
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
152
 
628 adamjking 153
        frame.setContentPane(virtualKeyboardGUIDriver);
154
 
155
        frame.setIconImage(ImageLoader.getImageIcon("piano.png").getImage());
156
        frame.pack();
157
        frame.setResizable(false);
158
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
159
 
160
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
161
 
162
        frame.setLocation(preferences.getInt("mainWindowX", 50), preferences
163
                .getInt("mainWindowY", 50));
164
 
165
        frame.setLocation(preferences.getInt("windowX", 50), preferences
166
                .getInt("windowY", 50));
167
 
168
        JMenuItem exitMenuItem = new JMenuItem("Exit");
169
        exitMenuItem.setMnemonic(KeyEvent.VK_X);
170
        exitMenuItem.setActionCommand("exit");
171
 
172
        exitMenuItem.addActionListener(new ActionListener() {
173
            public void actionPerformed(ActionEvent ae) {
174
                virtualKeyboardGUIDriver.destroy();
175
                exit(frame);
176
            }
177
        });
178
 
179
        virtualKeyboardGUIDriver.virtualKeyboardGUI.getFileMenu().add(
180
                exitMenuItem);
181
 
182
        virtualKeyboardGUIDriver.virtualKeyboardGUI.getMidiDeviceRoutingGUI()
183
                .addComponentListener(new ComponentAdapter() {
184
                    public void componentShown(ComponentEvent arg0) {
185
                        frame.pack();
186
                    }
187
 
188
                    public void componentHidden(ComponentEvent arg0) {
189
                        frame.pack();
190
                    }
191
                });
192
 
193
        frame.addWindowListener(new WindowAdapter() {
194
            public void windowClosing(WindowEvent we) {
195
                virtualKeyboardGUIDriver.destroy();
196
                exit(frame);
197
            }
198
        });
199
 
200
        frame.setVisible(true);
201
    }
202
}