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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
package com.c2kernel.gui.tabs.outcome.form;
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.table.JTableHeader;
import org.exolab.castor.xml.schema.ElementDecl;
import org.exolab.castor.xml.schema.Particle;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.c2kernel.gui.DomainKeyConsumer;
import com.c2kernel.gui.MainFrame;
import com.c2kernel.gui.tabs.outcome.OutcomeException;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.utils.Logger;
public class Dimension extends OutcomeStructure implements ActionListener {
DimensionTableModel tableModel;
Element parent;
GridBagConstraints position;
GridBagLayout gridbag;
JTabbedPane tabs;
JLabel msg;
DomKeyPushTable table;
Box tableBox;
ArrayList instances = new ArrayList(); // stores DimensionInstances if tabs
ArrayList elements = new ArrayList(); // stores current children
JButton addButton;
JButton delButton;
short mode;
protected static final short TABLE = 1;
protected static final short TABS = 2;
public Dimension(ElementDecl model, boolean readOnly, HelpPane help) {
super(model, readOnly, help);
// set up panel
gridbag = new java.awt.GridBagLayout();
setLayout(gridbag);
position = new GridBagConstraints();
position.anchor = GridBagConstraints.NORTHWEST;
position.fill = GridBagConstraints.HORIZONTAL;
position.weightx = 1.0; position.weighty = 0.0;
position.gridx = 0; position.gridy = 0;
position.ipadx = 0; position.ipady = 0;
position.insets = new Insets(0,0,0,0);
// TODO: an element or attribute of the dimension can be flagged as an index, so it can be used as a title for a tab
// set up the border
setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), model.getName()));
msg = new JLabel("No elements");
msg.setFont(new Font("SansSerif", Font.ITALIC, msg.getFont().getSize()));
gridbag.setConstraints(msg, position);
add(msg);
position.gridy++;
// decide whether a table or tabs
try {
tableModel = new DimensionTableModel(model, readOnly);
Logger.msg(8, "DIM "+model.getName()+" - Will be a table");
mode = TABLE;
tableBox = Box.createVerticalBox();
table = new DomKeyPushTable(tableModel, this);
new MultiLinePasteAdapter(table, this);
if (readOnly) table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setColumnSelectionAllowed(readOnly);
JTableHeader tableHeader = table.getTableHeader();
tableHeader.setReorderingAllowed(false);
tableBox.add(tableHeader);
tableBox.add(table);
gridbag.setConstraints(tableBox, position);
add(tableBox);
tableBox.setVisible(false);
} catch (StructuralException e) {
// use tabs
Logger.msg(8, "DIM "+model.getName()+" - Will be tabs: "+e.getMessage());
mode = TABS;
tabs = new JTabbedPane();
gridbag.setConstraints(tabs, position);
add(tabs);
tabs.setVisible(false);
}
if (!readOnly) {
JPanel rowAdjust = new JPanel(new FlowLayout());
addButton = new JButton("+");
addButton.setActionCommand("add");
addButton.addActionListener(this);
rowAdjust.add(addButton);
delButton = new JButton("-");
delButton.setActionCommand("del");
delButton.addActionListener(this);
delButton.setEnabled(false);
rowAdjust.add(delButton);
position.gridy++; position.weighty=0; position.weightx=0;
gridbag.setConstraints(rowAdjust, position);
this.add(rowAdjust);
}
}
public void setParentElement(Element parent) {
this.parent = parent;
}
public void addInstance(Element myElement, Document parentDoc) throws OutcomeException {
if (Logger.doLog(6))
Logger.msg(6, "DIM - adding instance "+ (elements.size()+1) +" for "+myElement.getTagName());
if (parent == null) setParentElement((Element)myElement.getParentNode());
// if table, pass to table model
if (mode == TABLE) {
tableModel.addInstance(myElement, -1);
elements.add(myElement);
}
else {
DimensionInstance target;
elements.add(myElement);
if (instances.size() < elements.size())
target = newInstance();
else
target = (DimensionInstance)instances.get(elements.size()-1);
target.addInstance(myElement, parentDoc);
}
checkButtons();
}
public int getChildCount() {
return elements.size();
}
public DimensionInstance newInstance() {
DimensionInstance newInstance = null;
try {
newInstance = new DimensionInstance(model, readOnly, helpPane, deferChild);
instances.add(newInstance);
newInstance.setTabNumber(instances.size());
newInstance.setParent(this);
deferChild = true;
tabs.addTab(newInstance.getName(), newInstance);
tabs.addChangeListener(newInstance);
} catch (OutcomeException e) {
// shouldn't happen, we've already done it once
Logger.error(e);
}
return newInstance;
}
public String validateStructure() {
if (mode == TABLE)
return table.validateStructure();
else {
StringBuffer errors = new StringBuffer();
for (Iterator iter = instances.iterator(); iter.hasNext();) {
OutcomeStructure element = (OutcomeStructure)iter.next();
errors.append(element.validateStructure());
}
return errors.toString();
}
}
public void checkButtons() {
// check if data visible
boolean dataVisible = elements.size() > 0;
if (mode == TABS) tabs.setVisible(dataVisible);
else tableBox.setVisible(dataVisible);
msg.setVisible(!dataVisible);
if (readOnly) return;
if (elements.size() <= model.getMinOccurs() || elements.size() == 0) {
delButton.setEnabled(false);
delButton.setToolTipText("Minimum row count of "+model.getMinOccurs()+" reached.");
} else {
delButton.setEnabled(true);
delButton.setToolTipText(null);
}
if (elements.size() < model.getMaxOccurs() || model.getMaxOccurs() == Particle.UNBOUNDED) {
addButton.setEnabled(true);
addButton.setToolTipText(null);
} else {
addButton.setEnabled(false);
addButton.setToolTipText("Maximum row count of "+model.getMaxOccurs()+" reached.");
}
}
public Element initNew(Document parent) {
Element newElement;
if (mode == TABLE) {
newElement = tableModel.initNew(parent, -1);
elements.add(newElement);
checkButtons();
return newElement;
}
else {
DimensionInstance newTab = null;
if (instances.size() < elements.size()+1)
newTab = newInstance();
else
newTab = (DimensionInstance)instances.get(elements.size()-1);
newElement = newTab.initNew(parent);
elements.add(newElement);
checkButtons();
return newElement;
}
}
public void actionPerformed(ActionEvent e) {
int index;
if (mode == TABS) index = tabs.getSelectedIndex();
else {
index = table.getSelectedRow();
if (index == -1) index = tableModel.getRowCount();
}
try {
if (table == null || table.getCellEditor() == null || table.getCellEditor().stopCellEditing()) {
if (e.getActionCommand().equals("add"))
addRow(index);
else if (e.getActionCommand().equals("del"))
removeRow(index);
}
} catch (CardinalException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Table error", JOptionPane.ERROR_MESSAGE);
}
}
public void addRow(int index) throws CardinalException {
if (elements.size() == model.getMaxOccurs())
throw new CardinalException("Maximum size of table reached");
if (mode == TABLE) {
Element newRow = tableModel.initNew(parent.getOwnerDocument(), index);
elements.add(index, newRow);
try {
Element following = (Element)elements.get(index+1);
parent.insertBefore(newRow, following);
} catch (IndexOutOfBoundsException ex) {
parent.appendChild(newRow);
}
table.clearSelection();
table.setRowSelectionInterval(index, index);
}
else {
Element newTab = initNew(parent.getOwnerDocument());
parent.appendChild(newTab);
}
checkButtons();
}
public void removeRow(int index) throws CardinalException {
if (elements.size() <= model.getMinOccurs())
throw new CardinalException("Minimum size of table reached");
if (mode == TABLE) {
parent.removeChild(tableModel.removeRow(index));
int selectRow = index;
if (index >= tableModel.getRowCount()) selectRow--;
if (tableModel.getRowCount() > 0) {
table.clearSelection();
table.setRowSelectionInterval(selectRow, selectRow);
}
}
else {
Element elementToGo = (Element)elements.get(index);
parent.removeChild(elementToGo);
instances.remove(index);
tabs.remove(index);
for (int i = index; i<instances.size(); i++) {
DimensionInstance thisInstance = (DimensionInstance)instances.get(i);
thisInstance.setTabNumber(i+1);
tabs.setTitleAt(i, thisInstance.getName());
}
}
elements.remove(index);
checkButtons();
}
private class DomKeyPushTable extends JTable implements DomainKeyConsumer, FocusListener {
Dimension dim;
public DomKeyPushTable(DimensionTableModel model, Dimension parent) {
super(model);
addFocusListener(this);
this.dim = parent;
}
public void push(DomainPath key) {
push(key.getName());
}
public void push(String name) {
int col = getSelectedColumn();
int row = getSelectedRow();
if (cellEditor != null)
cellEditor.stopCellEditing();
Logger.msg(8, "Pushing "+name+" to table at "+row+","+col);
if (col > -1 && row > -1) {
if (dataModel.getValueAt(row, col).toString().length()==0)
dataModel.setValueAt(name, row, col);
else {
if (row+1 == getRowCount()) {
try {
dim.addRow(row+1);
dataModel.setValueAt(name, row+1, col);
} catch (CardinalException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Table error", JOptionPane.ERROR_MESSAGE);
}
}
}
if (row+1 < getRowCount()) {
Logger.msg(8, "Shifting selection to row "+(row+1));
changeSelection(row+1, col, false, false);
}
}
}
public void focusGained(FocusEvent e) {
if (!readOnly)
MainFrame.itemFinder.setConsumer(this, "Insert");
}
public void focusLost(FocusEvent e) {
// release the itemFinder
if (!readOnly)
MainFrame.itemFinder.clearConsumer(this);
}
public String validateStructure() {
if (cellEditor != null)
cellEditor.stopCellEditing();
return null;
}
public void changeSelection( int rowIndex, int columnIndex, boolean toggle, boolean extend) {
super.changeSelection(rowIndex, columnIndex, toggle, extend);
DimensionTableModel dimModel = (DimensionTableModel)dataModel;
helpPane.setHelp(dimModel.getColumnName(columnIndex), dimModel.getHelp(columnIndex));
}
}
public void grabFocus() {
if (mode == TABLE) {
if (table.getSelectedRow() == -1 && table.getRowCount() > 0) {
table.changeSelection(0, 0, false, false);
table.editCellAt(0,0);
}
table.requestFocus();
}
else if (instances.size()> 0)
((DimensionInstance)instances.get(0)).grabFocus();
}
}
|