//{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.textselector;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.datamodel.*;
import com.nexirius.util.StringVector;
/**
* This is the default implementation to handle a SelectorModel based on DataModel selection
* It containes a DataModel which is created which createModel() and which is used as a search attribute holder
* and a selectionArray which is used to select a specific model
* It contains a command (name "selectorModelAdd") which is used to add the currrent search attribute model to the selectionArray
* <PRE>
* //Example:
* ArrayModel addressArray = new AddressArrayModel();
* TextSelectorManager.getInstance().addTextSelector(new AddressSelector("AddressSelector", addressArray));
* //...
* StringModel addressId = new StringModel("", "AddressId"));
* SelectorModel selector = new SelectorModel(addressId, "AddressSelector", "selector")); // will be displayed as push button
* //...
* class AddressSelector extends DefaultModelSelector {
* AddressSelector(String name, AddressArrayModel arr)
* throws Exception
* {
* super(name, arr);
* }
* <p/>
* public DataModel createModel()
* {
* return new AddressModel();
* }
* <p/>
* public String getIdFieldName()
* {
* return "ID";
* }
* }
* <p/>
* class AddressModel extends StuctModel
* {
* AddressModel()
* {
* super("Address");
* append(new StingModel("", "ID")); // primary key
* append(new StingModel("", "Name"));
* append(new StingModel("", "Address"));
* }
* }
* <p/>
* class AddressArrayModel extends ArrayModel
* {
* AddressArrayModel()
* {
* super(new AddressModel().getFullFieldName(), "AddressArray");
* }
* }
* </PRE>
*/
public abstract class DefaultModelSelector extends StructModel implements ModelSelector {
public static int maxCompletionList = 10;
DataModel model;
ArrayModel selectionArray;
DefaultDataModelCommand addCommand;
int minCharsForPopup = 2;
public DefaultModelSelector(String name, ArrayModel selectionArray)
throws Exception {
super(name);
this.selectionArray = selectionArray;
init();
}
/**
* get the name which is used to reference this unique instance via TextSelectorManager
*/
public String getName() {
return getFieldName();
}
public void addText(String text) {
// no implementation
}
/**
* calls getCompletionFor(getIdFieldName(), s);
*
* @param s the string pattern which is used to find the datamodel instance
* @return the data model as a String where all the attributes are separated with ';' characters
*/
public String getCompletionFor(String s) {
DataModel m = getCompletionFor(getIdFieldName(), s);
StringBuffer ret = new StringBuffer();
ret.append(m.getChildText(getIdFieldName()));
ret.append(';');
DataModelEnumeration e = model.getEnumeration();
while (e.hasMore()) {
DataModel child = e.next();
String fieldName = child.getFieldName();
if (!getIdFieldName().equals(fieldName)) {
ret.append(m.getChildText(fieldName));
ret.append(';');
}
}
return ret.toString();
}
/**
* calls getCompletionFor(getIdFieldName(), s, arrayModel);
*
* @param s the string pattern which is used to find the datamodel instance
* @param result this string vector will be filled with the found models (each model is translated into a string with ';' between the attribute values)
* @return true if there are more than the maxCompletionList hits
*/
public boolean getCompletionListFor(String s, StringVector result) {
int i = 0;
ArrayModel arr = new ArrayModel(null);
boolean ret = getCompletionListFor(getIdFieldName(), s, arr);
DataModelEnumeration enumVar = arr.getEnumeration();
while (enumVar.hasMore()) {
DataModel m = enumVar.next();
DataModelEnumeration e = model.getEnumeration();
StringBuffer buf = new StringBuffer();
buf.append(m.getChildText(getIdFieldName()));
buf.append(';');
while (e.hasMore()) {
DataModel child = e.next();
String fieldName = child.getFieldName();
if (!getIdFieldName().equals(fieldName)) {
buf.append(m.getChildText(fieldName));
buf.append(';');
}
}
result.append(buf.toString());
if (i >= maxCompletionList) {
return enumVar.hasMore();
}
++i;
}
return ret;
}
/**
* get the actually selected model as a String (with attribute values separated with ';'.
* If no model is selected then the search attribute model will be returned
*/
public String getText() {
DataModel m = getModel();
if (m == null) {
return null;
}
return m.getChildText(getIdFieldName());
}
/**
* calls setChildText(getIdFieldName(), text) on the search attribute model
*
* @param text the text which will be written to the search attribute model field getIdFieldName()
*/
public void setText(String text) {
model.clear();
model.setChildText(getIdFieldName(), text);
}
/**
* redefine this method to represent different status models
*
* @return a newly created array status model instance
*/
public abstract DataModel createModel();
/**
* access the fieldname of the field which holds the primary key
*
* @return a String representing the field name of the primary key field of the array status models
*/
public abstract String getIdFieldName();
/**
* registerTemplate new entry (must call setModel(model))
*/
public synchronized void addModel(DataModel model) {
setModel(model);
selectorModelAdd();
}
/**
* get a possible completion for a given string
*
* @param fieldName the field name of the field which is used as a search pattern
* @param s the search pattern
* @return the actual possible completion or null if none of the entries starts with the given string
*/
public DataModel getCompletionFor(String fieldName, String s) {
if (s == null) {
return null;
}
model.clear();
model.setChildText(fieldName, s);
return selectionArray.getHighlightedItem();
}
/**
* get all possible completions for a given string
*
* @param fieldName the field name of the field which is used as a search pattern
* @param s the search pattern
* @param result the resulting list of strings will be appended to this list
* @return true if there are more than the returned number of possible completions
*/
public boolean getCompletionListFor(String fieldName, String s, ArrayModel result) {
if (s == null) {
return false;
}
model.clear();
model.setChildText(fieldName, s);
if (selectionArray.getHighlightedItem() == null) {
return false;
}
boolean found = false;
int i = 0;
DataModelEnumeration e = selectionArray.getEnumeration();
while (e.hasMore()) {
DataModel item = e.next();
if (item.getChildText(fieldName).startsWith(s)) {
found = true;
if (i >= maxCompletionList) {
return true;
}
result.append(item.duplicate(null, null));
++i;
} else if (found) {
// the vector is sorted so the loop can be left here
return false;
}
}
return false;
}
/**
* access the model which has been selected. If no model is selected then the search attributes model will be returned
*/
public DataModel getModel() {
DataModel m = selectionArray.getHighlightedItem();
if (m == null) {
m = model;
}
return m;
}
/**
* set the search attributes model
*/
public void setModel(DataModel model) {
try {
this.model.dropData(model.dragData());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* popup a dialog which is used to edit the actual text which will be selected
*
* @return true if selection has been performed
*/
public boolean popup() {
DialogManager.getPopupEditorAdaptor().noDuplicatePopupEdit(this, null, null);
return true;
}
private void init()
throws Exception {
append(model = createModel());
append(selectionArray);
model.addDataModelListener(new MyModelListener());
selectionArray.addDataModelListener(new SelectorListener());
addCommand = new DefaultDataModelCommand("selectorModelAdd");
addCommand.setSynchronous(true);
appendMethod(addCommand);
}
/**
* enables or disabled the add command (if the primary key field of the search attribute model is already defined
* in an status, then the add command will be disabled
*/
public synchronized void update() {
String s = model.getChildText(getIdFieldName());
if (s != null && s.length() >= 1) {
DataModel item = selectionArray.getHighlightedItem();
if (item != null) {
addCommand.setEnabled(!s.equals(item.getChildText(getIdFieldName())));//FIX
} else {
addCommand.setEnabled(true);
}
} else {
addCommand.setEnabled(false);
}
}
/**
* adds a new status to the selection array
*/
public synchronized void selectorModelAdd() {
DataModel newElement = createModel();
if (model.getChildText(getIdFieldName()).length() == 0) {
return;
}
try {
newElement.dropData(model.dragData());
selectionArray.sortInsert(newElement);
setModel(newElement);
} catch (Exception ex) {
//ignore
}
update();
}
public class SelectorListener extends DataModelAdaptor {
public void dataModelEdit(DataModelEvent ev) {
}
}
public void updateHighlight() {
StringVector fieldNames = new StringVector();
StringVector fieldValues = new StringVector();
DataModelEnumeration e = model.getEnumeration();
while (e.hasMore()) {
DataModel child = e.next();
String fieldName = child.getFieldName();
String fieldValue = model.getChildText(fieldName);
if (fieldValue != null && fieldValue.length() > 0) {
fieldNames.append(fieldName);
fieldValues.append(fieldValue);
}
}
boolean found = false;
int i = 0;
if (fieldNames.size() > 0) {
e = selectionArray.getEnumeration();
while (e.hasMore()) {
DataModel item = e.next();
String fieldValue = fieldValues.firstItem();
boolean hit = true;
for (String fieldName = fieldNames.firstItem(); fieldName != null; fieldName = fieldNames.nextItem()) {
if (!item.getChildText(fieldName).startsWith(fieldValue)) {
hit = false;
break;
}
fieldValue = fieldValues.nextItem();
}
if (hit) {
found = true;
break;
}
++i;
}
}
if (found) {
selectionArray.setHighlightedItem(i);
} else {
selectionArray.setHighlightedItem(-1);
}
}
public class MyModelListener extends DataModelAdaptor {
public void dataModelChangeValue(DataModelEvent ev) {
updateHighlight();
update();
}
public void dataModelEdit(DataModelEvent ev) {
if (ev.getId() == DataModelEvent.VALUE_EDIT) {
updateHighlight();
update();
} else if (ev.getId() == DataModelEvent.START_EDIT) {
updateHighlight();
update();
} else if (ev.getId() == DataModelEvent.FINISH_EDIT) {
update();
}
}
}
/**
* @return
*/
public int getMinCharsForPopup() {
return minCharsForPopup;
}
/**
* @param i
*/
public void setMinCharsForPopup(int i) {
minCharsForPopup = i;
}
}