view src/luan/modules/swing/Text_area.luan @ 1996:d5c21ca9703e default tip

move threads to admin
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Jul 2025 11:39:41 -0600
parents bb12d502b72e
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local set_metatable = Luan.set_metatable or error()
local ipairs = Luan.ipairs or error()
local Utils = require "luan:swing/Utils.luan"
local fail = Utils.fail or error()
local make_metatable = Utils.make_metatable or error()
local remove = Utils.remove or error()
local check_empty = Utils.check_empty or error()
local Text_component = require("luan:swing/Text_component.luan")
local super__index = Text_component.__index or error()
local super__new_index = Text_component.__new_index or error()
local super_construct = Text_component.construct or error()
require "java"
local TextAreaLuan = require "java:luan.modules.swing.TextAreaLuan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "swing/Text_area"


local Text_area = {}

function Text_area.__index(text_area,key)
	local rtn = super__index(text_area,key)
	if rtn ~= fail then return rtn end
	local jtext_area = text_area.java
	if key == "line_wrap" then
		return jtext_area.getLineWrap()
	end
	if key == "wrap_style_word" then
		return jtext_area.getWrapStyleWord()
	end
	if key == "tab_size" then
		return jtext_area.getTabSize()
	end
	if key == "rows" then
		return jtext_area.getRows()
	end
	if key == "columns" then
		return jtext_area.getColumns()
	end
	if key == "line_count" then
		return jtext_area.getLineCount()
	end
	if key == "whitespace_visible" then
		return jtext_area.isWhitespaceVisible()
	end
	return fail
end

function Text_area.__new_index(text_area,key,value)
	local rtn = super__new_index(text_area,key,value)
	if rtn ~= fail then return end
	local jtext_area = text_area.java
	if key == "line_wrap" then
		jtext_area.setLineWrap(value)
		return
	end
	if key == "wrap_style_word" then
		jtext_area.setWrapStyleWord(value)
		return
	end
	if key == "tab_size" then
		jtext_area.setTabSize(value)
		return
	end
	if key == "rows" then
		jtext_area.setRows(value)
		return
	end
	if key == "columns" then
		jtext_area.setColumns(value)
		return
	end
	if key == "whitespace_visible" then
		jtext_area.setWhitespaceVisible(value)
		return
	end
	return fail
end

local mt = make_metatable(Text_area)

function Text_area.new(props)
	local jtext_area = TextAreaLuan.new()
	local text_area = { java = jtext_area }
	super_construct(text_area,props)
	local rows = remove(props,"rows")
	if rows~=nil then jtext_area.setRows(rows) end
	local columns = remove(props,"columns")
	if columns~=nil then jtext_area.setColumns(columns) end
	local wrap_style_word = remove(props,"wrap_style_word")
	if wrap_style_word~=nil then jtext_area.setWrapStyleWord(wrap_style_word) end
	local line_wrap = remove(props,"line_wrap")
	if line_wrap~=nil then jtext_area.setLineWrap(line_wrap) end
	local tab_size = remove(props,"tab_size")
	if tab_size~=nil then jtext_area.setTabSize(tab_size) end
	local whitespace_visible = remove(props,"whitespace_visible")
	if whitespace_visible~=nil then jtext_area.setWhitespaceVisible(whitespace_visible) end
	check_empty(props)
	function text_area.get_line_from_position(pos)
		return jtext_area.getLineOfOffset(pos-1) + 1
	end
	function text_area.get_line_start_position(line)
		return jtext_area.getLineStartOffset(line-1) + 1
	end
	function text_area.get_line_end_position(line)
		return jtext_area.getLineEndOffset(line-1) + 1
	end
	function text_area.insert(text,pos)
		return jtext_area.insert(text,pos-1)
	end
	function text_area.replace(start_pos,length,text)
		text_area._document.run_in_transaction(function()
			return jtext_area.getDocument().replace(start_pos-1,length,text)
		end)
	end
	function text_area.set_hightlights(hightlights)
		local list = {}
		for _, hightlight in ipairs(hightlights) do
			list[#list+1] = TextAreaLuan.Range.new(hightlight.start-1,hightlight.end_-1)
		end
		jtext_area.setHightlights(list)
	end
	text_area.clear_hightlights = jtext_area.clearHighlights
	set_metatable(text_area,mt)
	return text_area
end

return Text_area