|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.shell.component; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Comparator; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Function; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +import org.jline.terminal.Terminal; |
| 27 | +import org.jline.utils.AttributedString; |
| 28 | + |
| 29 | +import org.springframework.shell.component.context.ComponentContext; |
| 30 | +import org.springframework.shell.component.support.AbstractSelectorComponent; |
| 31 | +import org.springframework.shell.component.support.Enableable; |
| 32 | +import org.springframework.shell.component.support.Itemable; |
| 33 | +import org.springframework.shell.component.support.Matchable; |
| 34 | +import org.springframework.shell.component.support.Nameable; |
| 35 | +import org.springframework.shell.component.support.AbstractSelectorComponent.SelectorComponentContext; |
| 36 | +import org.springframework.shell.component.MultiItemSelector.MultiItemSelectorContext; |
| 37 | + |
| 38 | +/** |
| 39 | + * Component able to pick multiple items. |
| 40 | + * |
| 41 | + * @author Janne Valkealahti |
| 42 | + */ |
| 43 | +public class MultiItemSelector<T, I extends Nameable & Matchable & Enableable & Itemable<T>> |
| 44 | + extends AbstractSelectorComponent<T, MultiItemSelectorContext<T, I>, I> { |
| 45 | + |
| 46 | + private MultiItemSelectorContext<T, I> currentContext; |
| 47 | + |
| 48 | + public MultiItemSelector(Terminal terminal, List<I> items, String name, Comparator<I> comparator) { |
| 49 | + super(terminal, name, items, false, comparator); |
| 50 | + setRenderer(new DefaultRenderer()); |
| 51 | + setTemplateLocation("classpath:org/springframework/shell/component/multi-item-selector-default.stg"); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected MultiItemSelectorContext<T, I> getThisContext(ComponentContext<?> context) { |
| 56 | + if (context != null && currentContext == context) { |
| 57 | + return currentContext; |
| 58 | + } |
| 59 | + currentContext = MultiItemSelectorContext.empty(getItemMapper()); |
| 60 | + currentContext.setName(name); |
| 61 | + if (currentContext.getItems() == null) { |
| 62 | + currentContext.setItems(getItems()); |
| 63 | + } |
| 64 | + context.stream().forEach(e -> { |
| 65 | + currentContext.put(e.getKey(), e.getValue()); |
| 66 | + }); |
| 67 | + return currentContext; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected MultiItemSelectorContext<T, I> runInternal(MultiItemSelectorContext<T, I> context) { |
| 72 | + super.runInternal(context); |
| 73 | + loop(context); |
| 74 | + return context; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Context {@link MultiItemSelector}. |
| 79 | + */ |
| 80 | + public interface MultiItemSelectorContext<T, I extends Nameable & Matchable & Itemable<T>> |
| 81 | + extends SelectorComponentContext<T, I, MultiItemSelectorContext<T, I>> { |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets a values. |
| 85 | + * |
| 86 | + * @return a values |
| 87 | + */ |
| 88 | + List<String> getValues(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates an empty {@link MultiItemSelectorContext}. |
| 92 | + * |
| 93 | + * @return empty context |
| 94 | + */ |
| 95 | + static <T, I extends Nameable & Matchable & Itemable<T>> MultiItemSelectorContext<T, I> empty() { |
| 96 | + return new DefaultMultiItemSelectorContext<>(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Creates an {@link MultiItemSelectorContext}. |
| 101 | + * |
| 102 | + * @return context |
| 103 | + */ |
| 104 | + static <T, I extends Nameable & Matchable & Itemable<T>> MultiItemSelectorContext<T, I> empty(Function<T, String> itemMapper) { |
| 105 | + return new DefaultMultiItemSelectorContext<>(itemMapper); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static class DefaultMultiItemSelectorContext<T, I extends Nameable & Matchable & Itemable<T>> extends |
| 110 | + BaseSelectorComponentContext<T, I, MultiItemSelectorContext<T, I>> implements MultiItemSelectorContext<T, I> { |
| 111 | + |
| 112 | + private Function<T, String> itemMapper = item -> item.toString(); |
| 113 | + |
| 114 | + DefaultMultiItemSelectorContext() { |
| 115 | + } |
| 116 | + |
| 117 | + DefaultMultiItemSelectorContext(Function<T, String> itemMapper) { |
| 118 | + this.itemMapper = itemMapper; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public List<String> getValues() { |
| 123 | + if (getResultItems() == null) { |
| 124 | + return Collections.emptyList(); |
| 125 | + } |
| 126 | + return getResultItems().stream() |
| 127 | + .map(i -> i.getItem()) |
| 128 | + .map(i -> itemMapper.apply(i)) |
| 129 | + .collect(Collectors.toList()); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Map<String, Object> toTemplateModel() { |
| 134 | + Map<String, Object> attributes = super.toTemplateModel(); |
| 135 | + attributes.put("values", getValues()); |
| 136 | + List<Map<String, Object>> rows = getItemStateView().stream() |
| 137 | + .map(is -> { |
| 138 | + Map<String, Object> map = new HashMap<>(); |
| 139 | + map.put("name", is.getName()); |
| 140 | + map.put("selected", is.isSelected()); |
| 141 | + map.put("onrow", getCursorRow().intValue() == is.getIndex()); |
| 142 | + map.put("enabled", is.isEnabled()); |
| 143 | + return map; |
| 144 | + }) |
| 145 | + .collect(Collectors.toList()); |
| 146 | + attributes.put("rows", rows); |
| 147 | + // finally wrap it into 'model' as that's what |
| 148 | + // we expect in stg template. |
| 149 | + Map<String, Object> model = new HashMap<>(); |
| 150 | + model.put("model", attributes); |
| 151 | + return model; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private class DefaultRenderer implements Function<MultiItemSelectorContext<T, I>, List<AttributedString>> { |
| 156 | + |
| 157 | + @Override |
| 158 | + public List<AttributedString> apply(MultiItemSelectorContext<T, I> context) { |
| 159 | + return renderTemplateResource(context.toTemplateModel()); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments