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 24 import org.eclipse.core.resources.IFolder; 25 import org.eclipse.core.resources.IProject; 26 import org.eclipse.core.resources.IResource; 27 import org.eclipse.core.resources.IWorkspaceRoot; 28 import org.eclipse.jface.viewers.ITreeContentProvider; 29 import org.eclipse.jface.viewers.Viewer; 30 import org.eclipse.ui.IEditorInput; 31 import org.eclipse.ui.IEditorPart; 32 import org.eclipse.ui.IEditorReference; 33 import org.un4gvn.editorviewer.internal.EditorViewerPlugin; 34 import org.un4gvn.editorviewer.internal.Messages; 35 import org.un4gvn.editorviewer.internal.element.EditorElement; 36 import org.un4gvn.editorviewer.internal.element.FolderElement; 37 import org.un4gvn.editorviewer.internal.element.IViewerElement; 38 import org.un4gvn.editorviewer.internal.element.RootElement; 39 40 /*** 41 * Content Provider for the TreeViewer 42 * <p> 43 * @author Sean Ruff 44 */ 45 public class ContentProvider implements ITreeContentProvider { 46 47 /*** 48 * EditorViewer for callbacks 49 */ 50 private EditorViewer _viewer = null; 51 52 private EditorViewerListener _listener = null; 53 54 /*** 55 * IViewerElement which will house all other IViewerElements 56 * but will not be shown in the view 57 */ 58 private IViewerElement _root = null; 59 60 /*** 61 * Layout mode for the View, dictates how the Model 62 * is structed and thus returned to the view 63 */ 64 private int _layoutMode = EditorViewerPlugin.HIERARCHICAL_MODE; 65 66 /*** 67 * Constructor 68 * @param viewer 69 * @param layoutMode 70 */ 71 public ContentProvider(EditorViewer viewer, EditorViewerListener listener, int layoutMode){ 72 _viewer = viewer; 73 _listener = listener; 74 _layoutMode = layoutMode; 75 _root = new RootElement(null,Messages.getString("ContentProvider.0")); //$NON-NLS-1$ 76 refreshContent(); 77 } 78 79 /*** 80 * Retreive the input which will be the root element 81 * @return IViewerElement ( always returns the root element ) 82 */ 83 public IViewerElement getContent(){ 84 return _root; 85 } 86 87 /*** 88 * Sets the layout mode for the model(view). 89 * Note: Calling this causes a instant refresh of the 90 * content, but the view must be updated to see the 91 * change. 92 * @param mode Mode to change to 93 */ 94 public void setLayoutMode(int mode){ 95 if ( _layoutMode != mode ){ 96 _layoutMode = mode; 97 } 98 refreshContent(); 99 } 100 101 /*** 102 * 103 * @return 104 */ 105 public int getLayoutMode(){ 106 return _layoutMode; 107 } 108 109 110 /*** 111 * Create a new Element using the defined mode, where the mode 112 * defines the layout structure of the tree 113 * @see org.un4gvn.editorviewer.internal.EditorViewerPlugin 114 */ 115 public IViewerElement addElement(IEditorPart part){ 116 part.addPropertyListener(_listener); 117 IResource res = getResource(part); 118 IViewerElement parent = null; 119 switch(_layoutMode){ 120 case EditorViewerPlugin.FLAT_MODE: 121 parent = _root; 122 break; 123 case EditorViewerPlugin.HIERARCHICAL_MODE: 124 parent = getParentByHierarchy(_root,res == null ? res : res.getParent()); 125 break; 126 case EditorViewerPlugin.SEMI_FLAT_MODE: 127 parent = getParentBySemiHierarchy(_root,res); 128 break; 129 case EditorViewerPlugin.TYPE_MODE: 130 parent = getParentByType(_root,res); 131 break; 132 } 133 if ( parent == null ) 134 parent = _root; 135 IViewerElement child = null; 136 137 //TODO REFACTOR DO WE REALLY NEED A EXTERNALEDITORELEMENT TYPE? 138 //CAN BELOW CODE BE CLEANED 139 child = parent.getChild(part); 140 if ( child == null ){ 141 child = new EditorElement(parent,part,part); 142 parent.addChild(child); 143 } 144 return child; 145 } 146 147 /*** 148 * Remove an AbstractViewerElement from the Model 149 * and return the element which should be removed 150 * from the underlying tree, which may or may not be 151 * the same element. 152 * @param element IViewerElement to remove 153 */ 154 public void removeElement(IViewerElement child){ 155 //close referenced editor instance 156 if ( child instanceof EditorElement ){ 157 IEditorPart part = ((EditorElement)child).getEditorPart(); 158 part.removePropertyListener(_listener); 159 part.getSite().getPage().closeEditor(part,true); 160 } 161 IViewerElement parent = child.getParent(); 162 if ( parent == null ) 163 return; 164 parent.removeChild(child); 165 //if the parent has no children go ahead and remove it 166 if ( !parent.hasChildren() ) 167 removeElement(parent); 168 return; 169 } 170 171 172 /*** 173 * Refreshes the content by retreiving the list of open 174 * editors and loading them into the content tree based 175 * upon the currently selected layoutmode. 176 */ 177 public void refreshContent(){ 178 _root.removeChildren(); 179 IEditorReference[] refs = EditorViewerPlugin.getInstance().getOpenEditors(); 180 for ( int i = 0 ; i < refs.length ; i++ ){ 181 addElement(refs[i].getEditor(true)); 182 } 183 } 184 185 /*** 186 * Get the parent Element for a specified resource 187 * using a Hierarchical view 188 * @param parent 189 * @param res 190 * @return 191 */ 192 private IViewerElement getParentByHierarchy(IViewerElement parent, IResource res){ 193 if ( res == null || res instanceof IWorkspaceRoot ) 194 return parent; 195 parent = getParentByHierarchy(parent, res.getParent()); 196 if ( res instanceof IProject || res instanceof IFolder ){ 197 IViewerElement child = parent.getChild(res); 198 //not already present 199 if ( child == null ){ 200 if ( res instanceof IProject ) 201 child = new FolderElement(parent,res,res.getName(),EditorViewerPlugin.getInstance().getImage(Messages.getString("ContentProvider.3"))); 202 else 203 child = new FolderElement(parent,res,res.getName()); 204 parent.addChild(child); 205 } 206 return child; 207 } 208 return parent; 209 } 210 211 /*** 212 * Get the parent Element for a specified resource 213 * using a Semi-Hierarchical view 214 * @param parent 215 * @param res 216 * @return 217 */ 218 private IViewerElement getParentBySemiHierarchy(IViewerElement parent, IResource res){ 219 if ( res == null || res instanceof IWorkspaceRoot ) 220 return parent; 221 IProject proj = res.getProject(); 222 if ( proj == null ) 223 return parent; 224 IViewerElement projParent = parent.getChild(proj); 225 if ( projParent == null ){ 226 projParent = new FolderElement(parent,proj,proj.getName(),EditorViewerPlugin.getInstance().getImage(Messages.getString("ContentProvider.3"))); 227 parent.addChild(projParent); 228 } 229 IResource resParent = res.getParent(); 230 if ( !(resParent instanceof IFolder) ) 231 return projParent; 232 IViewerElement folderParent = projParent.getChild(resParent); 233 if ( folderParent == null ){ 234 folderParent = new FolderElement(projParent,resParent,res.getParent().getProjectRelativePath().toString()); 235 projParent.addChild(folderParent); 236 } 237 return folderParent; 238 } 239 240 /*** 241 * Get the parent Element for a specified resource 242 * categorizing by Type 243 * @param parent 244 * @param res 245 * @return 246 */ 247 private IViewerElement getParentByType(IViewerElement parent, IResource res){ 248 if ( res == null || res instanceof IWorkspaceRoot ) 249 return parent; 250 String type = res.getFileExtension(); 251 if ( type == null || type.length() < 1 ) 252 type = Messages.getString("ContentProvider.1"); //$NON-NLS-1$ 253 type += Messages.getString("ContentProvider.2"); //$NON-NLS-1$ 254 IViewerElement child = parent.getChild(type); 255 if ( child == null ){ 256 child = new FolderElement(parent,type,type); 257 parent.addChild(child); 258 } 259 return child; 260 } 261 262 /*** 263 * Retreive an IResource object from a IEditorReference object 264 * @param ref 265 * @return 266 */ 267 private IResource getResource(IEditorPart part){ 268 try { 269 IEditorInput input = part.getEditorInput(); 270 Object obj = input.getAdapter(IResource.class); 271 if ( obj == null ) 272 return null; 273 return (IResource)obj; 274 } catch (NullPointerException npe){ 275 return null; 276 } 277 } 278 279 280 /*** 281 * Retreive a IViewerElement by name 282 * @param name of the IViewerElement to retreive 283 * @return IViewerElement 284 */ 285 public IViewerElement getElement(IEditorPart part){ 286 IViewerElement[] elements = _root.getAllChildren(); 287 for ( int i = 0 ; i < elements.length ; i++ ){ 288 if ( part.equals(elements[i].getId()) ) 289 return elements[i]; 290 } 291 return null; 292 } 293 294 /*** 295 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) 296 */ 297 public Object[] getChildren(Object parentElement) { 298 if ( parentElement instanceof IViewerElement ) 299 return ((IViewerElement)parentElement).getChildren(); 300 return null; 301 } 302 303 /*** 304 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) 305 */ 306 public Object getParent(Object element) { 307 if ( element instanceof IViewerElement ) 308 return ((IViewerElement)element).getParent(); 309 return null; 310 } 311 312 /*** 313 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) 314 */ 315 public boolean hasChildren(Object element) { 316 if ( element instanceof IViewerElement ) 317 return ((IViewerElement)element).hasChildren(); 318 return false; 319 } 320 321 /*** 322 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) 323 */ 324 public Object[] getElements(Object inputElement) { 325 return ((IViewerElement)inputElement).getChildren(); 326 } 327 328 /*** 329 * @see org.eclipse.jface.viewers.IContentProvider#dispose() 330 */ 331 public void dispose() { 332 } 333 334 /*** 335 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) 336 */ 337 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 338 } 339 340 /*** 341 * Get all the Leaf Elements for the model 342 * @return IViewerElement[] 343 */ 344 public IViewerElement[] getAllLeafElements(IViewerElement parent){ 345 IViewerElement[] all = parent.getAllChildren(); 346 ArrayList list = new ArrayList(); 347 for ( int i = 0 ; i < all.length ; i++ ){ 348 if ( all[i].isLeaf() ) 349 list.add(all[i]); 350 } 351 return (IViewerElement[])list.toArray(new IViewerElement[list.size()]); 352 } 353 354 355 }

This page was automatically generated by Maven