From f25f248853f33755e9bf8ed62dbfa9a56956881a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Fr=C4=85cz?= Date: Tue, 17 Mar 2020 19:43:00 +0100 Subject: [PATCH] Line.java proposal --- src/main/java/pl/fracz/mcr/source/Line.java | 117 ++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/main/java/pl/fracz/mcr/source/Line.java diff --git a/src/main/java/pl/fracz/mcr/source/Line.java b/src/main/java/pl/fracz/mcr/source/Line.java new file mode 100644 index 0000000..ac3a611 --- /dev/null +++ b/src/main/java/pl/fracz/mcr/source/Line.java @@ -0,0 +1,117 @@ +package pl.fracz.mcr.source; + +/* + 2013-10-23, fracz, first implementation + 2013-10-30, fracz, added syntax highlighting + 2014-02-26, fracz, added ability to add voice comment + */ + +import android.annotation.SuppressLint; +import android.content.Context; +import android.graphics.Color; +import android.graphics.Typeface; +import android.text.Html; +import android.widget.LinearLayout; +import android.widget.TextView; +import pl.fracz.mcr.comment.AbstractComment; +import pl.fracz.mcr.comment.Comment; +import pl.fracz.mcr.comment.CommentNotAddedException; + +import java.io.File; +import java.io.Serializable; +import java.util.List; + +/** + * View that represents one line of code. + */ +@SuppressLint("ViewConstructor") +public class Line extends LinearLayout implements Serializable { + private static final long serialVersionUID = 3076583280108678995L; + private static final int TWO = 2; + + private final int _lineNumber; + + private final String _lineOfCode; + + // holds the line number + private TextView lineNumberView; + + private TextView lineContent; + + private SourceFile sourceFile; + + private List comments; + + public Line(Context context, SourceFile sourceFile, int lineNumber, + String lineOfCode, boolean syntaxColor) { + super(context); + this.sourceFile = sourceFile; + this._lineNumber = lineNumber; + this._lineOfCode = lineOfCode; + setOrientation(LinearLayout.HORIZONTAL); + + lineNumberView = new TextView(getContext()); + lineNumberView.setText(String.format("%d.", lineNumber);); + lineNumberView.setSingleLine(); + lineNumberView.setWidth(30); + addView(lineNumberView); + + TextView lineContent = new TextView(getContext()); + addLineContent(syntaxColor); + } + + public int get() { + return _lineNumber; + } + + /** + * Adds a text comment. + * + * @param comment + * @throws CommentNotAddedException + */ + public void addTextComment(String comment) throws CommentNotAddedException { + Comment textComment = new Comment(AbstractComment.Type.TEXT, this); + textComment.setText(comment); + comments.add(textComment); + if (comments.size() > 0) { + lineNumberView.setBackgroundColor(Color.parseColor("#008000")); + } + } + + /** + * Adds a voice comment. + * + * @param recordedFile + * @throws CommentNotAddedException + */ + public void createVoiceComment(File recodedFile) throws CommentNotAddedException { + Comment voiceComment = new Comment(AbstractComment.Type.VOICE, this); + voiceComment.setFile(recodedFile); + comments.add(voiceComment); + if (comments.size() > 0) { + lineNumberView.setBackgroundColor(Color.parseColor("#008000")); + } + } + +// public void addVideoComment(File videoFile) throws CommentNotAddedException { +// } + + private void addLineContent(boolean syntaxColor){ + if (!syntaxColor || !SyntaxHighlighter.canBeHighlighted(syntaxColor)) + lineContent.setText(Html.fromHtml(lineOfCode)); + else + lineContent.setText(SyntaxHighlighter.highlight(Html.fromHtml(lineOfCode))); + lineContent.setTypeface(Typeface.MONOSPACE); + addView(lineContent); + } + + public List getComments(){ + return this.comments; + } + + public boolean hasConversation() { + sourceFile.markConversation(this); + return getComments().size() > TWO; + } +}