Subversion Repositories svn.mios

Rev

Rev 741 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
628 adamjking 1
/*
741 adamjking 2
 * @(#)VirtualKeyboard.java beta8   2006/04/23
628 adamjking 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
 
32
import javax.swing.JApplet;
33
import javax.swing.JFrame;
34
import javax.swing.JMenuItem;
35
 
36
import org.midibox.apps.virtualkeyboard.gui.VirtualKeyboardGUI;
741 adamjking 37
import org.midibox.apps.virtualkeyboard.gui.xml.VirtualKeyboardGUIXML;
38
import org.midibox.utils.gui.DialogOwner;
628 adamjking 39
import org.midibox.utils.gui.ImageLoader;
40
 
41
public class VirtualKeyboard extends JApplet {
42
 
762 adamjking 43
    final static String oldConfigFileName = ".virtualkeyboard";
738 adamjking 44
 
762 adamjking 45
    final static String configDirectoryName = ".midibox";
46
 
47
    final static String configFileName = "virtualkeyboard.xml";
48
 
628 adamjking 49
    private org.midibox.apps.virtualkeyboard.VirtualKeyboard virtualKeyboard;
50
 
51
    private VirtualKeyboardGUI virtualKeyboardGUI;
52
 
53
    public VirtualKeyboard() {
738 adamjking 54
 
628 adamjking 55
        virtualKeyboard = new org.midibox.apps.virtualkeyboard.VirtualKeyboard();
56
 
762 adamjking 57
        File configDirectory = new File(System.getProperty("user.home"),
58
                configDirectoryName);
738 adamjking 59
 
762 adamjking 60
        File configFile = new File(configDirectory, configFileName);
61
 
741 adamjking 62
        if (configFile.exists()) {
628 adamjking 63
 
741 adamjking 64
            VirtualKeyboardGUIXML virtualKeyboardGUIXML = new VirtualKeyboardGUIXML(
65
                    virtualKeyboard, VirtualKeyboardGUIXML.TAG_ROOT_ELEMENT);
628 adamjking 66
 
741 adamjking 67
            virtualKeyboardGUIXML.loadXML(configFile);
762 adamjking 68
 
741 adamjking 69
            virtualKeyboardGUI = virtualKeyboardGUIXML.getVirtualKeyboardGUI();
762 adamjking 70
 
71
        } else {
72
 
73
            // check for old config file
74
 
75
            configFile = new File(System.getProperty("user.home"),
76
                    oldConfigFileName);
77
 
78
            if (configFile.exists()) {
79
 
80
                VirtualKeyboardGUIXML virtualKeyboardGUIXML = new VirtualKeyboardGUIXML(
81
                        virtualKeyboard, VirtualKeyboardGUIXML.TAG_ROOT_ELEMENT);
82
 
83
                virtualKeyboardGUIXML.loadXML(configFile);
84
 
85
                virtualKeyboardGUI = virtualKeyboardGUIXML
86
                        .getVirtualKeyboardGUI();
87
 
88
                configFile.delete();
89
            }
741 adamjking 90
        }
762 adamjking 91
 
92
        if (virtualKeyboardGUI == null) {
741 adamjking 93
            virtualKeyboardGUI = new VirtualKeyboardGUI(virtualKeyboard);
94
        }
762 adamjking 95
 
628 adamjking 96
        getContentPane().add(virtualKeyboardGUI);
97
 
98
        setJMenuBar(virtualKeyboardGUI.createMenuBar());
99
    }
100
 
101
    public void init() {
738 adamjking 102
 
628 adamjking 103
    }
104
 
105
    public void destroy() {
738 adamjking 106
 
762 adamjking 107
        File configDirectory = new File(System.getProperty("user.home"),
108
                configDirectoryName);
109
 
110
        File configFile = new File(configDirectory, configFileName);
111
 
112
        if (!configDirectory.exists()) {
113
 
114
            try {
115
 
116
                configDirectory.mkdir();
117
 
118
            } catch (Exception e) {
119
 
120
                e.printStackTrace();
121
            }
122
        }
123
 
738 adamjking 124
        if (!configFile.exists()) {
762 adamjking 125
 
738 adamjking 126
            try {
762 adamjking 127
 
738 adamjking 128
                configFile.createNewFile();
762 adamjking 129
 
738 adamjking 130
            } catch (Exception e) {
762 adamjking 131
 
738 adamjking 132
                e.printStackTrace();
133
            }
134
        }
135
 
136
        if (configFile.exists()) {
137
 
741 adamjking 138
            VirtualKeyboardGUIXML virtualKeyboardGUIXML = new VirtualKeyboardGUIXML(
139
                    virtualKeyboardGUI, VirtualKeyboardGUIXML.TAG_ROOT_ELEMENT);
738 adamjking 140
 
741 adamjking 141
            virtualKeyboardGUIXML.saveXML(configFile);
738 adamjking 142
        }
143
 
628 adamjking 144
    }
145
 
146
    public static void main(String[] args) {
147
 
148
        final JFrame frame = new JFrame("Virtual Keyboard");
762 adamjking 149
 
741 adamjking 150
        DialogOwner.setFrame(frame);
738 adamjking 151
 
152
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
762 adamjking 153
 
741 adamjking 154
        final VirtualKeyboard virtualKeyboardGUI = new VirtualKeyboard();
738 adamjking 155
 
741 adamjking 156
        virtualKeyboardGUI.init();
628 adamjking 157
 
741 adamjking 158
        frame.setContentPane(virtualKeyboardGUI);
159
 
628 adamjking 160
        frame.setIconImage(ImageLoader.getImageIcon("piano.png").getImage());
161
        frame.pack();
162
        frame.setResizable(false);
163
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
164
 
165
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
166
 
167
        JMenuItem exitMenuItem = new JMenuItem("Exit");
168
        exitMenuItem.setMnemonic(KeyEvent.VK_X);
169
        exitMenuItem.setActionCommand("exit");
170
 
171
        exitMenuItem.addActionListener(new ActionListener() {
172
            public void actionPerformed(ActionEvent ae) {
762 adamjking 173
 
741 adamjking 174
                virtualKeyboardGUI.destroy();
175
 
176
                System.exit(0);
628 adamjking 177
            }
178
        });
179
 
762 adamjking 180
        virtualKeyboardGUI.virtualKeyboardGUI.getFileMenu().add(exitMenuItem);
628 adamjking 181
 
741 adamjking 182
        virtualKeyboardGUI.virtualKeyboardGUI.getMidiDeviceRoutingGUI()
628 adamjking 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) {
762 adamjking 195
 
741 adamjking 196
                virtualKeyboardGUI.destroy();
197
 
198
                System.exit(0);
628 adamjking 199
            }
200
        });
201
 
202
        frame.setVisible(true);
203
    }
204
}