]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SetColours.java
1329d05fe7d9d54165161734c968eaf6d8e44e67
[GpsPrune.git] / tim / prune / function / SetColours.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
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;
12
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;
20
21 import tim.prune.App;
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;
29
30 /**
31  * Class to show the popup window for setting the colours
32  */
33 public class SetColours extends GenericFunction
34 {
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;
41
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
50         };
51
52         /**
53          * Inner class to react to patch clicks
54          */
55         class PatchListener extends MouseAdapter
56         {
57                 /** Associated patch */
58                 private ColourPatch _patch = null;
59                 /** Constructor */
60                 public PatchListener(ColourPatch inPatch) {
61                         _patch = inPatch;
62                 }
63                 /** React to mouse clicks */
64                 public void mouseClicked(MouseEvent e)
65                 {
66                         _colourChooser.showDialog(_patch.getBackground());
67                         Color colour = _colourChooser.getChosenColour();
68                         if (colour != null) _patch.setColour(colour);
69                 }
70         }
71
72         /**
73          * Constructor
74          * @param inApp app object
75          */
76         public SetColours(App inApp)
77         {
78                 super(inApp);
79         }
80
81         /**
82          * Return the name key for this function
83          */
84         public String getNameKey()
85         {
86                 return "function.setcolours";
87         }
88
89         /**
90          * @return the contents of the window as a Component
91          */
92         private Component makeContents()
93         {
94                 JPanel mainPanel = new JPanel();
95                 mainPanel.setLayout(new BorderLayout(0, 10));
96
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];
103
104                 ColourScheme scheme = Config.getColourScheme();
105                 ColourPatch patch = null;
106                 // Loop over four columns of patches
107                 for (int i=0; i<4; i++)
108                 {
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));
115                         colPanel.add(patch);
116                         _patches[i*2] = patch;
117                         // separator
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));
123                         colPanel.add(patch);
124                         _patches[i*2+1] = patch;
125
126                         // Add column to panel
127                         colPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
128                         centralPanel.add(colPanel);
129                 }
130                 mainPanel.add(centralPanel, BorderLayout.CENTER);
131
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()
137                 {
138                         public void actionPerformed(ActionEvent e)
139                         {
140                                 updateConfigColours();
141                                 _dialog.dispose();
142                         }
143                 });
144                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
145                 cancelButton.addActionListener(new ActionListener() {
146                         public void actionPerformed(ActionEvent e)
147                         {
148                                 _dialog.dispose();
149                         }
150                 });
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]));
156                                 }
157                         }
158                 });
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);
166                 return mainPanel;
167         }
168
169
170         /**
171          * Show window
172          */
173         public void begin()
174         {
175                 if (_dialog == null)
176                 {
177                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
178                         _dialog.setLocationRelativeTo(_parentFrame);
179                         _dialog.getContentPane().add(makeContents());
180                         _dialog.pack();
181                         _colourChooser = new ColourChooser(_dialog);
182                 }
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]));
187                 }
188                 _dialog.setVisible(true);
189                 _okButton.requestFocus();
190         }
191
192         /**
193          * Update the current colour scheme with the selected colours
194          */
195         private void updateConfigColours()
196         {
197                 ColourScheme scheme = Config.getColourScheme();
198                 for (int i=0; i<_patches.length; i++)
199                 {
200                         scheme.setColour(INDICES[i], _patches[i].getBackground());
201                 }
202                 Config.updateColourScheme();
203                 UpdateMessageBroker.informSubscribers();
204         }
205 }