1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw.helper;
19
20 import javax.swing.*;
21 import java.awt.*;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.KeyEvent;
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
28
29
30
31
32
33
34 public final class SwingHelper {
35
36
37
38
39
40 public static void centerOnScreen(Component component) {
41 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
42 component.setLocation(
43 (screenSize.width / 2) - (component.getWidth() / 2),
44 (screenSize.height / 2) - (component.getHeight() / 2));
45 }
46
47
48
49
50
51
52
53
54
55 public static void configureCancelForDialog(final JDialog dialog, JButton cancelButton) {
56 String CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";
57 int noModifiers = 0;
58 KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, noModifiers, false);
59 InputMap inputMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
60 inputMap.put(escapeKey, CANCEL_ACTION_KEY);
61
62 Action closeAction = new AbstractAction("Cancel") {
63
64 public void actionPerformed(ActionEvent arg0) {
65 dialog.dispose();
66 }
67 };
68 cancelButton.setAction(closeAction);
69 dialog.getRootPane().getActionMap().put(CANCEL_ACTION_KEY, closeAction);
70
71 }
72
73 public static void invokeOnEDT(Runnable runnable) {
74 if (EventQueue.isDispatchThread()) {
75 runnable.run();
76 } else {
77 EventQueue.invokeLater(runnable);
78 }
79 }
80
81 public static boolean isMacOSX() {
82 return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("mac os x");
83 }
84
85 public static List<JButton> orderOKCancelButtons(JButton okButton, JButton cancelButton) {
86 List<JButton> result = new ArrayList<>();
87 if (isMacOSX()) {
88 result.add(cancelButton);
89 result.add(okButton);
90 } else {
91 result.add(okButton);
92 result.add(cancelButton);
93 }
94 return result;
95 }
96
97 public static File promptForFile(Container parent, String defaultPath, String title, boolean loadDialog) {
98 if (SwingHelper.isMacOSX()) {
99
100 Component root = SwingUtilities.getRoot(parent);
101 Frame frame = null;
102 if (root instanceof Frame) {
103 frame = (Frame) root;
104 }
105
106 FileDialog fileDialog = new FileDialog(frame, title);
107 fileDialog.setModal(true);
108 fileDialog.setMode(loadDialog ? FileDialog.LOAD : FileDialog.SAVE);
109 if (defaultPath != null) {
110 fileDialog.setDirectory(defaultPath);
111 }
112 fileDialog.setVisible(true);
113 String fileString = fileDialog.getFile();
114 if (fileString == null) {
115 return null;
116 }
117 if (fileDialog.getDirectory() != null) {
118 return new File(fileDialog.getDirectory(), fileString);
119 } else {
120 return new File(fileString);
121 }
122 } else {
123
124 JFileChooser chooser;
125 if (defaultPath != null) {
126 chooser = new JFileChooser(defaultPath);
127 } else {
128 chooser = new JFileChooser();
129 }
130
131 chooser.setDialogTitle(title);
132
133 chooser.setAcceptAllFileFilterUsed(true);
134
135 int i;
136 if (loadDialog) {
137 i = chooser.showOpenDialog(parent);
138 } else {
139 i = chooser.showSaveDialog(parent);
140 }
141
142 if (i != JFileChooser.APPROVE_OPTION) {
143 return null;
144 }
145 return chooser.getSelectedFile();
146 }
147 }
148 }