1 11 package org.eclipse.jface.viewers; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.swt.events.DisposeEvent; 15 import org.eclipse.swt.events.DisposeListener; 16 import org.eclipse.swt.widgets.Control; 17 18 47 public abstract class ContentViewer extends Viewer { 48 49 52 private IContentProvider contentProvider = null; 53 54 58 private Object input = null; 59 60 64 private IBaseLabelProvider labelProvider = null; 65 66 72 private final ILabelProviderListener labelProviderListener = new ILabelProviderListener() { 73 public void labelProviderChanged(LabelProviderChangedEvent event) { 74 ContentViewer.this.handleLabelProviderChanged(event); 75 } 76 }; 77 78 82 protected ContentViewer() { 83 } 84 85 99 public IContentProvider getContentProvider() { 100 return contentProvider; 101 } 102 103 109 public Object getInput() { 110 return input; 111 } 112 113 127 public IBaseLabelProvider getLabelProvider() { 128 if (labelProvider == null) { 129 labelProvider = new LabelProvider(); 130 } 131 return labelProvider; 132 } 133 134 146 protected void handleDispose(DisposeEvent event) { 147 if (contentProvider != null) { 148 contentProvider.inputChanged(this, getInput(), null); 149 contentProvider.dispose(); 150 contentProvider = null; 151 } 152 if (labelProvider != null) { 153 labelProvider.removeListener(labelProviderListener); 154 labelProvider.dispose(); 155 labelProvider = null; 156 } 157 input = null; 158 } 159 160 169 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) { 170 labelProviderChanged(); 171 } 172 173 188 protected void hookControl(Control control) { 189 control.addDisposeListener(new DisposeListener() { 190 public void widgetDisposed(DisposeEvent event) { 191 handleDispose(event); 192 } 193 }); 194 } 195 196 203 protected void labelProviderChanged() { 204 refresh(); 205 } 206 207 220 public void setContentProvider(IContentProvider contentProvider) { 221 Assert.isNotNull(contentProvider); 222 IContentProvider oldContentProvider = this.contentProvider; 223 this.contentProvider = contentProvider; 224 if (oldContentProvider != null) { 225 Object currentInput = getInput(); 226 oldContentProvider.inputChanged(this, currentInput, null); 227 oldContentProvider.dispose(); 228 contentProvider.inputChanged(this, null, currentInput); 229 refresh(); 230 } 231 } 232 233 241 public void setInput(Object input) { 242 Assert 243 .isTrue(getContentProvider() != null, 244 "ContentViewer must have a content provider when input is set."); 246 Object oldInput = getInput(); 247 contentProvider.inputChanged(this, oldInput, input); 248 this.input = input; 249 250 inputChanged(this.input, oldInput); 252 } 253 254 267 public void setLabelProvider(IBaseLabelProvider labelProvider) { 268 IBaseLabelProvider oldProvider = this.labelProvider; 269 if (labelProvider == oldProvider) { 273 return; 274 } 275 if (oldProvider != null) { 276 oldProvider.removeListener(this.labelProviderListener); 277 } 278 this.labelProvider = labelProvider; 279 if (labelProvider != null) { 280 labelProvider.addListener(this.labelProviderListener); 281 } 282 refresh(); 283 284 if (oldProvider != null) { 286 oldProvider.dispose(); 287 } 288 } 289 } 290 | Popular Tags |