| Classes in this Package | Line Coverage | Branch Coverage | Complexity | ||||||||
| DirectoryVisitor |
|
| 2.142857142857143;2,143 |
| 1 | /** |
|
| 2 | * Redistribution and use in source and binary forms, with or without |
|
| 3 | * modification, are permitted provided that the following conditions are |
|
| 4 | * met : |
|
| 5 | * |
|
| 6 | * . Redistributions of source code must retain the above copyright |
|
| 7 | * notice, this list of conditions and the following disclaimer. |
|
| 8 | * |
|
| 9 | * . Redistributions in binary form must reproduce the above copyright |
|
| 10 | * notice, this list of conditions and the following disclaimer in the |
|
| 11 | * documentation and/or other materials provided with the distribution. |
|
| 12 | * |
|
| 13 | * . The name of the author may not be used to endorse or promote products |
|
| 14 | * derived from this software without specific prior written permission. |
|
| 15 | * |
|
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
| 19 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|
| 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
| 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
| 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
| 24 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
| 25 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
|
| 27 | * |
|
| 28 | * $Id$ |
|
| 29 | */ |
|
| 30 | ||
| 31 | package palmed.file; |
|
| 32 | ||
| 33 | import java.io.IOException; |
|
| 34 | import java.util.Enumeration; |
|
| 35 | import java.util.Vector; |
|
| 36 | import javax.microedition.lcdui.Image; |
|
| 37 | import javax.microedition.lcdui.List; |
|
| 38 | ||
| 39 | /** |
|
| 40 | * This class retrieves the content of a given directory. |
|
| 41 | * |
|
| 42 | * @author Mathieu Champlon |
|
| 43 | * @version $Revision$ $Date$ |
|
| 44 | */ |
|
| 45 | public final class DirectoryVisitor implements IDirectoryVisitor |
|
| 46 | { |
|
| 47 | /** |
|
| 48 | * The retrieved directories and files. |
|
| 49 | */ |
|
| 50 | private final Vector directories_, files_; |
|
| 51 | /** |
|
| 52 | * The directory and file icons. |
|
| 53 | */ |
|
| 54 | private final Image directoryIcon_, fileIcon_; |
|
| 55 | ||
| 56 | /** |
|
| 57 | * Create a directory visitor. |
|
| 58 | */ |
|
| 59 | public DirectoryVisitor() |
|
| 60 | 0 | { |
| 61 | 0 | directories_ = new Vector(); |
| 62 | 0 | files_ = new Vector(); |
| 63 | 0 | directoryIcon_ = load( "/icons/folder.png" ); |
| 64 | 0 | fileIcon_ = load( "/icons/text-x-generic.png" ); |
| 65 | 0 | } |
| 66 | ||
| 67 | private Image load( final String filename ) |
|
| 68 | { |
|
| 69 | try |
|
| 70 | { |
|
| 71 | 0 | return Image.createImage( filename ); |
| 72 | } |
|
| 73 | 0 | catch( Exception e ) |
| 74 | { |
|
| 75 | 0 | throw new RuntimeException( e.getMessage() ); |
| 76 | } |
|
| 77 | } |
|
| 78 | ||
| 79 | /** |
|
| 80 | * Lists alphabetically the directories and files contained in a given directory. |
|
| 81 | * |
|
| 82 | * @param list the list to fill |
|
| 83 | * @param directory the directory to visit |
|
| 84 | * @throws IOException an io exception occured |
|
| 85 | */ |
|
| 86 | public void fill( final List list, final IDirectory directory ) throws IOException |
|
| 87 | { |
|
| 88 | 0 | files_.removeAllElements(); |
| 89 | 0 | directories_.removeAllElements(); |
| 90 | 0 | directory.accept( this ); |
| 91 | 0 | Enumeration e = directories_.elements(); |
| 92 | 0 | while( e.hasMoreElements() ) |
| 93 | 0 | list.append( (String)e.nextElement(), directoryIcon_ ); |
| 94 | 0 | e = files_.elements(); |
| 95 | 0 | while( e.hasMoreElements() ) |
| 96 | 0 | list.append( (String)e.nextElement(), fileIcon_ ); |
| 97 | 0 | } |
| 98 | ||
| 99 | /** |
|
| 100 | * {@inheritDoc} |
|
| 101 | */ |
|
| 102 | public void visitFile( final String name ) |
|
| 103 | { |
|
| 104 | 0 | insertSorted( files_, name ); |
| 105 | 0 | } |
| 106 | ||
| 107 | /** |
|
| 108 | * {@inheritDoc} |
|
| 109 | */ |
|
| 110 | public void visitDirectory( final String name ) |
|
| 111 | { |
|
| 112 | 0 | insertSorted( directories_, name ); |
| 113 | 0 | } |
| 114 | ||
| 115 | private void insertSorted( final Vector vector, final String name ) |
|
| 116 | { |
|
| 117 | 0 | for( int index = 0; index < vector.size(); ++index ) |
| 118 | { |
|
| 119 | 0 | if( before( name, (String)vector.elementAt( index ) ) ) |
| 120 | { |
|
| 121 | 0 | vector.insertElementAt( name, index ); |
| 122 | 0 | return; |
| 123 | } |
|
| 124 | } |
|
| 125 | 0 | vector.addElement( name ); |
| 126 | 0 | } |
| 127 | ||
| 128 | private boolean before( final String str1, final String str2 ) |
|
| 129 | { |
|
| 130 | 0 | return str1.toLowerCase().compareTo( str2.toLowerCase() ) < 0; |
| 131 | } |
|
| 132 | } |