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