1 package tim.prune.function;
3 import java.awt.BorderLayout;
5 import java.awt.Component;
6 import java.awt.Dimension;
7 import java.awt.GridLayout;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.MouseAdapter;
11 import java.awt.event.MouseEvent;
13 import javax.swing.BorderFactory;
14 import javax.swing.Box;
15 import javax.swing.BoxLayout;
16 import javax.swing.JButton;
17 import javax.swing.JDialog;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
22 import tim.prune.GenericFunction;
23 import tim.prune.I18nManager;
24 import tim.prune.UpdateMessageBroker;
25 import tim.prune.config.ColourScheme;
26 import tim.prune.config.Config;
27 import tim.prune.gui.ColourChooser;
28 import tim.prune.gui.ColourPatch;
31 * Class to show the popup window for setting the colours
33 public class SetColours extends GenericFunction
35 private JDialog _dialog = null;
36 private JButton _okButton = null;
37 /** Array of 8 colour patches */
38 private ColourPatch[] _patches = null;
39 /** Single colour chooser */
40 private ColourChooser _colourChooser = null;
42 /** Array of label keys for the 8 patches */
43 private static final String[] LABEL_KEYS = {"background", "primary", "point", "secondary",
44 "selection", "borders", "text", "lines"};
45 /** Array of indices for the 8 patches */
46 private static final int[] INDICES = {ColourScheme.IDX_BACKGROUND, ColourScheme.IDX_PRIMARY,
47 ColourScheme.IDX_POINT, ColourScheme.IDX_SECONDARY,
48 ColourScheme.IDX_SELECTION, ColourScheme.IDX_BORDERS,
49 ColourScheme.IDX_TEXT, ColourScheme.IDX_LINES
53 * Inner class to react to patch clicks
55 class PatchListener extends MouseAdapter
57 /** Associated patch */
58 private ColourPatch _patch = null;
60 public PatchListener(ColourPatch inPatch) {
63 /** React to mouse clicks */
64 public void mouseClicked(MouseEvent e)
66 _colourChooser.showDialog(_patch.getBackground());
67 Color colour = _colourChooser.getChosenColour();
68 if (colour != null) _patch.setColour(colour);
74 * @param inApp app object
76 public SetColours(App inApp)
82 * Return the name key for this function
84 public String getNameKey()
86 return "function.setcolours";
90 * @return the contents of the window as a Component
92 private Component makeContents()
94 JPanel mainPanel = new JPanel();
95 mainPanel.setLayout(new BorderLayout(0, 10));
97 JLabel intro = new JLabel(I18nManager.getText("dialog.setcolours.intro"));
98 intro.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
99 mainPanel.add(intro, BorderLayout.NORTH);
100 JPanel centralPanel = new JPanel();
101 centralPanel.setLayout(new GridLayout());
102 _patches = new ColourPatch[8];
104 ColourScheme scheme = Config.getColourScheme();
105 ColourPatch patch = null;
106 // Loop over four columns of patches
107 for (int i=0; i<4; i++)
109 JPanel colPanel = new JPanel();
110 colPanel.setLayout(new BoxLayout(colPanel, BoxLayout.Y_AXIS));
111 // Top label and patch
112 colPanel.add(new JLabel(I18nManager.getText("dialog.setcolours." + LABEL_KEYS[i*2])));
113 patch = new ColourPatch(scheme.getColour(INDICES[i*2]));
114 patch.addMouseListener(new PatchListener(patch));
116 _patches[i*2] = patch;
118 colPanel.add(Box.createRigidArea(new Dimension(0, 5)));
119 // Bottom label and patch
120 colPanel.add(new JLabel(I18nManager.getText("dialog.setcolours." + LABEL_KEYS[i*2+1])));
121 patch = new ColourPatch(scheme.getColour(INDICES[i*2+1]));
122 patch.addMouseListener(new PatchListener(patch));
124 _patches[i*2+1] = patch;
126 // Add column to panel
127 colPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
128 centralPanel.add(colPanel);
130 mainPanel.add(centralPanel, BorderLayout.CENTER);
132 // Buttons at the bottom
133 JPanel buttonPanel = new JPanel();
134 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
135 _okButton = new JButton(I18nManager.getText("button.ok"));
136 _okButton.addActionListener(new ActionListener()
138 public void actionPerformed(ActionEvent e)
140 updateConfigColours();
144 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
145 cancelButton.addActionListener(new ActionListener() {
146 public void actionPerformed(ActionEvent e)
151 JButton resetButton = new JButton(I18nManager.getText("button.resettodefaults"));
152 resetButton.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
154 for (int i=0; i<8; i++) {
155 _patches[i].setColour(ColourScheme.getDefaultColour(INDICES[i]));
159 buttonPanel.add(resetButton);
160 buttonPanel.add(Box.createHorizontalGlue());
161 buttonPanel.add(_okButton);
162 buttonPanel.add(Box.createHorizontalStrut(5));
163 buttonPanel.add(cancelButton);
164 buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
165 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
177 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
178 _dialog.setLocationRelativeTo(_parentFrame);
179 _dialog.getContentPane().add(makeContents());
181 _colourChooser = new ColourChooser(_dialog);
183 // Reset colours to current ones
184 ColourScheme scheme = Config.getColourScheme();
185 for (int i=0; i<8; i++) {
186 _patches[i].setColour(scheme.getColour(INDICES[i]));
188 _dialog.setVisible(true);
189 _okButton.requestFocus();
193 * Update the current colour scheme with the selected colours
195 private void updateConfigColours()
197 ColourScheme scheme = Config.getColourScheme();
198 for (int i=0; i<_patches.length; i++)
200 scheme.setColour(INDICES[i], _patches[i].getBackground());
202 Config.updateColourScheme();
203 UpdateMessageBroker.informSubscribers();