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); } }