Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)SIDV2librarian.java  beta1   2008/01/21
3
 *
4
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
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.sidlibr.gui;
22
 
23
import java.awt.Dimension;
628 adamjking 24
import java.awt.Font;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.awt.event.KeyEvent;
28
import java.awt.event.KeyListener;
29
import java.awt.event.MouseEvent;
30
import java.awt.event.MouseListener;
31
import java.util.EventObject;
213 tk 32
import java.util.Observable;
33
import java.util.Observer;
34
 
35
import javax.swing.JMenu;
36
import javax.swing.JMenuItem;
37
import javax.swing.JPanel;
628 adamjking 38
import javax.swing.JPopupMenu;
213 tk 39
import javax.swing.JScrollPane;
40
import javax.swing.JTable;
41
import javax.swing.KeyStroke;
42
import javax.swing.ListSelectionModel;
628 adamjking 43
import javax.swing.event.ListSelectionEvent;
44
import javax.swing.event.ListSelectionListener;
45
import javax.swing.event.TableModelEvent;
46
import javax.swing.event.TableModelListener;
47
import javax.swing.table.TableModel;
48
 
213 tk 49
import org.midibox.sidlibr.Bank;
628 adamjking 50
import org.midibox.sidlibr.SIDLibController;
213 tk 51
 
628 adamjking 52
public class BankTable extends JPanel implements TableModelListener,
53
        ListSelectionListener, Observer, MouseListener, ActionListener,
54
        KeyListener {
213 tk 55
    private SIDLibController sidLibController;
56
    JTable table;
57
    JPopupMenu popupMenu;
58
    int bankNumber;
59
    int selectedBeforeDrag = 0;
628 adamjking 60
 
213 tk 61
    public BankTable(SIDLibController sidLibController, int bankNumber) {
62
        this.sidLibController = sidLibController;
63
        this.bankNumber = bankNumber;
64
        sidLibController.addObserver(this);
628 adamjking 65
 
213 tk 66
        createPopupMenu();
628 adamjking 67
 
68
        table = new JTable(new BankTableModel(sidLibController
69
                .getBank(bankNumber)));
213 tk 70
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
628 adamjking 71
        table.getModel().addTableModelListener(this);
213 tk 72
        table.addMouseListener(this);
73
        table.addKeyListener(this);
74
        table.getSelectionModel().addListSelectionListener(this);
628 adamjking 75
 
76
        table.getColumnModel().getColumn(1)
77
                .setCellEditor(new PatchNameEditor());
213 tk 78
        table.getColumnModel().getColumn(0).setPreferredWidth(30);
79
        table.getColumnModel().getColumn(1).setPreferredWidth(170);
80
        table.getColumnModel().getColumn(2).setPreferredWidth(70);
628 adamjking 81
 
82
        table.setPreferredScrollableViewportSize(new Dimension(table
83
                .getPreferredSize().width, Math
84
                .round(table.getPreferredSize().height / 4)));
85
        // TK: temporary commented out - not available under MacOS, we need a
86
        // replacement
247 tk 87
        // table.setFillsViewportHeight(true);
628 adamjking 88
 
213 tk 89
        JScrollPane scrollPane = new JScrollPane(table);
628 adamjking 90
        scrollPane.setPreferredSize(new Dimension(
91
                table.getPreferredSize().width, Math.round(table
92
                        .getPreferredSize().height / 4)));
93
 
213 tk 94
        add(scrollPane);
95
    }
628 adamjking 96
 
213 tk 97
    public void tableChanged(TableModelEvent e) {
98
        int row = e.getFirstRow();
99
        int column = e.getColumn();
628 adamjking 100
        TableModel model = (TableModel) e.getSource();
101
        Object data = model.getValueAt(row, column);
102
        if ((column == 1)
103
                && !(sidLibController.getBank(bankNumber).getPatchAt(row)
104
                        .getPatchName().equals((String) data))) {
105
            sidLibController.getBank(bankNumber).getPatchAt(row).setPatchName(
106
                    (String) data);
107
        }
108
    }
109
 
213 tk 110
    public void update(Observable observable, Object object) {
628 adamjking 111
        if (object == "Data changed") {
213 tk 112
            Bank b = sidLibController.getBank(bankNumber);
113
            TableModel m = table.getModel();
114
            for (int c = 0; c < b.bankSize; c++) {
115
                m.setValueAt(b.getPatchAt(c).getPatchName(), c, 1);
628 adamjking 116
                m.setValueAt(b.getPatchAt(c).getEngineStr(), c, 2);
213 tk 117
            }
118
        }
628 adamjking 119
        if (object == "Rename") {
213 tk 120
            table.editCellAt(table.getSelectedRow(), 1, new EventObject(this));
121
        }
122
    }
628 adamjking 123
 
213 tk 124
    public int getSelectedRow() {
125
        return table.getSelectedRow();
126
    }
628 adamjking 127
 
213 tk 128
    private void maybeShowPopup(MouseEvent e) {
628 adamjking 129
        if (e.isPopupTrigger()) {
130
            popupMenu.show(e.getComponent(), e.getX(), e.getY());
131
        }
132
    }
133
 
213 tk 134
    public void mousePressed(MouseEvent e) {
135
        if (e.getButton() == MouseEvent.BUTTON1) {
136
            selectedBeforeDrag = table.getSelectedRow();
137
        }
138
        maybeShowPopup(e);
139
    }
628 adamjking 140
 
141
    public void mouseReleased(MouseEvent e) {
142
        if (e.getButton() == MouseEvent.BUTTON1) {
143
            if (table.getSelectedRow() != selectedBeforeDrag) {
144
                sidLibController.editSwap(selectedBeforeDrag, table
145
                        .getSelectedRow());
146
            }
213 tk 147
        }
628 adamjking 148
        maybeShowPopup(e);
149
    }
213 tk 150
 
628 adamjking 151
    public void mouseEntered(MouseEvent e) {
152
    }
213 tk 153
 
628 adamjking 154
    public void mouseExited(MouseEvent e) {
155
    }
213 tk 156
 
628 adamjking 157
    public void mouseClicked(MouseEvent e) {
158
        if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 2)) {
159
            sidLibController.editCurrentPatch();
213 tk 160
        }
628 adamjking 161
    }
162
 
163
    private void createPopupMenu() {
164
        popupMenu = new JPopupMenu();
165
        JMenu submenu;
166
        JMenuItem menuItem;
167
 
168
        menuItem = new JMenuItem("Edit", KeyEvent.VK_E);
169
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
170
                ActionEvent.CTRL_MASK));
213 tk 171
        menuItem.setActionCommand("Edit");
172
        menuItem.addActionListener(this);
173
        popupMenu.add(menuItem);
628 adamjking 174
 
175
        menuItem = new JMenuItem("Rename", KeyEvent.VK_R);
213 tk 176
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
628 adamjking 177
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
178
                ActionEvent.CTRL_MASK));
213 tk 179
        menuItem.setActionCommand("Rename");
180
        menuItem.addActionListener(this);
181
        popupMenu.add(menuItem);
628 adamjking 182
 
213 tk 183
        popupMenu.addSeparator();
628 adamjking 184
 
185
        menuItem = new JMenuItem("Cut", KeyEvent.VK_U);
186
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
187
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
188
                ActionEvent.CTRL_MASK));
213 tk 189
        menuItem.setActionCommand("Cut");
190
        menuItem.addActionListener(this);
191
        popupMenu.add(menuItem);
628 adamjking 192
 
193
        menuItem = new JMenuItem("Copy", KeyEvent.VK_C);
213 tk 194
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
628 adamjking 195
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
196
                ActionEvent.CTRL_MASK));
213 tk 197
        menuItem.setActionCommand("Copy");
198
        menuItem.addActionListener(this);
199
        popupMenu.add(menuItem);
628 adamjking 200
 
201
        menuItem = new JMenuItem("Paste", KeyEvent.VK_P);
213 tk 202
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
628 adamjking 203
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
204
                ActionEvent.CTRL_MASK));
213 tk 205
        menuItem.setActionCommand("Paste");
206
        menuItem.addActionListener(this);
207
        popupMenu.add(menuItem);
628 adamjking 208
 
209
        menuItem = new JMenuItem("Clear", KeyEvent.VK_L);
213 tk 210
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
628 adamjking 211
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
213 tk 212
        menuItem.setActionCommand("Clear");
213
        menuItem.addActionListener(this);
214
        popupMenu.add(menuItem);
628 adamjking 215
 
216
        popupMenu.addSeparator();
217
 
213 tk 218
        submenu = new JMenu("Init current patch");
219
        submenu.setFont(submenu.getFont().deriveFont(Font.PLAIN));
220
        submenu.setMnemonic(KeyEvent.VK_I);
221
        popupMenu.add(submenu);
628 adamjking 222
 
223
        menuItem = new JMenuItem("LEAD engine", KeyEvent.VK_P);
213 tk 224
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
225
        menuItem.setActionCommand("Init LEAD patch");
226
        menuItem.addActionListener(this);
227
        submenu.add(menuItem);
628 adamjking 228
 
229
        menuItem = new JMenuItem("BASSLINE engine", KeyEvent.VK_P);
213 tk 230
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
231
        menuItem.setActionCommand("Init BASSLINE patch");
232
        menuItem.addActionListener(this);
233
        submenu.add(menuItem);
628 adamjking 234
 
235
        menuItem = new JMenuItem("DRUM engine", KeyEvent.VK_P);
213 tk 236
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
237
        menuItem.setActionCommand("Init DRUM patch");
238
        menuItem.addActionListener(this);
239
        submenu.add(menuItem);
628 adamjking 240
 
241
        menuItem = new JMenuItem("MULTI engine", KeyEvent.VK_P);
213 tk 242
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
243
        menuItem.setActionCommand("Init MULTI patch");
244
        menuItem.addActionListener(this);
245
        submenu.add(menuItem);
628 adamjking 246
 
247
        menuItem = new JMenuItem("Init current bank", KeyEvent.VK_N);
213 tk 248
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
249
        menuItem.setActionCommand("Init current bank");
250
        menuItem.addActionListener(this);
251
        popupMenu.add(menuItem);
628 adamjking 252
 
213 tk 253
        popupMenu.addSeparator();
628 adamjking 254
 
213 tk 255
        submenu = new JMenu("Transmit");
256
        submenu.setFont(submenu.getFont().deriveFont(Font.PLAIN));
257
        submenu.setMnemonic(KeyEvent.VK_I);
258
        popupMenu.add(submenu);
628 adamjking 259
 
260
        // Transmit menu items:
261
        menuItem = new JMenuItem("Patch to buffer", KeyEvent.VK_B);
213 tk 262
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
263
        menuItem.setActionCommand("Transmit patch to buffer");
264
        menuItem.addActionListener(this);
265
        submenu.add(menuItem);
628 adamjking 266
 
267
        menuItem = new JMenuItem("Patch to memory", KeyEvent.VK_M);
213 tk 268
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
269
        menuItem.setActionCommand("Transmit patch to memory");
270
        menuItem.addActionListener(this);
271
        submenu.add(menuItem);
628 adamjking 272
 
273
        submenu.addSeparator();
274
 
275
        menuItem = new JMenuItem("Bank", KeyEvent.VK_A);
213 tk 276
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
277
        menuItem.setActionCommand("Transmit bank to memory");
278
        menuItem.addActionListener(this);
279
        submenu.add(menuItem);
628 adamjking 280
 
213 tk 281
        submenu = new JMenu("Receive");
282
        submenu.setFont(submenu.getFont().deriveFont(Font.PLAIN));
283
        submenu.setMnemonic(KeyEvent.VK_I);
284
        popupMenu.add(submenu);
628 adamjking 285
 
286
        menuItem = new JMenuItem("Patch from buffer", KeyEvent.VK_B);
213 tk 287
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
288
        menuItem.setActionCommand("Receive patch from buffer");
289
        menuItem.addActionListener(this);
290
        submenu.add(menuItem);
628 adamjking 291
 
292
        menuItem = new JMenuItem("Patch from memory", KeyEvent.VK_M);
213 tk 293
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
294
        menuItem.setActionCommand("Receive patch from memory");
295
        menuItem.addActionListener(this);
296
        submenu.add(menuItem);
628 adamjking 297
 
298
        submenu.addSeparator();
299
 
300
        menuItem = new JMenuItem("Bank", KeyEvent.VK_A);
213 tk 301
        menuItem.setFont(menuItem.getFont().deriveFont(Font.PLAIN));
302
        menuItem.setActionCommand("Receive bank from memory");
303
        menuItem.addActionListener(this);
304
        submenu.add(menuItem);
305
    }
628 adamjking 306
 
307
    public void actionPerformed(ActionEvent ae) {
308
        sidLibController.actionPerformed(ae);
309
    }
310
 
213 tk 311
    public void valueChanged(ListSelectionEvent event) {
312
        int patchNumber = table.getSelectedRow();
313
        if (patchNumber < 0) {
314
            patchNumber = 0;
315
        }
316
        sidLibController.setCurrentPatchNumber(patchNumber);
317
    }
628 adamjking 318
 
213 tk 319
    public void keyPressed(KeyEvent e) {
628 adamjking 320
        if ((e.getKeyCode() == KeyEvent.VK_E)
321
                && (e.getModifiers() == ActionEvent.CTRL_MASK)) {
213 tk 322
            sidLibController.editCurrentPatch();
628 adamjking 323
        } else if ((e.getKeyCode() == KeyEvent.VK_R)
324
                && (e.getModifiers() == ActionEvent.CTRL_MASK)) {
213 tk 325
            sidLibController.editRename();
628 adamjking 326
        } else if ((e.getKeyCode() == KeyEvent.VK_X)
327
                && (e.getModifiers() == ActionEvent.CTRL_MASK)) {
328
            sidLibController.editCut();
329
        } else if ((e.getKeyCode() == KeyEvent.VK_C)
330
                && (e.getModifiers() == ActionEvent.CTRL_MASK)) {
213 tk 331
            sidLibController.editCopy();
628 adamjking 332
        } else if ((e.getKeyCode() == KeyEvent.VK_V)
333
                && (e.getModifiers() == ActionEvent.CTRL_MASK)) {
213 tk 334
            sidLibController.editPaste();
628 adamjking 335
        } else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
213 tk 336
            sidLibController.editClear();
337
        }
338
    }
628 adamjking 339
 
213 tk 340
    public void keyReleased(KeyEvent e) {
628 adamjking 341
 
213 tk 342
    }
628 adamjking 343
 
213 tk 344
    public void keyTyped(KeyEvent e) {
628 adamjking 345
 
213 tk 346
    }
628 adamjking 347
 
213 tk 348
}