Skip to content

Commit

Permalink
LCDUI: fix Item.notifyStateChanged()
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Aug 1, 2024
1 parent 5d18a8b commit 190bb47
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/javax/microedition/lcdui/ChoiceGroup.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2018-2021 Nikita Shakarun
* Copyright 2019-2023 Yury Kharchenko
* Copyright 2019-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -382,7 +382,7 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (choiceType == MULTIPLE) {
item.setSelected(!item.isSelected());
adapter.notifyDataSetChanged();
notifyStateChanged();
postStateChanged();
return;
}
if (!item.isSelected()) {
Expand All @@ -391,7 +391,7 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
item.setSelected(true);
adapter.notifyDataSetChanged();
notifyStateChanged();
postStateChanged();
}
}
}
Expand All @@ -409,7 +409,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
adapter.notifyDataSetChanged();
}
}
notifyStateChanged();
postStateChanged();
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/javax/microedition/lcdui/Display.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2017-2018 Nikita Shakarun
* Copyright 2020-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -140,8 +141,11 @@ public boolean vibrate(int duration) {
}

public void setCurrentItem(Item item) {
if (item.hasOwner()) {
setCurrent(item.getOwner());
Screen owner = item.getOwner();
if (owner instanceof Form) {
setCurrent(owner);
} else {
throw new IllegalStateException("Item is not owned by a Form");
}
}

Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/javax/microedition/lcdui/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2012 Kulikov Dmitriy
* Copyright 2015-2016 Nickolay Savchenko
* Copyright 2017-2018 Nikita Shakarun
* Copyright 2020-2023 Yury Kharchenko
* Copyright 2020-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -28,6 +28,7 @@

import java.util.ArrayList;

import javax.microedition.lcdui.event.SimpleEvent;
import javax.microedition.util.ContextHolder;

public class Form extends Screen {
Expand Down Expand Up @@ -158,10 +159,16 @@ public void setItemStateListener(ItemStateListener listener) {
this.listener = listener;
}

public void notifyItemStateChanged(Item item) {
if (listener != null) {
listener.itemStateChanged(item);
}
void notifyItemStateChanged(Item item) {
Display.postEvent(new SimpleEvent() {
@Override
public void process() {
ItemStateListener l = listener;
if (l != null) {
l.itemStateChanged(item);
}
}
});
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/javax/microedition/lcdui/Gauge.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2021-2023 Yury Kharchenko
* Copyright 2021-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -125,7 +125,7 @@ private class SeekBarListener implements SeekBar.OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
value = progress;
notifyStateChanged();
postStateChanged();
}
}

Expand Down
16 changes: 9 additions & 7 deletions app/src/main/java/javax/microedition/lcdui/Item.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2019-2023 Yury Kharchenko
* Copyright 2019-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -136,8 +136,8 @@ public int getPreferredWidth() {
}

public void notifyStateChanged() {
if (owner instanceof Form form) {
form.notifyItemStateChanged(this);
if (owner instanceof Form) {
postStateChanged();
} else {
throw new IllegalStateException("Item is not owned by a Form");
}
Expand Down Expand Up @@ -204,6 +204,12 @@ public void setPreferredSize(int width, int height) {
this.height = height == -1 ? 0 : height;
}

void postStateChanged() {
if (owner instanceof Form form) {
form.notifyItemStateChanged(this);
}
}

void setOwner(Screen owner) {
this.owner = owner;
ViewHandler.postEvent(new SimpleEvent() {
Expand All @@ -215,10 +221,6 @@ public void process() {
}

Screen getOwner() {
if (owner == null) {
throw new IllegalStateException("call setOwnerForm() before calling getOwnerForm()");
}

return owner;
}

Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/javax/microedition/lcdui/TextBox.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2017 Nikita Shakarun
* Copyright 2021-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +18,10 @@

package javax.microedition.lcdui;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;

import javax.microedition.util.ContextHolder;

Expand Down Expand Up @@ -90,8 +93,10 @@ public void delete(int offset, int length) {

@Override
public View getScreenView() {
Context context = ContextHolder.getActivity();
return textField.getView(context, null);
EditText et = textField.getView(ContextHolder.getActivity(), null);
et.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
et.setGravity(Gravity.TOP);
return et;
}

@Override
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/javax/microedition/lcdui/TextField.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2017-2018 Nikita Shakarun
* Copyright 2021-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +18,6 @@

package javax.microedition.lcdui;

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

import javax.microedition.util.ContextHolder;
Expand Down Expand Up @@ -105,8 +105,13 @@ public void delete(int offset, int length) {

@Override
public View getItemContentView() {
Context context = ContextHolder.getActivity();
return textField.getView(context, this);
View view = textField.getView(ContextHolder.getActivity(), this);
view.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
postStateChanged();
}
});
return view;
}

@Override
Expand Down
14 changes: 1 addition & 13 deletions app/src/main/java/javax/microedition/lcdui/TextFieldImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright 2012 Kulikov Dmitriy
* Copyright 2019 Nikita Shakarun
* Copyright 2021-2024 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,10 +24,7 @@
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.widget.AppCompatEditText;

Expand Down Expand Up @@ -193,16 +191,6 @@ public void afterTextChanged(Editable s) {
text = s.toString();
}
});

if (item != null) {
textview.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) item.notifyStateChanged();
});
} else {
textview.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textview.setGravity(Gravity.TOP);
}
}
return textview;
}
Expand Down

0 comments on commit 190bb47

Please sign in to comment.