View Javadoc

1   package com.melloware.jukes.gui.view.node;
2   
3   import java.text.MessageFormat;
4   import java.util.Iterator;
5   
6   import javax.swing.Icon;
7   
8   import org.apache.commons.lang.StringUtils;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  import com.jgoodies.uif.util.ResourceUtils;
13  import com.melloware.jukes.db.HibernateDao;
14  import com.melloware.jukes.db.orm.Artist;
15  import com.melloware.jukes.db.orm.Disc;
16  import com.melloware.jukes.gui.tool.Resources;
17  
18  /**
19   * This class represents ARTISTS in the navigation tree.
20   * <p>
21   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
22   * @author Emil A. Lefkof III <info@melloware.com>
23   * @version 4.0
24   *
25   * @see com.melloware.jukes.db.orm.Artist
26   */
27  public final class ArtistNode
28      extends AbstractTreeNode {
29  
30      private static final Log LOG = LogFactory.getLog(ArtistNode.class);
31  
32      /**
33       * Constructs a <code>ArtistNode</code> for the given parent and artist.
34       * <p>
35       * @param aParent this node's parent
36       * @param aModel the associated model, an instance of Artist
37       */
38      public ArtistNode(NavigationNode aParent, Artist aModel) {
39          super(aParent, aModel);
40          this.settings = ((AbstractTreeNode)aParent).settings;
41      }
42  
43      /**
44       * Returns this node's associated Artist instance.
45       * <p>
46       * @return this node's associated Artist instance.
47       * @see NavigationNode#getModel()
48       */
49      public Artist getArtist() {
50          return (Artist)getModel();
51      }
52  
53      /**
54       * Returns this node's icon, ignores the selection.
55       * The icons is requested from a global resource repository.
56       * <p>
57       * @return this node's icon.
58       */
59      public Icon getIcon(final boolean sel) {
60          Icon icon = null;
61          if (StringUtils.equalsIgnoreCase(getArtist().getName(), "grateful dead")) {
62              icon = Resources.DISC_GD_ICON;
63          } else if (StringUtils.equalsIgnoreCase(getArtist().getName(), "pink floyd")) {
64              icon = Resources.DISC_PF_ICON;
65          } else if (StringUtils.equalsIgnoreCase(getArtist().getName(), "who")) {
66              icon = Resources.DISC_WHO_ICON;
67          } else if (StringUtils.equalsIgnoreCase(getArtist().getName(), "beatles")) {
68              icon = Resources.DISC_BEATLES_ICON;
69          } else if (StringUtils.equalsIgnoreCase(getArtist().getName(), "rolling stones")) {
70              icon = Resources.DISC_STONES_ICON;
71          } else {
72              icon = Resources.ARTIST_TREE_ICON;
73          }
74          return icon;
75      }
76  
77      /**
78       * Returns this node's name, the identifier of the associated artist.
79       * <p>
80       * @return this node's name
81       */
82      public String getName() {
83          return getArtist().getName();
84      }
85  
86      /* (non-Javadoc)
87       * @see com.melloware.jukes.gui.tool.node.AbstractTreeNode#loadChildren()
88       */
89      public void loadChildren() {
90          if (!childrenLoaded) {
91              loadingChildren = true;
92              LOG.debug("Loading children");
93  
94              Iterator iterator = null;
95              final String filter = this.settings.getFilter();
96  
97              // if a filter was applied the use the filter HQL
98              if (StringUtils.isNotBlank(filter)) {
99                  final String resource = ResourceUtils.getString("hql.filter.disc");
100                 final String hql = MessageFormat.format(resource, new Object[] { getArtist().getId(), filter });
101                 LOG.debug(hql);
102                 iterator = HibernateDao.findByQuery(hql).iterator();
103             } else {
104                 // else just get all discs for this artist
105                 iterator = getArtist().getDiscs().iterator();
106             }
107 
108             // now loop through and add the children
109             while (iterator.hasNext()) {
110                 final Disc disc = (Disc)iterator.next();
111                 if (LOG.isDebugEnabled()) {
112                     LOG.debug("Loading disc " + disc.getName());
113                 }
114                 this.add(new DiscNode(this, disc));
115             }
116             loadingChildren = false;
117             childrenLoaded = true;
118         }
119 
120     }
121 
122 }