Dépôt officiel du code source de l'ERP OpenConcerto
Rev 86 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.openconcerto.modules.extensionbuilder.table;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openconcerto.erp.config.ComptaPropsConfiguration;
import org.openconcerto.modules.extensionbuilder.Extension;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.ui.DefaultGridBagConstraints;
public class FieldDescriptorEditor extends JPanel implements ActionListener {
final JComboBox comboType;
private JTextField fieldName;
private JLabel labelOption;
private JTextField textOption;
private String[] types = { "Texte", "Nombre entier", "Nombre décimal", "Booléen", "Date", "Heure", "Date et heure", "Référence" };
private String[] xmltypes = { "string", "integer", "decimal", "boolean", "date", "time", "dateAndTime", "ref" };
private JComboBox comboOption;
private static final String[] vBoolean = { "oui", "non" };
private static final String[] vDate = { "vide", "jour actuel" };
private static final String[] vTime = { "vide", "heure actuelle" };
private static final String[] vDateTime = { "vide", "date actuelle" };
private static final int TYPE_STRING = 0;
private static final int TYPE_INTEGER = 1;
private static final int TYPE_DECIMAL = 2;
private static final int TYPE_BOOLEAN = 3;
private static final int TYPE_DATE = 4;
private static final int TYPE_TIME = 5;
private static final int TYPE_DATE_TIME = 6;
private static final int TYPE_REF = 7;
FieldDescriptor fd;
private Extension extension;
FieldDescriptorEditor(Extension extension, FieldDescriptor fd) {
this.extension = extension;
this.fd = fd;
this.setLayout(new GridBagLayout());
GridBagConstraints c = new DefaultGridBagConstraints();
this.add(new JLabel("Type"), c);
c.gridx++;
this.comboType = new JComboBox(this.types);
this.comboType.setOpaque(false);
this.add(this.comboType, c);
c.gridx++;
this.add(new JLabel("Nom"), c);
c.gridx++;
c.weightx = 1;
this.fieldName = new JTextField(10);
this.add(this.fieldName, c);
c.weightx = 0;
c.gridx++;
this.labelOption = new JLabel("Longeur max");
this.add(this.labelOption, c);
c.gridx++;
this.textOption = new JTextField(6);
c.gridx++;
this.add(this.textOption, c);
this.comboOption = new JComboBox();
this.add(this.comboOption, c);
updateFrom(fd);
this.comboType.addActionListener(this);
this.fieldName.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
fieldNameModified();
}
@Override
public void insertUpdate(DocumentEvent e) {
fieldNameModified();
}
@Override
public void changedUpdate(DocumentEvent e) {
fieldNameModified();
}
});
this.comboOption.addActionListener(this);
this.textOption.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
fieldOptionModified();
}
@Override
public void insertUpdate(DocumentEvent e) {
fieldOptionModified();
}
@Override
public void changedUpdate(DocumentEvent e) {
fieldOptionModified();
}
});
}
protected void fieldNameModified() {
final String text = this.fieldName.getText();
if (text.trim().length() > 0) {
this.fd.setName(text);
}
this.extension.setChanged();
}
protected void fieldOptionModified() {
String text = this.textOption.getText();
if (text.trim().length() > 0) {
switch (this.comboType.getSelectedIndex()) {
case TYPE_STRING:
this.fd.setLength(text);
this.fd.setDefaultValue(null);
this.fd.setForeignTable(null);
this.fd.setLink(null);
break;
case TYPE_INTEGER:
this.fd.setLength(null);
try {
int i = Integer.parseInt(text);
this.fd.setDefaultValue(text);
} catch (Exception e) {
this.fd.setDefaultValue("0");
}
this.fd.setForeignTable(null);
this.fd.setLink(null);
case TYPE_DECIMAL:
this.fd.setLength(null);
try {
text = text.replace(',', '.');
float i = Float.parseFloat(text);
this.fd.setDefaultValue(text);
} catch (Exception e) {
this.fd.setDefaultValue("0");
}
this.fd.setForeignTable(null);
this.fd.setLink(null);
break;
}
this.extension.setChanged();
}
}
protected void comboOptionModified() {
final int index = this.comboOption.getSelectedIndex();
switch (this.comboType.getSelectedIndex()) {
case TYPE_BOOLEAN:
this.fd.setLength(null);
if (index == 0) {
this.fd.setDefaultValue("true");
} else {
this.fd.setDefaultValue("false");
}
this.fd.setForeignTable(null);
this.fd.setLink(null);
break;
case TYPE_DATE:
case TYPE_TIME:
case TYPE_DATE_TIME:
this.fd.setLength(null);
if (index == 0) {
this.fd.setDefaultValue(null);
} else {
this.fd.setDefaultValue("now");
}
this.fd.setForeignTable(null);
this.fd.setLink(null);
break;
case TYPE_REF:
this.fd.setLength(null);
this.fd.setDefaultValue(null);
this.fd.setForeignTable(this.comboOption.getSelectedItem().toString());
this.fd.setLink(null);
break;
}
this.extension.setChanged();
}
private void updateFrom(FieldDescriptor fd) {
if (fd.getType().equals("string")) {
this.comboType.setSelectedIndex(TYPE_STRING);
this.labelOption.setText("Longueur max");
this.textOption.setVisible(true);
this.textOption.setText(fd.getLength());
this.comboOption.setVisible(false);
} else if (fd.getType().equals("integer")) {
this.comboType.setSelectedIndex(TYPE_INTEGER);
this.labelOption.setText("Valeur par défaut");
this.textOption.setVisible(true);
this.textOption.setText(fd.getDefaultValue());
this.comboOption.setVisible(false);
} else if (fd.getType().equals("decimal")) {
this.comboType.setSelectedIndex(TYPE_DECIMAL);
this.labelOption.setText("Valeur par défaut");
this.textOption.setVisible(true);
this.textOption.setText(fd.getDefaultValue());
this.comboOption.setVisible(false);
} else if (fd.getType().equals("boolean")) {
this.comboType.setSelectedIndex(TYPE_BOOLEAN);
this.labelOption.setText("Valeur par défaut");
this.textOption.setVisible(false);
this.comboOption.setVisible(true);
this.comboOption.setModel(new DefaultComboBoxModel(vBoolean));
if (fd.getDefaultValue().equals("true")) {
this.comboOption.setSelectedIndex(0);
} else {
this.comboOption.setSelectedIndex(1);
}
} else if (fd.getType().equals("date")) {
this.comboType.setSelectedIndex(TYPE_DATE);
this.labelOption.setText("Valeur par défaut");
this.textOption.setVisible(false);
this.comboOption.setVisible(true);
this.comboOption.setModel(new DefaultComboBoxModel(vDate));
if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
this.comboOption.setSelectedIndex(0);
} else {
this.comboOption.setSelectedIndex(1);
}
} else if (fd.getType().equals("time")) {
this.comboType.setSelectedIndex(TYPE_TIME);
this.labelOption.setText("Valeur par défaut");
this.textOption.setVisible(false);
this.comboOption.setVisible(true);
this.comboOption.setModel(new DefaultComboBoxModel(vTime));
if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
comboOption.setSelectedIndex(0);
} else {
comboOption.setSelectedIndex(1);
}
} else if (fd.getType().equals("dateAndTime")) {
comboType.setSelectedIndex(TYPE_DATE_TIME);
labelOption.setText("Valeur par défaut");
textOption.setVisible(false);
comboOption.setVisible(true);
comboOption.setModel(new DefaultComboBoxModel(vDateTime));
if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
comboOption.setSelectedIndex(0);
} else {
comboOption.setSelectedIndex(1);
}
} else if (fd.getType().equals("ref")) {
comboType.setSelectedIndex(TYPE_REF);
labelOption.setText("Table");
textOption.setVisible(false);
comboOption.setVisible(true);
final Vector<String> tables = new Vector<String>();
tables.addAll(Arrays.asList(getTables()));
String tableName = fd.getForeignTable();
if (!tables.contains(tableName)) {
tables.add(tableName);
}
comboOption.setModel(new DefaultComboBoxModel<String>(tables));
comboOption.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (value.toString().trim().isEmpty()) {
value = "!! non renseignée !!";
}
DefaultListCellRenderer r = (DefaultListCellRenderer) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (!tables.contains(value)) {
r.setForeground(Color.RED);
}
return r;
}
});
comboOption.setSelectedItem(tableName);
} else {
throw new IllegalArgumentException("Unknow type " + fd.getType());
}
fieldName.setText(fd.getName().trim());
}
private String[] getTables() {
final ComptaPropsConfiguration instanceCompta = ComptaPropsConfiguration.getInstanceCompta();
Set<SQLTable> t = instanceCompta.getRootSociete().getTables();
ArrayList<String> names = new ArrayList<String>(t.size());
for (SQLTable table : t) {
// TODO: Creer un renderer
// final SQLElement element = instanceCompta.getDirectory().getElement(table);
// String e = table.getName();
// if (element != null) {
// e += " (" + element.getPluralName().toLowerCase() + ")";
// }
names.add(table.getName());
}
Collections.sort(names);
return names.toArray(new String[names.size()]);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.comboType) {
final String type = xmltypes[comboType.getSelectedIndex()];
if (!type.equals(fd.getType())) {
this.fd.setType(type);
updateFrom(fd);
}
} else if (e.getSource() == this.comboOption) {
comboOptionModified();
}
}
}