+ /**
+ * Selects the given note
+ * @param the note to select
+ */
+ public void selectNote(MooNoteElement elem) {
+ selection.add(elem);
+ }
+
+ /**
+ * Deselects the given note
+ * @param the note to deselect
+ */
+ public void deselectNote(MooNoteElement elem) {
+ selection.remove(elem);
+ }
+
+ /**
+ * Deselects all notes.
+ */
+ public void deselectAllNotes() {
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ ((MooNoteElement)it.next()).deselect();
+ }
+ selection.clear();
+ }
+
+ /**
+ * Determines if the given MooNoteElement is the only one in the track view that is selected.
+ * @return if the given element is the only selected one
+ */
+ public boolean isTheOnlySelected(MooNoteElement elem) {
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ if (!it.next().equals(elem)) return false;
+ }
+ return true;
+ }
+
+ /**
+ * Copies the current selection.
+ */
+ public void copySelectedNotes() {
+ 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);
+ }
+
+ /**
+ * Cuts the current selection.
+ */
+ public void cutSelectedNotes() {
+ copySelectedNotes();
+ removeSelectedNotes();
+ }
+
+ /**
+ * Pastes the current copy buffer at the given timestamp.
+ */
+ 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);
+ }
+ }
+ }
+
+ /**
+ * Removes the current selection.
+ */
+ public void removeSelectedNotes() {
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ removeNote((MooNoteElement)it.next());
+ }
+ selection.clear();
+ }
+
+ /**
+ * Transposes all selected notes the given number of halftones.
+ */
+ private void transposeSelectedNotes(int halftones) {
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ MooNoteElement elem = (MooNoteElement)it.next();
+ elem.transpose(halftones);
+ }
+ }
+
+ /**
+ * Moves the current selection the given number of ticks.
+ * @param ticks the number of ticks to move the selection.
+ */
+ public void moveSelectedNotes(int ticks) {
+ 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();
+ elem.getNote().setTick(elem.getNote().getTick() + ticks);
+ layoutElement(elem, true);
+ }
+ } else {
+ // 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()) {
+ MooNoteElement elem = (MooNoteElement)it.previous();
+ elem.getNote().setTick(elem.getNote().getTick() + ticks);
+ layoutElement(elem, true);
+ }
+ }
+ }
+
+ /**
+ * Moves the current selection, depending on the given delta y.
+ * @param y the number of pixels the selection is moved.
+ */
+ public void maybeMoveSelectedNotes(int y) {
+ 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
+ * @param x the x-coordinate in which to display the menu
+ * @param y the y-coordinate in which to display the menu
+ */
+ public void showSelectionPopup(Component c, int x, int y) {
+ selPopup.show(c, x, y);
+ }
+
+ /**
+ * Draws the grid that is on the background.
+ * @param g The Graphics object used to draw the grid.
+ */