|
Added
Link Here
|
| 1 |
package org.eclipse.tptp.test.manual.runner.ui.internal.widgets; |
| 2 |
|
| 3 |
import org.eclipse.swt.SWT; |
| 4 |
import org.eclipse.swt.events.ControlEvent; |
| 5 |
import org.eclipse.swt.events.ControlListener; |
| 6 |
import org.eclipse.swt.graphics.Color; |
| 7 |
import org.eclipse.swt.widgets.Composite; |
| 8 |
import org.eclipse.swt.widgets.Display; |
| 9 |
import org.eclipse.swt.widgets.Layout; |
| 10 |
|
| 11 |
/********************************************************************** |
| 12 |
* Copyright (c) 2007 IBM Corporation and others. |
| 13 |
* All rights reserved. This program and the accompanying materials |
| 14 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 15 |
* which accompanies this distribution, and is available at |
| 16 |
* http://www.eclipse.org/legal/epl-v10.html |
| 17 |
* $Id$ |
| 18 |
* |
| 19 |
* Contributors: |
| 20 |
* IBM - Initial API and implementation |
| 21 |
**********************************************************************/ |
| 22 |
|
| 23 |
/** |
| 24 |
* Simple by customizable progress bar. The progress bar has the |
| 25 |
* following customizable properties: |
| 26 |
* <p> |
| 27 |
* <ul> |
| 28 |
* <li>Minimum value.</li> |
| 29 |
* <li>Maximum value.</li> |
| 30 |
* <li>Current selection value.</li> |
| 31 |
* <li>Color.</li> |
| 32 |
* </ul> |
| 33 |
* <p> |
| 34 |
* This progress bar should be used instead of the SWT |
| 35 |
* {@link org.eclipse.swt.widgets.ProgressBar} when color customization is |
| 36 |
* required (see {@link https://bugs.eclipse.org/bugs/show_bug.cgi?id=168738}. |
| 37 |
* <p> |
| 38 |
* Note: The layout can not be set on a this class. |
| 39 |
* <p> |
| 40 |
* |
| 41 |
* |
| 42 |
* @author Paul E. Slauenwhite |
| 43 |
* @version February 19, 2007 |
| 44 |
* @since February 16, 2007 |
| 45 |
*/ |
| 46 |
public class SimpleProgressBar extends Composite { |
| 47 |
|
| 48 |
private int minimum = 0; |
| 49 |
|
| 50 |
private int maximum = 0; |
| 51 |
|
| 52 |
private int selection = 0; |
| 53 |
|
| 54 |
private Composite progressBar = null; |
| 55 |
|
| 56 |
private static final Color DEFAULT_PROGRESS_BAR_COLOR = Display.getDefault().getSystemColor(SWT.COLOR_DARK_BLUE); |
| 57 |
|
| 58 |
public SimpleProgressBar(Composite parent) { |
| 59 |
|
| 60 |
super(parent, (SWT.BORDER | SWT.SMOOTH)); |
| 61 |
|
| 62 |
buildProgressBar(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the maximum value of the progress bar. |
| 67 |
* <p> |
| 68 |
* |
| 69 |
* @return Maximum value of the progress bar. |
| 70 |
*/ |
| 71 |
public int getMaximum() { |
| 72 |
return maximum; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the minimum value of the progress bar. |
| 77 |
* <p> |
| 78 |
* |
| 79 |
* @return Minimum value of the progress bar. |
| 80 |
*/ |
| 81 |
public int getMinimum() { |
| 82 |
return minimum; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the current selected value of the progress bar. |
| 87 |
* <p> |
| 88 |
* |
| 89 |
* @return Current selected value of the progress bar. |
| 90 |
*/ |
| 91 |
public int getSelection() { |
| 92 |
return selection; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the color of the progress bar. |
| 97 |
* <p> |
| 98 |
* |
| 99 |
* @return Color of the progress bar. |
| 100 |
*/ |
| 101 |
public Color getColor() { |
| 102 |
return (progressBar.getBackground()); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Sets the maximum value of the progress bar. The parameter value is |
| 107 |
* ignored if any of the following conditions are true: |
| 108 |
* <p> |
| 109 |
* <ul> |
| 110 |
* <li>The maximum value is less than the minimum value.</li> |
| 111 |
* <li>The maximum value is less than zero.</li> |
| 112 |
* <p> |
| 113 |
* Note: Set the maximum value causes the current selected value to be reset |
| 114 |
* to the minimum value. |
| 115 |
* <p> |
| 116 |
* |
| 117 |
* @param maximum |
| 118 |
* Maximum value of the progress bar. |
| 119 |
*/ |
| 120 |
public void setMaximum(int maximum) { |
| 121 |
|
| 122 |
if ((maximum >= 0) && (maximum >= minimum)) { |
| 123 |
|
| 124 |
this.maximum = maximum; |
| 125 |
this.selection = minimum; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Sets the minimum value of the progress bar. The parameter value is |
| 131 |
* ignored if any of the following conditions are true: |
| 132 |
* <p> |
| 133 |
* <ul> |
| 134 |
* <li>The minimum value is greater than the maximum value.</li> |
| 135 |
* <li>The minimum value is less than zero.</li> |
| 136 |
* <p> |
| 137 |
* Note: Set the minimum value causes the current selected value to be reset |
| 138 |
* to the new minimum value. |
| 139 |
* <p> |
| 140 |
* |
| 141 |
* @param minimum |
| 142 |
* Minimum value of the progress bar. |
| 143 |
*/ |
| 144 |
public void setMinimum(int minimum) { |
| 145 |
|
| 146 |
if ((minimum >= 0) && (minimum <= maximum)) { |
| 147 |
|
| 148 |
this.minimum = minimum; |
| 149 |
this.selection = minimum; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Sets the current selected value of the progress bar. The parameter value |
| 155 |
* is ignored if any of the following conditions are true: |
| 156 |
* <p> |
| 157 |
* <ul> |
| 158 |
* <li>The current selected value is less than the minimum value.</li> |
| 159 |
* <li>The current selected value is greater than the maximum value.</li> |
| 160 |
* <li>The current selected value is less than zero.</li> |
| 161 |
* <p> |
| 162 |
* |
| 163 |
* @param selection |
| 164 |
* Current selected value of the progress bar. |
| 165 |
*/ |
| 166 |
public void setSelection(int selection) { |
| 167 |
|
| 168 |
if ((selection >= 0) && (selection >= minimum) && (selection <= maximum)) { |
| 169 |
|
| 170 |
this.selection = selection; |
| 171 |
|
| 172 |
updateProgress(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Sets the color of the progress bar. If the parameter value is |
| 178 |
* <code>null</code>, the color is set to a default color ({@link SWT.COLOR_DARK_BLUE}). |
| 179 |
* <p> |
| 180 |
* |
| 181 |
* @param color |
| 182 |
* Color of the progress bar. |
| 183 |
*/ |
| 184 |
public void setColor(Color color) { |
| 185 |
|
| 186 |
if (color != null) { |
| 187 |
progressBar.setBackground(color); |
| 188 |
} else { |
| 189 |
progressBar.setBackground(DEFAULT_PROGRESS_BAR_COLOR); |
| 190 |
} |
| 191 |
|
| 192 |
updateProgress(); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Increments the current selected value of the progress bar by one. |
| 197 |
* <p> |
| 198 |
*/ |
| 199 |
public void incrementSelection() { |
| 200 |
setSelection(getSelection() + 1); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* No-operation. |
| 205 |
* <p> |
| 206 |
* Note: The layout can not be set on a this class. |
| 207 |
* <p> |
| 208 |
* |
| 209 |
* @see org.eclipse.swt.widgets.Composite#setLayout(org.eclipse.swt.widgets.Layout) |
| 210 |
*/ |
| 211 |
public void setLayout(Layout layout) { |
| 212 |
// No-operation. |
| 213 |
} |
| 214 |
|
| 215 |
private void buildProgressBar() { |
| 216 |
|
| 217 |
progressBar = new Composite(this, SWT.NONE); |
| 218 |
progressBar.setBackground(DEFAULT_PROGRESS_BAR_COLOR); |
| 219 |
|
| 220 |
layout(false, true); |
| 221 |
|
| 222 |
addControlListener(new ControlListener() { |
| 223 |
|
| 224 |
public void controlMoved(ControlEvent con) { |
| 225 |
// No-operation. |
| 226 |
} |
| 227 |
|
| 228 |
public void controlResized(ControlEvent e) { |
| 229 |
updateProgress(); |
| 230 |
} |
| 231 |
}); |
| 232 |
} |
| 233 |
|
| 234 |
private void updateProgress() { |
| 235 |
|
| 236 |
progressBar.setBounds(0, 0, Math.round((((float)(selection - minimum)) / ((float)(maximum - minimum))) * ((float)(getBounds().width))), 17); |
| 237 |
|
| 238 |
layout(true, true); |
| 239 |
} |
| 240 |
} |