1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.Component ; 33 import java.awt.Container ; 34 import java.awt.Dimension ; 35 import java.awt.Frame ; 36 import java.io.File ; 37 import java.util.ArrayList ; 38 39 import javax.swing.JButton ; 40 import javax.swing.JCheckBox ; 41 import javax.swing.JComponent ; 42 import javax.swing.JFrame ; 43 import javax.swing.JLabel ; 44 import javax.swing.JOptionPane ; 45 import javax.swing.JRadioButton ; 46 47 import com.genimen.djeneric.language.Messages; 48 49 public class Util 50 { 51 public static Frame findFrame(Component c) 52 { 53 if (c instanceof Frame ) return (Frame ) c; 54 Component parent = c.getParent(); 55 if (parent != null) return findFrame(parent); 56 return null; 57 } 58 59 public static Frame findActiveFrame() 60 { 61 Frame [] frames = JFrame.getFrames(); 62 for (int i = 0; i < frames.length; i++) 63 { 64 Frame frame = frames[i]; 65 if (frame.isVisible()) 66 { 67 return frame; 68 } 69 } 70 return null; 71 } 72 73 public static void sizeButtons(Container panel) 74 { 75 sizeButtons(panel, true); 76 } 77 78 public static void sizeButtons(Container panel, boolean recurse) 79 { 80 ArrayList lst = new ArrayList (); 81 addButtons(panel, lst, recurse); 82 sizeButtons((JButton []) lst.toArray(new JButton [0])); 83 } 84 85 private static void addButtons(Container panel, ArrayList lst, boolean recurse) 86 { 87 for (int i = 0; i < panel.getComponentCount(); i++) 88 { 89 if (panel.getComponent(i) instanceof JButton ) lst.add(panel.getComponent(i)); 90 if (recurse && panel.getComponent(i) instanceof Container ) addButtons((Container ) panel.getComponent(i), lst, 91 recurse); 92 } 93 } 94 95 public static void sizeLabels(Container panel) 96 { 97 sizeLabels(panel, true); 98 } 99 100 public static void sizeLabels(Container panel, boolean recurse) 101 { 102 ArrayList lst = new ArrayList (); 103 addLabels(panel, lst, recurse); 104 sizeLabels((JComponent []) lst.toArray(new JComponent [0])); 105 } 106 107 private static void addLabels(Container panel, ArrayList lst, boolean recurse) 108 { 109 for (int i = 0; i < panel.getComponentCount(); i++) 110 { 111 if (panel.getComponent(i) instanceof JCheckBox || panel.getComponent(i) instanceof JRadioButton 112 || panel.getComponent(i) instanceof JLabel ) lst.add(panel.getComponent(i)); 113 if (recurse && panel.getComponent(i) instanceof Container ) addLabels((Container ) panel.getComponent(i), lst, 114 recurse); 115 } 116 } 117 118 public static void sizeButtons(JButton [] buttons) 119 { 120 if (buttons.length == 0) return; 121 122 Dimension biggest = buttons[0].getPreferredSize(); 123 for (int i = 0; i < buttons.length; i++) 124 { 125 if (buttons[i].getPreferredSize().getWidth() > biggest.getWidth()) 126 { 127 biggest = buttons[i].getPreferredSize(); 128 } 129 } 130 131 biggest.height = 24; 132 133 for (int i = 0; i < buttons.length; i++) 134 { 135 if (buttons[i].getPreferredSize().width == 24 && buttons[i].getPreferredSize().height == 24) continue; 136 if (buttons[i].getPreferredSize().width == 21 && buttons[i].getPreferredSize().height == 21) continue; 137 buttons[i].setPreferredSize(biggest); 138 } 139 } 140 141 public static void sizeLabels(JComponent [] labels) 142 { 143 if (labels.length == 0) return; 144 145 Dimension biggest = labels[0].getPreferredSize(); 146 for (int i = 0; i < labels.length; i++) 147 { 148 if (labels[i].getPreferredSize().getWidth() > biggest.getWidth()) 149 { 150 biggest = labels[i].getPreferredSize(); 151 } 152 } 153 154 biggest.height = 21; 155 156 for (int i = 0; i < labels.length; i++) 157 { 158 labels[i].setPreferredSize(biggest); 159 } 160 } 161 162 public static boolean isOkToOverwrite(Component c, File file) 163 { 164 int result = JOptionPane.showOptionDialog(findFrame(c), Messages.getString("global.Sure2Overwrite", file 165 .getAbsolutePath()), Messages.getString("global.FileExists"), JOptionPane.DEFAULT_OPTION, 166 JOptionPane.INFORMATION_MESSAGE, null, new String []{ 167 Messages.getString("global.Overwrite"), 168 Messages.getString("global.Cancel")}, null); 169 return result == 0; 170 } 171 172 public static Component findParent(Component comp, Class type) 173 { 174 if (type.isAssignableFrom(comp.getClass())) return comp; 175 Component parent = comp.getParent(); 176 if (parent != null) return findParent(parent, type); 177 return null; 178 } 179 } | Popular Tags |