You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
14 KiB

2 years ago
package com.fr.plugin.pielinecomb.comp;
import com.fr.base.GraphHelper;
import com.fr.data.core.FormatField;
import com.fr.design.beans.BasicBeanPane;
import com.fr.design.border.UIRoundedBorder;
import com.fr.design.constants.UIConstants;
import com.fr.design.event.GlobalNameListener;
import com.fr.design.event.UIObserverListener;
import com.fr.design.gui.icheckbox.UICheckBox;
import com.fr.design.gui.icombobox.TextFontComboBox;
import com.fr.design.gui.icombobox.UIComboBox;
import com.fr.design.gui.icombobox.UIComboBoxRenderer;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.style.FRFontPane;
import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.TableLayoutHelper;
import com.fr.general.ComparatorUtils;
import com.fr.stable.StringUtils;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.Format;
/**
* @author duan.jingliang
* @date 2022/10/21
*/
public class CustomFormatPane extends BasicBeanPane<TextFormat> {
private static final int LABEL_X = 4;
private static final int LABEL_Y = 18;
private static final int LABEL_DELTA_WIDTH = 8;
private static final int LABEL_HEIGHT = 15;
private static final int CURRENCY_FLAG_POINT = 6;
private static final Border LEFT_BORDER = BorderFactory.createEmptyBorder(0, 30, 0, 0);
private static final Integer[] TYPES = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7};
private static final Integer[] DATE_TYPES = new Integer[]{0, 5, 6};
private Format format;
private UIComboBox typeComboBox;
private TextFontComboBox textField;
private UILabel sampleLabel;
private JPanel contentPane;
private JPanel txtCenterPane;
private JPanel centerPane;
private JPanel optionPane;
private UICheckBox roundingBox;
private JPanel formatFontPane;
private FRFontPane frFontPane;
private boolean isRightFormat;
private boolean isDate = false;
private GlobalNameListener globalNameListener = null;
ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent var1) {
if (var1.getStateChange() == 1) {
int var2 = CustomFormatPane.this.getFormatContents();
String[] var3 = FormatField.getInstance().getFormatArray(var2, false);
CardLayout var4 = (CardLayout)CustomFormatPane.this.centerPane.getLayout();
if (CustomFormatPane.this.isTextOrNull()) {
CustomFormatPane.this.centerPane.setPreferredSize(new Dimension(0, 0));
var4.show(CustomFormatPane.this.centerPane, "hide");
} else {
CustomFormatPane.this.textField.removeAllItems();
CustomFormatPane.this.textField.setItemArray(var3);
CustomFormatPane.this.textField.setSelectedIndex(0);
CustomFormatPane.this.centerPane.setPreferredSize(new Dimension(270, 65));
var4.show(CustomFormatPane.this.centerPane, "show");
}
CardLayout var5 = (CardLayout)CustomFormatPane.this.optionPane.getLayout();
if (CustomFormatPane.this.getFormatContents() == 3) {
CustomFormatPane.this.optionPane.setPreferredSize(new Dimension(100, 20));
var5.show(CustomFormatPane.this.optionPane, "show");
} else {
CustomFormatPane.this.optionPane.setPreferredSize(new Dimension(0, 0));
var5.show(CustomFormatPane.this.optionPane, "hide");
CustomFormatPane.this.roundingBox.setSelected(false);
}
}
}
};
ItemListener textFieldItemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent var1) {
if (var1.getStateChange() == 1) {
CustomFormatPane.this.refreshPreviewLabel();
}
}
};
public CustomFormatPane() {
this.initComponents(TYPES);
}
protected void initComponents(Integer[] var1) {
this.setLayout(new BorderLayout(0, 4));
this.initSampleLabel();
this.contentPane = new JPanel(new BorderLayout(0, 4)) {
@Override
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, 65);
}
};
this.typeComboBox = new UIComboBox(var1);
UIComboBoxRenderer var2 = this.createComBoxRender();
this.typeComboBox.setRenderer(var2);
this.typeComboBox.addItemListener(this.itemListener);
this.typeComboBox.setGlobalName("typeComboBox");
this.contentPane.add(this.sampleLabel, "North");
this.txtCenterPane = new JPanel(new BorderLayout());
this.textField = new TextFontComboBox();
this.textField.addItemListener(this.textFieldItemListener);
this.textField.setEditable(true);
this.textField.setGlobalName("textField");
this.txtCenterPane.add(this.textField, "North");
this.contentPane.add(this.txtCenterPane, "Center");
this.centerPane = new JPanel(new CardLayout());
this.centerPane.add(new JPanel(), "hide");
this.centerPane.setPreferredSize(new Dimension(0, 0));
this.centerPane.add(this.contentPane, "show");
this.frFontPane = new FRFontPane();
UILabel var3 = new UILabel(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Form_FR_Font"), 2);
JPanel var4 = new JPanel(new BorderLayout());
var4.add(var3, "North");
this.typeComboBox.setPreferredSize(new Dimension(155, 20));
JPanel var5 = new JPanel(new BorderLayout());
var5.add(this.typeComboBox, "Center");
var5.setBorder(LEFT_BORDER);
this.frFontPane.setBorder(LEFT_BORDER);
JPanel var6 = new JPanel(new BorderLayout());
var6.add(new UILabel(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Base_Option"), 2), "West");
this.roundingBox = new UICheckBox(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Base_Option_Half_Up"));
this.roundingBox.setBorder(BorderFactory.createEmptyBorder(0, 40, 0, 0));
this.roundingBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent var1) {
}
});
this.roundingBox.setGlobalName("roundingBox");
var6.add(this.roundingBox, "Center");
this.optionPane = new JPanel(new CardLayout());
this.optionPane.add(new JPanel(), "hide");
this.optionPane.setPreferredSize(new Dimension(0, 0));
this.optionPane.add(var6, "show");
Component[][] var7 = this.getComponent(var4, this.centerPane, var5);
this.add(this.createContentPane(var7), "Center");
}
protected JPanel createContentPane(Component[][] var1) {
double var2 = -1.0D;
double var4 = -2.0D;
double[] var6 = new double[]{var4, var4, var4, var4, var4};
double[] var7 = new double[]{var4, var2};
int[][] var8 = new int[][]{{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}};
return TableLayoutHelper.createGapTableLayoutPane(var1, var6, var7, var8, 10.0D, 6.0D);
}
protected Component[][] getComponent(JPanel var1, JPanel var2, JPanel var3) {
return new Component[][]{{null, null}, {new UILabel(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Base_Format"), 2), var3}, {var2, null}, {this.optionPane, null}, {var1, this.frFontPane}};
}
protected UIComboBoxRenderer createComBoxRender() {
return new UIComboBoxRenderer() {
@Override
public Component getListCellRendererComponent(JList var1, Object var2, int var3, boolean var4, boolean var5) {
JLabel var6 = (JLabel)super.getListCellRendererComponent(var1, var2, var3, var4, var5);
if (var2 instanceof Integer) {
var6.setText(" " + FormatField.getInstance().getName((Integer)var2));
}
return var6;
}
};
}
private void initSampleLabel() {
UIRoundedBorder var1 = new UIRoundedBorder(UIConstants.LINE_COLOR, 1, 4);
String var2 = Toolkit.i18nText("Fine-Design_Report_Base_StyleFormat_Sample");
TitledBorder var3 = BorderFactory.createTitledBorder(var1, var2, 1, 0, (Font)null, UIConstants.LINE_COLOR);
this.sampleLabel = new UILabel(FormatField.getInstance().getFormatValue()) {
@Override
public void paint(Graphics var1) {
super.paint(var1);
int var2 = this.getWidth();
Color var3 = var1.getColor();
var1.setColor(this.getBackground());
var1.fillRect(4, 18, var2 - 8, 15);
var1.setColor(UIConstants.LINE_COLOR);
FontMetrics var4 = var1.getFontMetrics();
int var5 = var4.stringWidth(this.getText());
GraphHelper.drawString(var1, this.getText(), (double)((var2 - var5) / 2), 26.0D);
var1.setColor(var3);
}
};
this.sampleLabel.setHorizontalAlignment(0);
this.sampleLabel.setBorder(var3);
}
@Override
public Dimension getPreferredSize() {
return this.typeComboBox.getSelectedIndex() == 0 ? this.typeComboBox.getPreferredSize() : super.getPreferredSize();
}
@Override
public void populateBean(TextFormat textFormat) {
this.typeComboBox.setSelectedItem(textFormat.getType());
this.textField.setSelectedItem(textFormat.getFormatStr());
}
@Override
public TextFormat updateBean() {
TextFormat textFormat = new TextFormat();
textFormat.setFormatStr(String.valueOf(this.textField.getSelectedItem()));
textFormat.setType((Integer)this.typeComboBox.getSelectedItem());
return textFormat;
}
@Override
protected String title4PopupWindow() {
return null;
}
private boolean isCurrencyFormatStyle(String var1) {
if (var1.length() == 0) {
return false;
} else if (var1.charAt(0) != 164 && var1.charAt(0) != '$') {
return var1.length() > 6 && var1.startsWith("#,##0;");
} else {
return true;
}
}
public static int isArrayContainPattern(String[] var0, String var1) {
for(int var2 = 0; var2 < var0.length; ++var2) {
if (ComparatorUtils.equals(var0[var2], var1)) {
return var2;
}
}
return -1;
}
private void setPatternComboBoxAndList(int var1, String var2) {
this.typeComboBox.setSelectedItem(var1);
this.textField.setSelectedItem(var2);
}
private boolean isTimeType(String var1) {
return var1.matches(".*[Hhmsa].*");
}
private int getFormatContents() {
return (Integer)this.typeComboBox.getSelectedItem();
}
private void refreshPreviewLabel() {
this.sampleLabel.setText(FormatField.getInstance().getFormatValue());
this.sampleLabel.setForeground(UIManager.getColor("Label.foreground"));
try {
this.isRightFormat = true;
if (StringUtils.isEmpty(String.valueOf(this.textField.getSelectedItem()))) {
return;
}
this.sampleLabel.setText(FormatField.getInstance().getFormatValue(this.getFormatContents(), String.valueOf(this.textField.getSelectedItem())));
} catch (Exception var2) {
this.sampleLabel.setForeground(Color.red);
this.sampleLabel.setText(var2.getMessage());
this.isRightFormat = false;
}
}
private boolean isTextOrNull() {
int var1 = this.getFormatContents();
return var1 == 7 || var1 == 0;
}
public void justUsePercentFormat() {
this.typeComboBox.setEnabled(false);
this.typeComboBox.setSelectedItem(3);
}
public void setForDataSheet() {
Integer[] var1 = new Integer[]{0, 1, 2, 3, 4};
this.typeComboBox = new UIComboBox(var1);
UIComboBoxRenderer var2 = new UIComboBoxRenderer() {
@Override
public Component getListCellRendererComponent(JList var1, Object var2, int var3, boolean var4, boolean var5) {
JLabel var6 = (JLabel)super.getListCellRendererComponent(var1, var2, var3, var4, var5);
if (var2 instanceof Integer) {
var6.setText(" " + FormatField.getInstance().getName((Integer)var2));
}
return var6;
}
};
this.typeComboBox.setRenderer(var2);
this.typeComboBox.addItemListener(this.itemListener);
this.setTypeComboBoxPane(this.typeComboBox);
}
protected void setTypeComboBoxPane(UIComboBox var1) {
this.add(var1, "North");
}
public void setComboBoxModel(boolean var1) {
if (this.isDate != var1) {
this.isDate = var1;
this.typeComboBox.setSelectedIndex(0);
int var2;
if (var1) {
for(var2 = 0; var2 < DATE_TYPES.length; ++var2) {
this.typeComboBox.addItem(DATE_TYPES[var2]);
}
for(var2 = 0; var2 < TYPES.length; ++var2) {
this.typeComboBox.removeItemAt(1);
}
} else {
for(var2 = 0; var2 < TYPES.length; ++var2) {
this.typeComboBox.addItem(TYPES[var2]);
}
for(var2 = 0; var2 < DATE_TYPES.length; ++var2) {
this.typeComboBox.removeItemAt(1);
}
}
}
}
public void registerNameListener(GlobalNameListener var1) {
this.globalNameListener = var1;
}
public void registerChangeListener(UIObserverListener var1) {
this.typeComboBox.registerChangeListener(var1);
this.textField.registerChangeListener(var1);
}
public boolean shouldResponseNameListener() {
return false;
}
public void setGlobalName(String var1) {
}
}