Browse Source

REPORT-30739【10.0.6开接口】字体统一改成像素的可行性研究,及开接口

feature/big-screen
kerry 5 years ago
parent
commit
5b730fa256
  1. 7
      designer-base/src/main/java/com/fr/design/fun/FormAdaptiveConfigUIProcessor.java
  2. 30
      designer-form/src/main/java/com/fr/design/designer/creator/XButton.java
  3. 5
      designer-form/src/main/java/com/fr/design/designer/creator/XLabel.java
  4. 46
      designer-form/src/main/java/com/fr/design/form/util/FontTransformUtil.java
  5. 68
      designer-form/src/test/java/com/fr/design/form/util/FontTransformUtilTest.java
  6. 180
      designer-realize/src/main/java/com/fr/design/cell/editor/RichTextToolBar.java

7
designer-base/src/main/java/com/fr/design/fun/FormAdaptiveConfigUIProcessor.java

@ -32,5 +32,12 @@ public interface FormAdaptiveConfigUIProcessor extends Immutable {
*/ */
BufferedImage paintFormElementCaseImage(Dimension size, JComponent elementCasePane); BufferedImage paintFormElementCaseImage(Dimension size, JComponent elementCasePane);
/**
* 获取新自适应下字体显示的dpi
* @return dpi
*/
int fontResolution();
} }

30
designer-form/src/main/java/com/fr/design/designer/creator/XButton.java

@ -3,8 +3,8 @@
*/ */
package com.fr.design.designer.creator; package com.fr.design.designer.creator;
import com.fr.base.ScreenResolution;
import com.fr.base.background.GradientBackground; import com.fr.base.background.GradientBackground;
import com.fr.design.form.util.FontTransformUtil;
import com.fr.design.form.util.XCreatorConstants; import com.fr.design.form.util.XCreatorConstants;
import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.ilable.UILabel;
import com.fr.design.mainframe.widget.editors.ButtonTypeEditor; import com.fr.design.mainframe.widget.editors.ButtonTypeEditor;
@ -40,13 +40,13 @@ import java.beans.IntrospectionException;
* @since 6.5.3 * @since 6.5.3
*/ */
public class XButton extends XWidgetCreator { public class XButton extends XWidgetCreator {
public final static Background DEFAULTBG = new GradientBackground(new Color(247,247,247),new Color(210,210,210), GradientBackground.TOP2BOTTOM); public final static Background DEFAULTBG = new GradientBackground(new Color(247,247,247),new Color(210,210,210), GradientBackground.TOP2BOTTOM);
public final static Font DEFAULTFT = new Font("Song_TypeFace",0,12); public final static Font DEFAULTFT = new Font("Song_TypeFace",0,12);
public final static Color DEFAULTFOREGROUNDCOLOR = Color.BLACK; public final static Color DEFAULTFOREGROUNDCOLOR = Color.BLACK;
private Background bg; private Background bg;
private UILabel contentLabel; private UILabel contentLabel;
public XButton(Button widget, Dimension initSize) { public XButton(Button widget, Dimension initSize) {
this(new FreeButton(widget),initSize); this(new FreeButton(widget),initSize);
} }
@ -54,11 +54,11 @@ public class XButton extends XWidgetCreator {
public XButton(FreeButton widget, Dimension initSize) { public XButton(FreeButton widget, Dimension initSize) {
super(widget, initSize); super(widget, initSize);
} }
public XButton(FormSubmitButton widget, Dimension initSize) { public XButton(FormSubmitButton widget, Dimension initSize) {
super(widget, initSize); super(widget, initSize);
} }
public Background getContentBackground() { public Background getContentBackground() {
return bg; return bg;
} }
@ -74,7 +74,7 @@ public class XButton extends XWidgetCreator {
public void setContentLabel(UILabel contentLabel) { public void setContentLabel(UILabel contentLabel) {
this.contentLabel = contentLabel; this.contentLabel = contentLabel;
} }
/** /**
*根据下拉框选择返回按钮样式的默认设置或自定义设置列表 *根据下拉框选择返回按钮样式的默认设置或自定义设置列表
* @return 列表 * @return 列表
@ -148,7 +148,7 @@ public class XButton extends XWidgetCreator {
FreeButton button = (FreeButton) data; FreeButton button = (FreeButton) data;
if (button.getFont() != null) { if (button.getFont() != null) {
contentLabel.setFont(button.getFont().applyResolutionNP( contentLabel.setFont(button.getFont().applyResolutionNP(
ScreenResolution.getScreenResolution())); FontTransformUtil.getDesignerFontResolution()));
contentLabel.setForeground(button.getFont().getForeground()); contentLabel.setForeground(button.getFont().getForeground());
} }
} }
@ -205,7 +205,7 @@ public class XButton extends XWidgetCreator {
protected UILabel initContentLabel() { protected UILabel initContentLabel() {
return new UILabel(); return new UILabel();
} }
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
@ -218,15 +218,15 @@ public class XButton extends XWidgetCreator {
bg.paint(g, new Rectangle2D.Double(0, 0, panelSize.getWidth(), panelSize.getHeight())); bg.paint(g, new Rectangle2D.Double(0, 0, panelSize.getWidth(), panelSize.getHeight()));
} }
} }
public void setButtonText(String text) { public void setButtonText(String text) {
contentLabel.setText(text); contentLabel.setText(text);
} }
private void checkButonType() { private void checkButonType() {
UILabel l = contentLabel; UILabel l = contentLabel;
FreeButton button = (FreeButton) data; FreeButton button = (FreeButton) data;
if (!button.isCustomStyle()) { if (!button.isCustomStyle()) {
l.setBorder(BorderFactory.createLineBorder(new Color(148, 148, 148))); l.setBorder(BorderFactory.createLineBorder(new Color(148, 148, 148)));
bg = DEFAULTBG; bg = DEFAULTBG;
@ -240,7 +240,7 @@ public class XButton extends XWidgetCreator {
editor.add(l,BorderLayout.CENTER); editor.add(l,BorderLayout.CENTER);
if (button.getFont() != null) { if (button.getFont() != null) {
contentLabel.setFont(button.getFont().applyResolutionNP( contentLabel.setFont(button.getFont().applyResolutionNP(
ScreenResolution.getScreenResolution())); FontTransformUtil.getDesignerFontResolution()));
contentLabel.setForeground(button.getFont().getForeground()); contentLabel.setForeground(button.getFont().getForeground());
} }
l.setBounds(0, 0, button.getButtonWidth() == 0 ? this.getWidth() : button.getButtonWidth(), button l.setBounds(0, 0, button.getButtonWidth() == 0 ? this.getWidth() : button.getButtonWidth(), button
@ -248,7 +248,7 @@ public class XButton extends XWidgetCreator {
bg = button.getInitialBackground(); bg = button.getInitialBackground();
} }
} }
@Override @Override
protected void initXCreatorProperties() { protected void initXCreatorProperties() {
super.initXCreatorProperties(); super.initXCreatorProperties();
@ -257,7 +257,7 @@ public class XButton extends XWidgetCreator {
FreeButton button = (FreeButton) data; FreeButton button = (FreeButton) data;
l.setText(button.getText()); l.setText(button.getText());
if (button.isCustomStyle() && button.getFont() != null) { if (button.isCustomStyle() && button.getFont() != null) {
l.setFont(button.getFont().applyResolutionNP(ScreenResolution.getScreenResolution())); l.setFont(button.getFont().applyResolutionNP(FontTransformUtil.getDesignerFontResolution()));
l.setForeground(button.getFont().getForeground()); l.setForeground(button.getFont().getForeground());
} }
@ -303,7 +303,7 @@ public class XButton extends XWidgetCreator {
checkButonType(); checkButonType();
if (button.getFont() != null) { if (button.getFont() != null) {
contentLabel.setFont(button.getFont().applyResolutionNP( contentLabel.setFont(button.getFont().applyResolutionNP(
ScreenResolution.getScreenResolution())); FontTransformUtil.getDesignerFontResolution()));
contentLabel.setForeground(button.getFont().getForeground()); contentLabel.setForeground(button.getFont().getForeground());
} }

5
designer-form/src/main/java/com/fr/design/designer/creator/XLabel.java

@ -4,9 +4,9 @@
package com.fr.design.designer.creator; package com.fr.design.designer.creator;
import com.fr.base.BaseUtils; import com.fr.base.BaseUtils;
import com.fr.base.ScreenResolution;
import com.fr.base.Style; import com.fr.base.Style;
import com.fr.design.border.UIRoundedBorder; import com.fr.design.border.UIRoundedBorder;
import com.fr.design.form.util.FontTransformUtil;
import com.fr.design.form.util.XCreatorConstants; import com.fr.design.form.util.XCreatorConstants;
import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.ilable.UILabel;
import com.fr.design.mainframe.widget.editors.FontEditor; import com.fr.design.mainframe.widget.editors.FontEditor;
@ -98,7 +98,8 @@ public class XLabel extends XWidgetCreator {
.toString(), Style.getInstance(label.getFont()).deriveHorizontalAlignment(label.getTextalign()) .toString(), Style.getInstance(label.getFont()).deriveHorizontalAlignment(label.getTextalign())
.deriveVerticalAlignment(label.isVerticalCenter() ? SwingConstants.CENTER : SwingConstants.TOP) .deriveVerticalAlignment(label.isVerticalCenter() ? SwingConstants.CENTER : SwingConstants.TOP)
.deriveTextStyle(label.isWrap() ? Style.TEXTSTYLE_WRAPTEXT : Style.TEXTSTYLE_SINGLELINE), .deriveTextStyle(label.isWrap() ? Style.TEXTSTYLE_WRAPTEXT : Style.TEXTSTYLE_SINGLELINE),
ScreenResolution.getScreenResolution()); FontTransformUtil.getDesignerFontResolution());
} }
} }

46
designer-form/src/main/java/com/fr/design/form/util/FontTransformUtil.java

@ -0,0 +1,46 @@
package com.fr.design.form.util;
import com.fr.design.ExtraDesignClassManager;
import com.fr.design.fun.FormAdaptiveConfigUIProcessor;
import com.fr.stable.Constants;
import com.fr.stable.unit.PT;
/**
* Created by kerry on 2020-04-16
*/
public class FontTransformUtil {
/**
* 获取设计器字体显示dpi
* @return dpi
*/
public static int getDesignerFontResolution() {
int dpi = Constants.FR_PAINT_RESOLUTION;
FormAdaptiveConfigUIProcessor adaptiveConfigUI = ExtraDesignClassManager.getInstance().getSingle(FormAdaptiveConfigUIProcessor.MARK_STRING);
if (adaptiveConfigUI != null) {
dpi = adaptiveConfigUI.fontResolution();
}
return dpi;
}
/**
* pt值转px
* @param value pt值
* @return px值
*/
public static double pt2px(double value) {
return PT.pt2pix(value, getDesignerFontResolution());
}
/**
* px值转pt
* @param value px值
* @return pt值
*/
public static double px2pt(double value) {
return value * (double) Constants.DEFAULT_FONT_PAINT_RESOLUTION / (double) getDesignerFontResolution();
}
}

68
designer-form/src/test/java/com/fr/design/form/util/FontTransformUtilTest.java

@ -0,0 +1,68 @@
package com.fr.design.form.util;
import com.fr.design.ExtraDesignClassManager;
import com.fr.design.fun.FormAdaptiveConfigUIProcessor;
import com.fr.stable.Constants;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* Created by kerry on 2020-05-14
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(ExtraDesignClassManager.class)
public class FontTransformUtilTest {
@Test
public void testGetDesignerFontResolution() {
mockEnvironment(Constants.DEFAULT_FONT_PAINT_RESOLUTION);
Assert.assertEquals(Constants.DEFAULT_FONT_PAINT_RESOLUTION, FontTransformUtil.getDesignerFontResolution());
mockEnvironment(Constants.DEFAULT_WEBWRITE_AND_SCREEN_RESOLUTION);
Assert.assertEquals(Constants.DEFAULT_WEBWRITE_AND_SCREEN_RESOLUTION, FontTransformUtil.getDesignerFontResolution());
}
@Test
public void testPt2px() {
mockEnvironment(Constants.DEFAULT_FONT_PAINT_RESOLUTION);
Assert.assertEquals(12, (int) FontTransformUtil.pt2px(12));
mockEnvironment(Constants.DEFAULT_WEBWRITE_AND_SCREEN_RESOLUTION);
Assert.assertEquals(16, (int) FontTransformUtil.pt2px(12));
}
@Test
public void testPx2pt() {
mockEnvironment(Constants.DEFAULT_FONT_PAINT_RESOLUTION);
Assert.assertEquals(12, (int) FontTransformUtil.px2pt(12));
mockEnvironment(Constants.DEFAULT_WEBWRITE_AND_SCREEN_RESOLUTION);
Assert.assertEquals(9, (int) FontTransformUtil.px2pt(12));
}
private void mockEnvironment(int dpi) {
ExtraDesignClassManager mockDesignManager = EasyMock.mock(ExtraDesignClassManager.class);
EasyMock.expect(mockDesignManager.getSingle(FormAdaptiveConfigUIProcessor.MARK_STRING))
.andReturn(mockProcessor(dpi)).anyTimes();
EasyMock.replay(mockDesignManager);
PowerMock.mockStatic(ExtraDesignClassManager.class);
EasyMock.expect(ExtraDesignClassManager.getInstance()).andReturn(mockDesignManager).once();
PowerMock.replayAll(ExtraDesignClassManager.class);
}
private FormAdaptiveConfigUIProcessor mockProcessor(int dpi) {
FormAdaptiveConfigUIProcessor processor = EasyMock.mock(FormAdaptiveConfigUIProcessor.class);
EasyMock.expect(processor.fontResolution()).andReturn(dpi).once();
EasyMock.replay(processor);
return processor;
}
}

180
designer-realize/src/main/java/com/fr/design/cell/editor/RichTextToolBar.java

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package com.fr.design.cell.editor; package com.fr.design.cell.editor;
@ -8,6 +8,7 @@ import com.fr.base.BaseUtils;
import com.fr.base.Utils; import com.fr.base.Utils;
import com.fr.design.dialog.BasicPane; import com.fr.design.dialog.BasicPane;
import com.fr.design.dialog.DialogActionAdapter; import com.fr.design.dialog.DialogActionAdapter;
import com.fr.design.form.util.FontTransformUtil;
import com.fr.design.formula.FormulaFactory; import com.fr.design.formula.FormulaFactory;
import com.fr.design.formula.UIFormula; import com.fr.design.formula.UIFormula;
import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.ibutton.UIButton;
@ -21,7 +22,6 @@ import com.fr.design.style.color.UIToolbarColorButton;
import com.fr.general.FRFont; import com.fr.general.FRFont;
import com.fr.log.FineLoggerFactory; import com.fr.log.FineLoggerFactory;
import com.fr.report.cell.cellattr.core.RichTextConverter; import com.fr.report.cell.cellattr.core.RichTextConverter;
import com.fr.stable.Constants;
import com.fr.stable.StableUtils; import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils; import com.fr.stable.StringUtils;
@ -71,17 +71,17 @@ public class RichTextToolBar extends BasicPane{
private UIToggleButton superPane; private UIToggleButton superPane;
private UIToggleButton subPane; private UIToggleButton subPane;
private UIToggleButton formulaPane; private UIToggleButton formulaPane;
//外部传进来的 //外部传进来的
private RichTextEditingPane textPane; private RichTextEditingPane textPane;
public RichTextToolBar() { public RichTextToolBar() {
this.initComponents(); this.initComponents();
} }
public RichTextToolBar(RichTextEditingPane textPane) { public RichTextToolBar(RichTextEditingPane textPane) {
this.textPane = textPane; this.textPane = textPane;
this.initComponents(); this.initComponents();
} }
@ -96,14 +96,14 @@ public class RichTextToolBar extends BasicPane{
//添加到工具栏 //添加到工具栏
addToToolBar(); addToToolBar();
} }
private void initAllButton(){ private void initAllButton(){
fontNameComboBox = new UIComboBox(Utils.getAvailableFontFamilyNames4Report()); fontNameComboBox = new UIComboBox(Utils.getAvailableFontFamilyNames4Report());
fontNameComboBox.setPreferredSize(new Dimension(144, 20)); fontNameComboBox.setPreferredSize(new Dimension(144, 20));
fontSizeComboBox = new UIComboBox(FRFontPane.getFontSizes()); fontSizeComboBox = new UIComboBox(FRFontPane.getFontSizes());
colorSelectPane = new UIToolbarColorButton(BaseUtils.readIcon("/com/fr/design/images/gui/color/foreground.png")); colorSelectPane = new UIToolbarColorButton(BaseUtils.readIcon("/com/fr/design/images/gui/color/foreground.png"));
colorSelectPane.set4Toolbar(); colorSelectPane.set4Toolbar();
bold = new UIToggleButton(BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/bold.png")); bold = new UIToggleButton(BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/bold.png"));
italic = new UIToggleButton(BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/italic.png")); italic = new UIToggleButton(BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/italic.png"));
underline = new UIToggleButton(BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/underline.png")); underline = new UIToggleButton(BaseUtils.readIcon("/com/fr/design/images/m_format/cellstyle/underline.png"));
@ -120,7 +120,7 @@ public class RichTextToolBar extends BasicPane{
//绑定监听器 //绑定监听器
bindListener(); bindListener();
} }
private void setAllButtonStyle(){ private void setAllButtonStyle(){
setButtonStyle(bold); setButtonStyle(bold);
setButtonStyle(italic); setButtonStyle(italic);
@ -129,7 +129,7 @@ public class RichTextToolBar extends BasicPane{
setButtonStyle(superPane); setButtonStyle(superPane);
setButtonStyle(formulaPane); setButtonStyle(formulaPane);
} }
private void setButtonStyle(UIButton button){ private void setButtonStyle(UIButton button){
button.setNormalPainted(false); button.setNormalPainted(false);
button.setBackground(null); button.setBackground(null);
@ -137,10 +137,10 @@ public class RichTextToolBar extends BasicPane{
button.setPreferredSize(BUTTON_SIZE); button.setPreferredSize(BUTTON_SIZE);
button.setBorderPaintedOnlyWhenPressed(true); button.setBorderPaintedOnlyWhenPressed(true);
} }
private void addToToolBar(){ private void addToToolBar(){
this.setLayout(new FlowLayout(FlowLayout.LEFT)); this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(fontNameComboBox); this.add(fontNameComboBox);
this.add(fontSizeComboBox); this.add(fontSizeComboBox);
this.add(bold); this.add(bold);
@ -151,14 +151,14 @@ public class RichTextToolBar extends BasicPane{
this.add(subPane); this.add(subPane);
this.add(formulaPane); this.add(formulaPane);
} }
private void bindListener(){ private void bindListener(){
FRFont defaultFont = (this.textPane != null) ? FRFont.getInstance(this.textPane.getFont()) : RichTextPane.DEFAUL_FONT; FRFont defaultFont = (this.textPane != null) ? FRFont.getInstance(this.textPane.getFont()) : RichTextPane.DEFAUL_FONT;
fontNameComboBox.addItemListener(fontNameItemListener); fontNameComboBox.addItemListener(fontNameItemListener);
fontNameComboBox.setSelectedItem(defaultFont.getFontName()); fontNameComboBox.setSelectedItem(defaultFont.getFontName());
fontSizeComboBox.addItemListener(fontSizeItemListener); fontSizeComboBox.addItemListener(fontSizeItemListener);
fontSizeComboBox.setSelectedItem(scaleDown(defaultFont.getSize())); fontSizeComboBox.setSelectedItem(scaleDown(defaultFont.getSize()));
bold.addActionListener(blodChangeAction); bold.addActionListener(blodChangeAction);
italic.addActionListener(itaChangeAction); italic.addActionListener(itaChangeAction);
underline.addActionListener(underlineChangeAction); underline.addActionListener(underlineChangeAction);
@ -166,7 +166,7 @@ public class RichTextToolBar extends BasicPane{
superPane.addActionListener(superChangeAction); superPane.addActionListener(superChangeAction);
colorSelectPane.addColorChangeListener(colorChangeAction); colorSelectPane.addColorChangeListener(colorChangeAction);
formulaPane.addActionListener(formulaActionListener); formulaPane.addActionListener(formulaActionListener);
//选中文字的监听器 //选中文字的监听器
textPane.addCaretListener(textCareListener); textPane.addCaretListener(textCareListener);
textPane.addMouseListener(setMouseCurrentStyle); textPane.addMouseListener(setMouseCurrentStyle);
@ -192,32 +192,32 @@ public class RichTextToolBar extends BasicPane{
subPane.setToolTipText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Sub_Script")); subPane.setToolTipText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Sub_Script"));
formulaPane.setToolTipText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula")); formulaPane.setToolTipText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Formula"));
} }
/** /**
* 移除输入监听 * 移除输入监听
* 用于populate时, 插入字符串, 那时不需要插入监听 * 用于populate时, 插入字符串, 那时不需要插入监听
* *
* *
* @date 2015-1-5-下午5:13:04 * @date 2015-1-5-下午5:13:04
* *
*/ */
public void removeInputListener(){ public void removeInputListener(){
this.textPane.getDocument().removeDocumentListener(inputListener); this.textPane.getDocument().removeDocumentListener(inputListener);
} }
/** /**
* 增加输入监听事件 * 增加输入监听事件
* *
* *
* @date 2015-1-5-下午5:13:26 * @date 2015-1-5-下午5:13:26
* *
*/ */
public void addInputListener(){ public void addInputListener(){
this.textPane.getDocument().addDocumentListener(inputListener); this.textPane.getDocument().addDocumentListener(inputListener);
} }
private ActionListener blodChangeAction = new ActionListener() { private ActionListener blodChangeAction = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean isBold = RichTextToolBar.this.bold.isSelected(); boolean isBold = RichTextToolBar.this.bold.isSelected();
@ -227,9 +227,9 @@ public class RichTextToolBar extends BasicPane{
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); setCharacterAttributes(RichTextToolBar.this.textPane, attr, false);
} }
}; };
private ActionListener itaChangeAction = new ActionListener() { private ActionListener itaChangeAction = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean isIta = RichTextToolBar.this.italic.isSelected(); boolean isIta = RichTextToolBar.this.italic.isSelected();
@ -239,9 +239,9 @@ public class RichTextToolBar extends BasicPane{
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); setCharacterAttributes(RichTextToolBar.this.textPane, attr, false);
} }
}; };
private ActionListener underlineChangeAction = new ActionListener() { private ActionListener underlineChangeAction = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean isUnder = RichTextToolBar.this.underline.isSelected(); boolean isUnder = RichTextToolBar.this.underline.isSelected();
@ -252,7 +252,7 @@ public class RichTextToolBar extends BasicPane{
} }
}; };
private ActionListener subChangeAction = new ActionListener() { private ActionListener subChangeAction = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean isSub = RichTextToolBar.this.subPane.isSelected(); boolean isSub = RichTextToolBar.this.subPane.isSelected();
@ -263,7 +263,7 @@ public class RichTextToolBar extends BasicPane{
} }
}; };
private ActionListener superChangeAction = new ActionListener() { private ActionListener superChangeAction = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
boolean isSuper = RichTextToolBar.this.superPane.isSelected(); boolean isSuper = RichTextToolBar.this.superPane.isSelected();
@ -273,7 +273,7 @@ public class RichTextToolBar extends BasicPane{
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); setCharacterAttributes(RichTextToolBar.this.textPane, attr, false);
} }
}; };
private ChangeListener colorChangeAction = new ChangeListener() { private ChangeListener colorChangeAction = new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
@ -285,13 +285,13 @@ public class RichTextToolBar extends BasicPane{
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); setCharacterAttributes(RichTextToolBar.this.textPane, attr, false);
} }
}; };
// 设置文本区选择文本的样式 // 设置文本区选择文本的样式
private void setCharacterAttributes(JEditorPane editor, AttributeSet attr, private void setCharacterAttributes(JEditorPane editor, AttributeSet attr,
boolean replace) { boolean replace) {
//注意不要失焦 //注意不要失焦
textPane.requestFocus(); textPane.requestFocus();
// 取得选择文本的起始位置和结束位置 // 取得选择文本的起始位置和结束位置
int start = editor.getSelectionStart(); int start = editor.getSelectionStart();
int end = editor.getSelectionEnd(); int end = editor.getSelectionEnd();
@ -303,19 +303,19 @@ public class RichTextToolBar extends BasicPane{
doc.setCharacterAttributes(start, end - start, attr, replace); doc.setCharacterAttributes(start, end - start, attr, replace);
} }
} }
private ItemListener fontSizeItemListener = new ItemListener() { private ItemListener fontSizeItemListener = new ItemListener() {
@Override @Override
public void itemStateChanged(ItemEvent e) { public void itemStateChanged(ItemEvent e) {
int fontSize = (Integer) RichTextToolBar.this.fontSizeComboBox.getSelectedItem(); int fontSize = (Integer) RichTextToolBar.this.fontSizeComboBox.getSelectedItem();
fontSize = scaleUp(fontSize); fontSize= scaleUp(fontSize);
// 调用setCharacterAttributes函数设置文本区选择文本的字体 // 调用setCharacterAttributes函数设置文本区选择文本的字体
MutableAttributeSet attr = new SimpleAttributeSet(); MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, fontSize); StyleConstants.setFontSize(attr, fontSize);
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); setCharacterAttributes(RichTextToolBar.this.textPane, attr, false);
} }
}; };
private ItemListener fontNameItemListener = new ItemListener() { private ItemListener fontNameItemListener = new ItemListener() {
@Override @Override
public void itemStateChanged(ItemEvent e) { public void itemStateChanged(ItemEvent e) {
@ -326,7 +326,7 @@ public class RichTextToolBar extends BasicPane{
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); setCharacterAttributes(RichTextToolBar.this.textPane, attr, false);
} }
}; };
private ActionListener formulaActionListener = new ActionListener() { private ActionListener formulaActionListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
final UIFormula formulaPane = FormulaFactory.createFormulaPane(); final UIFormula formulaPane = FormulaFactory.createFormulaPane();
@ -348,7 +348,7 @@ public class RichTextToolBar extends BasicPane{
}).setVisible(true); }).setVisible(true);
} }
}; };
private int roundUp(double num){ private int roundUp(double num){
String numStr = Double.toString(num); String numStr = Double.toString(num);
numStr = new BigDecimal(numStr).setScale(0, BigDecimal.ROUND_HALF_UP).toString(); numStr = new BigDecimal(numStr).setScale(0, BigDecimal.ROUND_HALF_UP).toString();
@ -356,7 +356,7 @@ public class RichTextToolBar extends BasicPane{
} }
private CaretListener textCareListener = new CaretListener() { private CaretListener textCareListener = new CaretListener() {
//根据选中部分的文字样式, 来动态显示工具栏上按钮的状态 //根据选中部分的文字样式, 来动态显示工具栏上按钮的状态
private void setSelectedCharStyle(int start, int end, StyledDocument doc){ private void setSelectedCharStyle(int start, int end, StyledDocument doc){
boolean isBold = true; boolean isBold = true;
@ -367,11 +367,11 @@ public class RichTextToolBar extends BasicPane{
String fontName_1st = null; String fontName_1st = null;
int fontSize_1st = 0; int fontSize_1st = 0;
Color fontColor_1st = null; Color fontColor_1st = null;
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
Element ele = doc.getCharacterElement(i); Element ele = doc.getCharacterElement(i);
AttributeSet attrs = ele.getAttributes(); AttributeSet attrs = ele.getAttributes();
//粗体 //粗体
isBold = isBold && StyleConstants.isBold(attrs); isBold = isBold && StyleConstants.isBold(attrs);
//斜体 //斜体
@ -382,22 +382,22 @@ public class RichTextToolBar extends BasicPane{
isSubscript = isSubscript && StyleConstants.isSubscript(attrs); isSubscript = isSubscript && StyleConstants.isSubscript(attrs);
//上标 //上标
isSuperscript = isSuperscript && StyleConstants.isSuperscript(attrs); isSuperscript = isSuperscript && StyleConstants.isSuperscript(attrs);
if(i == start){ if(i == start){
fontName_1st = (String) attrs.getAttribute(StyleConstants.FontFamily); fontName_1st = (String) attrs.getAttribute(StyleConstants.FontFamily);
fontSize_1st = (Integer) attrs.getAttribute(StyleConstants.FontSize); fontSize_1st = (Integer) attrs.getAttribute(StyleConstants.FontSize);
fontColor_1st = (Color) attrs.getAttribute(StyleConstants.Foreground); fontColor_1st = (Color) attrs.getAttribute(StyleConstants.Foreground);
fontColor_1st = fontColor_1st == null ? Color.BLACK : fontColor_1st; fontColor_1st = fontColor_1st == null ? Color.BLACK : fontColor_1st;
} }
} }
setButtonSelected(isBold, isItalic, isUnderline, isSubscript, isSuperscript, setButtonSelected(isBold, isItalic, isUnderline, isSubscript, isSuperscript,
fontName_1st, fontSize_1st, fontColor_1st); fontName_1st, fontSize_1st, fontColor_1st);
} }
//动态显示工具栏上按钮的状态 //动态显示工具栏上按钮的状态
private void setButtonSelected(boolean isBold, boolean isItalic, boolean isUnderline, private void setButtonSelected(boolean isBold, boolean isItalic, boolean isUnderline,
boolean isSubscript, boolean isSuperscript, String fontName_1st, boolean isSubscript, boolean isSuperscript, String fontName_1st,
int fontSize_1st, Color fontColor_1st){ int fontSize_1st, Color fontColor_1st){
bold.setSelected(isBold); bold.setSelected(isBold);
italic.setSelected(isItalic); italic.setSelected(isItalic);
@ -412,75 +412,75 @@ public class RichTextToolBar extends BasicPane{
fontSizeComboBox.addItemListener(fontSizeItemListener); fontSizeComboBox.addItemListener(fontSizeItemListener);
selectColorPane(fontColor_1st); selectColorPane(fontColor_1st);
} }
private void selectColorPane(Color color){ private void selectColorPane(Color color){
colorSelectPane.removeColorChangeListener(colorChangeAction); colorSelectPane.removeColorChangeListener(colorChangeAction);
colorSelectPane.setColor(color); colorSelectPane.setColor(color);
colorSelectPane.addColorChangeListener(colorChangeAction); colorSelectPane.addColorChangeListener(colorChangeAction);
} }
@Override @Override
public void caretUpdate(CaretEvent e) { public void caretUpdate(CaretEvent e) {
StyledDocument doc = (StyledDocument) textPane.getDocument(); StyledDocument doc = (StyledDocument) textPane.getDocument();
// 取得选择文本的起始位置和结束位置 // 取得选择文本的起始位置和结束位置
int start = textPane.getSelectionStart(); int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd(); int end = textPane.getSelectionEnd();
//如果没有选定字符 //如果没有选定字符
if(end == start){ if(end == start){
return; return;
} }
setSelectedCharStyle(start, end, doc); setSelectedCharStyle(start, end, doc);
} }
}; };
//设置当前光标位样式 //设置当前光标位样式
private MouseListener setMouseCurrentStyle = new MouseAdapter() { private MouseListener setMouseCurrentStyle = new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
StyledDocument doc = (StyledDocument) textPane.getDocument(); StyledDocument doc = (StyledDocument) textPane.getDocument();
// 取得选择文本的起始位置和结束位置 // 取得选择文本的起始位置和结束位置
int start = textPane.getSelectionStart(); int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd(); int end = textPane.getSelectionEnd();
if(start != end){ if(start != end){
return; return;
} }
setToLastCharStyle(end, doc); setToLastCharStyle(end, doc);
} }
//如果默认不选字符, 那么设置为最后一个字符的样式 //如果默认不选字符, 那么设置为最后一个字符的样式
private void setToLastCharStyle(int end, StyledDocument doc){ private void setToLastCharStyle(int end, StyledDocument doc){
if(textPane.isUpdating()){ if(textPane.isUpdating()){
return; return;
} }
//取前一个字符的样式 //取前一个字符的样式
Element ele = doc.getCharacterElement(end - 1); Element ele = doc.getCharacterElement(end - 1);
AttributeSet attrs = ele.getAttributes(); AttributeSet attrs = ele.getAttributes();
populateToolBar(attrs); populateToolBar(attrs);
} }
}; };
/** /**
* 从样式中更新工具栏上的按钮状态 * 从样式中更新工具栏上的按钮状态
* *
* @param attrs 样式 * @param attrs 样式
* *
* *
* @date 2015-1-5-下午5:12:33 * @date 2015-1-5-下午5:12:33
* *
*/ */
public void populateToolBar(AttributeSet attrs){ public void populateToolBar(AttributeSet attrs){
int size = scaleDown(StyleConstants.getFontSize(attrs)); int size = scaleDown(StyleConstants.getFontSize(attrs));
fontNameComboBox.setSelectedItem(StyleConstants.getFontFamily(attrs)); fontNameComboBox.setSelectedItem(StyleConstants.getFontFamily(attrs));
fontSizeComboBox.setSelectedItem(size); fontSizeComboBox.setSelectedItem(size);
bold.setSelected(StyleConstants.isBold(attrs)); bold.setSelected(StyleConstants.isBold(attrs));
italic.setSelected(StyleConstants.isItalic(attrs)); italic.setSelected(StyleConstants.isItalic(attrs));
underline.setSelected(StyleConstants.isUnderline(attrs)); underline.setSelected(StyleConstants.isUnderline(attrs));
@ -491,31 +491,23 @@ public class RichTextToolBar extends BasicPane{
colorSelectPane.setColor(foreGround); colorSelectPane.setColor(foreGround);
colorSelectPane.repaint(); colorSelectPane.repaint();
} }
//pt转为px =*4/3 //pt转为px =*4/3
private int scaleUp(int fontSize){ private int scaleUp(int fontSize) {
return scale(fontSize, true); return (int) FontTransformUtil.pt2px(fontSize);
} }
//px转pt = *3/4 //px转pt = *3/4
private int scaleDown(int fontSize){ private int scaleDown(int fontSize) {
return scale(fontSize, false); return (int) FontTransformUtil.px2pt(fontSize);
}
private int scale(int fontSize, boolean isUp){
double dpi96 = Constants.FR_PAINT_RESOLUTION;
double dpi72 = Constants.DEFAULT_FONT_PAINT_RESOLUTION;
double scale = isUp ? (dpi96 / dpi72) : (dpi72 / dpi96);
return roundUp(fontSize * scale);
} }
private DocumentListener inputListener = new DocumentListener() { private DocumentListener inputListener = new DocumentListener() {
@Override @Override
public void removeUpdate(DocumentEvent e) { public void removeUpdate(DocumentEvent e) {
} }
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
//标志正在更新内容 //标志正在更新内容
@ -523,7 +515,7 @@ public class RichTextToolBar extends BasicPane{
final MutableAttributeSet attr = updateStyleFromToolBar(); final MutableAttributeSet attr = updateStyleFromToolBar();
final int start = textPane.getSelectionStart(); final int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd(); int end = textPane.getSelectionEnd();
if (start != end) { if (start != end) {
textPane.finishUpdating(); textPane.finishUpdating();
return; return;
@ -538,19 +530,19 @@ public class RichTextToolBar extends BasicPane{
} }
}); });
} }
//根据Style来显示populate按钮 //根据Style来显示populate按钮
private void changeContentStyle(int start, MutableAttributeSet attr){ private void changeContentStyle(int start, MutableAttributeSet attr){
changeContentStyle(start, attr, 1); changeContentStyle(start, attr, 1);
} }
private void changeContentStyle(int start, MutableAttributeSet attr, int contentLength){ private void changeContentStyle(int start, MutableAttributeSet attr, int contentLength){
// 将所选文本设置为新的样式,replace为false表示不覆盖原有的样式 // 将所选文本设置为新的样式,replace为false表示不覆盖原有的样式
StyledDocument doc = (StyledDocument) textPane.getDocument(); StyledDocument doc = (StyledDocument) textPane.getDocument();
doc.setCharacterAttributes(start, contentLength, attr, false); doc.setCharacterAttributes(start, contentLength, attr, false);
textPane.finishUpdating(); textPane.finishUpdating();
} }
//将界面上的设置赋值给输入的字符 //将界面上的设置赋值给输入的字符
private MutableAttributeSet updateStyleFromToolBar(){ private MutableAttributeSet updateStyleFromToolBar(){
final boolean isBold = bold.isSelected(); final boolean isBold = bold.isSelected();
@ -561,7 +553,7 @@ public class RichTextToolBar extends BasicPane{
final String fontName = (String) fontNameComboBox.getSelectedItem(); final String fontName = (String) fontNameComboBox.getSelectedItem();
final int fontSize = scaleUp((Integer) fontSizeComboBox.getSelectedItem()); final int fontSize = scaleUp((Integer) fontSizeComboBox.getSelectedItem());
final Color foreGround = colorSelectPane.getColor() == null ? Color.BLACK : colorSelectPane.getColor(); final Color foreGround = colorSelectPane.getColor() == null ? Color.BLACK : colorSelectPane.getColor();
MutableAttributeSet attr = new SimpleAttributeSet(); MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBold(attr, isBold); StyleConstants.setBold(attr, isBold);
StyleConstants.setItalic(attr, isItalic); StyleConstants.setItalic(attr, isItalic);
@ -571,7 +563,7 @@ public class RichTextToolBar extends BasicPane{
StyleConstants.setForeground(attr, foreGround); StyleConstants.setForeground(attr, foreGround);
StyleConstants.setFontFamily(attr, fontName); StyleConstants.setFontFamily(attr, fontName);
StyleConstants.setFontSize(attr, fontSize); StyleConstants.setFontSize(attr, fontSize);
return attr; return attr;
} }
@ -581,7 +573,7 @@ public class RichTextToolBar extends BasicPane{
private int inputStart = NOT_INITED; private int inputStart = NOT_INITED;
private static final int JDK_6 = 6; private static final int JDK_6 = 6;
private static final int JDK_7 = 7; private static final int JDK_7 = 7;
@Override @Override
public void changedUpdate(DocumentEvent e) { public void changedUpdate(DocumentEvent e) {
//这边需要注意, jdk1.6和1.7对于输入法的处理逻辑不一样, jdk6时直接在输入法中输入一大段中文 //这边需要注意, jdk1.6和1.7对于输入法的处理逻辑不一样, jdk6时直接在输入法中输入一大段中文
@ -595,7 +587,7 @@ public class RichTextToolBar extends BasicPane{
StyledDocument doc = (StyledDocument) textPane.getDocument(); StyledDocument doc = (StyledDocument) textPane.getDocument();
final String content; final String content;
initFlag(doc); initFlag(doc);
final int start = textPane.getSelectionStart(); final int start = textPane.getSelectionStart();
final int inputLen = start - inputStart; final int inputLen = start - inputStart;
//检测输入内容 //检测输入内容
@ -604,7 +596,7 @@ public class RichTextToolBar extends BasicPane{
} catch (BadLocationException e1) { } catch (BadLocationException e1) {
return; return;
} }
//中文输入法, 默认输入字符会被输入法的框截取住, jtextpane得到是一个空格, 此时不做处理 //中文输入法, 默认输入字符会被输入法的框截取住, jtextpane得到是一个空格, 此时不做处理
if(StringUtils.isBlank(content) || inputLen <= 0){ if(StringUtils.isBlank(content) || inputLen <= 0){
return; return;
@ -613,7 +605,7 @@ public class RichTextToolBar extends BasicPane{
setContentStyle(inputLen); setContentStyle(inputLen);
} }
} }
private void setContentStyle(final int inputLen){ private void setContentStyle(final int inputLen){
//缓存下Start, 下面要用来设置样式 //缓存下Start, 下面要用来设置样式
final int _start = inputStart; final int _start = inputStart;
@ -632,15 +624,15 @@ public class RichTextToolBar extends BasicPane{
} }
}); });
} }
private boolean isUpdating(){ private boolean isUpdating(){
return inputStart == UPDATING; return inputStart == UPDATING;
} }
private void startUpdating(){ private void startUpdating(){
inputStart = UPDATING; inputStart = UPDATING;
} }
//初始标记状态, 用于记录中文输入法多个字符同时输入的问题 //初始标记状态, 用于记录中文输入法多个字符同时输入的问题
private void initFlag(StyledDocument doc){ private void initFlag(StyledDocument doc){
if(inputStart != NOT_INITED){ if(inputStart != NOT_INITED){
@ -648,7 +640,7 @@ public class RichTextToolBar extends BasicPane{
} }
inputStart = textPane.getSelectionStart() - 1; inputStart = textPane.getSelectionStart() - 1;
} }
//重置标记状态 //重置标记状态
private void resetFlag(){ private void resetFlag(){
inputStart = NOT_INITED; inputStart = NOT_INITED;

Loading…
Cancel
Save