OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 106 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
106 ilm 1
package org.openconcerto.modules.humanresources.travel.expense;
2
 
3
import java.beans.PropertyChangeEvent;
4
import java.beans.PropertyChangeListener;
5
import java.text.DecimalFormat;
6
import java.util.HashSet;
7
import java.util.Set;
8
 
9
import javax.swing.JComponent;
181 ilm 10
import javax.swing.JLabel;
106 ilm 11
import javax.swing.text.AbstractDocument;
12
import javax.swing.text.JTextComponent;
13
 
14
import org.openconcerto.erp.core.common.ui.DeviseField;
15
import org.openconcerto.sql.element.GroupSQLComponent;
16
import org.openconcerto.sql.element.SQLElement;
181 ilm 17
import org.openconcerto.sql.sqlobject.ElementComboBox;
106 ilm 18
import org.openconcerto.sql.sqlobject.itemview.VWRowItemView;
19
import org.openconcerto.ui.JDate;
181 ilm 20
import org.openconcerto.ui.JLabelBold;
106 ilm 21
import org.openconcerto.ui.component.text.TextComponentUtils;
22
import org.openconcerto.ui.group.Group;
23
import org.openconcerto.utils.text.DocumentFilterList;
24
import org.openconcerto.utils.text.DocumentFilterList.FilterType;
25
import org.openconcerto.utils.text.LimitedSizeDocumentFilter;
26
 
27
public class ExpenseSQLComponent extends GroupSQLComponent {
181 ilm 28
	private DecimalFormat decimalFormat = new DecimalFormat("0.##");
106 ilm 29
 
181 ilm 30
	public ExpenseSQLComponent(SQLElement element, Group group) {
31
		super(element, group);
106 ilm 32
 
181 ilm 33
	}
106 ilm 34
 
181 ilm 35
	@Override
36
	protected void initDone() {
37
		super.initDone();
38
		final PropertyChangeListener listener = new PropertyChangeListener() {
106 ilm 39
 
181 ilm 40
			@Override
41
			public void propertyChange(PropertyChangeEvent evt) {
42
				updateAmount();
43
			}
44
		};
106 ilm 45
 
181 ilm 46
		final AbstractDocument comp1 = (AbstractDocument) TextComponentUtils
47
				.getDocument(getView("TRAVEL_DISTANCE").getComp());
48
		DocumentFilterList.add(comp1, new LimitedSizeDocumentFilter(5), FilterType.SIMPLE_FILTER);
49
		getView("TRAVEL_DISTANCE").addValueListener(listener);
106 ilm 50
 
181 ilm 51
		final AbstractDocument comp2 = (AbstractDocument) TextComponentUtils
52
				.getDocument(getView("TRAVEL_RATE").getComp());
53
		DocumentFilterList.add(comp2, new LimitedSizeDocumentFilter(5), FilterType.SIMPLE_FILTER);
106 ilm 54
 
181 ilm 55
		getView("TRAVEL_RATE").addValueListener(listener);
56
	}
106 ilm 57
 
181 ilm 58
	@Override
59
	protected Set<String> createRequiredNames() {
60
		final Set<String> s = new HashSet<String>(1);
61
		s.add("DATE");
62
		s.add("DESCRIPTION");
63
		s.add("ID_USER_COMMON");
64
		s.add("ID_EXPENSE_STATE");
65
		return s;
66
	}
106 ilm 67
 
181 ilm 68
	@Override
69
	public JComponent createEditor(String id) {
70
		if(id.equals("ID_USER_COMMON") || id.equals("ID_EXPENSE_STATE")) {
71
            ElementComboBox comp = new ElementComboBox(false, 15);
72
            ((ElementComboBox) comp).init(getElement().getForeignElement(id));
73
            return comp;
74
		}
75
		else if (id.equals("DATE")) {
76
			return new JDate(true);
77
		} else if (id.endsWith("AMOUNT")) {
78
			return new DeviseField();
79
		}
80
		return super.createEditor(id);
81
	}
106 ilm 82
 
181 ilm 83
	@Override
84
	public JLabel getLabel(String id) {
85
		if (id.equals("humanresources.travel.expense.travel")) {
86
			return new JLabelBold("Frais kilometriques");
87
		} else if (id.equals("humanresources.travel.expense.default")) {
88
			return new JLabelBold("Descriptif");
89
		} else if (id.equals("humanresources.travel.expense.misc")) {
90
			return new JLabelBold("Frais annexes");
91
		} else {
92
			return (JLabel) super.getLabel(id);
93
		}
94
	}
106 ilm 95
 
181 ilm 96
	private void updateAmount() {
97
		float v1 = getFloat("TRAVEL_DISTANCE");
98
		float v2 = getFloat("TRAVEL_RATE");
99
		float total = v1 * v2;
100
		if (total > 1000000 || total < 0) {
101
			total = 0;
102
		}
103
		final String valueOf = decimalFormat.format(total);
104
		((JTextComponent) getView("TRAVEL_AMOUNT").getComp()).setText(valueOf);
105
	}
106 ilm 106
 
181 ilm 107
	private float getFloat(String id) {
108
		final Number n = (Number) ((VWRowItemView<?>) getView(id)).getWrapper().getValue();
109
		if (n == null) {
110
			return 0;
111
		}
112
		return n.floatValue();
113
	}
114
 
106 ilm 115
}