sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.wizards/src/com/nokia/carbide/cpp/internal/pi/wizards/ui/util/PkgListTreeContentProvider.java
changeset 2 b9ab3b238396
child 5 844b047e260d
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.internal.pi.wizards.ui.util;
       
    19 
       
    20 import java.util.ArrayList;
       
    21 import java.util.HashMap;
       
    22 import java.util.List;
       
    23 import java.util.Map;
       
    24 
       
    25 import org.eclipse.core.resources.IProject;
       
    26 import org.eclipse.core.resources.IWorkspaceRoot;
       
    27 import org.eclipse.core.resources.ResourcesPlugin;
       
    28 import org.eclipse.core.runtime.CoreException;
       
    29 import org.eclipse.jface.viewers.ITreeContentProvider;
       
    30 import org.eclipse.jface.viewers.Viewer;
       
    31 import org.eclipse.ui.IViewSite;
       
    32 import org.eclipse.ui.model.WorkbenchContentProvider;
       
    33 
       
    34 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
       
    35 import com.nokia.carbide.cdt.builder.project.ICarbideBuildConfiguration;
       
    36 import com.nokia.carbide.cdt.builder.project.ICarbideProjectInfo;
       
    37 
       
    38 public class PkgListTreeContentProvider implements ITreeContentProvider {
       
    39 	PkgListTree tree;
       
    40 	// need to work around CarbideBuildConfiguration equal() that claims
       
    41 	// configuration objects are equal even when their projects are different
       
    42 	Map<IProject, ArrayList<Object>> configMap = new HashMap<IProject, ArrayList<Object>>();
       
    43 	WorkbenchContentProvider cp = new WorkbenchContentProvider();
       
    44 	
       
    45 	public PkgListTreeContentProvider(PkgListTree myTree) {
       
    46 		tree = myTree;
       
    47 	}
       
    48 	
       
    49 	public Object[] getChildren(Object parentElement) {
       
    50 		if (parentElement instanceof IPkgEntry) {
       
    51 			return null;
       
    52 		} else if (parentElement instanceof IViewSite) {
       
    53 			return getCarbideCppProjects();
       
    54 		} else if (parentElement instanceof IWorkspaceRoot) {
       
    55 			return getCarbideCppProjects();
       
    56 		} else if (parentElement instanceof IProject) {
       
    57 			return getConfigs((IProject)parentElement).toArray();
       
    58 		} else if (parentElement instanceof PkgListTree) {
       
    59 			return getElements(parentElement);
       
    60 		}
       
    61 		return null;
       
    62 	}
       
    63 
       
    64 	public Object getParent(Object element) {
       
    65 		if (element instanceof ICarbideBuildConfiguration) {
       
    66 			return ((ICarbideBuildConfiguration)element).getCarbideProject().getProject();
       
    67 		}
       
    68 		return cp.getParent(element);
       
    69 	}
       
    70 
       
    71 	public boolean hasChildren(Object element) {
       
    72 		if (element instanceof IPkgEntry) {
       
    73 			return false;
       
    74 		}else if (element instanceof ICarbideBuildConfiguration) {
       
    75 			return false;
       
    76 		}else if (element instanceof IProject) {
       
    77 			return true;
       
    78 		}else if (element instanceof PkgListTree) {
       
    79 			return true;
       
    80 		}
       
    81 		
       
    82 		return cp.hasChildren(element);
       
    83 	}
       
    84 
       
    85 	public Object[] getElements(Object inputElement) {
       
    86 		return tree.getRoot();
       
    87 	}
       
    88 
       
    89 	public void dispose() {
       
    90 		cp.dispose();
       
    91 	}
       
    92 
       
    93 	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
       
    94 		configMap.clear();
       
    95 		cp.inputChanged(viewer, oldInput, newInput);
       
    96 	}
       
    97 
       
    98 	private Object[] getCarbideCppProjects() {
       
    99 		// lifted from com.nokia.carbide.cpp.project.ui.views.SPNViewContentProvider
       
   100 		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
       
   101 		List<IProject> list = new ArrayList<IProject>();
       
   102 		for (int i = 0; i < projects.length; i++) {
       
   103 			try {
       
   104 				if (projects[i].isAccessible() && projects[i].hasNature(CarbideBuilderPlugin.CARBIDE_PROJECT_NATURE_ID)) {
       
   105 					if (projects[i].isOpen()) {
       
   106 						ICarbideProjectInfo cpi = CarbideBuilderPlugin.getBuildManager().getProjectInfo(projects[i]);
       
   107 						if (cpi == null)
       
   108 							continue; // must have valid Carbide project info
       
   109 						List<ICarbideBuildConfiguration> bc = cpi.getBuildConfigurations();
       
   110 						boolean haveConfigWithPKG = false;
       
   111 						// if this project have all configs with (none) PKG, don't show the project
       
   112 						for (ICarbideBuildConfiguration config : bc) {
       
   113 							if (config.getSISBuilderInfoList().size() > 0) {
       
   114 								haveConfigWithPKG = true;
       
   115 								break;
       
   116 							}
       
   117 						}
       
   118 						if (haveConfigWithPKG == false)
       
   119 							continue;	// must have buildconfig with good PKG to show up
       
   120 						list.add(projects[i]);
       
   121 					}
       
   122 				}
       
   123 			} catch (CoreException e) {
       
   124 				e.printStackTrace();
       
   125 			}
       
   126 		}
       
   127 		return list.toArray();
       
   128 	}
       
   129 	
       
   130 	private ArrayList<Object> getConfigs(IProject project) {
       
   131 		ArrayList<Object> configs = configMap.get(project);
       
   132 		if (configs == null) {
       
   133 			configs = new ArrayList<Object>();
       
   134 			configMap.put(project, configs);
       
   135 			if (project.exists() == false || project.isOpen() == false)
       
   136 				return configs;	// closed project do not have config
       
   137 			ICarbideProjectInfo cpi = CarbideBuilderPlugin.getBuildManager().getProjectInfo(project);
       
   138 			if (cpi == null)
       
   139 				return configs;
       
   140 			List<ICarbideBuildConfiguration> bc = cpi.getBuildConfigurations();
       
   141 			if (bc.size() < 1)
       
   142 				return configs;
       
   143 			for (ICarbideBuildConfiguration config : bc) {
       
   144 				// exclude configuration with (none) PKG
       
   145 				if (config.getSISBuilderInfoList().size() > 0) {
       
   146 					configs.add(config);
       
   147 				}
       
   148 			}
       
   149 		}
       
   150 		return configs;
       
   151 	}
       
   152 }