3 import java.awt.event.*;
4 import javax.sound.midi.*;
9 * The GUI-class representing the popupdialogs in the Track menu.
11 * @author Björn Lanneskog
14 public class MooDialog extends JDialog {
16 private JLabel labelA, labelB, labelC;
17 private JTextField textFieldA, textFieldB, textFieldC;
18 private JComboBox trackListA, trackListB;
19 private JButton okButton, cancelButton;
20 public static final int ADD_TRACK = 1,
28 * Constructor of the dialogs.
30 public MooDialog(int type) {
31 super(Moosique.getGUI(), false);
33 Container pane = getContentPane();
36 Track[] tracks = Moosique.getSequence().getTracks();
39 case ADD_TRACK: makeAddDialog(pane, tracks); break;
40 case DELETE_TRACK: makeDelDialog(pane, tracks); break;
41 case COPY_TRACK: makeCopyDialog(pane, tracks); break;
42 case MOVE_TRACK: makeMoveDialog(pane, tracks); break;
43 case JUMP: makeJumpDialog(pane); break;
44 case CONTENTS: makeTextDialog(pane, "Manual.txt"); break;
49 * Builds the add track popupdialog.
50 * @param pane The container to put the dialog in.
51 * @param tracks A array containing miditracks.
53 private void makeAddDialog(Container pane, Track[] tracks) {
55 setTitle("Add track");
56 labelA = new JLabel("Name of track", JLabel.CENTER);
58 textFieldA = new JTextField();
60 labelB = new JLabel("Add after", JLabel.CENTER);
62 trackListA = new JComboBox();
63 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
65 cancelButton = new JButton("Cancel");
66 pane.add(cancelButton);
67 okButton = new JButton("OK");
70 labelA.setBounds(50, 10, 100, 20);
71 textFieldA.setBounds(40, 35, 120, 20);
72 labelB.setBounds(50, 70, 100, 20);
73 trackListA.setBounds(40, 95, 120, 20);
74 cancelButton.setBounds(10, 150, 80, 30);
75 okButton.setBounds(120, 150, 60, 30);
77 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
78 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
86 * Builds the delete track popupdialog.
87 * @param pane The container to put the dialog in.
88 * @param tracks A array containing miditracks.
90 private void makeDelDialog(Container pane, Track[] tracks) {
92 setTitle("Delete track");
93 labelB = new JLabel("Delete track", JLabel.CENTER);
95 trackListA = new JComboBox();
96 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
98 cancelButton = new JButton("Cancel");
99 pane.add(cancelButton);
100 okButton = new JButton("OK");
103 labelB.setBounds(50, 10, 100, 20);
104 trackListA.setBounds(40, 35, 120, 20);
105 cancelButton.setBounds(10, 90, 80, 30);
106 okButton.setBounds(120, 90, 60, 30);
108 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
109 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
117 * Builds the copy track popupdialog.
118 * @param pane The container to put the dialog in.
119 * @param tracks A array containing miditracks.
121 private void makeCopyDialog(Container pane, Track[] tracks) {
123 setTitle("Copy Track");
124 labelA = new JLabel("Track to copy", JLabel.CENTER);
126 trackListA = new JComboBox();
127 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
128 pane.add(trackListA);
129 labelB = new JLabel("Insert after", JLabel.CENTER);
131 trackListB = new JComboBox();
132 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
133 pane.add(trackListB);
134 cancelButton = new JButton("Cancel");
135 pane.add(cancelButton);
136 okButton = new JButton("OK");
139 labelA.setBounds(50, 10, 100, 20);
140 trackListA.setBounds(40, 35, 120, 20);
141 labelB.setBounds(50, 70, 100, 20);
142 trackListB.setBounds(40, 95, 120, 20);
143 cancelButton.setBounds(10, 150, 80, 30);
144 okButton.setBounds(120, 150, 60, 30);
146 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
147 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
155 * Builds the move track popupdialog
156 * @param pane The container to put the dialog in.
157 * @param tracks A array containing miditracks.
159 private void makeMoveDialog(Container pane, Track[] tracks) {
161 setTitle("Move track");
162 labelA = new JLabel("Track to move", JLabel.CENTER);
164 trackListA = new JComboBox();
165 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
166 pane.add(trackListA);
167 labelB = new JLabel("Insert after", JLabel.CENTER);
169 trackListB = new JComboBox();
170 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
171 pane.add(trackListB);
172 cancelButton = new JButton("Cancel");
173 pane.add(cancelButton);
174 okButton = new JButton("OK");
177 labelA.setBounds(40, 10, 120, 20);
178 trackListA.setBounds(40, 35, 120, 20);
179 labelB.setBounds(50, 70, 100, 20);
180 trackListB.setBounds(40, 95, 120, 20);
181 cancelButton.setBounds(10, 150, 80, 30);
182 okButton.setBounds(120, 150, 60, 30);
184 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
185 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
193 * Builds the jump popupdialog.
194 * @param pane The container to put the dialog in.
195 * @param tracks A array containing miditracks.
197 private void makeJumpDialog(Container pane) {
200 labelA = new JLabel("Msr", JLabel.CENTER);
202 labelB = new JLabel("Beat", JLabel.CENTER);
204 labelC = new JLabel("Tick", JLabel.CENTER);
206 textFieldA = new JTextField();
207 pane.add(textFieldA);
208 textFieldB = new JTextField();
209 pane.add(textFieldB);
210 textFieldC = new JTextField();
211 pane.add(textFieldC);
212 cancelButton = new JButton("Cancel");
213 pane.add(cancelButton);
214 okButton = new JButton("OK");
217 labelA.setBounds(30, 25, 50, 20);
218 labelB.setBounds(105, 25, 50, 20);
219 labelC.setBounds(180, 25, 50, 20);
220 textFieldA.setBounds(30, 45, 50, 20);
221 textFieldB.setBounds(105, 45, 50, 20);
222 textFieldC.setBounds(180, 45, 50, 20);
223 cancelButton.setBounds(35, 90, 80, 30);
224 okButton.setBounds(155, 90, 60, 30);
226 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
227 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
234 private void makeTextDialog(Container pane, String filename) {
235 setTitle("Contents");
236 File manual = new File(filename);
239 BufferedReader br = new BufferedReader(new FileReader(manual));
240 char[] chars = new char[(int)manual.length()];
241 br.read(chars, 0, (int)manual.length());
242 s = new String(chars);
243 } catch (Exception ex) {
244 s = "Manual not found";
246 JTextArea contents = new JTextArea(s, 30, 40);
247 contents.setAutoscrolls(true);
249 contents.setBounds(10, 10, 500, 350);
253 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
254 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
258 private MooNote note;
259 private JOptionPane optionPane;
260 private JTextField pitch;
261 private JTextField velocity;
262 private JTextField length;
265 * Creates a new note preference dialog.
266 * @param mn the note that will be graphically represented
268 public MooDialog(MooNote mn) {
269 super(Moosique.getGUI(), "Note properties", false);
271 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
272 JPanel pitchpanel = new JPanel();
273 pitchpanel.add(new Label("Pitch: "));
274 pitchpanel.add(pitch);
277 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
278 JPanel velocitypanel = new JPanel();
279 velocitypanel.add(new Label("Velocity: "));
280 velocitypanel.add(velocity);
282 length = new JTextField(new Integer(note.getDuration()).toString(),5);
283 JPanel lengthpanel = new JPanel();
284 lengthpanel.add(new Label("Length: "));
285 lengthpanel.add(length);
287 Object[] array = {"Set the note properties",
292 final String btnString1 = "Enter";
293 final String btnString2 = "Cancel";
294 Object[] options = {btnString1, btnString2};
296 optionPane = new JOptionPane(array,
297 JOptionPane.QUESTION_MESSAGE,
298 JOptionPane.YES_NO_OPTION,
302 setContentPane(optionPane);
303 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
305 ActionListener intValidator = new ActionListener() {
306 public void actionPerformed(ActionEvent e) {
307 if (e.getSource() instanceof JTextField){
308 JTextField s = (JTextField)e.getSource();
309 int num = Integer.parseInt(s.getText());
312 else if (num > 127 && s != length)
314 s.setText(new Integer(num).toString());
318 pitch.addActionListener(intValidator);
319 velocity.addActionListener(intValidator);
320 length.addActionListener(intValidator);
322 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
323 public void propertyChange(PropertyChangeEvent e) {
324 String prop = e.getPropertyName();
327 && (e.getSource() == optionPane)
328 && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
329 prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
330 Object value = optionPane.getValue();
332 if (value == JOptionPane.UNINITIALIZED_VALUE) {
337 // Reset the JOptionPane's value.
338 // If you don't do this, then if the user
339 // presses the same button next time, no
340 // property change event will be fired.
342 JOptionPane.UNINITIALIZED_VALUE);
344 if (value.equals(btnString1)) {
345 note.setPitch(Integer.parseInt(pitch.getText()));
346 note.setVelocity(Integer.parseInt(velocity.getText()));
347 note.setDuration(Integer.parseInt(length.getText()));
350 } else { // user closed dialog or clicked cancel