1 package org.wcb.common; 2 /*** 3 * Copyright (C) 1999 Walter Bogaardt 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18 * 19 */ 20 21 import javax.swing.SwingConstants; 22 import javax.swing.JButton; 23 import javax.swing.Icon; 24 import javax.swing.ImageIcon; 25 import javax.swing.JComponent; 26 import javax.swing.JOptionPane; 27 import java.awt.Frame; 28 import java.awt.Color; 29 import java.awt.Component; 30 31 public class GuiLib implements SwingConstants 32 { 33 34 private static GuiLib instance = null; 35 36 private ImageSource imageSource = null; 37 38 private GuiLib() 39 { 40 } 41 42 /*** 43 * Singleton return of a class the allows basic 44 * parsing of basic GUI information used by the application 45 * for changing colors and setting fonts. 46 * @return GuiLib class instance. 47 */ 48 public static GuiLib getInstance() 49 { 50 if (null == instance) instance = new GuiLib(); 51 return instance; 52 } 53 54 public JButton setupAButton(String action, String image) 55 { 56 JButton returnValue; 57 if (null != imageSource) { 58 Icon icon = imageSource.getIcon(image); 59 returnValue = new JButton(action, icon); 60 } 61 else { 62 returnValue = new JButton(action, new ImageIcon(image)); 63 } 64 returnValue.setVerticalTextPosition(BOTTOM); 65 returnValue.setHorizontalTextPosition(CENTER); 66 returnValue.setActionCommand(action); 67 returnValue.setToolTipText(action); 68 return returnValue; 69 } 70 71 public static JButton setupButton(String action, String image) 72 { 73 return getInstance().setupAButton(action,image); 74 } 75 76 public void setImageSource(ImageSource source) 77 { 78 imageSource = source; 79 } 80 81 public ImageSource getImageSource() 82 { 83 return imageSource; 84 } 85 86 public static Icon getIcon(String location) 87 { 88 Icon returnValue; 89 ImageSource source = getInstance().getImageSource(); 90 if (source != null) { 91 returnValue = source.getIcon(location); 92 } 93 else returnValue = new ImageIcon(location); 94 return returnValue; 95 } 96 97 public void showHelp(JComponent comp) 98 { 99 JOptionPane.showMessageDialog(comp, "Help is not yet implemented"); 100 } 101 102 103 /*** 104 * Returns the specified component's Frame. 105 * 106 * @param parentComponent the Component to check for a Frame 107 * @return the Frame that contains the component, or the default 108 * frame if the component is null, or does not have a valid 109 * Frame parent 110 */ 111 public static Frame getFrameForComponent(Component parentComponent) { 112 if (parentComponent == null) 113 return getRootFrame(); 114 if (parentComponent instanceof Frame) 115 return (Frame)parentComponent; 116 return JOptionPane.getFrameForComponent(parentComponent.getParent()); 117 } 118 119 /*** 120 * Returns the Frame to use for the class methods in which a frame 121 * is not provided. 122 * 123 * @return the default Frame to use 124 */ 125 public static Frame getRootFrame() { 126 Frame sharedFrame = 127 new Frame(); 128 return sharedFrame; 129 } 130 131 132 public static Color getColor(String value) 133 { 134 Color returnValue = null; 135 if ("white".equals(value)) { 136 returnValue = Color.white; 137 } else if ("lightGray".equals(value)) { 138 returnValue = Color.lightGray; 139 } else if ("gray".equals(value)) { 140 returnValue = Color.gray; 141 } else if ("darkGray".equals(value)) { 142 returnValue = Color.darkGray; 143 } else if ("black".equals(value)) { 144 returnValue = Color.black; 145 } else if ("red".equals(value)) { 146 returnValue = Color.red; 147 } else if ("pink".equals(value)) { 148 returnValue = Color.pink; 149 } else if ("orange".equals(value)) { 150 returnValue = Color.orange; 151 } else if ("yellow".equals(value)) { 152 returnValue = Color.yellow; 153 } else if ("green".equals(value)) { 154 returnValue = Color.green; 155 } else if ("magenta".equals(value)) { 156 returnValue = Color.magenta; 157 } else if ("cyan".equals(value)) { 158 returnValue = Color.cyan; 159 } else if ("blue".equals(value)) { 160 returnValue = Color.blue; 161 } 162 return returnValue; 163 } 164 165 }