Browse Source

重写参数输入控件

master
richie 5 years ago
parent
commit
5753ef07eb
  1. 195
      src/main/java/com/fanruan/api/design/work/ParameterInputPane.java

195
src/main/java/com/fanruan/api/design/work/ParameterInputPane.java

@ -1,16 +1,203 @@
package com.fanruan.api.design.work; 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 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<ValueEditorPane, String> editorNameMap;
private boolean allowBlank = true;
/**
* 构造一个携带参数数组的参数值输入控件
*
* @param parameters 参数数组
*/
public ParameterInputPane(ParameterProvider[] parameters) { public ParameterInputPane(ParameterProvider[] parameters) {
super(parameters); this.initComponents(parameters);
} }
/**
* 构造一个携带参数数组的参数值输入控件
*
* @param parameters 参数数组
* @param allowBlank 允许参数值为空
*/
public ParameterInputPane(ParameterProvider[] parameters, boolean 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<ValueEditorPane, String>();
java.util.List<String> nameAddedList = new java.util.ArrayList<String>();
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<String, Object> update() {
java.util.Map<String, Object> nameValueMap = new java.util.HashMap<String, Object>();
for (Map.Entry<ValueEditorPane, String> 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<ValueEditorPane, String> 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<UILabel> labelList = new ArrayList<UILabel>();
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);
}
}
} }
} }
Loading…
Cancel
Save