Overhaul ListView to support checked states

- ListView can be defined to use nocheck, checked or radio
- List can now scroll through up/down
- Actual visual is handled in a DefaultListCell
- Modify/add scenarios and catalog app
- Relates #865
This commit is contained in:
Janne Valkealahti
2023-09-09 08:59:37 +03:00
parent a679de0067
commit 51c250608b
12 changed files with 971 additions and 195 deletions

View File

@@ -44,7 +44,7 @@ import org.springframework.shell.component.view.control.MenuView.MenuItemCheckSt
import org.springframework.shell.component.view.control.StatusBarView;
import org.springframework.shell.component.view.control.StatusBarView.StatusItem;
import org.springframework.shell.component.view.control.View;
import org.springframework.shell.component.view.control.cell.ListCell;
import org.springframework.shell.component.view.control.cell.AbstractListCell;
import org.springframework.shell.component.view.event.EventLoop;
import org.springframework.shell.component.view.event.KeyEvent;
import org.springframework.shell.component.view.event.KeyEvent.Key;
@@ -148,7 +148,7 @@ public class Catalog {
// start main scenario browser
ui.setRoot(app, true);
ui.setFocus(categories);
categories.setSelected(0);
// categories.setSelected(0);
ui.run();
}
@@ -238,7 +238,11 @@ public class Catalog {
return categories;
}
private static class ScenarioListCell extends ListCell<ScenarioData> {
private static class ScenarioListCell extends AbstractListCell<ScenarioData> {
public ScenarioListCell(ScenarioData item) {
super(item);
}
@Override
public void draw(Screen screen) {
@@ -257,7 +261,7 @@ public class Catalog {
scenarios.setTitle("Scenarios");
scenarios.setFocusedTitleStyle(ScreenItem.STYLE_BOLD);
scenarios.setShowBorder(true);
scenarios.setCellFactory(list -> new ScenarioListCell());
scenarios.setCellFactory((list, item) -> new ScenarioListCell(item));
return scenarios;
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2023 the original author or authors.
*
* 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
*
* https://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 org.springframework.shell.samples.catalog.scenario.listview;
import java.util.Arrays;
import org.springframework.shell.component.view.control.ListView;
import org.springframework.shell.component.view.control.View;
import org.springframework.shell.component.view.control.ListView.ItemStyle;
import org.springframework.shell.samples.catalog.scenario.AbstractScenario;
import org.springframework.shell.samples.catalog.scenario.Scenario;
import org.springframework.shell.samples.catalog.scenario.ScenarioComponent;
@ScenarioComponent(name = "Radio List", description = "Items with radio states", category = { Scenario.CATEGORY_LISTVIEW })
public class CheckedListViewScenario extends AbstractScenario {
@Override
public View build() {
ListView<String> view = new ListView<>(ItemStyle.RADIO);
view.setEventLoop(getEventloop());
view.setItems(Arrays.asList("item1", "item2", "item3"));
return view;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2023 the original author or authors.
*
* 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
*
* https://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 org.springframework.shell.samples.catalog.scenario.listview;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.shell.component.view.control.ListView;
import org.springframework.shell.component.view.control.View;
import org.springframework.shell.samples.catalog.scenario.AbstractScenario;
import org.springframework.shell.samples.catalog.scenario.Scenario;
import org.springframework.shell.samples.catalog.scenario.ScenarioComponent;
@ScenarioComponent(name = "Long List", description = "Show many items", category = { Scenario.CATEGORY_LISTVIEW })
public class LongListViewScenario extends AbstractScenario {
@Override
public View build() {
ListView<String> view = new ListView<>();
view.setEventLoop(getEventloop());
List<String> items = IntStream.of(20).mapToObj(i -> "item" + i).collect(Collectors.toList());
view.setItems(items);
return view;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2023 the original author or authors.
*
* 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
*
* https://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 org.springframework.shell.samples.catalog.scenario.listview;
import java.util.Arrays;
import org.springframework.shell.component.view.control.ListView;
import org.springframework.shell.component.view.control.View;
import org.springframework.shell.component.view.control.ListView.ItemStyle;
import org.springframework.shell.samples.catalog.scenario.AbstractScenario;
import org.springframework.shell.samples.catalog.scenario.Scenario;
import org.springframework.shell.samples.catalog.scenario.ScenarioComponent;
@ScenarioComponent(name = "Checked List", description = "Items with checked states", category = { Scenario.CATEGORY_LISTVIEW })
public class RadioListViewScenario extends AbstractScenario {
@Override
public View build() {
ListView<String> view = new ListView<>(ItemStyle.CHECKED);
view.setEventLoop(getEventloop());
view.setItems(Arrays.asList("item1", "item2", "item3"));
return view;
}
}