1 19 20 package org.netbeans.modules.refactoring.api.impl; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import org.netbeans.modules.refactoring.api.ProgressEvent; 25 import org.netbeans.modules.refactoring.api.ProgressListener; 26 import org.openide.ErrorManager; 27 28 32 public final class ProgressSupport { 33 34 private transient ArrayList progressListenerList = null; 35 private int counter; 36 37 38 public ProgressSupport() { 39 progressListenerList = new ArrayList (); 40 } 41 public synchronized void addProgressListener(ProgressListener listener) { 42 progressListenerList.add(listener); 43 } 44 45 49 public synchronized void removeProgressListener(ProgressListener listener) { 50 progressListenerList.remove(listener); 51 } 52 53 59 public void fireProgressListenerStart(Object source, int type, int count) { 60 counter = -1; 61 ArrayList list; 62 ProgressEvent event = new ProgressEvent(source, ProgressEvent.START, type, count); 63 synchronized (this) { 64 list = (ArrayList ) progressListenerList.clone(); 65 } 66 for (Iterator it = list.iterator(); it.hasNext();) { 67 try { 68 ((ProgressListener) it.next()).start(event); 69 } catch (RuntimeException e) { 70 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 71 } 72 } 73 } 74 75 81 public void fireProgressListenerStart(int type, int count) { 82 fireProgressListenerStart(this, type, count); 83 } 84 85 86 88 public void fireProgressListenerStep(Object source, int count) { 89 counter = count; 90 ArrayList list; 91 ProgressEvent event = new ProgressEvent(source, ProgressEvent.STEP, 0, count); 92 synchronized (this) { 93 list = (ArrayList ) progressListenerList.clone(); 94 } 95 for (Iterator it = list.iterator(); it.hasNext();) { 96 try { 97 ((ProgressListener) it.next()).step(event); 98 } catch (RuntimeException e) { 99 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 100 } 101 } 102 } 103 105 public void fireProgressListenerStep(Object source) { 106 fireProgressListenerStep(source, counter+1); 107 } 108 110 public void fireProgressListenerStop(Object source) { 111 ArrayList list; 112 ProgressEvent event = new ProgressEvent(source, ProgressEvent.STOP); 113 synchronized (this) { 114 list = (ArrayList ) progressListenerList.clone(); 115 } 116 for (Iterator it = list.iterator(); it.hasNext();) { 117 try { 118 ((ProgressListener) it.next()).stop(event); 119 } catch (RuntimeException e) { 120 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 121 } 122 } 123 } 124 125 127 public void fireProgressListenerStop() { 128 fireProgressListenerStop(this); 129 } 130 } 131 | Popular Tags |