GrxGuiUtil.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * GrxGuiUtil.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro Kawasumi (General Robotix, Inc.)
17  */
18 
19 package com.generalrobotix.ui.util;
20 
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.Font;
26 import java.awt.Frame;
27 import java.awt.Insets;
28 import java.io.File;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Set;
32 
33 import javax.swing.AbstractButton;
34 import javax.swing.JButton;
35 import javax.swing.JCheckBox;
36 import javax.swing.JComponent;
37 import javax.swing.JFileChooser;
38 import javax.swing.JRadioButton;
39 import javax.swing.JTextField;
40 import javax.swing.JToggleButton;
41 import javax.swing.UIDefaults;
42 import javax.swing.UIManager;
43 import javax.swing.border.LineBorder;
44 import javax.swing.border.TitledBorder;
45 import javax.swing.filechooser.FileFilter;
46 import javax.swing.plaf.FontUIResource;
47 
48 public class GrxGuiUtil {
49  public static void setTitleBorder(JComponent pane, String title) {
50  if (title != null) {
51  pane.setBorder(new TitledBorder(new LineBorder(Color.BLACK), title,
52  TitledBorder.LEFT, TitledBorder.TOP));
53  } else {
54  pane.setBorder(new LineBorder(Color.BLACK));
55  }
56  }
57 
58  public static void setWholeFont(Font f){
59  FontUIResource fontUIResource = new FontUIResource(f);
60  UIDefaults defaultTable = UIManager.getLookAndFeelDefaults();
61  Set<Object> set = defaultTable.keySet();
62  Iterator<Object> it = set.iterator();
63  while (it.hasNext()) {
64  Object o = it.next();
65  if (o instanceof String) {
66  String s = (String) o;
67  if (s.endsWith("font") || s.endsWith("Font"))
68  UIManager.put(s, fontUIResource);
69  }
70  }
71  }
72 
73  public static Frame getParentFrame(Component c) {
74  Component parent = c.getParent();
75  if (parent == null) {
76  return null;
77  }
78  if (parent instanceof Frame) {
79  return (Frame) parent;
80  }
81  return getParentFrame(parent);
82  }
83 
84  public static void setButtonInsetsRecursive(Insets insets,
85  Container container) {
86  Component[] components = container.getComponents();
87  for (int i = 0; i < components.length; i++) {
88  Component c = components[i];
89  if (c instanceof JButton || c instanceof JToggleButton) {
90  if (!(c instanceof JCheckBox) && !(c instanceof JRadioButton))
91  ((AbstractButton) c).setMargin(insets);
92  } else if (c instanceof Container)
93  setButtonInsetsRecursive(insets, (Container) c);
94  }
95  }
96 
97  public static void setButtonSizeRecursive(Dimension size,
98  Container container) {
99  Component[] components = container.getComponents();
100  for (int i = 0; i < components.length; i++) {
101  Component c = components[i];
102  if (c instanceof JButton || c instanceof JToggleButton) {
103  if (!(c instanceof JCheckBox) && !(c instanceof JRadioButton)) {
104  ((AbstractButton) c).setPreferredSize(size);
105  ((AbstractButton) c).setMaximumSize(size);
106  }
107  } else if (c instanceof Container) {
108  setButtonSizeRecursive(size, (Container) c);
109  }
110  }
111  }
112 
113  public static JFileChooser createFileChooser(String title, File dir, String filter) {
114  return createFileChooser(title, dir, new String[] { filter });
115  }
116 
117  public static JFileChooser createFileChooser(String title, File dir, String[] filter) {
118  JFileChooser jFileChooser = new JFileChooser();
119  // title
120  if (title != null)
121  jFileChooser.setDialogTitle(title);
122  // directory
123  if (dir == null)
124  dir = new File(System.getProperty("user.dir"));
125  jFileChooser.setCurrentDirectory(dir);
126  // filter
127  FileFilter[] ff = jFileChooser.getChoosableFileFilters();
128  for (int i = 1; i < ff.length; i++)
129  jFileChooser.removeChoosableFileFilter(ff[i]);
130  for (int i = 0; i < filter.length; i++) {
131  if (filter[i] != null && !filter[i].equals(""))
132  jFileChooser.addChoosableFileFilter(createFileFilter(filter[i]));
133  }
134  return jFileChooser;
135  }
136 
137  public static FileFilter createFileFilter(final String filter) {
138  return new FileFilter() {
139  public boolean accept(File f) {
140  String ext = "";
141  String path = f.getPath();
142  int idx = path.lastIndexOf('.');
143  if (idx > 0) {
144  ext = path.substring(idx + 1).toLowerCase();
145  if (ext.equals(filter))
146  return true;
147  }
148  return f.isDirectory();
149  }
150 
151  public String getDescription() {
152  return filter + " files (*." + filter + ")";
153  }
154  };
155  }
156 
157  public static void setEnableRecursive(boolean e,Container container,HashMap except){
158  Component[] components = container.getComponents();
159  for (int i=0;i<components.length;i++){
160  Component c = components[i];
161  if (!(c instanceof JCheckBox) &&
162  (c instanceof JButton || c instanceof JToggleButton)){
163  if (except == null || except.get(((AbstractButton)c).getText()) == null)
164  c.setEnabled(e);
165  } else if (c instanceof JTextField) {
166  c.setEnabled(e);
167  } else if (c instanceof Container) {
168  setEnableRecursive(e,(Container)c,except);
169  }
170  }
171  }
172 }
int c
Definition: autoplay.py:16
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_uint_32 size
Definition: png.h:1521
png_uint_32 i
Definition: png.h:2735
static JFileChooser createFileChooser(String title, File dir, String[] filter)
static FileFilter createFileFilter(final String filter)
static Frame getParentFrame(Component c)
Definition: GrxGuiUtil.java:73
static void setTitleBorder(JComponent pane, String title)
Definition: GrxGuiUtil.java:49
path
static JFileChooser createFileChooser(String title, File dir, String filter)
static void setEnableRecursive(boolean e, Container container, HashMap except)
static void setButtonSizeRecursive(Dimension size, Container container)
Definition: GrxGuiUtil.java:97
static void setButtonInsetsRecursive(Insets insets, Container container)
Definition: GrxGuiUtil.java:84


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:21