PageRenderTime 76ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src_configDesign/org/hxzon/configdesigner/zk/CfgValueViewer2.java

https://gitlab.com/BGCX261/zk-full-demo-git
Java | 521 lines | 452 code | 51 blank | 18 comment | 63 complexity | d666aa238f9f17627aeecefb86acdf97 MD5 | raw file
  1. package org.hxzon.configdesigner.zk;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.hxzon.configdesigner.core.CfgInfo;
  5. import org.hxzon.configdesigner.core.CfgParser;
  6. import org.hxzon.configdesigner.core.CfgValue;
  7. import org.zkoss.zk.ui.Component;
  8. import org.zkoss.zk.ui.event.EventListener;
  9. import org.zkoss.zk.ui.event.Events;
  10. import org.zkoss.zk.ui.event.MouseEvent;
  11. import org.zkoss.zul.Button;
  12. import org.zkoss.zul.Hlayout;
  13. import org.zkoss.zul.Label;
  14. import org.zkoss.zul.Space;
  15. import org.zkoss.zul.Textbox;
  16. import org.zkoss.zul.Vlayout;
  17. import org.zkoss.zul.Window;
  18. import com.alibaba.fastjson.JSON;
  19. public class CfgValueViewer2 implements CfgValueHolder {
  20. private CfgValue cfgValue;
  21. //
  22. private boolean embed;
  23. private Component view;
  24. private Component viewParent;
  25. //
  26. private Component mainPanel;
  27. private Component buttonPanel;
  28. private Button addBtn;
  29. private Button saveBtn;
  30. private Component addPartDialog;
  31. private List<CfgValueHolder> valueHolders;
  32. //
  33. public CfgValueViewer2(CfgValue cfgValue, Component viewParent) {
  34. this(cfgValue, viewParent, false);
  35. }
  36. private CfgValueViewer2(CfgValue cfgValue, Component viewParent, boolean embed) {
  37. this.cfgValue = cfgValue;
  38. this.viewParent = viewParent;
  39. this.embed = embed;
  40. createView();
  41. //
  42. if (!embed) {
  43. Component rootViewOrig = (Component) viewParent.getAttribute("rootView");
  44. if (rootViewOrig != null) {
  45. viewParent.removeChild(rootViewOrig);
  46. }
  47. viewParent.appendChild(view);
  48. viewParent.setAttribute("rootView", view);
  49. viewParent.setAttribute("rootValue", cfgValue);
  50. }
  51. }
  52. private void createView() {
  53. CfgInfo info = cfgValue.getCfgInfo();
  54. if (info.isTextArea()) {
  55. view = createTextareaPane(cfgValue);
  56. return;
  57. }
  58. if (info.getType().isSimple()) {
  59. view = createSimpleValuePane(cfgValue);
  60. return;
  61. }
  62. //
  63. view = createComboPane();
  64. }
  65. private Component createComboPane() {
  66. valueHolders = new ArrayList<CfgValueHolder>();
  67. //
  68. Window window = new Window();
  69. Vlayout pane = new Vlayout();
  70. Hlayout titleLayout = new Hlayout();
  71. titleLayout.appendChild(embed ? createLabel(cfgValue) : createTitle(cfgValue));
  72. if (cfgValue.isDeletable()) {
  73. Button delBtn = createDeleteBtn(cfgValue, window);
  74. titleLayout.appendChild(delBtn);
  75. }
  76. addBtn = createAddBtn();
  77. titleLayout.appendChild(addBtn);
  78. if (cfgValue.isElement()) {
  79. titleLayout.appendChild(createCopyElementBtn(cfgValue, window));
  80. }
  81. if (!embed && cfgValue.getParent() != null) {
  82. Button returnBtn = createReturnBtn();
  83. titleLayout.appendChild(returnBtn);
  84. }
  85. titleLayout.appendChild(createViewDataBtn(cfgValue));
  86. titleLayout.setStyle("background-color:#eee");
  87. pane.appendChild(titleLayout);
  88. pane.appendChild(new Space());
  89. if (cfgValue.isMapElement()) {
  90. pane.appendChild(createKeyPane(cfgValue));
  91. }
  92. Component body = createBody(cfgValue);
  93. pane.appendChild(body);
  94. //
  95. window.appendChild(pane);
  96. window.setBorder(true);
  97. return window;
  98. }
  99. private Component createBody(CfgValue cfgValue) {
  100. if (cfgValue.getCfgInfo().getType().isStruct()) {
  101. return createBody_struct();
  102. } else {
  103. return createBody_listOrMap();
  104. }
  105. }
  106. private Component createBody_struct() {
  107. buttonPanel = new Vlayout();
  108. //
  109. mainPanel = new Vlayout();
  110. ((Vlayout) mainPanel).setSpacing("0.5em");
  111. //
  112. CfgInfo cfgInfo = cfgValue.getCfgInfo();
  113. for (CfgInfo cCfgInfo : cfgInfo.getPartsInfo()) {
  114. CfgValue cCfgValue = cfgValue.getValue(cCfgInfo.getId());
  115. if (cCfgValue == null) {
  116. Button btn = createAddPartBtn(cCfgInfo);
  117. buttonPanel.appendChild(btn);
  118. } else {
  119. Component partPane = createPane(cCfgValue);
  120. mainPanel.appendChild(partPane);
  121. }
  122. }
  123. createSaveBtn();
  124. addBtn.setVisible(!buttonPanel.getChildren().isEmpty());
  125. return mainPanel;
  126. }
  127. private Component createBody_listOrMap() {
  128. mainPanel = new Vlayout();
  129. for (CfgValue cCfgValue : cfgValue.getValues()) {
  130. Component elePane = createPane(cCfgValue);
  131. mainPanel.appendChild(elePane);
  132. }
  133. createSaveBtn();
  134. return mainPanel;
  135. }
  136. private void createSaveBtn() {
  137. if (!embed) {
  138. saveBtn = new Button("保存");
  139. saveBtn.addEventListener(Events.ON_CLICK, new EventListener<MouseEvent>() {
  140. @Override
  141. public void onEvent(MouseEvent event) throws Exception {
  142. saveValue();
  143. }
  144. });
  145. mainPanel.appendChild(saveBtn);
  146. mainPanel.setAttribute("saveBtn", saveBtn);
  147. }
  148. }
  149. private Component createPane(CfgValue cfgValue) {
  150. CfgInfo info = cfgValue.getCfgInfo();
  151. if (info.isTextArea()) {
  152. return createTextareaPane(cfgValue);
  153. }
  154. if (info.getType().isSimple()) {
  155. return createSimpleValuePane(cfgValue);
  156. }
  157. if (!info.isEmbed()) {
  158. return createLinkPane(cfgValue);
  159. }
  160. CfgValueViewer2 viewer = new CfgValueViewer2(cfgValue, viewParent, true);
  161. valueHolders.add(viewer);
  162. return viewer.getView();
  163. }
  164. private Component createTextareaPane(CfgValue cfgValue) {
  165. CfgValueInputCompHolder inputCompHolder = new CfgValueInputCompHolder(cfgValue);
  166. valueHolders.add(inputCompHolder);
  167. Vlayout pane = new Vlayout();
  168. Hlayout hlayout = new Hlayout();
  169. hlayout.appendChild(createLabel(cfgValue));
  170. if (cfgValue.isDeletable()) {
  171. hlayout.appendChild(createDeleteBtn(cfgValue, pane));
  172. }
  173. if (cfgValue.isElement()) {
  174. hlayout.appendChild(createCopyElementBtn(cfgValue, pane));
  175. }
  176. pane.appendChild(hlayout);
  177. if (cfgValue.isMapElement()) {
  178. pane.appendChild(createKeyPane(cfgValue));
  179. }
  180. pane.appendChild(inputCompHolder.getInputComponent());
  181. return pane;
  182. }
  183. private Component createSimpleValuePane(CfgValue cfgValue) {
  184. CfgValueInputCompHolder inputCompHolder = new CfgValueInputCompHolder(cfgValue);
  185. valueHolders.add(inputCompHolder);
  186. Hlayout pane = new Hlayout();
  187. if (cfgValue.isMapElement()) {
  188. pane.appendChild(createKeyPane(cfgValue));
  189. } else {
  190. pane.appendChild(createLabel(cfgValue));
  191. }
  192. pane.appendChild(inputCompHolder.getInputComponent());
  193. if (cfgValue.isDeletable()) {
  194. pane.appendChild(createDeleteBtn(cfgValue, pane));
  195. }
  196. if (cfgValue.isElement()) {
  197. pane.appendChild(createCopyElementBtn(cfgValue, pane));
  198. }
  199. return pane;
  200. }
  201. private Component createLinkPane(CfgValue cfgValue) {
  202. Hlayout pane = new Hlayout();
  203. //pane.appendChild(createLabel(cfgValue));
  204. pane.appendChild(createEnterBtn(cfgValue));
  205. if (cfgValue.isDeletable()) {
  206. pane.appendChild(createDeleteBtn(cfgValue, pane));
  207. }
  208. pane.appendChild(createViewDataBtn(cfgValue));
  209. return pane;
  210. }
  211. private Component createKeyPane(CfgValue cfgValue) {
  212. CfgValueKeybox keyInputComp = new CfgValueKeybox(cfgValue);
  213. valueHolders.add(keyInputComp);
  214. Hlayout pane = new Hlayout();
  215. pane.appendChild(new Label("键:"));
  216. pane.appendChild(keyInputComp);
  217. return pane;
  218. }
  219. //==============
  220. @Override
  221. public void saveValue() {
  222. if (valueHolders != null) {
  223. for (CfgValueHolder c : valueHolders) {
  224. c.saveValue();
  225. }
  226. }
  227. }
  228. private void deleteValue(Component btn) {
  229. CfgValue deleteCfgValue = (CfgValue) btn.getAttribute("cfgValue");
  230. CfgValue rootValue = (CfgValue) viewParent.getAttribute("rootValue");
  231. if (deleteCfgValue == rootValue) {
  232. CfgValue parentCfgValue = deleteCfgValue.getParent();
  233. parentCfgValue.removeValue(deleteCfgValue);
  234. new CfgValueViewer2(parentCfgValue, viewParent);
  235. return;
  236. }
  237. CfgValue parent = deleteCfgValue.getParent();
  238. if (parent.getCfgInfo().getType().isStruct()) {
  239. buttonPanel.appendChild(createAddPartBtn(deleteCfgValue.getCfgInfo()));
  240. addBtn.setVisible(true);
  241. }
  242. parent.removeValue(deleteCfgValue);
  243. ((Component) btn.getAttribute("partPane")).setParent(null);
  244. }
  245. private void addPart(Component btn) {
  246. CfgInfo cfgInfo = (CfgInfo) btn.getAttribute("cfgInfo");
  247. CfgValue newPartValue = CfgParser.buildCfgValue(cfgInfo, null, 1, 1);
  248. btn.setParent(null);
  249. if (buttonPanel.getChildren().isEmpty()) {
  250. addBtn.setVisible(false);
  251. addPartDialog.setVisible(false);
  252. }
  253. addPartValue_(newPartValue);
  254. }
  255. private void addValue() {
  256. if (buttonPanel != null) {
  257. //复用buttonPanel,所以cfgInfo新增字段时,不会立即反映出来,需刷新页面
  258. //更新buttonPanel只解决新增“可选字段”的问题,新增“必填”字段,需要更新view面板
  259. //所以,让用户自己刷新页面,毕竟修改模式的情况不频繁
  260. //另一用户添加或删除了字段(或元素),目前也需要用户自己刷新页面
  261. addPartDialog = createDialog(buttonPanel, "添加字段");
  262. } else {
  263. CfgValue newEle = CfgParser.buildListElementCfgValue(cfgValue, 1);
  264. if (newEle.isMapElement()) {
  265. newEle.setKey("_new");
  266. }
  267. String idPrefix = newEle.getCfgInfo().getIdPrefix();
  268. String keyKey = newEle.getCfgInfo().getKeyKey();
  269. if (idPrefix != null && keyKey != null) {
  270. CfgValue keyValue = newEle.findCfgValue(keyKey);
  271. if (keyValue != null) {
  272. String nextId = idPrefix + CfgParser.nextEntityId(idPrefix);
  273. keyValue.setValue(nextId);
  274. newEle.setKey(nextId);
  275. }
  276. }
  277. addPartValue_(newEle);
  278. }
  279. }
  280. private void addPartValue_(CfgValue newPartValue) {
  281. cfgValue.addValue(newPartValue);
  282. if (!embed) {
  283. mainPanel.removeChild(saveBtn);
  284. }
  285. Component partPane = createPane(newPartValue);
  286. mainPanel.appendChild(partPane);
  287. if (!embed) {
  288. mainPanel.appendChild(saveBtn);
  289. }
  290. }
  291. private void copyValue(Component btn) {
  292. CfgValue origValue = (CfgValue) btn.getAttribute("cfgValue");
  293. CfgValue newValue = CfgParser.copy(origValue);
  294. if (origValue.isMapElement()) {
  295. newValue.setKey(newValue.getKey() + "_copy");
  296. }
  297. String idPrefix = newValue.getCfgInfo().getIdPrefix();
  298. String keyKey = newValue.getCfgInfo().getKeyKey();
  299. if (idPrefix != null && keyKey != null) {
  300. CfgValue keyValue = newValue.findCfgValue(keyKey);
  301. if (keyValue != null) {
  302. String nextId = idPrefix + CfgParser.nextEntityId(idPrefix);
  303. keyValue.setValue(nextId);
  304. newValue.setKey(nextId);
  305. }
  306. }
  307. origValue.getParent().addValue(newValue);
  308. //
  309. CfgValue rootValue = (CfgValue) viewParent.getAttribute("rootValue");
  310. if (origValue == rootValue) {
  311. new CfgValueViewer2(newValue, viewParent);
  312. return;
  313. }
  314. //
  315. Component elePane = createPane(newValue);
  316. Component origValuePane = (Component) btn.getAttribute("partPane");
  317. Component parentComp = origValuePane.getParent();
  318. Component saveBtn = (Component) parentComp.getAttribute("saveBtn");
  319. if (saveBtn != null) {
  320. parentComp.removeChild(saveBtn);
  321. }
  322. parentComp.appendChild(elePane);
  323. if (saveBtn != null) {
  324. parentComp.appendChild(saveBtn);
  325. }
  326. }
  327. private void returnParent() {
  328. CfgValue parent = cfgValue.getParent();
  329. new CfgValueViewer2(parent, viewParent);
  330. }
  331. private void enter(Component btn) {
  332. CfgValue cfgValue = (CfgValue) btn.getAttribute("cfgValue");
  333. new CfgValueViewer2(cfgValue, viewParent);
  334. }
  335. private void viewData(Component btn) {
  336. CfgValue cfgValue = (CfgValue) btn.getAttribute("cfgValue");
  337. Object json = CfgParser.toJson(cfgValue, true);
  338. String jsonStr = json == null ? "" : JSON.toJSONString(json, true);
  339. Textbox v = new Textbox();
  340. v.setMultiline(true);
  341. v.setReadonly(true);
  342. v.setWidth("100%");
  343. v.setHeight("100%");
  344. v.setValue(jsonStr);
  345. createDialog(v, "查看数据");
  346. }
  347. private final EventListener<MouseEvent> AddPartBtnEventListener = new EventListener<MouseEvent>() {
  348. @Override
  349. public void onEvent(MouseEvent event) throws Exception {
  350. addPart(event.getTarget());
  351. }
  352. };
  353. private final EventListener<MouseEvent> DeleteValueBtnEventListener = new EventListener<MouseEvent>() {
  354. @Override
  355. public void onEvent(MouseEvent event) throws Exception {
  356. deleteValue(event.getTarget());
  357. }
  358. };
  359. private final EventListener<MouseEvent> AddValueBtnEventListener = new EventListener<MouseEvent>() {
  360. @Override
  361. public void onEvent(MouseEvent event) throws Exception {
  362. addValue();
  363. }
  364. };
  365. private final EventListener<MouseEvent> CopyValueBtnEventListener = new EventListener<MouseEvent>() {
  366. @Override
  367. public void onEvent(MouseEvent event) throws Exception {
  368. copyValue(event.getTarget());
  369. }
  370. };
  371. private final EventListener<MouseEvent> ReturnBtnEventListener = new EventListener<MouseEvent>() {
  372. @Override
  373. public void onEvent(MouseEvent event) throws Exception {
  374. returnParent();
  375. }
  376. };
  377. private final EventListener<MouseEvent> EnterBtnEventListener = new EventListener<MouseEvent>() {
  378. @Override
  379. public void onEvent(MouseEvent event) throws Exception {
  380. enter(event.getTarget());
  381. }
  382. };
  383. private final EventListener<MouseEvent> ViewDataBtnEventListener = new EventListener<MouseEvent>() {
  384. @Override
  385. public void onEvent(MouseEvent event) throws Exception {
  386. viewData(event.getTarget());
  387. }
  388. };
  389. private Button createAddPartBtn(CfgInfo cfgInfo) {
  390. Button btn = new Button();
  391. btn.setAttribute("cfgInfo", cfgInfo);
  392. btn.setLabel(cfgInfo.getLabelOrId());
  393. btn.setImage("images/easyicon_add.png");
  394. btn.setTooltiptext("添加");
  395. btn.addEventListener(Events.ON_CLICK, AddPartBtnEventListener);
  396. return btn;
  397. }
  398. private Button createDeleteBtn(CfgValue cfgValue, Component partPane) {
  399. Button btn = new Button();
  400. btn.setAttribute("cfgValue", cfgValue);
  401. btn.setImage("images/easyicon_remove.png");
  402. btn.setTooltiptext("删除");
  403. btn.addEventListener(Events.ON_CLICK, DeleteValueBtnEventListener);
  404. btn.setAttribute("partPane", partPane);
  405. return btn;
  406. }
  407. private Button createAddBtn() {
  408. Button btn = new Button();
  409. btn.setImage("images/easyicon_add.png");
  410. btn.setTooltiptext("添加");
  411. btn.addEventListener(Events.ON_CLICK, AddValueBtnEventListener);
  412. return btn;
  413. }
  414. private Button createCopyElementBtn(CfgValue origValue, Component partPane) {
  415. Button btn = new Button();
  416. btn.setAttribute("cfgValue", origValue);
  417. btn.setImage("images/easyicon_copy.png");
  418. btn.setTooltiptext("复制");
  419. btn.addEventListener(Events.ON_CLICK, CopyValueBtnEventListener);
  420. btn.setAttribute("partPane", partPane);
  421. return btn;
  422. }
  423. private Button createReturnBtn() {
  424. Button btn = new Button();
  425. btn.setImage("images/easyicon_return.png");
  426. btn.setTooltiptext("返回");
  427. btn.addEventListener(Events.ON_CLICK, ReturnBtnEventListener);
  428. return btn;
  429. }
  430. private Button createEnterBtn(CfgValue cfgValue) {
  431. Button btn = new Button();
  432. btn.setAttribute("cfgValue", cfgValue);
  433. btn.setImage("images/easyicon_enter.png");
  434. btn.setLabel(cfgValue.getLabel());
  435. btn.setTooltiptext("进入");
  436. btn.addEventListener(Events.ON_CLICK, EnterBtnEventListener);
  437. return btn;
  438. }
  439. private Button createViewDataBtn(CfgValue cfgValue) {
  440. Button btn = new Button();
  441. btn.setAttribute("cfgValue", cfgValue);
  442. btn.setImage("images/easyicon_view.png");
  443. btn.setTooltiptext("查看数据");
  444. btn.addEventListener(Events.ON_CLICK, ViewDataBtnEventListener);
  445. return btn;
  446. }
  447. private Component createDialog(Component content, String title) {
  448. Window dialog = new Window();
  449. dialog.appendChild(content);
  450. dialog.setTitle(title);
  451. dialog.setClosable(true);
  452. dialog.setSizable(true);
  453. dialog.setWidth("50%");
  454. dialog.setHeight("50%");
  455. dialog.setBorder(true);
  456. dialog.setParent(view);
  457. dialog.setMode(Window.MODAL);
  458. return dialog;
  459. }
  460. private Label createLabel(CfgValue cfgValue) {
  461. return new Label(cfgValue.getLabel() + ":");
  462. }
  463. private Label createTitle(CfgValue cfgValue) {
  464. return new Label(cfgValue.getTitle());
  465. }
  466. //==============
  467. private Component getView() {
  468. return view;
  469. }
  470. }