/* License see bottom */
package jtrackbase.gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import jonkoshare.gui.CheckBox;
import jonkoshare.gui.FileInputField;
import jtrackbase.Constants;
import jtrackbase.db.Artist;
import jtrackbase.db.ArtistFacade;
import jtrackbase.db.ArtistRelation;
import jtrackbase.db.Genre;
import jtrackbase.db.Label;
import jtrackbase.db.LabelFacade;
import jtrackbase.db.Medium;
import jtrackbase.db.MediumType;
import jtrackbase.db.MediumTypeFacade;
import jtrackbase.db.Tag;
import jtrackbase.db.TagFacade;
import jtrackbase.db.Track;
public class MediumPanel extends DataInputPanel<Medium> {
private static final Logger log = Logger.getLogger(MediumPanel.class
.getName());
private static final String DURATION_INITIAL_CONTENT = "00:00:00";
private static int TRACK_OFFSET = 3;
public MediumPanel(MediumType mt) {
this(new Medium(mt));
}
public MediumPanel(Medium m) {
this.trackCount = m.getTracks().size() == 0 ? m.getMediumType()
.getTrackCount() : m.getTracks().size();
medium = m;
loadGenres();
createUI();
loadData();
}
private void loadGenres() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("trackbase");
EntityManager em = factory.createEntityManager();
@SuppressWarnings("unchecked")
List<Genre> g = em
.createNativeQuery("select * from Genre", Genre.class)
.getResultList();
genres = Collections.unmodifiableCollection(g);
}
private void createUI() {
setLayout(new BorderLayout());
add(createHeaderPanel(), BorderLayout.NORTH);
add(new JScrollPane(createTrackPanel()), BorderLayout.CENTER);
}
private JPanel createHeaderPanel() {
JPanel hp = new JPanel();
hp.setBorder(createBorder(medium.getMediumType().getName()));
hp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHEAST;
gbc.insets = new Insets(2, 2, 2, 2);
hp.add(new JLabel("Name:"), gbc);
gbc.gridy++;
hp.add(new JLabel("Künstler:"), gbc);
gbc.gridy++;
hp.add(new JLabel("Bild:"), gbc);
gbc.gridy++;
hp.add(new JLabel("Kommentar:"), gbc);
gbc.gridy++;
hp.add(new JLabel("Tags:"), gbc);
gbc.gridx = 2;
gbc.gridy = 0;
hp.add(new JLabel("Jahr:"), gbc);
gbc.gridy++;
hp.add(new JLabel("Wert:"), gbc);
gbc.gridx = 4;
gbc.gridy = 0;
hp.add(new JLabel("Label:"), gbc);
gbc.gridy++;
hp.add(new JLabel("Typ:"), gbc);
nameField.setColumns(Constants.TEXT_FIELD_COLUMNS);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
hp.add(nameField, gbc);
artistField.setColumns(Constants.TEXT_FIELD_COLUMNS);
gbc.gridy++;
hp.add(artistField, gbc);
if (medium.getMediumType().getRelation() != ArtistRelation.PER_MEDIUM) {
artistField.setEnabled(false);
}
log.finest("adding image field");
gbc.gridy++;
hp.add(imageFileField, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
yearField.setColumns(Constants.YEAR_FIELD_COLUMNS);
hp.add(yearField, gbc);
gbc.gridy++;
valueField.setColumns(Constants.CURRENCY_FIELD_COLUMNS);
hp.add(valueField, gbc);
gbc.gridx = 5;
gbc.gridy = 0;
labelField.setColumns(Constants.SHORT_TEXT_FIELD_COLUMNS);
hp.add(labelField, gbc);
gbc.gridy++;
typeBox.setModel(new DefaultComboBoxModel(new Vector<MediumType>(
MediumTypeFacade.findAll())));
ListCellRenderer lcr = new MediumTypeListRenderer();
typeBox.setRenderer(lcr);
gbc.fill = GridBagConstraints.NONE;
hp.add(typeBox, gbc);
commentField.setRows(Constants.TEXT_AREA_ROWS);
commentField.setColumns(Constants.TEXT_AREA_COLUMNS);
gbc.gridy = 3;
gbc.gridx = 1;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = GridBagConstraints.REMAINDER;
hp.add(new JScrollPane(commentField), gbc);
int idx = 0;
gbc.gridy++;
JPanel tagPanel = new JPanel();
Collection<Tag> tags = TagFacade.findMediumFlags();
tagPanel
.setLayout(new GridLayout(tags.size()
/ Constants.TAGS_PER_ROW_MEDIUM,
Constants.TAGS_PER_ROW_MEDIUM));
for (Tag t : tags) {
CheckBox<Tag> cb = new CheckBox<Tag>(t,
new CheckBox.Renderer<Tag>() {
@Override
public String renderLabel(Tag t) {
return t.getName();
}
});
tagBoxes.add(cb);
tagPanel.add(cb);
idx++;
}
hp.add(tagPanel, gbc);
return hp;
}
private JPanel createTrackPanel() {
JPanel tp = new JPanel();
tp.setBorder(createBorder("Titel"));
tp.setLayout(new GridBagLayout());
List<Genre> g2 = new ArrayList<Genre>(genres);
g2.add(0, Genre.NO_GENRE);
ListCellRenderer lcr = new GenreListRenderer();
int cnt = 0;
int increment;
if (medium.getMediumType().getRelation() != ArtistRelation.PER_MEDIUM) {
increment=2;
} else {
increment=1;
}
for (int i = 0; i < 2 * trackCount; i += increment) {
ComboBoxModel cbm1 = new DefaultComboBoxModel(genres
.toArray(new Genre[genres.size()]));
ComboBoxModel cbm2 = new DefaultComboBoxModel(g2.toArray(new Genre[g2
.size()]));
addTrackLine(tp, i, cbm1, cbm2, lcr, ++cnt);
}
return tp;
}
private void addTrackLine(JPanel jp, int idx, ComboBoxModel cbm1,
ComboBoxModel cbm2, ListCellRenderer lcr, int num) {
TrackLine tl = new TrackLine();
tl.genre1.setModel(cbm1);
tl.genre1.setRenderer(lcr);
tl.genre2.setModel(cbm2);
tl.genre2.setRenderer(lcr);
trackLines.add(tl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = TRACK_OFFSET + idx;
gbc.gridx = 0;
gbc.insets = new Insets(0, 0, 0, 1);
jp.add(new JLabel("" + num), gbc);
gbc.gridx++;
jp.add(tl.trackName, gbc);
if (medium.getMediumType().getRelation() != ArtistRelation.PER_MEDIUM) {
gbc.gridy++;
jp.add(tl.artistName, gbc);
gbc.gridy--;
}
gbc.gridx++;
tl.duration.setColumns(8);
jp.add(tl.duration, gbc);
gbc.gridx++;
jp.add(tl.genre1, gbc);
gbc.gridx++;
jp.add(tl.genreAssign, gbc);
gbc.gridx++;
jp.add(tl.genre2, gbc);
gbc.gridx++;
jp.add(tl.bpm, gbc);
}
@Override
public Medium getData() {
medium.setName(nameField.getText());
medium.setComment(commentField.getText());
medium.setYearReleased(((Number) yearField.getValue()).intValue());
medium.setValue(((Number) valueField.getValue()).intValue() * 100);
medium.setMediumType((MediumType) typeBox.getSelectedItem());
Collection<Label> labels=LabelFacade.findByName(labelField.getText());
Label label;
if (labels==null || labels.size()==0) {
label=new Label();
label.setName(labelField.getText());
} else if (labels.size()==1) {
label=labels.iterator().next();
} else {
throw new UnsupportedOperationException("There ought to popup a list");
}
medium.setLabel(label);
Collection<Artist> artists = ArtistFacade.findByName(artistField
.getText());
Artist artist;
if (artists == null || artists.size() == 0) {
artist = new Artist();
artist.setName(artistField.getText());
} else if (artists.size() == 1) {
artist = artists.iterator().next();
} else {
throw new UnsupportedOperationException("There ought to popup a list");
}
medium.setArtist(artist);
int idx = 0;
List<Track> tracksToAdd=new ArrayList<Track>();
for (TrackLine tl : trackLines) {
String name = tl.trackName.getText();
if (name == null || name.length() == 0) {
break;
}
int tCount = medium.getTracks().size();
Track t;
if (tCount == 0 || tCount < idx) {
t = new Track();
tracksToAdd.add(t);
} else {
t = medium.getTracks().get(idx);
}
t.setPosition(idx);
t.setName(tl.trackName.getText());
t.setMedium(medium);
Number bpmNum = (Number) tl.bpm.getValue();
if (bpmNum != null) {
t.setBpm(bpmNum.intValue());
}
t.setArtist(artist);
t.setGenre1((Genre) tl.genre1.getSelectedItem());
if (tl.genre2.getSelectedItem() == null) {
continue;
}
t.setGenreWeight(((Integer) tl.genreAssign.getSelectedItem())
.intValue());
t.setGenre2((Genre) tl.genre2.getSelectedItem());
t.setDuration(tl.duration.getText());
idx++;
}
if (tracksToAdd.size()>0) {
medium.addTracks(tracksToAdd);
}
for (CheckBox<Tag> cb : tagBoxes) {
if (cb.get() == null) {
continue;
}
medium.addTag(cb.get());
}
return medium;
}
@Override
public boolean isDataValid() {
errTxt=null;
if (artistField.getText() == null
|| artistField.getText().length() == 0) {
errTxt += "- Artist empty\n";
}
if (labelField.getText() == null
|| labelField.getText().length() == 0) {
errTxt += "- Label empty\n";
}
return errTxt==null || errTxt.length() == 0;
}
@Override
public void displayErrors() {
if (errTxt==null || errTxt.length() == 0) {
return;
}
JOptionPane.showMessageDialog(this,
"Following data is wrong/missing: \n" + errTxt,
"Input insufficient", JOptionPane.ERROR_MESSAGE);
}
private class TrackLine {
private TrackLine() {
trackName.setColumns(Constants.SHORT_TEXT_FIELD_COLUMNS);
trackName.setToolTipText("Track's Name");
artistName.setColumns(Constants.SHORT_TEXT_FIELD_COLUMNS);
artistName.setToolTipText("Artist performing track");
genreAssign.setModel(Constants.GENRE_ASSIGN_MODEL);
bpm.setColumns(Constants.BPM_FIELD_COLUMNS);
duration.setColumns(Constants.DURATION_FIELD_COLUMNS);
duration.setText(DURATION_INITIAL_CONTENT);
}
private JTextField trackName = new JTextField();
private JTextField artistName = new JTextField();
private JFormattedTextField bpm = new JFormattedTextField(
Constants.FORMAT_BPM);
private JComboBox genre1 = new JComboBox();
private JComboBox genre2 = new JComboBox();
private JComboBox genreAssign = new JComboBox();
private JFormattedTextField duration = new JFormattedTextField(
Constants.FORMAT_DURATION);
}
private void loadData() {
if (medium == null) {
return;
}
nameField.setText(medium.getName());
commentField.setText(medium.getComment());
yearField.setValue(medium.getYearReleased());
valueField.setValue(medium.getValue() / 100.0);
labelField.setText(medium.getLabel() == null ? null : medium.getLabel()
.getName());
typeBox.setSelectedItem(medium.getMediumType());
int idx = 0;
for (Track t : medium.getTracks()) {
TrackLine tl = trackLines.get(idx);
tl.trackName.setText(t.getName());
tl.artistName.setText(t.getArtist() == null ? null : t.getArtist()
.getName());
tl.bpm.setValue(t.getBpm());
tl.duration.setValue(t.getDurationTime());
tl.genre1.setSelectedItem(t.getGenre1());
tl.genreAssign.setSelectedItem(t.getGenreWeight());
if (t.getGenre2() == null) {
tl.genre2.setSelectedIndex(0);
} else {
tl.genre2.setSelectedItem(t.getGenre2());
}
idx++;
}
List<Tag> mTags = medium.getTags();
if (mTags == null) {
return;
}
for (CheckBox<Tag> cb : tagBoxes) {
if (mTags.contains(cb.getValue())) {
cb.setSelected(true);
}
}
}
private int trackCount;
private Collection<Genre> genres;
private Medium medium;
private String errTxt;
private JComboBox typeBox = new JComboBox();
private JTextField nameField = new JTextField();
private JTextField artistField = new JTextField();
private JTextField labelField = new JTextField();
private JTextArea commentField = new JTextArea();
private JFormattedTextField yearField = new JFormattedTextField(
Constants.FORMAT_YEAR);
private JFormattedTextField valueField = new JFormattedTextField(
Constants.FORMAT_CURRENCY);
private FileInputField imageFileField = new FileInputField(true);
private List<TrackLine> trackLines = new ArrayList<TrackLine>();
private List<CheckBox<Tag>> tagBoxes = new ArrayList<CheckBox<Tag>>();
}
/*
* Copyright (C) 2008 Onkobu Tanaake
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program (gplv3.txt).
*/