Package jtrackbase.gui

Source Code of jtrackbase.gui.TrackbaseFrame$ListGenreAction

/* License see bottom */
package jtrackbase.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;

import jonkoshare.gui.DropDownButton;
import jtrackbase.db.DBController;
import jtrackbase.db.Genre;
import jtrackbase.db.Medium;
import jtrackbase.db.MediumType;
import jtrackbase.db.MediumTypeFacade;

public class TrackbaseFrame extends JFrame
              implements ActionListener {
  private static final Logger log = Logger.getLogger(TrackbaseFrame.class
      .getName());

  private JButton cmb1;
  private JButton cmb2;
  private JButton cmb3;
  private Map<JButton, AbstractAction> lastActions=new HashMap<JButton, AbstractAction>();
 
  private interface ListPanelFactory {
    String getPanelName();
   
    AbstractListPanel<?> createInstance();
  }
 
  public TrackbaseFrame() {
    super("Trackbase");
    listPanelMap=new HashMap<String, AbstractListPanel<?>>();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    createUI();
    pack();
  }

// ActionListener
  public void actionPerformed(ActionEvent e) {
    Object source=e.getSource();
    if (source==cmb1) {
      invokeLastAction(cmb1, e);
    } else if (source==cmb2) {
      invokeLastAction(cmb2, e);
    } else if (source==cmb3) {
      invokeLastAction(cmb3, e);
    }
  }
 
  private void createUI() {
    setLayout(new BorderLayout());
    add(new JScrollPane(new TrackbaseTree()), BorderLayout.WEST);
    add(createToolBar(), BorderLayout.NORTH);
    centerPane = new JTabbedPane();
    add(centerPane, BorderLayout.CENTER);
  }

  private JToolBar createToolBar() {
    JToolBar tb = new JToolBar();
    tb.add(new CreateGenreAction());

    CreateMediaButton mediaButton = new CreateMediaButton();
    mediaButton.setIcon(new ImageIcon(getClass().getResource(
        "/jtrackbase/gui/new_medium.png")));
    cmb1=mediaButton.addToToolBar(tb);
    cmb1.addActionListener(this);
   
    mediaButton = new CreateMediaButton();
    mediaButton.setIcon(new ImageIcon(getClass().getResource(
        "/jtrackbase/gui/new_medium.png")));
    cmb2=mediaButton.addToToolBar(tb);
    cmb2.addActionListener(this);

    mediaButton = new CreateMediaButton();
    mediaButton.setIcon(new ImageIcon(getClass().getResource(
        "/jtrackbase/gui/new_medium.png")));
    cmb3=mediaButton.addToToolBar(tb);
    cmb3.addActionListener(this);

    ListButton lb = new ListButton();
    lb.setIcon(new ImageIcon(getClass().getResource(
        "/jtrackbase/gui/list.png")));
    lb.addToToolBar(tb);
    return tb;
  }

  private void showListPanel(ListPanelFactory fac) {
    AbstractListPanel<?> alp=listPanelMap.get(fac.getPanelName());
    if (alp==null) {
      alp=fac.createInstance();
      listPanelMap.put(fac.getPanelName(), alp);
      centerPane.add(fac.getPanelName(), alp);
    }
    alp.load();
    centerPane.setSelectedComponent(alp);
  }
 
  private void setLastAction(JButton jb, AbstractAction a) {
    lastActions.put(jb, a);
  }
 
  private void invokeLastAction(JButton jb, ActionEvent e) {
    AbstractAction aa=lastActions.get(jb);
    if (aa==null) {
      JOptionPane.showMessageDialog(this, "Keine Aktion gewählt", "Aktion nicht möglich", JOptionPane.WARNING_MESSAGE);
      return;
    }
    aa.actionPerformed(e);
  }
 
  private class CreateGenreAction extends AbstractAction {
    public CreateGenreAction() {
      super("G");
      putValue(SHORT_DESCRIPTION, "Genre");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      log.finest("Starting for genre");
      InputDialog<Genre> ip = new InputDialog<Genre>(TrackbaseFrame.this,
          new GenrePanel());
      ip.setVisible(true);
      Genre g = ip.getData();
      log.finest("Persisting: " + g);
      if (g == null) {
        return;
      }
      EntityManager em = DBController.getEntityManager();
      EntityTransaction trans = em.getTransaction();
      trans.begin();
      em.persist(g);
      trans.commit();
      em.close();
    }
  }

  private class CreateMediumAction extends AbstractAction {
    private final MediumType mediumType;
    private final JButton dropDownParent;
   
    public CreateMediumAction(MediumType mt) {
      this(null, mt);
    }
   
    public CreateMediumAction(JButton key, MediumType mt) {
      super(mt.getName());
      if (mt.getComment() != null && mt.getComment().length() > 0) {
        putValue(SHORT_DESCRIPTION, mt.getComment());
      }
      mediumType=mt;
      dropDownParent=key;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      setLastAction(dropDownParent, this);
      log.finest("Starting Album");
      InputDialog<Medium> ip = new InputDialog<Medium>(
          TrackbaseFrame.this, new MediumPanel(mediumType));
      ip.setVisible(true);
      log.finest("Nothing to persist yet");
      Medium m = ip.getData();
      log.finest("Persisting: " + m);
      if (m == null) {
        return;
      }
      EntityManager em = DBController.getEntityManager();
      EntityTransaction trans = em.getTransaction();
      trans.begin();
      em.merge(m);
      trans.commit();
      em.close();
    }

  }

  private class ListMediaAction extends AbstractAction {
    public ListMediaAction() {
      super(ListMediumPanel.PANEL_NAME);
      putValue(SHORT_DESCRIPTION, "List all media");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      showListPanel(new ListPanelFactory() {

        @Override
        public AbstractListPanel<?> createInstance() {
          return new ListMediumPanel();
        }

        @Override
        public String getPanelName() {
          return ListMediumPanel.PANEL_NAME;
        }
       
      });
    }
  }

  private class ListArtistAction extends AbstractAction {
    public ListArtistAction() {
      super(ListArtistPanel.PANEL_NAME);
      putValue(SHORT_DESCRIPTION, "List all artists");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      showListPanel(new ListPanelFactory() {

        @Override
        public AbstractListPanel<?> createInstance() {
          return new ListArtistPanel();
        }

        @Override
        public String getPanelName() {
          return ListArtistPanel.PANEL_NAME;
        }
       
      });
    }
  }

  private class ListGenreAction extends AbstractAction {
    public ListGenreAction() {
      super(ListGenrePanel.PANEL_NAME);
      putValue(SHORT_DESCRIPTION, "List all genres");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      showListPanel(new ListPanelFactory() {

        @Override
        public AbstractListPanel<?> createInstance() {
          return new ListGenrePanel();
        }

        @Override
        public String getPanelName() {
          return ListGenrePanel.PANEL_NAME;
        }
       
      });
    }
  }

  private class ListLabelAction extends AbstractAction {
    public ListLabelAction() {
      super(ListLabelPanel.PANEL_NAME);
      putValue(SHORT_DESCRIPTION, "List all genres");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      showListPanel(new ListPanelFactory() {

        @Override
        public AbstractListPanel<?> createInstance() {
          return new ListLabelPanel();
        }

        @Override
        public String getPanelName() {
          return ListLabelPanel.PANEL_NAME;
        }
       
      });
    }
  }

  private class ListMediumTypeAction extends AbstractAction {
    public ListMediumTypeAction() {
      super(ListMediumTypePanel.PANEL_NAME);
      putValue(SHORT_DESCRIPTION, "List all medium types");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      showListPanel(new ListPanelFactory() {

        @Override
        public AbstractListPanel<?> createInstance() {
          return new ListMediumTypePanel();
        }

        @Override
        public String getPanelName() {
          return ListMediumTypePanel.PANEL_NAME;
        }
       
      });
    }
  }

  private class CreateMediaButton extends DropDownButton {
    @Override
    protected JPopupMenu getPopupMenu() {
      JPopupMenu popup = new JPopupMenu();
      List<MediumType> types=new ArrayList<MediumType>(MediumTypeFacade.findAll());
      Collections.sort(types, MediumType.NAME_COMPARATOR);
      for (MediumType mt : types) {
        popup.add(new CreateMediumAction(getMainButton(), mt));
      }
      return popup;
    }
  }

  private class ListButton extends DropDownButton {
    @Override
    protected JPopupMenu getPopupMenu() {
      JPopupMenu popup = new JPopupMenu();
      popup.add(new ListMediaAction());
      popup.add(new ListArtistAction());
      popup.add(new ListGenreAction());
      popup.add(new ListMediumTypeAction());
      popup.add(new ListLabelAction());
      return popup;
    }
  }

  private JTabbedPane centerPane;

  private Map<String, AbstractListPanel<?>> listPanelMap;
}
/*
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).
*/
TOP

Related Classes of jtrackbase.gui.TrackbaseFrame$ListGenreAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.