// -------------------------- Listing.java ------------------------------
/************************************************************************
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.List;
import java.lang.StringBuffer;
/**
* Listing class categorizes and lists the key/value
* pairs contained within the javax.swing.UIDefaults
* collection.
*
* @author Dr. Lazlo Jamf
* @see Sun Java Developer's Forum Member[DrLazloJamf]
* @see Dr. Laszlo Jamf's original post
* @see javax.swing.UIDefaults
*
*/
public class Listing
{
public static void main(String[] args) throws Exception
{
JFrame.setDefaultLookAndFeelDecorated(true);
System.setProperty("sun.awt.noerasebackground", "true");
JFrame f = new JFrame("Listing");
JPanel view = new JPanel(new GridBagLayout());
Container controls = new JToolBar();
controls.add(Box.createGlue());
controls.add(getTypesCombo(view));
controls.add(Box.createGlue());
Container cp = f.getContentPane();
cp.add(new JScrollPane(view));
cp.add(controls, BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
/**
* Statically creates a javax.swing.JLabel object and returns this object as
* javax.swing.JComponent.
*
* @param key java.lang.Object of the key value of this collection
* @param value java.lang.Object of the value of this collection
* @return javax.swing.JComponent
*/
static class ComponentMaker
{
public JComponent make(Object key, Object value)
{
return new JLabel(value == null ? "(null)" : value.toString());
}
}
/**
* Statically creates an array of ComponentMaker objects of all the categorized UI types.
* i.e. (Boolean, Border, Color, Font, , Number, String, and The Rest).
*
* @param Boolean.class
* @param Border.class
* @param Color.class
* @param Font.class
* @param Icon.class
* @param Number.class
* @param String.class
* @param Object.class
*/
static ComponentMaker[] makers =
{
new ComponentMaker(), //Boolean
new ComponentMaker()
{ //Border
public JComponent make(Object key, Object value)
{
JLabel lbl = new JLabel(" ");
lbl.setBorder(new BorderAdapter((Border)value, key.toString()));
return lbl;
}
},
new ComponentMaker()
{ //Color
public JComponent make(Object key, Object value)
{
return new JLabel(new ColorIcon((Color)value));
}
},
new ComponentMaker()
{ //Font
public JComponent make(Object key, Object value)
{
Font font = (Font)value;
JLabel lbl = new JLabel(key+": "+font.getFontName()+", size="+font.getSize2D());
lbl.setFont(font);
return lbl;
}
},
new ComponentMaker()
{ //Icon
public JComponent make(Object key, Object value)
{
return new JLabel(new IconAdapter((Icon)value, key.toString()));
}
},
new ComponentMaker(), //Number
new ComponentMaker(), //String
new ComponentMaker() //The Rest
};
/**
* Returns the types javax.swing.JComboBox listing of all the LAF properties and resources.
* Attachs java.awt.event.ActionListener to the cboTypes javax.swing.JComboBox.
*
* @param view javax.swing.JPanel of this view.
* @return cboTypes javax.swing.JComboBox of all UIDefaults properties and resources.
*/
@SuppressWarnings(value = "unchecked") // suppresses the raw List warnings
static JComboBox getTypesCombo(final JPanel view)
{
JComboBox cboTypes = new JComboBox(
new String[]{"Boolean","Border","Color","Font","Icon", "Number", "String", "The Rest"});
cboTypes.addActionListener(new ActionListener() // anonymous class
{
Class[] types = {Boolean.class, Border.class, Color.class, Font.class, Icon.class, Number.class, String.class};
public void actionPerformed(ActionEvent evt)
{
int idx = ((JComboBox) evt.getSource()).getSelectedIndex();
if (idx != -1)
{
view.removeAll();
List keys = (idx < types.length) ? findKeys(types[idx]) : findRestKeys(types);//
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.gridy = 0;
for(int i=0, ub=keys.size(); ijava.util.Collections#sort method.
*
* @param cls java.lang.Class of one of the defined types.
* @return keys java.util.List of this class' keys.
*/
@SuppressWarnings(value = "unchecked")
static List findKeys(Class cls)
{
List keys = new ArrayList();
UIDefaults defs = UIManager.getLookAndFeel().getDefaults();
for(Iterator i=defs.keySet().iterator(); i.hasNext(); )
{
Object key = i.next();
if(cls.isInstance(UIManager.get(key)))
{
keys.add(key);
}
}
Collections.sort(keys);
return keys;
}
/**
* Finds the rest of the keys in the javax.swing.UIDefaults collection
* and sorts the keys using Collections#sort.
*
* @param cls java.lang.Class array of defined class types.
* @return keys java.util.List of class keys.
*/
@SuppressWarnings(value = "unchecked")
static List findRestKeys(Class[] cls)
{
List keys = new ArrayList();
StringBuffer sbuf;
UIDefaults defs = UIManager.getLookAndFeelDefaults();
for(Iterator objects.
*
* @param icon javax.swing.Icon interface object for this instance.
* @param key java.lang.String of this instance's
* javax.swing.UIDefaults key attribute.
* @author Dr. Lazlo Jamf
*/
class IconAdapter implements Icon
{
private Icon icon;
private Component component;
/**
* Class constructor sets an instance of the javax.swing.Icon interface
* and a java.lang.String containing the qualified javax.swing.JComponent
* class name.
*
* @param icon javax.swing.Icon interface instance.
* @param key java.lang.String containing qualified javax.swing.JComponent
* class name.
* @see javax.swing.Icon interface
*/
public IconAdapter(Icon icon, String key)
{
this.icon = icon;
try
{
int dotIndex = key.lastIndexOf('.');
if (dotIndex != -1)
{
String classname = "javax.swing.J" + key.substring(0, dotIndex);
component = (Component) Class.forName(classname).newInstance();
}
}
catch (Exception e)
{
}
}
/**
* Accessor for icon pixel width.
*
* @return int value representing icon pixel width.
*/
public int getIconWidth()
{
return icon.getIconWidth();
}
/**
* Accessor for icon pixel height.
*
* @return int value representing icon pixel height.
*/
public int getIconHeight()
{
return icon.getIconHeight();
}
/**
* Paints icon at this x, y location.
*
* @param c java.awt.Component reference of the component to paint.
* @param g java.awt.Graphics object.
* @param x int value of the x axis coordinate.
* @param y int value of the y axis coordinate.
*/
public void paintIcon(Component c, Graphics g, int x, int y)
{
try
{
if (component != null)
{
c = component;
}
icon.paintIcon(c, g, x, y);
}
catch(Exception e)
{
}
}
}
/**
* BorderAdapter class handles this instance's displaying of
* javax.swing.border.Border objects.
*
* @param icon javax.swing.Border interface instance.
* @param key java.lang.String of this instance's
* javax.swing.UIDefaults key attribute.
* @author Dr. Lazlo Jamf
*/
class BorderAdapter implements Border
{
private Border border;
private Component component;
/**
* Class constructor sets an instance of the javax.swing.Border interface
* and a java.lang.String containing the qualified javax.swing.JComponent
* class name.
*
* @param icon javax.swing.Border interface instance.
* @param key java.lang.String containing qualified javax.swing.JComponent
* class name.
* @see javax.swing.Border interface
*/
public BorderAdapter(Border border, String key)
{
this.border = border;
try
{
int dotIndex = key.lastIndexOf('.');
if (dotIndex != -1)
{
String classname = "javax.swing.J" + key.substring(0, dotIndex);
component = (Component) Class.forName(classname).newInstance();
}
}
catch (Exception e)
{
}
}
/**
* Returns the boolean value of the border#isBorderOpaque method.
*
* @return boolean value for this instance's border#isBorderOpaque method.
*/
public boolean isBorderOpaque()
{
return border.isBorderOpaque();
}
/**
* Accessor for border's java.awt.Insets.
*
* @return java.awt.Insets of this border's insets.
*/
public Insets getBorderInsets(Component c)
{
try
{
if (component != null)
{
c = component;
}
return border.getBorderInsets(c);
} catch(Exception e)
{
return new Insets(0,0,0,0);
}
}
/**
* Paints border at this x, y location.
*
* @param c java.awt.Component reference to the component to paint.
* @param g java.awt.Graphics object.
* @param x int value of the x axis coordinate.
* @param y int value of the y axis coordinate.
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
{
try
{
if (component != null)
{
c = component;
}
border.paintBorder(c, g, x, y, width, height);
}
catch(Exception e)
{
}
}
}
/*
public class StringListWrapper implements Serializable {
public List data;
}
StringListWrapper rap = (StringListWrapper) in.readObject();
List strings = rap.data;
*/
/*
47.How can I process through the keys of a Hashtable in sorted order ?
In order to get all the keys for a Hashtable, you use the keys() method to get an Enumeration or the keySet() method
to get a Set. If you are using Java 2, and can use the collections framework, what you should do is get the key set
of the Hashtable and create a TreeSet from it. You can then get an iterator() from the created TreeSet that will have
the keys in order. If you can’t use the collections framework, you’ll have the sort the Enumeration you get back from
keys() yourself.
*/