Skip to content

Instantly share code, notes, and snippets.

View marius-m's full-sized avatar

Marius Merkevičius marius-m

View GitHub Profile
@marius-m
marius-m / TextAreaLineCounter.kt
Created March 1, 2022 10:18
Line count mechanism in JTextArea
package lt.linecounter
import java.awt.Font
import java.awt.FontMetrics
import java.awt.Insets
import java.awt.font.LineBreakMeasurer
import java.awt.font.TextAttribute
import java.text.AttributedString
import java.text.BreakIterator
import javax.swing.JTextArea
@marius-m
marius-m / MainJTextArea.kt
Created March 1, 2022 10:20
GUI App snippet for testing out line count in JTextArea
package lt.linecounter
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.awt.*
import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JTextArea
@marius-m
marius-m / TextAreaLineCounter2.kt
Created March 4, 2022 12:37
Text area line counter with parsing
/**
* Parses text to fit in [TextProvider.formatWidth] and wraps whenever needed
*/
class TextAreaLineCounter(
private val textProvider: TextProvider
) {
private val formatWidth: Float
get() = textProvider.formatWidth
@marius-m
marius-m / TextAreaDrawer.kt
Created March 4, 2022 12:38
Text area drawing mechanism + font scaling into bounding box
import lt.ito.export.TextAreaLineCounter
import sun.font.FontDesignMetrics
import java.awt.Font
import java.awt.FontMetrics
import java.awt.Graphics
/**
* Scales font into bounding [boxWidth], [boxHeight] to fit in and draws it using word wrap
*/
class TextAreaScaleDrawer(
/**
* Inspiration and main resource taken from [com.google.android.material.divider.MaterialDividerItemDecoration]
*
* Divider is solving only vertical scrolling use case.
* It divides items that are vertically scrolled + "divides" the first item (paints divider on top)
*
* Will not paint a full rectangle if the [RecyclerView] scrolls only in one direction, because
* the dividers are pointed on top. So the dividers "stays on top" whenever scrolling.
*/
class MaterialDividerVerticalOnlyWithFirstItem(
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class LogUtils {
@NonNull public static String objPrefix(@Nullable Object obj) {
if (obj != null) {
return String.format(
"[%s@%s]",
obj.getClass().getSimpleName(),
/**
* Debugging utils
*/
object LogUtils {
/**
* Converts [obj] to instance signature
* Easier to figure out different object instance usage in combination to logging
*/
@JvmStatic
@marius-m
marius-m / ItemOffsetCornerItemsDecoration.kt
Created May 16, 2022 13:08
Item decorator which can apply offsets to first and last items
package lt.markmerkk
import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import lt.ermitazas.base.dpToPx
/**
@marius-m
marius-m / ItemDecoratorOffset.kt
Created May 20, 2022 07:16
Helper class to define offset values in ItemDecorator
data class ItemDecoratorOffset private constructor(
val top: Int,
val bottom: Int,
val start: Int,
val end: Int,
) {
companion object {
fun asEmpty(): ItemDecoratorOffset {
return ItemDecoratorOffset(
top = 0,
@marius-m
marius-m / DividerItemWithoutLastDecoration.kt
Last active May 20, 2022 08:12
ItemDecorator for RecyclerView + no decorator on the last item + offset for decorator
class DividerItemWithoutLastDecoration(
val divider: Drawable,
val dividerOffset: ItemDecoratorOffset = ItemDecoratorOffset.asEmpty(),
) : RecyclerView.ItemDecoration() {
override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val dividerLeft = parent.paddingLeft + dividerOffset.start
val dividerRight = parent.width - parent.paddingRight - dividerOffset.end
val childCount = parent.childCount
for (i in 0..childCount - 2) {