1 23 package org.enhydra.kelp.common; 24 25 import org.enhydra.tool.common.event.CancelEvent; 27 import org.enhydra.tool.common.event.CancelListener; 28 import org.enhydra.tool.common.event.ProgressEvent; 29 import org.enhydra.tool.common.event.ProgressListener; 30 31 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 35 public class ProgressBuilder implements CancelListener { 37 private ProgressListener[] progressListeners = new ProgressListener[0]; 38 private static boolean fresh = true; 39 40 public ProgressBuilder() { 41 } 42 43 44 47 public void setFresh(boolean b) { 48 fresh = b; 49 } 50 51 public boolean isFresh() { 52 return fresh; 53 } 54 55 public ProgressListener[] getProgressListeners() { 56 return progressListeners; 57 } 58 59 public void addProgressListener(ProgressListener l) { 60 ArrayList list = null; 61 list = new ArrayList (Arrays.asList(progressListeners)); 62 if (! list.contains(l)) { 63 list.add(l); 64 } 65 list.trimToSize(); 66 progressListeners = new ProgressListener[list.size()]; 67 progressListeners = (ProgressListener[]) list.toArray(progressListeners); 68 list.clear(); 69 } 70 71 public void removeProgressListener(ProgressListener l) { 72 ArrayList list = null; 73 list = new ArrayList (Arrays.asList(progressListeners)); 74 if (list.contains(l)) { 75 list.remove(l); 76 } 77 list.trimToSize(); 78 progressListeners = new ProgressListener[list.size()]; 79 progressListeners = (ProgressListener[]) list.toArray(progressListeners); 80 list.clear(); 81 } 82 83 public void refreshProgress(int progress, String message) { 84 if (progressListeners.length == 0) { 85 86 } else { 88 ProgressEvent event = null; 89 90 event = new ProgressEvent(this, progress, message); 91 for (int i = 0; i < progressListeners.length; i++) { 92 progressListeners[i].onProgress(event); 93 } 94 } 95 } 96 97 public void onCancel(CancelEvent event) { 98 setFresh(false); 99 } 100 101 } 102 | Popular Tags |