Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
628 adamjking 1
/*
2
 * @(#)MidiMonitorFilteredGUI.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.Dimension;
26
import java.awt.Font;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.event.WindowAdapter;
31
import java.awt.event.WindowEvent;
32
import java.util.LinkedList;
33
import java.util.Observable;
34
import java.util.Observer;
35
 
36
import javax.sound.midi.MetaMessage;
37
import javax.sound.midi.ShortMessage;
38
import javax.sound.midi.SysexMessage;
39
import javax.swing.JButton;
40
import javax.swing.JDialog;
41
import javax.swing.JPanel;
42
import javax.swing.JScrollPane;
43
import javax.swing.JTextPane;
44
import javax.swing.JToolBar;
45
import javax.swing.text.BadLocationException;
46
import javax.swing.text.Style;
47
import javax.swing.text.StyleConstants;
48
import javax.swing.text.StyleContext;
49
import javax.swing.text.StyledDocument;
50
 
51
import org.midibox.midi.MidiMessageReceived;
52
import org.midibox.midi.MidiMonitorFiltered;
640 adamjking 53
import org.midibox.utils.gui.DialogOwner;
628 adamjking 54
import org.midibox.utils.gui.ImageLoader;
55
 
56
public class MidiMonitorFilteredGUI extends JPanel implements ActionListener,
57
        Observer {
58
    private MidiMonitorFiltered midiMonitorFiltered;
59
 
60
    private JTextPane textArea;
61
 
62
    private JButton clearButton;
63
 
64
    private JButton filterButton;
65
 
66
    private JDialog filterDialog;
67
 
68
    int maxLength = 50000;
69
 
70
    public MidiMonitorFilteredGUI(MidiMonitorFiltered midiFilteredPortMonitor) {
71
 
72
        super(new BorderLayout());
73
        this.midiMonitorFiltered = midiFilteredPortMonitor;
74
        midiFilteredPortMonitor.getMidiMonitor().addObserver(this);
75
 
76
        // Create a text area.
77
        textArea = new JTextPane();
78
        textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
79
        textArea.setBackground(Color.BLACK);
80
        textArea.setForeground(Color.LIGHT_GRAY);
81
        textArea.setEditable(false);
82
        JScrollPane areaScrollPane = new JScrollPane(textArea);
83
        areaScrollPane
84
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
85
        areaScrollPane.setPreferredSize(new Dimension(100, 100));
86
 
87
        JToolBar toolbar = new JToolBar("Tool Bar", JToolBar.HORIZONTAL);
88
        toolbar.setRollover(true);
89
        toolbar.setFloatable(false);
90
 
641 adamjking 91
        filterButton = new JButton(ImageLoader.getImageIcon("filter.png"));
628 adamjking 92
        filterButton.addActionListener(this);
93
        filterButton.setMargin(new Insets(2, 2, 2, 2));
94
        filterButton.setToolTipText("Filter Messages");
95
        toolbar.add(filterButton);
96
 
641 adamjking 97
        clearButton = new JButton(ImageLoader.getImageIcon("delete.png"));
628 adamjking 98
        clearButton.addActionListener(this);
99
        clearButton.setMargin(new Insets(2, 2, 2, 2));
100
        clearButton.setToolTipText("Clear Screen");
101
        toolbar.add(clearButton);
102
 
103
        add(toolbar, BorderLayout.NORTH);
104
        add(areaScrollPane, BorderLayout.CENTER);
105
 
106
        // Voice Messages
107
        setColour(ShortMessage.NOTE_OFF, new Color(0x008000));
108
        setColour(ShortMessage.NOTE_ON, Color.GREEN);
109
        setColour(ShortMessage.POLY_PRESSURE, Color.YELLOW);
110
        setColour(ShortMessage.CONTROL_CHANGE, Color.CYAN);
111
        setColour(ShortMessage.PROGRAM_CHANGE, Color.YELLOW);
112
        setColour(ShortMessage.CHANNEL_PRESSURE, Color.YELLOW);
113
        setColour(ShortMessage.PITCH_BEND, new Color(0x8080FF));
114
 
115
        setColour(SysexMessage.SYSTEM_EXCLUSIVE, Color.LIGHT_GRAY);
116
 
117
        // System Common
118
        setColour(ShortMessage.MIDI_TIME_CODE, Color.BLUE);
119
        setColour(ShortMessage.SONG_POSITION_POINTER, Color.BLUE);
120
        setColour(ShortMessage.SONG_SELECT, Color.BLUE);
121
        setColour(ShortMessage.TUNE_REQUEST, Color.BLUE);
122
 
123
        // System Realtime
124
        setColour(ShortMessage.TIMING_CLOCK, Color.MAGENTA);
125
        setColour(ShortMessage.START, Color.MAGENTA);
126
        setColour(ShortMessage.CONTINUE, Color.MAGENTA);
127
        setColour(ShortMessage.STOP, Color.MAGENTA);
128
        setColour(ShortMessage.ACTIVE_SENSING, Color.MAGENTA);
129
        setColour(ShortMessage.SYSTEM_RESET, Color.MAGENTA);
130
 
131
        // Meta Messages
132
        setColour(MetaMessage.META, Color.WHITE);
133
 
134
        setSize(300, 300);
135
        setLocation(0, 0);
136
    }
137
 
138
    public void setColour(int status, Color colour) {
139
 
140
        Style def = StyleContext.getDefaultStyleContext().getStyle(
141
                StyleContext.DEFAULT_STYLE);
142
 
143
        StyledDocument doc = textArea.getStyledDocument();
144
 
145
        Style style = doc.addStyle("" + status, def);
146
        StyleConstants.setForeground(style, colour);
147
    }
148
 
149
    public void check() {
150
 
151
        LinkedList messages = midiMonitorFiltered.getMidiMonitor()
152
                .getReceivedMessages();
153
 
154
        if (!messages.isEmpty()) {
155
 
156
            while (!messages.isEmpty()) {
157
 
158
                MidiMessageReceived message = (MidiMessageReceived) messages
159
                        .removeFirst();
160
 
161
                String string = midiMonitorFiltered.getMidiMonitor()
162
                        .decodeMessage(message.midiMessage, message.timeStamp);
163
 
164
                // return value is null if message should be filtered (required
165
                // for SysEx messages)
166
                if (string != null) {
167
                    StyledDocument doc = textArea.getStyledDocument();
168
 
169
                    int status = message.midiMessage.getStatus();
170
 
171
                    if (message.midiMessage instanceof ShortMessage) {
172
                        status = ((ShortMessage) message.midiMessage)
173
                                .getCommand();
174
                    }
175
 
176
                    try {
177
                        doc.remove(0, Math.max(doc.getLength() - maxLength, 0));
178
 
179
                        doc.insertString(doc.getLength(), string + "\n", doc
180
                                .getStyle("" + status));
181
 
182
                    } catch (BadLocationException ble) {
183
                        System.out.println(ble.getMessage());
184
                    }
185
                }
186
            }
187
            textArea.setCaretPosition(textArea.getDocument().getLength());
188
        }
189
    }
190
 
191
    private void showFilterDialog() {
192
        if (filterDialog == null) {
193
            final MidiFilterGUI midiFilterGUI = new MidiFilterGUI(
194
                    midiMonitorFiltered.getMidiFilter());
195
 
640 adamjking 196
            filterDialog = new JDialog(DialogOwner.getFrame(), "MIDI Filter Properties", false);
628 adamjking 197
            filterDialog.setContentPane(midiFilterGUI);
198
            filterDialog.pack();
199
            filterDialog.setLocationRelativeTo(this);
200
            filterDialog.setVisible(true);
201
 
202
            filterDialog.addWindowListener(new WindowAdapter() {
203
                public void windowClosing(WindowEvent we) {
204
                    filterDialog = null;
205
                }
206
            });
207
        }
208
        filterDialog.requestFocus();
209
    }
210
 
211
    public void update(Observable observable, Object object) {
212
        check();
213
    }
214
 
215
    public void actionPerformed(ActionEvent ae) {
216
        Object source = ae.getSource();
217
 
218
        if (source == filterButton) {
219
            showFilterDialog();
220
        } else if (source == clearButton) {
221
            textArea.setText("");
222
        }
223
    }
224
 
225
    public Dimension getMinimumSize() {
226
        return getPreferredSize();
227
    }
228
}