1 11 package org.eclipse.compare; 12 13 import org.eclipse.compare.contentmergeviewer.IFlushable; 14 import org.eclipse.compare.internal.*; 15 import org.eclipse.compare.structuremergeviewer.ICompareInput; 16 import org.eclipse.jface.viewers.*; 17 import org.eclipse.swt.events.DisposeEvent; 18 import org.eclipse.swt.events.DisposeListener; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.widgets.*; 21 22 import com.ibm.icu.text.MessageFormat; 23 24 25 38 public abstract class CompareViewerSwitchingPane extends CompareViewerPane { 39 40 private Viewer fViewer; 41 private boolean fControlVisibility= false; 42 private String fTitle; 43 private String fTitleArgument; 44 45 59 public CompareViewerSwitchingPane(Composite parent, int style) { 60 this(parent, style, false); 61 } 62 63 78 public CompareViewerSwitchingPane(Composite parent, int style, boolean visibility) { 79 super(parent, style); 80 81 fControlVisibility= visibility; 82 83 setViewer(new NullViewer(this)); 84 85 addDisposeListener( 86 new DisposeListener() { 87 public void widgetDisposed(DisposeEvent e) { 88 if (fViewer != null) 89 fViewer.removeSelectionChangedListener(CompareViewerSwitchingPane.this); 90 if (fViewer instanceof StructuredViewer) { 91 StructuredViewer sv= (StructuredViewer) fViewer; 92 sv.removeDoubleClickListener(CompareViewerSwitchingPane.this); 93 sv.removeOpenListener(CompareViewerSwitchingPane.this); 94 } 95 fViewer= null; 96 } 97 } 98 ); 99 } 100 101 106 public Viewer getViewer() { 107 return fViewer; 108 } 109 110 private void setViewer(Viewer newViewer) { 111 112 if (newViewer == fViewer) 113 return; 114 115 boolean oldEmpty= isEmpty(); 116 117 if (fViewer != null) { 118 119 fViewer.removeSelectionChangedListener(this); 120 121 if (fViewer instanceof StructuredViewer) { 122 StructuredViewer sv= (StructuredViewer) fViewer; 123 sv.removeDoubleClickListener(this); 124 sv.removeOpenListener(this); 125 } 126 127 Control content= getContent(); 128 setContent(null); 129 130 fViewer.setInput(null); 131 132 if (content != null && !content.isDisposed()) 133 content.dispose(); 134 135 } else 136 oldEmpty= false; 137 setContent(null); 138 139 fViewer= newViewer; 140 141 if (fViewer != null) { 142 boolean old= getVisible(); 145 setContent(fViewer.getControl()); 146 setVisible(old); 148 boolean newEmpty= isEmpty(); 149 150 fViewer.addSelectionChangedListener(this); 151 152 if (fViewer instanceof StructuredViewer) { 153 StructuredViewer sv= (StructuredViewer) fViewer; 154 sv.addDoubleClickListener(this); 155 sv.addOpenListener(this); 156 } 157 158 if (oldEmpty != newEmpty) { Composite parent= getParent(); 160 if (parent instanceof Splitter) 161 ((Splitter)parent).setVisible(this, fControlVisibility ? !newEmpty : true); 162 } 163 164 layout(true); 165 } 166 } 167 168 176 public String getTitleArgument() { 177 return fTitleArgument; 178 } 179 180 186 public boolean isEmpty() { 187 return fViewer == null || fViewer instanceof NullViewer; 188 } 189 190 193 public ISelection getSelection() { 194 if (fViewer != null) 195 return fViewer.getSelection(); 196 return super.getSelection(); 197 } 198 199 202 public void setSelection(ISelection s) { 203 if (fViewer != null) 204 fViewer.setSelection(s); 205 } 206 207 private boolean hasFocus2() { 208 Display display= getDisplay(); 210 if (display != null) 211 for (Control focus= display.getFocusControl(); focus != null; focus= focus.getParent()) 212 if (focus == this) 213 return true; 214 return false; 215 } 216 217 231 public void setInput(Object input) { 232 233 if (getInput() == input) 234 return; 235 236 boolean hadFocus= hasFocus2(); 237 238 super.setInput(input); 239 240 Viewer newViewer= null; 242 if (input != null) 243 newViewer= getViewer(fViewer, input); 244 245 if (newViewer == null) { 246 if (fViewer instanceof NullViewer) 247 return; 248 newViewer= new NullViewer(this); 249 } 250 251 setViewer(newViewer); 252 253 fViewer.setInput(input); 255 256 Image image= null; 257 if (!(fViewer instanceof NullViewer) && input instanceof ICompareInput) 258 image= ((ICompareInput)input).getImage(); 259 setImage(image); 260 261 String title= null; 262 if (fViewer != null) { 263 Control c= fViewer.getControl(); 264 if (c != null) { 265 Object data= c.getData(CompareUI.COMPARE_VIEWER_TITLE); 266 if (data instanceof String ) 267 title= (String ) data; 268 if (hadFocus) 269 c.setFocus(); 270 } 271 } 272 273 fTitle= title; 274 updateTitle(); 275 } 276 277 283 public void setTitleArgument(String argument) { 284 fTitleArgument= argument; 285 updateTitle(); 286 } 287 288 private void updateTitle() { 289 if (fTitle != null) { 290 if (fTitleArgument != null) { 291 String format= CompareMessages.CompareViewerSwitchingPane_Titleformat; 292 String t= MessageFormat.format(format, new String [] { fTitle, fTitleArgument } ); 293 setText(t); 294 } else 295 setText(fTitle); 296 } else { 297 setText(""); } 299 } 300 301 306 public Object getAdapter(Class adapter) { 307 if (adapter == INavigatable.class) { 308 if (isEmpty()) 309 return null; 310 Viewer viewer= getViewer(); 311 if (viewer == null) 312 return null; 313 Control control= viewer.getControl(); 314 if (control == null) 315 return null; 316 Object data= control.getData(INavigatable.NAVIGATOR_PROPERTY); 317 if (data instanceof INavigatable) 318 return data; 319 } 320 if (adapter == IFlushable.class) { 321 Viewer v= getViewer(); 322 if (v != null) { 323 IFlushable flushable = (IFlushable)Utilities.getAdapter(v, IFlushable.class); 324 if (flushable != null) 325 return flushable; 326 } 327 } 328 return super.getAdapter(adapter); 329 } 330 331 334 public boolean setFocus() { 335 Viewer v= getViewer(); 336 if (v != null) { 337 Control c= v.getControl(); 338 if (c != null) { 339 if (c.setFocus()) 340 return true; 341 } 342 } 343 return super.setFocus(); 344 } 345 346 358 abstract protected Viewer getViewer(Viewer oldViewer, Object input); 359 } 360 | Popular Tags |