/*******************************************************************************
 * Copyright (c) 2005-2009 VecTrace (Zingo Andersen) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Andrei Loskutov (Intland) - implementation
 *******************************************************************************/
package com.vectrace.MercurialEclipse.model;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;

/**
 * A temporary changeset which holds not commited resources. This changeset cannot be used
 * as a usual changeset, as many of it's functionality is not supported or limited.
 *
 * @author Andrei
 */
public class WorkingChangeSet extends ChangeSet {

	private static final String REMOVED = "removed";
	private static final String ADDED = "added";
	private final List<IPropertyChangeListener> listeners;

	public WorkingChangeSet(String name) {
		super(-1, name, null, null, "", null, "", null, null); //$NON-NLS-1$
		direction = Direction.OUTGOING;
		listeners = new CopyOnWriteArrayList<IPropertyChangeListener>();
	}

	public void add(IFile file){
		Set<IFile> files2 = getFiles();
		files = new HashSet<IFile>(files2);
		boolean added = files.add(file);
		if(added) {
			for (IPropertyChangeListener listener : listeners) {
				listener.propertyChange(new PropertyChangeEvent(this, ADDED, null, file));
			}
		}
	}

	@Override
	public void remove(IResource file){
		Set<IFile> files2 = getFiles();
		files = new HashSet<IFile>(files2);
		boolean removed = files.remove(file);
		if(removed) {
			for (IPropertyChangeListener listener : listeners) {
				listener.propertyChange(new PropertyChangeEvent(this, REMOVED, file, null));
			}
		}
	}

	public void addListener(IPropertyChangeListener listener){
		if(!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}

	public void removeListener(IPropertyChangeListener listener){
		listeners.remove(listener);
	}

	@Override
	public boolean equals(Object obj) {
		return this == obj;
	}

	@Override
	public int hashCode() {
		return System.identityHashCode(this);
	}

	@Override
	public String toString() {
		String changeset = getChangeset();
		int size = getFiles().size();
		if(size == 0){
			return changeset + " (empty)";
		}
		return changeset + " (" + size + ")";
	}

	public void clear(){
		files = new HashSet<IFile>();
	}

	public void beginInput() {
		// TODO Auto-generated method stub

	}

	public void endInput(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}
}
@@ -583,16 +583,74 @@
 model;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 import j
@@ -1412,16 +1412,97 @@
 steners;
+%0A%09private final List%3CPropertyChangeEvent%3E eventCache;%0A%09private boolean cachingOn;
 %0A%0A%09publi
@@ -1703,24 +1703,77 @@
 istener%3E();%0A
+%09%09eventCache = new ArrayList%3CPropertyChangeEvent%3E();%0A
 %09%7D%0A%0A%09public 
@@ -1911,24 +1911,216 @@
 if(added) %7B%0A
+%09%09%09PropertyChangeEvent event = new PropertyChangeEvent(this, ADDED, null, file);%0A%09%09%09// we need only one event%0A%09%09%09if(cachingOn && eventCache.isEmpty())%7B%0A%09%09%09%09eventCache.add(event);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09for (IPro
@@ -2159,32 +2159,33 @@
 isteners) %7B%0A%09%09%09%09
+%09
 listener.propert
@@ -2196,58 +2196,21 @@
 nge(
-new PropertyChangeEvent(this, ADDED, null, file));
+event);%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -2395,16 +2395,181 @@
 oved) %7B%0A
+%09%09%09PropertyChangeEvent event = new PropertyChangeEvent(this, REMOVED, file, null);%0A%09%09%09if(cachingOn && eventCache.isEmpty())%7B%0A%09%09%09%09eventCache.add(event);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09for (
@@ -2616,24 +2616,25 @@
 ners) %7B%0A%09%09%09%09
+%09
 listener.pro
@@ -2649,60 +2649,21 @@
 nge(
-new PropertyChangeEvent(this, REMOVED, file, null));
+event);%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -3356,43 +3356,25 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A
+cachingOn = true;
 %0A%09%7D%0A
@@ -3430,43 +3430,233 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A
+cachingOn = false;%0A%09%09Collections.reverse(eventCache);%0A%09%09for (IPropertyChangeListener listener : listeners) %7B%0A%09%09%09for (PropertyChangeEvent event : eventCache) %7B%0A%09%09%09%09listener.propertyChange(event);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09eventCache.clear();
 %0A%09%7D%0A

Send only one event