package com.drawbridge.vl.blocks; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; // import java.awt.Insets; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import com.drawbridge.jsengine.ast.Evaluator; import com.drawbridge.vl.VLCanvas; import com.drawbridge.vl.VLPanel; import com.google.caja.lexer.FilePosition; public class BlockPrimitive extends Block { private static final long serialVersionUID = 1411967550040191998L; public enum PrimitiveType { TYPE_NUMBER(Color.decode("#30B66F")), TYPE_STRING(Color.decode("#FF2626")), TYPE_BOOLEAN(Color.gray); ; PrimitiveType(Color color) { this.color = color; } private final Color color; }; private PrimitiveType mType = PrimitiveType.TYPE_NUMBER; public BlockPrimitive(FilePosition filePosition, Evaluator eval, String text) { super(filePosition, eval, VLCanvas.LINE_HEIGHT, Color.decode("#BBCDE9"), null, text); setImmutableProperties(); } public BlockPrimitive(BlockPrimitive block) { super(block); setImmutableProperties(); } private void setImmutableProperties() { mRoundedText.getDocument().addDocumentListener(primitiveDocumentListener); int textHeight = mRoundedText.getFontMetrics(mRoundedText.getFont()).getMaxAdvance() + mRoundedText.getFontMetrics(mRoundedText.getFont()).getMaxDescent(); mRoundedText.setMaximumSize(new Dimension(1000, textHeight)); // Set Font Size of Text mRoundedText.setForeground(Color.white); mRoundedText.setFont(new Font(this.getFont().getFontName(), Font.BOLD, 22)); // Insets oldBorderInsets = new Insets(10, 5, 5, 5); // mRoundedText.setBorder(BorderFactory.createLineBorder(Color.black)); mRoundedText.setHorizontalAlignment(JTextField.CENTER); // mRoundedText.setBorder(BorderFactory.createEmptyBorder(oldBorderInsets.left, oldBorderInsets.top, // oldBorderInsets.bottom, oldBorderInsets.right)); // Add extra mouse listener for mRoundedText so we can dial mRoundedText.addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent me) { if (mType == PrimitiveType.TYPE_NUMBER) { VLPanel.getInstance().mCanvas.displayDialler(BlockPrimitive.this, mRoundedText.getText(), BlockPrimitive.this.getLocation()); } } }); processChange(); validateForText(); } @Override public Color getColor() { return BlockPrimitive.this.mColor; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; switch (mType) { case TYPE_NUMBER: break; case TYPE_STRING: g2.setColor(Color.black); g2.setFont(new Font(mRoundedText.getFont().getFontName(), Font.BOLD, 30)); g2.drawString("\"", 16, 32); g2.drawString("\"", getWidth() - 32, 32); break; case TYPE_BOOLEAN: // nothing break; } } DocumentListener primitiveDocumentListener = new DocumentListener() { @Override public void changedUpdate(DocumentEvent de) { } @Override public void insertUpdate(DocumentEvent de) { processChange(); } @Override public void removeUpdate(DocumentEvent de) { processChange(); } }; public void processChange() { // Differentiate between string and number String text = mRoundedText.getText(); String[] splitResult = text.split("[-]?[0-9][0-9.]*"); if (splitResult.length == 0) { // is Integer mType = PrimitiveType.TYPE_NUMBER; } else if (text.length() == 0) { // is Unknown mType = PrimitiveType.TYPE_NUMBER; } else if (text.length() > 0 && splitResult.length == 1 && (splitResult[0].equals("true") || splitResult[0].equals("false"))) { mType = PrimitiveType.TYPE_BOOLEAN; } else { // is String mType = PrimitiveType.TYPE_STRING; mRoundedText.setForeground(Color.white); } mColor = mType.color; repaint(); validateForText(); } @Override public String generateCode() { return (mType == BlockPrimitive.PrimitiveType.TYPE_STRING) ? ("\"" + mRoundedText.getText() + "\"") : mRoundedText.getText(); } @Override protected void validateForText() { super.validateForText(); if (this.getGraphics() != null) { int width = Math.max(10, mRoundedText.getPreferredSize().width) + getPadding().left + getPadding().right - 5; Dimension newBlockSize = new Dimension(width, getSize().height); setPreferredSize(newBlockSize); setSize(newBlockSize); mPath = Block.getSimplePath(0, 0, newBlockSize.width, newBlockSize.height); validate(); repaint(); } } }