From 5753ef07ebaaf3f15533898316c7feb6b135743c Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 31 Oct 2019 19:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E5=8F=82=E6=95=B0=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/design/work/ParameterInputPane.java | 195 +++++++++++++++++- 1 file changed, 191 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fanruan/api/design/work/ParameterInputPane.java b/src/main/java/com/fanruan/api/design/work/ParameterInputPane.java index c96b72b..813a406 100644 --- a/src/main/java/com/fanruan/api/design/work/ParameterInputPane.java +++ b/src/main/java/com/fanruan/api/design/work/ParameterInputPane.java @@ -1,16 +1,203 @@ package com.fanruan.api.design.work; +import com.fanruan.api.design.DesignKit; +import com.fanruan.api.design.ui.component.UILabel; +import com.fanruan.api.util.GeneralKit; +import com.fanruan.api.util.StringKit; +import com.fr.base.BaseFormula; +import com.fr.base.StoreProcedureParameter; +import com.fr.data.impl.storeproc.StoreProcedureConstants; +import com.fr.design.dialog.BasicPane; +import com.fr.design.editor.ValueEditorPane; +import com.fr.design.editor.ValueEditorPaneFactory; +import com.fr.design.editor.editor.BooleanEditor; +import com.fr.design.editor.editor.DateEditor; +import com.fr.design.editor.editor.DoubleEditor; +import com.fr.design.editor.editor.Editor; +import com.fr.design.editor.editor.FloatEditor; +import com.fr.design.editor.editor.IntegerEditor; +import com.fr.design.editor.editor.TextEditor; import com.fr.stable.ParameterProvider; +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + /** + * @author richie + * @version 10.0 + * Created by richie on 2019/9/24 * 用于输入参数的容器 */ -public class ParameterInputPane extends com.fr.design.parameter.ParameterInputPane { +public class ParameterInputPane extends BasicPane { + + private java.util.Map editorNameMap; + + private boolean allowBlank = true; + + /** + * 构造一个携带参数数组的参数值输入控件 + * + * @param parameters 参数数组 + */ public ParameterInputPane(ParameterProvider[] parameters) { - super(parameters); + this.initComponents(parameters); } + /** + * 构造一个携带参数数组的参数值输入控件 + * + * @param parameters 参数数组 + * @param allowBlank 允许参数值为空 + */ public ParameterInputPane(ParameterProvider[] parameters, boolean allowBlank) { - super(parameters, allowBlank); + this.allowBlank = allowBlank; + this.initComponents(parameters); + } + + private void initComponents(ParameterProvider[] parameters) { + this.setLayout(new BorderLayout(0, 4)); + JPanel contentPane = new JPanel(); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + this.add(new JScrollPane(contentPane), BorderLayout.CENTER); + contentPane.setBorder(BorderFactory.createTitledBorder(DesignKit.i18nText("Fine-Design_Basic_Parameters") + ":")); + + ParameterInputPane.FlowTableLayoutHelper flowTableLayoutHelper = new ParameterInputPane.FlowTableLayoutHelper(); + + editorNameMap = new java.util.HashMap(); + + java.util.List nameAddedList = new java.util.ArrayList(); + if (parameters != null && parameters.length > 0) { + for (int i = 0; i < parameters.length; i++) { + ParameterProvider parameter = parameters[i]; + if (nameAddedList.contains(parameter.getName())) { + continue; + } + if (parameter instanceof StoreProcedureParameter + && ((StoreProcedureParameter) parameter).getSchema() == StoreProcedureConstants.OUT) { + continue; + } + final Object pv = parameter.getValue(); + Editor[] editors = makeEditorByValue(pv); + + final ValueEditorPane textF = ValueEditorPaneFactory.createValueEditorPane(editors); + textF.populate(pv); + JPanel editPane = new JPanel(new BorderLayout()); + editPane.add(textF, BorderLayout.CENTER); + editPane.setPreferredSize(new Dimension(180, editPane.getPreferredSize().height)); + + String parameterDisplayName = parameter.getName(); + if (StringKit.isNotBlank(parameter.getName())) { + parameterDisplayName = parameter.getName(); + } + contentPane.add(flowTableLayoutHelper.createLabelFlowPane(parameterDisplayName + ":", editPane)); + + this.editorNameMap.put(textF, parameter.getName()); + nameAddedList.add(parameter.getName()); + } + } + + flowTableLayoutHelper.adjustLabelWidth(); + } + + private Editor[] makeEditorByValue(Object pv) { + Editor[] editors = {null}; + if (pv instanceof Integer) { + editors[0] = new IntegerEditor(); + } else if (pv instanceof Double) { + editors[0] = new DoubleEditor(); + } else if (pv instanceof Float) { + editors[0] = new FloatEditor(); + } else if (pv instanceof Date) { + editors[0] = new DateEditor(true, DesignKit.i18nText("Fine-Design_Basic_Date")); + } else if (pv instanceof Boolean) { + editors[0] = new BooleanEditor(); + } else if (pv instanceof BaseFormula) { + editors = ValueEditorPaneFactory.basicEditors(); + } else { + editors[0] = new TextEditor(); + } + return editors; + } + + @Override + protected String title4PopupWindow() { + return DesignKit.i18nText("Fine-Design_Basic_Parameters"); + } + + /** + * 返回所有的参数,包括了新输入的参数值 + * + * @return 参数组成的集合 + */ + public Map update() { + java.util.Map nameValueMap = new java.util.HashMap(); + for (Map.Entry entry : this.editorNameMap.entrySet()) { + ValueEditorPane editor = entry.getKey(); + String parameterName = entry.getValue(); + Object editorStringValue = editor.update(); + nameValueMap.put(parameterName, editorStringValue); + } + return nameValueMap; + } + + public void checkValid() throws Exception { + if (!allowBlank) { + boolean valid = true; + StringBuilder error = new StringBuilder(StringKit.EMPTY); + for (Map.Entry entry : this.editorNameMap.entrySet()) { + ValueEditorPane editor = entry.getKey(); + String parameterName = entry.getValue(); + Object editorStringValue = editor.update(); + if (editorStringValue == null || StringKit.isEmpty(GeneralKit.objectToString(editorStringValue))) { + valid = false; + error.append(parameterName).append(DesignKit.i18nText("Fine-Design_Basic_Not_Null_Des")).append("\n"); + } + } + if (!valid) { + throw new Exception(error.toString()); + } + } + } + + private static class FlowTableLayoutHelper { + private List labelList = new ArrayList(); + + private FlowTableLayoutHelper() { + + } + + private JPanel createLabelFlowPane(String text, JComponent comp) { + JPanel centerPane = new JPanel(); + centerPane.setLayout(new FlowLayout(FlowLayout.LEFT)); + + UILabel textLabel = new UILabel(text); + centerPane.add(textLabel); + textLabel.setHorizontalAlignment(SwingConstants.LEFT); + + this.labelList.add(textLabel); + centerPane.add(comp); + + return centerPane; + } + + private void adjustLabelWidth() { + int maxWidth = 0; + + for (UILabel uiLabel : labelList) { + maxWidth = Math.max(maxWidth, uiLabel.getPreferredSize().width); + } + + for (UILabel label : labelList) { + Dimension labelDim = new Dimension(maxWidth, label.getPreferredSize().height); + + label.setPreferredSize(labelDim); + label.setSize(labelDim); + label.setMinimumSize(labelDim); + } + } } -} +} \ No newline at end of file