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.internal;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.jface.resource.ImageDescriptor;
014: import org.eclipse.swt.graphics.Image;
015: import org.eclipse.ui.ISaveablePart;
016: import org.eclipse.ui.IViewPart;
017: import org.eclipse.ui.IWorkbenchPage;
018: import org.eclipse.ui.IWorkbenchPart;
019: import org.eclipse.ui.IWorkbenchPart2;
020: import org.eclipse.ui.IWorkbenchPartReference;
021: import org.eclipse.ui.PartInitException;
022: import org.eclipse.ui.Saveable;
023:
024: /**
025: * A default {@link Saveable} implementation that wrappers a regular
026: * workbench part (one that does not itself adapt to Saveable).
027: *
028: * @since 3.2
029: */
030: public class DefaultSaveable extends Saveable {
031:
032: private IWorkbenchPart part;
033:
034: /**
035: * Creates a new DefaultSaveable.
036: *
037: * @param part
038: * the part represented by this model
039: */
040: public DefaultSaveable(IWorkbenchPart part) {
041: this .part = part;
042: }
043:
044: /*
045: * (non-Javadoc)
046: *
047: * @see org.eclipse.ui.Saveable#doSave(org.eclipse.core.runtime.IProgressMonitor)
048: */
049: public void doSave(IProgressMonitor monitor) {
050: if (part instanceof ISaveablePart) {
051: ISaveablePart saveable = (ISaveablePart) part;
052: saveable.doSave(monitor);
053: }
054: }
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see org.eclipse.ui.Saveable#getName()
060: */
061: public String getName() {
062: if (part instanceof IWorkbenchPart2) {
063: return ((IWorkbenchPart2) part).getPartName();
064: }
065: return part.getTitle();
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.eclipse.ui.Saveable#getImageDescriptor()
072: */
073: public ImageDescriptor getImageDescriptor() {
074: Image image = part.getTitleImage();
075: if (image == null) {
076: return null;
077: }
078: return ImageDescriptor.createFromImage(image);
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.eclipse.ui.Saveable#getToolTipText()
085: */
086: public String getToolTipText() {
087: return part.getTitleToolTip();
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.eclipse.ui.Saveable#isDirty()
094: */
095: public boolean isDirty() {
096: if (part instanceof ISaveablePart) {
097: return ((ISaveablePart) part).isDirty();
098: }
099: return false;
100: }
101:
102: /* (non-Javadoc)
103: * @see java.lang.Object#hashCode()
104: */
105: public int hashCode() {
106: return part.hashCode();
107: }
108:
109: /* (non-Javadoc)
110: * @see java.lang.Object#equals(java.lang.Object)
111: */
112: public boolean equals(Object obj) {
113: if (this == obj)
114: return true;
115: if (obj == null)
116: return false;
117: if (getClass() != obj.getClass())
118: return false;
119: final DefaultSaveable other = (DefaultSaveable) obj;
120: if (part == null) {
121: if (other.part != null)
122: return false;
123: } else if (!part.equals(other.part))
124: return false;
125: return true;
126: }
127:
128: /* (non-Javadoc)
129: * @see org.eclipse.ui.Saveable#show(org.eclipse.ui.IWorkbenchPage)
130: */
131: public boolean show(IWorkbenchPage page) {
132: IWorkbenchPartReference reference = page.getReference(part);
133: if (reference != null) {
134: page.activate(part);
135: return true;
136: }
137: if (part instanceof IViewPart) {
138: IViewPart viewPart = (IViewPart) part;
139: try {
140: page.showView(viewPart.getViewSite().getId(), viewPart
141: .getViewSite().getSecondaryId(),
142: IWorkbenchPage.VIEW_ACTIVATE);
143: } catch (PartInitException e) {
144: return false;
145: }
146: return true;
147: }
148: return false;
149: }
150:
151: }
|