sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.button/src/com/nokia/carbide/cpp/internal/pi/button/ui/BupProfileTreeViewer.java
changeset 2 b9ab3b238396
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.button.ui;
       
    19 
       
    20 import java.util.ArrayList;
       
    21 import java.util.Set;
       
    22 
       
    23 import org.eclipse.jface.viewers.IBaseLabelProvider;
       
    24 import org.eclipse.jface.viewers.ISelection;
       
    25 import org.eclipse.jface.viewers.ITreeContentProvider;
       
    26 import org.eclipse.jface.viewers.ITreeSelection;
       
    27 import org.eclipse.jface.viewers.LabelProvider;
       
    28 import org.eclipse.jface.viewers.TreeViewer;
       
    29 import org.eclipse.jface.viewers.Viewer;
       
    30 import org.eclipse.swt.SWT;
       
    31 import org.eclipse.swt.events.SelectionEvent;
       
    32 import org.eclipse.swt.events.SelectionListener;
       
    33 import org.eclipse.swt.widgets.Composite;
       
    34 
       
    35 import com.nokia.carbide.cpp.pi.button.BupEventMapManager;
       
    36 import com.nokia.carbide.cpp.pi.button.IBupEventMapProfile;
       
    37 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    38 
       
    39 
       
    40 public class BupProfileTreeViewer extends TreeViewer {
       
    41 	private static final String WORKSPACE_PREF = Messages.getString("BupProfileTreeViewer.0"); //$NON-NLS-1$
       
    42 	private static final String CARBIDE_BUILTIN = Messages.getString("BupProfileTreeViewer.1"); //$NON-NLS-1$
       
    43 
       
    44 	private ISymbianSDK romKit;
       
    45 	private Set<ISymbianSDK> appKits;
       
    46 	private ISelection lastValidSelection;
       
    47 	
       
    48 	public class BupProfileTreeData {
       
    49 		private ISymbianSDK romSdk;
       
    50 		private Set<ISymbianSDK> appSdks;
       
    51 		
       
    52 		public BupProfileTreeData(ISymbianSDK rom, Set<ISymbianSDK> app) {
       
    53 			romSdk = rom;
       
    54 			appSdks = app;
       
    55 		}
       
    56 		
       
    57 		public ISymbianSDK getRomSdk() {
       
    58 			return romSdk;
       
    59 		}
       
    60 		
       
    61 		public Set<ISymbianSDK> getAppSdk() {
       
    62 			return appSdks;
       
    63 		}
       
    64 	}
       
    65 		
       
    66 	public class BupProfileTreeContentProvider implements ITreeContentProvider {
       
    67 
       
    68 		/* (non-Javadoc)
       
    69 		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
       
    70 		 */
       
    71 		public Object[] getChildren(Object arg0) {
       
    72 			if (arg0 instanceof String) {
       
    73 				if (((String)arg0).equals(WORKSPACE_PREF)) {
       
    74 					ArrayList<IBupEventMapProfile> workspaceProfiles = BupEventMapManager.getInstance().getProfilesFromWorkspacePref();
       
    75 					return workspaceProfiles.toArray();
       
    76 				}
       
    77 				if (((String)arg0).equals(CARBIDE_BUILTIN)) {
       
    78 					ArrayList<IBupEventMapProfile> builtinProfiles = BupEventMapManager.getInstance().getProfilesFromBuiltin();
       
    79 					return builtinProfiles.toArray();
       
    80 				}
       
    81 			}
       
    82 			if (arg0 instanceof ISymbianSDK) {
       
    83 				ArrayList<IBupEventMapProfile> sdkProfile = BupEventMapManager.getInstance().getProfilesFromSDK((ISymbianSDK)arg0);
       
    84 				return sdkProfile.toArray();
       
    85 			}
       
    86 			return null;
       
    87 		}
       
    88 
       
    89 		/* (non-Javadoc)
       
    90 		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
       
    91 		 */
       
    92 		public Object getParent(Object arg0) {
       
    93 			// not implemented
       
    94 			return null;
       
    95 		}
       
    96 
       
    97 		/* (non-Javadoc)
       
    98 		 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
       
    99 		 */
       
   100 		public boolean hasChildren(Object arg0) {
       
   101 			if (arg0 instanceof String) {
       
   102 				if (((String)arg0).equals(WORKSPACE_PREF) || ((String)arg0).equals(CARBIDE_BUILTIN)) {
       
   103 					return true;
       
   104 				}
       
   105 			}
       
   106 			if (arg0 instanceof ISymbianSDK) {
       
   107 				return true;
       
   108 			}
       
   109 			return false;
       
   110 		}
       
   111 
       
   112 		/* (non-Javadoc)
       
   113 		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
       
   114 		 */
       
   115 		public Object[] getElements(Object arg0) {
       
   116 			ArrayList<Object> elements = new ArrayList<Object>();
       
   117 			if (romKit != null) {
       
   118 				if (getChildren(romKit).length > 0) {
       
   119 					elements.add(romKit);
       
   120 				}
       
   121 			}
       
   122 			
       
   123 			ISymbianSDK [] appSdks = appKits.toArray(new ISymbianSDK[appKits.size()]);
       
   124 			for (ISymbianSDK sdk : appSdks) {
       
   125 				if (getChildren(sdk).length > 0) {
       
   126 					elements.add(sdk);
       
   127 				}
       
   128 			}
       
   129 			elements.add(WORKSPACE_PREF);
       
   130 			elements.add(CARBIDE_BUILTIN);
       
   131 			
       
   132 			return elements.toArray();
       
   133 		}
       
   134 
       
   135 		/* (non-Javadoc)
       
   136 		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
       
   137 		 */
       
   138 		public void dispose() {
       
   139 		}
       
   140 
       
   141 		/* (non-Javadoc)
       
   142 		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
       
   143 		 */
       
   144 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
       
   145 			if (newInput instanceof BupProfileTreeData) {
       
   146 				BupProfileTreeData data = (BupProfileTreeData)newInput;
       
   147 				romKit = data.romSdk;
       
   148 				appKits = data.appSdks;
       
   149 			}
       
   150 		}
       
   151 		
       
   152 	}
       
   153 	
       
   154 	private class BupProfileTreeLabelProvider extends LabelProvider implements IBaseLabelProvider{
       
   155 		public String getText(Object element) {
       
   156 			if (element instanceof IBupEventMapProfile) {
       
   157 				return ((IBupEventMapProfile)element).getProfileId();
       
   158 			}
       
   159 			if (element instanceof String) {
       
   160 				if (((String)element).equals(WORKSPACE_PREF)) {
       
   161 					return (String)element;
       
   162 				}
       
   163 				if (((String)element).equals(CARBIDE_BUILTIN)) {
       
   164 					return (String)element;
       
   165 				}
       
   166 			}
       
   167 			if (element instanceof ISymbianSDK) {
       
   168 				return Messages.getString("BupProfileTreeViewer.2") + ((ISymbianSDK)element).getUniqueId(); //$NON-NLS-1$
       
   169 			}
       
   170 			return ""; //$NON-NLS-1$
       
   171 		}
       
   172 	}
       
   173 
       
   174 	/**
       
   175 	 * @param parent
       
   176 	 * @param style
       
   177 	 */
       
   178 	public BupProfileTreeViewer(Composite parent) {
       
   179 		super(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
       
   180 		setContentProvider(new BupProfileTreeContentProvider());
       
   181 		setLabelProvider(new BupProfileTreeLabelProvider());
       
   182 		final BupProfileTreeViewer viewerSelf = this;
       
   183 		this.getTree().addSelectionListener(new SelectionListener() {
       
   184 			public void widgetDefaultSelected(SelectionEvent arg0) {
       
   185 			}
       
   186 
       
   187 			public void widgetSelected(SelectionEvent arg0) {
       
   188 				Object data = arg0.item.getData();
       
   189 				if (!(data instanceof IBupEventMapProfile)) {
       
   190 					// we are a SWT.SINGLE, so we select last good selection canceling this one,
       
   191 					// it behaves like last valid selection stick 
       
   192 					viewerSelf.setSelection(lastValidSelection);
       
   193 				} else {
       
   194 					// remember last valid selection
       
   195 					lastValidSelection = viewerSelf.getSelection();
       
   196 				}
       
   197 			}
       
   198 		});
       
   199 	}
       
   200 
       
   201 	/**
       
   202 	 * Handy routine for reading selection
       
   203 	 * 
       
   204 	 * @return
       
   205 	 */
       
   206 	public IBupEventMapProfile getSelectedProfile() {
       
   207 		if (getSelection().isEmpty()) {
       
   208 			return null;
       
   209 		} else {
       
   210 			ISelection selection = getSelection();
       
   211 			if (selection instanceof ITreeSelection) {
       
   212 				Object firstElement = ((ITreeSelection)selection).getFirstElement();
       
   213 				if (firstElement instanceof IBupEventMapProfile) {
       
   214 					return (IBupEventMapProfile)firstElement;
       
   215 				}
       
   216 			}
       
   217 		}
       
   218 		return null;
       
   219 	}
       
   220 }