View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  
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   * A collection of standard utility methods for use within Swing.
31   *
32   * @author Paul Smith <psmith@apache.org>
33   */
34  public final class SwingHelper {
35      /**
36       * Centers the Component on screen.
37       *
38       * @param component
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       * This method configures a standard Cancel action, bound to the ESC key, to dispose of the dialog,
49       * and sets the buttons action to be this action, and adds the action to the dialog's rootPane
50       * action map
51       *
52       * @param dialog
53       * @param cancelButton
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              //use filedialog on mac
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 }