View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Arduino Menu Bar.
36   */
37  public class ArduinoMenuBar {
38      /**
39       * The arduino base.
40       */
41      private static final String ARDUINO_LOC = System.getProperty("user.home") + "/Documents/Arduino";
42  
43      /**
44       * The main panel.
45       */
46      private final ArduinoPanelMain theMain;
47  
48      /**
49       * The MenuBar.
50       */
51      private final JMenuBar theMenuBar;
52  
53      /**
54       * The help dialog.
55       */
56      private final ArduinoDialogHelp theHelp;
57  
58      /**
59       * The about box.
60       */
61      private final ArduinoAbout theAbout;
62  
63      /**
64       * The characterSet.
65       */
66      private Charset theCharset;
67  
68      /**
69       * Constructor.
70       * @param pFrame the frame
71       * @param pMain the main panel
72       * @throws ArduinoException on error
73       */
74      ArduinoMenuBar(final JFrame pFrame,
75                     final ArduinoPanelMain pMain) throws ArduinoException {
76          /* Store parameters */
77          theMain = pMain;
78  
79          /* Create the help dialog */
80          theHelp = new ArduinoDialogHelp(pFrame, pMain);
81  
82          /* Create the aboutBox */
83          theAbout = new ArduinoAbout(pFrame);
84  
85          /* Create menu */
86          theMenuBar = new JMenuBar();
87  
88          /* Create the file Menu */
89          createFileMenu();
90  
91          /* Create the encoding Menu */
92          createEncodingMenu();
93  
94          /* Create the help Menu */
95          createHelpMenu();
96  
97          /* Handle the default location */
98          final File myLocation = ArduinoPreferences.getDefaultLocation();
99          if (myLocation != null) {
100             handleNewSelection(myLocation);
101         }
102     }
103 
104     /**
105      * Obtain the component.
106      * @return the component
107      */
108     JMenuBar getComponent() {
109         return theMenuBar;
110     }
111 
112     /**
113      * Create file menu.
114      */
115     private void createFileMenu() {
116         /* Create the file Menu */
117         final JMenu myFile = new JMenu("File");
118 
119         /* Create load item */
120         final JMenuItem myLoad = new JMenuItem("Load DBC file");
121         myLoad.addActionListener(e -> handleSelect());
122         myFile.add(myLoad);
123 
124         /* Create export items */
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         /* Create Exit item */
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         /* Add to menuBar */
140         theMenuBar.add(myFile);
141     }
142 
143     /**
144      * Create encoding menu.
145      */
146     private void createEncodingMenu() {
147         /* Access the current charSet */
148         theCharset = ArduinoPreferences.getCharSet();
149 
150         /* Create the encoding Menu and group */
151         final JMenu myEncoding = new JMenu("Encoding");
152         final ButtonGroup myEncGroup = new ButtonGroup();
153 
154         /* Create charSet menus */
155         addCharSetToMenu(myEncoding, myEncGroup, StandardCharsets.ISO_8859_1);
156         addCharSetToMenu(myEncoding, myEncGroup, StandardCharsets.UTF_8);
157 
158         /* Add to menuBar */
159         theMenuBar.add(myEncoding);
160     }
161 
162     /**
163      * Create help menu.
164      */
165     private void createHelpMenu() {
166         /* Create the help Menu */
167         final JMenu myMenu = new JMenu("Help");
168 
169         /* Create help item */
170         final JMenuItem myHelp = new JMenuItem("Help");
171         myHelp.addActionListener(e -> theHelp.showDialog());
172         myMenu.add(myHelp);
173 
174         /* Create About item */
175         final JMenuItem myAbout = new JMenuItem("About");
176         myAbout.addActionListener(e -> theAbout.showDialog());
177         myMenu.add(myAbout);
178 
179         /* Add to menuBar */
180         theMenuBar.add(myMenu);
181     }
182     /**
183      * Add charSet to menu.
184      * @param pEncoding the encoding menu
185      * @param pGroup the button group
186      * @param pCharset the character set
187      */
188     private void addCharSetToMenu(final JMenu pEncoding,
189                                   final ButtonGroup pGroup,
190                                   final Charset pCharset) {
191         /* Create menu item */
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      * Handle selection.
201      */
202     private void handleSelect() {
203         /* Create and configure the file chooser */
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         /* Access the current selection */
210         final File myCurrent = ArduinoPreferences.getDefaultLocation();
211         if (myCurrent != null) {
212             myChooser.setCurrentDirectory(new File(myCurrent.getParent()));
213         }
214 
215         /* Select the file */
216         final int myResult = myChooser.showOpenDialog(theMain.getFrame());
217 
218         /* If we selected a file */
219         if (myResult == JFileChooser.APPROVE_OPTION) {
220             /* Access file and process it */
221             final File mySelected = myChooser.getSelectedFile();
222             handleNewSelection(mySelected);
223         }
224     }
225 
226     /**
227      * Handle new location.
228      * @param pSelected the selected file
229      */
230     private void handleNewSelection(final File pSelected) {
231         /* Access file and process it */
232         ArduinoException myError = theMain.setSelected(pSelected, theCharset);
233         if (myError == null) {
234             myError = ArduinoPreferences.storeDefaultLocation(pSelected);
235         }
236 
237         /* If we have an error */
238         if (myError != null) {
239             /* Record it */
240             theMain.setError(myError);
241         }
242     }
243 
244     /**
245      * Handle sketch.
246      */
247     private void handleSketch() {
248         /* Create and configure the file chooser */
249         final JFileChooser myChooser = new JFileChooser();
250         myChooser.setDialogTitle("Select Sketch directory");
251         myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
252 
253         /* Access the current selection */
254         File myCurrent = ArduinoPreferences.getSketchLocation();
255         if (myCurrent == null) {
256             /* Try the arduino base */
257             myCurrent = new File(ARDUINO_LOC);
258         }
259 
260         /* set current directory */
261         myChooser.setCurrentDirectory(myCurrent);
262 
263         /* Select the directory */
264         final int myResult = myChooser.showSaveDialog(theMain.getFrame());
265 
266         /* If we selected a file */
267         if (myResult == JFileChooser.APPROVE_OPTION) {
268             /* Access directory and process it */
269             final File myOutDir = myChooser.getSelectedFile();
270             handleWritingSketch(myOutDir);
271         }
272     }
273 
274     /**
275      * Handle writing sketch files.
276      * @param pOutDir the selected output directory
277      */
278     private void handleWritingSketch(final File pOutDir) {
279         /* Output the files */
280         ArduinoException myError = theMain.writeSketch(pOutDir, theCharset);
281         if (myError == null) {
282             myError = ArduinoPreferences.storeSketchLocation(pOutDir);
283         }
284 
285         /* record error */
286         if (myError != null) {
287             theMain.setError(myError);
288         }
289     }
290 
291     /**
292      * Handle library.
293      */
294     private void handleLibrary() {
295         /* Create and configure the file chooser */
296         final JFileChooser myChooser = new JFileChooser();
297         myChooser.setDialogTitle("Select Library directory");
298         myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
299 
300         /* Access the current selection */
301         File myCurrent = ArduinoPreferences.getLibraryLocation();
302         if (myCurrent == null) {
303             /* Try the arduino library base */
304             myCurrent = new File(ARDUINO_LOC + "/libraries");
305         }
306 
307         /* set current directory */
308         myChooser.setCurrentDirectory(myCurrent);
309 
310         /* Select the directory */
311         final int myResult = myChooser.showSaveDialog(theMain.getFrame());
312 
313         /* If we selected a file */
314         if (myResult == JFileChooser.APPROVE_OPTION) {
315             /* Access directory and process it */
316             final File myOutDir = myChooser.getSelectedFile();
317             handleWritingLibrary(myOutDir);
318         }
319     }
320 
321     /**
322      * Handle writing library files.
323      * @param pOutDir the selected output directory
324      */
325     private void handleWritingLibrary(final File pOutDir) {
326         /* Output the files */
327         ArduinoException myError = theMain.writeLibrary(pOutDir, theCharset);
328         if (myError == null) {
329             myError = ArduinoPreferences.storeLibraryLocation(pOutDir);
330         }
331 
332         /* record error */
333         if (myError != null) {
334             theMain.setError(myError);
335         }
336     }
337 
338     /**
339      * Set characterSet.
340      * @param pCharset the character set
341      */
342     private void setCharSet(final Charset pCharset) {
343         /* Store the characterSet */
344         theCharset = pCharset;
345         final ArduinoException myError = ArduinoPreferences.storeCharSet(theCharset);
346 
347         /* record error */
348         if (myError != null) {
349             theMain.setError(myError);
350         }
351     }
352 }