/* * SimplyHTML, a word processor based on Java, HTML and CSS * Copyright (C) 2002 Ulrich Hilger * * 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 2 * 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package nu.fw.jeti.plugins.xhtml.fontchooser; import java.awt.*; import java.util.Enumeration; import java.util.Vector; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.html.CSS; import javax.swing.text.html.StyleSheet; import nu.fw.jeti.util.I18N; /** * A panel for showing and manipulating font information. *
* FontPanel
shows and manipulates CSS attributes.
* To set it to HTML attributes, methods setAttributes and getAttributes have to be overridden.
*
TitledPickList
with a way to set values special to font family values
*/
static class FamilyPickList extends TitledPickList implements AttributeComponent
{
private int setValCount = 0;
private Object originalValue;
/**
* constructor
*
* @param options the options to be selectable in this list
* @param titleText the title for the pick list
*/
FamilyPickList(String[] options, String titleText)
{
super(options, titleText);
}
/**
* set the value of this TitledPickList
*
* @param a the set of attributes possibly having a font family attribute this pick list could display
* @return true, if the set of attributes had a font family attribute, false if not
*/
public boolean setValue(AttributeSet a)
{
boolean success = false;
ignoreTextChanges = true;
Object newSelection;
// if(a.isDefined(CSS.Attribute.FONT_FAMILY)) {
// newSelection = a.getAttribute(CSS.Attribute.FONT_FAMILY);
// setSelection(a.getAttribute(CSS.Attribute.FONT_FAMILY));
// success = true;
// }
//a.isDefined(StyleConstants.FontFamily)
String family = StyleConstants.getFontFamily(a);
if (family != null)
{
newSelection = family;
setSelection(family);
success = true;
} else
{
newSelection = "SansSerif";
setSelection(newSelection);
}
ignoreTextChanges = false;
if (++setValCount < 2)
{
originalValue = newSelection;
}
return success;
}
public AttributeSet getValue()
{
SimpleAttributeSet set = new SimpleAttributeSet();
Object value = getSelection();
//System.out.println("FamilyPicker getValue originalValue=" + originalValue);
//System.out.println("FamilyPicker getValue value=" + value);
if (((originalValue == null) && (value != null))
|| ((originalValue != null) && (value != null) && (!originalValue.toString().equalsIgnoreCase(value.toString()))))
{
//Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, value.toString());
StyleConstants.setFontFamily(set, value.toString());
}
return set;
}
public AttributeSet getValue(boolean includeUnchanged)
{
if (includeUnchanged)
{
SimpleAttributeSet set = new SimpleAttributeSet();
Object value = getSelection();
//System.out.println("FamilyPicker getValue originalValue=" + originalValue);
//System.out.println("FamilyPicker getValue value=" + value);
//Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_FAMILY, value.toString());
StyleConstants.setFontFamily(set, value.toString());
return set;
} else
{
return getValue();
}
}
public String getFamily()
{
return (String) getSelection();
}
public void reset()
{
setValCount = 0;
originalValue = null;
}
}
/**
* extend TitledPickList
with a way to set values special to font size values
*/
static class SizePickList extends TitledPickList implements AttributeComponent
{
// private Object key;
private int setValCount = 0;
private String originalValue;
/**
* constructor
*
* @param options the options to be selectable in this list
* @param titleText the title for the pick list
*/
SizePickList(String[] options, String titleText)
{
super(options, titleText);
//this.key = key;
}
/**
* set the value of this TitledPickList
*
* @param a the set of attributes possibly having a font size attribute this pick list could display
* @return true, if the set of attributes had a font size attribute, false if not
*/
public boolean setValue(AttributeSet a)
{
ignoreTextChanges = true;
boolean success = false;
//Object attr = a.getAttribute(key);
String newSelection;
if (a != null)
{
//LengthValue lv = new LengthValue(a.getAttribute(key));
//int val = new Float(lv.getAttrValue(attr.toString(), LengthValue.pt)).intValue();
int val = StyleConstants.getFontSize(a); //(int) Util.getAttrValue(a.getAttribute(key));
if (val > 0)
{
success = true;
newSelection = new Integer(val).toString();
setSelection(newSelection);
} else
{
newSelection = "12";
setSelection(newSelection);
}
} else
{
newSelection = "12";
setSelection(newSelection);
}
ignoreTextChanges = false;
if (++setValCount < 2)
{
originalValue = newSelection;
}
return success;
}
public AttributeSet getValue()
{
SimpleAttributeSet set = new SimpleAttributeSet();
String value = (String) getSelection();
if (((originalValue == null) && (value != null)) || ((originalValue != null) && (!originalValue.equalsIgnoreCase(value))))
{
// Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE,
// set.addAttribute("size", (String) getSelection() /*+ "pt"*/);
StyleConstants.setFontSize(set, Integer.parseInt((String) getSelection()));
}
return set;
}
public AttributeSet getValue(boolean includeUnchanged)
{
if (includeUnchanged)
{
SimpleAttributeSet set = new SimpleAttributeSet();
//Util.styleSheet().addCSSAttribute(set, CSS.Attribute.FONT_SIZE,
//(String) getSelection() /*+ "pt"*/);
StyleConstants.setFontSize(set, Integer.parseInt((String) getSelection()));
return set;
} else
{
return getValue();
}
}
public void reset()
{
setValCount = 0;
originalValue = null;
}
}
private static StyleSheet styleSheet = new StyleSheet();
/**
* extend TitledPickList
with a way to set values special to font style values
*/
static class StylePickList extends TitledPickList implements AttributeComponent
{
private int setValCount = 0;
/**
* constructor
*
* @param options the options to be selectable in this list
* @param titleText the title for the pick list
*/
StylePickList(String[] options, String titleText)
{
super(options, titleText);
}
/**
* set the value of this TitledPickList
*
* @param a the set of attributes possibly having a font style attribute this pick list could display
* @return true, if the set of attributes had a font style attribute, false if not
*/
public boolean setValue(AttributeSet a)
{
ignoreTextChanges = true;
boolean success = false;
int styleNo = 0;
String value;
if (StyleConstants.isBold(a))
{
styleNo++;
}
if (StyleConstants.isItalic(a))
{
styleNo += 2;
}
setSelection(styleNo);
if (++setValCount < 2)
{
}
ignoreTextChanges = false;
return success;
}
public AttributeSet getValue()
{
SimpleAttributeSet set = new SimpleAttributeSet();
int styleNo = getIndex();
switch (styleNo)
{
case 1:
StyleConstants.setBold(set,true);
break;
case 2:
StyleConstants.setItalic(set,true);
break;
case 3:
StyleConstants.setBold(set,true);
StyleConstants.setItalic(set,true);
break;
}
return set;
}
public AttributeSet getValue(boolean includeUnchanged)
{
// if (includeUnchanged)
// {
// String value = "CSS_ATTRIBUTE_NORMAL";
// SimpleAttributeSet set = new SimpleAttributeSet();
// int styleNo = getIndex();
// switch (styleNo)
// {
// case 0:
// styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_STYLE, value);
// break;
// case 1:
// styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_WEIGHT, StyleConstants.Bold.toString());
// break;
// case 2:
// styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_STYLE, StyleConstants.Italic.toString());
// break;
// case 3:
// styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_WEIGHT, StyleConstants.Bold.toString());
// styleSheet.addCSSAttribute(set, CSS.Attribute.FONT_STYLE, StyleConstants.Italic.toString());
// break;
// }
// return set;
// } else
{
return getValue();
}
}
public int getFontStyle()
{
int fontStyle = 0;
switch (getIndex())
{
case 0:
fontStyle = Font.PLAIN;
break;
case 1:
fontStyle = Font.BOLD;
break;
case 2:
fontStyle = Font.ITALIC;
break;
case 3:
fontStyle = Font.BOLD | Font.ITALIC;
break;
}
return fontStyle;
}
public void reset()
{
setValCount = 0;
}
}
}
/*
* Overrides for emacs
* Local variables:
* tab-width: 4
* End:
*/