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