From bec0a0e1d8e030e09832322d70e4e82f9aadb07e Mon Sep 17 00:00:00 2001 From: mikusugar Date: Wed, 21 Jul 2021 11:26:54 +0800 Subject: [PATCH 1/6] add: web delNode Buutton --- .../views/sugarrandom/SugarRandomView.java | 613 ++++++++++-------- .../sugar/random/cli/service/CliService.java | 6 +- .../random/core/bean/SugarJsonNode.java | 2 + 3 files changed, 335 insertions(+), 286 deletions(-) diff --git a/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java b/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java index cd58ad4..35aef88 100755 --- a/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java +++ b/src/main/java/me/mikusugar/randomsugar/app/views/sugarrandom/SugarRandomView.java @@ -46,297 +46,344 @@ @CssImport("./views/sugarrandom/sugar-random-view.css") public class SugarRandomView extends HorizontalLayout { - protected Logger log = LoggerFactory.getLogger(getClass()); - - private TextField configName; - private Button readConfig; - private Button saveConfig; - private Button delConfig; - - private TextField fieldName; - private TextField fieldFather; - private Select filedType; - private Select randomType; - private TextArea randomInfo; - private Button next; - private Label label; - private TreeGrid treeGrid; - private NumberField number; - private Button start; - private Map map; - private SugarJsonNode rootNode; - private TextArea area; - - // 结果预览 - private Dialog resPreDialog; - private TextArea resPreTextArea; - private Button resPreButton; - - @Autowired private Map randomServiceMap; - - @Autowired private ConfigSavaRepository configSavaRepository; - - public SugarRandomView() { - this.rootNode = - SugarJsonNode.builder() - .name("root") - .type(SugarJsonNode.TYPE.OBJECT) - .desc("默认根节点") - .randomServiceName(ServiceName.RANDOM_OBJ) - .build(); - this.map = new HashMap<>(); - this.map.put("root", rootNode); - initView(); - even(); - } - - private void even() { - - readConfig.addClickListener( - buttonClickEvent -> { - if (StringUtils.isEmpty(configName.getValue())) NotionUtils.defaultNotion("配置名为空!!!无法读取"); - else { - final Optional savaRepositoryById = - configSavaRepository.findById(configName.getValue()); - if (savaRepositoryById.isPresent()) { - try { - rootNode = - SugarJsonNodeSerialization.read( - savaRepositoryById.get().getJson(), randomServiceMap); + + /////////////////////////////////////////////////////////////////////////// + // 定义成员变量 + /////////////////////////////////////////////////////////////////////////// + + protected Logger log = LoggerFactory.getLogger(getClass()); + + private TextField configName; + private Button readConfig; + private Button saveConfig; + private Button delConfig; + + private TextField fieldName; + private TextField fieldFather; + private Select filedType; + private Select randomType; + private TextArea randomInfo; + private Button next; + private Button delNode; + private Label label; + private TreeGrid treeGrid; + private NumberField number; + private Button start; + private Map map; + private SugarJsonNode rootNode; + private TextArea area; + + // 结果预览 + private Dialog resPreDialog; + private TextArea resPreTextArea; + private Button resPreButton; + + @Autowired + private Map randomServiceMap; + + @Autowired + private ConfigSavaRepository configSavaRepository; + + public SugarRandomView() { + this.rootNode = + SugarJsonNode.builder() + .name("root") + .type(SugarJsonNode.TYPE.OBJECT) + .desc("默认根节点") + .randomServiceName(ServiceName.RANDOM_OBJ) + .build(); + this.map = new HashMap<>(); + this.map.put("root", rootNode); + initView(); + even(); + } + + + /////////////////////////////////////////////////////////////////////////// + // 页面事件处理 + /////////////////////////////////////////////////////////////////////////// + + + private void even() { + + readConfig.addClickListener( + buttonClickEvent -> { + if (StringUtils.isEmpty(configName.getValue())) NotionUtils.defaultNotion("配置名为空!!!无法读取"); + else { + final Optional savaRepositoryById = + configSavaRepository.findById(configName.getValue()); + if (savaRepositoryById.isPresent()) { + try { + rootNode = + SugarJsonNodeSerialization.read( + savaRepositoryById.get().getJson(), randomServiceMap); + flushTree(); + NotionUtils.defaultNotion("配置[" + configName.getValue() + "]读取成功"); + } catch (JsonProcessingException e) { + NotionUtils.defaultNotion("配置存在问题"); + log.error(savaRepositoryById.get().getJson() + e); + } + } else NotionUtils.defaultNotion("没有找到配置"); + } + }); + + saveConfig.addClickListener( + buttonClickEvent -> { + if (StringUtils.isEmpty(configName.getValue())) NotionUtils.defaultNotion("配置名为空!!!无法存储"); + else { + try { + final String json = SugarJsonNodeSerialization.write(rootNode); + ConfigSave configSave = new ConfigSave(); + configSave.setId(configName.getValue()); + configSave.setJson(json); + configSavaRepository.save(configSave); + NotionUtils.defaultNotion("配置[" + configName.getValue() + "]存储成功"); + } catch (JsonProcessingException e) { + log.error(e.toString()); + } + } + }); + + delConfig.addClickListener( + buttonClickEvent -> { + if (StringUtils.isEmpty(configName.getValue())) NotionUtils.defaultNotion("配置名为空!!!无法删除"); + else { + configSavaRepository.deleteById(configName.getValue()); + NotionUtils.defaultNotion("配置[" + configName.getValue() + "]删除成功"); + } + }); + + randomType.addBlurListener( + event -> { + AbstractRandomService randomService = randomServiceMap.get(randomType.getValue()); + if (randomService == null) return; + randomInfo.setHelperText(randomService.helpText()); + }); + + next.addClickListener( + new ComponentEventListener>() { + @Override + public void onComponentEvent(ClickEvent