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
27
28
29
30
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
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
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
63 timespan = new TimeSpan(totalTime * 1000);
64 data.setTimeTotal(timespan.toString());
65 timespan = new TimeSpan(avgTimePerDisc);
66 data.setTimeAveragePerDisc(timespan.toString());
67
68
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
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
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
109
110
111
112 public String getCountArtists() {
113 return this.countArtists;
114 }
115
116
117
118
119
120
121 public String getCountDiscs() {
122 return this.countDiscs;
123 }
124
125
126
127
128
129
130 public String getCountTracks() {
131 return this.countTracks;
132 }
133
134
135
136
137
138
139 public String getFileTotal() {
140 return this.fileTotal;
141 }
142
143
144
145
146
147
148 public String getTimeAveragePerDisc() {
149 return this.timeAveragePerDisc;
150 }
151
152
153
154
155
156
157 public String getTimeTotal() {
158 return this.timeTotal;
159 }
160
161
162
163
164
165
166 public void setCountArtists(String aCountArtists) {
167 this.countArtists = aCountArtists;
168 }
169
170
171
172
173
174
175 public void setCountDiscs(String aCountDiscs) {
176 this.countDiscs = aCountDiscs;
177 }
178
179
180
181
182
183
184 public void setCountTracks(String aCountTracks) {
185 this.countTracks = aCountTracks;
186 }
187
188
189
190
191
192
193 public void setFileTotal(String aFileTotal) {
194 this.fileTotal = aFileTotal;
195 }
196
197
198
199
200
201
202 public void setTimeAveragePerDisc(String aTimeAveragePerDisc) {
203 this.timeAveragePerDisc = aTimeAveragePerDisc;
204 }
205
206
207
208
209
210
211 public void setTimeTotal(String aTimeTotal) {
212 this.timeTotal = aTimeTotal;
213 }
214
215
216
217
218
219
220 public String getFileAveragePerDisc() {
221 return this.fileAveragePerDisc;
222 }
223
224
225
226
227
228
229 public void setFileAveragePerDisc(String aFileAveragePerDisc) {
230 this.fileAveragePerDisc = aFileAveragePerDisc;
231 }
232
233 }
234
235
236
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 }