]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/settings/SetColours.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / function / settings / SetColours.java
1 package tim.prune.function.settings;
2
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;
16
17 import tim.prune.App;
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;
28
29 /**
30  * Class to show the popup window for setting the colours
31  */
32 public class SetColours extends GenericFunction
33 {
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;
42
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
51         };
52
53
54         /**
55          * Constructor
56          * @param inApp app object
57          */
58         public SetColours(App inApp)
59         {
60                 super(inApp);
61         }
62
63         /**
64          * Return the name key for this function
65          */
66         public String getNameKey()
67         {
68                 return "function.setcolours";
69         }
70
71         /**
72          * @return the contents of the window as a Component
73          */
74         private Component makeContents()
75         {
76                 JPanel mainPanel = new JPanel();
77                 mainPanel.setLayout(new BorderLayout(0, 10));
78
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);
82
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));
86
87                 // Make panel for 8 colour patches
88                 JPanel patchPanel = new JPanel();
89                 patchPanel.setLayout(new GridLayout());
90                 _patches = new ColourPatch[8];
91
92                 ColourScheme scheme = Config.getColourScheme();
93                 ColourPatch patch = null;
94                 // Loop over four columns of patches
95                 for (int i=0; i<4; i++)
96                 {
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));
103                         colPanel.add(patch);
104                         _patches[i*2] = patch;
105                         // separator
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));
111                         colPanel.add(patch);
112                         _patches[i*2+1] = patch;
113
114                         // Add column to panel
115                         colPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
116                         patchPanel.add(colPanel);
117                 }
118                 patchPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
119                 centralPanel.add(patchPanel);
120                 centralPanel.add(Box.createVerticalStrut(15));
121
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);
128
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()
134                 {
135                         public void actionPerformed(ActionEvent e)
136                         {
137                                 updateConfigColours();
138                                 _dialog.dispose();
139                         }
140                 });
141                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
142                 cancelButton.addActionListener(new ActionListener() {
143                         public void actionPerformed(ActionEvent e)
144                         {
145                                 _dialog.dispose();
146                         }
147                 });
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]));
153                                 }
154                         }
155                 });
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);
163                 return mainPanel;
164         }
165
166
167         /**
168          * Show window
169          */
170         public void begin()
171         {
172                 if (_dialog == null)
173                 {
174                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
175                         _dialog.setLocationRelativeTo(_parentFrame);
176                         _colourChooser = new ColourChooser(_dialog);
177                         _dialog.getContentPane().add(makeContents());
178                         _dialog.pack();
179                 }
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]));
184                 }
185                 PointColourer colourer = Config.getPointColourer();
186                 _colourerSelector.init(colourer, scheme.getColour(ColourScheme.IDX_POINT));
187                 _dialog.setVisible(true);
188                 _okButton.requestFocus();
189         }
190
191         /**
192          * Update the current colour scheme with the selected colours
193          */
194         private void updateConfigColours()
195         {
196                 ColourScheme scheme = Config.getColourScheme();
197                 for (int i=0; i<_patches.length; i++)
198                 {
199                         scheme.setColour(INDICES[i], _patches[i].getBackground());
200                 }
201                 Config.updateColourScheme();
202                 PointColourer colourer = _colourerSelector.getSelectedColourer();
203                 Config.updatePointColourer(colourer);
204                 _app.updatePointColourer();
205                 UpdateMessageBroker.informSubscribers();
206         }
207 }