sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.button/src/com/nokia/carbide/cpp/internal/pi/button/ui/SwitchBupMapDialog.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
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.HashSet;
       
    22 import java.util.Set;
       
    23 
       
    24 import org.eclipse.jface.dialogs.IDialogConstants;
       
    25 import org.eclipse.jface.dialogs.TitleAreaDialog;
       
    26 import org.eclipse.jface.layout.GridDataFactory;
       
    27 import org.eclipse.jface.layout.GridLayoutFactory;
       
    28 import org.eclipse.jface.viewers.ISelectionChangedListener;
       
    29 import org.eclipse.jface.viewers.SelectionChangedEvent;
       
    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.Button;
       
    34 import org.eclipse.swt.widgets.Composite;
       
    35 import org.eclipse.swt.widgets.Control;
       
    36 import org.eclipse.swt.widgets.Label;
       
    37 import org.eclipse.swt.widgets.Shell;
       
    38 import org.eclipse.swt.widgets.Text;
       
    39 import org.eclipse.ui.PlatformUI;
       
    40 
       
    41 import com.nokia.carbide.cpp.pi.button.ComNokiaCarbidePiButtonHelpIDs;
       
    42 import com.nokia.carbide.cpp.pi.button.IBupEventMapProfile;
       
    43 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    44 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    45 import com.nokia.carbide.cpp.ui.CarbideUIPlugin;
       
    46 import com.nokia.carbide.cpp.ui.ICarbideSharedImages;
       
    47 
       
    48 
       
    49 public class SwitchBupMapDialog extends TitleAreaDialog {
       
    50 	private IBupEventMapProfile oldProfile;
       
    51 	private IBupEventMapProfile newProfile;
       
    52 	private boolean resetAll;
       
    53 	
       
    54 	// control
       
    55 	private Composite composite = null;
       
    56 	private BupProfileTreeViewer profileTreeViewer = null;
       
    57 	private Composite currentProfileComposite = null;
       
    58 	private Label currentProfileLabel = null;
       
    59 	private Text currenProfileText = null;
       
    60 	private Button resetAllKeysButton = null;
       
    61 	
       
    62 	public SwitchBupMapDialog (Shell parentShell, IBupEventMapProfile formerProfile) {
       
    63 		super(parentShell);
       
    64 		oldProfile = formerProfile;
       
    65 	}
       
    66 	
       
    67 	public Control createDialogArea(Composite parent) {
       
    68 		composite = new Composite(parent, SWT.NONE);
       
    69 		PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ComNokiaCarbidePiButtonHelpIDs.PI_KEY_MAP_SWITCH_DIALOG);
       
    70 		getShell().setText(Messages.getString("SwitchBupMapDialog.switchKeyPressProfile")); //$NON-NLS-1$
       
    71 		setTitleImage(CarbideUIPlugin.getSharedImages().getImageDescriptor(ICarbideSharedImages.IMG_PI_METER_20_20).createImage());
       
    72 		setTitle(Messages.getString("SwitchBupMapDialog.remapMessage")); //$NON-NLS-1$
       
    73 		
       
    74 		GridLayoutFactory layoutExpandBoth = GridLayoutFactory.fillDefaults();
       
    75 		GridDataFactory gridDataExpandBoth = GridDataFactory.fillDefaults().grab(true, true);
       
    76 		
       
    77 		layoutExpandBoth.applyTo(composite);
       
    78 		gridDataExpandBoth.applyTo(composite);
       
    79 		
       
    80 		profileTreeViewer = new BupProfileTreeViewer(composite);
       
    81 		gridDataExpandBoth.applyTo(profileTreeViewer.getTree());
       
    82 		profileTreeViewer.addPostSelectionChangedListener(new ISelectionChangedListener() {
       
    83 			public void selectionChanged(SelectionChangedEvent arg0) {
       
    84 				validatePage();
       
    85 			}
       
    86 		});
       
    87 		
       
    88 		currentProfileComposite = new Composite(composite, SWT.NONE);
       
    89 		layoutExpandBoth.copy().numColumns(2).applyTo(currentProfileComposite);
       
    90 		gridDataExpandBoth.applyTo(currentProfileComposite);
       
    91 		
       
    92 		currentProfileLabel = new Label(currentProfileComposite, SWT.NONE);
       
    93 		currentProfileLabel.setText(Messages.getString("SwitchBupMapDialog.currentProfile")); //$NON-NLS-1$
       
    94 		
       
    95 		currenProfileText = new Text(currentProfileComposite, SWT.READ_ONLY);
       
    96 		
       
    97 		resetAllKeysButton = new Button(composite, SWT.CHECK);
       
    98 		resetAllKeysButton.setText(Messages.getString("SwitchBupMapDialog.overwriteAllEventsWithDefault")); //$NON-NLS-1$
       
    99 		resetAllKeysButton.addSelectionListener(new SelectionListener() {
       
   100 
       
   101 			public void widgetDefaultSelected(SelectionEvent arg0) {
       
   102 			}
       
   103 
       
   104 			public void widgetSelected(SelectionEvent arg0) {
       
   105 				resetAll = resetAllKeysButton.getSelection();
       
   106 			}
       
   107 			
       
   108 		});
       
   109 		
       
   110 		buildBupMapProfileList();
       
   111 		resetAllKeysButton.setSelection(true);
       
   112 		resetAllKeysButton.setEnabled(false);
       
   113 		currenProfileText.setText(Messages.getString("SwitchBupMapDialog.none")); //$NON-NLS-1$
       
   114 		
       
   115 		if (oldProfile != null) {
       
   116 			resetAllKeysButton.setSelection(false);
       
   117 			resetAllKeysButton.setEnabled(true);
       
   118 			currenProfileText.setText(oldProfile.toString());
       
   119 		}
       
   120 		
       
   121 		return composite;
       
   122 	}
       
   123 
       
   124 	/**
       
   125 	 * 
       
   126 	 */
       
   127 	protected void validatePage() {
       
   128 		Button buttonSwitch = getButton(IDialogConstants.OK_ID);
       
   129 		if ((newProfile = profileTreeViewer.getSelectedProfile()) == null) {
       
   130 			buttonSwitch.setEnabled(false);
       
   131 		} else {
       
   132 			buttonSwitch.setEnabled(true);
       
   133 		}
       
   134 	}
       
   135 	
       
   136 	private void buildBupMapProfileList() {
       
   137 		Set<ISymbianSDK> sdks = new HashSet<ISymbianSDK>();
       
   138 		sdks.addAll(SDKCorePlugin.getSDKManager().getSDKList());
       
   139 		BupProfileTreeViewer.BupProfileTreeData data = profileTreeViewer.new BupProfileTreeData(null, sdks);
       
   140 		profileTreeViewer.setInput(data);
       
   141 		profileTreeViewer.expandAll();
       
   142 	}
       
   143 	
       
   144 	ArrayList<Integer> allId = new ArrayList<Integer>();
       
   145 	protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
       
   146 		Button result = super.createButton(parent, id, label, defaultButton);
       
   147 		if (id == IDialogConstants.OK_ID) {
       
   148 			result.setText(Messages.getString("SwitchBupMapDialog.switchToNewProfile")); //$NON-NLS-1$
       
   149 		}
       
   150 		allId.add(id);
       
   151 		return result;
       
   152 	}
       
   153 	
       
   154 	protected void createButtonsForButtonBar(Composite parent) {
       
   155 		super.createButtonsForButtonBar(parent);
       
   156 		GridDataFactory gridDataButton = GridDataFactory.swtDefaults();
       
   157 		int maxWidth = 0;
       
   158 		for (Integer id : allId) {
       
   159 			Button button = getButton(id);
       
   160 			maxWidth = Math.max(maxWidth, button.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
       
   161 		}
       
   162 		for (Integer id : allId) {
       
   163 			Button button = getButton(id);
       
   164 			gridDataButton.hint(maxWidth, button.computeSize(SWT.DEFAULT, SWT.DEFAULT).y).applyTo(button);
       
   165 		}
       
   166 		// now buttons are there, we can  disable key
       
   167 		getButton(IDialogConstants.OK_ID).setEnabled(false);
       
   168 	}
       
   169 
       
   170 	public boolean resetAll() {
       
   171 		return resetAll;
       
   172 	}
       
   173 	
       
   174 	public IBupEventMapProfile getNewProfile() {
       
   175 		return newProfile;
       
   176 	}
       
   177 }