/*
* [Fence.java]
*
* Summary: Describes one {} () [] token for display. Not considered an operator.
*
* Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
*
* Licence: This software may be copied and used freely for any purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.8+
*
* Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
*
* Version History:
* 2.0 2009-04-19 tidy comments, more accurate colour names
*/
package com.mindprod.jtokens.java;
import com.mindprod.jtokens.Token;
import java.awt.Color;
import java.awt.Font;
import static com.mindprod.jtokens.TokenColourScheme.JAVA_FOREGROUND_FOR_FENCE_BRACE;
import static com.mindprod.jtokens.TokenColourScheme.JAVA_FOREGROUND_FOR_FENCE_BRACKET;
import static com.mindprod.jtokens.TokenColourScheme.JAVA_FOREGROUND_FOR_FENCE_PAREN;
import static com.mindprod.jtokens.TokenFonts.BIGGEST_FENCE_SIZE_IN_POINTS;
import static com.mindprod.jtokens.TokenFonts.MONO_FONTS;
import static com.mindprod.jtokens.TokenFonts.NORMAL_FONT_SIZE_IN_POINTS;
/**
* Describes one {} () [] token for display. Not considered an operator.
*
* @author Roedy Green, Canadian Mind Products
* @version 2.0 2009-04-19 tidy comments, more accurate colour names
* @since 2004-04-24
*/
public final class Fence extends Token
{
/**
* version number for the class
*/
static final long serialVersionUID = 1L;
/**
* Font to render this token, prepare five variant sizes
*/
private static final Font[] font = new Font[ 5 ];
static
{
for ( int i = 0; i < 5; i++ )
{
int size = BIGGEST_FENCE_SIZE_IN_POINTS - i * 3;
// only bold larger point sizes
int style =
( size >= NORMAL_FONT_SIZE_IN_POINTS
? Font.BOLD
: Font.PLAIN );
Fence.font[ i ] = bestFont( MONO_FONTS, style, size );
}
}
/**
* The text to render
*/
private final char fenceChar;
/**
* nesting depth
*/
private int nestingDepth;
/**
* Constructor
*
* @param fenceChar e.g. {} () []
* @param nestingDepth nesting depth of this bracket. 0 is outermost, biggest bracket.
*/
public Fence( char fenceChar, int nestingDepth )
{
super( fenceChar );
this.fenceChar = fenceChar;
this.nestingDepth = nestingDepth;
}
/**
* gets the char to render this token
*
* @return The char representation of the text
*/
public char getChar()
{
return fenceChar;
}
/**
* font to render this token
*
* @return Font, in the correct size.
*/
public Font getFont()
{
// start () [] off smaller than {}
int i;
switch ( fenceChar )
{
case '(':
case ')':
case '[':
case ']':
i = nestingDepth + 2;
break;
case '{':
case '}':
default:
i = nestingDepth;
break;
}
if ( i < 0 )
{
i = 0;
}
if ( i >= 5 )
{
i = 4;
}
return font[ i ];
}
/**
* foreground colour to render this token.
*
* @return Color object.
*/
public Color getForeground()
{
switch ( fenceChar )
{
case '(':
case ')':
return JAVA_FOREGROUND_FOR_FENCE_PAREN;
case '[':
case ']':
return JAVA_FOREGROUND_FOR_FENCE_BRACKET;
case '{':
case '}':
default:
return JAVA_FOREGROUND_FOR_FENCE_BRACE;
}
}
/**
* get the text surrounded by CSS html * @return decorated HTML in a " + getRawHTML() + "";
case 1:
return "" + getRawHTML() + "";
case 2:
return "" + getRawHTML() + "";
default:
return "" + getRawHTML() + "";
}
}
/**
* Get nesting depth
*
* @return nesting depth of this bracket. 0 is outermost, biggest bracket.
*/
public int getNestingDepth()
{
return this.nestingDepth;
}
/**
* Set nesting depth
*
* @param nestingDepth nesting depth of this bracket. 0 is outermost, biggest bracket.
*/
public void setNestingDepth( int nestingDepth )
{
if ( nestingDepth < 0 )
{
nestingDepth = 0;
}
this.nestingDepth = nestingDepth;
}
/**
* gets the raw text to render this token
*
* @return The plain String representation of the text
*/
public String getText()
{
return String.valueOf( fenceChar );
}
/**
* Can this token be collapsed with following space or with a following identical token?
*
* @return true if can be collapsed simply by combining text
*/
public boolean isCollapsible()
{
// too individual
return false;
}
}