1 18 19 package org.apache.tools.ant.taskdefs.optional.sound; 20 21 import java.io.File ; 22 import java.util.Random ; 23 import java.util.Vector ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.Task; 27 28 45 46 public class SoundTask extends Task { 47 48 private BuildAlert success = null; 49 private BuildAlert fail = null; 50 51 55 public BuildAlert createSuccess() { 56 success = new BuildAlert(); 57 return success; 58 } 59 60 64 public BuildAlert createFail() { 65 fail = new BuildAlert(); 66 return fail; 67 } 68 69 70 public SoundTask() { 71 } 72 73 76 public void init() { 77 } 78 79 82 public void execute() { 83 84 AntSoundPlayer soundPlayer = new AntSoundPlayer(); 85 86 if (success == null) { 87 log("No nested success element found.", Project.MSG_WARN); 88 } else { 89 soundPlayer.addBuildSuccessfulSound(success.getSource(), 90 success.getLoops(), success.getDuration()); 91 } 92 93 if (fail == null) { 94 log("No nested failure element found.", Project.MSG_WARN); 95 } else { 96 soundPlayer.addBuildFailedSound(fail.getSource(), 97 fail.getLoops(), fail.getDuration()); 98 } 99 100 getProject().addBuildListener(soundPlayer); 101 102 } 103 104 108 public class BuildAlert { 109 private File source = null; 110 private int loops = 0; 111 private Long duration = null; 112 113 117 public void setDuration(Long duration) { 118 this.duration = duration; 119 } 120 121 126 public void setSource(File source) { 127 this.source = source; 128 } 129 130 135 public void setLoops(int loops) { 136 this.loops = loops; 137 } 138 139 143 public File getSource() { 144 File nofile = null; 145 if (source.exists()) { 147 if (source.isDirectory()) { 148 String [] entries = source.list(); 150 Vector files = new Vector (); 151 for (int i = 0; i < entries.length; i++) { 152 File f = new File (source, entries[i]); 153 if (f.isFile()) { 154 files.addElement(f); 155 } 156 } 157 if (files.size() < 1) { 158 throw new BuildException("No files found in directory " + source); 159 } 160 int numfiles = files.size(); 161 Random rn = new Random (); 163 int x = rn.nextInt(numfiles); 164 this.source = (File ) files.elementAt(x); 166 } 167 } else { 168 log(source + ": invalid path.", Project.MSG_WARN); 169 this.source = nofile; 170 } 171 return this.source; 172 } 173 174 179 public int getLoops() { 180 return this.loops; 181 } 182 183 187 public Long getDuration() { 188 return this.duration; 189 } 190 } 191 } 192 193 | Popular Tags |