1 //
2 // Neuros Database Manipulator
3 // Copyright (C) 2003 Neuros Database Manipulator
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 // For information about Neuros Database Manipulator and its authors,
20 // please contact the Neuros Database Manipulator Web Site at
21 // http://neurosdbm.sourceforge.net
22 //
23 //
24
25 package net.sourceforge.neurosdbm;
26
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.awt.Frame;
31 import java.awt.GridBagLayout;
32 import java.awt.GridBagConstraints;
33 import java.awt.GridLayout;
34 import java.awt.Insets;
35 import java.awt.event.ActionListener;
36 import java.awt.event.ActionEvent;
37 import javax.swing.JDialog;
38 import javax.swing.JPanel;
39 import javax.swing.JButton;
40 import javax.swing.JList;
41 import javax.swing.JScrollPane;
42 import javax.swing.AbstractListModel;
43 import javax.swing.JOptionPane;
44 import net.sourceforge.neurosdbm.db.Database;
45 import net.sourceforge.neurosdbm.db.DisplayIndex;
46 import net.sourceforge.neurosdbm.db.PlaylistDisplayRecord;
47
48 public class PlaylistSelector extends JDialog implements ActionListener {
49
50 private Frame parent;
51 private Database database;
52 private ArrayList selectedTracks;
53 private JList playlistList;
54 private JButton createButton;
55 private JButton addButton;
56 private JButton cancelButton;
57
58 public PlaylistSelector(Frame parent, ArrayList selectedTracks,
59 Database database) {
60 super(parent, "Choose a Playlist", true);
61 this.parent = parent;
62 this.database = database;
63 this.selectedTracks = selectedTracks;
64
65 GridBagLayout gridbag = new GridBagLayout();
66 GridBagConstraints constraints = new GridBagConstraints();
67 constraints.insets = new Insets(5, 5, 5, 5);
68
69 getContentPane().setLayout(gridbag);
70
71 playlistList = new JList(new PlaylistSelectorListModel(database));
72 constraints.fill = GridBagConstraints.BOTH;
73 constraints.gridwidth = GridBagConstraints.REMAINDER;
74 JScrollPane listScroll = new JScrollPane(playlistList);
75 gridbag.setConstraints(listScroll, constraints);
76 getContentPane().add(listScroll);
77
78 JPanel buttonPanel = new JPanel();
79 buttonPanel.setLayout(new GridLayout(1, 0, 10, 0));
80
81 createButton = new JButton("Create");
82 createButton.addActionListener(this);
83 buttonPanel.add(createButton);
84
85 addButton = new JButton("Add");
86 addButton.addActionListener(this);
87 buttonPanel.add(addButton);
88
89 cancelButton = new JButton("Cancel");
90 cancelButton.addActionListener(this);
91 buttonPanel.add(cancelButton);
92
93 constraints.gridwidth = GridBagConstraints.REMAINDER;
94 constraints.weightx = 1;
95 gridbag.setConstraints(buttonPanel, constraints);
96 getContentPane().add(buttonPanel);
97
98 pack();
99 setLocationRelativeTo(parent);
100 setVisible(true);
101 }
102
103
104 public void actionPerformed(ActionEvent evt) {
105 if (evt.getSource() == createButton) {
106 String newPlaylist =
107 JOptionPane.showInputDialog(parent,
108 "Please enter a new Playlist name",
109 "New Playlist Name",
110 JOptionPane.QUESTION_MESSAGE);
111 if (newPlaylist == null) {
112 return;
113 }
114 int newKey = database.createPlaylist(newPlaylist);
115 try {
116 database.addToPlaylist(selectedTracks, newKey);
117 } catch (Exception exp) {
118 Logging.fatal("Exception occured");
119 Logging.fatal(exp);
120
121 System.err.println("An exception occured. Please check the log file.");
122 String message = "An exception occured. Please check the log file.\n";
123 message += "Exception: " + exp + "\n";
124 if (exp.getMessage() != null)
125 message += "Message: " + exp.getMessage();
126 JOptionPane.showMessageDialog(parent, message,
127 "Neuros Database Manipulator",
128 JOptionPane.ERROR_MESSAGE);
129 }
130 dispose();
131 ((MainFrame)parent).dataChanged();
132 }
133
134 if (evt.getSource() == addButton) {
135 int index = playlistList.getSelectedIndex();
136 if (index == -1) {
137 dispose();
138 return;
139 }
140 PlaylistSelectorListModel model =
141 (PlaylistSelectorListModel)playlistList.getModel();
142 PlaylistDisplayRecord rec = model.getDisplayRecordAt(index);
143 try {
144 database.addToPlaylist(selectedTracks, rec.getKey());
145 } catch (Exception exp) {
146 Logging.fatal("Exception occured");
147 Logging.fatal(exp);
148
149 System.err.println("An exception occured. Please check the log file.");
150 String message = "An exception occured. Please check the log file.\n";
151 message += "Exception: " + exp + "\n";
152 if (exp.getMessage() != null)
153 message += "Message: " + exp.getMessage();
154 JOptionPane.showMessageDialog(parent, message,
155 "Neuros Database Manipulator",
156 JOptionPane.ERROR_MESSAGE);
157 }
158 dispose();
159 ((MainFrame)parent).dataChanged();
160 }
161
162 if (evt.getSource() == cancelButton) {
163 dispose();
164 }
165 }
166
167
168 private class PlaylistSelectorListModel extends AbstractListModel {
169
170 ArrayList playlists;
171
172 PlaylistSelectorListModel(Database database) {
173 playlists = database.query(Database.DB_AUDIO, Database.DB_PLAYLISTS);
174 }
175
176 public int getSize() {
177 return playlists.size();
178 }
179
180 public Object getElementAt(int index) {
181 DisplayIndex displayIndex = (DisplayIndex)playlists.get(index);
182 PlaylistDisplayRecord playlist =
183 (PlaylistDisplayRecord)database.query(displayIndex);
184 return playlist.getName();
185 }
186
187 PlaylistDisplayRecord getDisplayRecordAt(int index) {
188 DisplayIndex displayIndex = (DisplayIndex)playlists.get(index);
189 return (PlaylistDisplayRecord)database.query(displayIndex);
190 }
191
192 void fireDataChanged() {
193 fireIntervalAdded(this, 0, playlists.size());
194 }
195 }
196 }