SWTCommandBuilder.java :  » Database-Client » dbmjui » fr » aliacom » form » swt » Java Open Source

Java Open Source » Database Client » dbmjui 
dbmjui » fr » aliacom » form » swt » SWTCommandBuilder.java
/*
 * Created on May 5, 2003
 *
 * Dbmjui is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *  
 * Dbmjui is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public
 * License along with dbmjui; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */
package fr.aliacom.form.swt;

import java.lang.reflect.Constructor;

import org.w3c.dom.Element;

import fr.aliacom.commands.Command;
import fr.aliacom.commands.CommandPool;
import fr.aliacom.form.common.FormContext;
import fr.aliacom.form.common.IForm;
import fr.aliacom.form.common.events.CommandEvent;
import fr.aliacom.form.common.events.ICommandListener;
import fr.aliacom.form.swt.events.CommandSelectionListener;

/**
 * @author tom
 *
 * (c) 2001, 2003 Thomas Cataldo
 */
public abstract class SWTCommandBuilder extends SWTBuilder {

  protected void initFromPythonAction(
    IItem item,
    Element elem,
    IForm form,
    FormContext ctx) {
    item.setText(getNodeText(elem));
    addListener(
      item,
      CommandPool.getInstance().getCommand(
        form,
        ctx,
        elem.getAttribute("pythonAction")));
  }
  
  protected void initFromJavaAction(IItem item, Element elem, IForm form) {
    Command c = null;
    try {
      Class klass = Class.forName(elem.getAttribute("javaAction"));
      Constructor ctor =
        klass.getConstructor(new Class[] { IForm.class });
      c = (Command) ctor.newInstance(new Object[] { form });
    } catch (Exception e) {
      log.fatal("Error while loading command ", e);
      return;
    }
    item.setText(getNodeText(elem));
    addListener(item, c);
  }

  private void addListener(final IItem item, Command c) {
    // execute command on click
    item.addSelectionListener(new CommandSelectionListener(c));
    // disable the item while the command runs...
    c.addCommandListener(new ICommandListener() {
      public void stateChanged(CommandEvent ce) {
        switch (ce.getState()) {
          case Command.STATE_ENABLED :
            item.setEnabled(true);
            break;
          case Command.STATE_DISABLED :
            item.setEnabled(false);
            break;
          default :
            break;
        }
      }
    });
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.