1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.gui;
18
19 import net.sourceforge.jarduino.ArduinoException;
20
21 import javax.swing.ButtonGroup;
22 import javax.swing.JFileChooser;
23 import javax.swing.JFrame;
24 import javax.swing.JMenu;
25 import javax.swing.JMenuBar;
26 import javax.swing.JMenuItem;
27 import javax.swing.JRadioButtonMenuItem;
28 import javax.swing.JSeparator;
29 import javax.swing.filechooser.FileNameExtensionFilter;
30 import java.io.File;
31 import java.nio.charset.Charset;
32 import java.nio.charset.StandardCharsets;
33
34
35
36
37 public class ArduinoMenuBar {
38
39
40
41 private static final String ARDUINO_LOC = System.getProperty("user.home") + "/Documents/Arduino";
42
43
44
45
46 private final ArduinoPanelMain theMain;
47
48
49
50
51 private final JMenuBar theMenuBar;
52
53
54
55
56 private final ArduinoDialogHelp theHelp;
57
58
59
60
61 private final ArduinoAbout theAbout;
62
63
64
65
66 private Charset theCharset;
67
68
69
70
71
72
73
74 ArduinoMenuBar(final JFrame pFrame,
75 final ArduinoPanelMain pMain) throws ArduinoException {
76
77 theMain = pMain;
78
79
80 theHelp = new ArduinoDialogHelp(pFrame, pMain);
81
82
83 theAbout = new ArduinoAbout(pFrame);
84
85
86 theMenuBar = new JMenuBar();
87
88
89 createFileMenu();
90
91
92 createEncodingMenu();
93
94
95 createHelpMenu();
96
97
98 final File myLocation = ArduinoPreferences.getDefaultLocation();
99 if (myLocation != null) {
100 handleNewSelection(myLocation);
101 }
102 }
103
104
105
106
107
108 JMenuBar getComponent() {
109 return theMenuBar;
110 }
111
112
113
114
115 private void createFileMenu() {
116
117 final JMenu myFile = new JMenu("File");
118
119
120 final JMenuItem myLoad = new JMenuItem("Load DBC file");
121 myLoad.addActionListener(e -> handleSelect());
122 myFile.add(myLoad);
123
124
125 myFile.add(new JSeparator());
126 final JMenuItem mySources = new JMenuItem("Export Sketch");
127 mySources.addActionListener(e -> handleSketch());
128 myFile.add(mySources);
129 final JMenuItem myLibrary = new JMenuItem("Export Library");
130 myLibrary.addActionListener(e -> handleLibrary());
131 myFile.add(myLibrary);
132
133
134 myFile.add(new JSeparator());
135 final JMenuItem myExit = new JMenuItem("Exit");
136 myExit.addActionListener(e -> System.exit(0));
137 myFile.add(myExit);
138
139
140 theMenuBar.add(myFile);
141 }
142
143
144
145
146 private void createEncodingMenu() {
147
148 theCharset = ArduinoPreferences.getCharSet();
149
150
151 final JMenu myEncoding = new JMenu("Encoding");
152 final ButtonGroup myEncGroup = new ButtonGroup();
153
154
155 addCharSetToMenu(myEncoding, myEncGroup, StandardCharsets.ISO_8859_1);
156 addCharSetToMenu(myEncoding, myEncGroup, StandardCharsets.UTF_8);
157
158
159 theMenuBar.add(myEncoding);
160 }
161
162
163
164
165 private void createHelpMenu() {
166
167 final JMenu myMenu = new JMenu("Help");
168
169
170 final JMenuItem myHelp = new JMenuItem("Help");
171 myHelp.addActionListener(e -> theHelp.showDialog());
172 myMenu.add(myHelp);
173
174
175 final JMenuItem myAbout = new JMenuItem("About");
176 myAbout.addActionListener(e -> theAbout.showDialog());
177 myMenu.add(myAbout);
178
179
180 theMenuBar.add(myMenu);
181 }
182
183
184
185
186
187
188 private void addCharSetToMenu(final JMenu pEncoding,
189 final ButtonGroup pGroup,
190 final Charset pCharset) {
191
192 final JMenuItem myItem = new JRadioButtonMenuItem(pCharset.displayName());
193 myItem.setSelected(pCharset.equals(theCharset));
194 myItem.addActionListener(e -> setCharSet(pCharset));
195 pEncoding.add(myItem);
196 pGroup.add(myItem);
197 }
198
199
200
201
202 private void handleSelect() {
203
204 final JFileChooser myChooser = new JFileChooser();
205 myChooser.setDialogTitle("Select DBC File");
206 myChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
207 myChooser.setFileFilter(new FileNameExtensionFilter("DBC Files", "dbc"));
208
209
210 final File myCurrent = ArduinoPreferences.getDefaultLocation();
211 if (myCurrent != null) {
212 myChooser.setCurrentDirectory(new File(myCurrent.getParent()));
213 }
214
215
216 final int myResult = myChooser.showOpenDialog(theMain.getFrame());
217
218
219 if (myResult == JFileChooser.APPROVE_OPTION) {
220
221 final File mySelected = myChooser.getSelectedFile();
222 handleNewSelection(mySelected);
223 }
224 }
225
226
227
228
229
230 private void handleNewSelection(final File pSelected) {
231
232 ArduinoException myError = theMain.setSelected(pSelected, theCharset);
233 if (myError == null) {
234 myError = ArduinoPreferences.storeDefaultLocation(pSelected);
235 }
236
237
238 if (myError != null) {
239
240 theMain.setError(myError);
241 }
242 }
243
244
245
246
247 private void handleSketch() {
248
249 final JFileChooser myChooser = new JFileChooser();
250 myChooser.setDialogTitle("Select Sketch directory");
251 myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
252
253
254 File myCurrent = ArduinoPreferences.getSketchLocation();
255 if (myCurrent == null) {
256
257 myCurrent = new File(ARDUINO_LOC);
258 }
259
260
261 myChooser.setCurrentDirectory(myCurrent);
262
263
264 final int myResult = myChooser.showSaveDialog(theMain.getFrame());
265
266
267 if (myResult == JFileChooser.APPROVE_OPTION) {
268
269 final File myOutDir = myChooser.getSelectedFile();
270 handleWritingSketch(myOutDir);
271 }
272 }
273
274
275
276
277
278 private void handleWritingSketch(final File pOutDir) {
279
280 ArduinoException myError = theMain.writeSketch(pOutDir, theCharset);
281 if (myError == null) {
282 myError = ArduinoPreferences.storeSketchLocation(pOutDir);
283 }
284
285
286 if (myError != null) {
287 theMain.setError(myError);
288 }
289 }
290
291
292
293
294 private void handleLibrary() {
295
296 final JFileChooser myChooser = new JFileChooser();
297 myChooser.setDialogTitle("Select Library directory");
298 myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
299
300
301 File myCurrent = ArduinoPreferences.getLibraryLocation();
302 if (myCurrent == null) {
303
304 myCurrent = new File(ARDUINO_LOC + "/libraries");
305 }
306
307
308 myChooser.setCurrentDirectory(myCurrent);
309
310
311 final int myResult = myChooser.showSaveDialog(theMain.getFrame());
312
313
314 if (myResult == JFileChooser.APPROVE_OPTION) {
315
316 final File myOutDir = myChooser.getSelectedFile();
317 handleWritingLibrary(myOutDir);
318 }
319 }
320
321
322
323
324
325 private void handleWritingLibrary(final File pOutDir) {
326
327 ArduinoException myError = theMain.writeLibrary(pOutDir, theCharset);
328 if (myError == null) {
329 myError = ArduinoPreferences.storeLibraryLocation(pOutDir);
330 }
331
332
333 if (myError != null) {
334 theMain.setError(myError);
335 }
336 }
337
338
339
340
341
342 private void setCharSet(final Charset pCharset) {
343
344 theCharset = pCharset;
345 final ArduinoException myError = ArduinoPreferences.storeCharSet(theCharset);
346
347
348 if (myError != null) {
349 theMain.setError(myError);
350 }
351 }
352 }