Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
package timeflow.app.ui;
 
import timeflow.format.field.*;
 
import javax.swing.*;
import javax.swing.text.JTextComponent;
 
import java.awt.*;
import java.awt.event.*;
 
public class EditValuePanel extends JPanel
{
 
        FieldFormat parser;
        boolean longField;
        JLabel feedback = new JLabel()
        {
 
                public Dimension getPreferredSize()
                {
                        Dimension d = super.getPreferredSize();
                        return new Dimension(200, d.height);
                }
        };
        static final String space = "   ";
        JTextComponent input;
 
        public EditValuePanel(String name, Object startValue, Class type, boolean longField)
        {
                parser = FieldFormatCatalog.getFormat(type);
 
                if (longField)
                {
                        setLayout(new BorderLayout());
                        JPanel top = new JPanel();
                        top.setLayout(new GridLayout(2, 2));
                        top.add(new JPanel());
                        top.add(new JPanel());
                        JLabel fieldLabel = new JLabel(space + name + "   (long)");
                        top.add(fieldLabel);
                        top.add(feedback);
                        add(top, BorderLayout.NORTH);
                        input = new JTextArea(5, 60);
                        ((JTextArea) input).setLineWrap(true);
                        ((JTextArea) input).setWrapStyleWord(true);
                        JScrollPane scroller = new JScrollPane(input);
                        scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                        add(scroller, BorderLayout.CENTER);
                        add(new JPanel(), BorderLayout.WEST);
                        add(new JPanel(), BorderLayout.SOUTH);
                } else
                {
                        setLayout(new GridLayout(1, 4));
                        JLabel fieldLabel = new JLabel(space + name);
                        add(fieldLabel);
                        JLabel typeLabel = new JLabel(FieldFormatCatalog.humanName(type));
                        add(typeLabel);
                        typeLabel.setForeground(Color.gray);
                        input = new JTextField(8);
                        add(input);
                        // enough room for "couldn't understand"
                        add(feedback);
                }
                input.setText(startValue == null ? "" : parser.format(startValue));
                input.addKeyListener(new KeyAdapter()
                {
 
                        @Override
                        public void keyReleased(KeyEvent e)
                        {
                                parse();
                        }
                });
                parse();
        }
 
        void parse()
        {
                try
                {
                        parser.parse(input.getText());
                } catch (Exception e)
                {
                }
                feedback.setText("  " + parser.feedback());
                feedback.setForeground(parser.isUnderstood() ? Color.gray : Color.red);
        }
 
        public Object getInputValue()
        {
                try
                {
                        return parser.parse(input.getText());
                } catch (Exception e)
                {
                        return null;
                }
        }
 
        public boolean isOK()
        {
                return parser.isUnderstood();
        }
 
        public Dimension getPreferredSize()
        {
                Dimension d = super.getPreferredSize();
                int w = Math.max(300, d.width);
                return new Dimension(w, d.height);
        }
}