1 package org.netbeans.modules.loadgenerator.spi; 2 3 import java.awt.Image ; 4 import java.beans.PropertyChangeListener ; 5 import java.beans.PropertyChangeSupport ; 6 import java.io.File ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Collections ; 10 import java.util.HashMap ; 11 import java.util.Map ; 12 import org.netbeans.modules.loadgenerator.api.impl.ManagerOutputWindowRegistry; 13 import org.netbeans.modules.loadgenerator.utils.NullOutputWriter; 14 import org.openide.filesystems.FileAttributeEvent; 15 import org.openide.filesystems.FileChangeListener; 16 import org.openide.filesystems.FileEvent; 17 import org.openide.filesystems.FileObject; 18 import org.openide.filesystems.FileRenameEvent; 19 import org.openide.filesystems.FileUtil; 20 import org.openide.util.WeakListeners; 21 import org.openide.windows.OutputWriter; 22 23 29 public abstract class ProcessInstance { 30 public static final String FACTORY = ProcessInstance.class.getName() + "#FACTORY"; 31 public final static String STATE = ProcessInstance.class.getName() + "#STATE"; 32 33 private static final OutputWriter NULLWRITER = new NullOutputWriter(); 34 35 36 public ProcessInstance(final Engine factory) { 37 pcs = new PropertyChangeSupport (this); 38 listeners = new ArrayList <ProcessInstanceListener>(); 39 listenerMap = Collections.synchronizedMap(new HashMap <ProcessInstanceListener, ProcessInstanceListener>()); 40 41 setFactory(factory); 42 } 43 44 public synchronized boolean isNew() { 45 return isNewFlag; 46 } 47 48 public synchronized void touch() { 49 isNewFlag = false; 50 isModifiedFlag = false; 51 isDeletedFlag = false; 52 } 53 54 void attachFactory(final Engine factory) { 55 setFactory(factory); 56 } 57 58 void detachFactory() { 59 setFactory(null); 60 } 61 62 66 public void addListener(final ProcessInstanceListener listener) { 67 ProcessInstanceListener weak = WeakListeners.create(ProcessInstanceListener.class, listener, this); 68 if (!listeners.contains(weak)) { 69 listeners.add(weak); 70 listenerMap.put(listener, weak); 71 } 72 } 73 74 public void addPropertyChangeListener(final PropertyChangeListener pcl) { 76 pcs.addPropertyChangeListener(pcl); 77 } 78 79 public void addPropertyChangeListener(final String propertyName, final PropertyChangeListener pcl) { 80 pcs.addPropertyChangeListener(propertyName, pcl); 81 } 82 83 public void removePropertyChangeListener(final PropertyChangeListener pcl) { 84 pcs.removePropertyChangeListener(pcl); 85 } 86 87 public void removePropertyChangeListener(final String propertyName, final PropertyChangeListener pcl) { 88 pcs.removePropertyChangeListener(propertyName, pcl); 89 } 90 92 96 public void removeListener(final ProcessInstanceListener listener) { 97 ProcessInstanceListener weak = listenerMap.get(listener); 98 if (weak != null) { 99 listeners.remove(weak); 100 listenerMap.remove(listener); 101 } 102 } 103 104 public Engine getFactory() { 105 return factory; 106 } 107 108 public void start(final String scriptFileName) { 109 setCurrentScript(scriptFileName); 110 performStart(scriptFileName); 111 } 112 113 public void start() { 114 if (currentScript != null) { 115 performStart(currentScript); 116 } 117 } 118 119 public void stop(final boolean force) { 120 if (isRunning()) { 121 performStop(force); 122 } else { 123 publishStart(); publishStop(); 125 } 126 } 127 128 public String getCurrentScript() { 129 return currentScript; 130 } 131 132 public void setCurrentScript(final String value) { 133 if (currentScriptFile != null) { 134 currentScriptFile.removeFileChangeListener(fcl); 135 currentScriptFile = null; 136 } 137 if (value != null) { 138 currentScriptFile = FileUtil.toFileObject(new File (value)); 139 currentScriptFile.addFileChangeListener(fcl); 140 } 141 currentScript = value; 142 } 143 144 147 public void attachWriter(final OutputWriter writer) { 148 this.writer = writer; 149 } 150 151 154 public void detachWriter() { 155 this.writer = null; 156 } 157 158 public boolean isModified() { 159 return isModifiedFlag; 160 } 161 162 public boolean isDeleted() { 163 return isDeletedFlag; 164 } 165 166 public abstract void performStart(final String scriptFileName); 167 public abstract void performStop(final boolean force); 168 169 172 public abstract boolean isRunning(); 173 174 178 public abstract String getDisplayName(); 179 180 184 public abstract Image getIcon(); 185 186 187 private Collection <ProcessInstanceListener> listeners; 188 private Map <ProcessInstanceListener, ProcessInstanceListener> listenerMap; 189 private PropertyChangeSupport pcs; 190 private String currentScript; 191 private FileObject currentScriptFile; 192 private OutputWriter writer; 193 private Engine factory; 194 private boolean isNewFlag = true, isModifiedFlag = false, isDeletedFlag = false; 195 196 final private FileChangeListener fcl = new FileChangeListener() { 197 public void fileFolderCreated(FileEvent fe) { 198 } 200 201 public void fileDataCreated(FileEvent fe) { 202 } 204 205 public void fileChanged(FileEvent fe) { 206 setModified(); 207 } 208 209 public void fileDeleted(FileEvent fe) { 210 fe.getFile().removeFileChangeListener(this); 211 currentScriptFile = null; 212 currentScript = null; 213 if (!isRunning()) { 214 ManagerOutputWindowRegistry.getDefault().close(ProcessInstance.this); 215 } else { 216 setDeleted(); 217 } 218 } 219 220 public void fileRenamed(FileRenameEvent fe) { 221 currentScript = fe.getFile().getPath(); 222 if (!isRunning()) { 223 ManagerOutputWindowRegistry.getDefault().close(ProcessInstance.this); 224 ManagerOutputWindowRegistry.getDefault().open(ProcessInstance.this); 225 } else { 226 setModified(); 227 } 228 } 229 230 public void fileAttributeChanged(FileAttributeEvent fe) { 231 } 233 }; 234 235 236 protected synchronized void publishStart() { 237 for(ProcessInstanceListener listener : listeners) { 238 listener.generatorStarted(this); 239 } 240 pcs.firePropertyChange(STATE, false, true); 241 } 242 243 protected synchronized void publishStart(final String logPath) { 244 for(ProcessInstanceListener listener : listeners) { 245 listener.generatorStarted(this, logPath); 246 } 247 pcs.firePropertyChange(STATE, false, true); 248 } 249 250 protected synchronized void publishStop() { 251 Collection <ProcessInstanceListener> immutableListeners = new ArrayList <ProcessInstanceListener>(listeners); 252 for(ProcessInstanceListener listener : immutableListeners) { 253 listener.generatorStopped(this); 254 } 255 pcs.firePropertyChange(STATE, true, false); 256 } 257 258 protected synchronized void publishInvalidated() { 259 Collection <ProcessInstanceListener> immutableListeners = new ArrayList <ProcessInstanceListener>(listeners); 260 for(ProcessInstanceListener listener : immutableListeners) { 261 listener.instanceInvalidated(this); 262 } 263 } 264 265 protected OutputWriter getWriter() { 266 return writer != null ? writer : NULLWRITER; 267 } 268 269 private void setFactory(final Engine factory) { 270 pcs.firePropertyChange(FACTORY, this.factory, factory); 271 this.factory = factory; 272 } 273 274 private void setModified() { 275 isModifiedFlag = true; 276 publishInvalidated(); 277 } 278 279 private void setDeleted() { 280 isDeletedFlag = true; 281 isModifiedFlag = false; 282 publishInvalidated(); 283 } 284 } 285 | Popular Tags |