at.spardat.xma.mdl.simple.SimpleWMClient Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: SimpleWMClient.java 7142 2011-01-27 15:17:25Z hoenninger $
package at.spardat.xma.mdl.simple;
import at.spardat.xma.mdl.*;
import at.spardat.enterprise.fmt.*;
import at.spardat.xma.mdl.UIDelegateClient;
import at.spardat.xma.page.Page;
import at.spardat.xma.page.PageClient;
import at.spardat.xma.session.XMASessionClient;
import at.spardat.enterprise.util.Types;
/**
* Implementation of IWModelClient.
*/
public class SimpleWMClient extends SimpleWM implements ISimpleWMClient {
/**
* An optionally attached Formatter.
*/
private IFmt fmt_;
/**
* The UIDelegateClient of this. Never null.
*/
private SimpleUIDelegateClient ui_;
/**
* The Page this WidgetModel belongs to. Never null.
*/
private PageClient pageModel_;
/**
* Constructor
*
* @param id the unique id of a WidgetModel
* @param type a type constant of interface {@link at.spardat.enterprise.util.Types}.
* @param pm the Page this model belongs to
*/
public SimpleWMClient (short id, byte type, PageClient pm) {
super (id, type, pm);
ui_ = (SimpleUIDelegateClient) UIDelegateFactoryClient.getInstance(pm).newUIDelegateFor(this);
pageModel_ = pm;
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#isValueValid()
*/
public boolean isValueValid() {
if (fmt_ == null) return true;
else return fmt_.isLegalInternal(value_);
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#getFmt()
*/
public IFmt getFmt() {
if (fmt_ == null) return null;
return (IFmt) fmt_.clone();
}
/**
* Returns the formatter for internal, read-only access. May be null.
*/
protected IFmt getFmtInternal () {
return fmt_;
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#setFmt(at.spardat.enterprise.fmt.IFmt)
*/
public void setFmt (IFmt formatter) {
if (formatter != null) {
byte type = getType();
if (type != Types.T_STRING) {
if (!formatter.mayBeAppliedTo(type)) throw new IllegalArgumentException ("type incompatible formatter");
}
}
fmt_ = formatter;
handle (new FormatterChangedEvent());
}
/**
* The user of this class may forget to assign a correct formatter. This does not matter
* if the type is T_STRING, but with other types, we cannot exchange strings with the UI.
*
* @return true, if formatter state is correct and data may exchanged with the UI.
*/
public boolean doUITransfer () {
if (type_ == Types.T_STRING || fmt_ != null) return true;
return false;
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#getFormattedString()
*/
public String getFormattedString () {
if (!doUITransfer()) return "";
if (fmt_ == null) return value_;
else return fmt_.format(value_);
}
/**
* @return at.spardat.xma.mdl.client.UIDelegateClient
*/
public UIDelegateClient getUIDelegate() {
return ui_;
}
/**
* This method does not simply apply the ChangeEvent to this, but also notifies
* the UIDelegateClient to allow it to synchronize the UI.
*
* @see at.spardat.xma.mdl.WModel#handle(ModelChangeEvent)
*/
public boolean handle (ModelChangeEvent event) {
// the event may change the value of this and that may violate the
// formatter. So we save the old value
// String oldValue = value_;
// first, execute the ModelChangeEvent as usually
boolean success = event.execute();
if (!success) return false;
/**
* check if the new value is accepted by the Formatter;
* YSD, 20.8.2004: this check has been removed because it is to restrictive.
* For example, if the server sends a String of length 15 and the
* formatter at the client only accepts Strings at max length 10, the
* value would have been lost since it violates the client side formatter.
* The validity of the value with respect to the validator may be checked
* using isValueValid() anyway.
*/
// if (!isValueValid()) {
// // restore to old value
// setInternal (oldValue);
// return false;
// }
// on success, update the UIDelegateClient
if (!event.isFromUI()) {
// but only if the event does not emenate from the UI itself
ui_.handleModelChangeEvent(event);
}
return true;
}
/**
* Returns the PageClient this WidgetModelC belongs to.
*
* @return PageClient, never null.
*/
public PageClient getPageModelC () {
return pageModel_;
}
/**
* @see at.spardat.xma.mdl.IWModelClient#isEditable()
*/
public boolean isEditable() {
return ui_.isEditable();
}
/**
* @see at.spardat.xma.mdl.IWModelClient#isEnabled()
*/
public boolean isEnabled() {
return ui_.isEnabled();
}
/**
* @see at.spardat.xma.mdl.IWModelClient#setEditable(boolean)
*/
public void setEditable(boolean what) {
ui_.setEditable(what);
}
/**
* @see at.spardat.xma.mdl.IWModelClient#setEnabled(boolean)
*/
public void setEnabled(boolean what) {
ui_.setEnabled(what);
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#isMandatory()
*/
public boolean isMandatory () {
if (fmt_ == null) return false;
return fmt_.isMandatory();
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#setMandatory(boolean)
*/
public void setMandatory (boolean what) {
if (fmt_ == null) {
if(what==false) return;
fmt_ = AStringFmt.getInstance(-1); // create an unrestricted validator
}
boolean oldMand = fmt_.isMandatory();
if(oldMand!=what) {
fmt_.setMandatory(what);
if(isEditable()) {
ui_.UI2Model();
} else if(isValidateIfUneditable()) {
ui_.updateErrorState();
}
}
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#setValidateIfUneditable(boolean)
*
* @param what
* @since version_number
* @author s3460
*/
public void setValidateIfUneditable(boolean what){
ui_.setValidateIfUneditable(what);
}
/**
* @see at.spardat.xma.mdl.simple.ISimpleWMClient#isValidateIfUneditable()
*
* @return the validateUneditable property
* @since version_number
* @author s3460
*/
public boolean isValidateIfUneditable(){
return ui_.isValidateIfUneditable();
}
/**
* Indicates that the formatter has been changed programmatically
*/
class FormatterChangedEvent extends Notification {
public FormatterChangedEvent () {
super (SimpleWMClient.this, false);
}
}
/**
* @see at.spardat.xma.mdl.simple.SimpleWM#settersShouldBeSloppy(Page)
*/
protected boolean settersShouldBeSloppy (Page page) {
XMASessionClient session = ((PageClient)page).getComponent().getSession();
if(session!=null) { // null only in junit tests
return "true".equals(session.getRuntimeProperty("sloppyModelSetters", "false"));
} else {
return false;
}
}
/**
* Event class used to notify the dynamic registration of a new SimpleWMClient.
* @author gub
* @since 2.1.0
* @see Page#addWModel(WModel)
*/
public static class NewSimpleWMClientEvent extends NewSimpleWMEvent {
/** empty contructor for deserialization */
public NewSimpleWMClientEvent() {}
/**
* constructor which initializes the modelType
* @param type must be one of the type constants defined in {@link Types}
*/
public NewSimpleWMClientEvent(byte type) {
super(type);
}
// see at.spardat.xma.mdl.NewModelEvent.createModel()
public WModel createModel(short id, Page page) {
return new SimpleWMClient(id,modelType,(PageClient) page);
}
}
// see at.sparda.xma.mdl.WModel.createNewModelEvent()
public NewModelEvent createNewModelEvent() {
return new NewSimpleWMClientEvent(type_);
}
}