View Javadoc

1   package com.melloware.jukes.gui.view.dialogs;
2   
3   import java.beans.BeanInfo;
4   import java.beans.PropertyChangeEvent;
5   import java.beans.PropertyChangeListener;
6   
7   import javax.swing.JPanel;
8   
9   import org.apache.commons.io.FileUtils;
10  
11  import com.l2fprod.common.beans.BaseBeanInfo;
12  import com.l2fprod.common.beans.ExtendedPropertyDescriptor;
13  import com.l2fprod.common.model.DefaultBeanInfoResolver;
14  import com.l2fprod.common.propertysheet.Property;
15  import com.l2fprod.common.propertysheet.PropertySheet;
16  import com.l2fprod.common.propertysheet.PropertySheetPanel;
17  import com.l2fprod.common.swing.LookAndFeelTweaks;
18  import com.melloware.jukes.db.HibernateDao;
19  import com.melloware.jukes.db.orm.Artist;
20  import com.melloware.jukes.db.orm.Disc;
21  import com.melloware.jukes.db.orm.Track;
22  import com.melloware.jukes.gui.tool.Resources;
23  import com.melloware.jukes.util.TimeSpan;
24  
25  /**
26   * A <code>PropertySheet</code> for the <code>Jukes</code> statistics.
27   * <p>
28   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
29   * @author Emil A. Lefkof III <info@melloware.com>
30   * @version 4.0
31   */
32  public final class PropertySheetApplicationStats
33      extends JPanel {
34  
35      public PropertySheetApplicationStats() {
36          setLayout(LookAndFeelTweaks.createVerticalPercentLayout());
37  
38          final Bean data = new Bean();
39  
40          TimeSpan timespan = null;
41  
42          // counts of objects
43          long discCount = HibernateDao.countAll(Disc.class);
44          data.setCountArtists(Long.toString(HibernateDao.countAll(Artist.class)));
45          data.setCountDiscs(Long.toString(discCount));
46          data.setCountTracks(Long.toString(HibernateDao.countAll(Track.class)));
47  
48          // time stats
49          long totalTime = 0;
50          long totalSize = 0;
51          long avgTimePerDisc = 0;
52          long avgSizePerDisc = 0;
53          try {
54              totalTime = HibernateDao.sum(Disc.class, Disc.PROPERTYNAME_DURATION);
55              totalSize = HibernateDao.sum(Track.class, Track.PROPERTYNAME_TRACK_SIZE);
56              avgTimePerDisc = (totalTime / discCount) * 1000;
57              avgSizePerDisc = (totalSize / discCount);
58          } catch (RuntimeException ex) {
59              totalTime = 0;
60          }
61  
62          // calculate time fields to display properly
63          timespan = new TimeSpan(totalTime * 1000);
64          data.setTimeTotal(timespan.toString());
65          timespan = new TimeSpan(avgTimePerDisc);
66          data.setTimeAveragePerDisc(timespan.toString());
67  
68          // file stats
69          data.setFileTotal(FileUtils.byteCountToDisplaySize(totalSize));
70          data.setFileAveragePerDisc(FileUtils.byteCountToDisplaySize(avgSizePerDisc));
71  
72          DefaultBeanInfoResolver resolver = new DefaultBeanInfoResolver();
73          BeanInfo beanInfo = resolver.getBeanInfo(data);
74  
75          PropertySheetPanel sheet = new PropertySheetPanel();
76          sheet.setMode(PropertySheet.VIEW_AS_CATEGORIES);
77          sheet.setProperties(beanInfo.getPropertyDescriptors());
78          sheet.readFromObject(data);
79          sheet.setDescriptionVisible(true);
80          sheet.setSortingCategories(true);
81          sheet.setSortingProperties(true);
82          add(sheet, "*");
83  
84          // everytime a property change, update the button with it
85          PropertyChangeListener listener = new PropertyChangeListener() {
86              public void propertyChange(PropertyChangeEvent evt) {
87                  Property prop = (Property)evt.getSource();
88                  prop.writeToObject(data);
89              }
90          };
91          sheet.addPropertySheetChangeListener(listener);
92      }
93  
94      /**
95       * Class used to hold the property info.
96       */
97      public static class Bean {
98  
99          private String countArtists;
100         private String countDiscs;
101         private String countTracks;
102         private String fileTotal;
103         private String fileAveragePerDisc;
104         private String timeAveragePerDisc;
105         private String timeTotal;
106 
107         /**
108          * Gets the countArtists.
109          * <p>
110          * @return Returns the countArtists.
111          */
112         public String getCountArtists() {
113             return this.countArtists;
114         }
115 
116         /**
117          * Gets the countDiscs.
118          * <p>
119          * @return Returns the countDiscs.
120          */
121         public String getCountDiscs() {
122             return this.countDiscs;
123         }
124 
125         /**
126          * Gets the countTracks.
127          * <p>
128          * @return Returns the countTracks.
129          */
130         public String getCountTracks() {
131             return this.countTracks;
132         }
133 
134         /**
135          * Gets the fileTotal.
136          * <p>
137          * @return Returns the fileTotal.
138          */
139         public String getFileTotal() {
140             return this.fileTotal;
141         }
142 
143         /**
144          * Gets the timeAveragePerDisc.
145          * <p>
146          * @return Returns the timeAveragePerDisc.
147          */
148         public String getTimeAveragePerDisc() {
149             return this.timeAveragePerDisc;
150         }
151 
152         /**
153          * Gets the timeTotal.
154          * <p>
155          * @return Returns the timeTotal.
156          */
157         public String getTimeTotal() {
158             return this.timeTotal;
159         }
160 
161         /**
162          * Sets the countArtists.
163          * <p>
164          * @param aCountArtists The countArtists to set.
165          */
166         public void setCountArtists(String aCountArtists) {
167             this.countArtists = aCountArtists;
168         }
169 
170         /**
171          * Sets the countDiscs.
172          * <p>
173          * @param aCountDiscs The countDiscs to set.
174          */
175         public void setCountDiscs(String aCountDiscs) {
176             this.countDiscs = aCountDiscs;
177         }
178 
179         /**
180          * Sets the countTracks.
181          * <p>
182          * @param aCountTracks The countTracks to set.
183          */
184         public void setCountTracks(String aCountTracks) {
185             this.countTracks = aCountTracks;
186         }
187 
188         /**
189          * Sets the fileTotal.
190          * <p>
191          * @param aFileTotal The fileTotal to set.
192          */
193         public void setFileTotal(String aFileTotal) {
194             this.fileTotal = aFileTotal;
195         }
196 
197         /**
198          * Sets the timeAveragePerDisc.
199          * <p>
200          * @param aTimeAveragePerDisc The timeAveragePerDisc to set.
201          */
202         public void setTimeAveragePerDisc(String aTimeAveragePerDisc) {
203             this.timeAveragePerDisc = aTimeAveragePerDisc;
204         }
205 
206         /**
207          * Sets the timeTotal.
208          * <p>
209          * @param aTimeTotal The timeTotal to set.
210          */
211         public void setTimeTotal(String aTimeTotal) {
212             this.timeTotal = aTimeTotal;
213         }
214 
215 		/**
216 		 * Gets the fileAveragePerDisc.
217 		 * <p>
218 		 * @return Returns the fileAveragePerDisc.
219 		 */
220 		public String getFileAveragePerDisc() {
221 			return this.fileAveragePerDisc;
222 		}
223 
224 		/**
225 		 * Sets the fileAveragePerDisc.
226 		 * <p>
227 		 * @param aFileAveragePerDisc The fileAveragePerDisc to set.
228 		 */
229 		public void setFileAveragePerDisc(String aFileAveragePerDisc) {
230 			this.fileAveragePerDisc = aFileAveragePerDisc;
231 		}
232 
233     }
234 
235     /**
236      * Class used to hold the property sheet descriptor info.
237      */
238     public static class BeanBeanInfo
239         extends BaseBeanInfo {
240 
241         public BeanBeanInfo() {
242             super(Bean.class);
243             ExtendedPropertyDescriptor descriptor = null;
244 
245             descriptor = addProperty("countArtists");
246             descriptor.setCategory("Counts");
247             descriptor.setDisplayName(Resources.getString("label.CountArtist"));
248             descriptor.setShortDescription(Resources.getString("label.CountArtistMessage"));
249 
250             descriptor = addProperty("countDiscs");
251             descriptor.setCategory("Counts");
252             descriptor.setDisplayName(Resources.getString("label.CountDisc"));
253             descriptor.setShortDescription(Resources.getString("label.CountDiscMessage"));
254 
255             descriptor = addProperty("countTracks");
256             descriptor.setCategory("Counts");
257             descriptor.setDisplayName(Resources.getString("label.CountTrack"));
258             descriptor.setShortDescription(Resources.getString("label.CountTrackMessage"));
259 
260             descriptor = addProperty("timeTotal");
261             descriptor.setCategory("Times");
262             descriptor.setDisplayName(Resources.getString("label.TimeTotal"));
263             descriptor.setShortDescription(Resources.getString("label.TimeTotalMessage"));
264 
265             descriptor = addProperty("timeAveragePerDisc");
266             descriptor.setCategory("Times");
267             descriptor.setDisplayName(Resources.getString("label.TimeAvg"));
268             descriptor.setShortDescription(Resources.getString("label.TimeAvgMessage"));
269 
270             descriptor = addProperty("fileTotal");
271             descriptor.setCategory("File");
272             descriptor.setDisplayName(Resources.getString("label.FileTotal"));
273             descriptor.setShortDescription(Resources.getString("label.FileTotalMessage"));
274             
275             descriptor = addProperty("fileAveragePerDisc");
276             descriptor.setCategory("File");
277             descriptor.setDisplayName(Resources.getString("label.FileAvg"));
278             descriptor.setShortDescription(Resources.getString("label.FileAvgMessage"));
279         }
280     }
281 
282 }