summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/tabs/outcome/form/field/ArrayEditField.java
blob: 742d1b4745a63d258759e01f5120d6793c30edd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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);
    }
    /**
     *
     */
    @Override
	public String getDefaultValue() {
        return "";
    }
    /**
     *
     */
    @Override
	public String getText() {
        return arrayModel.getData();
    }
    /**
     *
     */
    @Override
	public void setText(String text) {
        arrayModel.setData(text);
        arrayLabel.setText("Array ("+arrayModel.getArrayLength()+" values)");
    }
    /**
     *
     */
    @Override
	public Component getControl() {
        return arrayBox;
    }
    /**
     *
     */
    @Override
	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);
        }

    }

    /**
     *
     */
    @Override
	public JTextComponent makeTextField() {
        // not used by array
        return null;
    }
	@Override
	public void setEditable(boolean editable) {
		readOnly = !editable;
		arrayModel.setReadOnly(!readOnly);
	}

}