001: /*
002: * SwingBugUtilities.java
003: *
004: * Created on March 30, 2007, 12:27 AM
005: *
006: * Copyright 2006-2007 Nigel Hughes
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
009: * in compliance with the License. You may obtain a copy of the License at http://www.apache.org/
010: * licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
012: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
013: * governing permissions and limitations under the License.
014: */
015:
016: package com.blogofbug.swing;
017:
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import javax.swing.Action;
021: import javax.swing.Timer;
022:
023: /**
024: * Contains some utility methods applicable to any swing application.
025: *
026: * @author nigel
027: */
028: public class SwingBugUtilities {
029:
030: private static Timer timer;
031:
032: /** Creates a new instance of SwingBugUtilities */
033: private SwingBugUtilities() {
034: }
035:
036: private static void createTimer() {
037: if (timer != null) {
038: return;
039: }
040: timer = new Timer(20, new ActionListener() {
041: public void actionPerformed(ActionEvent actionEvent) {
042: }
043: });
044: timer.setDelay(20);
045: timer.setInitialDelay(20);
046: timer.setCoalesce(true);
047: timer.setRepeats(true);
048: timer.start();
049: }
050:
051: public static boolean isTimerListening(ActionListener testListener) {
052: createTimer();
053: ActionListener[] listeners = timer.getActionListeners();
054: for (ActionListener candidate : listeners) {
055: if (candidate == testListener) {
056: return true;
057: }
058: }
059: return false;
060: }
061:
062: public static void addTimerListener(ActionListener newListener) {
063: if (!isTimerListening(newListener)) {
064: timer.addActionListener(newListener);
065: reportListeners();
066: }
067: }
068:
069: public static int getTimerCount() {
070: if (timer == null) {
071: return 0;
072: }
073: return timer.getActionListeners().length;
074: }
075:
076: public static void reportListeners() {
077: System.out.println(timer.getActionListeners().length);
078: }
079:
080: public static void removeTimerListener(ActionListener oldListener) {
081: timer.removeActionListener(oldListener);
082: reportListeners();
083: }
084:
085: /**
086: * Runs the supplied class after a certain period of time, the thread
087: * will be executed in the EDT.
088: *
089: * @param execute The runnable object whose method will be called after the
090: * specified delay
091: * @param after The delay in ms before the event will be called
092: */
093: public static Timer invokeAfter(final Runnable execute, int after) {
094: Timer timer = new Timer(after, new ActionListener() {
095: public void actionPerformed(ActionEvent actionEvent) {
096: execute.run();
097: }
098: });
099: timer.setRepeats(false);
100: timer.start();
101: return timer;
102: }
103:
104: private class TimerListener implements ActionListener {
105: public void actionPerformed(ActionEvent actionEvent) {
106: }
107:
108: }
109: }
|