1 /* 2 * Created on Jan 19, 2004 3 * 4 * Copyright (C) 2004 Sean Ruff 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 package org.un4gvn.editorviewer.internal.ui; 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.Iterator; 25 import java.util.List; 26 27 import org.eclipse.jface.action.IAction; 28 import org.eclipse.jface.action.IMenuListener; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.action.IToolBarManager; 31 import org.eclipse.jface.action.MenuManager; 32 import org.eclipse.jface.action.Separator; 33 import org.eclipse.jface.viewers.DecoratingLabelProvider; 34 import org.eclipse.jface.viewers.ILabelProvider; 35 import org.eclipse.jface.viewers.IStructuredSelection; 36 import org.eclipse.jface.viewers.StructuredSelection; 37 import org.eclipse.jface.viewers.TreeViewer; 38 import org.eclipse.jface.viewers.ViewerFilter; 39 import org.eclipse.jface.viewers.ViewerSorter; 40 import org.eclipse.swt.widgets.Composite; 41 import org.eclipse.swt.widgets.Menu; 42 import org.eclipse.ui.IEditorPart; 43 import org.eclipse.ui.IMemento; 44 import org.eclipse.ui.IViewSite; 45 import org.eclipse.ui.IWorkbenchActionConstants; 46 import org.eclipse.ui.IWorkbenchPage; 47 import org.eclipse.ui.IWorkbenchWindow; 48 import org.eclipse.ui.PartInitException; 49 import org.eclipse.ui.PlatformUI; 50 import org.eclipse.ui.part.ViewPart; 51 import org.un4gvn.editorviewer.internal.EditorViewerPlugin; 52 import org.un4gvn.editorviewer.internal.Messages; 53 import org.un4gvn.editorviewer.internal.action.CloseAction; 54 import org.un4gvn.editorviewer.internal.action.CloseAllAction; 55 import org.un4gvn.editorviewer.internal.action.CollapseAllAction; 56 import org.un4gvn.editorviewer.internal.action.ExpandAllAction; 57 import org.un4gvn.editorviewer.internal.action.LayoutActionGroup; 58 import org.un4gvn.editorviewer.internal.action.LinkEditorAction; 59 import org.un4gvn.editorviewer.internal.action.MultiActionGroup; 60 import org.un4gvn.editorviewer.internal.action.RefreshAction; 61 import org.un4gvn.editorviewer.internal.action.SelectAllAction; 62 import org.un4gvn.editorviewer.internal.action.SortActionGroup; 63 import org.un4gvn.editorviewer.internal.element.IViewerElement; 64 65 /*** 66 * ViewPart which adds a TreeViewer to the view and provides callback functions 67 * to the Listeners,ContentProviders and Actions. 68 * <p> 69 * @author Sean Ruff 70 */ 71 public class EditorViewer extends ViewPart { 72 73 /*** 74 * TreeViewer 75 */ 76 private TreeViewer _tree = null; 77 78 /*** 79 * IActions 80 */ 81 private IAction _closeItemAction = null; 82 private IAction _closeAllItemsAction = null; 83 private IAction _selectAllItemsAction = null; 84 private IAction _refreshItemsAction = null; 85 private IAction _collapseAllItemsAction = null; 86 private IAction _expandAllItemsAction = null; 87 private IAction _linkWithEditorAction = null; 88 89 /*** 90 * MultiActionGroup 91 */ 92 private MultiActionGroup _layoutItemsAction = null; 93 private MultiActionGroup _sortItemsAction = null; 94 95 /*** 96 * EditorViewerListener 97 */ 98 private EditorViewerListener _listener = null; 99 100 101 /*** 102 * State information storage 103 */ 104 private IMemento _memento = null; 105 106 /*** 107 * Content Provider 108 */ 109 private ContentProvider _contentProvider = null; 110 111 /*** 112 * Label Provider 113 */ 114 private ILabelProvider _labelProvider = null; 115 116 /*** 117 * Default Constructor 118 */ 119 public EditorViewer(){} 120 121 122 /*** 123 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento) 124 */ 125 public void init(IViewSite site, IMemento memento) throws PartInitException { 126 super.init(site, memento); 127 _memento = memento; 128 } 129 130 131 132 /*** 133 * @see org.eclipse.ui.part.ViewPart#saveState(org.eclipse.ui.IMemento) 134 */ 135 public void saveState(IMemento memento) { 136 super.saveState(memento); 137 if ( memento == null ) 138 return; 139 memento.putInteger(Messages.getString("EditorViewer.3"),_contentProvider.getLayoutMode()); //$NON-NLS-1$ 140 memento.putInteger(Messages.getString("EditorViewer.4"),_sortItemsAction.getSelection()); //$NON-NLS-1$ 141 memento.putInteger(Messages.getString("EditorViewer.5"),_linkWithEditorAction.isChecked() ? 0 : 1); //$NON-NLS-1$ 142 } 143 144 145 /*** 146 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) 147 */ 148 public void createPartControl(Composite parent) { 149 //initialize 150 _listener = new EditorViewerListener(this); 151 _contentProvider = new ContentProvider(this,_listener,getStoredStateAsInt(Messages.getString("EditorViewer.0"),EditorViewerPlugin.HIERARCHICAL_MODE)); //$NON-NLS-1$ 152 _labelProvider = new DecoratingLabelProvider(new LabelProvider(),PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()); 153 _labelProvider.addListener(_listener); 154 155 //add listeners 156 getSite().getWorkbenchWindow().addPageListener(_listener); 157 getSite().getWorkbenchWindow().getActivePage().addPartListener(_listener); 158 // ResourcesPlugin.getWorkspace().addResourceChangeListener(_listener,ResourceChangeEvent.POST_AUTO_BUILD); 159 160 //get the treeviewer instance 161 _tree = new TreeViewer(parent); 162 _tree.setLabelProvider(_labelProvider); 163 _tree.setContentProvider(_contentProvider); 164 _tree.addOpenListener(_listener); 165 166 //Create Menu/Toolbars 167 initActions(); 168 createMenu(); 169 createToolbar(); 170 createContextMenu(); 171 172 //set the input 173 _tree.setInput(_contentProvider.getContent()); 174 } 175 176 private void initActions(){ 177 //create actions 178 _closeItemAction = new CloseAction(this); 179 _closeAllItemsAction = new CloseAllAction(this); 180 _collapseAllItemsAction = new CollapseAllAction(this); 181 _expandAllItemsAction = new ExpandAllAction(this); 182 _selectAllItemsAction = new SelectAllAction(this); 183 _refreshItemsAction = new RefreshAction(this); 184 _linkWithEditorAction = new LinkEditorAction(this, getStoredStateAsInt(Messages.getString("EditorViewer.1"),EditorViewerPlugin.NON_LINKED_MODE)); //$NON-NLS-1$ 185 _layoutItemsAction = new LayoutActionGroup(this,_contentProvider.getLayoutMode()); 186 _sortItemsAction = new SortActionGroup(this, getStoredStateAsInt(Messages.getString("EditorViewer.2"),EditorViewerPlugin.NONE_SORT_MODE)); //$NON-NLS-1$ 187 } 188 189 190 /*** 191 * @see org.eclipse.ui.IWorkbenchPart#setFocus() 192 */ 193 public void setFocus() {} 194 195 196 /*** 197 * Retreive int values from the memento object or a defaultVal if 198 * null 199 * @param key 200 * @param defaultVal 201 * @return 202 */ 203 private int getStoredStateAsInt(String key, int defaultVal){ 204 if ( _memento == null ) 205 return defaultVal; 206 Integer val = _memento.getInteger(key); 207 if ( val == null ) 208 return defaultVal; 209 return val.intValue(); 210 } 211 212 /*** 213 * Create the Menu 214 */ 215 private void createMenu() { 216 IMenuManager manager = this.getViewSite().getActionBars().getMenuManager(); 217 manager.add(_selectAllItemsAction); 218 manager.add(_refreshItemsAction); 219 manager.add(_collapseAllItemsAction); 220 manager.add(_expandAllItemsAction); 221 manager.add(_closeItemAction); 222 manager.add(_closeAllItemsAction); 223 _layoutItemsAction.fillActionBars(getViewSite().getActionBars()); 224 _sortItemsAction.fillActionBars(getViewSite().getActionBars()); 225 } 226 227 /*** 228 * Create the ToolBar 229 */ 230 private void createToolbar() { 231 IToolBarManager manager = this.getViewSite().getActionBars().getToolBarManager(); 232 manager.add(_closeAllItemsAction); 233 manager.add(_collapseAllItemsAction); 234 manager.add(_linkWithEditorAction); 235 } 236 237 /*** 238 * Create Context Menu 239 */ 240 private void createContextMenu() { 241 MenuManager manager = new MenuManager(Messages.getString("EditorViewer.6")); //$NON-NLS-1$ 242 manager.setRemoveAllWhenShown(true); 243 manager.addMenuListener(new IMenuListener() { 244 public void menuAboutToShow(IMenuManager manager) { 245 EditorViewer.this.fillContextMenu(manager); 246 } 247 }); 248 Menu menu = manager.createContextMenu(_tree.getControl()); 249 _tree.getControl().setMenu(menu); 250 getSite().registerContextMenu(manager,_tree); 251 } 252 253 /*** 254 * Fill the context menu 255 * @param manager 256 */ 257 private void fillContextMenu(IMenuManager manager){ 258 manager.add(_closeItemAction); 259 manager.add(_closeAllItemsAction); 260 manager.add(_collapseAllItemsAction); 261 manager.add(_expandAllItemsAction); 262 manager.add(new Separator()); 263 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 264 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS+Messages.getString("EditorViewer.7"))); //$NON-NLS-1$ 265 manager.add(_refreshItemsAction); 266 } 267 268 /*** 269 * Retreive current selection 270 * @return ISelection 271 */ 272 public IViewerElement[] getSelection(){ 273 IStructuredSelection sel = (IStructuredSelection)_tree.getSelection(); 274 Iterator it = sel.iterator(); 275 List leafs = new ArrayList(sel.size()); 276 while ( it.hasNext() ){ 277 IViewerElement el = (IViewerElement)it.next(); 278 if ( el.isLeaf() ){ 279 leafs.add(el); 280 } else { 281 leafs.addAll(Arrays.asList(_contentProvider.getAllLeafElements(el))); 282 } 283 } 284 285 return (IViewerElement[])leafs.toArray(new IViewerElement[leafs.size()]); 286 } 287 288 /*** 289 * Set the Selection for the viewer 290 * @param elements 291 */ 292 public void setSelection(IViewerElement[] elements){ 293 _tree.setSelection(new StructuredSelection(elements)); 294 } 295 296 /*** 297 * Select everything in the tree 298 */ 299 public void selectAll(){ 300 _tree.setSelection(new StructuredSelection(_contentProvider.getAllLeafElements(_contentProvider.getContent()))); 301 } 302 303 /*** 304 * Add the element to the Model (Content Provider) 305 * then add it to the tree; 306 */ 307 public IViewerElement addElement(IEditorPart part){ 308 //turn off redrawing so the change seems faster to user 309 _tree.getTree().setRedraw(false); 310 IViewerElement element = _contentProvider.addElement(part); 311 // part.addPropertyListener(_listener); 312 _tree.refresh(); 313 expand(element,1); 314 //change complete turn redrawing back on 315 _tree.getTree().setRedraw(true); 316 return element; 317 } 318 319 // public EditorViewerListener getListener(){ return _listener; } 320 321 /*** 322 * Removes an element from the Model 323 * then removes it from the tree 324 * @param child 325 */ 326 public void removeElement(IViewerElement child){ 327 _tree.getTree().setRedraw(false); 328 //remove from model 329 _contentProvider.removeElement(child); 330 331 //note this has to be done before the below selection is made otherwise 332 //the tree will not respond new inputs after a close all (??-Why) 333 _tree.refresh(); 334 _tree.getTree().setRedraw(true); 335 336 //link with active editor if necessary 337 if ( isLinkedWithEditor() ){ 338 //we need to update selection to correspond to the active editor 339 IWorkbenchWindow win = getSite().getWorkbenchWindow(); 340 if ( win == null ) return; 341 IWorkbenchPage page = win.getActivePage(); 342 if ( page == null ) return; 343 IEditorPart part = page.getActiveEditor(); 344 if ( part == null ) return; 345 IViewerElement active = getElement(part); 346 if ( active != null ) 347 setSelection(new IViewerElement[]{active}); 348 } 349 } 350 351 /*** 352 * Expand the entire tree 353 */ 354 public void expandAll(){ 355 _tree.getTree().setRedraw(false); 356 _tree.expandAll(); 357 _tree.getTree().setRedraw(true); 358 } 359 360 /*** 361 * Expand a specific element and all children elements 362 * to <code>level</code> 363 * @param el The place in the Viewer to expand to 364 * @param level depth to expand to 365 */ 366 public void expand(IViewerElement el, int level){ 367 _tree.getTree().setRedraw(false); 368 _tree.expandToLevel(el,level); 369 _tree.getTree().setRedraw(true); 370 } 371 372 /*** 373 * Determine if the specified element is already expanded in the tree 374 * @param obj element to check for expansion state 375 * @return 376 */ 377 public boolean isExpanded(IViewerElement obj){ 378 return _tree.getExpandedState(obj); 379 } 380 381 /*** 382 * Collapse all elements within the tree 383 */ 384 public void collapseAll(){ 385 _tree.getTree().setRedraw(false); 386 _tree.collapseAll(); 387 _tree.getTree().setRedraw(true); 388 } 389 390 /*** 391 * Collapse specified element to specified level 392 * @param el 393 * @param level 394 */ 395 public void collapse(IViewerElement el, int level){ 396 _tree.getTree().setRedraw(false); 397 _tree.collapseToLevel(el,level); 398 _tree.getTree().setRedraw(true); 399 } 400 401 /*** 402 * Switch the Expansion state of the specified element 403 * if collapsed then expand, if expanded then collapsed 404 * @param obj 405 * @param level 406 */ 407 public void switchExpansion(IViewerElement el, int level){ 408 if ( _tree.getExpandedState(el) ) 409 collapse(el,level); 410 else 411 expand(el,level); 412 } 413 414 /*** 415 * Set the Sorter for the Viewer 416 * @param sorter 417 */ 418 public void setSorter(ViewerSorter sorter){ 419 _tree.setSorter(sorter); 420 } 421 422 /*** 423 * Set the Filter for the Viewer 424 */ 425 public void setFilter(ViewerFilter filter){ 426 //TODO Add filtering capability 427 return; 428 } 429 430 /*** 431 * Dispatch to the ContentProvider to change the layout mode 432 * @param mode 433 */ 434 public void setLayoutMode(int mode){ 435 _tree.getTree().setRedraw(false); 436 IViewerElement content = _contentProvider.getContent(); 437 _contentProvider.setLayoutMode(mode); 438 _tree.getTree().removeAll(); 439 _tree.setInput(content); 440 _tree.expandAll(); 441 _tree.refresh(); 442 _tree.getTree().setRedraw(true); 443 } 444 445 /*** 446 * Refresh the label for the specified IViewerElement 447 * @param el 448 */ 449 public void refreshLabel(IViewerElement el){ 450 _tree.getTree().setRedraw(false); 451 _tree.refresh(el,true); 452 _tree.getTree().setRedraw(true); 453 } 454 455 /*** 456 * Refresh a specific element 457 * @param el 458 */ 459 public void refreshElement(IViewerElement el){ 460 _tree.getTree().setRedraw(false); 461 _tree.refresh(el); 462 _tree.getTree().setRedraw(true); 463 } 464 465 /*** 466 * Refresh the content (Model) 467 */ 468 public void refreshContent(){ 469 setLayoutMode(_contentProvider.getLayoutMode()); 470 } 471 472 /*** 473 * Refresh the view 474 */ 475 public void refreshView(){ 476 _tree.getTree().setRedraw(false); 477 _tree.refresh(); 478 _tree.getTree().setRedraw(true); 479 } 480 481 /*** 482 * Determine whether the View is linked to editors 483 * @return 484 */ 485 public boolean isLinkedWithEditor(){ 486 if ( _linkWithEditorAction == null ) 487 return false; 488 return _linkWithEditorAction.isChecked(); 489 } 490 491 /*** 492 * Retreive an EditorElement by it's ad implementation 493 * @param ad 494 * @return 495 */ 496 public IViewerElement getElement(IEditorPart part){ 497 return _contentProvider.getElement(part); 498 } 499 500 }

This page was automatically generated by Maven