Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)SysexSendReceiveGUI.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
package org.midibox.midi.gui;
22
 
23
import java.awt.BorderLayout;
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Dimension;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Insets;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.ItemEvent;
33
import java.awt.event.ItemListener;
34
import java.io.File;
35
import java.util.Observable;
36
import java.util.Observer;
37
 
38
import javax.swing.BorderFactory;
39
import javax.swing.JButton;
40
import javax.swing.JFileChooser;
41
import javax.swing.JLabel;
42
import javax.swing.JPanel;
43
import javax.swing.JScrollPane;
44
import javax.swing.JSpinner;
45
import javax.swing.JTextField;
46
import javax.swing.JTextPane;
47
import javax.swing.SpinnerNumberModel;
48
import javax.swing.event.ChangeEvent;
49
import javax.swing.event.ChangeListener;
50
import javax.swing.text.Style;
51
import javax.swing.text.StyleConstants;
52
import javax.swing.text.StyleContext;
53
import javax.swing.text.StyledDocument;
54
 
55
import org.midibox.midi.MidiUtils;
56
import org.midibox.midi.SysexSendReceive;
57
import org.midibox.utils.gui.ImageLoader;
58
import org.midibox.utils.gui.SimpleFileChooserFilter;
59
 
60
public class SysexSendReceiveGUI extends JPanel implements ActionListener,
61
        ItemListener, ChangeListener, Observer {
62
 
800 adamjking 63
    private static String currentDirectory = "";
628 adamjking 64
 
65
    private static JFileChooser fc = null;
66
 
67
    private SysexSendReceive sysexSendReceive;
68
 
69
    // GUI components
70
 
71
    private JButton browseButton;
72
 
73
    private JTextField fileName;
74
 
75
    private JButton startButton;
76
 
77
    private JButton stopButton;
78
 
79
    private JTextPane sysexSendTextArea;
80
 
81
    private JTextPane sysexReceiveTextArea;
82
 
83
    private JSpinner sendBufferSizeSpinner;
84
 
85
    public SysexSendReceiveGUI(SysexSendReceive sysexSendReceive) {
86
        super(new BorderLayout());
87
 
88
        this.sysexSendReceive = sysexSendReceive;
89
        sysexSendReceive.addObserver(this);
90
 
91
        browseButton = new JButton("SysEx File", ImageLoader
800 adamjking 92
                .getImageIcon("open.png"));
628 adamjking 93
        fileName = new JTextField();
94
        fileName.setEditable(false);
95
        browseButton.setActionCommand("browse");
96
        browseButton.addActionListener(this);
97
 
98
        startButton = new JButton("Send/Receive");
99
        startButton.setActionCommand("start");
100
        startButton.addActionListener(this);
101
        stopButton = new JButton("Stop");
102
        stopButton.addActionListener(this);
103
        stopButton.setActionCommand("stop");
104
 
105
        startButton.setAlignmentX(Component.LEFT_ALIGNMENT);
106
        stopButton.setAlignmentX(Component.LEFT_ALIGNMENT);
107
 
108
        JPanel fileButtonsPanel = new JPanel(new BorderLayout(2, 2));
109
        fileButtonsPanel.add(browseButton, BorderLayout.WEST);
110
        fileButtonsPanel.add(fileName, BorderLayout.CENTER);
111
        fileButtonsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
112
 
113
        JPanel controlButtonsPanel = new JPanel(new GridBagLayout());
114
 
115
        GridBagConstraints gbc = new GridBagConstraints();
116
        gbc.gridx = 0;
117
        gbc.gridy = 0;
118
        gbc.gridwidth = 1;
119
        gbc.gridheight = 1;
120
 
121
        gbc.insets = new Insets(5, 5, 5, 5);
122
 
123
        controlButtonsPanel.add(startButton, gbc);
124
        gbc.gridx++;
125
 
126
        gbc.insets = new Insets(5, 13, 5, 5);
127
 
128
        controlButtonsPanel.add(stopButton, gbc);
129
        gbc.gridx++;
130
 
131
        gbc.insets = new Insets(2, 2, 2, 2);
132
 
133
        gbc.weightx = 1.0;
134
        controlButtonsPanel.add(new JPanel(), gbc);
135
        gbc.gridx++;
136
        gbc.weightx = 0.0;
137
 
138
        gbc.fill = GridBagConstraints.NONE;
139
 
140
        JLabel label = new JLabel("Send Buffer Size (bytes): ");
141
        controlButtonsPanel.add(label, gbc);
142
        gbc.gridx++;
143
 
144
        sendBufferSizeSpinner = new JSpinner(new SpinnerNumberModel(
145
                sysexSendReceive.getSendBufferSize(), 1, 1000, 1));
146
        sendBufferSizeSpinner.addChangeListener(this);
147
        controlButtonsPanel.add(sendBufferSizeSpinner, gbc);
148
 
149
        JPanel sendPanel = new JPanel(new GridBagLayout());
150
 
151
        gbc.fill = GridBagConstraints.BOTH;
152
        gbc.weightx = 1.0;
153
 
154
        sendPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
155
                .createEmptyBorder(2, 2, 2, 2), BorderFactory
156
                .createTitledBorder("Send")));
157
 
158
        sysexSendTextArea = new JTextPane();
159
        sysexSendTextArea.setBackground(Color.WHITE);
160
        sysexSendTextArea.setEditable(false);
161
 
162
        StyledDocument doc = sysexSendTextArea.getStyledDocument();
163
 
164
        Style def = StyleContext.getDefaultStyleContext().getStyle(
165
                StyleContext.DEFAULT_STYLE);
166
 
167
        doc.addStyle("regular", def);
168
        StyleConstants.setFontFamily(def, "Monospaced");
169
 
170
        JScrollPane sysexSendTextAreaScrollPane = new JScrollPane(
171
                sysexSendTextArea);
172
        sysexSendTextAreaScrollPane
173
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
174
 
175
        // Make preferred width smaller than the other panel's minimum width.
176
        sysexSendTextAreaScrollPane.setPreferredSize(new Dimension(300, 100));
177
 
178
        sendPanel.add(fileButtonsPanel, gbc);
179
        gbc.gridy++;
180
        gbc.weighty = 1.0;
181
        gbc.insets = new Insets(5, 5, 5, 5);
182
        sendPanel.add(sysexSendTextAreaScrollPane, gbc);
183
 
184
        JPanel receivePanel = new JPanel(new GridBagLayout());
185
        receivePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
186
                .createEmptyBorder(2, 2, 2, 2), BorderFactory
187
                .createTitledBorder("Receive")));
188
 
189
        sysexReceiveTextArea = new JTextPane();
190
        sysexReceiveTextArea.setBackground(Color.WHITE);
191
 
192
        sysexReceiveTextArea.setEditable(false);
193
        JScrollPane sysexReceiveTextAreaScrollPane = new JScrollPane(
194
                sysexReceiveTextArea);
195
        sysexReceiveTextAreaScrollPane
196
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
197
 
198
        // Make preferred width smaller than the other panel's minimum width.
199
        sysexReceiveTextAreaScrollPane
200
                .setPreferredSize(new Dimension(300, 100));
201
 
202
        gbc.gridy = 0;
203
        receivePanel.add(sysexReceiveTextAreaScrollPane, gbc);
204
 
205
        JPanel mainPanel = new JPanel(new GridBagLayout());
206
 
207
        gbc.gridx = 0;
208
        gbc.gridy = 0;
209
        gbc.insets = new Insets(4, 4, 4, 4);
210
 
211
        mainPanel.add(controlButtonsPanel, gbc);
212
        gbc.gridy++;
213
 
214
        gbc.weighty = 1.0;
215
        mainPanel.add(sendPanel, gbc);
216
        gbc.gridy++;
217
        mainPanel.add(receivePanel, gbc);
218
 
800 adamjking 219
        add(controlButtonsPanel, BorderLayout.NORTH);
220
        add(mainPanel, BorderLayout.CENTER);
628 adamjking 221
 
222
        UIUpdate();
223
    }
224
 
225
    public void UIUpdate() {
226
 
227
    }
228
 
229
    public void actionPerformed(ActionEvent e) {
230
        if (e.getActionCommand() == "start") {
231
 
232
        } else if (e.getActionCommand() == "stop") {
233
 
234
        } else if (e.getActionCommand() == "browse") {
235
            onOpenSysexFile();
236
        }
237
    }
238
 
239
    protected void onOpenSysexFile() {
240
 
241
        if (fc == null) {
242
            fc = new JFileChooser(currentDirectory);
243
            fc.addChoosableFileFilter(new SimpleFileChooserFilter(
244
                    "SysEx files (*.syx)", "syx", "Sysex File"));
245
        }
246
 
247
        File noFile = new File("");
248
        File noFiles[] = { noFile };
249
        fc.setSelectedFile(noFile);
250
        fc.setSelectedFiles(noFiles);
251
 
252
        int nRetVal = fc.showOpenDialog(this);
253
 
254
        if (nRetVal == JFileChooser.APPROVE_OPTION) {
255
            File file = fc.getSelectedFile();
256
            sysexSendReceive.setFile(file);
257
            currentDirectory = fc.getCurrentDirectory().toString();
258
        }
259
    }
260
 
261
    public void itemStateChanged(ItemEvent e) {
262
        UIUpdate();
263
    }
264
 
265
    public void stateChanged(ChangeEvent e) {
266
 
267
        UIUpdate();
268
    }
269
 
270
    public void update(Observable observable, Object object) {
271
        if (observable == sysexSendReceive) {
272
            if (object == sysexSendReceive.getFile()) {
273
                sysexSendTextArea.setText("");
274
                StyledDocument doc = sysexSendTextArea.getStyledDocument();
275
                try {
276
                    doc
277
                            .insertString(0, MidiUtils
278
                                    .getHexString(sysexSendReceive
279
                                            .getSysexFile().getData()), doc
280
                                    .getStyle("regular"));
281
                } catch (Exception e) {
282
 
283
                }
284
            } else if (object == sysexSendReceive.getReceivedBytes()) {
285
 
286
            }
287
        }
288
    }
289
 
290
    public SysexSendReceive getSysexSendReceive() {
291
        return sysexSendReceive;
292
    }
800 adamjking 293
 
294
    public static String getCurrentDirectory() {
295
        return currentDirectory;
296
    }
297
 
298
    public static void setCurrentDirectory(String currentDirectory) {
299
        SysexSendReceiveGUI.currentDirectory = currentDirectory;
300
    }
808 adamjking 301
 
628 adamjking 302
}