1 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 32 public class DefaultSaveable extends Saveable { 33 34 private IWorkbenchPart part; 35 36 42 public DefaultSaveable(IWorkbenchPart part) { 43 this.part = part; 44 } 45 46 51 public void doSave(IProgressMonitor monitor) { 52 if (part instanceof ISaveablePart) { 53 ISaveablePart saveable = (ISaveablePart) part; 54 saveable.doSave(monitor); 55 } 56 } 57 58 63 public String getName() { 64 if (part instanceof IWorkbenchPart2) { 65 return ((IWorkbenchPart2) part).getPartName(); 66 } 67 return part.getTitle(); 68 } 69 70 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 88 public String getToolTipText() { 89 return part.getTitleToolTip(); 90 } 91 92 97 public boolean isDirty() { 98 if (part instanceof ISaveablePart) { 99 return ((ISaveablePart) part).isDirty(); 100 } 101 return false; 102 } 103 104 107 public int hashCode() { 108 return part.hashCode(); 109 } 110 111 114 public boolean equals(Object 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 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 |