1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.GridLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import javax.swing.BorderFactory;
10 import javax.swing.Box;
11 import javax.swing.BoxLayout;
12 import javax.swing.JButton;
13 import javax.swing.JDialog;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
18 import tim.prune.GenericFunction;
19 import tim.prune.I18nManager;
20 import tim.prune.UpdateMessageBroker;
21 import tim.prune.config.ColourScheme;
22 import tim.prune.config.Config;
23 import tim.prune.gui.colour.ColourChooser;
24 import tim.prune.gui.colour.ColourPatch;
25 import tim.prune.gui.colour.ColourerSelectorPanel;
26 import tim.prune.gui.colour.PatchListener;
27 import tim.prune.gui.colour.PointColourer;
30 * Class to show the popup window for setting the colours
32 public class SetColours extends GenericFunction
34 private JDialog _dialog = null;
35 private JButton _okButton = null;
36 /** Array of 8 colour patches */
37 private ColourPatch[] _patches = null;
38 /** colourer selection panel */
39 private ColourerSelectorPanel _colourerSelector = null;
40 /** Single colour chooser */
41 private ColourChooser _colourChooser = null;
43 /** Array of label keys for the 8 patches */
44 private static final String[] LABEL_KEYS = {"background", "primary", "point", "secondary",
45 "selection", "borders", "text", "lines"};
46 /** Array of indices for the 8 patches */
47 private static final int[] INDICES = {ColourScheme.IDX_BACKGROUND, ColourScheme.IDX_PRIMARY,
48 ColourScheme.IDX_POINT, ColourScheme.IDX_SECONDARY,
49 ColourScheme.IDX_SELECTION, ColourScheme.IDX_BORDERS,
50 ColourScheme.IDX_TEXT, ColourScheme.IDX_LINES
56 * @param inApp app object
58 public SetColours(App inApp)
64 * Return the name key for this function
66 public String getNameKey()
68 return "function.setcolours";
72 * @return the contents of the window as a Component
74 private Component makeContents()
76 JPanel mainPanel = new JPanel();
77 mainPanel.setLayout(new BorderLayout(0, 10));
79 JLabel introLabel = new JLabel(I18nManager.getText("dialog.setcolours.intro"));
80 introLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
81 mainPanel.add(introLabel, BorderLayout.NORTH);
83 // Panel in centre, to hold both the patch panel and the colourer panel (and maybe introLabel too?)
84 JPanel centralPanel = new JPanel();
85 centralPanel.setLayout(new BoxLayout(centralPanel, BoxLayout.Y_AXIS));
87 // Make panel for 8 colour patches
88 JPanel patchPanel = new JPanel();
89 patchPanel.setLayout(new GridLayout());
90 _patches = new ColourPatch[8];
92 ColourScheme scheme = Config.getColourScheme();
93 ColourPatch patch = null;
94 // Loop over four columns of patches
95 for (int i=0; i<4; i++)
97 JPanel colPanel = new JPanel();
98 colPanel.setLayout(new BoxLayout(colPanel, BoxLayout.Y_AXIS));
99 // Top label and patch
100 colPanel.add(new JLabel(I18nManager.getText("dialog.setcolours." + LABEL_KEYS[i*2])));
101 patch = new ColourPatch(scheme.getColour(INDICES[i*2]));
102 patch.addMouseListener(new PatchListener(patch, _colourChooser));
104 _patches[i*2] = patch;
106 colPanel.add(Box.createRigidArea(new Dimension(0, 5)));
107 // Bottom label and patch
108 colPanel.add(new JLabel(I18nManager.getText("dialog.setcolours." + LABEL_KEYS[i*2+1])));
109 patch = new ColourPatch(scheme.getColour(INDICES[i*2+1]));
110 patch.addMouseListener(new PatchListener(patch, _colourChooser));
112 _patches[i*2+1] = patch;
114 // Add column to panel
115 colPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
116 patchPanel.add(colPanel);
118 patchPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
119 centralPanel.add(patchPanel);
120 centralPanel.add(Box.createVerticalStrut(15));
122 // now the colourer selector
123 _colourerSelector = new ColourerSelectorPanel(_colourChooser);
124 _colourerSelector.setAlignmentX(Component.LEFT_ALIGNMENT);
125 centralPanel.add(_colourerSelector);
126 // add the central panel to the main one
127 mainPanel.add(centralPanel, BorderLayout.CENTER);
129 // Buttons at the bottom
130 JPanel buttonPanel = new JPanel();
131 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
132 _okButton = new JButton(I18nManager.getText("button.ok"));
133 _okButton.addActionListener(new ActionListener()
135 public void actionPerformed(ActionEvent e)
137 updateConfigColours();
141 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
142 cancelButton.addActionListener(new ActionListener() {
143 public void actionPerformed(ActionEvent e)
148 JButton resetButton = new JButton(I18nManager.getText("button.resettodefaults"));
149 resetButton.addActionListener(new ActionListener() {
150 public void actionPerformed(ActionEvent e) {
151 for (int i=0; i<8; i++) {
152 _patches[i].setColour(ColourScheme.getDefaultColour(INDICES[i]));
156 buttonPanel.add(resetButton);
157 buttonPanel.add(Box.createHorizontalGlue());
158 buttonPanel.add(_okButton);
159 buttonPanel.add(Box.createHorizontalStrut(5));
160 buttonPanel.add(cancelButton);
161 buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
162 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
174 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
175 _dialog.setLocationRelativeTo(_parentFrame);
176 _colourChooser = new ColourChooser(_dialog);
177 _dialog.getContentPane().add(makeContents());
180 // Reset colours to current ones
181 ColourScheme scheme = Config.getColourScheme();
182 for (int i=0; i<8; i++) {
183 _patches[i].setColour(scheme.getColour(INDICES[i]));
185 PointColourer colourer = Config.getPointColourer();
186 _colourerSelector.init(colourer, scheme.getColour(ColourScheme.IDX_POINT));
187 _dialog.setVisible(true);
188 _okButton.requestFocus();
192 * Update the current colour scheme with the selected colours
194 private void updateConfigColours()
196 ColourScheme scheme = Config.getColourScheme();
197 for (int i=0; i<_patches.length; i++)
199 scheme.setColour(INDICES[i], _patches[i].getBackground());
201 Config.updateColourScheme();
202 PointColourer colourer = _colourerSelector.getSelectedColourer();
203 Config.updatePointColourer(colourer);
204 _app.updatePointColourer();
205 UpdateMessageBroker.informSubscribers();