Subversion Repositories svn.mios

Rev

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