+ private int ticksPerSixteenth, popupY = 0;
+ private boolean leftMouseButtonPressed = false, quantizeRecording = false;
+ protected static int viewLength = 0;
+ protected static int extraHeight = 0;
+ public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
+
+ /**
+ * Creates the trackview.
+ * @param track The track it represents graphically and operates on.
+ * @param title The object that is used to manipulate instrument, channel, solo, mute.
+ */
+ public MooTrackView (Track track, MooTrackTitle title) {
+ super(true);
+
+ // Defines instance variables
+ this.track = track;
+ this.title = title;
+ insets = getInsets();
+
+ // Configures panel
+ setBackground(Color.white);
+ setBorder(BorderFactory.createLineBorder(Color.black));
+ setLayout(null);
+ setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
+
+ placeNoteElements(false);
+
+ // 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(boolean quantize) {
+ // Converts the track.
+ Moosique.convertTrack(track);
+
+ // Empties the container
+ removeAll();
+ coords = new ArrayList(track.size() / 2);
+ selection = new TreeSet();
+
+ // Creates temporary variables
+ MidiEvent note;
+ MooNoteElement elem;
+ extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
+
+ // Places note elements
+ for (int i = 0; i < track.size(); i++) {
+ note = track.get(i);
+ if (note instanceof MooNote) {
+ // Adds the note element to the note area and moves it to the appropriate place.
+ MooNote mn = (MooNote)note;
+ elem = new MooNoteElement(this, mn);
+ add(elem);
+ layoutElement(elem, false);
+ }
+ setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
+ }
+ }
+
+ /**
+ * Layouts the element to the right place.
+ * @param elem the element that will be layouted.
+ * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
+ */
+ public void layoutElement(MooNoteElement elem, boolean old){
+ // If the element is currently in the view, removes its coordinates from the list.
+ Rectangle r = new Rectangle();
+ if (old){
+ r = elem.getBounds(r);
+ for (Iterator i = coords.iterator(); i.hasNext();){
+ Object ob = i.next();
+ if (r.equals(ob)){
+ coords.remove(ob);
+ break;
+ }
+ }
+ }
+
+ // Creates temporary variables.
+ ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
+ MooNote mn = elem.getNote();
+ int x, y, height;
+
+ // Calculates coordinates.
+ x = insets.left;
+ if (quantizeRecording) {
+ // Snap to nearest sixteenth
+ y = insets.top + Math.round(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
+ height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
+ } else {
+ y = insets.top + (int)((mn.getTick() * NOTE_HEIGHT) / ticksPerSixteenth);
+ height = (mn.getDuration() * NOTE_HEIGHT) / ticksPerSixteenth;
+ }
+ if (height == 0) height = NOTE_HEIGHT;
+ r = new Rectangle(x, y, NOTE_WIDTH, height);
+
+ // Places the element in the appropriate place.
+ while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
+ elem.setBounds(r);
+ coords.add(r);
+ if (viewLength < (y + height)){
+ viewLength = y + height;
+ if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
+ }
+ if (old) {
+ validate();
+ repaint();
+ }
+ }