Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MIOSStudioDriver.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.KeyEvent;
26
import java.awt.event.WindowAdapter;
27
import java.awt.event.WindowEvent;
28
import java.util.Enumeration;
29
import java.util.Hashtable;
30
import java.util.prefs.Preferences;
31
 
32
import javax.swing.JApplet;
33
import javax.swing.JDialog;
34
import javax.swing.JFrame;
35
import javax.swing.JInternalFrame;
36
import javax.swing.JMenuItem;
37
import javax.swing.UIManager;
38
 
39
import org.midibox.apps.miosstudio.gui.MIOSStudioGUI;
40
import org.midibox.apps.miosstudio.gui.MIOSStudioGUI.ExternalCommandButton;
41
import org.midibox.midi.gui.SysexSendReceiveGUI;
42
import org.midibox.mios.gui.HexFileUploadGUI;
43
import org.midibox.utils.gui.DialogOwner;
44
import org.midibox.utils.gui.ImageLoader;
45
import org.midibox.utils.gui.SplashScreen;
46
 
633 adamjking 47
/**
48
 * Driver class for launching MIOS Studio application. MIOS Studio can be launched as an application or embedded in a a web page as an applet.
49
 * When launched as an application, the app shows a splash screen and is launched within its own frame.
50
 * Preferences are saved and restored from this class.
51
 *
52
 * @author Adam King (adamjking@optusnet.com.au)
53
 */
54
 
628 adamjking 55
public class MIOSStudio extends JApplet {
56
 
57
    protected static String frameTitle = "MIOS Studio";
58
 
641 adamjking 59
    protected static String splashTitle = "MIOS Studio beta8.3";
628 adamjking 60
 
61
    protected static String splashImage = "splash.jpg";
62
 
641 adamjking 63
    protected static String frameComment = "MIOS Studio beta8.3";
628 adamjking 64
 
65
    protected org.midibox.apps.miosstudio.MIOSStudio miosStudio;
66
 
67
    protected MIOSStudioGUI miosStudioGUI;
68
 
69
    protected Hashtable windows;
70
 
71
    public MIOSStudio() {
72
 
73
        this.miosStudio = new org.midibox.apps.miosstudio.MIOSStudio();
74
 
75
        Preferences preferences = getPreferences();
76
 
77
        try {
78
            UIManager.setLookAndFeel(preferences.get("lookAndFeel", UIManager
79
                    .getCrossPlatformLookAndFeelClassName()));
80
        } catch (Exception e) {
81
            System.out.println(e.toString());
82
        }
83
 
84
        JDialog.setDefaultLookAndFeelDecorated(preferences.getBoolean(
85
                "defaultDecoratedFrames", false));
86
 
87
        this.miosStudioGUI = new MIOSStudioGUI(miosStudio);
88
        miosStudioGUI.setCommentLabel(frameComment);
89
 
90
        this.windows = new Hashtable();
91
        setContentPane(miosStudioGUI);
92
        setJMenuBar(miosStudioGUI.createMenuBar());
93
    }
94
 
95
    public void init() {
96
 
97
        Preferences preferences = getPreferences();
98
 
99
        SysexSendReceiveGUI.currentDirectory = preferences.get(
100
                "sysexCurrentDirectory", SysexSendReceiveGUI.currentDirectory);
101
 
102
        HexFileUploadGUI.setCurrentDirectory(preferences.get(
103
                "uploadCurrentDirectory", HexFileUploadGUI
104
                        .getCurrentDirectory()));
105
 
106
        miosStudio.getHexFileUploadDeviceManager().setMIOS32_Mode(
107
                preferences.getBoolean("uploadMIOS32_Mode", false));
108
 
109
        String[] frames = preferences.get("visibleFrames", "").split(",");
110
        String[] ec = preferences.get("externalCommands", "").split("\n");
111
 
112
        createWindowsHashtable();
113
 
114
        Enumeration keys = windows.keys();
115
 
116
        int windowNo = 0;
117
 
118
        while (keys.hasMoreElements()) {
119
            String keyName = (String) keys.nextElement();
120
 
121
            JInternalFrame window = (JInternalFrame) windows.get(keyName);
122
 
123
            int x = preferences.getInt(keyName + "X", 20 * windowNo);
124
            int y = preferences.getInt(keyName + "Y", 20 * windowNo);
125
            int width = preferences.getInt(keyName + "Width", window
126
                    .getPreferredSize().width);
127
            int height = preferences.getInt(keyName + "Height", window
128
                    .getPreferredSize().height);
129
 
130
            Dimension d = window.getPreferredSize();
131
 
132
            if (window.isResizable()) {
133
                window.setBounds(x, y, Math.max(width, d.width), Math.max(
134
                        height, d.height));
135
            } else {
136
                window.setBounds(x, y, d.width, d.height);
137
            }
138
 
139
            if (preferences.getBoolean(keyName + "Visible", false)) {
140
                miosStudioGUI.showFrame(window);
141
                try {
142
                    window.setMaximum(preferences.getBoolean(keyName
143
                            + "Maximized", false));
144
                    window.setIcon(preferences.getBoolean(
145
                            keyName + "Iconified", false));
146
                } catch (Exception e) {
147
                    System.out.println(e.toString());
148
                }
149
            }
150
 
151
            windowNo++;
152
        }
153
 
154
        for (int i = frames.length - 1; i >= 0; i--) {
155
            if (windows.containsKey(frames[i])) {
156
                ((JInternalFrame) windows.get(frames[i])).toFront();
157
            }
158
        }
159
 
160
        for (int i = 0; i < ec.length; i++) {
161
            String[] temp = ec[i].split("\t");
162
            if (temp.length == 2) {
163
                miosStudioGUI.addExternalCommandButton(temp[0], temp[1]);
164
            }
165
        }
166
    }
167
 
168
    public void destroy() {
169
 
170
        Preferences preferences = getPreferences();
171
 
172
        preferences.put("lookAndFeel", miosStudioGUI.getLookAndFeel());
173
        preferences.putBoolean("defaultDecoratedFrames", miosStudioGUI
174
                .isDefaultDecoratedFrames());
175
 
176
        preferences.put("sysexCurrentDirectory",
177
                SysexSendReceiveGUI.currentDirectory);
178
 
179
        preferences.put("uploadCurrentDirectory", HexFileUploadGUI
180
                .getCurrentDirectory());
181
 
182
        preferences.putBoolean("uploadMIOS32_Mode", miosStudio
183
                .getHexFileUploadDeviceManager().isMIOS32_Mode());
184
 
185
        JInternalFrame[] frames = miosStudioGUI.getDesktop().getAllFrames();
186
        String visibleFrames = "";
187
 
188
        for (int i = 0; i < frames.length; i++) {
189
            Enumeration keys = windows.keys();
190
            while (keys.hasMoreElements()) {
191
                String key = (String) keys.nextElement();
192
                if (windows.get(key) == frames[i]) {
193
                    visibleFrames += (key + ",");
194
                }
195
            }
196
        }
197
 
198
        preferences.put("visibleFrames", visibleFrames);
199
 
200
        Enumeration keys = windows.keys();
201
        while (keys.hasMoreElements()) {
202
            String keyName = (String) keys.nextElement();
203
            JInternalFrame window = (JInternalFrame) windows.get(keyName);
204
 
205
            boolean visible = window.isVisible();
206
            try {
207
                preferences.putBoolean(keyName + "Iconified", window.isIcon());
208
                preferences.putBoolean(keyName + "Maximized", window
209
                        .isMaximum());
210
                window.setIcon(false);
211
                window.setMaximum(false);
212
            } catch (Exception e) {
213
                System.out.println(e);
214
            }
215
            preferences.putInt(keyName + "X", window.getX());
216
            preferences.putInt(keyName + "Y", window.getY());
217
            preferences.putInt(keyName + "Width", window.getWidth());
218
            preferences.putInt(keyName + "Height", window.getHeight());
219
            preferences.putBoolean(keyName + "Visible", visible);
220
        }
221
 
222
        String externalCommandsString = "";
223
 
224
        for (int i = 0; i < miosStudioGUI.getExternalCommands().size(); i++) {
225
            ExternalCommandButton ecb = (ExternalCommandButton) miosStudioGUI
226
                    .getExternalCommands().elementAt(i);
227
            externalCommandsString += ecb.commandName + "\t"
228
                    + ecb.externalCommand + "\n";
229
        }
230
        preferences.put("externalCommands", externalCommandsString);
231
    }
232
 
233
    public void exit(JFrame frame) {
234
 
235
        Preferences preferences = getPreferences();
236
 
237
        preferences.putInt("mainWindowX", frame.getX());
238
        preferences.putInt("mainWindowY", frame.getY());
239
        preferences.putInt("mainWindowWidth", frame.getWidth());
240
        preferences.putInt("mainWindowHeight", frame.getHeight());
241
        System.exit(0);
242
    }
243
 
244
    protected Preferences getPreferences() {
245
        return Preferences.userRoot().node("org/midibox/miostudio/gui");
246
    }
247
 
248
    protected void createWindowsHashtable() {
249
 
250
        windows.put("midiDevicesWindow", miosStudioGUI
251
                .getMidiDeviceRoutingWindow());
252
        windows.put("midiOutPortMonitorWindow", miosStudioGUI
253
                .getMidiOutPortMonitorWindow());
254
        windows.put("midiInPortMonitorWindow", miosStudioGUI
255
                .getMidiInPortMonitorWindow());
256
        windows.put("virtualKeyboardWindow", miosStudioGUI
257
                .getMidiKeyboardControllerWindow());
258
        /*
259
         * windows.put("sysexWindow", miosStudioGUI
260
         * .getSysexSendReceiveDeviceManagerWindow());
261
         */
262
 
263
        windows.put("uploadWindow", miosStudioGUI
264
                .getHexFileUploadDeviceManagerWindow());
265
        windows.put("lcdWindow", miosStudioGUI.getLcdMessageWindow());
266
        windows.put("debugWindow", miosStudioGUI.getDebugFunctionWindow());
267
        windows
268
                .put("miosTerminalWindow", miosStudioGUI
269
                        .getMIOSTerminalWindow());
270
        windows.put("helpWindow", miosStudioGUI.getHelpWindow());
271
 
272
    }
273
 
274
    public static void main(String[] args) {
275
        SplashScreen splashScreen = new SplashScreen(splashImage, splashTitle);
276
 
277
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
278
 
279
        splashScreen.setLocation(
280
                (screenSize.width - splashScreen.getWidth()) / 2,
281
                (screenSize.height - splashScreen.getHeight()) / 2);
282
 
283
        splashScreen.setVisible(true);
284
 
285
        Preferences preferences = Preferences.userRoot().node(
286
                "org/midibox/miostudio/gui");
287
 
288
        JFrame.setDefaultLookAndFeelDecorated(preferences.getBoolean(
289
                "defaultDecoratedFrames", false));
290
 
291
        final JFrame frame = new JFrame(frameTitle);
292
 
293
        DialogOwner.setFrame(frame);
294
 
295
        final MIOSStudio miosStudio = new MIOSStudio();
296
        miosStudio.init();
297
 
298
        frame.setContentPane(miosStudio);
299
        frame.setIconImage(ImageLoader.getImageIcon("ucIcon.png").getImage());
300
 
301
        frame.setBounds(preferences.getInt("mainWindowX", 50), preferences
302
                .getInt("mainWindowY", 50), preferences.getInt(
303
                "mainWindowWidth", (screenSize.width - 100)), preferences
304
                .getInt("mainWindowHeight", (screenSize.height - 100)));
305
 
306
        JMenuItem exitMenuItem = new JMenuItem("Exit");
307
        exitMenuItem.setMnemonic(KeyEvent.VK_X);
308
        exitMenuItem.setActionCommand("exit");
309
 
310
        exitMenuItem.addActionListener(new ActionListener() {
311
            public void actionPerformed(ActionEvent ae) {
312
                miosStudio.destroy();
313
                miosStudio.exit(frame);
314
            }
315
        });
316
 
317
        miosStudio.miosStudioGUI.getFileMenu().add(exitMenuItem);
318
 
319
        frame.addWindowListener(new WindowAdapter() {
320
            public void windowClosing(WindowEvent we) {
321
                miosStudio.destroy();
322
                miosStudio.exit(frame);
323
            }
324
        });
325
 
326
        frame.setVisible(true);
327
        splashScreen.setVisible(false);
328
    }
329
}