summaryrefslogtreecommitdiff
path: root/client/inputforms/Preferences.java
blob: 6be32f3cb92fdfb215510cf5689f66baa6d3c7a8 (plain)
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
package client.inputforms;

import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import client.NetCalendarClient;
import client.helper.GUIHelper;


import shared.*;
/**
 * This class contains all the GUI components of the preferences/options/config dialog.
 * Its used for editing the current config values of the config.txt file.
 * @author buetow
 *
 */
public class Preferences extends InputForm {
	private final static long serialVersionUID = 1L;
	
    private String[] labels = null; 
    private int iNumPairs = -1;

    /**
     * Create the input form window and show it.  
     * @param netCalendarClient Specifies the current calendar client session window. 
     */
    public Preferences(NetCalendarClient netCalendarClient) {
    		super("Preferences", netCalendarClient);
        	initComponents();
        	setFieldValues();
    		pack();
    		setVisible(true);
    }

	/**
	 * Initializes all the GUI components.
	 */
	protected void initComponents() {
		super.initComponents();
		setFieldValues();
		JPanel jPanel = new JPanel(new SpringLayout());
        
        labels = Config.getSortedKeyArray();
        iNumPairs = labels.length;
        
		ActionListener actionListenerTextFields = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				submit();
			} 
		};
		
        vecFields = new Vector();
        for (int i = 0; i < iNumPairs; ++i) {
            JLabel jLable = new JLabel(labels[i], JLabel.TRAILING);
            jPanel.add(jLable);
            JTextField textField = new JTextField(InputForm.TEXTFIELD_LENGTH);
            textField.addActionListener(actionListenerTextFields);
            jLable.setLabelFor(textField);
            jPanel.add(textField);
            vecFields.add(textField);
        }

        //Lay out the panel.
        GUIHelper.makeCompactGrid(jPanel, 
        		iNumPairs, 2, 	// iRows, iCols
        		6, 6,        	// iInitX, iInitY
        		6, 6);       	// iXPad, iYPad

        jPanelButtons.remove(jButtonClear);
        JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        jSplitPane.setTopComponent(jPanel);
        jSplitPane.setBottomComponent(jPanelButtons);
        jSplitPane.setDividerSize(0);
        
        setContentPane(jSplitPane);
	}
	
	/**
	 * This method sets the fields of the edit frame according to the current configuration options.
	 */
	private void setFieldValues() {
		for (int i = 0; i < iNumPairs; ++i) 
			((JTextField) vecFields.get(i)).setText(Config.getStringValue(labels[i]));
	}
	
	/**
	 * This method is invoked if the enter key is pressed or if the submit button
	 * has been pressed. It starts a client request relating to the user's input of
	 * the text fields. It will write all changes to the config.txt file.
	 */
	protected void submit() {
		for (int i = 0; i < iNumPairs; ++i) 
			Config.setValue(labels[i], ((JTextField) vecFields.get(i)).getText());
		
		Config.writeConfigToFile();
	}
}