/[svn]/jlscp/trunk/src/org/linuxsampler/lscp/DbInstrumentInfo.java
ViewVC logotype

Contents of /jlscp/trunk/src/org/linuxsampler/lscp/DbInstrumentInfo.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1202 - (show annotations) (download)
Thu May 24 20:17:25 2007 UTC (16 years, 11 months ago) by iliev
File size: 7226 byte(s)
* updated to version 0.5a

1 /*
2 * jlscp - a java LinuxSampler control protocol API
3 *
4 * Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com>
5 *
6 * This file is part of jlscp.
7 *
8 * jlscp is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * jlscp is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with jlscp; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23 package org.linuxsampler.lscp;
24
25 import java.text.DateFormat;
26 import java.text.NumberFormat;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29
30 import java.util.Date;
31
32
33 /**
34 * Provides information about a database instrument.
35 * @author Grigor Iliev
36 */
37 public class DbInstrumentInfo extends AbstractInstrument implements Parseable {
38 private String directoryPath = null;
39 private final Date dateCreated = new EnhancedDate();
40 private final Date dateModified = new EnhancedDate();
41 private String formatFamily = "";
42 private String formatVersion = "";
43 private long size = 0;
44 private boolean drum = false;
45 private String product = "";
46 private String artists = "";
47 private String keywords = "";
48 private boolean showAbsolutePath = false;
49
50 private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
51 private static DateFormat dateFormat2 = DateFormat.getInstance();
52 private static NumberFormat numberFormat = NumberFormat.getInstance();
53
54 static {
55 numberFormat.setMinimumFractionDigits(0);
56 numberFormat.setMaximumFractionDigits(1);
57 }
58
59 /**
60 * Creates a new instance of <code>DbInstrumentInfo</code>.
61 * @param resultSet An array with information categories about a DB instrument.
62 */
63 public
64 DbInstrumentInfo(String[] resultSet) throws LscpException {
65 for(String s : resultSet)
66 if(!parse(s)) Client.getLogger().info(LscpI18n.getLogMsg("unknownLine", s));
67 }
68
69 /**
70 * Returns the absolute path name of the directory containing this instrument.
71 **/
72 public String
73 getDirectoryPath() { return directoryPath; }
74
75 /**
76 * Sets the absolute path name of the directory containing this instrument.
77 **/
78 public void
79 setDirectoryPath(String dir) { directoryPath = dir; }
80
81 /**
82 * Returns the absolute path name of this instrument or
83 * <code>null</code> if the directory path for this instrument is not set.
84 **/
85 public String
86 getInstrumentPath() {
87 if(getDirectoryPath() == null) return null;
88 if(getDirectoryPath().length() == 1) {
89 if(!getDirectoryPath().equals("/")) return null;
90 return getDirectoryPath() + getName();
91 }
92
93 return getDirectoryPath() + "/" + getName();
94 }
95
96 /**
97 * Returns the date when the instrument is created.
98 **/
99 public Date
100 getDateCreated() { return dateCreated; }
101
102 /**
103 * Returns the date when the instrument is last modified.
104 **/
105 public Date
106 getDateModified() { return dateModified; }
107
108 /**
109 * Returns the format family of the instrument.
110 **/
111 public String
112 getFormatFamily() { return formatFamily; }
113
114 /**
115 * Returns the format version of the instrument.
116 **/
117 public String
118 getFormatVersion() { return formatVersion; }
119
120 /**
121 * Returns the size of the instrument in bytes.
122 **/
123 public long
124 getSize() { return size; }
125
126 /**
127 * Gets a user friendly representation of the instruments size.
128 */
129 public String
130 getFormatedSize() {
131 String s;
132 long i = getSize();
133 if(i > 1024*1024*1024) {
134 double d = i;
135 d /= (1024*1024*1024);
136 s = numberFormat.format(d) + " GB";
137 } else if(i > 1024*1024) {
138 double d = i;
139 d /= (1024*1024);
140 s = numberFormat.format(d) + " MB";
141 } else if(i > 1024) {
142 double d = i;
143 d /= (1024);
144 s = numberFormat.format(d) + " KB";
145 } else {
146 s = numberFormat.format(i) + " bytes";
147 }
148
149 return s;
150 }
151
152 /**
153 * Determines whether the instrument is a drumkit or a chromatic instrument.
154 **/
155 public boolean
156 isDrum() { return drum; }
157
158 /**
159 * Returns the product title of the instrument.
160 **/
161 public String
162 getProduct() { return product; }
163
164 /**
165 * Lists the artist names.
166 **/
167 public String
168 getArtists() { return artists; }
169
170 /**
171 * Provides a list of keywords that refer to the instrument.
172 * Keywords are separated with semicolon and blank.
173 **/
174 public String
175 getKeywords() { return keywords; }
176
177 /**
178 * Parses a line of text.
179 * @param s The string to be parsed.
180 * @return <code>true</code> if the line has been processed, <code>false</code> otherwise.
181 * @throws LscpException If some error occurs.
182 */
183 public boolean
184 parse(String s) throws LscpException {
185 if (super.parse(s)) return true;
186
187 if(s.startsWith("CREATED: ")) {
188 s = s.substring("CREATED: ".length());
189 try { dateCreated.setTime(dateFormat.parse(s).getTime()); }
190 catch(ParseException e) { throw new LscpException(e.getMessage()); }
191 } else if(s.startsWith("MODIFIED: ")) {
192 s = s.substring("MODIFIED: ".length());
193 try { dateModified.setTime(dateFormat.parse(s).getTime()); }
194 catch(ParseException e) { throw new LscpException(e.getMessage()); }
195 } else if(s.startsWith("FORMAT_FAMILY: ")) {
196 formatFamily = s.substring("FORMAT_FAMILY: ".length());
197 } else if(s.startsWith("FORMAT_VERSION: ")) {
198 formatVersion = s.substring("FORMAT_VERSION: ".length());
199 } else if(s.startsWith("SIZE: ")) {
200 try { size = Long.parseLong(s.substring("SIZE: ".length())); }
201 catch(NumberFormatException x) {
202 throw new LscpException(LscpI18n.getLogMsg("CommandFailed!"), x);
203 }
204 } else if(s.startsWith("IS_DRUM: ")) {
205 drum = Boolean.parseBoolean(s.substring("IS_DRUM: ".length()));
206 } else if(s.startsWith("PRODUCT: ")) {
207 product = s.substring("PRODUCT: ".length());
208 } else if(s.startsWith("ARTISTS: ")) {
209 artists = s.substring("ARTISTS: ".length());
210 } else if(s.startsWith("KEYWORDS: ")) {
211 keywords = s.substring("KEYWORDS: ".length());
212 } else return false;
213
214 return true;
215 }
216
217 /**
218 * Determines whether the <code>toString()</code>
219 * method should return the instrument name or
220 * the absolute path name of the instrument.
221 * The default value is <code>false</code>.
222 */
223 public boolean
224 getShowAbsolutePath() { return showAbsolutePath; }
225
226 /**
227 * Sets whether the <code>toString()</code> method
228 * should return the absolute path name of the instrument.
229 * @param b If <code>true</code> the <code>toString()</code>
230 * method will return the absolute path name of the
231 * instrument instead of the instrument name.
232 */
233 public void
234 setShowAbsolutePath(boolean b) { showAbsolutePath = b; }
235
236 /**
237 * Returns the name or the absolute path name of
238 * the instrument as specified by {@link #getShowAbsolutePath}.
239 * @see #setShowAbsolutePath
240 */
241 public String
242 toString() {
243 if(getShowAbsolutePath()) return getInstrumentPath();
244 return getName();
245 }
246
247 private class EnhancedDate extends Date {
248 public String
249 toString() { return dateFormat2.format(this); }
250 }
251 }

  ViewVC Help
Powered by ViewVC