GrxGuiUtil.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxGuiUtil.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  */
00018 
00019 package com.generalrobotix.ui.util;
00020 
00021 import java.awt.Color;
00022 import java.awt.Component;
00023 import java.awt.Container;
00024 import java.awt.Dimension;
00025 import java.awt.Font;
00026 import java.awt.Frame;
00027 import java.awt.Insets;
00028 import java.io.File;
00029 import java.util.HashMap;
00030 import java.util.Iterator;
00031 import java.util.Set;
00032 
00033 import javax.swing.AbstractButton;
00034 import javax.swing.JButton;
00035 import javax.swing.JCheckBox;
00036 import javax.swing.JComponent;
00037 import javax.swing.JFileChooser;
00038 import javax.swing.JRadioButton;
00039 import javax.swing.JTextField;
00040 import javax.swing.JToggleButton;
00041 import javax.swing.UIDefaults;
00042 import javax.swing.UIManager;
00043 import javax.swing.border.LineBorder;
00044 import javax.swing.border.TitledBorder;
00045 import javax.swing.filechooser.FileFilter;
00046 import javax.swing.plaf.FontUIResource;
00047 
00048 public class GrxGuiUtil {
00049         public static void setTitleBorder(JComponent pane, String title) {
00050                 if (title != null) {
00051                         pane.setBorder(new TitledBorder(new LineBorder(Color.BLACK), title,
00052                                         TitledBorder.LEFT, TitledBorder.TOP));
00053                 } else {
00054                         pane.setBorder(new LineBorder(Color.BLACK));
00055                 }
00056         }
00057 
00058         public static void setWholeFont(Font f){
00059                 FontUIResource fontUIResource = new FontUIResource(f);
00060                 UIDefaults defaultTable = UIManager.getLookAndFeelDefaults();
00061                 Set<Object> set = defaultTable.keySet();
00062                 Iterator<Object> it = set.iterator();
00063                 while (it.hasNext()) {
00064                         Object o = it.next();
00065                         if (o instanceof String) {
00066                                 String s = (String) o;
00067                                 if (s.endsWith("font") || s.endsWith("Font"))
00068                                         UIManager.put(s, fontUIResource);
00069                         }
00070                 }
00071         }
00072 
00073         public static Frame getParentFrame(Component c) {
00074                 Component parent = c.getParent();
00075                 if (parent == null) {
00076                         return null;
00077                 }
00078                 if (parent instanceof Frame) {
00079                         return (Frame) parent;
00080                 }
00081                 return getParentFrame(parent);
00082         }
00083 
00084         public static void setButtonInsetsRecursive(Insets insets,
00085                         Container container) {
00086                 Component[] components = container.getComponents();
00087                 for (int i = 0; i < components.length; i++) {
00088                         Component c = components[i];
00089                         if (c instanceof JButton || c instanceof JToggleButton) {
00090                                 if (!(c instanceof JCheckBox) && !(c instanceof JRadioButton))
00091                                         ((AbstractButton) c).setMargin(insets);
00092                         } else if (c instanceof Container)
00093                                 setButtonInsetsRecursive(insets, (Container) c);
00094                 }
00095         }
00096 
00097         public static void setButtonSizeRecursive(Dimension size,
00098                         Container container) {
00099                 Component[] components = container.getComponents();
00100                 for (int i = 0; i < components.length; i++) {
00101                         Component c = components[i];
00102                         if (c instanceof JButton || c instanceof JToggleButton) {
00103                                 if (!(c instanceof JCheckBox) && !(c instanceof JRadioButton)) {
00104                                         ((AbstractButton) c).setPreferredSize(size);
00105                                         ((AbstractButton) c).setMaximumSize(size);
00106                                 }
00107                         } else if (c instanceof Container) {
00108                                 setButtonSizeRecursive(size, (Container) c);
00109                         }
00110                 }
00111         }
00112 
00113         public static JFileChooser createFileChooser(String title, File dir, String filter) {
00114                 return createFileChooser(title, dir, new String[] { filter });
00115         }
00116 
00117         public static JFileChooser createFileChooser(String title, File dir, String[] filter) {
00118                 JFileChooser jFileChooser = new JFileChooser();
00119                 // title
00120                 if (title != null)
00121                         jFileChooser.setDialogTitle(title);
00122                 // directory
00123                 if (dir == null)
00124                         dir = new File(System.getProperty("user.dir"));
00125                 jFileChooser.setCurrentDirectory(dir);
00126                 // filter
00127                 FileFilter[] ff = jFileChooser.getChoosableFileFilters();
00128                 for (int i = 1; i < ff.length; i++)
00129                         jFileChooser.removeChoosableFileFilter(ff[i]);
00130                 for (int i = 0; i < filter.length; i++) {
00131                         if (filter[i] != null && !filter[i].equals(""))
00132                                 jFileChooser.addChoosableFileFilter(createFileFilter(filter[i]));
00133                 }
00134                 return jFileChooser;
00135         }
00136 
00137         public static FileFilter createFileFilter(final String filter) {
00138                 return new FileFilter() {
00139                         public boolean accept(File f) {
00140                                 String ext = "";
00141                                 String path = f.getPath();
00142                                 int idx = path.lastIndexOf('.');
00143                                 if (idx > 0) {
00144                                         ext = path.substring(idx + 1).toLowerCase();
00145                                         if (ext.equals(filter))
00146                                                 return true;
00147                                 }
00148                                 return f.isDirectory();
00149                         }
00150 
00151                         public String getDescription() {
00152                                 return filter + " files (*." + filter + ")";
00153                         }
00154                 };
00155         }
00156         
00157         public static void setEnableRecursive(boolean e,Container container,HashMap except){
00158                 Component[] components = container.getComponents();
00159                 for (int i=0;i<components.length;i++){
00160                         Component c = components[i];
00161                         if (!(c instanceof JCheckBox) && 
00162                                  (c instanceof JButton || c instanceof JToggleButton)){
00163                                 if (except == null || except.get(((AbstractButton)c).getText()) == null)
00164                                         c.setEnabled(e);
00165                         } else if (c instanceof JTextField) {
00166                                 c.setEnabled(e);
00167                         } else if (c instanceof Container) {
00168                                 setEnableRecursive(e,(Container)c,except);
00169                         }
00170                 }
00171         }
00172 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16