Subversion Repositories svn.mios

Rev

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