forked from makerbot/ReplicatorG
/
MainWindow.java
executable file
·2472 lines (2119 loc) · 67.6 KB
/
MainWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Part of the ReplicatorG project - http://www.replicat.org
Copyright (c) 2008 Zach Smith
Forked from Arduino: http://www.arduino.cc
Based on Processing http://www.processing.org
Copyright (c) 2004-05 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: MainWindow.java 370 2008-01-19 16:37:19Z mellis $
*/
package replicatorg.app.ui;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.prefs.BackingStoreException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSplitPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.CompoundEdit;
import javax.swing.undo.UndoManager;
import net.miginfocom.swing.MigLayout;
import org.w3c.dom.Document;
import replicatorg.app.Base;
import replicatorg.app.MRUList;
import replicatorg.app.MachineController;
import replicatorg.app.MachineFactory;
import replicatorg.app.Serial;
import replicatorg.app.Base.InitialOpenBehavior;
import replicatorg.app.exceptions.SerialException;
import replicatorg.app.syntax.JEditTextArea;
import replicatorg.app.syntax.PdeKeywords;
import replicatorg.app.syntax.PdeTextAreaDefaults;
import replicatorg.app.syntax.SyntaxDocument;
import replicatorg.app.syntax.TextAreaPainter;
import replicatorg.app.ui.modeling.PreviewPanel;
import replicatorg.app.util.PythonUtils;
import replicatorg.app.util.SwingPythonSelector;
import replicatorg.drivers.EstimationDriver;
import replicatorg.drivers.OnboardParameters;
import replicatorg.drivers.SDCardCapture;
import replicatorg.drivers.UsesSerial;
import replicatorg.machine.MachineListener;
import replicatorg.machine.MachineProgressEvent;
import replicatorg.machine.MachineState;
import replicatorg.machine.MachineStateChangeEvent;
import replicatorg.machine.MachineToolStatusEvent;
import replicatorg.model.Build;
import replicatorg.model.BuildCode;
import replicatorg.model.BuildElement;
import replicatorg.model.BuildModel;
import replicatorg.model.JEditTextAreaSource;
import replicatorg.plugin.toolpath.ToolpathGenerator;
import replicatorg.plugin.toolpath.ToolpathGeneratorFactory;
import replicatorg.plugin.toolpath.ToolpathGeneratorThread;
import replicatorg.plugin.toolpath.ToolpathGeneratorFactory.ToolpathGeneratorDescriptor;
import replicatorg.uploader.FirmwareUploader;
import com.apple.mrj.MRJAboutHandler;
import com.apple.mrj.MRJApplicationUtils;
import com.apple.mrj.MRJOpenDocumentHandler;
import com.apple.mrj.MRJPrefsHandler;
import com.apple.mrj.MRJQuitHandler;
@SuppressWarnings("deprecation")
public class MainWindow extends JFrame implements MRJAboutHandler, MRJQuitHandler,
MRJPrefsHandler, MRJOpenDocumentHandler,
MachineListener, ChangeListener,
ToolpathGenerator.GeneratorListener
{
/**
*
*/
private static final long serialVersionUID = 4144538738677712284L;
static final String WINDOW_TITLE = "ReplicatorG" + " - "
+ Base.VERSION_NAME;
final static String MODEL_TAB_KEY = "MODEL";
final static String GCODE_TAB_KEY = "GCODE";
// p5 icon for the window
Image icon;
// our machines.xml document.
public Document dom;
MachineController machine;
static public final KeyStroke WINDOW_CLOSE_KEYSTROKE = KeyStroke
.getKeyStroke('W', Toolkit.getDefaultToolkit()
.getMenuShortcutKeyMask());
static final int HANDLE_NEW = 1;
static final int HANDLE_OPEN = 2;
static final int HANDLE_QUIT = 3;
int checkModifiedMode;
String handleOpenPath;
boolean handleNewShift;
PageFormat pageFormat;
PrinterJob printerJob;
MainButtonPanel buttons;
CardLayout cardLayout = new CardLayout();
JPanel cardPanel = new JPanel(cardLayout);
EditorHeader header = new EditorHeader(this);
{
header.setChangeListener(this);
}
MachineStatusPanel machineStatusPanel;
MessagePanel console;
JSplitPane splitPane;
JLabel lineNumberComponent;
// currently opened program
public Build build;
public JEditTextArea textarea;
public PreviewPanel previewPanel;
public SimulationThread simulationThread;
public EstimationThread estimationThread;
JMenuItem saveMenuItem;
JMenuItem saveAsMenuItem;
JMenuItem stopItem;
JMenuItem pauseItem;
JMenuItem controlPanelItem;
JMenuItem buildMenuItem;
JMenu machineMenu;
MachineMenuListener machineMenuListener;
public boolean building;
public boolean simulating;
public boolean debugging;
// boolean presenting;
// undo fellers
JMenuItem undoItem, redoItem;
protected UndoAction undoAction;
protected RedoAction redoAction;
public void updateUndo() {
undoAction.updateUndoState();
redoAction.updateRedoState();
}
// used internally, and only briefly
CompoundEdit compoundEdit;
FindReplace find;
public Build getBuild() { return build; }
private PreviewPanel getPreviewPanel() {
if (previewPanel == null) {
previewPanel = new PreviewPanel(this);
cardPanel.add(previewPanel,MODEL_TAB_KEY);
}
return previewPanel;
}
private MRUList mruList;
public MainWindow() {
super(WINDOW_TITLE);
setLocationByPlatform(true);
MRJApplicationUtils.registerAboutHandler(this);
MRJApplicationUtils.registerPrefsHandler(this);
MRJApplicationUtils.registerQuitHandler(this);
MRJApplicationUtils.registerOpenDocumentHandler(this);
PythonUtils.setSelector(new SwingPythonSelector(this));
// load up the most recently used files list
mruList = MRUList.getMRUList();
// set the window icon
icon = Base.getImage("images/icon.gif", this);
setIconImage(icon);
// add listener to handle window close box hit event
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
handleQuitInternal();
}
});
// don't close the window when clicked, the app will take care
// of that via the handleQuitInternal() methods
// http://dev.processing.org/bugs/show_bug.cgi?id=440
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
menubar.add(buildFileMenu());
menubar.add(buildEditMenu());
menubar.add(buildGCodeMenu());
menubar.add(buildMachineMenu());
menubar.add(buildHelpMenu());
setJMenuBar(menubar);
Container pane = getContentPane();
MigLayout layout = new MigLayout("nocache,fill,flowy,gap 0 0,ins 0");
pane.setLayout(layout);
buttons = new MainButtonPanel(this);
pane.add(buttons,"growx,dock north");
machineStatusPanel = new MachineStatusPanel();
pane.add(machineStatusPanel,"growx,dock north");
pane.add(header,"growx,dock north");
textarea = new JEditTextArea(new PdeTextAreaDefaults());
textarea.setRightClickPopup(new TextAreaPopup());
textarea.setHorizontalOffset(6);
cardPanel.add(textarea,GCODE_TAB_KEY);
console = new MessagePanel(this);
console.setBorder(null);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, cardPanel,
console);
//splitPane.setOneTouchExpandable(true);
// repaint child panes while resizing: a little heavyweight
// splitPane.setContinuousLayout(true);
// if window increases in size, give all of increase to
// the textarea in the uppper pane
splitPane.setResizeWeight(0.86);
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
//splitPane.setBorder(null);
// the default size on windows is too small and kinda ugly
int dividerSize = Base.preferences.getInt("editor.divider.size",5);
if (dividerSize < 5) dividerSize = 5;
if (dividerSize != 0) {
splitPane.setDividerSize(dividerSize);
}
splitPane.setPreferredSize(new Dimension(600,600));
pane.add(splitPane,"growx,growy,shrinkx,shrinky");
pack();
textarea.setTransferHandler(new TransferHandler() {
private static final long serialVersionUID = 2093323078348794384L;
public boolean canImport(JComponent dest, DataFlavor[] flavors) {
// claim that we can import everything
return true;
}
public boolean importData(JComponent src, Transferable transferable) {
DataFlavor[] flavors = transferable.getTransferDataFlavors();
int successful = 0;
for (int i = 0; i < flavors.length; i++) {
try {
// System.out.println(flavors[i]);
// System.out.println(transferable.getTransferData(flavors[i]));
Object stuff = transferable.getTransferData(flavors[i]);
if (!(stuff instanceof java.util.List<?>))
continue;
java.util.List<?> list = (java.util.List<?>) stuff;
for (int j = 0; j < list.size(); j++) {
Object item = list.get(j);
if (item instanceof File) {
File file = (File) item;
// see if this is a .gcode file to be opened
String filename = file.getName();
if (filename.endsWith(".gcode")) {
handleOpenFile(file);
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
if (successful == 0) {
error("No files were added to the sketch.");
} else if (successful == 1) {
message("One file added to the sketch.");
} else {
message(successful + " files added to the sketch.");
}
return true;
}
});
}
// ...................................................................
/**
* Post-constructor setup for the editor area. Loads the last sketch that
* was used (if any), and restores other MainWindow settings. The complement to
* "storePreferences", this is called when the application is first
* launched.
*/
public void restorePreferences() {
if (Base.openedAtStartup != null) {
handleOpen2(Base.openedAtStartup);
} else {
// last sketch that was in use, or used to launch the app
final String prefName = "replicatorg.initialopenbehavior";
int ordinal = Base.preferences.getInt(prefName, InitialOpenBehavior.OPEN_LAST.ordinal());
final InitialOpenBehavior openBehavior = InitialOpenBehavior.values()[ordinal];
if (openBehavior == InitialOpenBehavior.OPEN_NEW) {
handleNew2(true);
} else {
// Get last path opened; MRU keeps this.
Iterator<String> i = mruList.iterator();
if (i.hasNext()) {
String lastOpened = i.next();
if (new File(lastOpened).exists()) {
handleOpen2(lastOpened);
} else {
handleNew2(true);
}
} else {
handleNew2(true);
}
}
}
// read the preferences that are settable in the preferences window
applyPreferences();
}
/**
* Read and apply new values from the preferences, either because the app is
* just starting up, or the user just finished messing with things in the
* Preferences window.
*/
public void applyPreferences() {
// apply the setting for 'use external editor'
boolean external = Base.preferences.getBoolean("editor.external",false);
textarea.setEditable(!external);
saveMenuItem.setEnabled(!external);
saveAsMenuItem.setEnabled(!external);
// beautifyMenuItem.setEnabled(!external);
TextAreaPainter painter = textarea.getPainter();
if (external) {
// disable line highlight and turn off the caret when disabling
Color color = Base.getColorPref("editor.external.bgcolor","#168299");
painter.setBackground(color);
painter.setLineHighlightEnabled(false);
textarea.setCaretVisible(false);
} else {
Color color = Base.getColorPref("editor.bgcolor","#ffffff");
painter.setBackground(color);
boolean highlight = Base.preferences.getBoolean("editor.linehighlight",true);
painter.setLineHighlightEnabled(highlight);
textarea.setCaretVisible(true);
}
// apply changes to the font size for the editor
// TextAreaPainter painter = textarea.getPainter();
painter.setFont(Base.getFontPref("editor.font","Monospaced,plain,10"));
// Font font = painter.getFont();
// textarea.getPainter().setFont(new Font("Courier", Font.PLAIN, 36));
// in case moved to a new location
// For 0125, changing to async version (to be implemented later)
// sketchbook.rebuildMenus();
//sketchbook.rebuildMenusAsync();
}
/**
* Store preferences about the editor's current state. Called when the
* application is quitting.
*/
public void storePreferences() {
// System.out.println("storing preferences");
// window location information
Rectangle bounds = getBounds();
Base.preferences.putInt("last.window.x", bounds.x);
Base.preferences.putInt("last.window.y", bounds.y);
Base.preferences.putInt("last.window.width", bounds.width);
Base.preferences.putInt("last.window.height", bounds.height);
// last sketch that was in use
// Preferences.set("last.sketch.name", sketchName);
// Preferences.set("last.sketch.name", sketch.name);
if (build != null) {
String lastPath = build.getMainFilePath();
if (lastPath != null) {
Base.preferences.put("last.sketch.path", build.getMainFilePath());
}
}
// location for the console/editor area divider
int location = splitPane.getDividerLocation();
Base.preferences.putInt("last.divider.location", location);
try {
Base.preferences.flush();
} catch (BackingStoreException bse) {
// Not much we can do about this, so let it go with a stack
// trace.
bse.printStackTrace();
}
}
public void runToolpathGenerator() {
// Check for modified STL
if (build.getModel().isModified()) {
final String message = "<html>You have made changes to this model. Any unsaved changes will<br>" +
"not be reflected in the generated toolpath.<br>" +
"Save the model now?</html>";
int option = JOptionPane.showConfirmDialog(this, message, "Save model?",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (option == JOptionPane.CANCEL_OPTION) { return; }
if (option == JOptionPane.YES_OPTION) {
// save model
handleSave(true);
}
}
ToolpathGenerator generator = ToolpathGeneratorFactory.createSelectedGenerator();
ToolpathGeneratorThread tgt = new ToolpathGeneratorThread(this, generator, build);
tgt.addListener(this);
tgt.start();
}
// ...................................................................
private JMenu serialMenu = null;
private void reloadSerialMenu() {
if (serialMenu == null) return;
serialMenu.removeAll();
if (machine == null) {
JMenuItem item = new JMenuItem("No machine selected.");
item.setEnabled(false);
serialMenu.add(item);
return;
} else if (!(machine.driver instanceof UsesSerial)) {
JMenuItem item = new JMenuItem("Currently selected machine does not use a serial port.");
item.setEnabled(false);
serialMenu.add(item);
return;
}
String currentName = null;
UsesSerial us = (UsesSerial)machine.driver;
if (us.getSerial() != null) {
currentName = us.getSerial().getName();
}
Vector<Serial.Name> names = Serial.scanSerialNames();
Collections.sort(names);
for (Serial.Name name : names) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(name.getName());
item.setEnabled(name.isAvailable());
item.setSelected(name.getName().equals(currentName));
final String portName = name.getName();
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread t = new Thread() {
public void run() {
try {
UsesSerial us = (UsesSerial)machine.driver;
if (us != null) synchronized(us) {
us.setSerial(new Serial(portName, us));
Base.preferences.put("serial.last_selected", portName);
machine.reset();
}
} catch (SerialException se) {
se.printStackTrace();
}
}
};
t.start();
}
});
serialMenu.add(item);
}
if (names.isEmpty()) {
JMenuItem item = new JMenuItem("No serial ports detected");
item.setEnabled(false);
serialMenu.add(item);
}
serialMenu.addSeparator();
JMenuItem item = new JMenuItem("Rescan serial ports");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reloadSerialMenu();
}
});
serialMenu.add(item);
}
private JMenu mruMenu = null;
private void reloadMruMenu() {
if (mruMenu == null) {
return;
}
mruMenu.removeAll();
class FileOpenActionListener implements ActionListener {
public String path;
FileOpenActionListener(String path) {
this.path = path;
}
public void actionPerformed(ActionEvent e) {
handleOpen(path);
}
}
if (mruList != null) {
int index = 0;
for (String fileName : mruList) {
String entry = Integer.toString(index) + ". "
+ fileName.substring(fileName.lastIndexOf('/') + 1);
JMenuItem item = new JMenuItem(entry, KeyEvent.VK_0 + index);
item.addActionListener(new FileOpenActionListener(fileName));
mruMenu.add(item);
index = index + 1;
if (index >= 9) {
break;
}
}
}
}
protected JMenu buildFileMenu() {
JMenuItem item;
JMenu menu = new JMenu("File");
item = newJMenuItem("New", 'N');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNew(false);
}
});
menu.add(item);
item = newJMenuItem("Open...", 'O', false);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleOpen(null);
}
});
menu.add(item);
saveMenuItem = newJMenuItem("Save", 'S');
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleSave(false);
}
});
menu.add(saveMenuItem);
saveAsMenuItem = newJMenuItem("Save As...", 'S', true);
saveAsMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleSaveAs();
}
});
menu.add(saveAsMenuItem);
menu.addSeparator();
mruMenu = new JMenu("Recent");
reloadMruMenu();
menu.add(mruMenu);
// macosx already has its own preferences and quit menu
if (!Base.isMacOS()) {
menu.addSeparator();
item = newJMenuItem("Preferences", ',');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePrefs();
}
});
menu.add(item);
menu.addSeparator();
item = newJMenuItem("Quit", 'Q');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleQuitInternal();
}
});
menu.add(item);
}
return menu;
}
protected JMenu buildGCodeMenu() {
JMenuItem item;
JMenu menu = new JMenu("GCode");
item = newJMenuItem("Estimate", 'E');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleEstimate();
}
});
menu.add(item);
item = newJMenuItem("Simulate", 'L');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleSimulate();
}
});
menu.add(item);
buildMenuItem = newJMenuItem("Build", 'B');
buildMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleBuild();
}
});
menu.add(buildMenuItem);
pauseItem = newJMenuItem("Pause", 'E');
pauseItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePause();
}
});
pauseItem.setEnabled(false);
menu.add(pauseItem);
stopItem = newJMenuItem("Stop", '.');
stopItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleStop();
}
});
stopItem.setEnabled(false);
menu.add(stopItem);
// GENERATOR
JMenu genMenu = new JMenu("Choose GCode generator");
Vector<ToolpathGeneratorDescriptor> generators = ToolpathGeneratorFactory.getGeneratorList();
String name = ToolpathGeneratorFactory.getSelectedName();
ButtonGroup group = new ButtonGroup();
for (ToolpathGeneratorDescriptor tgd : generators) {
JRadioButtonMenuItem i = new JRadioButtonMenuItem(tgd.name);
group.add(i);
final String n = tgd.name;
i.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ToolpathGeneratorFactory.setSelectedName(n);
}
});
if (name.equals(tgd.name)) { i.setSelected(true); }
genMenu.add(i);
}
menu.add(genMenu);
return menu;
}
JMenuItem onboardParamsItem = new JMenuItem("Cupcake Onboard Preferences");
JMenuItem extruderParamsItem = new JMenuItem("Extruder Onboard Preferences");
protected JMenu buildMachineMenu() {
JMenuItem item;
JMenu menu = new JMenu("Machine");
machineMenu = new JMenu("Driver");
populateMachineMenu();
menu.add(machineMenu);
menu.addMenuListener(new MenuListener() {
public void menuCanceled(MenuEvent e) {
}
public void menuDeselected(MenuEvent e) {
}
public void menuSelected(MenuEvent e) {
populateMachineMenu();
}
});
machineMenuListener = new MachineMenuListener();
serialMenu = new JMenu("Serial Port");
reloadSerialMenu();
menu.add(serialMenu);
controlPanelItem = new JMenuItem("Control Panel", 'C');
controlPanelItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J,ActionEvent.CTRL_MASK));
controlPanelItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleControlPanel();
}
});
menu.add(controlPanelItem);
onboardParamsItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleOnboardPrefs();
}
});
onboardParamsItem.setVisible(false);
menu.add(onboardParamsItem);
extruderParamsItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleExtruderPrefs();
}
});
extruderParamsItem.setVisible(false);
menu.add(extruderParamsItem);
item = new JMenuItem("Upload new firmware...");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FirmwareUploader.startUploader(MainWindow.this);
}
});
menu.add(item);
return menu;
}
class MachineMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (machineMenu == null) {
System.out.println("machineMenu is null");
return;
}
int count = machineMenu.getItemCount();
for (int i = 0; i < count; i++) {
((JCheckBoxMenuItem) machineMenu.getItem(i)).setState(false);
}
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
item.setState(true);
final String name = item.getText();
Base.preferences.put("machine.name", name);
// load it and set it.
Thread t = new Thread() {
public void run() {
loadMachine(name);
}
};
t.start();
}
}
protected void populateMachineMenu() {
machineMenu.removeAll();
boolean empty = true;
try {
for (String name : MachineFactory.getMachineNames() ) {
JMenuItem rbMenuItem = new JCheckBoxMenuItem(name,
name.equals(Base.preferences
.get("machine.name",null)));
rbMenuItem.addActionListener(machineMenuListener);
machineMenu.add(rbMenuItem);
empty = false;
}
if (!empty) {
// System.out.println("enabling the machineMenu");
machineMenu.setEnabled(true);
}
} catch (Exception exception) {
System.out.println("error retrieving machine list");
exception.printStackTrace();
}
if (machineMenu.getItemCount() == 0)
machineMenu.setEnabled(false);
}
protected JMenu buildHelpMenu() {
JMenu menu = new JMenu("Help");
JMenuItem item;
if (!Base.isLinux()) {
item = newJMenuItem("Getting Started", '1');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://www.replicat.org/getting-started");
}
});
menu.add(item);
}
item = newJMenuItem("Hardware Setup", '2');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://www.replicat.org/hardware");
}
});
menu.add(item);
item = newJMenuItem("Troubleshooting", '3');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://www.replicat.org/troubleshooting");
}
});
menu.add(item);
item = newJMenuItem("Reference", '4');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://www.replicat.org/reference");
}
});
menu.add(item);
item = newJMenuItem("Frequently Asked Questions", '5');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://www.replicat.org/faq");
}
});
menu.add(item);
item = newJMenuItem("Visit Replicat.orG", '6');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.openURL("http://www.replicat.org/");
}
});
menu.add(item);
// macosx already has its own about menu
menu.addSeparator();
JMenuItem aboutitem = new JMenuItem("About ReplicatorG");
aboutitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleAbout();
}
});
menu.add(aboutitem);
return menu;
}
public JMenu buildEditMenu() {
JMenu menu = new JMenu("Edit");
JMenuItem item;
undoItem = newJMenuItem("Undo", 'Z');
undoItem.addActionListener(undoAction = new UndoAction());
menu.add(undoItem);
redoItem = newJMenuItem("Redo", 'Y');
redoItem.addActionListener(redoAction = new RedoAction());
menu.add(redoItem);
menu.addSeparator();
// TODO "cut" and "copy" should really only be enabled
// if some text is currently selected
item = newJMenuItem("Cut", 'X');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textarea.cut();
build.getCode().setModified(true);
}
});
menu.add(item);
item = newJMenuItem("Copy", 'C');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textarea.copy();
}
});
menu.add(item);
item = newJMenuItem("Paste", 'V');
item.addActionListener(new ActionListener() {