/*
* Created on 2004/10/07
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.swt.ui;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Vector;
import org.apache.commons.beanutils.MethodUtils;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.jpn.xucker.commons.property.StatusChangeListener;
import org.jpn.xucker.commons.property.StatusChangeMonitor;
/**
*
*
*/
public class SWTStatusChangedMonitor implements StatusChangeMonitor {
private boolean changed;
private List enabler = new Vector();
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#addSelectableItem(java.lang.Object)
*/
private UpdateControler updateControler;
private ChangeControler changeControler;
private StatusChangeListener listener;
public SWTStatusChangedMonitor() {
updateControler = new UpdateControler();
changeControler = new ChangeControler();
}
public void addSelectableItem(Object object) {
try {
MethodUtils.invokeMethod(object, "addSelectionListener",
changeControler);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
throw new RuntimeException("not support method");
}
}
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#addModifiableItem(java.lang.Object)
*/
public void addModifiableItem(Object object) {
try {
MethodUtils.invokeMethod(object, "addModifyListener",
changeControler);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
throw new RuntimeException("not support method");
}
}
public class UpdateControler implements SelectionListener {
/*
* (non-Javadoc)
*
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent e) {
updatePreference();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
}
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#addUpdateControlItem(java.lang.Object)
*/
public void addUpdateControlItem(Object object) {
try {
MethodUtils.invokeMethod(object, "addSelectionListener",
updateControler);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
throw new RuntimeException("not support method");
}
}
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#addEnableControlItem(java.lang.Object)
*/
public void addEnableControlItem(Object object) {
// TODO Auto-generated method stub
enabler.add(object);
}
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#updatePreference()
*/
public void updatePreference() {
if(listener!=null){
listener.statusChanged(changed);
}
setChanged(false);
}
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#setStatusChangeListener(org.jpn.xucker.commons.util.StatusChangeListener)
*/
public void setStatusChangeListener(StatusChangeListener listener) {
this.listener=listener;
}
/*
* (non-Javadoc)
*
* @see org.jpn.xucker.commons.util.StatusChangeMonitor#getStatusChangeListener()
*/
public StatusChangeListener getStatusChangeListener() {
// TODO Auto-generated method stub
return null;
}
public class ChangeControler implements SelectionListener,ModifyListener{
/*
* (non-Javadoc)
*
* @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
*/
public void modifyText(ModifyEvent e) {
setChanged(true);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent e) {
setChanged(true);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
}
public boolean isChanged() {
return changed;
}
public void setChanged(boolean changed) {
this.changed = changed;
Object items[]=enabler.toArray();
for (int i = 0; i < items.length; i++) {
try {
MethodUtils.invokeMethod(items[i], "setEnabled",
Boolean.valueOf(changed));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
throw new RuntimeException("not support method");
}
}
}
}
|