package gui.mainFrame.codeViewer;
import common.Constants;
import gui.GUIController;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
import net.sf.jhighlighter.kits.JavaHighlightKit;
import net.sf.jhighlighter.ui.FScrollPane;
import testGenerator.testMethodModel.AutoJUnitMethod;
/**
* Provides window to input code.
* @author William Whitney
*/
public class CodeViewer extends JPanel
{
private static final long serialVersionUID = 1L;
private final GUIController guiController;
private FScrollPane codeWindow;
private JTextPane text;
public CodeViewer(GUIController guiController)
{
this.guiController = guiController;
//Setup panel
BorderLayout layout = new BorderLayout();
this.setLayout(layout);
this.setMinimumSize(new Dimension(400, 0));
//Add Title
this.add(getTitle(), BorderLayout.NORTH);
//Add Code Input Window
this.add(getCodeWindow(), BorderLayout.CENTER);
}
private JLabel getTitle()
{
JLabel label = new JLabel("Source Code Viewer");
label.setFont(Constants.getHeadingFont());
return label;
}
/**
* Add windows that accepts code from user.
*/
private FScrollPane getCodeWindow()
{
JavaHighlightKit kit = new JavaHighlightKit();
this.text = new JTextPane((StyledDocument) kit.createDefaultDocument());
this.codeWindow = new FScrollPane(text);
this.text.setEditable(false);
this.text.setFont(Constants.getSourceFont());
codeWindow.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
return codeWindow;
}
public void setCode(String sourceCode)
{
this.text.setText(sourceCode);
this.text.setCaretPosition(0);
}
/**
* Highlights the specific method and test branch. If the default test branch
* is given then the method is highlighted. This also occurs when branch is null.
* @param method
*/
public void highlightLine(AutoJUnitMethod method)
{
SourceCaretFinder caretFinder = new SourceCaretFinder(this.text.getText(), method);
this.text.setCaretPosition(caretFinder.getMethodStartCaretPosition());
}
}