001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.navigator;
011:
012: import org.eclipse.core.runtime.IAdaptable;
013: import org.eclipse.ui.ISaveablesLifecycleListener;
014: import org.eclipse.ui.Saveable;
015: import org.eclipse.ui.SaveablesLifecycleEvent;
016: import org.eclipse.ui.services.IDisposable;
017:
018: /**
019: * Provides {@link Saveable} objects to the common navigator, and allows to map
020: * between elements in the tree and models.
021: * <p>
022: * This class is intended to be subclassed by clients.
023: * </p>
024: * Instances of subclasses will be returned from content extensions that
025: * implement {@link IAdaptable}.
026: *
027: * @since 3.2
028: *
029: */
030: public abstract class SaveablesProvider implements IDisposable {
031:
032: private ISaveablesLifecycleListener listener;
033:
034: /**
035: * Creates a new saveable model provider. May only be called by subclasses.
036: *
037: */
038: protected SaveablesProvider() {
039: }
040:
041: /**
042: * Initializes this SaveablesProvider with the given listener, and calls the hook method doInit().
043: *
044: * @param listener
045: */
046: final public void init(ISaveablesLifecycleListener listener) {
047: this .listener = listener;
048: doInit();
049: }
050:
051: /**
052: * May be overridden by clients. The default implementation does nothing.
053: */
054: protected void doInit() {
055: }
056:
057: /**
058: * Notifies the listener that the given models were opened in this model
059: * provider. This method must be called on the UI thread.
060: *
061: * @param models
062: */
063: final protected void fireSaveablesOpened(Saveable[] models) {
064: listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this ,
065: SaveablesLifecycleEvent.POST_OPEN, models, false));
066: }
067:
068: /**
069: * Notifies the listener that the given models are about to be closed in
070: * this model provider. This method must be called on the UI thread.
071: *
072: * @param models
073: * @param force
074: * true if the closing may be canceled by the user
075: * @return true if the listener vetoed the closing (may be ignored if force
076: * is true)
077: */
078: final protected boolean fireSaveablesClosing(Saveable[] models,
079: boolean force) {
080: SaveablesLifecycleEvent saveablesLifecycleEvent = new SaveablesLifecycleEvent(
081: this , SaveablesLifecycleEvent.PRE_CLOSE, models, force);
082: listener.handleLifecycleEvent(saveablesLifecycleEvent);
083: return saveablesLifecycleEvent.isVeto();
084: }
085:
086: /**
087: * Notifies the listener that the given models were closed in this model
088: * provider. This method must be called on the UI thread.
089: *
090: * @param models
091: */
092: final protected void fireSaveablesClosed(Saveable[] models) {
093: listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this ,
094: SaveablesLifecycleEvent.POST_CLOSE, models, false));
095: }
096:
097: /**
098: * Notifies the listener that the given models' dirty state has changed.
099: * This method must be called on the UI thread.
100: *
101: * @param models
102: */
103: final protected void fireSaveablesDirtyChanged(Saveable[] models) {
104: listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this ,
105: SaveablesLifecycleEvent.DIRTY_CHANGED, models, false));
106: }
107:
108: /**
109: * Returns the saveables reachable through this provider. Changes to the
110: * list of saveables or to the saveables' dirty state must be announced
111: * using the appropriate fire* methods.
112: *
113: * @return the saveables returned by this saveables provider.
114: */
115: public abstract Saveable[] getSaveables();
116:
117: /**
118: * Returns the elements representing the given saveable. It is recommended
119: * that a saveable be represented by only one element.
120: *
121: * @param saveable
122: * @return the elements representing the given saveable (array may be empty)
123: */
124: public abstract Object[] getElements(Saveable saveable);
125:
126: /**
127: * Returns the saveable for the given element, or null if the element does
128: * not represent a saveable.
129: *
130: * @param element
131: * @return the saveable for the given element, or null
132: */
133: public abstract Saveable getSaveable(Object element);
134:
135: /**
136: * Disposes of this saveables provider. Subclasses may extend, but must call
137: * the super implementation.
138: */
139: public void dispose() {
140: listener = null;
141: }
142:
143: }
|