package com.drawbridge.vl.blocks;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import com.drawbridge.jsengine.ast.Evaluator;
import com.drawbridge.utils.Utils;
import com.drawbridge.vl.VLCanvas;
import com.google.caja.lexer.FilePosition;
public class BlockDeclaration extends Block
{
/**
*
*/
private static final long serialVersionUID = 7059870345356142308L;
private JLabel varLabel = null;
public BlockDeclaration(FilePosition filePosition, Evaluator eval, String text) {
super(filePosition, eval, VLCanvas.LINE_HEIGHT, Color.decode("#FFBFBF"), null, text);
setImmutableProperties();
}
public BlockDeclaration(BlockDeclaration block) {
super(block);
setImmutableProperties();
}
private void setImmutableProperties()
{
// Restrict Formatting
Utils.adaptTextFieldInputForVariableNames(mRoundedText, '_');
// Set Font Size of Text
mRoundedText.setForeground(Color.white);
mRoundedText.setFont(new Font(this.getFont().getFontName(), Font.BOLD, 28));
varLabel = new JLabel("var");
varLabel.setFont(new Font(this.getFont().getFontName(), Font.PLAIN, 16));
varLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
varLabel.setForeground(Color.white);
this.remove(mRoundedText);
this.add(varLabel);
this.add(mRoundedText);
validateForText();
}
@Override
protected void validateForText()
{
super.validateForText();
if (varLabel != null) {
int width = Math.max(10, mRoundedText.getPreferredSize().width) + varLabel.getPreferredSize().width + getPadding().left + getPadding().right;
Dimension newBlockSize = new Dimension(width, getPreferredSize().height);
setPreferredSize(newBlockSize);
setSize(newBlockSize);
validate();
repaint();
}
}
@Override
public String generateCode()
{
return "var " + mRoundedText.getText();
}
}