KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > mapping > SaveableComparison


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.ui.mapping;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.ui.*;
15
16 /**
17  * A saveable comparison is used to buffer changes made when comparing
18  * or merging model elements. A buffer can be shared between multiple
19  * typed elements within a comparison. The saveable is used by the comparison
20  * container in order to determine when a save is required.
21  * <p>
22  * Clients may subclass this class.
23  *
24  * @since 3.2
25  */

26 public abstract class SaveableComparison extends Saveable {
27
28     /**
29      * The property id for <code>isDirty</code>.
30      */

31     public static final int PROP_DIRTY = IWorkbenchPartConstants.PROP_DIRTY;
32     
33     private boolean dirty;
34     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
35
36     /**
37      * {@inheritDoc}
38      */

39     public boolean isDirty() {
40         return dirty;
41     }
42
43     /**
44      * {@inheritDoc}
45      */

46     public void doSave(IProgressMonitor monitor) throws CoreException {
47         if (!isDirty())
48             return;
49         performSave(monitor);
50         setDirty(false);
51     }
52
53     /**
54      * Revert any changes in the buffer back to the last saved state.
55      * @param monitor a progress monitor on <code>null</code>
56      * if progress feedback is not required
57      */

58     public void doRevert(IProgressMonitor monitor) {
59         if (!isDirty())
60             return;
61         performRevert(monitor);
62         setDirty(false);
63     }
64
65     /**
66      * Add a property change listener. Adding a listener
67      * that is already registered has no effect.
68      * @param listener the listener
69      */

70     public void addPropertyListener(IPropertyListener listener) {
71         listeners.add(listener);
72     }
73
74     /**
75      * Remove a property change listener. Removing a listener
76      * that is not registered has no effect.
77      * @param listener the listener
78      */

79     public void removePropertyListener(IPropertyListener listener) {
80         listeners.remove(listener);
81     }
82
83     /**
84      * Set the dirty state of this buffer. If the state
85      * has changed, a property change event will be fired.
86      * @param dirty the dirty state
87      */

88     protected void setDirty(boolean dirty) {
89         if (this.dirty == dirty) {
90             return;
91         }
92         this.dirty = dirty;
93         firePropertyChange(PROP_DIRTY);
94     }
95
96     /**
97      * Fire a property change event for this buffer.
98      * @param property the property that changed
99      */

100     protected void firePropertyChange(final int property) {
101         Object JavaDoc[] allListeners = listeners.getListeners();
102         for (int i = 0; i < allListeners.length; i++) {
103             final Object JavaDoc object = allListeners[i];
104             SafeRunner.run(new ISafeRunnable() {
105                 public void run() throws Exception JavaDoc {
106                     ((IPropertyListener)object).propertyChanged(SaveableComparison.this, property);
107                 }
108                 public void handleException(Throwable JavaDoc exception) {
109                     // handled by platform
110
}
111             });
112         }
113     }
114     
115     /**
116      * Method invoked from {@link #doSave(IProgressMonitor)} to write
117      * out the buffer. By default, this method invokes <code>doSave</code>
118      * on the buffers saveable model.
119      * @param monitor a progress monitor
120      * @throws CoreException if errors occur
121      */

122     protected abstract void performSave(IProgressMonitor monitor) throws CoreException;
123     
124     /**
125      * Method invoked from {@link #doRevert(IProgressMonitor)} to discard the
126      * changes in the buffer.
127      * @param monitor a progress monitor
128      */

129     protected abstract void performRevert(IProgressMonitor monitor);
130 }
131
Popular Tags