Multi Form Button Binding

I have to break the form into 2 columns that need to use a separate form panels.
But FormButtonBinding not support more than 1 form panels. So I decided to write a new one based on GXT FormButtonBinding

package com.vcari.client;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.Timer;

/**
 * Monitors the valid state of a form and enabled / disabled all buttons.
 */
public class MultiFormButtonBinding {

  // private FormPanel panel;
  private List<FormPanel> panels = new ArrayList<FormPanel>();
  private Timer timer;
  private int interval = 500;
  private Listener<ComponentEvent> listener;
  private List<Button> buttons;

  public MultiFormButtonBinding(List<FormPanel> panels) {
    this.panels = panels;

    buttons = new ArrayList<Button>();
    timer = new Timer() {
      @Override
      public void run() {
        MultiFormButtonBinding.this.checkPanel();
      }
    };
    listener = new Listener<ComponentEvent>() {
      public void handleEvent(ComponentEvent be) {
        if (be.getType() == Events.Attach) {
          MultiFormButtonBinding.this.startMonitoring();
        } else if (be.getType() == Events.Detach) {
          MultiFormButtonBinding.this.stopMonitoring();
        }
      }
    };

    for (FormPanel formPanel : panels) {
      formPanel.addListener(Events.Attach, listener);
      formPanel.addListener(Events.Detach, listener);
      if (formPanel.isAttached()) {
        startMonitoring();
      }
    }

    // panel.addListener(Events.Attach, listener);
    // panel.addListener(Events.Detach, listener);

  }

  public void addButton(Button button) {
    buttons.add(button);
  }

  public int getInterval() {
    return interval;
  }

  public void removeButton(Button button) {
    buttons.remove(button);
  }

  public void setInterval(int interval) {
    this.interval = interval;
  }

  public void startMonitoring() {
    for (FormPanel formPanel : panels) {
      if (formPanel.isAttached()) {
        timer.run();
        timer.scheduleRepeating(interval);
        break;
      }      
    }    
  }

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

  protected boolean checkPanel() {
    boolean v=true;
    for (FormPanel formPanel : panels) {
      boolean v1 = formPanel.isValid(true);
      v=v && v1;
      
    }  
    
    for (Button button : buttons) {
      if (v != button.isEnabled()) {
        button.setEnabled(v);
      }
    }
    return v;
  }

}

Example :

List<FormPanel> panels=new ArrayList<FormPanel>();
panels.add(form);
panels.add(form2);
MultiFormButtonBinding buttonBinding=new MultiFormButtonBinding(panels);
buttonBinding.addButton(btnSubmit);

0 comments:

 
Copyright © peyotest