sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/internal/ui/graph/GraphSettingsThresholdComposite.java
changeset 15 0367d2db2c06
equal deleted inserted replaced
14:bb339882c6e9 15:0367d2db2c06
       
     1 /*
       
     2  * Copyright (c) 2008-2010 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 "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:  Definitions for the class AnalyzeToolGraph
       
    15  *
       
    16  */
       
    17 package com.nokia.s60tools.analyzetool.internal.ui.graph;
       
    18 
       
    19 import org.eclipse.jface.dialogs.IDialogConstants;
       
    20 import org.eclipse.jface.dialogs.IInputValidator;
       
    21 import org.eclipse.jface.layout.GridDataFactory;
       
    22 import org.eclipse.jface.layout.GridLayoutFactory;
       
    23 import org.eclipse.jface.resource.StringConverter;
       
    24 import org.eclipse.swt.SWT;
       
    25 import org.eclipse.swt.events.ModifyEvent;
       
    26 import org.eclipse.swt.events.ModifyListener;
       
    27 import org.eclipse.swt.widgets.Button;
       
    28 import org.eclipse.swt.widgets.Composite;
       
    29 import org.eclipse.swt.widgets.Control;
       
    30 import org.eclipse.swt.widgets.Display;
       
    31 import org.eclipse.swt.widgets.Group;
       
    32 import org.eclipse.swt.widgets.Label;
       
    33 import org.eclipse.swt.widgets.Text;
       
    34 
       
    35 /**
       
    36  * Composite for entering the graph's threshold value. This is intended to go
       
    37  * onto the GraphSettingsDialog.
       
    38  */
       
    39 public class GraphSettingsThresholdComposite extends Composite {
       
    40 	private static final String B = "b";
       
    41 	private static final String GB = "gb";
       
    42 	private static final String BYTE = "byte";
       
    43 	private static final String BYTES = "bytes";
       
    44 	private static final String MB = "mb";
       
    45 	private static final String KB = "kb";
       
    46 	private static final String DIALOG_MESSAGE = "Only show allocations on the graph which are";
       
    47 	private static final String ABOVE = "above or equals the threshold";
       
    48 	private static final String BELOW = "below or equals the threshold";
       
    49 	private static final String DIALOG_MESSAGE2 = "Examples for valid entries are 20B, 4KB, or 1MB.  Leaving the value empty will clear the threshold.";
       
    50 	private static final String THRESHOLD_LABEL = "Threshold:";
       
    51 	private static final String INVALID_INPUT_VALUE = "Invalid input value.";
       
    52 	private static final String OUT_OF_RANGE_VALUE = "The value is out of range.";
       
    53 
       
    54 	private GraphSettingsDialog parentDialog;
       
    55 	private boolean aboveValue;
       
    56 	private IInputValidator validator;
       
    57 
       
    58 	/** Threshold Value text entry widget */
       
    59 	private Text text;
       
    60 	private String value;
       
    61 	/** Error message label widget. */
       
    62 	private Text errorMessageText;
       
    63 	private Button radioAbove;
       
    64 
       
    65 	/**
       
    66 	 * Constructor
       
    67 	 * 
       
    68 	 * @param parent
       
    69 	 *            the parent composite
       
    70 	 * @param parentDialog
       
    71 	 *            The calling dialog
       
    72 	 * @param oldThreshold
       
    73 	 *            previous threshold value as user entered it
       
    74 	 * @param above
       
    75 	 *            true if previous threshold was filtering "above" the threshold
       
    76 	 */
       
    77 	public GraphSettingsThresholdComposite(Composite parent,
       
    78 			GraphSettingsDialog parentDialog, String oldThreshold, boolean above) {
       
    79 		super(parent, SWT.NONE);
       
    80 		this.parentDialog = parentDialog;
       
    81 		value = oldThreshold == null ? "" : oldThreshold;
       
    82 		aboveValue = above;
       
    83 		validator = new ThresholdEntryValidator();
       
    84 	}
       
    85 
       
    86 	/**
       
    87 	 * Creates the content of this composite
       
    88 	 */
       
    89 	public void createControl() {
       
    90 		setLayout(GridLayoutFactory.fillDefaults().create());
       
    91 
       
    92 		Group group = new Group(this, SWT.NONE);
       
    93 		group.setText("Graph Threshold");
       
    94 		group.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).margins(
       
    95 				10, 10).equalWidth(false).create());
       
    96 		GridDataFactory.fillDefaults().hint(350, SWT.DEFAULT).applyTo(group);
       
    97 
       
    98 		Label message = new Label(group, SWT.WRAP);
       
    99 		message.setText(DIALOG_MESSAGE);
       
   100 		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).span(3, 1)
       
   101 				.grab(true, true).applyTo(message);
       
   102 
       
   103 		radioAbove = new Button(group, SWT.RADIO);
       
   104 		radioAbove.setText(ABOVE);
       
   105 		radioAbove.setSelection(aboveValue);
       
   106 		GridDataFactory.fillDefaults().span(3, 1).indent(10, 0).applyTo(
       
   107 				radioAbove);
       
   108 
       
   109 		Button radioBelow = new Button(group, SWT.RADIO);
       
   110 		radioBelow.setText(BELOW);
       
   111 		radioBelow.setSelection(!aboveValue);
       
   112 		GridDataFactory.fillDefaults().span(3, 1).indent(10, 0).applyTo(
       
   113 				radioBelow);
       
   114 
       
   115 		Label thresholdLabel = new Label(group, SWT.NONE);
       
   116 		thresholdLabel.setText(THRESHOLD_LABEL);
       
   117 		GridDataFactory.swtDefaults().align(SWT.BEGINNING, SWT.CENTER).grab(
       
   118 				false, false).indent(0, 10).applyTo(thresholdLabel);
       
   119 
       
   120 		text = new Text(group, SWT.SINGLE | SWT.BORDER);
       
   121 		GridDataFactory.fillDefaults().indent(0, 7).align(SWT.BEGINNING,
       
   122 				SWT.END).hint(80, SWT.DEFAULT).applyTo(text);
       
   123 		text.addModifyListener(new ModifyListener() {
       
   124 			public void modifyText(ModifyEvent e) {
       
   125 				validateInput();
       
   126 			}
       
   127 		});
       
   128 		text.setText(value);
       
   129 		text.setFocus();
       
   130 		text.selectAll();
       
   131 
       
   132 		errorMessageText = new Text(group, SWT.READ_ONLY | SWT.WRAP);
       
   133 		GridDataFactory.fillDefaults().grab(true, false).indent(0, 10).applyTo(
       
   134 				errorMessageText);
       
   135 		errorMessageText.setBackground(errorMessageText.getDisplay()
       
   136 				.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
       
   137 		errorMessageText.setForeground(Display.getDefault().getSystemColor(
       
   138 				SWT.COLOR_RED));
       
   139 
       
   140 		Label examplesLabel = new Label(group, SWT.WRAP);
       
   141 		examplesLabel.setText(DIALOG_MESSAGE2);
       
   142 		GridDataFactory.fillDefaults().span(3, 1).grab(true, false).indent(0,
       
   143 				10).applyTo(examplesLabel);
       
   144 
       
   145 		validateInput();
       
   146 	}
       
   147 
       
   148 	private void validateInput() {
       
   149 		setErrorMessage(validator.isValid(text.getText()));
       
   150 	}
       
   151 
       
   152 	/**
       
   153 	 * Sets or clears the error message. If not <code>null</code>, the OK button
       
   154 	 * is disabled.
       
   155 	 * 
       
   156 	 * @param errorMessage
       
   157 	 *            the error message, or <code>null</code> to clear
       
   158 	 * @since 3.0
       
   159 	 */
       
   160 	public void setErrorMessage(String errorMessage) {
       
   161 		if (errorMessageText != null && !errorMessageText.isDisposed()) {
       
   162 			errorMessageText.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
       
   163 
       
   164 			// Disable the error message text control if there is no error, or
       
   165 			// no error text (empty or whitespace only). Hide it also to avoid
       
   166 			// color change.
       
   167 			// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130281
       
   168 			boolean hasError = errorMessage != null
       
   169 					&& (StringConverter.removeWhiteSpaces(errorMessage))
       
   170 							.length() > 0;
       
   171 			errorMessageText.setEnabled(hasError);
       
   172 			errorMessageText.setVisible(hasError);
       
   173 			errorMessageText.getParent().update();
       
   174 
       
   175 			Control button = parentDialog.getOkButton();
       
   176 			if (button != null) {
       
   177 				button.setEnabled(errorMessage == null);
       
   178 			}
       
   179 		}
       
   180 	}
       
   181 
       
   182 	private static long parseValue(String value) throws NumberFormatException {
       
   183 		String tmpVal = value.trim().toLowerCase();
       
   184 		int factor = 1;
       
   185 		if (tmpVal.endsWith(KB)) {
       
   186 			tmpVal = tmpVal.substring(0, tmpVal.lastIndexOf(KB)).trim();
       
   187 			factor = 1024;
       
   188 		} else if (tmpVal.endsWith(MB)) {
       
   189 			tmpVal = tmpVal.substring(0, tmpVal.lastIndexOf(MB)).trim();
       
   190 			factor = 1024 * 1024;
       
   191 		} else if (tmpVal.endsWith(BYTES)) {
       
   192 			tmpVal = tmpVal.substring(0, tmpVal.lastIndexOf(BYTES)).trim();
       
   193 		} else if (tmpVal.endsWith(BYTE)) {
       
   194 			tmpVal = tmpVal.substring(0, tmpVal.lastIndexOf(BYTE)).trim();
       
   195 		} else if (tmpVal.endsWith(GB)) {
       
   196 			tmpVal = tmpVal.substring(0, tmpVal.lastIndexOf(GB)).trim();
       
   197 			factor = 1024 * 1024 * 1024;
       
   198 		} else if (tmpVal.endsWith(B)) {
       
   199 			tmpVal = tmpVal.substring(0, tmpVal.lastIndexOf(B)).trim();
       
   200 		}
       
   201 		return Long.parseLong(tmpVal) * factor;
       
   202 	}
       
   203 
       
   204 	/**
       
   205 	 * Returns the threshold value as entered by the user
       
   206 	 * 
       
   207 	 * @return the threshold value as entered by the user
       
   208 	 */
       
   209 	public String getThresholdString() {
       
   210 		return value;
       
   211 	};
       
   212 
       
   213 	/**
       
   214 	 * Returns the threshold value in bytes
       
   215 	 * 
       
   216 	 * @return the threshold value in bytes
       
   217 	 */
       
   218 	public long getThreshold() {
       
   219 		if (value == null || value.length() == 0) {
       
   220 			return 0;
       
   221 		}
       
   222 		return parseValue(value);
       
   223 	}
       
   224 
       
   225 	/**
       
   226 	 * Returns filtering direction for threshold, true for
       
   227 	 * "above and equals threshold", false for "below and equals threshold"
       
   228 	 * 
       
   229 	 * @return
       
   230 	 */
       
   231 	public boolean getAbove() {
       
   232 		return aboveValue;
       
   233 	}
       
   234 
       
   235 	/**
       
   236 	 * Called from the dialog's buttonPressed() method
       
   237 	 * 
       
   238 	 * @param buttonId
       
   239 	 *            the ID of the button that was pressed
       
   240 	 */
       
   241 	public void buttonPressed(int buttonId) {
       
   242 		value = buttonId == IDialogConstants.OK_ID ? text.getText() : null;
       
   243 		aboveValue = radioAbove.getSelection();
       
   244 	}
       
   245 
       
   246 	/**
       
   247 	 * This class validates the user-entered threshold value
       
   248 	 * 
       
   249 	 */
       
   250 	class ThresholdEntryValidator implements IInputValidator {
       
   251 
       
   252 		/*
       
   253 		 * (non-Javadoc)
       
   254 		 * 
       
   255 		 * @see
       
   256 		 * org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
       
   257 		 */
       
   258 		public String isValid(String newText) {
       
   259 			if (newText.length() == 0) {
       
   260 				return null;
       
   261 			}
       
   262 
       
   263 			try {
       
   264 				long value = parseValue(newText);
       
   265 				if (value < 0 || value > 4294967296L) {
       
   266 					return OUT_OF_RANGE_VALUE;
       
   267 				}
       
   268 			} catch (NumberFormatException e) {
       
   269 				return INVALID_INPUT_VALUE;
       
   270 			}
       
   271 			return null;
       
   272 		}
       
   273 	}
       
   274 }