KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > DefaultSaveable


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
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.ui.ISaveablePart;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.IWorkbenchPart2;
22 import org.eclipse.ui.IWorkbenchPartReference;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.Saveable;
25
26 /**
27  * A default {@link Saveable} implementation that wrappers a regular
28  * workbench part (one that does not itself adapt to Saveable).
29  *
30  * @since 3.2
31  */

32 public class DefaultSaveable extends Saveable {
33
34     private IWorkbenchPart part;
35
36     /**
37      * Creates a new DefaultSaveable.
38      *
39      * @param part
40      * the part represented by this model
41      */

42     public DefaultSaveable(IWorkbenchPart part) {
43         this.part = part;
44     }
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.eclipse.ui.Saveable#doSave(org.eclipse.core.runtime.IProgressMonitor)
50      */

51     public void doSave(IProgressMonitor monitor) {
52         if (part instanceof ISaveablePart) {
53             ISaveablePart saveable = (ISaveablePart) part;
54             saveable.doSave(monitor);
55         }
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.eclipse.ui.Saveable#getName()
62      */

63     public String JavaDoc getName() {
64         if (part instanceof IWorkbenchPart2) {
65             return ((IWorkbenchPart2) part).getPartName();
66         }
67         return part.getTitle();
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see org.eclipse.ui.Saveable#getImageDescriptor()
74      */

75     public ImageDescriptor getImageDescriptor() {
76         Image image = part.getTitleImage();
77         if (image == null) {
78             return null;
79         }
80         return ImageDescriptor.createFromImage(image);
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see org.eclipse.ui.Saveable#getToolTipText()
87      */

88     public String JavaDoc getToolTipText() {
89         return part.getTitleToolTip();
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.eclipse.ui.Saveable#isDirty()
96      */

97     public boolean isDirty() {
98         if (part instanceof ISaveablePart) {
99             return ((ISaveablePart) part).isDirty();
100         }
101         return false;
102     }
103
104     /* (non-Javadoc)
105      * @see java.lang.Object#hashCode()
106      */

107     public int hashCode() {
108         return part.hashCode();
109     }
110
111     /* (non-Javadoc)
112      * @see java.lang.Object#equals(java.lang.Object)
113      */

114     public boolean equals(Object JavaDoc obj) {
115         if (this == obj)
116             return true;
117         if (obj == null)
118             return false;
119         if (getClass() != obj.getClass())
120             return false;
121         final DefaultSaveable other = (DefaultSaveable) obj;
122         if (part == null) {
123             if (other.part != null)
124                 return false;
125         } else if (!part.equals(other.part))
126             return false;
127         return true;
128     }
129     
130     /* (non-Javadoc)
131      * @see org.eclipse.ui.Saveable#show(org.eclipse.ui.IWorkbenchPage)
132      */

133     public boolean show(IWorkbenchPage page) {
134         IWorkbenchPartReference reference = page.getReference(part);
135         if (reference != null) {
136             page.activate(part);
137             return true;
138         }
139         if (part instanceof IViewPart) {
140             IViewPart viewPart = (IViewPart) part;
141             try {
142                 page.showView(viewPart.getViewSite().getId(), viewPart
143                         .getViewSite().getSecondaryId(),
144                         IWorkbenchPage.VIEW_ACTIVATE);
145             } catch (PartInitException e) {
146                 return false;
147             }
148             return true;
149         }
150         return false;
151     }
152
153 }
154
Popular Tags