summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/QDatePickerImpl.java
blob: 9c6adf3a0779054702e0b8348d7390b2b47d76d6 (plain) (blame)
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
package org.openslx.dozmod.gui.control;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JTextField;

import org.apache.log4j.Logger;
import org.jdatepicker.ComponentColorDefaults;
import org.jdatepicker.ComponentColorDefaults.Key;
import org.jdatepicker.JDatePicker;
import org.openslx.dozmod.gui.helper.ColorUtil;
import org.openslx.dozmod.gui.helper.TextChangeListener;

@SuppressWarnings("serial")
public class QDatePickerImpl extends JDatePicker {

	private static final Logger LOGGER = Logger.getLogger(QDatePickerImpl.class);

	private final JButton pickButton;

	private final JTextField displayTextField;

	private final Set<ActionListener> actionListeners = new HashSet<>();
	
	static {
		ComponentColorDefaults def = ComponentColorDefaults.getInstance();
		Color c1 = def.getColor(Key.FG_MONTH_SELECTOR);
		Color c2 = def.getColor(Key.BG_MONTH_SELECTOR);
		if (ColorUtil.getContrast(c1, c2) < 4.5) {
			def.setColor(Key.FG_MONTH_SELECTOR, Color.WHITE);
			def.setColor(Key.BG_MONTH_SELECTOR, new Color(0, 0, 128));
		}
	}

	public QDatePickerImpl() {
		super(Calendar.getInstance());
		Field cfButton = null;
		Field cfTextField = null;
		try {
			cfButton = JDatePicker.class.getDeclaredField("button");
			cfButton.setAccessible(true);
		} catch (Exception e) {
			LOGGER.warn("Could not get button field", e);
		}
		try {
			cfTextField = JDatePicker.class.getDeclaredField("formattedTextField");
			cfTextField.setAccessible(true);
		} catch (Exception e) {
			LOGGER.warn("Could not get text field", e);
		}
		JButton button = null;
		JTextField textField = null;
		if (cfButton != null) {
			try {
				button = (JButton) cfButton.get(this);
			} catch (Exception e) {
				LOGGER.warn("Could not get button from this", e);
			}
		}
		if (cfTextField != null) {
			try {
				textField = (JTextField) cfTextField.get(this);
			} catch (Exception e) {
				LOGGER.warn("Could not get textfield from this", e);
			}
		}
		pickButton = button;
		displayTextField = textField;
		if (displayTextField != null) {
			displayTextField.getDocument().addDocumentListener(new TextChangeListener() {
				@Override
				public void changed() {
					fireChangeEvent();
				}
			});
		}
	}

	private void fireChangeEvent() {
		if (actionListeners.isEmpty())
			return;
		ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Date selected");
		for (ActionListener al : actionListeners) {
			al.actionPerformed(ae);
		}
	}

	@Override
	public void setEnabled(boolean enabled) {
		if (pickButton != null) {
			pickButton.setEnabled(enabled);
		}
		if (displayTextField != null) {
			displayTextField.setEnabled(enabled);
		}
		super.setEnabled(enabled);
	}

	@Override
	public void addActionListener(ActionListener actionListener) {
		actionListeners.add(actionListener);
		//super.addActionListener(actionListener);
	}

}