Normand Briere
2018-07-07 e416acb9b012b17d1efe49ad2199ea7132d874d1
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
package timeflow.app.ui;
 
import timeflow.model.*;
import timeflow.app.ui.ImportDelimitedPanel.SchemaPanel;
import timeflow.data.time.*;
import timeflow.data.db.*;
 
import javax.swing.*;
 
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
// panel with form for editing a given database entry
public class EditRecordPanel extends JPanel
{
 
        Act act;
        HashMap<Field, EditValuePanel> fieldUI = new HashMap<Field, EditValuePanel>();
        JButton submit, cancel;
        Dimension idealSize = new Dimension();
        TFModel model;
 
        private static void edit(final TFModel model, final Act act, final boolean isAdd)
        {
                final JFrame window = new JFrame(isAdd ? "Add Record" : "Edit Record");
                window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                final EditRecordPanel editor = new EditRecordPanel(model, act);
                window.getContentPane().setLayout(new GridLayout(1, 1));
                window.getContentPane().add(editor);
                editor.submit.addActionListener(new ActionListener()
                {
 
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                window.setVisible(false);
                                editor.submitValues();
                                model.noteAdd(this);
                        }
                });
                editor.cancel.addActionListener(new ActionListener()
                {
 
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                                window.setVisible(false);
                                if (isAdd)
                                {
                                        model.getDB().delete(act);
                                }
                        }
                });
                window.setBounds(50, 50, 700, 500);
                window.pack();
                window.setVisible(true);
        }
 
        public static void edit(TFModel model, Act act)
        {
                edit(model, act, false);
        }
 
        public static void add(TFModel model)
        {
                Act act = model.getDB().createAct();
                edit(model, act, true);
        }
 
        public static void add(TFModel model, RoughTime r)
        {
                Act act = model.getDB().createAct();
                act.set(act.getDB().getField(VirtualField.START), r);
                edit(model, act, true);
        }
 
        public EditRecordPanel(TFModel model, Act act)
        {
                this.model = model;
                this.act = act;
 
                setBackground(Color.white);
                setLayout(new BorderLayout());
 
                JPanel buttons = new JPanel();
                add(buttons, BorderLayout.SOUTH);
                buttons.setBackground(Color.lightGray);
                submit = new JButton("OK");
                buttons.add(submit);
                cancel = new JButton("Cancel");
                buttons.add(cancel);
 
                JPanel entryPanel = new JPanel();
                JScrollPane scroller = new JScrollPane(entryPanel);
                add(scroller, BorderLayout.CENTER);
 
                java.util.List<Field> fields = act.getDB().getFields();
                int n = fields.size();
                entryPanel.setLayout(null);
 
                DBUtils.setRecSizesFromCurrent(act.getDB());
                int top = 0;
 
                for (Field f : fields)
                {
                        EditValuePanel p = new EditValuePanel(f.getName(), act.get(f),
                                f.getType(), f.getRecommendedSize() > 100);
                        entryPanel.add(p);
                        Dimension d = p.getPreferredSize();
                        p.setBounds(0, top, d.width, d.height);
                        top += d.height;
                        idealSize.width = Math.max(d.width + 5, idealSize.width);
                        idealSize.height = Math.max(top + 45, idealSize.height);
                        fieldUI.put(f, p);
                }
 
        }
 
        public Dimension getPreferredSize()
        {
                return idealSize;
        }
 
        public void submitValues()
        {
                for (Field f : fieldUI.keySet())
                {
                        act.set(f, fieldUI.get(f).getInputValue());
                }
                model.noteRecordChange(this);
        }
}