forked from fanruan/finekit
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.
366 lines
16 KiB
366 lines
16 KiB
package com.fanruan.api.design.work.component.container; |
|
|
|
|
|
import com.fanruan.api.design.DesignKit; |
|
import com.fanruan.api.design.macro.UIConstants; |
|
import com.fanruan.api.design.ui.component.UIColorButton; |
|
import com.fanruan.api.design.ui.component.UIComboBox; |
|
import com.fanruan.api.design.ui.component.UIToggleButton; |
|
import com.fanruan.api.design.ui.layout.TableLayoutKit; |
|
import com.fanruan.api.design.ui.layout.macro.LayoutConstants; |
|
import com.fanruan.api.design.util.GUICoreKit; |
|
import com.fanruan.api.design.work.component.LineComboBox; |
|
import com.fanruan.api.macro.LineConstants; |
|
import com.fanruan.api.util.AssistKit; |
|
import com.fanruan.api.util.GeneralKit; |
|
import com.fanruan.api.util.IOKit; |
|
import com.fanruan.api.util.MathKit; |
|
import com.fr.base.FRContext; |
|
import com.fr.base.Style; |
|
import com.fr.design.event.GlobalNameListener; |
|
import com.fr.design.event.GlobalNameObserver; |
|
import com.fr.design.gui.style.AbstractBasicStylePane; |
|
import com.fr.general.DefaultValues; |
|
import com.fr.general.FRFont; |
|
|
|
import javax.swing.*; |
|
import javax.swing.event.ChangeEvent; |
|
import javax.swing.event.ChangeListener; |
|
import java.awt.*; |
|
import java.util.Vector; |
|
|
|
/** |
|
* @author richie |
|
* @version 10.0 |
|
* Created by richie on 2019/10/28 |
|
* 用于编辑字体的面板容器 |
|
*/ |
|
public class FontPane extends AbstractBasicStylePane implements GlobalNameObserver { |
|
|
|
public static final Integer[] FONT_SIZES = { |
|
6, 8, 9, 10, 11, 12, 13, 14, 16, |
|
18, 20, 22, 24, 26, 28, 30, 32, 34, |
|
36, 38, 40, 48, 64, 72, 128 |
|
}; |
|
private static final int MAX_FONT_SIZE = 100; |
|
private static final Dimension BUTTON_SIZE = new Dimension(20, 18); |
|
private static final Dimension UNDER_LINE_SIZE = new Dimension(87, 20); |
|
private static final Dimension HIDE_SIZE = new Dimension(0, 0); |
|
private final String[] fontSizeStyles = {DesignKit.i18nText("Fine-Design_Basic_FR_Font_Plain"), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Bold"), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Italic"), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Bolditalic")}; |
|
private JPanel buttonPane; |
|
private JPanel isSuperOrSubPane; |
|
private UIComboBox fontNameComboBox; |
|
private UIComboBox fontSizeStyleComboBox; |
|
private UIComboBox fontSizeComboBox; |
|
private UIToggleButton bold; |
|
private UIToggleButton italic; |
|
private UIToggleButton underline; |
|
private GlobalNameListener globalNameListener = null; |
|
|
|
private LineComboBox underlineCombo; |
|
private UIColorButton colorSelectPane; |
|
|
|
private UIToggleButton isStrikethroughCheckBox; |
|
private UIToggleButton isShadowCheckBox; |
|
private UIToggleButton superPane; |
|
private UIToggleButton subPane; |
|
private JPanel linePane; |
|
|
|
public FontPane() { |
|
this.initComponents(); |
|
} |
|
|
|
public static void main(String[] args) { |
|
JFrame jf = new JFrame("test"); |
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
JPanel content = (JPanel) jf.getContentPane(); |
|
content.setLayout(new BorderLayout()); |
|
content.add(new com.fr.design.gui.style.FRFontPane(), BorderLayout.CENTER); |
|
GUICoreKit.centerWindow(jf); |
|
jf.setSize(290, 400); |
|
jf.setVisible(true); |
|
} |
|
|
|
|
|
@Override |
|
protected String title4PopupWindow() { |
|
return DesignKit.i18nText("Fine-Design_Report_Sytle_FRFont"); |
|
} |
|
|
|
/** |
|
* Use font to populate pane. |
|
*/ |
|
public void populateBean(FRFont frFont) { |
|
fontNameComboBox.setSelectedItem(frFont.getFamily()); |
|
fontSizeStyleComboBox.setSelectedIndex(frFont.getStyle()); |
|
fontSizeComboBox.setSelectedItem(MathKit.round5(frFont.getSize2D())); |
|
bold.setSelected(frFont.isBold()); |
|
italic.setSelected(frFont.isItalic()); |
|
|
|
this.colorSelectPane.setColor(frFont.getForeground()); |
|
this.colorSelectPane.repaint(); |
|
|
|
CardLayout cly = (CardLayout) linePane.getLayout(); |
|
int line = frFont.getUnderline(); |
|
if (line == LineConstants.LINE_NONE) { |
|
underline.setSelected(false); |
|
cly.show(linePane, "none"); |
|
linePane.setPreferredSize(HIDE_SIZE); |
|
} else { |
|
underline.setSelected(true); |
|
cly.show(linePane, "combobox"); |
|
linePane.setPreferredSize(UNDER_LINE_SIZE); |
|
this.underlineCombo.setSelectedLineStyle(line); |
|
} |
|
// effects |
|
this.isStrikethroughCheckBox.setSelected(frFont.isStrikethrough()); |
|
this.isShadowCheckBox.setSelected(frFont.isShadow()); |
|
if (frFont.isSuperscript()) { |
|
this.superPane.setSelected(true); |
|
this.subPane.setSelected(false); |
|
} else if (frFont.isSubscript()) { |
|
this.superPane.setSelected(false); |
|
this.subPane.setSelected(true); |
|
} else { |
|
this.superPane.setSelected(false); |
|
this.subPane.setSelected(false); |
|
} |
|
} |
|
|
|
/** |
|
* Update pane to get new font. |
|
*/ |
|
public FRFont update(FRFont frFont) { |
|
|
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_Name"))) { |
|
frFont = frFont.applyName((String) fontNameComboBox.getSelectedItem()); |
|
} |
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Style"))) { |
|
frFont = frFont.applyStyle(fontSizeStyleComboBox.getSelectedIndex()); |
|
} |
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Form_FRFont_Size"))) { |
|
frFont = frFont.applySize(Float.parseFloat(fontSizeComboBox.getSelectedItem().toString())); |
|
} |
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Foreground"))) { |
|
frFont = frFont.applyForeground(this.colorSelectPane.getColor()); |
|
} |
|
|
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Underline"))) { |
|
|
|
int line = underline.isSelected() ? this.underlineCombo.getSelectedLineStyle() : LineConstants.LINE_NONE; |
|
frFont = frFont.applyUnderline(line); |
|
|
|
} |
|
|
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Form_FRFont_Line_Style"))) { |
|
frFont = frFont.applyUnderline(this.underlineCombo.getSelectedLineStyle()); |
|
} |
|
|
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Strikethrough"))) { |
|
frFont = frFont.applyStrikethrough(isStrikethroughCheckBox.isSelected()); |
|
} |
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Shadow"))) { |
|
frFont = frFont.applyShadow(isShadowCheckBox.isSelected()); |
|
} |
|
|
|
frFont = updateOthers(frFont); |
|
|
|
return frFont; |
|
} |
|
|
|
|
|
private FRFont updateOthers(FRFont frFont) { |
|
frFont = updateSubSuperscript(frFont); |
|
return frFont; |
|
} |
|
|
|
private FRFont updateSubSuperscript(FRFont frFont) { |
|
boolean isSuper = frFont.isSuperscript(); |
|
boolean isSub = frFont.isSubscript(); |
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Superscript"))) { |
|
//如果上标没有选中,点击则选中上标,并且下标一定是不选中状态 |
|
//如果上标选中,点击则取消选中上标,字体回复正常 |
|
if (superPane.isSelected() && !isSuper) { |
|
frFont = frFont.applySuperscript(true); |
|
frFont = frFont.applySubscript(false); |
|
this.subPane.setSelected(false); |
|
} else if (!superPane.isSelected() && isSuper) { |
|
frFont = frFont.applySuperscript(false); |
|
} |
|
} |
|
if (AssistKit.equals(globalNameListener.getGlobalName(), DesignKit.i18nText("Fine-Design_Basic_FR_Font_Subscript"))) { |
|
if (subPane.isSelected() && !isSub) { |
|
frFont = frFont.applySubscript(true); |
|
frFont = frFont.applySuperscript(false); |
|
this.superPane.setSelected(false); |
|
} else if (!subPane.isSelected() && isSub) { |
|
frFont = frFont.applySubscript(false); |
|
} |
|
} |
|
return frFont; |
|
} |
|
|
|
@Override |
|
public void populateBean(Style style) { |
|
this.populateBean(style.getFRFont()); |
|
} |
|
|
|
@Override |
|
public Style update(Style style) { |
|
FRFont frFont = style.getFRFont(); |
|
frFont = this.update(frFont); |
|
return style.deriveFRFont(frFont); |
|
} |
|
|
|
public static Vector<Integer> getFontSizes() { |
|
Vector<Integer> FONT_SIZES = new Vector<Integer>(); |
|
for (int i = 1; i < MAX_FONT_SIZE; i++) { |
|
FONT_SIZES.add(i); |
|
} |
|
return FONT_SIZES; |
|
} |
|
|
|
protected void initComponents() { |
|
fontSizeStyleComboBox = new UIComboBox<>(fontSizeStyles); |
|
fontNameComboBox = new UIComboBox<>(GeneralKit.getAvailableFontFamilyNames()); |
|
fontNameComboBox.setPreferredSize(new Dimension(144, 20)); |
|
fontSizeComboBox = new UIComboBox<>(getFontSizes()); |
|
fontSizeComboBox.setEditable(true); |
|
this.underlineCombo = new LineComboBox(UIConstants.BORDER_LINE_STYLE_ARRAY); |
|
colorSelectPane = new UIColorButton(); |
|
bold = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/bold.png")); |
|
italic = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/italic.png")); |
|
underline = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/underline.png")); |
|
bold.setPreferredSize(BUTTON_SIZE); |
|
italic.setPreferredSize(BUTTON_SIZE); |
|
underline.setPreferredSize(BUTTON_SIZE); |
|
isStrikethroughCheckBox = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/strikethrough.png")); |
|
isStrikethroughCheckBox.setPreferredSize(BUTTON_SIZE); |
|
isShadowCheckBox = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/shadow.png")); |
|
isShadowCheckBox.setPreferredSize(BUTTON_SIZE); |
|
superPane = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/sup.png")); |
|
superPane.setPreferredSize(BUTTON_SIZE); |
|
subPane = new UIToggleButton(IOKit.readIcon("/com/fr/design/images/m_format/cellstyle/sub.png")); |
|
subPane.setPreferredSize(BUTTON_SIZE); |
|
Component[] SuperOrSubComponents = new Component[]{ |
|
superPane, subPane |
|
}; |
|
isSuperOrSubPane = new JPanel(new BorderLayout()); |
|
isSuperOrSubPane.add(GUICoreKit.createFlowPane(SuperOrSubComponents, FlowLayout.LEFT, LayoutConstants.HGAP_SMALL)); |
|
Component[] components_font = new Component[]{ |
|
colorSelectPane, underline, isStrikethroughCheckBox, isShadowCheckBox |
|
}; |
|
buttonPane = new JPanel(new BorderLayout()); |
|
buttonPane.add(GUICoreKit.createFlowPane(components_font, FlowLayout.LEFT, LayoutConstants.HGAP_SMALL)); |
|
linePane = new JPanel(new CardLayout()); |
|
initAllNames(); |
|
setToolTips(); |
|
this.setLayout(new BorderLayout()); |
|
this.add(createPane(), BorderLayout.CENTER); |
|
DefaultValues defaultValues = FRContext.getDefaultValues(); |
|
populateBean(defaultValues.getFRFont()); |
|
} |
|
|
|
private void initAllNames() { |
|
fontSizeStyleComboBox.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Style")); |
|
fontNameComboBox.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_Name")); |
|
fontSizeComboBox.setGlobalName(DesignKit.i18nText("Fine-Design_Form_FRFont_Size")); |
|
colorSelectPane.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Foreground")); |
|
italic.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Italic")); |
|
bold.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Bold")); |
|
underline.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Underline")); |
|
underlineCombo.setGlobalName(DesignKit.i18nText("Fine-Design_Form_FRFont_Line_Style")); |
|
isStrikethroughCheckBox.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Strikethrough")); |
|
isShadowCheckBox.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Shadow")); |
|
superPane.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Superscript")); |
|
subPane.setGlobalName(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Subscript")); |
|
} |
|
|
|
private void setToolTips() { |
|
colorSelectPane.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Foreground")); |
|
italic.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Italic")); |
|
bold.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Bold")); |
|
underline.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Underline")); |
|
isStrikethroughCheckBox.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Strikethrough")); |
|
isShadowCheckBox.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Shadow")); |
|
superPane.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Superscript")); |
|
subPane.setToolTipText(DesignKit.i18nText("Fine-Design_Basic_FR_Font_Subscript")); |
|
} |
|
|
|
|
|
private JPanel createLinePane() { |
|
linePane.add(new JPanel(), "none"); |
|
JPanel gap = new JPanel(new GridLayout(0, 1)); |
|
gap.add(underlineCombo); |
|
linePane.add(gap, "combobox"); |
|
underline.addChangeListener(new ChangeListener() { |
|
|
|
@Override |
|
public void stateChanged(ChangeEvent e) { |
|
CardLayout cly = (CardLayout) linePane.getLayout(); |
|
cly.show(linePane, underline.isSelected() ? "combobox" : "none"); |
|
if (underline.isSelected()) { |
|
linePane.setPreferredSize(UNDER_LINE_SIZE); |
|
} else { |
|
linePane.setPreferredSize(HIDE_SIZE); |
|
} |
|
} |
|
}); |
|
|
|
return linePane; |
|
} |
|
|
|
private JPanel createLeftPane() { |
|
double p = TableLayoutKit.PREFERRED; |
|
double f = TableLayoutKit.FILL; |
|
double[] columnSize = {p}; |
|
double[] rowSize = {p, p, p}; |
|
int[][] rowCount = {{1, 1}, {1, 1}, {1, 1}}; |
|
Component[][] components = new Component[][]{ |
|
new Component[]{fontSizeStyleComboBox}, |
|
new Component[]{buttonPane}, |
|
new Component[]{createLinePane()} |
|
}; |
|
return TableLayoutKit.createGapTableLayoutPane(components, rowSize, columnSize, rowCount, LayoutConstants.VGAP_MEDIUM, LayoutConstants.VGAP_MEDIUM); |
|
} |
|
|
|
protected JPanel createRightPane() { |
|
double p = TableLayoutKit.PREFERRED; |
|
double f = TableLayoutKit.FILL; |
|
double[] columnSize = {f}; |
|
double[] rowSize = {p, p}; |
|
int[][] rowCount = {{1, 1}, {1, 1}}; |
|
Component[][] components = new Component[][]{ |
|
new Component[]{fontSizeComboBox}, |
|
new Component[]{isSuperOrSubPane} |
|
}; |
|
return TableLayoutKit.createGapTableLayoutPane(components, rowSize, columnSize, rowCount, LayoutConstants.VGAP_MEDIUM, LayoutConstants.VGAP_MEDIUM); |
|
} |
|
|
|
private JPanel createPane() { |
|
JPanel createPane = new JPanel(new BorderLayout()); |
|
createPane.add(fontNameComboBox, BorderLayout.NORTH); |
|
JPanel jPanel = TableLayoutKit.createGapTableLayoutPane( |
|
new Component[][]{new Component[]{createLeftPane(), createRightPane()}}, |
|
TableLayoutKit.FILL_LAST_COLUMN, |
|
LayoutConstants.VGAP_LARGE, |
|
LayoutConstants.VGAP_LARGE); |
|
jPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0)); |
|
createPane.add(jPanel, BorderLayout.CENTER); |
|
return createPane; |
|
} |
|
|
|
/** |
|
* @param listener 观察者监听事件 |
|
*/ |
|
public void registerNameListener(GlobalNameListener listener) { |
|
globalNameListener = listener; |
|
} |
|
|
|
public boolean shouldResponseNameListener() { |
|
return false; |
|
} |
|
|
|
public void setGlobalName(String name) { |
|
} |
|
}
|
|
|