|
3 | 3 | import com.willwinder.universalgcodesender.i18n.Localization;
|
4 | 4 | import com.willwinder.universalgcodesender.model.UnitUtils;
|
5 | 5 | import com.willwinder.universalgcodesender.uielements.components.GcodeFileTypeFilter;
|
6 |
| -import java.awt.Component; |
7 |
| -import java.awt.Container; |
8 |
| -import java.io.File; |
9 |
| -import java.util.Optional; |
| 6 | + |
10 | 7 | import javax.swing.JComponent;
|
11 | 8 | import javax.swing.JFileChooser;
|
12 | 9 | import javax.swing.JFrame;
|
13 | 10 | import javax.swing.SpinnerNumberModel;
|
14 | 11 | import javax.swing.filechooser.FileFilter;
|
| 12 | +import java.awt.Component; |
| 13 | +import java.awt.Container; |
| 14 | +import java.awt.Frame; |
| 15 | +import java.io.File; |
| 16 | +import java.util.Optional; |
15 | 17 |
|
16 | 18 | /**
|
17 |
| - * |
18 | 19 | * @author wwinder
|
19 | 20 | */
|
20 | 21 | public class SwingHelpers {
|
21 |
| - private static final String[] UNIT_OPTIONS = { |
22 |
| - Localization.getString("mainWindow.swing.mmRadioButton"), |
23 |
| - Localization.getString("mainWindow.swing.inchRadioButton") |
24 |
| - }; |
25 |
| - |
26 |
| - public static String[] getUnitOptions() { |
27 |
| - return UNIT_OPTIONS; |
28 |
| - } |
29 |
| - |
30 |
| - public static UnitUtils.Units selectedUnit(int idx) { |
31 |
| - return idx == 0 ? UnitUtils.Units.MM : UnitUtils.Units.INCH; |
32 |
| - } |
33 |
| - |
34 |
| - public static int unitIdx(UnitUtils.Units units) { |
35 |
| - if (units == UnitUtils.Units.INCH) { |
36 |
| - return 1; |
37 |
| - } else { |
38 |
| - return 0; |
| 22 | + private static final String[] UNIT_OPTIONS = { |
| 23 | + Localization.getString("mainWindow.swing.mmRadioButton"), |
| 24 | + Localization.getString("mainWindow.swing.inchRadioButton") |
| 25 | + }; |
| 26 | + |
| 27 | + public static String[] getUnitOptions() { |
| 28 | + return UNIT_OPTIONS; |
39 | 29 | }
|
40 |
| - } |
41 |
| - |
42 |
| - // deal with casting the spinner model to a double. |
43 |
| - public static double getDouble(SpinnerNumberModel model) { |
44 |
| - return (double) model.getValue(); |
45 |
| - } |
46 |
| - |
47 |
| - // deal with casting the spinner model to a double. |
48 |
| - public static int getInt(SpinnerNumberModel model) { |
49 |
| - return (int) model.getValue(); |
50 |
| - } |
51 |
| - |
52 |
| - public static Optional<File> createFile(String sourceDir) { |
53 |
| - JFileChooser fileChooser = GcodeFileTypeFilter.getGcodeFileChooser(sourceDir); |
54 |
| - int returnVal = fileChooser.showSaveDialog(new JFrame()); |
55 |
| - if (returnVal == JFileChooser.APPROVE_OPTION) { |
56 |
| - return Optional.ofNullable(fileChooser.getSelectedFile()); |
57 |
| - } else { |
58 |
| - return Optional.empty(); |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - public static Optional<File> openFile(String sourceDir) { |
63 |
| - JFileChooser fileChooser = GcodeFileTypeFilter.getGcodeFileChooser(sourceDir); |
64 |
| - int returnVal = fileChooser.showOpenDialog(new JFrame()); |
65 |
| - if (returnVal == JFileChooser.APPROVE_OPTION) { |
66 |
| - return Optional.ofNullable(fileChooser.getSelectedFile()); |
67 |
| - } else { |
68 |
| - return Optional.empty(); |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - public static Optional<File> openDirectory(String title, File defaultDirectory) { |
73 |
| - JFileChooser chooser = new JFileChooser(); |
74 |
| - if(defaultDirectory != null && defaultDirectory.isDirectory()) { |
75 |
| - chooser.setCurrentDirectory(defaultDirectory); |
| 30 | + |
| 31 | + public static UnitUtils.Units selectedUnit(int idx) { |
| 32 | + return idx == 0 ? UnitUtils.Units.MM : UnitUtils.Units.INCH; |
76 | 33 | }
|
77 |
| - chooser.setDialogTitle(title); |
78 |
| - chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
79 |
| - chooser.setAcceptAllFileFilterUsed(false); |
80 |
| - chooser.setFileFilter(new FileFilter() { |
81 |
| - @Override |
82 |
| - public boolean accept(File f) { |
83 |
| - return f.isDirectory(); |
84 |
| - } |
85 |
| - |
86 |
| - @Override |
87 |
| - public String getDescription() { |
88 |
| - return "Directories"; |
89 |
| - } |
90 |
| - }); |
91 |
| - |
92 |
| - if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
93 |
| - return Optional.of(chooser.getSelectedFile()); |
| 34 | + |
| 35 | + // deal with casting the spinner model to a double. |
| 36 | + public static double getDouble(SpinnerNumberModel model) { |
| 37 | + return (double) model.getValue(); |
94 | 38 | }
|
95 | 39 |
|
96 |
| - return Optional.empty(); |
97 |
| - } |
98 |
| - |
99 |
| - @FunctionalInterface |
100 |
| - public interface ApplyToComponent { |
101 |
| - public void apply(JComponent component); |
102 |
| - } |
103 |
| - |
104 |
| - /** |
105 |
| - * Provides component hierarchy traversal. |
106 |
| - * |
107 |
| - * @param aContainer start node for the traversal. |
108 |
| - */ |
109 |
| - public static void traverse(Container aContainer, ApplyToComponent applicator) { |
110 |
| - for (final Component comp : aContainer.getComponents()) { |
111 |
| - if (comp instanceof JComponent) { |
112 |
| - applicator.apply((JComponent) comp); |
113 |
| - } |
114 |
| - if (comp instanceof Container) { |
115 |
| - traverse((Container) comp, applicator); |
116 |
| - } |
117 |
| - } |
118 |
| - } |
| 40 | + // deal with casting the spinner model to a double. |
| 41 | + public static int getInt(SpinnerNumberModel model) { |
| 42 | + return (int) model.getValue(); |
| 43 | + } |
| 44 | + |
| 45 | + public static Optional<File> createFile(String sourceDir) { |
| 46 | + JFileChooser fileChooser = GcodeFileTypeFilter.getGcodeFileChooser(sourceDir); |
| 47 | + int returnVal = fileChooser.showSaveDialog(new JFrame()); |
| 48 | + if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 49 | + return Optional.ofNullable(fileChooser.getSelectedFile()); |
| 50 | + } else { |
| 51 | + return Optional.empty(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static Optional<File> openFile(String sourceDir) { |
| 56 | + JFileChooser fileChooser = GcodeFileTypeFilter.getGcodeFileChooser(sourceDir); |
| 57 | + int returnVal = fileChooser.showOpenDialog(new JFrame()); |
| 58 | + if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 59 | + return Optional.ofNullable(fileChooser.getSelectedFile()); |
| 60 | + } else { |
| 61 | + return Optional.empty(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static Optional<File> openDirectory(String title, File defaultDirectory) { |
| 66 | + JFileChooser chooser = new JFileChooser(); |
| 67 | + if (defaultDirectory != null && defaultDirectory.isDirectory()) { |
| 68 | + chooser.setCurrentDirectory(defaultDirectory); |
| 69 | + } |
| 70 | + chooser.setDialogTitle(title); |
| 71 | + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| 72 | + chooser.setAcceptAllFileFilterUsed(false); |
| 73 | + chooser.setFileFilter(new FileFilter() { |
| 74 | + @Override |
| 75 | + public boolean accept(File f) { |
| 76 | + return f.isDirectory(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getDescription() { |
| 81 | + return "Directories"; |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
| 86 | + return Optional.of(chooser.getSelectedFile()); |
| 87 | + } |
| 88 | + |
| 89 | + return Optional.empty(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Provides component hierarchy traversal. |
| 94 | + * |
| 95 | + * @param aContainer start node for the traversal. |
| 96 | + */ |
| 97 | + public static void traverse(Container aContainer, ApplyToComponent applicator) { |
| 98 | + for (final Component comp : aContainer.getComponents()) { |
| 99 | + if (comp instanceof JComponent) { |
| 100 | + applicator.apply((JComponent) comp); |
| 101 | + } |
| 102 | + if (comp instanceof Container) { |
| 103 | + traverse((Container) comp, applicator); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static Frame getRootFrame() { |
| 109 | + Frame[] frames = Frame.getFrames(); |
| 110 | + if (frames.length == 0) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + return frames[0]; |
| 115 | + } |
| 116 | + |
| 117 | + @FunctionalInterface |
| 118 | + public interface ApplyToComponent { |
| 119 | + public void apply(JComponent component); |
| 120 | + } |
119 | 121 | }
|
0 commit comments