sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi/src/com/nokia/carbide/cpp/internal/pi/visual/GraphComposite.java
changeset 5 844b047e260d
parent 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
4:615035072f7e 5:844b047e260d
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 package com.nokia.carbide.cpp.internal.pi.visual;
    18 package com.nokia.carbide.cpp.internal.pi.visual;
    19 
    19 
       
    20 import java.net.URL;
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.core.runtime.FileLocator;
       
    25 import org.eclipse.core.runtime.Path;
       
    26 import org.eclipse.core.runtime.Platform;
    20 import org.eclipse.draw2d.FigureCanvas;
    27 import org.eclipse.draw2d.FigureCanvas;
       
    28 import org.eclipse.jface.action.Action;
       
    29 import org.eclipse.jface.resource.ImageDescriptor;
    21 import org.eclipse.swt.SWT;
    30 import org.eclipse.swt.SWT;
       
    31 import org.eclipse.swt.events.ControlEvent;
       
    32 import org.eclipse.swt.events.ControlListener;
       
    33 import org.eclipse.swt.events.SelectionEvent;
       
    34 import org.eclipse.swt.events.SelectionListener;
       
    35 import org.eclipse.swt.graphics.Color;
       
    36 import org.eclipse.swt.graphics.Image;
    22 import org.eclipse.swt.layout.FormAttachment;
    37 import org.eclipse.swt.layout.FormAttachment;
    23 import org.eclipse.swt.layout.FormData;
    38 import org.eclipse.swt.layout.FormData;
    24 import org.eclipse.swt.layout.FormLayout;
    39 import org.eclipse.swt.layout.FormLayout;
       
    40 import org.eclipse.swt.layout.GridData;
       
    41 import org.eclipse.swt.layout.GridLayout;
       
    42 import org.eclipse.swt.widgets.Button;
       
    43 import org.eclipse.swt.widgets.Combo;
    25 import org.eclipse.swt.widgets.Composite;
    44 import org.eclipse.swt.widgets.Composite;
       
    45 import org.eclipse.swt.widgets.Control;
       
    46 import org.eclipse.swt.widgets.Display;
    26 import org.eclipse.swt.widgets.Label;
    47 import org.eclipse.swt.widgets.Label;
    27 
    48 import org.eclipse.ui.PlatformUI;
       
    49 import org.osgi.framework.Bundle;
       
    50 
       
    51 import com.nokia.carbide.cpp.internal.pi.plugin.model.ITitleBarMenu;
       
    52 import com.nokia.carbide.cpp.internal.pi.visual.PICompositePanel.GraphComponentWrapper;
       
    53 import com.nokia.carbide.cpp.pi.PiPlugin;
       
    54 import com.nokia.carbide.cpp.pi.editors.Messages;
    28 import com.nokia.carbide.cpp.pi.editors.PIPageEditor;
    55 import com.nokia.carbide.cpp.pi.editors.PIPageEditor;
       
    56 import com.nokia.carbide.cpp.pi.visual.IGenericTraceGraph;
       
    57 import com.nokia.carbide.cpp.pi.visual.IGraphChangeListener;
    29 
    58 
    30 /**
    59 /**
    31  *
    60  * 
    32  * A GraphComposite is added to the SashForm for a tab.
    61  * A GraphComposite is added to the SashForm for a tab.
    33  * 
    62  * 
    34  * A GraphComposite has an option centered title, a left area to contain the y-axis
    63  * A GraphComposite has an option centered title, a left area to contain the
    35  * legend, and a scrollable area containing the graph and the x-axis legend.
    64  * y-axis legend, and a scrollable area containing the graph and the x-axis
       
    65  * legend.
    36  */
    66  */
    37 public class GraphComposite extends Composite
    67 public class GraphComposite extends Composite implements SelectionListener,
    38 {
    68 		ControlListener, IGraphChangeListener {
       
    69 
       
    70 	// Strings for tooltips
       
    71 	private static final String TOOLTIP_MAXIMIZE = "Maximize graph";
       
    72 	private static final String TOOLTIP_MAXIMIZE_RESTORE = "Restore graph";
       
    73 	private static final String TOOLTIP_MINIMIZE = "Minimize graph";
       
    74 	private static final String TOOLTIP_MINIMIZE_RESTORE = "Restore graph";
       
    75 	private static final String TOOLTIP_MOVE_UP = "Move graph up";
       
    76 	private static final String TOOLTIP_MOVE_DOWN = "Move graph down";
       
    77 
       
    78 	// size of the titlebar
       
    79 	private static final int TITLESIZE = 20;
       
    80 
       
    81 	private static final int TOTAL_SASH_WEIGHT = 1000;
       
    82 
       
    83 	// UI components
    39 	public FigureCanvas leftLegend;
    84 	public FigureCanvas leftLegend;
    40 	public FigureCanvas figureCanvas;
    85 	public FigureCanvas figureCanvas;
    41 
    86 	private PICompositePanel compositePanel;
    42 	public GraphComposite(Composite parent, int style, String titleString)
    87 	private Button buttonMinimize = null;
    43 	{
    88 	private Button buttonMaximize = null;
       
    89 	private Label labelTitle = null;
       
    90 	private Button buttonMoveGraphUp = null;
       
    91 	private Button buttonMoveGraphDown = null;
       
    92 	private Composite compositeTitleBar = null;
       
    93 	private Combo comboGraphType = null;
       
    94 	private Button buttonHelp;
       
    95 
       
    96 	// Images for title buttons
       
    97 	private Image imageMoveGraphUp = null;
       
    98 	private Image imageMoveGraphDown = null;
       
    99 	private Image imageMinimizeGraph = null;
       
   100 	private Image imageMinimizeRestoreGraph = null;
       
   101 	private Image imageMaximizeGraph = null;
       
   102 	private Image imageMaximizeRestoreGraph = null;
       
   103 
       
   104 	// PreviousWight variable that is used for saving graph's size when
       
   105 	// minimizing/restoring
       
   106 	private double previousWeight = 0.0;
       
   107 
       
   108 	// State of graph(minimized/maximized)
       
   109 	private boolean isMaximized = false;
       
   110 	private boolean isMinimized = false;
       
   111 
       
   112 	// Is view initialized completely
       
   113 	private boolean isViewInitialized = false;
       
   114 
       
   115 	// Title Bar actions
       
   116 	private Action[] titleBarActions = null;
       
   117 	private String contextHelpId;
       
   118 
       
   119 	private GraphComponentWrapper graphComponent;
       
   120 
       
   121 	/**
       
   122 	 * Constructor
       
   123 	 * 
       
   124 	 * @param parent
       
   125 	 *            Parent where UI components are placed
       
   126 	 * @param style
       
   127 	 *            that is used
       
   128 	 * @param titleString
       
   129 	 *            String for titlebar
       
   130 	 * @param compositePanel
       
   131 	 *            panel that is using this component
       
   132 	 */
       
   133 	public GraphComposite(Composite parent, int style, String titleString,
       
   134 			final PICompositePanel compositePanel, ITitleBarMenu titleBar, 
       
   135 			GraphComponentWrapper graphComponent) {
    44 		super(parent, style);
   136 		super(parent, style);
    45 		this.setLayout(new FormLayout());
   137 		this.setLayout(new FormLayout());
    46 
   138 		this.compositePanel = compositePanel;
    47        	FormData formData;
   139 		this.graphComponent = graphComponent;
    48     	Label title = null;
   140 
    49 
   141 		// Create TitleBar if needed
    50        	if (titleString != null) {
   142 		if (titleString != null) {
    51 			title = new Label(this, SWT.CENTER);
   143 			createTitleBarUIComponents(parent, titleString, titleBar);
    52 			title.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_CYAN));
   144 		}
    53 			title.setFont(PIPageEditor.helvetica_8);
   145 
    54 			title.setText(titleString);
   146 		// Create left legend and canvas where graph is drawn
    55 
   147 		leftLegend = new FigureCanvas(this);
    56 			formData = new FormData();
   148 		figureCanvas = new FigureCanvas(this);
    57     		formData.top    = new FormAttachment(0);
   149 
    58     		formData.left   = new FormAttachment(0);
   150 		// Set layouts for canvases and title bar
    59     		formData.right  = new FormAttachment(100);
   151 		FormData formData;
    60     		title.setLayoutData(formData);
       
    61 		}
       
    62 
       
    63 		leftLegend   = new FigureCanvas(this);
       
    64        	figureCanvas = new FigureCanvas(this);
       
    65 
       
    66 		formData = new FormData();
   152 		formData = new FormData();
    67 		if (titleString != null)
   153 		if (compositeTitleBar != null) {
    68     		formData.top = new FormAttachment(title, 0, SWT.BOTTOM);
   154 			formData.top = new FormAttachment(compositeTitleBar, 0, SWT.BOTTOM);
    69 		else
   155 
       
   156 		} else {
    70 			formData.top = new FormAttachment(0);
   157 			formData.top = new FormAttachment(0);
       
   158 		}
    71 		formData.bottom = new FormAttachment(100);
   159 		formData.bottom = new FormAttachment(100);
    72 		formData.left   = new FormAttachment(0);
   160 		formData.left = new FormAttachment(0);
    73 		formData.width  = GenericTraceGraph.yLegendWidth;
   161 		formData.width = IGenericTraceGraph.Y_LEGEND_WIDTH;
    74 		leftLegend.setLayoutData(formData);
   162 		leftLegend.setLayoutData(formData);
    75 
   163 
    76 		formData = new FormData();
   164 		formData = new FormData();
    77 		if (titleString != null)
   165 		if (compositeTitleBar != null) {
    78     		formData.top = new FormAttachment(title, 0, SWT.BOTTOM);
   166 			formData.top = new FormAttachment(compositeTitleBar, 0, SWT.BOTTOM);
    79 		else
   167 
       
   168 		} else {
    80 			formData.top = new FormAttachment(0);
   169 			formData.top = new FormAttachment(0);
       
   170 		}
    81 		formData.bottom = new FormAttachment(100);
   171 		formData.bottom = new FormAttachment(100);
    82 		formData.left   = new FormAttachment(leftLegend, 0, SWT.RIGHT);
   172 		formData.left = new FormAttachment(leftLegend, 0, SWT.RIGHT);
    83 		formData.right  = new FormAttachment(100);
   173 		formData.right = new FormAttachment(100);
    84 		figureCanvas.setLayoutData(formData);
   174 		figureCanvas.setLayoutData(formData);
    85 	}
   175 
       
   176 		// Enable/disable needed UI Components
       
   177 		updateTitleBarButtonsStatus();
       
   178 
       
   179 		// add listener for resizing the view
       
   180 		figureCanvas.addControlListener(this);
       
   181 
       
   182 	}
       
   183 
       
   184 	/**
       
   185 	 * @return returns index if this graph in sashform
       
   186 	 * @throws IllegalStateException
       
   187 	 *             if component not found in sashform
       
   188 	 */
       
   189 	private int getIndexOfThisComponentInSashForm() {
       
   190 
       
   191 		Control[] array = compositePanel.getSashForm().getChildren();
       
   192 		for (int i = 0; i < array.length; i++) {
       
   193 			if (array[i].getClass() == this.getClass()) {
       
   194 				GraphComposite composite = (GraphComposite) array[i];
       
   195 				if (composite.equals(this)) {
       
   196 					return i;
       
   197 				}
       
   198 			}
       
   199 		}
       
   200 
       
   201 		// if not found return error value
       
   202 		throw new IllegalStateException();
       
   203 
       
   204 	}
       
   205 
       
   206 	/**
       
   207 	 * @return returns control above this component in the sashform
       
   208 	 */
       
   209 	private Control getFormAbove() {
       
   210 		GraphComposite graphAbove = null;
       
   211 		GraphComposite[] graphs = getVisibleGraphs();
       
   212 
       
   213 		for (int i = 0; i < graphs.length; i++) {
       
   214 			if (graphs[i] == this) {
       
   215 				if (i > 0) {
       
   216 					graphAbove = graphs[i - 1];
       
   217 				}
       
   218 				break;
       
   219 			}
       
   220 		}
       
   221 		return graphAbove;
       
   222 	}
       
   223 
       
   224 	/**
       
   225 	 * @return returns control below this component in the sashform
       
   226 	 */
       
   227 	private Control getFormBelow() {
       
   228 		GraphComposite graphBelow = null;
       
   229 		GraphComposite[] graphs = getVisibleGraphs();
       
   230 
       
   231 		for (int i = 0; i < graphs.length; i++) {
       
   232 			if (graphs[i] == this) {
       
   233 				if (i != graphs.length - 1) {
       
   234 					graphBelow = graphs[i + 1];
       
   235 				}
       
   236 				break;
       
   237 			}
       
   238 		}
       
   239 		return graphBelow;
       
   240 	}
       
   241 
       
   242 	/**
       
   243 	 * Updates titlebar buttons status. Updates images and tooltips and
       
   244 	 * enables/disables needed component according to status
       
   245 	 * (minimized/maximized) and place of this graph.
       
   246 	 */
       
   247 	public void updateTitleBarButtonsStatus() {
       
   248 
       
   249 		// Ensure that buttons are created.
       
   250 		if (buttonMinimize == null) {
       
   251 			return;
       
   252 		}
       
   253 
       
   254 		// If graph is maximized, only maximize button needs to be updated
       
   255 		if (this.isMaximized) {
       
   256 			buttonMaximize.setImage(imageMaximizeRestoreGraph);
       
   257 			buttonMaximize.setToolTipText(TOOLTIP_MAXIMIZE_RESTORE);
       
   258 
       
   259 			// Disable move and minimize buttons if maximized
       
   260 			buttonMoveGraphUp.setEnabled(false);
       
   261 			buttonMoveGraphDown.setEnabled(false);
       
   262 			buttonMinimize.setEnabled(false);
       
   263 
       
   264 		} else {
       
   265 
       
   266 			// Minimize/restore button
       
   267 			if (this.isMinimized) {
       
   268 				buttonMinimize.setImage(imageMinimizeRestoreGraph);
       
   269 				buttonMinimize.setToolTipText(TOOLTIP_MINIMIZE_RESTORE);
       
   270 				buttonMaximize.setEnabled(false);
       
   271 			} else {
       
   272 				buttonMinimize.setImage(imageMinimizeGraph);
       
   273 				buttonMinimize.setToolTipText(TOOLTIP_MINIMIZE);
       
   274 				buttonMaximize.setEnabled(true);
       
   275 			}
       
   276 
       
   277 			buttonMaximize.setImage(imageMaximizeGraph);
       
   278 			buttonMaximize.setToolTipText(TOOLTIP_MAXIMIZE);
       
   279 
       
   280 			GraphComposite[] children = getVisibleGraphs();
       
   281 			int childCnt = children.length;
       
   282 
       
   283 			// Enable minimize button if more than one graph
       
   284 			buttonMinimize.setEnabled(childCnt > 1);
       
   285 			buttonMaximize.setEnabled(childCnt > 1);
       
   286 
       
   287 			int index = -1;
       
   288 			for (int i = 0; i < childCnt; i++) {
       
   289 				if (children[i] == this) {
       
   290 					index = i;
       
   291 				}
       
   292 			}
       
   293 			// if index number is illegal do nothing.
       
   294 			if (index < 0) {
       
   295 				return;
       
   296 			}
       
   297 			// disable up arrow if already on the top of the form or only one
       
   298 			// graph exists on page
       
   299 			buttonMoveGraphUp.setEnabled(index > 0 && childCnt > 1);
       
   300 			// disable down arrow if already on the bottom of the form
       
   301 			buttonMoveGraphDown
       
   302 					.setEnabled(index < childCnt - 1 && childCnt > 1);
       
   303 		}
       
   304 
       
   305 		compositePanel.getSashForm().layout();
       
   306 
       
   307 	}
       
   308 
       
   309 	/**
       
   310 	 * Updates all graphs titlebar buttons status if graph is visible
       
   311 	 */
       
   312 	public void updateAllGraphsButtons() {
       
   313 		if(!this.isDisposed() && this.isVisible()){
       
   314 			GraphComposite[] controlArray = getVisibleGraphs();
       
   315 			for (GraphComposite item : controlArray) {
       
   316 				GraphComposite graph = (GraphComposite) item;
       
   317 				graph.updateTitleBarButtonsStatus();
       
   318 			}
       
   319 		}
       
   320 	}
       
   321 
       
   322 	/**
       
   323 	 * Initializes images for titlebar buttons
       
   324 	 */
       
   325 	private void createImagesForTitleBar() {
       
   326 
       
   327 		// Get budle's location in file system
       
   328 		URL url;
       
   329 		ImageDescriptor createFromURL;
       
   330 		Bundle piBundle = Platform.getBundle("com.nokia.carbide.cpp.pi"); //$NON-NLS-1$
       
   331 		if (piBundle == null)
       
   332 			return;
       
   333 
       
   334 		// Maximize
       
   335 		url = FileLocator.find(piBundle, new Path(Messages
       
   336 				.getString("PIPageEditorContributor.maximizeGraphIcon")), null); //$NON-NLS-1$
       
   337 		if (url != null) {
       
   338 			createFromURL = ImageDescriptor.createFromURL(url);
       
   339 			imageMaximizeGraph = createFromURL.createImage();
       
   340 			buttonMaximize.setImage(imageMaximizeGraph);
       
   341 
       
   342 		}
       
   343 
       
   344 		// Maximize restore
       
   345 		url = FileLocator
       
   346 				.find(
       
   347 						piBundle,
       
   348 						new Path(
       
   349 								Messages
       
   350 										.getString("PIPageEditorContributor.maximizeRestoreGraphIcon")), null); //$NON-NLS-1$
       
   351 		if (url != null) {
       
   352 			createFromURL = ImageDescriptor.createFromURL(url);
       
   353 			imageMaximizeRestoreGraph = createFromURL.createImage();
       
   354 
       
   355 		}
       
   356 
       
   357 		// Minimize
       
   358 		url = FileLocator.find(piBundle, new Path(Messages
       
   359 				.getString("PIPageEditorContributor.minimizeGraphIcon")), null); //$NON-NLS-1$
       
   360 		if (url != null) {
       
   361 			createFromURL = ImageDescriptor.createFromURL(url);
       
   362 			imageMinimizeGraph = createFromURL.createImage();
       
   363 			buttonMinimize.setImage(imageMinimizeGraph);
       
   364 		}
       
   365 
       
   366 		// Minimize restore
       
   367 		url = FileLocator
       
   368 				.find(
       
   369 						piBundle,
       
   370 						new Path(
       
   371 								Messages
       
   372 										.getString("PIPageEditorContributor.minimizeRestoreGraphIcon")), null); //$NON-NLS-1$
       
   373 		if (url != null) {
       
   374 			createFromURL = ImageDescriptor.createFromURL(url);
       
   375 			imageMinimizeRestoreGraph = createFromURL.createImage();
       
   376 
       
   377 		}
       
   378 
       
   379 		// Move down
       
   380 		url = FileLocator.find(piBundle, new Path(Messages
       
   381 				.getString("PIPageEditorContributor.moveGraphDown")), null); //$NON-NLS-1$
       
   382 		if (url != null) {
       
   383 			createFromURL = ImageDescriptor.createFromURL(url);
       
   384 			imageMoveGraphDown = createFromURL.createImage();
       
   385 			buttonMoveGraphDown.setImage(imageMoveGraphDown);
       
   386 
       
   387 		}
       
   388 
       
   389 		// Move Up
       
   390 		url = FileLocator.find(piBundle, new Path(Messages
       
   391 				.getString("PIPageEditorContributor.moveGraphUp")), null); //$NON-NLS-1$
       
   392 		if (url != null) {
       
   393 			createFromURL = ImageDescriptor.createFromURL(url);
       
   394 			imageMoveGraphUp = createFromURL.createImage();
       
   395 			buttonMoveGraphUp.setImage(imageMoveGraphUp);
       
   396 		}
       
   397 		
       
   398 	}
       
   399 
       
   400 	/**
       
   401 	 * Creates ui components located int titlebar
       
   402 	 * 
       
   403 	 * @param parent
       
   404 	 *            parent component where components are placed
       
   405 	 * @param titleString
       
   406 	 *            title text
       
   407 	 * @param titleBar object implementing ITitleBarMenu (actions and help for title bar)
       
   408 	 */
       
   409 	private void createTitleBarUIComponents(Composite parent,
       
   410 			String titleString, ITitleBarMenu titleBar) {
       
   411 
       
   412 		FormData formData;
       
   413 
       
   414 		// Create composite where all components are placed
       
   415 		compositeTitleBar = new Composite(this, SWT.NONE);
       
   416 		GridLayout layoutComposite = new GridLayout();
       
   417 		layoutComposite.numColumns = titleBar != null && titleBar.getContextHelpId() != null ? 7 : 6;
       
   418 		layoutComposite.marginWidth = 0;
       
   419 		layoutComposite.marginHeight = 0;
       
   420 		layoutComposite.horizontalSpacing = 1;
       
   421 		compositeTitleBar.setLayout(layoutComposite);
       
   422 
       
   423 		// Minimize/restore button
       
   424 		buttonMinimize = new Button(compositeTitleBar, SWT.PUSH);
       
   425 		buttonMinimize.setFont(PIPageEditor.helvetica_7);
       
   426 		buttonMinimize.setLayoutData(new GridData(GridData.FILL_VERTICAL));
       
   427 
       
   428 		// Create drop-down list if action array is not null
       
   429 		if (titleBar != null && titleBar.addTitleBarMenuItems() != null) {
       
   430 			// graph type Combo box
       
   431 			comboGraphType = new Combo(compositeTitleBar, SWT.READ_ONLY);
       
   432 			GridData gridData = new GridData(GridData.FILL_VERTICAL);
       
   433 			comboGraphType.setLayoutData(gridData);
       
   434 			this.titleBarActions = titleBar.addTitleBarMenuItems();
       
   435 			int checkedItemIndex = 0;
       
   436 			for (int i = 0; i < titleBarActions.length; i++) {
       
   437 				comboGraphType.add(titleBarActions[i].getText());
       
   438 				if (titleBarActions[i].isChecked()) {
       
   439 					checkedItemIndex = i;
       
   440 				}
       
   441 			}
       
   442 			comboGraphType.select(checkedItemIndex);
       
   443 			comboGraphType.setFont(PIPageEditor.helvetica_7);
       
   444 		}
       
   445 		// title label
       
   446 		labelTitle = new Label(compositeTitleBar, SWT.CENTER);
       
   447 		labelTitle.setFont(PIPageEditor.helvetica_8);
       
   448 		labelTitle.setText(titleString);
       
   449 
       
   450 		if (titleBar != null && titleBar.getContextHelpId() != null){
       
   451 			this.contextHelpId = titleBar.getContextHelpId();
       
   452 			PlatformUI.getWorkbench().getHelpSystem().setHelp(this, this.contextHelpId);
       
   453 			
       
   454 			buttonHelp = new Button(compositeTitleBar, SWT.PUSH);
       
   455 			buttonHelp.setToolTipText("Help");
       
   456 			buttonHelp.setLayoutData(new GridData(GridData.FILL_VERTICAL));
       
   457 			Image helpImage = PiPlugin.getImage("icons/linkto_help.gif"); //$NON-NLS-1$
       
   458 			if (helpImage != null){
       
   459 				buttonHelp.setImage(helpImage);
       
   460 			}			
       
   461 		}
       
   462 
       
   463 		// move down button
       
   464 		buttonMoveGraphDown = new Button(compositeTitleBar, SWT.PUSH);
       
   465 		buttonMoveGraphDown.setToolTipText(TOOLTIP_MOVE_DOWN);
       
   466 		buttonMoveGraphDown.setLayoutData(new GridData(GridData.FILL_VERTICAL));
       
   467 
       
   468 		// mode graph up
       
   469 		buttonMoveGraphUp = new Button(compositeTitleBar, SWT.PUSH);
       
   470 		buttonMoveGraphUp.setToolTipText(TOOLTIP_MOVE_UP);
       
   471 		buttonMoveGraphUp.setLayoutData(new GridData(GridData.FILL_VERTICAL));
       
   472 
       
   473 		// maximize/restore button
       
   474 		buttonMaximize = new Button(compositeTitleBar, SWT.PUSH);
       
   475 		buttonMaximize.setFont(PIPageEditor.helvetica_7);
       
   476 		buttonMaximize.setLayoutData(new GridData(GridData.FILL_VERTICAL));
       
   477 
       
   478 		createImagesForTitleBar();
       
   479 
       
   480 		// calculate what intent should be used so that title label is in the
       
   481 		// center of the view
       
   482 		int indent = 0;
       
   483 
       
   484 		// increase intent value by all components that are left from the title
       
   485 
       
   486 		// get size of the combobox
       
   487 		if (comboGraphType != null) {
       
   488 			indent -= comboGraphType.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
       
   489 		}
       
   490 		indent -= buttonMinimize.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
       
   491 
       
   492 		// decrease intent value by all components that are right from the title
       
   493 		if (buttonHelp != null){
       
   494 			indent += buttonHelp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;			
       
   495 		}
       
   496 		indent += buttonMaximize.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
       
   497 		indent += buttonMoveGraphDown.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
       
   498 		indent += buttonMoveGraphDown.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
       
   499 
       
   500 		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
       
   501 		gridData.horizontalIndent = indent;
       
   502 		labelTitle.setLayoutData(gridData);
       
   503 
       
   504 		// Set layouts for composite
       
   505 		formData = new FormData();
       
   506 		formData.top = new FormAttachment(0);
       
   507 		formData.left = new FormAttachment(0);
       
   508 		formData.right = new FormAttachment(100);
       
   509 		formData.height = TITLESIZE;
       
   510 		compositeTitleBar.setLayoutData(formData);
       
   511 
       
   512 		// Set background color for all components
       
   513 		Color BgColor = parent.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
       
   514 		compositeTitleBar.setBackground(BgColor);
       
   515 		buttonMinimize.setBackground(BgColor);
       
   516 		/*
       
   517 		 * if (comboGraphType != null) { comboGraphType.setBackground(BgColor);
       
   518 		 * }
       
   519 		 */
       
   520 		labelTitle.setBackground(BgColor);
       
   521 		buttonMoveGraphUp.setBackground(BgColor);
       
   522 		buttonMoveGraphDown.setBackground(BgColor);
       
   523 		buttonMaximize.setBackground(BgColor);
       
   524 
       
   525 		// Add selection listeners for buttons
       
   526 		buttonMoveGraphDown.addSelectionListener(this);
       
   527 		buttonMoveGraphUp.addSelectionListener(this);
       
   528 		buttonMaximize.addSelectionListener(this);
       
   529 		buttonMinimize.addSelectionListener(this);
       
   530 		if (comboGraphType != null) {
       
   531 			comboGraphType.addSelectionListener(this);
       
   532 		}
       
   533 		if (buttonHelp != null){
       
   534 			buttonHelp.addSelectionListener(this);
       
   535 		}
       
   536 		
       
   537 
       
   538 	}
       
   539 
       
   540 	/*
       
   541 	 * (non-Javadoc)
       
   542 	 * 
       
   543 	 * @see
       
   544 	 * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse
       
   545 	 * .swt.events.SelectionEvent)
       
   546 	 */
       
   547 	public void widgetDefaultSelected(SelectionEvent e) {
       
   548 		// No actions needed here
       
   549 	}
       
   550 
       
   551 	/*
       
   552 	 * (non-Javadoc)
       
   553 	 * 
       
   554 	 * @see
       
   555 	 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
       
   556 	 * .events.SelectionEvent)
       
   557 	 */
       
   558 	public void widgetSelected(SelectionEvent e) {
       
   559 
       
   560 		if (e.widget == comboGraphType) {
       
   561 			if (titleBarActions == null) {
       
   562 				return;
       
   563 			}
       
   564 			titleBarActions[comboGraphType.getSelectionIndex()].run();
       
   565 		}
       
   566 
       
   567 		else {
       
   568 			if (e.widget == buttonMoveGraphDown) {
       
   569 				this.moveGraphDown();
       
   570 			} else if (e.widget == buttonMoveGraphUp) {
       
   571 				this.moveGraphUp();
       
   572 			} else if (e.widget == buttonMinimize) {
       
   573 				this.minimizeOrRestoreGraph();
       
   574 			} else if (e.widget == buttonMaximize) {
       
   575 				if (this.isMinimized) {
       
   576 					this.minimizeOrRestoreGraph();
       
   577 				}
       
   578 				this.maximizeOrRestoreGraph();
       
   579 			} else if (e.widget == buttonHelp){
       
   580 				PlatformUI.getWorkbench().getHelpSystem().displayHelp(this.contextHelpId);
       
   581 			}
       
   582 			this.updateAllGraphsButtons();
       
   583 			compositePanel.getSashForm().layout();
       
   584 		}
       
   585 	}
       
   586 
       
   587 	/**
       
   588 	 * moves this graph down in the form
       
   589 	 */
       
   590 	private void moveGraphDown() {
       
   591 		Control control = getFormBelow();
       
   592 		if (control != null) {
       
   593 			moveBelow(control);
       
   594 		}
       
   595 	}
       
   596 
       
   597 	/**
       
   598 	 * moves this graph up in the form
       
   599 	 */
       
   600 	private void moveGraphUp() {
       
   601 		Control control = getFormAbove();
       
   602 		if (control != null) {
       
   603 			moveAbove(control);
       
   604 		}
       
   605 	}
       
   606 
       
   607 	/**
       
   608 	 * minimizes or maximizes graph according to its current state
       
   609 	 */
       
   610 	private void minimizeOrRestoreGraph() {
       
   611 
       
   612 		// Since minimizing and restoring forms in SashForm is not very
       
   613 		// handy this algorithm is pretty complex...
       
   614 
       
   615 		int graphIndex = getIndexOfThisComponentInSashForm();
       
   616 
       
   617 		// Set none of the forms in SashForm maximised
       
   618 		this.compositePanel.getSashForm().setMaximizedControl(null);
       
   619 
       
   620 		int[] weights = getWeightOfGraphs(true, false);
       
   621 
       
   622 		// if index not found or it is greater than size of sash
       
   623 		// do nothing(this should not happen)
       
   624 		if (graphIndex < 0 || graphIndex >= weights.length) {
       
   625 			return;
       
   626 		}
       
   627 
       
   628 		// count sum of weight values in array
       
   629 		int weightSum = 0;
       
   630 		for (int item : weights) {
       
   631 			weightSum += item;
       
   632 		}
       
   633 
       
   634 		// get other(other than the one minimized/restored) graphs
       
   635 		// combined weight
       
   636 		double otherGraphsWeight = weightSum - weights[graphIndex];
       
   637 
       
   638 		int restoredGraphSize = 0;
       
   639 
       
   640 		if (this.isMinimized && weights[graphIndex] < 100) { // Restore graph
       
   641 
       
   642 			this.isMinimized = false;
       
   643 			// calculate restored size based on graphs previous size
       
   644 			double multiplier = (double) previousWeight * 2;
       
   645 			restoredGraphSize = (int) (multiplier * otherGraphsWeight);
       
   646 			weights[graphIndex] = restoredGraphSize;
       
   647 			graphComponent.graphComponent.graphVisibilityChanged(true);
       
   648 
       
   649 			// update legend counts in case selection area has changed.
       
   650 			graphComponent.graphComponent.updateSelectionArea(PIPageEditor
       
   651 					.currentPageEditor().getStartTime(), PIPageEditor
       
   652 					.currentPageEditor().getEndTime());
       
   653 
       
   654 		} else { // minimize
       
   655 			this.isMinimized = true;
       
   656 			graphComponent.graphComponent.graphVisibilityChanged(false);
       
   657 
       
   658 		}
       
   659 		compositePanel.getSashForm().layout();// after revealing or hiding
       
   660 												// legend tables
       
   661 		compositePanel.getVisualiser().getBottomComposite().layout();
       
   662 		// After graph is restored or marked as minimized, we need calculate
       
   663 		// size of minimized graphs again.
       
   664 
       
   665 		// Add restored graphs size to othergraphsSize
       
   666 		otherGraphsWeight += restoredGraphSize;
       
   667 
       
   668 		Control[] controlArray = compositePanel.getSashForm().getChildren();
       
   669 
       
   670 		// calculate percent value of title size(the size which is used for
       
   671 		// minimizing)
       
   672 		int xSize = compositePanel.getSashForm().getSize().y;
       
   673 		double percentValue = (double) TITLESIZE / (double) xSize;
       
   674 		previousWeight = (double) weights[graphIndex] / (double) weightSum;
       
   675 		int minimizedSize = (int) ((double) otherGraphsWeight * percentValue);
       
   676 
       
   677 		// Go through array of form items and set all minimized forms size to
       
   678 		// calculated minimizedSize
       
   679 		for (int i = 0; i < controlArray.length; i++) {
       
   680 			if (controlArray[i].getClass() == this.getClass()) {
       
   681 				GraphComposite graph = (GraphComposite) controlArray[i];
       
   682 				if (graph.isMinimized) {
       
   683 					weights[i] = minimizedSize;
       
   684 				}
       
   685 			}
       
   686 		}
       
   687 
       
   688 		// set calculated weights to sashform
       
   689 		try {
       
   690 			compositePanel.getSashForm().setWeights(weights);
       
   691 		} catch (Exception e2) {
       
   692 			// do nothing
       
   693 		}
       
   694 
       
   695 	}
       
   696 
       
   697 	/**
       
   698 	 * Returns the weight of the children controls.
       
   699 	 * 
       
   700 	 * @param zerohidden
       
   701 	 *            : if true, zero out values for currently hidden controls in
       
   702 	 *            return array
       
   703 	 * @param onlyHidden
       
   704 	 *            : if true, only return weights of currently hidden controls
       
   705 	 *            (all others zeroed out)
       
   706 	 * @return
       
   707 	 */
       
   708 	private int[] getWeightOfGraphs(boolean zeroHidden, boolean onlyHidden) {
       
   709 		int[] weights = this.compositePanel.getSashForm().getWeights();
       
   710 
       
   711 		// ignore the weight of currently hidden controls
       
   712 		Control[] children = this.compositePanel.getSashForm().getChildren();
       
   713 		int j = 0;
       
   714 		for (int i = 0; i < weights.length; i++) {
       
   715 			while (children[j].getClass() != this.getClass()) {
       
   716 				j++;// skip any children that are not graphs
       
   717 			}
       
   718 			if (children[j].isVisible() && onlyHidden
       
   719 					|| !children[j].isVisible() && zeroHidden) {
       
   720 				weights[i] = 0;
       
   721 			}
       
   722 			j++;
       
   723 		}
       
   724 		return weights;
       
   725 	}
       
   726 
       
   727 	/**
       
   728 	 * @return GraphComposite[] of all currently visible graphs on the current
       
   729 	 *         page.
       
   730 	 */
       
   731 	private GraphComposite[] getVisibleGraphs() {
       
   732 		List<GraphComposite> graphs = new ArrayList<GraphComposite>();
       
   733 		for (Control control : this.compositePanel.getSashForm().getChildren()) {
       
   734 			if (control.getClass() == this.getClass() && control.isVisible()) {
       
   735 				graphs.add((GraphComposite) control);
       
   736 			}
       
   737 		}
       
   738 		return graphs.toArray(new GraphComposite[graphs.size()]);
       
   739 	}
       
   740 
       
   741 	/**
       
   742 	 * Maximizes or restores graph according to its current state
       
   743 	 */
       
   744 	private void maximizeOrRestoreGraph() {
       
   745 		if (compositePanel.getSashForm().getMaximizedControl() != this) {
       
   746 			isMaximized = true;
       
   747 			compositePanel.getSashForm().setMaximizedControl(this);
       
   748 			graphComponent.graphComponent.graphMaximized(true);
       
   749 			if (isMinimized) {
       
   750 				graphComponent.graphComponent.graphVisibilityChanged(true);
       
   751 			}
       
   752 
       
   753 		} else {
       
   754 			isMaximized = false;
       
   755 			compositePanel.getSashForm().setMaximizedControl(null);
       
   756 			graphComponent.graphComponent.graphMaximized(false);
       
   757 			if (isMinimized) {
       
   758 				graphComponent.graphComponent.graphVisibilityChanged(false);
       
   759 			}
       
   760 			
       
   761 			// update selection area to all visible graphs
       
   762 			for (GraphComposite graph : getVisibleGraphs()) {
       
   763 				graph.graphComponent.graphComponent.updateSelectionArea(
       
   764 						PIPageEditor.currentPageEditor().getStartTime(),
       
   765 						PIPageEditor.currentPageEditor().getEndTime());
       
   766 			}
       
   767 			
       
   768 		}
       
   769 	}
       
   770 
       
   771 	/*
       
   772 	 * (non-Javadoc)
       
   773 	 * 
       
   774 	 * @see
       
   775 	 * org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events
       
   776 	 * .ControlEvent)
       
   777 	 */
       
   778 	public void controlMoved(ControlEvent e) {
       
   779 		updateTitleBarButtonsStatus();
       
   780 	}
       
   781 
       
   782 	/*
       
   783 	 * (non-Javadoc)
       
   784 	 * 
       
   785 	 * @see
       
   786 	 * org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt
       
   787 	 * .events.ControlEvent)
       
   788 	 */
       
   789 	public void controlResized(ControlEvent e) {
       
   790 
       
   791 		// update minimize button status if size of the form is changed
       
   792 		// manually.
       
   793 
       
   794 		// exclude status update when graph is maximized
       
   795 		// (so that graph remembers if it was minimized previously)
       
   796 
       
   797 		this.getParent().layout();
       
   798 		if (this.isMaximized == false) {
       
   799 			if (this.isMinimized
       
   800 					&& (double) this.getSize().y
       
   801 							/ (double) compositePanel.getSashForm().getSize().y > 0.1) {
       
   802 				this.isMinimized = false;
       
   803 				graphComponent.graphComponent.graphVisibilityChanged(true);
       
   804 				compositePanel.getSashForm().layout();// after revealing or
       
   805 														// hiding legend tables
       
   806 				updateTitleBarButtonsStatus();
       
   807 			}
       
   808 		}
       
   809 
       
   810 	}
       
   811 
       
   812 	/*
       
   813 	 * (non-Javadoc)
       
   814 	 * 
       
   815 	 * @see
       
   816 	 * com.nokia.carbide.cpp.pi.visual.IVisibilityListener#onVisiblityChanged
       
   817 	 * (boolean)
       
   818 	 */
       
   819 	public void onVisiblityChanged(boolean visible) {
       
   820 
       
   821 		// change the visibility of this graph composite
       
   822 		// and reset to default layout
       
   823 
       
   824 		if (visible) {
       
   825 			// check the newly made visible control has a weight
       
   826 			// if not, set an arbitrary weight to prevent an exception in the
       
   827 			// sashForm
       
   828 			int[] weights = this.compositePanel.getSashForm().getWeights();
       
   829 			int idx = getIndexOfThisComponentInSashForm();
       
   830 			if (weights[idx] == 0) {
       
   831 				weights[idx] = 100;
       
   832 				compositePanel.getSashForm().setWeights(weights);
       
   833 			}
       
   834 		}
       
   835 
       
   836 		// we need to reset maximised controls, because if they are now
       
   837 		// invisible, they
       
   838 		// prevent other graphs from showing
       
   839 		if (compositePanel.getSashForm().getMaximizedControl() != null) {
       
   840 			((GraphComposite) compositePanel.getSashForm()
       
   841 					.getMaximizedControl()).isMaximized = false;
       
   842 			compositePanel.getSashForm().setMaximizedControl(null);
       
   843 		}
       
   844 
       
   845 		this.setVisible(visible);
       
   846 		// deal with the legend as well
       
   847 		graphComponent.graphComponent.graphVisibilityChanged(visible
       
   848 				&& isMinimized ? false : visible);
       
   849 
       
   850 		resetLayoutToDefault();
       
   851 
       
   852 		// button status needs to be updated for all graphs
       
   853 		for (GraphComposite graph : getVisibleGraphs()) {
       
   854 			graph.updateTitleBarButtonsStatus();
       
   855 		}
       
   856 	}
       
   857 
       
   858 	/**
       
   859 	 * Resets the layout of all visible component to a default layout. Minimised
       
   860 	 * components stay minimised (just toolbar visible), all other components
       
   861 	 * are equally sized.
       
   862 	 */
       
   863 	private void resetLayoutToDefault() {
       
   864 		int xSize = compositePanel.getSashForm().getSize().y;
       
   865 		if (xSize == 0) {// not yet initialised
       
   866 			return;
       
   867 		}
       
   868 		double pixelWeight = (double) TOTAL_SASH_WEIGHT / xSize;
       
   869 		int minCount = 0;
       
   870 		GraphComposite[] visibleGraphs = getVisibleGraphs();
       
   871 
       
   872 		if (visibleGraphs.length > 0) {
       
   873 			int minimisedWeight = (int) (TITLESIZE * pixelWeight);
       
   874 
       
   875 			for (GraphComposite cmp : visibleGraphs) {
       
   876 				if (cmp.isMinimized) {
       
   877 					minCount++;
       
   878 				}
       
   879 			}
       
   880 
       
   881 			double defaultWeight = TOTAL_SASH_WEIGHT
       
   882 					- (minCount * minimisedWeight);
       
   883 			if (minCount < visibleGraphs.length - 1) {
       
   884 				defaultWeight /= (visibleGraphs.length - minCount);
       
   885 			}
       
   886 
       
   887 			int[] weights = compositePanel.getSashForm().getWeights();
       
   888 			int i = 0;
       
   889 			for (Control control : this.compositePanel.getSashForm()
       
   890 					.getChildren()) {
       
   891 				if (control.getClass() == this.getClass()
       
   892 						&& control.isVisible()) {
       
   893 					if (((GraphComposite) control).isMinimized) {
       
   894 						weights[i] = minimisedWeight;
       
   895 					} else {
       
   896 						weights[i] = (int) defaultWeight;
       
   897 					}
       
   898 				}
       
   899 				i++;
       
   900 			}
       
   901 			this.compositePanel.getSashForm().setWeights(weights);
       
   902 		}
       
   903 	}
       
   904 
       
   905 	/*
       
   906 	 * (non-Javadoc)
       
   907 	 * 
       
   908 	 * @see
       
   909 	 * com.nokia.carbide.cpp.pi.visual.IGraphChangeListener#onTitleChange(java
       
   910 	 * .lang.String, java.lang.String)
       
   911 	 */
       
   912 	public void onTitleChange(final String newTitle) {
       
   913 		Display.getDefault().syncExec(new Runnable() {
       
   914 			public void run() {
       
   915 				labelTitle.setText(newTitle);
       
   916 			}
       
   917 		});
       
   918 	}
       
   919 
       
   920 	/**
       
   921 	 * Initialisation code for all graphs should go here. This method is called
       
   922 	 * on editor part activation
       
   923 	 */
       
   924 	public void initialiseGraphs() {
       
   925 		// minimize other than Memory and Thread Load graphs when the view is
       
   926 		// opened
       
   927 
       
   928 		// this code can only be executed once the sashform is visible
       
   929 		// otherwise the weights of controls on the sashform are zero and the
       
   930 		// minimise code doesn't work.
       
   931 		if (this.isViewInitialized == false
       
   932 				&& compositePanel.getSashForm().isVisible()) {
       
   933 			this.isViewInitialized = true;
       
   934 			if (graphComponent.graphComponent.isGraphMinimizedWhenOpened()) {
       
   935 				this.minimizeOrRestoreGraph();
       
   936 				updateTitleBarButtonsStatus();
       
   937 				compositePanel.getSashForm().layout();
       
   938 			}
       
   939 		}
       
   940 	}
       
   941 
    86 }
   942 }