X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=blobdiff_plain;f=MooTrackView.java;fp=MooTrackView.java;h=f7e8335bf365748478972feff97c7ce4fb879f53;hp=c1d8b5878cdb66b4f4b0d1ceef38a3f62c499f83;hb=c31857b9fcb119f0d4c12b96222f66340b3dcc56;hpb=4526e51b70110f7272b0c2a3a5f207657d690029 diff --git a/MooTrackView.java b/MooTrackView.java index c1d8b58..f7e8335 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -15,16 +15,18 @@ public class MooTrackView extends JPanel { private Track track; private MooTrackTitle title; - private Rectangle box; + private MooKeyboard keyboard; private JPopupMenu popup, selPopup; private JMenu selPopupTranspUp, selPopupTranspDown; private JMenuItem popupAdd, popupPaste; - private JMenuItem selPopupCopy, selPopupCut, selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct; + private JMenuItem selPopupCopy, selPopupCut, selPopupRemove; + private JMenuItem[] selPopupTranspUpItems, selPopupTranspDownItems; - private ArrayList coords, copyBuffer; + private ArrayList coords; private TreeSet selection; private Insets insets; + private Rectangle box; private int ticksPerSixteenth, popupY = 0; private boolean leftMouseButtonPressed = false; protected static int viewLength = 0; @@ -38,26 +40,58 @@ public class MooTrackView extends JPanel { */ public MooTrackView (Track track, MooTrackTitle title) { super(true); + + // Defines instance variables this.track = track; this.title = title; - - // Creates instance variables insets = getInsets(); + + // Configures panel + setBackground(Color.white); + setBorder(BorderFactory.createLineBorder(Color.black)); + setLayout(null); + setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT)); + + placeNoteElements(); + + // Creates panel pop-up menu. + popup = new JPopupMenu(); + popupAdd = addMenuItem(popup, "Add note..."); + popupPaste = addMenuItem(popup, "Paste"); + + // Creates selection pop-up menu. + selPopup = new JPopupMenu(); + selPopupCopy = addMenuItem(selPopup, "Copy selection"); + selPopupCut = addMenuItem(selPopup, "Cut selection"); + selPopupRemove = addMenuItem(selPopup, "Remove selection"); + selPopupTranspUpItems = new JMenuItem[12]; + selPopupTranspDownItems = new JMenuItem[12]; + selPopupTranspUp = createTransposeMenu(selPopup, selPopupTranspUpItems, "selection up"); + selPopupTranspDown = createTransposeMenu(selPopup, selPopupTranspDownItems, "selection down"); + + // Adds listeners for popup menu and keyboard synthesizer. + addMouseListener(new MAdapter()); + keyboard = new MooKeyboard(title); + addKeyListener(keyboard); + } + + /** + * Creates note elements for all MooNotes in the track, and places them in the appropriate place. + */ + public void placeNoteElements() { + // Converts the track. + Moosique.convertTrack(track); + + // Empties the container + removeAll(); coords = new ArrayList(track.size() / 2); selection = new TreeSet(); - copyBuffer = new ArrayList(); // Creates temporary variables MidiEvent note; MooNoteElement elem; extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150; - // Configures panel - setBackground(Color.white); - setBorder(BorderFactory.createLineBorder(Color.black)); - setLayout(null); - setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT)); - // Places note elements for (int i = 0; i < track.size(); i++) { note = track.get(i); @@ -69,45 +103,8 @@ public class MooTrackView extends JPanel { layoutElement(elem, false); } setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight)); - } - - // Creates panel pop-up menu. - popup = new JPopupMenu(); - PopupListener pList = new PopupListener(); - popupAdd = new JMenuItem("Add note..."); - popupAdd.addActionListener(pList); - popup.add(popupAdd); - popupPaste = new JMenuItem("Paste"); - popupPaste.addActionListener(pList); - popup.add(popupPaste); - - // Creates selection pop-up menu. - selPopup = new JPopupMenu(); - selPopupCopy = new JMenuItem("Copy selection"); - selPopupCopy.addActionListener(pList); - selPopup.add(selPopupCopy); - selPopupCut = new JMenuItem("Cut selection"); - selPopupCut.addActionListener(pList); - selPopup.add(selPopupCut); - selPopupRemove = new JMenuItem("Remove selection"); - selPopupRemove.addActionListener(pList); - selPopup.add(selPopupRemove); - selPopupTranspUp = new JMenu("Transpose selection up"); - selPopup.add(selPopupTranspUp); - selPopupTranspUpOct = new JMenuItem("One octave"); - selPopupTranspUpOct.addActionListener(pList); - selPopupTranspUp.add(selPopupTranspUpOct); - selPopupTranspDown = new JMenu("Transpose selection down"); - selPopup.add(selPopupTranspDown); - selPopupTranspDownOct = new JMenuItem("One octave"); - selPopupTranspDownOct.addActionListener(pList); - selPopupTranspDown.add(selPopupTranspDownOct); - - // Adds listeners for popup menu and keyboard synthesizer. - addMouseListener(new MAdapter()); - addKeyListener(new MooKeyboard()); - } + } /** * Layouts the element to the right place. @@ -264,12 +261,13 @@ public class MooTrackView extends JPanel { * Copies the current selection. */ public void copySelectedNotes() { - copyBuffer = new ArrayList(selection.size()); + ArrayList copyBuffer = new ArrayList(selection.size()); Iterator it = selection.iterator(); while(it.hasNext()) { copyBuffer.add(((MooNoteElement)it.next()).getNote().clone()); } Collections.sort(copyBuffer); + Moosique.setCopyBuffer(copyBuffer); } /** @@ -286,12 +284,14 @@ public class MooTrackView extends JPanel { public void pasteCopiedNotes() { int row = (popupY - insets.top) / NOTE_HEIGHT; long timestamp = (long)(ticksPerSixteenth * row); + ArrayList copyBuffer = Moosique.getCopyBuffer(); if (copyBuffer.size() > 0) { long startTime = ((MooNote)copyBuffer.get(0)).getTick(); Iterator it = copyBuffer.iterator(); while(it.hasNext()) { MooNote mn = (MooNote)((MooNote)it.next()).clone(); mn.setTick(mn.getTick() - startTime + timestamp); + mn.setChannel(title.getChannel()); addNote(mn); } } @@ -324,8 +324,8 @@ public class MooTrackView extends JPanel { * @param ticks the number of ticks to move the selection. */ public void moveSelectedNotes(int ticks) { - if (ticks > 0) { - // If the selection should be moved downwards, traverses the list in the natural order. + if (ticks < 0) { + // If the selection should be moved upwards, traverses the list in the natural order. Iterator it = selection.iterator(); while(it.hasNext()) { MooNoteElement elem = (MooNoteElement)it.next(); @@ -333,7 +333,7 @@ public class MooTrackView extends JPanel { layoutElement(elem, true); } } else { - // If the selection should be moved upwards, traverses the list in the opposite order. + // If the selection should be moved downwards, traverses the list in the opposite order. ArrayList selectedList = new ArrayList(selection); ListIterator it = selectedList.listIterator(selectedList.size()); while(it.hasPrevious()) { @@ -352,6 +352,54 @@ public class MooTrackView extends JPanel { moveSelectedNotes(ticksPerSixteenth * (y / NOTE_HEIGHT)); } + /** + * Enables keyboard recording. + */ + public void enableKeyboardRecording() { + keyboard.recordEnable(); + } + + /** + * Disables keyboard recording. + */ + public void disableKeyboardRecording() { + keyboard.recordDisable(); + } + + /** + * Adds a menu item with the given command to the given popup menu. + */ + private JMenuItem addMenuItem(JPopupMenu menu, String command) { + JMenuItem item = new JMenuItem(command); + item.addActionListener(new PopupListener()); + menu.add(item); + return item; + } + + /** + * Adds a menu item with the given command to the given menu. + */ + private JMenuItem addMenuItem(JMenu menu, String command) { + JMenuItem item = new JMenuItem(command); + item.addActionListener(new PopupListener()); + menu.add(item); + return item; + } + + /** + * Creates a transpose sub menu with the given title in the given popup menu, + * inserting the items into the given array. + */ + private JMenu createTransposeMenu(JPopupMenu menu, JMenuItem[] items, String title) { + JMenu trans = new JMenu("Transpose " + title); + menu.add(trans); + items[0] = addMenuItem(trans, "One octave"); + for (int i = 1; i < 12; i++) { + items[i] = addMenuItem(trans, (i) + " halftones"); + } + return trans; + } + /** * Shows a popup-menu with options for the current selection of note elements. * @param c the component over which to display the menu @@ -434,7 +482,7 @@ public class MooTrackView extends JPanel { } /** - * Listens on actions on the popup menu and executes the appropriate action. + * Takes the appropriate action when a user selects an item on the popup menu. */ class PopupListener implements ActionListener { public void actionPerformed(ActionEvent e) { @@ -451,10 +499,15 @@ public class MooTrackView extends JPanel { cutSelectedNotes(); } else if (source == selPopupRemove) { removeSelectedNotes(); - } else if (source == selPopupTranspUpOct) { + } else if (source == selPopupTranspUpItems[0]) { transposeSelectedNotes(12); - } else if (source == selPopupTranspDownOct) { + } else if (source == selPopupTranspDownItems[0]) { transposeSelectedNotes(-12); + } else { + for (int i = 1; i < 12; i++) { + if (source == selPopupTranspUpItems[i]) transposeSelectedNotes(i); + else if (source == selPopupTranspDownItems[i]) transposeSelectedNotes(-i); + } } } }