1 11 package org.eclipse.ltk.ui.refactoring; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.custom.CLabel; 17 import org.eclipse.swt.custom.ViewForm; 18 import org.eclipse.swt.events.DisposeEvent; 19 import org.eclipse.swt.events.DisposeListener; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 24 import org.eclipse.jface.dialogs.Dialog; 25 import org.eclipse.jface.resource.ImageDescriptor; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.jface.text.IRegion; 28 import org.eclipse.jface.text.source.SourceViewer; 29 30 import org.eclipse.ui.model.IWorkbenchAdapter; 31 32 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages; 33 34 46 public abstract class TextStatusContextViewer implements IStatusContextViewer { 47 48 private SourceViewer fSourceViewer; 49 private ViewForm fForm; 50 private CLabel fLabel; 51 private Image fPaneImage; 52 53 59 protected SourceViewer getSourceViewer() { 60 return fSourceViewer; 61 } 62 63 71 protected abstract SourceViewer createSourceViewer(Composite parent); 72 73 75 85 protected void updateTitle(IAdaptable element) { 86 String title= null; 87 ImageDescriptor imageDescriptor= null; 88 if (element != null) { 89 IWorkbenchAdapter adapter= (IWorkbenchAdapter)element.getAdapter(IWorkbenchAdapter.class); 90 if (adapter != null) { 91 title= adapter.getLabel(element); 92 imageDescriptor= adapter.getImageDescriptor(element); 93 } 94 } 95 if (title == null || title.length() == 0) 96 title= RefactoringUIMessages.RefactoringStatusViewer_Problem_context; 97 fLabel.setText(title); 98 if (fPaneImage != null) { 99 fPaneImage.dispose(); 100 fPaneImage= null; 101 } 102 if (imageDescriptor != null) { 103 fPaneImage= imageDescriptor.createImage(getControl().getDisplay()); 104 } 105 fLabel.setImage(fPaneImage); 106 } 107 108 115 protected void setInput(IDocument document, IRegion region) { 116 Control ctrl= getControl(); 117 if (ctrl != null && ctrl.isDisposed()) 118 ctrl= null; 119 try { 120 if (ctrl != null) 121 ctrl.setRedraw(false); 122 fSourceViewer.setInput(document); 123 if (region != null && document != null) { 124 int offset= region.getOffset(); 125 int length= region.getLength(); 126 if (offset >= 0 && length >= 0) { 127 fSourceViewer.setSelectedRange(offset, length); 128 fSourceViewer.revealRange(offset, length); 129 } 130 } 131 } finally { 132 if (ctrl != null) 133 ctrl.setRedraw(true); 134 } 135 } 136 137 139 142 public void createControl(Composite parent) { 143 fForm= new ViewForm(parent, SWT.BORDER | SWT.FLAT); 144 fForm.marginWidth= 0; 145 fForm.marginHeight= 0; 146 fLabel= new CLabel(fForm, SWT.NONE); 147 fForm.setTopLeft(fLabel); 148 fForm.addDisposeListener(new DisposeListener() { 149 public void widgetDisposed(DisposeEvent e) { 150 if (fPaneImage != null) 151 fPaneImage.dispose(); 152 } 153 }); 154 155 Dialog.applyDialogFont(parent); 156 157 fSourceViewer= createSourceViewer(fForm); 158 fSourceViewer.setEditable(false); 159 fForm.setContent(fSourceViewer.getControl()); 160 } 161 162 165 public Control getControl() { 166 return fForm; 167 } 168 } 169 170 | Popular Tags |