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 BlockFunctionDefinition extends Block
{
/**
*
*/
private static final long serialVersionUID = 4818423557418859817L;
private JLabel functionLabel = null;
public BlockFunctionDefinition(FilePosition filePosition, Evaluator eval, String text) {
super(filePosition, eval, VLCanvas.LINE_HEIGHT, Color.decode("#607EA3"),null, text);
setImmutableProperties();
}
public BlockFunctionDefinition(BlockFunctionDefinition 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));
functionLabel = new JLabel("function");
functionLabel.setFont(new Font(this.getFont().getFontName(), Font.PLAIN, 16));
functionLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
functionLabel.setForeground(Color.white);
this.remove(mRoundedText);
this.add(functionLabel);
this.add(mRoundedText);
validateForText();
}
@Override
protected void validateForText()
{
super.validateForText();
if (functionLabel != null) {
int width = Math.max(10, mRoundedText.getPreferredSize().width) + functionLabel.getPreferredSize().width + getPadding().left + getPadding().right;
Dimension newBlockSize = new Dimension(width, getPreferredSize().height);
setPreferredSize(newBlockSize);
setSize(newBlockSize);
mPath = Block.getSimplePath(0, 0, newBlockSize.width, newBlockSize.height);
validate();
repaint();
}
}
@Override
public String generateCode()
{
return "function " + mRoundedText.getText() + "(";
}
}