📄 xcomponentbuilder.java
字号:
/****************************************************************
* XBrowser - eXtended web Browser *
* *
* Copyright (c) 2000-2001 Armond Avanes *
* Refer to ReadMe & License files for more information *
* *
* *
* By: Armond Avanes *
* Armond555@yahoo.com & Armond333@yahoo.com *
* http://xbrowser.sourceforge.net/ *
*****************************************************************/
package xbrowser.widgets;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.awt.color.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.beans.*;
import xbrowser.*;
import xbrowser.util.*;
public final class XComponentBuilder
{
public XComponentBuilder(XResourceManager resource_man)
{
resourceManager = resource_man;
}
public JFileChooser getLoadFileChooser()
{
if( loadFileChooser==null )
{
XFileFilter web_filter = new XFileFilter(new String[] {"htm", "html"}, resourceManager.getProperty(this, "XFileChooser.WebDescription"));
loadFileChooser = new JFileChooser();
loadFileChooser.addChoosableFileFilter(web_filter);
loadFileChooser.addChoosableFileFilter(new XFileFilter("txt", resourceManager.getProperty(this, "XFileChooser.TextDescription")));
loadFileChooser.addChoosableFileFilter(new XFileFilter("rtf", resourceManager.getProperty(this, "XFileChooser.RichTextDescription")));
loadFileChooser.setFileFilter(web_filter);
}
return loadFileChooser;
}
public JFileChooser getSaveFileChooser()
{
if( saveFileChooser==null )
{
XFileFilter web_filter = new XFileFilter(new String[] {"htm", "html"}, resourceManager.getProperty(this, "XFileChooser.WebPageCompleteDescription"));
saveFileChooser = new JFileChooser();
saveFileChooser.addChoosableFileFilter(web_filter);
saveFileChooser.addChoosableFileFilter(new XFileFilter(new String[] {"htm", "html"}, resourceManager.getProperty(this, "XFileChooser.WebPageHTMLOnlyDescription")));
saveFileChooser.addChoosableFileFilter(new XFileFilter("txt", resourceManager.getProperty(this, "XFileChooser.TextDescription")));
saveFileChooser.addChoosableFileFilter(new XFileFilter("rtf", resourceManager.getProperty(this, "XFileChooser.RichTextDescription")));
saveFileChooser.setFileFilter(web_filter);
}
return saveFileChooser;
}
public JFileChooser getPluginFileChooser()
{
if( pluginFileChooser==null )
{
XFileFilter jar_filter = new XFileFilter("jar", resourceManager.getProperty(this, "XFileChooser.JarDescription"));
pluginFileChooser = new JFileChooser();
pluginFileChooser.addChoosableFileFilter(jar_filter);
pluginFileChooser.addChoosableFileFilter(new XFileFilter("zip", resourceManager.getProperty(this, "XFileChooser.ZipDescription")));
pluginFileChooser.setFileFilter(jar_filter);
}
return pluginFileChooser;
}
public JFileChooser getImportExportFileChooser()
{
if( importExportFileChooser==null )
importExportFileChooser = new JFileChooser();
return importExportFileChooser;
}
public ImageIcon buildImageIcon(Object owner, String icon_key)
{
if( (icon_key==null) || (icon_key.trim().equals("")) )
return null;
java.net.URL url = resourceManager.getResourceURL(owner, icon_key);
return( (url!=null) ? new ImageIcon(url) : null );
}
public JLabel buildLabel(Object owner, String key)
{
return( new JLabel(resourceManager.getProperty(owner, key)) );
}
public JLabel buildLabel(Object owner, String key, int align)
{
return( new JLabel(resourceManager.getProperty(owner, key),align) );
}
public JCheckBox buildCheckBox(Object owner, String key)
{
return( new JCheckBox(resourceManager.getProperty(owner, key)) );
}
public JCheckBox buildCheckBox(Object owner, String key, boolean selected)
{
return( new JCheckBox(resourceManager.getProperty(owner, key), selected) );
}
public JRadioButton buildRadioButton(Object owner, String key)
{
return( new JRadioButton(resourceManager.getProperty(owner, key)) );
}
public JRadioButton buildRadioButton(Object owner, String key, String arg1, String arg2)
{
return( new JRadioButton(resourceManager.getProperty(owner, key, arg1, arg2)) );
}
public JButton buildButton(XAction action)
{
JButton btn = (JButton)initiateAbstractButton(new JButton(), action);
btn.setMargin(new Insets(3,3,3,3));
return btn;
}
/* public JButton buildSignButton(XAction action)
{
JButton btn = (JButton)initiateAbstractButton(new JButton(), action);
Border border = BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(),BorderFactory.createLoweredBevelBorder());
btn.setBorder( BorderFactory.createCompoundBorder(border,BorderFactory.createRaisedBevelBorder()) );
btn.setFocusPainted(false);
return btn;
}*/
public JButton buildSignButton(XAction action)
{
JButton btn = (JButton)initiateAbstractButton(new JButton(), action);
btn.setBorder( new EmptyBorder(0, 0, 0, 0) );
btn.setFocusPainted(false);
return btn;
}
public XPopupButton buildPopupButton(XAction action)
{
return( new XPopupButton(action) );
}
public JButton buildToolbarButton(XAction action)
{
XToolbarButton btn = (XToolbarButton)initiateAbstractButton(new XToolbarButton(), action);
buildFlatLook(btn, -1, -1);
btn.setHorizontalTextPosition(JButton.CENTER);
btn.setVerticalTextPosition(JButton.BOTTOM);
return buildBnWLook(btn, (ImageIcon)btn.getRealIcon());
}
public JButton buildFlatButton(XAction action, int width, int height)
{
JButton btn = (JButton)initiateAbstractButton(new JButton(), action);
return buildFlatLook(btn, width, height);
}
public JButton buildFlatLook(JButton btn, int width, int height)
{
return XFlatLook.buildFlatLook(btn, width, height);
}
public JButton buildBnWLook(JButton btn, ImageIcon icon)
{
return XBnWLook.buildBnWLook(btn, icon);
}
public JButton buildBnWLook(JButton btn)
{
return XBnWLook.buildBnWLook(btn);
}
public JMenu buildMenu(Object owner, String key)
{
JMenu menu = new JMenu( resourceManager.getProperty(owner, "Menu."+key+".name") );
ImageIcon icon = buildImageIcon(owner, "Menu."+key+".icon");
if( icon!=null )
menu.setIcon(icon);
String mnemonic_str = resourceManager.getProperty(owner, "Menu."+key+".mnemonic");
if( mnemonic_str!=null )
menu.setMnemonic( mnemonic_str.charAt(0) );
return menu;
}
public JMenuItem buildMenuItem(XAction action)
{
JMenuItem menu_item = (JMenuItem)initiateAbstractButton(new JMenuItem(), action);
KeyStroke accel = (KeyStroke)action.getValue(XAction.KEY_STROKE);
if( accel!=null )
menu_item.setAccelerator(accel);
return menu_item;
}
public JCheckBoxMenuItem buildCheckBoxMenuItem(XAction action)
{
JCheckBoxMenuItem menu_item = (JCheckBoxMenuItem)initiateAbstractButton(new JCheckBoxMenuItem(), action);
KeyStroke accel = (KeyStroke)action.getValue(XAction.KEY_STROKE);
if( accel!=null )
menu_item.setAccelerator(accel);
return menu_item;
}
/*
public JRadioButton buildRadioButton(XAction action)
{
return( (JRadioButton)initiateAbstractButton(new JRadioButton(), action) );
}
*/
private AbstractButton initiateAbstractButton(final AbstractButton abs_btn, XAction action)
{
String tooltip = (String)action.getValue(Action.SHORT_DESCRIPTION);
Integer mnemonic_obj = (Integer)action.getValue(XAction.MNEMONIC);
int mnemonic = (mnemonic_obj!=null) ? mnemonic_obj.intValue() : -1;
abs_btn.setText( (String)action.getValue(Action.NAME) );
abs_btn.setIcon( (Icon)action.getValue(Action.SMALL_ICON) );
abs_btn.setEnabled(action.isEnabled());
if( mnemonic!=-1 )
abs_btn.setMnemonic(mnemonic);
if( tooltip!=null && !tooltip.equals("") )
abs_btn.setToolTipText(tooltip);
abs_btn.addActionListener(action);
action.addPropertyChangeListener( new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e)
{
Object new_value = e.getNewValue();
if( e.getPropertyName().equals("enabled") )
abs_btn.setEnabled( ((Boolean)new_value).booleanValue() );
else if( e.getPropertyName().equals(Action.NAME) )
abs_btn.setText( (new_value==null) ? null : new_value.toString() );
else if( e.getPropertyName().equals(Action.SMALL_ICON) )
abs_btn.setIcon( (Icon)new_value );
else if( e.getPropertyName().equals(Action.SHORT_DESCRIPTION) )
abs_btn.setToolTipText( (new_value==null) ? null : new_value.toString() );
else if( e.getPropertyName().equals(XAction.MNEMONIC) )
abs_btn.setMnemonic( (new_value==null) ? -1 : ((Integer)new_value).intValue() );
}
});
return abs_btn;
}
public ImageIcon prepareBnWImage(Image img)
{
ImageProducer producer = new FilteredImageSource(img.getSource(), imageFilter);
return( new ImageIcon(Toolkit.getDefaultToolkit().createImage(producer)) );
}
public Border buildRaisedBorder()
{
return new BevelBorder(BevelBorder.RAISED) {
protected void paintRaisedBevel(Component c, Graphics g, int x, int y, int width, int height)
{
Color oldColor = g.getColor();
int h = height;
int w = width;
g.translate(x, y);
g.setColor(getHighlightOuterColor(c));
g.drawLine(0, 0, 0, h-1);
g.drawLine(1, 0, w-1, 0);
//g.setColor(getHighlightInnerColor(c));
//g.drawLine(1, 1, 1, h-2);
//g.drawLine(2, 1, w-2, 1);
g.setColor(getShadowOuterColor(c));
g.drawLine(1, h-1, w-1, h-1);
g.drawLine(w-1, 1, w-1, h-2);
//g.setColor(getShadowInnerColor(c));
//g.drawLine(2, h-2, w-2, h-2);
//g.drawLine(w-2, 2, w-2, h-3);
g.translate(-x, -y);
g.setColor(oldColor);
}
};
}
public Border buildLoweredBorder()
{
return new BevelBorder(BevelBorder.LOWERED) {
protected void paintLoweredBevel(Component c, Graphics g, int x, int y, int width, int height)
{
Color oldColor = g.getColor();
int h = height;
int w = width;
g.translate(x, y);
g.setColor(getShadowInnerColor(c));
g.drawLine(0, 0, 0, h-1);
g.drawLine(1, 0, w-1, 0);
//g.setColor(getShadowOuterColor(c));
//g.drawLine(1, 1, 1, h-2);
//g.drawLine(2, 1, w-2, 1);
g.setColor(getHighlightOuterColor(c));
g.drawLine(1, h-1, w-1, h-1);
g.drawLine(w-1, 1, w-1, h-2);
//g.setColor(getHighlightInnerColor(c));
//g.drawLine(2, h-2, w-2, h-2);
//g.drawLine(w-2, 2, w-2, h-3);
g.translate(-x, -y);
g.setColor(oldColor);
}
};
}
public Border buildInactiveBorder()
{
return new EmptyBorder(1,1,1,1);
}
private class XDesaturateFilter extends RGBImageFilter
{
public XDesaturateFilter(int brightness)
{
brightnessAdjustment = brightness;
canFilterIndexColorModel = false;
}
public int filterRGB(int x, int y, int rgb)
{
int a = rgb & 0xff000000;
float r = (rgb & 0xff0000) >> 16;
float g = (rgb & 0x00ff00) >> 8;
float b = rgb & 0x0000ff;
r *= CO_RedBandWeight;
g *= CO_GreenBandWeight;
b *= CO_BlueBandWeight;
int gray = (int) Math.min( (r + g + b + brightnessAdjustment), 255 );
return( a | (gray<<16) | (gray<<8) | gray );
}
// Attributes:
private static final float CO_RedBandWeight = 0.2125f;
private static final float CO_GreenBandWeight = 0.7154f;
private static final float CO_BlueBandWeight = 0.0721f;
private int brightnessAdjustment;
}
private class XToolbarButton extends JButton implements PropertyChangeListener
{
public XToolbarButton()
{
XRepository.getConfiguration().addPropertyChangeListener("ToolBarStyle", this);
updateSize();
}
public void setText(String text)
{
myText = text;
if( XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_TEXT_ONLY ||
XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_TEXT_ICON )
super.setText(myText);
else
super.setText("");
}
public void setIcon(Icon icon)
{
myIcon = icon;
if( XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_ICON_ONLY ||
XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_TEXT_ICON )
super.setIcon(myIcon);
else
super.setIcon(null);
}
public Icon getRealIcon()
{
return myIcon;
}
public void propertyChange(PropertyChangeEvent e)
{
updateSize();
setText(myText);
setIcon(myIcon);
}
private void updateSize()
{
Dimension size;
if( XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_ICON_ONLY )
size = XProjectConstants.TOOLBAR_ICON_ONLY_SIZE;
else if( XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_TEXT_ONLY )
size = XProjectConstants.TOOLBAR_TEXT_ONLY_SIZE;
else if( XRepository.getConfiguration().getToolBarStyle()==XProjectConstants.TOOLBAR_STYLE_TEXT_ICON )
size = XProjectConstants.TOOLBAR_TEXT_ICON_SIZE;
else
return;
setMinimumSize(size);
setMaximumSize(size);
setPreferredSize(size);
}
// Attributes:
private String myText = "";
private Icon myIcon = null;
}
// Attributes:
private XResourceManager resourceManager = null;
private JFileChooser loadFileChooser = null;
private JFileChooser saveFileChooser = null;
private JFileChooser importExportFileChooser = null;
private JFileChooser pluginFileChooser = null;
private XDesaturateFilter imageFilter = new XDesaturateFilter(20);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -