/*
 * Copyright 2012 Kulikov Dmitriy
 * Copyright 2017 Nikita Shakarun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package javax.microedition.lcdui;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import javax.microedition.lcdui.event.SimpleEvent;

public class StringItem extends Item {
	private String text;
	private TextView textview;
	private int appearanceMode;

	private SimpleEvent msgSetText = new SimpleEvent() {
		@Override
		public void process() {
			textview.setText(text);
		}
	};

	public StringItem(String label, String text) {
		this(label, text, PLAIN);
	}

	public StringItem(String label, String text, int appearanceMode) {
		setLabel(label);
		setText(text);
		this.appearanceMode = appearanceMode;
	}

	public void setText(String text) {
		this.text = text;

		if (textview != null) {
			ViewHandler.postEvent(msgSetText);
		}
	}

	public String getText() {
		return text;
	}

	public Font getFont() {
		return Font.getDefaultFont();
	}

	public void setFont(Font font) {
	}

	public int getAppearanceMode() {
		return appearanceMode;
	}

	@Override
	public View getItemContentView() {
		if (textview == null) {
			Context context = getOwnerForm().getParentActivity();

			textview = new TextView(context);
			textview.setTextAppearance(context, android.R.style.TextAppearance_Small);
			textview.setText(text);
			textview.setOnClickListener(v -> fireDefaultCommandAction());
		}

		return textview;
	}

	@Override
	public void clearItemContentView() {
		textview = null;
	}
}
@@ -720,16 +720,46 @@
 w.View;%0A
+import android.widget.Button;%0A
 import a
@@ -1778,16 +1778,100 @@
 ity();%0A%0A
+%09%09%09if (appearanceMode == BUTTON) %7B%0A%09%09%09%09textview = new Button(context);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09textv
@@ -1895,24 +1895,30 @@
 w(context);%0A
+%09%09%09%7D%0A%0A
 %09%09%09textview.

Fix StringItem appearance