]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
*** empty log message ***
[moosique.git] / MooNoteElement.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 /**
6  * Graphical representation of a MIDI note.
7  * 
8  * @author  Andersson, Andreen, Lanneskog, Pehrson
9  * @version 1
10  */
11  
12 public class MooNoteElement extends JPanel {
13
14         private MooTrackView mtv;
15         private MooNote note;
16         private boolean selected;
17         private Rectangle pitchRect, veloRect;
18         public static final Color bgColor = new Color(160, 218, 255);
19         private String notePitch;
20         private String noteVelocity;
21         private JPopupMenu popup;
22         private JMenuItem popupRemove, popupProp;
23
24         /** 
25          * Creates a new note element.
26          * @param mn    the note that will be graphically represented
27          * @param rows  the number of rows that the note will occupy
28          */
29         public MooNoteElement (MooTrackView parent, MooNote mn) {
30                 mtv = parent;
31                 note = mn;
32                 calculateString();
33                 setBorder(BorderFactory.createLineBorder(Color.black));
34                 setBackground(bgColor);
35                 addMouseListener(new MAdapter());
36
37                 // Defines coordinates.
38                 pitchRect = new Rectangle(0, 0, 15, 10);
39                 veloRect = new Rectangle(20, 0, 40, 10);
40
41                 // Creates pop-up menu.
42                 popup = new JPopupMenu();
43                 PopupListener pList = new PopupListener();
44                 popupProp = new JMenuItem("Preferences...");
45                 popupProp.addActionListener(pList);
46                 popup.add(popupProp);
47
48                 popupRemove = new JMenuItem("Remove");
49                 popupRemove.addActionListener(pList);
50                 popup.add(popupRemove);
51
52         }
53
54         /** 
55          * Returns true if the current NoteElement is selected, otherwise false.
56          * @return if the element is selected
57          */
58         public boolean isSelected() {
59                 return selected;
60         }
61
62         /** 
63          * Selects the current NoteElement.
64          * @param state if the element should be selected
65          */
66         public void setSelected(boolean state) {
67                 selected = state;
68         }
69
70         /**
71          *
72          */
73         public void paintComponent(Graphics g)
74         {
75                 super.paintComponent(g);
76                 if (!(g instanceof Graphics2D)) return;
77                 Graphics2D g2 = (Graphics2D)g;
78                 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
79         /*
80                 switch(columns) {
81                         case 0:
82                         case 1:
83                         ...
84                 }
85         */
86                 
87                 g2.drawString(notePitch, 1, 9);
88                 g2.drawString("" + noteVelocity, 21, 9);
89         }
90
91         protected void calculateString(){
92
93                 noteVelocity = ""; 
94                 notePitch = "";
95                 if(note == null) return;
96
97                 int pitch = note.getPitch();
98                 switch (pitch % 12) {
99                         case  0: notePitch = "C";  break;
100                         case  1: notePitch = "C#"; break;
101                         case  2: notePitch = "D";  break;
102                         case  3: notePitch = "D#"; break;
103                         case  4: notePitch = "E";  break;
104                         case  5: notePitch = "F";  break;
105                         case  6: notePitch = "F#"; break;
106                         case  7: notePitch = "G";  break;
107                         case  8: notePitch = "G#"; break;
108                         case  9: notePitch = "A";  break;
109                         case 10: notePitch = "A#"; break;
110                         case 11: notePitch = "B";  break;
111                 }
112                 notePitch += pitch / 12;
113                 noteVelocity = ""+note.getVelocity();
114         }
115
116         public MooNote getNote(){
117                 return note;
118         }
119
120         class MAdapter extends MouseAdapter {
121                 public void mousePressed(MouseEvent e) {
122                         if (e.isControlDown()) {
123                                 if (pitchRect.contains(e.getPoint())) {
124                                         if (SwingUtilities.isRightMouseButton(e)) {
125                                                 note.setPitch(note.getPitch() + 1);
126                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
127                                                 note.setPitch(note.getPitch() - 1);
128                                         }
129                                         calculateString();
130                                 } else if (veloRect.contains(e.getPoint())) {
131                                         if (SwingUtilities.isRightMouseButton(e)) {
132                                                 note.setVelocity(note.getVelocity() + 1);
133                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
134                                                 note.setVelocity(note.getVelocity() - 1);
135                                         }
136                                         calculateString();
137                                 }
138                                 e.getComponent().repaint();
139                         } else if (e.isPopupTrigger()) {
140                                 popup.show(e.getComponent(), e.getX(), e.getY());
141                         }
142                 }
143         }
144
145         class PopupListener implements ActionListener {
146                 public void actionPerformed(ActionEvent e) {
147                         Object source = e.getSource();
148                         if  (source == popupProp) {
149                                 new MooDialog(note);
150                         } else if  (source == popupRemove) {
151                                 remove();
152                         }
153                 }
154         }
155
156         protected void remove(){
157                 mtv.removeNote(this, note);
158         }
159
160 }