View Javadoc
1 package net.sourceforge.pmd.swingui; 2 3 import net.sourceforge.pmd.swingui.event.ListenerList; 4 import net.sourceforge.pmd.swingui.event.StatusBarEvent; 5 import net.sourceforge.pmd.swingui.event.StatusBarEventListener; 6 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.SwingUtilities; 10 import javax.swing.UIManager; 11 import javax.swing.border.BevelBorder; 12 import javax.swing.border.EmptyBorder; 13 import java.awt.BorderLayout; 14 import java.awt.Color; 15 import java.awt.Component; 16 import java.awt.Dimension; 17 import java.awt.Font; 18 import java.awt.Graphics; 19 import java.awt.Insets; 20 import java.awt.Rectangle; 21 22 /*** 23 * 24 * @author Donald A. Leckie 25 * @since January 6, 2003 26 * @version $Revision: 1.5 $, $Date: 2003/05/28 18:08:35 $ 27 */ 28 class StatusBar extends JPanel { 29 private JLabel m_message; 30 private String m_defaultMessage; 31 private StatusArea m_statusArea; 32 33 /*** 34 **************************************************************************** 35 * 36 * @param border 37 */ 38 protected StatusBar(String defaultMessage) { 39 super(new BorderLayout()); 40 41 m_defaultMessage = defaultMessage; 42 43 // 44 // Status Indicator 45 // 46 m_statusArea = new StatusArea(); 47 add(m_statusArea, BorderLayout.WEST); 48 49 // 50 // Message 51 // 52 m_message = new JLabel(); 53 m_message.setFont(new Font("Dialog", Font.BOLD, 12)); 54 m_message.setBackground(UIManager.getColor("pmdMessageAreaBackground")); 55 m_message.setForeground(UIManager.getColor("pmdBlue")); 56 m_message.setBorder(new EmptyBorder(0, 5, 0, 0)); 57 setDefaultMessage(); 58 add(m_message, BorderLayout.CENTER); 59 60 ListenerList.addListener((StatusBarEventListener) new StatusBarEventHandler()); 61 } 62 63 /*** 64 ********************************************************************************* 65 * 66 */ 67 protected void setDefaultMessage() { 68 setMessage(m_defaultMessage); 69 } 70 71 /*** 72 ********************************************************************************* 73 * 74 * @param message The message to be displayed in the status area. 75 */ 76 protected void setMessage(String message) { 77 if (message == null) { 78 message = ""; 79 } 80 81 m_message.setText(message); 82 } 83 84 /*** 85 ********************************************************************************* 86 ********************************************************************************* 87 ********************************************************************************* 88 */ 89 private class StatusArea extends JPanel { 90 private StatusActionThread m_actionThread; 91 private Color m_inactiveBackground; 92 private Color m_activeBackground; 93 private Color m_actionColor; 94 private int m_direction; 95 private int m_indicatorCurrentPosition; 96 private final int POSITION_INCREMENT = 5; 97 private final int START_MOVING = 0; 98 private final int MOVE_FORWARD = 1; 99 private final int MOVE_BACKWARD = 2; 100 101 /*** 102 **************************************************************************** 103 * 104 * @param border 105 */ 106 private StatusArea() { 107 super(null); 108 109 m_inactiveBackground = Color.gray; 110 m_activeBackground = UIManager.getColor("pmdStatusAreaBackground"); 111 m_actionColor = Color.red; 112 113 setOpaque(true); 114 setBackground(m_inactiveBackground); 115 setBorder(new BevelBorder(BevelBorder.LOWERED)); 116 117 Dimension size = new Dimension(160, 12); 118 119 setMinimumSize(size); 120 setMaximumSize(size); 121 setSize(size); 122 setPreferredSize(size); 123 } 124 125 /*** 126 **************************************************************************** 127 * 128 */ 129 private void startAction() { 130 if (m_actionThread == null) { 131 setBackground(m_activeBackground); 132 m_direction = START_MOVING; 133 m_actionThread = new StatusActionThread(this); 134 m_actionThread.start(); 135 } 136 } 137 138 /*** 139 **************************************************************************** 140 * 141 */ 142 private void stopAction() { 143 if (m_actionThread != null) { 144 m_actionThread.stopAction(); 145 m_actionThread = null; 146 setBackground(m_inactiveBackground); 147 repaint(); 148 } 149 } 150 151 /*** 152 **************************************************************************** 153 * 154 * @param graphics 155 */ 156 public void paint(Graphics graphics) { 157 super.paint(graphics); 158 159 if (getBackground() == m_activeBackground) { 160 Rectangle totalArea; 161 Insets insets; 162 int indicatorWidth; 163 int indicatorHeight; 164 int indicatorY; 165 int indicatorX; 166 int totalAreaRight; 167 168 totalArea = getBounds(); 169 insets = getInsets(); 170 totalArea.x += insets.left; 171 totalArea.y += insets.top; 172 totalArea.width -= (insets.left + insets.right); 173 totalArea.height -= (insets.top + insets.bottom); 174 totalAreaRight = totalArea.x + totalArea.width; 175 indicatorWidth = totalArea.width / 3; 176 indicatorHeight = totalArea.height; 177 indicatorY = totalArea.y; 178 179 if (m_direction == MOVE_FORWARD) { 180 m_indicatorCurrentPosition += POSITION_INCREMENT; 181 182 if (m_indicatorCurrentPosition >= totalAreaRight) { 183 m_indicatorCurrentPosition = totalAreaRight - POSITION_INCREMENT; 184 m_direction = MOVE_BACKWARD; 185 } 186 } else if (m_direction == MOVE_BACKWARD) { 187 m_indicatorCurrentPosition -= POSITION_INCREMENT; 188 189 if (m_indicatorCurrentPosition < totalArea.x) { 190 m_indicatorCurrentPosition = totalArea.x + POSITION_INCREMENT; 191 m_direction = MOVE_FORWARD; 192 } 193 } else { 194 m_indicatorCurrentPosition = totalArea.x + POSITION_INCREMENT; 195 m_direction = MOVE_FORWARD; 196 } 197 198 indicatorX = m_indicatorCurrentPosition; 199 200 Rectangle oldClip = graphics.getClipBounds(); 201 Color oldColor = graphics.getColor(); 202 203 graphics.setColor(m_activeBackground); 204 graphics.setClip(totalArea.x, totalArea.y, totalArea.width, totalArea.height); 205 graphics.clipRect(totalArea.x, totalArea.y, totalArea.width, totalArea.height); 206 graphics.fillRect(totalArea.x, totalArea.y, totalArea.width, totalArea.height); 207 208 if (m_direction == MOVE_FORWARD) { 209 int stopX = indicatorX - indicatorWidth; 210 211 if (stopX < totalArea.x) { 212 stopX = totalArea.x; 213 } 214 215 int y1 = indicatorY; 216 int y2 = y1 + indicatorHeight; 217 Color color = m_actionColor; 218 219 for (int x = indicatorX; x > stopX; x--) { 220 graphics.setColor(color); 221 graphics.drawLine(x, y1, x, y2); 222 color = brighter(color); 223 } 224 } else { 225 int stopX = indicatorX + indicatorWidth; 226 227 if (stopX > totalAreaRight) { 228 stopX = totalAreaRight; 229 } 230 231 int y1 = indicatorY; 232 int y2 = indicatorY + indicatorHeight; 233 Color color = m_actionColor; 234 235 for (int x = indicatorX; x < stopX; x++) { 236 graphics.setColor(color); 237 graphics.drawLine(x, y1, x, y2); 238 color = brighter(color); 239 } 240 } 241 242 graphics.setColor(oldColor); 243 244 if (oldClip != null) { 245 graphics.clipRect(oldClip.x, oldClip.y, oldClip.width, oldClip.height); 246 graphics.setClip(oldClip.x, oldClip.y, oldClip.width, oldClip.height); 247 } 248 } 249 } 250 251 /*** 252 **************************************************************************** 253 * 254 * @param color 255 * 256 * @return 257 */ 258 private Color brighter(Color color) { 259 int red; 260 int green; 261 int blue; 262 263 red = color.getRed() + 5; 264 green = color.getGreen() + 5; 265 blue = color.getBlue() + 5; 266 267 if (red > 255) { 268 red = 255; 269 } 270 271 if (green > 255) { 272 green = 255; 273 } 274 275 if (blue > 255) { 276 blue = 255; 277 } 278 279 return new Color(red, green, blue); 280 } 281 } 282 283 /*** 284 ********************************************************************************* 285 ********************************************************************************* 286 ********************************************************************************* 287 */ 288 private class StatusActionThread extends Thread { 289 private StatusArea m_statusArea; 290 private boolean m_stopAction; 291 private int m_doNothing; 292 private final long ELAPSED_TIME = 25; 293 294 /*** 295 **************************************************************************** 296 * 297 * @param statusArea 298 */ 299 private StatusActionThread(StatusArea statusArea) { 300 super("Status Action"); 301 302 m_statusArea = statusArea; 303 } 304 305 /*** 306 **************************************************************************** 307 * 308 */ 309 public void run() { 310 while (m_stopAction == false) { 311 m_statusArea.repaint(); 312 313 try { 314 sleep(ELAPSED_TIME); 315 } catch (InterruptedException exception) { 316 m_doNothing++; 317 } 318 } 319 } 320 321 /*** 322 **************************************************************************** 323 * 324 */ 325 private void stopAction() { 326 m_stopAction = true; 327 } 328 } 329 330 /*** 331 ********************************************************************************* 332 ********************************************************************************* 333 ********************************************************************************* 334 */ 335 private class StatusBarEventHandler implements StatusBarEventListener { 336 337 /*** 338 ***************************************************************************** 339 * 340 * @param event 341 */ 342 public void startAnimation(StatusBarEvent event) { 343 m_statusArea.startAction(); 344 m_message.setText(""); 345 SwingUtilities.invokeLater(new Repaint(m_message)); 346 } 347 348 /*** 349 ***************************************************************************** 350 * 351 * @param event 352 */ 353 public void showMessage(StatusBarEvent event) { 354 m_message.setText(event.getMessage()); 355 SwingUtilities.invokeLater(new Repaint(m_message)); 356 } 357 358 /*** 359 ***************************************************************************** 360 * 361 * @param event 362 */ 363 public void stopAnimation(StatusBarEvent event) { 364 setDefaultMessage(); 365 SwingUtilities.invokeLater(new Repaint(m_message)); 366 m_statusArea.stopAction(); 367 } 368 } 369 370 /*** 371 ********************************************************************************* 372 ********************************************************************************* 373 ********************************************************************************* 374 */ 375 private class Repaint implements Runnable { 376 private Component m_component; 377 378 /*** 379 ***************************************************************************** 380 * 381 * @param component 382 */ 383 private Repaint(Component component) { 384 m_component = component; 385 } 386 387 /*** 388 ***************************************************************************** 389 * 390 */ 391 public void run() { 392 m_component.repaint(); 393 } 394 } 395 }

This page was automatically generated by Maven