views:

436

answers:

3

Hi, I'm building an web intra-net application which has the need for a flashing/blinking button (this has been discussed in depth and is our design decision so please do not lecture me about it). Our UI framework is GWT.
Currently in the prototype this effect is achieved be scheduling a timer which adds or removes a css style thus creating the effect. (The timer can be as fast as having 200ms scheduled repeating).
This design contains a few problems for me:
1. Leans very heavily on a specific css heirarchy and thus is problematic to reproduce this behaviour in different widgets of the application.
2. I think it will not scale nicely when I have 10 buttons which all run these timers on the same page.
3. Very uncomfortable to debug.

I've spent quite a few hours trying to investigate GWT libraries offring this ability but I have not really found one that I am satisfied with.
Basically what I'm looking for is advice regarding specific GWT libraries or maybe ideas how this effect can be implemented more wisely.

Thanks in advance

+1  A: 

Have you considered using the CSS text-decoration: blink?

Otherwise you can create a timer wich switches the button style attribute, someting like that:

   blinkTimer = new Timer()
    {
        @Override
        public void run()
        {
            changeStyle();
        }
    };

    blinkTimer.scheduleRepeating(1000);


...

public void changeStyle()
{
   for (Button b: blinkButtons)
   {
        if (b.getStyleName().equals(blinkOn)
               b.setStyleName(blinkOff);
           else
               b.setStyleName(blinkOn;        
    }
}
Drejc
Hi Drejc, The timer and styles method is what we're using right now (I've written it above) but could you elaborate more on the blink attribute? Is it supported by IE6?Thanks
Ittai
"Note: The "blink" value is not supported in IE, Chrome, or Safari." http://www.w3schools.com/Css/pr_text_text-decoration.asp
Igor Klimer
As said by @Igor blink will only for FF. I don't see any other solution than using a timer. If you only use one at a time, your application will not suffer from it. Make sure that only the active page switches the styles. Plus make sure you have only one timer for all buttons used (as in my given example). You don't need a timer for each button.
Drejc
A: 

Nice post, give a sample output flash.

chinathambi
Hi, What do you mean by sample output flash?
Ittai
A: 

Hi, If you're concerned about reusability you could write a wrapper class for a UIObject and have a timer add/remove a dependent style name. (Note I wrote the code below in the browser so it is probably not perfect but should give you an idea of what I mean).


public class BlinkAnimation {

  private UIObject obj;

  private Timer timer;

  private int interval, iteration, stopIter;

  private boolean blink;

  public BlinkAnimation(UIObject obj) {
    this(obj, 200);
  }

  public BlinkAnimation(Element elm, int interval) {
    this.obj = obj;
    this.interval = interval;

    timer = new Timer() {
      @Override
      public void run() {
        if (!blink) {
          obj.addStyleDependentName("blink");
          iteration++;
        } else {
          obj.removeStyleDependentName("blink");
          if (iteration == stopIter) {
            timer.cancel();
          }
        }
        blink = !blink;
      }
    };
  }

  public void animate() {
    anmiate(-1);
  }

  public void animate(int numTimes) {
    iteration = 0;
    stopIter = numTimes;
    blink = false;
    timer.scheduleRepeating(interval);
  }

  public void stop() {
    timer.cancel();
  }
}

// Usage
Widget widget = ....
BlinkAnimation ohTheHumanity = new BlinkAnimation(widget);
ohTheHumanity.animate();

Then for different element types you can style them differently


/* Normal widget styles */
.my-Widget {
  /* ... */
  border:none;
  /* ... */
}

.my-Widget-blink {
  border:1px solid red;
}
pgraham