diff options
Diffstat (limited to 'source/com/c2kernel/gui/tabs/outcome/form/field')
11 files changed, 1179 insertions, 0 deletions
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayEditField.java new file mode 100755 index 0000000..e7dc8bc --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayEditField.java @@ -0,0 +1,166 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.Box;
+import javax.swing.JButton;
+import javax.swing.JLabel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.text.JTextComponent;
+
+import org.exolab.castor.xml.schema.SimpleType;
+
+import com.c2kernel.utils.Language;
+
+/**************************************************************************
+ *
+ * $Revision: 1.7 $
+ * $Date: 2006/05/24 07:51:51 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+
+public class ArrayEditField extends StringEditField implements ActionListener {
+
+ Box arrayBox;
+ Box expandBox;
+ Box editBox;
+ JScrollPane arrayView;
+ JButton arrayButton;
+ JButton expandButton;
+ JButton contractButton;
+ JButton addButton;
+ JButton removeButton;
+ ArrayTableModel arrayModel;
+ JLabel arrayLabel = new JLabel("Array");
+ boolean panelShown = false;
+ boolean readOnly = false;
+
+ public ArrayEditField(SimpleType type) {
+ arrayBox = Box.createVerticalBox();
+ arrayBox.add(arrayLabel);
+ arrayButton = new JButton(Language.translate("Show"));
+ arrayButton.addActionListener(this);
+ arrayButton.setActionCommand("toggle");
+ arrayBox.add(arrayButton);
+
+ expandBox = Box.createHorizontalBox();
+ expandButton = new JButton(">>");
+ expandButton.setToolTipText("Increase the number of columns displaying this array");
+ expandButton.addActionListener(this);
+ expandButton.setActionCommand("extend");
+
+ contractButton = new JButton("<<");
+ contractButton.setToolTipText("Decrease the number of columns displaying this array");
+ contractButton.addActionListener(this);
+ contractButton.setActionCommand("contract");
+
+ expandBox.add(contractButton);
+ expandBox.add(Box.createHorizontalGlue());
+ expandBox.add(expandButton);
+
+ arrayModel = new ArrayTableModel(type);
+ if (arrayModel.getColumnCount() < 2) contractButton.setEnabled(false);
+ arrayView = new JScrollPane(new JTable(arrayModel));
+
+ editBox = Box.createHorizontalBox();
+ addButton = new JButton("+");
+ addButton.setToolTipText("Add a field to the end of this array");
+ addButton.addActionListener(this);
+ addButton.setActionCommand("add");
+ removeButton = new JButton("-");
+ removeButton.setToolTipText("Remove the last field from this array");
+ removeButton.addActionListener(this);
+ removeButton.setActionCommand("remove");
+ editBox.add(addButton);
+ editBox.add(Box.createHorizontalGlue());
+ editBox.add(removeButton);
+ }
+ /**
+ *
+ */
+ public String getDefaultValue() {
+ return "";
+ }
+ /**
+ *
+ */
+ public String getText() {
+ return arrayModel.getData();
+ }
+ /**
+ *
+ */
+ public void setText(String text) {
+ arrayModel.setData(text);
+ arrayLabel.setText("Array ("+arrayModel.getArrayLength()+" values)");
+ }
+ /**
+ *
+ */
+ public Component getControl() {
+ return arrayBox;
+ }
+ /**
+ *
+ */
+ public void actionPerformed(ActionEvent e) {
+ if (e.getActionCommand().equals("toggle")) {
+ arrayBox.removeAll();
+ if (panelShown) {
+ arrayBox.add(arrayLabel);
+ arrayBox.add(Box.createVerticalStrut(7));
+ arrayBox.add(arrayButton);
+ arrayButton.setText("Show");
+ }
+ else {
+ arrayBox.add(arrayLabel);
+ arrayBox.add(Box.createVerticalStrut(7));
+ arrayBox.add(arrayButton);
+ arrayBox.add(Box.createVerticalStrut(7));
+ arrayBox.add(expandBox);
+ arrayBox.add(Box.createVerticalStrut(7));
+ arrayBox.add(arrayView);
+ if (!readOnly) arrayBox.add(editBox);
+ arrayButton.setText("Hide");
+ }
+ panelShown = !panelShown;
+ arrayBox.validate();
+ }
+ else if (e.getActionCommand().equals("add")) {
+ arrayModel.addField();
+ arrayLabel.setText("Array ("+arrayModel.getArrayLength()+" values)");
+ }
+ else if (e.getActionCommand().equals("remove")) {
+ arrayModel.removeField();
+ arrayLabel.setText("Array ("+arrayModel.getArrayLength()+" values)");
+ }
+ else {
+ int currentCols = arrayModel.getColumnCount();
+ if (e.getActionCommand().equals("extend"))
+ currentCols++;
+ else if (e.getActionCommand().equals("contract"))
+ currentCols--;
+ arrayModel.setColumnCount(currentCols);
+ contractButton.setEnabled(currentCols > 1);
+ }
+
+ }
+
+ /**
+ *
+ */
+ public JTextComponent makeTextField() {
+ // not used by array
+ return null;
+ }
+ public void setEditable(boolean editable) {
+ readOnly = !editable;
+ arrayModel.setReadOnly(!readOnly);
+ }
+
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayTableModel.java b/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayTableModel.java new file mode 100755 index 0000000..0807f78 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayTableModel.java @@ -0,0 +1,106 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+
+import javax.swing.table.AbstractTableModel;
+
+import org.exolab.castor.xml.schema.SimpleType;
+
+import com.c2kernel.gui.tabs.outcome.form.OutcomeStructure;
+import com.c2kernel.utils.Language;
+
+/**************************************************************************
+ *
+ * $Revision: 1.2 $
+ * $Date: 2006/05/24 07:51:53 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+
+public class ArrayTableModel extends AbstractTableModel {
+
+ ArrayList contents = new ArrayList();
+ Class type;
+ int numCols = 1;
+ boolean readOnly = false;
+
+ public ArrayTableModel(SimpleType type) {
+ super();
+ this.type = OutcomeStructure.getJavaClass(type.getTypeCode());
+ }
+
+ public void setReadOnly(boolean readOnly) {
+ this.readOnly = readOnly;
+ }
+
+ public void setData(String data) {
+ contents.clear();
+ StringTokenizer tok = new StringTokenizer(data);
+ while(tok.hasMoreTokens())
+ contents.add(OutcomeStructure.getTypedValue(tok.nextToken(), type));
+ fireTableStructureChanged();
+ }
+
+ public String getData() {
+ if (contents.size() == 0) return "";
+ Iterator iter = contents.iterator();
+ StringBuffer result = new StringBuffer(iter.next().toString());
+ while (iter.hasNext())
+ result.append(" ").append(iter.next().toString());
+ return result.toString();
+ }
+
+ public void addField() {
+ contents.add(OutcomeStructure.getTypedValue("", type));
+ fireTableStructureChanged();
+ }
+
+ public void removeField() {
+ contents.remove(contents.size()-1);
+ fireTableStructureChanged();
+ }
+
+ public Class getColumnClass(int columnIndex) {
+ return type;
+ }
+
+ public int getColumnCount() {
+ return numCols;
+ }
+
+ public int getArrayLength() {
+ return contents.size();
+ }
+
+ public void setColumnCount(int newCols) {
+ numCols = newCols;
+ fireTableStructureChanged();
+ }
+
+ public String getColumnName(int column) {
+ return Language.translate("Value");
+ }
+
+ public int getRowCount() {
+ return (contents.size()/numCols)+1;
+ }
+
+ public Object getValueAt(int arg0, int arg1) {
+ int index = arg1+(arg0 * numCols);
+ if (index >= contents.size())
+ return null;
+ return contents.get(arg1+(arg0 * numCols));
+ }
+
+ public boolean isCellEditable(int rowIndex, int columnIndex) {
+ if (columnIndex+(rowIndex*numCols) > contents.size()-1) return false;
+ return !readOnly;
+ }
+
+ public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
+ contents.set(columnIndex+(rowIndex*numCols), aValue);
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/BooleanEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/BooleanEditField.java new file mode 100755 index 0000000..5776f73 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/BooleanEditField.java @@ -0,0 +1,68 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Component;
+import java.awt.event.FocusEvent;
+
+import javax.swing.JCheckBox;
+import javax.swing.text.JTextComponent;
+
+import com.c2kernel.utils.Logger;
+
+/**************************************************************************
+ *
+ * $Revision: 1.7 $
+ * $Date: 2005/08/16 13:59:56 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+public class BooleanEditField extends StringEditField {
+
+ JCheckBox checkbox;
+
+ public BooleanEditField() {
+ checkbox = new JCheckBox();
+ checkbox.setSelected(false);
+ checkbox.addFocusListener(this);
+ }
+
+ public String getText() {
+ return String.valueOf(checkbox.isSelected());
+ }
+
+ public void setText(String text) {
+ boolean newState = false;
+ try {
+ newState = Boolean.valueOf(text).booleanValue();
+ } catch (Exception ex) {
+ Logger.error("Invalid value for checkbox: "+text);
+ }
+ checkbox.setSelected(newState);
+ }
+
+ public void setEditable(boolean editable) {
+ super.setEditable(editable);
+ checkbox.setEnabled(editable);
+ }
+
+ public Component getControl() {
+ return checkbox;
+ }
+
+ public String getDefaultValue() {
+ return "false";
+ }
+
+ /** don't reserve the item finder for a boolean */
+ public void focusGained(FocusEvent e) {
+ helpPane.setHelp(name, helpText);
+ }
+
+ /**
+ *
+ */
+ public JTextComponent makeTextField() {
+ // not used by boolean
+ return null;
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/ComboField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/ComboField.java new file mode 100755 index 0000000..2a0b34a --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/ComboField.java @@ -0,0 +1,135 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Component;
+import java.util.Enumeration;
+import java.util.StringTokenizer;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.text.JTextComponent;
+
+import org.exolab.castor.types.AnyNode;
+import org.exolab.castor.xml.schema.AttributeDecl;
+import org.exolab.castor.xml.schema.ElementDecl;
+import org.exolab.castor.xml.schema.Facet;
+import org.exolab.castor.xml.schema.SimpleType;
+
+import com.c2kernel.gui.tabs.outcome.form.StructuralException;
+import com.c2kernel.scripting.Script;
+import com.c2kernel.utils.Logger;
+
+/*******************************************************************************
+ *
+ * $Revision: 1.4 $ $Date: 2005/08/16 13:59:56 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research All
+ * rights reserved.
+ ******************************************************************************/
+
+public class ComboField extends StringEditField {
+
+ JComboBox comboField;
+ ListOfValues vals;
+ DefaultComboBoxModel comboModel;
+ AnyNode listNode;
+
+ public ComboField(SimpleType type, AnyNode listNode) {
+ super();
+ comboField = new JComboBox();
+ content = type;
+ this.listNode = listNode;
+ createLOV();
+ }
+
+ public String getDefaultValue() {
+ if (vals.getDefaultKey() != null)
+ return vals.get(vals.getDefaultKey()).toString();
+ else
+ return "";
+ }
+
+ public String getText() {
+ return vals.get(comboModel.getSelectedItem()).toString();
+ }
+
+ public JTextComponent makeTextField() {
+ // not used by this control
+ return null;
+ }
+
+ public void setText(String text) {
+ comboModel.setSelectedItem(text);
+ }
+
+ public Component getControl() {
+ return comboField;
+ }
+
+ private void createLOV() {
+ vals = new ListOfValues();
+
+ if (listNode != null) { // schema instructions for list building
+ String lovType = listNode.getLocalName();
+ String param = listNode.getFirstChild().getStringValue();
+ if (lovType.equals("ScriptList"))
+ populateLOVFromScript(param);
+ if (lovType.equals("PathList"))
+ populateLOVFromLDAP(param);
+ }
+
+ // handle enumerations
+ // TODO: should be ANDed with above results
+ if (content.hasFacet(Facet.ENUMERATION)) {
+ //ListOfValues andList = new ListOfValues();
+ Enumeration enums = content.getFacets(Facet.ENUMERATION);
+ while (enums.hasMoreElements()) {
+ Facet thisEnum = (Facet)enums.nextElement();
+ vals.put(thisEnum.getValue(), thisEnum.getValue(), false);
+ }
+ }
+
+ comboModel = new DefaultComboBoxModel(vals.keySet().toArray());
+ comboModel.setSelectedItem(vals.getDefaultKey());
+ comboField.setModel(comboModel);
+ }
+
+ /**
+ * @param param
+ */
+ private void populateLOVFromLDAP(String param) {
+ // TODO '/root/path;prop=val;prop=val'
+
+
+ }
+
+ private void populateLOVFromScript(String scriptName) {
+ try {
+ StringTokenizer tok = new StringTokenizer(scriptName, "_");
+ if (tok.countTokens() != 2)
+ throw new Exception("Invalid LOVScript name");
+ Script lovscript = new Script(tok.nextToken(), Integer.parseInt(tok.nextToken()));
+ lovscript.setInputParamValue("LOV", vals);
+ lovscript.execute();
+ } catch (Exception ex) {
+ Logger.exceptionDialog(ex);
+ }
+ }
+
+ public void setDecl(AttributeDecl model) throws StructuralException {
+ super.setDecl(model);
+ createLOV();
+ }
+
+ public void setDecl(ElementDecl model) throws StructuralException {
+ super.setDecl(model);
+ createLOV();
+ }
+
+ /**
+ *
+ */
+
+ public void setEditable(boolean editable) {
+ comboField.setEditable(editable);
+ }
+}
\ No newline at end of file diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/DecimalEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/DecimalEditField.java new file mode 100755 index 0000000..d77dff3 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/DecimalEditField.java @@ -0,0 +1,115 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Toolkit;
+import java.math.BigDecimal;
+
+import javax.swing.JTextField;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import javax.swing.text.PlainDocument;
+
+/**************************************************************************
+ *
+ * $Revision: 1.3 $
+ * $Date: 2005/08/16 13:59:56 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+public class DecimalEditField extends StringEditField {
+
+ public DecimalEditField() {
+ super();
+ field.addFocusListener(this);
+ field.setToolTipText("This field must contains a decimal number e.g. 3.14159265");
+ }
+
+ public String getText() {
+ return field.getText();
+ }
+
+ public void setText(String text) {
+ field.setText(text);
+ }
+
+ public String getDefaultValue() {
+ return "0.0";
+ }
+
+ public JTextComponent makeTextField() {
+ return new DecimalTextField();
+ }
+
+ private class DecimalTextField extends JTextField {
+
+ public DecimalTextField() {
+ super();
+ setHorizontalAlignment(RIGHT);
+ }
+ protected Document createDefaultModel() {
+ return new Decimal();
+ }
+ }
+
+ private class Decimal extends PlainDocument {
+
+ BigDecimal currentVal = new BigDecimal(0.0);
+
+
+ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
+
+ if (str == null || str.equals("")) {
+ return;
+ }
+
+ String proposedResult = null;
+
+ if (getLength() == 0) {
+ proposedResult = str;
+ } else {
+ StringBuffer currentBuffer = new StringBuffer( this.getText(0, getLength()) );
+ currentBuffer.insert(offs, str);
+ proposedResult = currentBuffer.toString();
+ }
+
+ try {
+ currentVal = parse(proposedResult);
+ super.insertString(offs, str, a);
+ } catch (Exception e) {
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ }
+
+ public void remove(int offs, int len) throws BadLocationException {
+
+ String currentText = this.getText(0, getLength());
+ String beforeOffset = currentText.substring(0, offs);
+ String afterOffset = currentText.substring(len + offs, currentText.length());
+ String proposedResult = beforeOffset + afterOffset;
+
+ if (proposedResult.length() == 0) { // empty is ok
+ super.remove(offs, len);
+ return;
+ }
+ try {
+ currentVal = parse(proposedResult);
+ super.remove(offs, len);
+ } catch (Exception e) {
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ }
+
+ public BigDecimal parse(String proposedResult) throws NumberFormatException {
+
+ BigDecimal value = new BigDecimal(0);
+ if ( proposedResult.length() != 0) {
+ value = new BigDecimal(proposedResult);
+ }
+ return value;
+ }
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/FieldConstraints.java b/source/com/c2kernel/gui/tabs/outcome/form/field/FieldConstraints.java new file mode 100755 index 0000000..d09cd91 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/FieldConstraints.java @@ -0,0 +1,51 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.util.Enumeration;
+
+import org.exolab.castor.types.AnyNode;
+import org.exolab.castor.xml.schema.Annotation;
+import org.exolab.castor.xml.schema.AppInfo;
+import org.exolab.castor.xml.schema.XMLType;
+
+/**************************************************************************
+ *
+ * $Revision: 1.1 $
+ * $Date: 2005/04/26 06:48:12 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+
+public class FieldConstraints {
+
+ XMLType content;
+ ListOfValues lov;
+ int rows = 1;
+
+ public FieldConstraints(XMLType content) {
+ this.content = content;
+ Enumeration e = content.getAnnotations();
+ while (e.hasMoreElements()) {
+ Annotation note = (Annotation)e.nextElement();
+ for (Enumeration f = note.getAppInfo(); f.hasMoreElements();) {
+ addAppInfo((AppInfo)f.nextElement());
+ }
+ }
+ }
+
+ private void addAppInfo(AppInfo element) {
+ Enumeration e = element.getObjects();
+ while (e.hasMoreElements()) {
+ AnyNode node = (AnyNode)e.nextElement();
+
+ }
+ }
+
+ public ListOfValues getLOV() {
+ return lov;
+ }
+
+ public int getRows() {
+ return rows;
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/ImageEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/ImageEditField.java new file mode 100755 index 0000000..b0bb079 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/ImageEditField.java @@ -0,0 +1,104 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Component;
+import java.awt.Toolkit;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.FileInputStream;
+import java.lang.reflect.Array;
+
+import javax.swing.Box;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JFileChooser;
+import javax.swing.JLabel;
+
+import org.apache.xerces.impl.dv.util.Base64;
+
+import com.c2kernel.utils.Logger;
+
+public class ImageEditField extends StringEditField {
+
+ JLabel imageLabel;
+
+ Box imagePanel;
+
+ JButton browseButton;
+
+ String encodedImage;
+
+ static JFileChooser chooser = new JFileChooser();
+ static {
+ chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
+ public String getDescription() {
+ return "Image Files";
+ }
+
+ public boolean accept(File f) {
+ return (f.isDirectory() || (f.isFile() && (f.getName()
+ .endsWith(".gif")
+ || f.getName().endsWith(".jpg")
+ || f.getName().endsWith(".jpeg")
+ || f.getName().endsWith(".png"))));
+ }
+ });
+ }
+
+ public ImageEditField() {
+ super();
+ imageLabel = new JLabel();
+ imagePanel = Box.createVerticalBox();
+ browseButton = new JButton("Browse");
+ browseButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ int returnVal = chooser.showOpenDialog(null);
+ if (returnVal == JFileChooser.APPROVE_OPTION) {
+ File file = chooser.getSelectedFile();
+ try {
+ FileInputStream fis = new FileInputStream(file);
+ byte[] bArray = (byte[]) Array.newInstance(byte.class,
+ (int) file.length());
+ fis.read(bArray, 0, (int) file.length());
+ fis.close();
+
+ ImageIcon newImage = new ImageIcon(Toolkit
+ .getDefaultToolkit().createImage(bArray));
+ imageLabel.setIcon(newImage);
+ encodedImage = Base64.encode(bArray);
+ } catch (Exception ex) {
+ Logger.exceptionDialog(ex);
+ }
+ }
+ }
+ });
+ imagePanel.add(imageLabel);
+ imagePanel.add(Box.createVerticalStrut(5));
+ imagePanel.add(browseButton);
+ }
+
+ public String getDefaultValue() {
+ return "";
+ }
+
+ public Component getControl() {
+ return imagePanel;
+ }
+
+ public String getText() {
+ return encodedImage == null ? "" : encodedImage;
+ }
+
+ public void setText(String text) {
+ encodedImage = text;
+ if (text != null && text.length() > 0) {
+ byte[] decodedImage = Base64.decode(encodedImage);
+ imageLabel.setIcon(new ImageIcon(Toolkit.getDefaultToolkit()
+ .createImage(decodedImage)));
+ }
+ }
+
+ public void setEditable(boolean editable) {
+ browseButton.setVisible(false);
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/IntegerEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/IntegerEditField.java new file mode 100755 index 0000000..7c858a4 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/IntegerEditField.java @@ -0,0 +1,113 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Toolkit;
+import java.math.BigInteger;
+
+import javax.swing.JTextField;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import javax.swing.text.PlainDocument;
+
+/**************************************************************************
+ *
+ * $Revision: 1.4 $
+ * $Date: 2005/08/16 13:59:56 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+public class IntegerEditField extends StringEditField {
+
+ public IntegerEditField() {
+ super();
+ field.setToolTipText("This field must contains a whole number e.g. 3");
+ }
+
+ public String getText() {
+ return field.getText();
+ }
+
+ public void setText(String text) {
+ field.setText(text);
+ }
+
+ public String getDefaultValue() {
+ return "0";
+ }
+
+ public JTextComponent makeTextField() {
+ return new IntegerTextField();
+ }
+
+ private class IntegerTextField extends JTextField {
+
+ public IntegerTextField() {
+ super();
+ setHorizontalAlignment(RIGHT);
+ }
+ protected Document createDefaultModel() {
+ return new IntegerDocument();
+ }
+ }
+
+ private class IntegerDocument extends PlainDocument {
+
+ BigInteger currentVal = new BigInteger("0");
+
+ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
+
+ if (str == null || str.equals("")) {
+ return;
+ }
+
+ String proposedResult = null;
+
+ if (getLength() == 0) {
+ proposedResult = str;
+ } else {
+ StringBuffer currentBuffer = new StringBuffer( this.getText(0, getLength()) );
+ currentBuffer.insert(offs, str);
+ proposedResult = currentBuffer.toString();
+ }
+
+ try {
+ currentVal = parse(proposedResult);
+ super.insertString(offs, str, a);
+ } catch (Exception e) {
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ }
+
+ public void remove(int offs, int len) throws BadLocationException {
+
+ String currentText = this.getText(0, getLength());
+ String beforeOffset = currentText.substring(0, offs);
+ String afterOffset = currentText.substring(len + offs, currentText.length());
+ String proposedResult = beforeOffset + afterOffset;
+ if (proposedResult.length() == 0) { // empty is ok
+ super.remove(offs, len);
+ return;
+ }
+
+ try {
+ currentVal = parse(proposedResult);
+ super.remove(offs, len);
+ } catch (Exception e) {
+ Toolkit.getDefaultToolkit().beep();
+ }
+
+ }
+
+ public BigInteger parse(String proposedResult) throws NumberFormatException {
+
+ BigInteger value = new BigInteger("0");
+ if ( proposedResult.length() != 0) {
+ value = new BigInteger(proposedResult);
+ }
+ return value;
+ }
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/ListOfValues.java b/source/com/c2kernel/gui/tabs/outcome/form/field/ListOfValues.java new file mode 100755 index 0000000..2557cbe --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/ListOfValues.java @@ -0,0 +1,31 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.util.HashMap;
+
+/**************************************************************************
+ *
+ * $Revision: 1.2 $
+ * $Date: 2005/04/26 06:48:12 $
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+
+public class ListOfValues extends HashMap {
+
+ String defaultKey = null;
+
+ public ListOfValues() {
+ super();
+ }
+
+ public String put(String key, String value, boolean isDefaultKey) {
+ if (isDefaultKey) defaultKey = key;
+ return (String)super.put(key, value);
+ }
+
+ public String getDefaultKey() {
+ return defaultKey;
+ }
+
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/LongStringEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/LongStringEditField.java new file mode 100755 index 0000000..b782c65 --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/LongStringEditField.java @@ -0,0 +1,38 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+
+import java.awt.Component;
+
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.text.JTextComponent;
+
+import com.c2kernel.utils.Language;
+
+
+/**************************************************************************
+ *
+ * $Revision$
+ * $Date$
+ *
+ * Copyright (C) 2003 CERN - European Organization for Nuclear Research
+ * All rights reserved.
+ **************************************************************************/
+public class LongStringEditField extends StringEditField {
+
+ JTextArea bigText;
+ JScrollPane bigScroller;
+ public LongStringEditField() {
+ super();
+ field.setToolTipText(Language.translate("This field can contain any string."));
+ }
+
+ public JTextComponent makeTextField() {
+ return new JTextArea();
+ }
+ public Component getControl() {
+ if (bigScroller == null) {
+ bigScroller = new JScrollPane(field);
+ }
+ return bigScroller;
+ }
+}
diff --git a/source/com/c2kernel/gui/tabs/outcome/form/field/StringEditField.java b/source/com/c2kernel/gui/tabs/outcome/form/field/StringEditField.java new file mode 100755 index 0000000..310ee2e --- /dev/null +++ b/source/com/c2kernel/gui/tabs/outcome/form/field/StringEditField.java @@ -0,0 +1,252 @@ +package com.c2kernel.gui.tabs.outcome.form.field;
+import java.awt.Component;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Enumeration;
+
+import javax.swing.ImageIcon;
+import javax.swing.JTextField;
+import javax.swing.text.JTextComponent;
+
+import org.exolab.castor.types.AnyNode;
+import org.exolab.castor.xml.schema.Annotation;
+import org.exolab.castor.xml.schema.AppInfo;
+import org.exolab.castor.xml.schema.AttributeDecl;
+import org.exolab.castor.xml.schema.ElementDecl;
+import org.exolab.castor.xml.schema.Facet;
+import org.exolab.castor.xml.schema.SimpleType;
+import org.exolab.castor.xml.schema.Structure;
+import org.exolab.castor.xml.schema.XMLType;
+import org.exolab.castor.xml.schema.simpletypes.ListType;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+import com.c2kernel.gui.DomainKeyConsumer;
+import com.c2kernel.gui.MainFrame;
+import com.c2kernel.gui.tabs.outcome.OutcomeException;
+import com.c2kernel.gui.tabs.outcome.form.HelpPane;
+import com.c2kernel.gui.tabs.outcome.form.OutcomeStructure;
+import com.c2kernel.gui.tabs.outcome.form.StructuralException;
+import com.c2kernel.lookup.DomainPath;
+
+/** Superclass for the entry field for Field and AttributeList.
+ */
+public class StringEditField implements FocusListener, DomainKeyConsumer {
+
+ Node data;
+ Structure model;
+ protected SimpleType content;
+ HelpPane helpPane;
+ String helpText;
+ protected JTextComponent field;
+
+ boolean isValid = true;
+ boolean editable = true;
+ String name;
+
+
+ public StringEditField() {
+ field = makeTextField();
+ if (field != null)
+ field.addFocusListener(this);
+ }
+
+ private static StringEditField getFieldForType(SimpleType type) {
+ // handle lists special
+ if (type instanceof ListType)
+ return new ArrayEditField(type.getBuiltInBaseType());
+
+ // is a combobox
+ if (type.hasFacet(Facet.ENUMERATION))
+ return new ComboField(type, null);
+ //find LOVscript
+ Enumeration e = type.getAnnotations();
+ while (e.hasMoreElements()) {
+ Annotation note = (Annotation)e.nextElement();
+ for (Enumeration f = note.getAppInfo(); f.hasMoreElements();) {
+ AppInfo thisAppInfo = (AppInfo)f.nextElement();
+ for (Enumeration g = thisAppInfo.getObjects(); g.hasMoreElements();) {
+ AnyNode appInfoNode = (AnyNode)g.nextElement();
+ if (appInfoNode.getLocalName().equals("ScriptList")
+ || appInfoNode.getLocalName().equals("LDAPList")) {
+ return new ComboField(type, appInfoNode);
+ }
+ }
+ }
+ }
+ // find info on length before we go to the base type
+ long length = -1;
+ if (type.getLength()!=null) length = type.getLength().longValue();
+ else if (type.getMaxLength()!=null) length = type.getMaxLength().longValue();
+ else if (type.getMinLength()!=null) length = type.getMinLength().longValue();
+
+ // find base type if derived
+ if (!(type.isBuiltInType()))
+ type = type.getBuiltInBaseType();
+ // else derive the class
+ Class contentClass = OutcomeStructure.getJavaClass(type.getTypeCode());
+ // disable list edits for the moment
+ if (contentClass.equals(Boolean.class))
+ return new BooleanEditField();
+ else if (contentClass.equals(BigInteger.class))
+ return new IntegerEditField();
+ else if (contentClass.equals(BigDecimal.class))
+ return new DecimalEditField();
+ else if (contentClass.equals(ImageIcon.class))
+ return new ImageEditField();
+ else if (length > 60)
+ return new LongStringEditField();
+ else return new StringEditField();
+ }
+
+ public static StringEditField getEditField(AttributeDecl model) throws StructuralException {
+ StringEditField newField = getFieldForType(model.getSimpleType());
+ newField.setDecl(model);
+ return newField;
+ }
+
+ public static StringEditField getEditField(ElementDecl model) throws StructuralException {
+ try {
+ XMLType baseType = model.getType();
+ while (!(baseType instanceof SimpleType))
+ baseType = baseType.getBaseType();
+ StringEditField newField = getFieldForType((SimpleType)baseType);
+ newField.setDecl(model);
+ return newField;
+ } catch (Exception ex) {
+ throw new StructuralException("No type defined in model");
+ }
+ }
+
+ public void setDecl(AttributeDecl model) throws StructuralException {
+ this.model=model;
+ this.content=model.getSimpleType();
+ this.name = model.getName();
+ if (model.isFixed()) setEditable(false);
+ }
+
+ public void setDecl(ElementDecl model) throws StructuralException {
+ this.model=model;
+ this.name = model.getName();
+ XMLType type = model.getType();
+
+ // derive base type
+ if (type.isSimpleType())
+ this.content = (SimpleType)type;
+ else
+ this.content = (SimpleType)(type.getBaseType());
+
+ if (this.content == null) throw new StructuralException("No declared base type of element");
+
+ //
+ if (model.getFixedValue() != null) setEditable(false);
+
+ }
+
+ public void setData(Attr newData) throws StructuralException {
+ if (!(newData.getName().equals(name)))
+ throw new StructuralException("Tried to add a "+newData.getName()+" into a "+name+" attribute.");
+
+ this.data = newData;
+ setText(newData.getValue());
+ }
+
+ public void setData(Text newData) {
+ String contents = newData.getData();
+ this.data = newData;
+ setText(contents);
+ }
+
+ public void setData(String newData) throws OutcomeException {
+ if (data == null) throw new OutcomeException("No node exists");
+ setText(newData);
+ updateNode();
+
+ }
+
+ public Structure getModel() {
+ return model;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Node getData() {
+ return data;
+ }
+
+ public String getDefaultValue() {
+ return "";
+ }
+
+ public void setHelp(HelpPane helpPane, String helpText) {
+ this.helpPane = helpPane;
+ this.helpText = helpText;
+ }
+
+ public void focusLost(FocusEvent e) {
+ if (MainFrame.itemFinder != null)
+ MainFrame.itemFinder.clearConsumer(this);
+ updateNode();
+ }
+
+ public void focusGained(FocusEvent e) {
+ helpPane.setHelp(name, helpText);
+ if (editable && MainFrame.itemFinder != null)
+ MainFrame.itemFinder.setConsumer(this, "Insert");
+ }
+
+ public void updateNode() {
+ if (data == null) return;
+ if (data instanceof Text) {
+ ((Text)data).setData(getText());
+ }
+ else { //attribute
+ ((Attr)data).setValue(getText());
+ }
+ }
+
+ /**
+ * Read domkey from barcode input
+ */
+ public void push(DomainPath key) {
+ setText(key.getName());
+ }
+
+ /**
+ * Read string from barcode input
+ */
+ public void push(String key) {
+ setText(key);
+ }
+
+ public void setEditable(boolean editable) {
+ this.editable = editable;
+ if (field != null)
+ field.setEditable(editable);
+ }
+
+ public String getText() {
+ return field.getText();
+ }
+
+ public void setText(String text) {
+ field.setText(text);
+ }
+
+ public JTextComponent makeTextField() {
+ return new JTextField();
+ }
+
+ public Component getControl() {
+ return field;
+ }
+
+ public void grabFocus() {
+ getControl().requestFocus();
+ }
+}
|
