//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.datamodel;
import com.nexirius.framework.textselector.TextSelector;
import com.nexirius.framework.textselector.TextSelectorManager;
import com.nexirius.util.CopyPairs;
/**
* This class is used to attach a selector functionality to any SimpleModel
* based on this the F1 button can be used to popup the associated TextSelector and RIGHT
* can be used to accept the currently displayed completion tip
* <PRE>
* // init and registerTemplate text selector
* String arr[] = {"title1", "title2"};
* SimpleArrayModel titleArray = new SimpleArrayModel(arr);
* TextSelectorManager.getInstance().addTextSelector(new DefaultTextSelector("mySelector", titleArray));
* //...
* private void init()
* {
* append(title = new StringModel("", "title"));
* append(titleSelector = new SelectorModel(title, "mySelector", "titleSelector")); // will be displayed as push button
* }
* </PRE>
*/
public class SelectorModel extends StructModel {
SimpleModel target;
String textSelectorName;
DefaultDataModelCommand popupCommand;
/**
* creates a SelectorModel instance
*
* @param target the model instance which will be extended with selection functionality
* @param textSelectorName the name of the text selector which manages he selectable values for the target
* @param fieldName the field Name of the associated selection command (use this name to lay out the selection button)
*/
public SelectorModel(SimpleModel target, String textSelectorName, String fieldName) {
super(fieldName);
this.target = target;
this.textSelectorName = textSelectorName;
init(fieldName);
}
private void init(String fieldName) {
target.addDataModelListener(new TargetListener());
setTransient(true);
popupCommand = new PopupCommand(fieldName);
appendMethod(popupCommand);
}
/**
* access the target data model
*/
public SimpleModel getTarget() {
return target;
}
/**
* activates the selector popup method
*/
public void popupSelectorModel() {
TextSelector textSelector = TextSelectorManager.getInstance().getTextSelector(textSelectorName);
if (textSelector == null) {
return;
}
if (textSelector.popup()) {
try {
target.setText(textSelector.getText());
} catch (Exception ex) {
//ignore
}
}
}
/**
* access the associated command
*/
public DataModelCommand getPopupCommand() {
return popupCommand;
}
/**
* access the name of the currently used text selector
*/
public String getTextSelectorName() {
return textSelectorName;
}
/**
* associate a new text selector
*/
public void setTextSelectorName(String textSelectorName) {
this.textSelectorName = textSelectorName;
}
/**
* access the current target text
*/
public String getText() {
return target.getText();
}
/**
* set the target text (also set the text of the associated text selector)
*/
public void setText(String s)
throws Exception {
try {
target.setText(s);
TextSelector textSelector = TextSelectorManager.getInstance().getTextSelector(textSelectorName);
if (textSelector != null) {
textSelector.setText(target.getText());
}
} catch (Exception ex) {
//ignore
}
}
public synchronized DataModel duplicate(DataModel instance, CopyPairs copyPairs) {
if (instance == null) {
instance = new SelectorModel(target, textSelectorName, getFieldName());
}
return super.duplicate(instance, copyPairs);
}
public class TargetListener extends DataModelAdaptor {
public void dataModelChangeValue(DataModelEvent ev) {
}
public void dataModelEdit(DataModelEvent ev) {
}
}
class PopupCommand extends DefaultDataModelCommand {
public PopupCommand(String commandName) {
super(commandName);
}
public void doAction() {
popupSelectorModel();
}
}
}