//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.util.text;
import com.nexirius.util.TextFile;
import com.nexirius.util.TextToken;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import java.awt.*;
public class TextStyles {
static final String file_id = new String("TextStyles");
static final int file_version = 1;
static StyledDocument m_doc;
private abstract class Attribute {
abstract String getIdentifier();
void writeTo(TextFile file) throws Exception {
file.writeIdentifier(getIdentifier());
file.write('=');
writeValue(file);
}
abstract void writeValue(TextFile file) throws Exception;
abstract Attribute createFrom(TextToken token);
abstract void set(Style style);
}
private abstract class FloatAttribute extends Attribute {
float m_value;
FloatAttribute(float value) {
m_value = value;
}
void writeValue(TextFile file) throws Exception {
file.write((double) m_value);
}
Attribute createFrom(TextToken token) {
m_value = (float) token.getDouble();
return null;
}
}
private abstract class BooleanAttribute extends Attribute {
boolean m_value;
BooleanAttribute(boolean value) {
m_value = value;
}
void writeValue(TextFile file) throws Exception {
if (m_value) {
file.writeIdentifier("TRUE");
} else {
file.writeIdentifier("FALSE");
}
}
Attribute createFrom(TextToken token) {
m_value = token.isIdentifier("TRUE");
return null;
}
}
private abstract class StringAttribute extends Attribute {
String m_value;
StringAttribute(String value) {
m_value = value;
}
void writeValue(TextFile file) throws Exception {
file.writeString(m_value);
}
Attribute createFrom(TextToken token) {
m_value = token.getString();
return null;
}
}
private abstract class IntAttribute extends Attribute {
int m_value;
IntAttribute(int value) {
m_value = value;
}
void writeValue(TextFile file) throws Exception {
file.write(m_value);
}
Attribute createFrom(TextToken token) {
m_value = token.getInt();
return null;
}
}
private class Bold extends BooleanAttribute {
Bold(boolean value) {
super(value);
}
String getIdentifier() {
return "BOLD";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new Bold(m_value);
}
void set(Style style) {
StyleConstants.setBold(style, m_value);
}
}
private class Alignment extends Attribute {
int m_value;
Alignment(int value) {
m_value = value;
}
String getIdentifier() {
return "ALIGNMENT";
}
void writeValue(TextFile file) throws Exception {
switch (m_value) {
case StyleConstants.ALIGN_LEFT:
file.writeIdentifier("ALIGN_LEFT");
break;
case StyleConstants.ALIGN_RIGHT:
file.writeIdentifier("ALIGN_RIGHT");
break;
case StyleConstants.ALIGN_CENTER:
file.writeIdentifier("ALIGN_CENTER");
break;
case StyleConstants.ALIGN_JUSTIFIED:
file.writeIdentifier("ALIGN_JUSTIFIED");
break;
default:
file.writeIdentifier("ALIGN_LEFT");
}
}
Attribute createFrom(TextToken token) {
if (token.isIdentifier("ALIGN_LEFT")) {
m_value = StyleConstants.ALIGN_LEFT;
} else if (token.isIdentifier("ALIGN_RIGHT")) {
m_value = StyleConstants.ALIGN_RIGHT;
} else if (token.isIdentifier("ALIGN_CENTER")) {
m_value = StyleConstants.ALIGN_CENTER;
} else if (token.isIdentifier("ALIGN_JUSTIFIED")) {
m_value = StyleConstants.ALIGN_JUSTIFIED;
} else {
m_value = StyleConstants.ALIGN_LEFT;
}
return new Alignment(m_value);
}
void set(Style style) {
StyleConstants.setAlignment(style, m_value);
}
}
private class FontFamily extends StringAttribute {
FontFamily(String value) {
super(value);
}
String getIdentifier() {
return "FONT_FAMILY";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new FontFamily(m_value);
}
void set(Style style) {
StyleConstants.setFontFamily(style, m_value);
}
}
private class FontSize extends IntAttribute {
FontSize(int value) {
super(value);
}
String getIdentifier() {
return "FONT_SIZE";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new FontSize(m_value);
}
void set(Style style) {
StyleConstants.setFontSize(style, m_value);
}
}
private class Foreground extends StringAttribute {
Foreground(String value) {
super(value);
}
String getIdentifier() {
return "FOREGROUND";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new Foreground(m_value);
}
void set(Style style) {
try {
Color color = new Color(Integer.parseInt(m_value, 16));
StyleConstants.setForeground(style, color);
} catch (Exception ex) {
error("Can't create color: " + m_value);
}
}
}
private class Italic extends BooleanAttribute {
Italic(boolean value) {
super(value);
}
String getIdentifier() {
return "ITALIC";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new Italic(m_value);
}
void set(Style style) {
StyleConstants.setItalic(style, m_value);
}
}
private class LeftIndent extends FloatAttribute {
LeftIndent(float value) {
super(value);
}
String getIdentifier() {
return "LEFT_INDENT";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new LeftIndent(m_value);
}
void set(Style style) {
StyleConstants.setLeftIndent(style, m_value);
}
}
private class LineSpacing extends FloatAttribute {
LineSpacing(float value) {
super(value);
}
String getIdentifier() {
return "LINE_SPACING";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new LineSpacing(m_value);
}
void set(Style style) {
StyleConstants.setLineSpacing(style, m_value);
}
}
private class RightIndent extends FloatAttribute {
RightIndent(float value) {
super(value);
}
String getIdentifier() {
return "RIGHT_INDENT";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new RightIndent(m_value);
}
void set(Style style) {
StyleConstants.setRightIndent(style, m_value);
}
}
private class SpaceAbove extends FloatAttribute {
SpaceAbove(float value) {
super(value);
}
String getIdentifier() {
return "SPACE_ABOVE";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new SpaceAbove(m_value);
}
void set(Style style) {
StyleConstants.setSpaceAbove(style, m_value);
}
}
private class SpaceBelow extends FloatAttribute {
SpaceBelow(float value) {
super(value);
}
String getIdentifier() {
return "SPACE_BELOW";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new SpaceBelow(m_value);
}
void set(Style style) {
StyleConstants.setSpaceBelow(style, m_value);
}
}
private class Underline extends BooleanAttribute {
Underline(boolean value) {
super(value);
}
String getIdentifier() {
return "UNDERLINE";
}
Attribute createFrom(TextToken token) {
super.createFrom(token);
return new Underline(m_value);
}
void set(Style style) {
StyleConstants.setUnderline(style, m_value);
}
}
Attribute attribute[] = {
new Bold(false)
, new Alignment(StyleConstants.ALIGN_LEFT)
, new FontFamily("Courier")
, new FontSize(8)
, new Foreground("black")
, new Italic(false)
, new LeftIndent(0)
, new LineSpacing(5)
, new RightIndent(0)
, new SpaceAbove(0)
, new SpaceBelow(0)
, new Underline(false)
};
public TextStyles(StyledDocument doc) {
m_doc = doc;
}
/**
* @return an array which contains all available attributes
*/
public String[] getAvailableAttributeFlags() {
String ret[] = new String[attribute.length];
for (int i = 0; i < attribute.length; ++i) {
ret[i] = attribute[i].getIdentifier();
}
return ret;
}
public void error(String message) {
System.out.println(message);
}
/**
* initializes a list of hierarchic paragraph styles
* <BR>e.g.
* <CODE>
* <B><BR>FILE_ID:"TextStyles"
* <BR>VERSION:1
* </B><BR>
* <BR>normal {
* <BR>FONT_FAMILY = "Helvetica"
* <BR>FONT_SIZE = 10
* <BR>ALIGNMENT = "ALIGN_CENTER"
* <BR>LEFT_INDENT = 100.0
* <BR>}
* <BR>
* <BR>italic extends normal {
* <BR>ITALIC = TRUE
* <BR>ALIGNMENT = "ALIGN_RIGHT"
* <BR>}
* <BR>
* <BR>bold extends normal {
* <BR>FONT_SIZE = 40
* <BR>FONT_FAMILY = "Arial"
* <BR>ALIGNMENT = "ALIGN_JUSTIFIED"
* <BR>FOREGROUND = "ff"
* <BR>LEFT_INDENT = 200.0
* <BR>}
* <BR>
* <BR>code extends normal {
* <BR>FONT_FAMILY = "Courier"
* <BR>FOREGROUND = "808080"
* <BR>LEFT_INDENT = 20.0
* <BR>}
* <BR>
* <BR>bigcode extends code {
* <BR>FONT_SIZE = 24
* <BR>UNDERLINE = FALSE
* <BR>}
* <BR>
* <BR>underline extends bold {
* <BR>UNDERLINE = TRUE
* <BR>FOREGROUND = "ff0000"
* <BR>}
* <BR>
* <BR>address extends italic {
* <BR>FONT_SIZE = 20
* <BR>FOREGROUND = "50A000"
* <BR>}
* </CODE>
*
* @param stylefile the name of the text file
* @throws Exception an Exception is thrown if the stylefile can't be read
*/
public void init(String stylefile)
throws Exception {
Style default_style = m_doc.addStyle(StyleContext.DEFAULT_STYLE, m_doc.getLogicalStyle(0));
Style parent_style = default_style;
TextFile file = new TextFile(stylefile);
file.openForRead(file_id);
TextToken token = file.nextToken();
for (; token != null; token = file.nextToken()) {
parent_style = default_style;
String style_name = token.getString();
if (token.isIdentifier()) {
token = file.nextToken();
if (token.isIdentifier("extends")) {
token = file.nextToken();
Style ps = m_doc.getStyle(token.getString());
if (ps != null) {
parent_style = ps;
}
token = file.nextToken();
}
Style new_style = m_doc.addStyle(style_name, parent_style);
error("new style " + style_name);
if (!token.isChar('{')) {
throw new Exception("Can't read style on line " + file.getActLine());
}
Attribute attr = readNextAttribute(file);
// set the attributes
for (; attr != null; attr = readNextAttribute(file)) {
attr.set(new_style);
}
} else {
break;
}
}
file.close();
}
/**
* not yet supported!
*/
public void writeTo(String stylefile)
throws Exception {
TextFile file = new TextFile(stylefile);
if (file.exists()) {
file.openExistingFileForWrite(file_id, file_version);
} else {
file.openNewFileForWrite(file_id, file_version);
}
file.close();
}
private Attribute readNextAttribute(TextFile file)
throws Exception {
TextToken token = file.nextToken();
if (token != null && token.isChar('}')) {
return null;
}
if (token != null && token.isIdentifier()) {
for (int i = 0; i < attribute.length; ++i) {
if (token.isIdentifier(attribute[i].getIdentifier())) {
token = file.nextToken();
if (token != null && token.isChar('=')) {
token = file.nextToken();
if (token != null) {
return attribute[i].createFrom(token);
}
} else {
break;
}
}
}
}
throw new Exception("Syntax error on line " + file.getActLine());
}
}