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 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   * A collection of standard utility methods for use within Swing.
46   * 
47   * @author Paul Smith <psmith@apache.org>
48   *
49   */
50  public final class SwingHelper {
51    /**
52     * Centers the Component on screen.
53     *
54     * @param component
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     * This method configures a standard Cancel action, bound to the ESC key, to dispose of the dialog,
65     * and sets the buttons action to be this action, and adds the action to the dialog's rootPane 
66     * action map
67     * @param dialog
68     * @param cancelButton
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             //use filedialog on mac
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 }