Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: entagged/common/Utils.java


1   /*
2    *  ********************************************************************   **
3    *  Copyright notice                                                       **
4    *  **                                     **
5    *  (c) 2003 Entagged Developpement Team                           **
6    *  http://www.sourceforge.net/projects/entagged                           **
7    *  **                                     **
8    *  All rights reserved                                                    **
9    *  **                                     **
10   *  This script is part of the Entagged project. The Entagged          **
11   *  project is free software; you can redistribute it and/or modify        **
12   *  it under the terms of the GNU General Public License as published by   **
13   *  the Free Software Foundation; either version 2 of the License, or      **
14   *  (at your option) any later version.                                    **
15   *  **                                     **
16   *  The GNU General Public License can be found at                         **
17   *  http://www.gnu.org/copyleft/gpl.html.                                  **
18   *  **                                     **
19   *  This copyright notice MUST APPEAR in all copies of the file!           **
20   *  ********************************************************************
21   */
22  package entagged.common;
23  
24  import entagged.gui.Entagged;
25  import java.io.File;
26  import java.text.DecimalFormat;
27  import java.util.*;
28  
29  
30  /**
31   *  This Class is a regrouping of all functions that could not fit in another
32   *  place or function of general use $Id: Utils.java,v 1.8 2003/09/29 13:05:18
33   *  kikidonk Exp $
34   *
35   * @author     Raphaël Slinckx (KiKiDonK)
36   * @version    v0.03
37   */
38  public class Utils {
39  
40    /**  String displayed to indicate that the id3v1 Genre is empty */
41    public final static String EMPTY = "- Empty -";
42    /**  Index equivalent of the Empty genre in ID3v1 */
43    public final static String EMPTY_INDEX = "127";
44    /**  Genre Associated with the 0xFF value in ID3v1 Tags */
45    public final static String FFGenre = "";
46  
47    /**  Path to the resources files */
48    public final static String RESOURCES_PATH = "../resources/";
49  
50    /**  Holds String array containing the fields for this version of ID3 tag */
51    public final static String[] STANDARD_ID3_FIELDS = {"TIT2", "TPE1", "TALB", "TYER", "COMM", "TRCK", "TCON"};
52    /**
53     *  String used, when the users select multiples files in the GUI, to indicate
54     *  that a field contains information that aren't equal
55     */
56    public final static String VARIES = "<var>";
57  
58    /**  Index Of the <varies> in the ID3v1genre comboBox */
59    public final static String VARIES_INDEX = "126";
60  
61    /**
62     *  Array containing the 126 genres defined for the ID3v1.1 plus winamp
63     *  specials
64     */
65    public final static String[] genres = {"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz",
66      "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative",
67      "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion",
68      "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock",
69      "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic",
70      "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
71      "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave",
72      "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro",
73      "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin", "Revival",
74      "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
75      "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata",
76      "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad",
77      "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall", VARIES, EMPTY};
78  
79    /**  The Software name that use the freedb server */
80    public final static String softwareName = "Entagged";
81  
82    /**  The Software version that use the freedb server */
83    public final static String softwareVersion = "v0.06";
84  
85    /**  The langage resources filenames */
86    private final static String[] langageFilenames = {"english.lang", "french.lang"};
87    /**  The corresponding langage Names in english */
88    private final static String[] langageNames = {"English", "French"};
89  
90    /**  Resolution of the screen */
91    public static double screenWidth, screenHeight;
92  
93  
94    /**
95     *  converts a Vector containing Object of File type , to an Array of File
96     *
97     * @param  v  a vector containing String objects only
98     * @return    a File[] from the vector v
99     */
100   public static File[] toFileArray( Vector v ) {
101   File[] listeFichiers = new File[v.size()];
102     for ( int i = 0; i < v.size(); i++ )
103       listeFichiers[i] = ( (File)v.elementAt( i ) );
104     return listeFichiers;
105   }
106 
107 
108   /**
109    *  converts a Vector containing Object of String type , to an Array of String
110    *
111    * @param  v  a vector containing String objects only
112    * @return    a String[] from the vector v
113    */
114   public static String[] toStringArray( Vector v ) {
115   String[] stringArray = new String[v.size()];
116     for ( int i = 0; i < v.size(); i++ )
117       stringArray[i] = ( (String)v.elementAt( i ) );
118     return stringArray;
119   }
120 
121 
122   /**
123    *  Computes String representation of the genre Index b
124    *
125    * @param  b  the index of the genre
126    * @return    the String representing the genre, given the index b
127    */
128   public static String translateGenre( int b ) {
129     int ub = (b<0) ? (256+b) : b;
130     if ( ub == -1 || ub>genres.length-2)
131       return FFGenre;
132     return genres[b];
133   }
134 
135 
136   /**
137    *  Translate a genre in plain text in an index for id3v1 Genre (using a
138    *  hashtable)
139    *
140    * @param  name  the name of the index
141    * @return       the index number as a string of the given genre
142    */
143   public static String translateGenre( String name ) {
144   Hashtable translateTable = new Hashtable( 128, 1.0f );
145     for ( int i = 0; i < genres.length; i++ )
146       translateTable.put( genres[i].toLowerCase(), new Integer( i ).toString() );
147 
148     if ( translateTable.get( name.toLowerCase() ) == null )
149       return "0";
150     else
151       return (String)translateTable.get( name.toLowerCase() );
152   }
153 
154 
155   /**
156    *  Returns the name of the given langage file
157    *
158    * @param  langageFilename  Description of the Parameter
159    * @return                  the name of the langage in English
160    */
161   public static String translateLangageFilename( String langageFilename ) {
162   Hashtable translateTable = new Hashtable( 10, 1.0f );
163     for ( int i = 0; i < langageFilenames.length; i++ )
164       translateTable.put( langageFilenames[i], langageNames[i] );
165     return (String)translateTable.get( langageFilename );
166   }
167 
168 
169   /**
170    *  Returns the Filename of the given langage name
171    *
172    * @param  langageName  the langage name
173    * @return              the langage filename
174    */
175   public static String translateLangageName( String langageName ) {
176   Hashtable translateTable = new Hashtable( 10, 1.0f );
177     for ( int i = 0; i < langageFilenames.length; i++ )
178       translateTable.put( langageNames[i], langageFilenames[i] );
179     return (String)translateTable.get( langageName );
180   }
181 
182 
183   /**
184    *  Converts the Java signed bytes into more useful short - unsigned bytes
185    *
186    * @param  b  a byte array to be converted
187    * @return    a short array containing the unsigned bytes
188    */
189   public static short[] unsignedBytes( byte[] b ) {
190   short[] output = new short[b.length];
191     for ( int i = 0; i < b.length; i++ )
192       output[i] = ( b[i] < 0 ) ? (short)( b[i] + 256 ) : b[i];
193     return output;
194   }
195   
196   public static String formatTrackNumber(int trackNumber, int totalTrackNumber, String singleTrackFormat, String totalTrackFormat, String trackPattern){
197       String formattedTrackNumber = trackPattern.replaceAll("%1",(new DecimalFormat(singleTrackFormat)).format(new Integer(trackNumber)));
198       formattedTrackNumber = formattedTrackNumber.replaceAll("%2",(new DecimalFormat(totalTrackFormat)).format(new Integer(totalTrackNumber)));
199       return formattedTrackNumber;
200   }
201   
202   public static String formatTrackNumber(int trackNumber, String singleTrackFormat, String trackPattern){
203     return formatTrackNumber(trackNumber, 0, singleTrackFormat, "", trackPattern);
204   }
205   
206   public static String getFormattedLength(int length) {
207     //System.out.println("utils length: "+length);
208     if ( length != -1 ) {
209     int minutes = length / 60;
210     int sec = ( length - minutes * 60 );
211     DecimalFormat fmt = new DecimalFormat( "00" );
212       return fmt.format( minutes ) + ":" + fmt.format( sec );
213     }
214     else
215       return Entagged.langage.getProperty( "common.misc.error" );
216   }
217     
218 }
219