|
|
@ -58,7 +58,7 @@ import java.math.BigDecimal; |
|
|
|
* |
|
|
|
* |
|
|
|
* @date: 2014-12-5-下午1:10:31 |
|
|
|
* @date: 2014-12-5-下午1:10:31 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class RichTextToolBar extends BasicPane{ |
|
|
|
public class RichTextToolBar extends BasicPane { |
|
|
|
|
|
|
|
|
|
|
|
private static final Dimension BUTTON_SIZE = new Dimension(24, 20); |
|
|
|
private static final Dimension BUTTON_SIZE = new Dimension(24, 20); |
|
|
|
|
|
|
|
|
|
|
@ -103,7 +103,7 @@ 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()); |
|
|
@ -127,7 +127,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
bindListener(); |
|
|
|
bindListener(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void setAllButtonStyle(){ |
|
|
|
private void setAllButtonStyle() { |
|
|
|
setButtonStyle(bold); |
|
|
|
setButtonStyle(bold); |
|
|
|
setButtonStyle(italic); |
|
|
|
setButtonStyle(italic); |
|
|
|
setButtonStyle(underline); |
|
|
|
setButtonStyle(underline); |
|
|
@ -136,7 +136,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
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); |
|
|
|
button.setOpaque(false); |
|
|
|
button.setOpaque(false); |
|
|
@ -144,7 +144,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
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); |
|
|
@ -158,7 +158,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
this.add(formulaPane); |
|
|
|
this.add(formulaPane); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void bindListener(){ |
|
|
|
private void bindListener() { |
|
|
|
// 这里下拉框默认选中字体 不由UI界面字体决定 两套不同体系
|
|
|
|
// 这里下拉框默认选中字体 不由UI界面字体决定 两套不同体系
|
|
|
|
fontNameComboBox.addItemListener(fontNameItemListener); |
|
|
|
fontNameComboBox.addItemListener(fontNameItemListener); |
|
|
|
fontNameComboBox.setSelectedItem(DEFAULT_FONT.getFamily()); |
|
|
|
fontNameComboBox.setSelectedItem(DEFAULT_FONT.getFamily()); |
|
|
@ -207,7 +207,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
* @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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -218,7 +218,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
* @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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -322,7 +322,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
@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); |
|
|
@ -334,11 +334,13 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void itemStateChanged(ItemEvent e) { |
|
|
|
public void itemStateChanged(ItemEvent e) { |
|
|
|
String fontName = (String) RichTextToolBar.this.fontNameComboBox.getSelectedItem(); |
|
|
|
String fontName = (String) RichTextToolBar.this.fontNameComboBox.getSelectedItem(); |
|
|
|
|
|
|
|
if (fontName != null) { |
|
|
|
// 调用setCharacterAttributes函数设置文本区选择文本的字体
|
|
|
|
// 调用setCharacterAttributes函数设置文本区选择文本的字体
|
|
|
|
MutableAttributeSet attr = new SimpleAttributeSet(); |
|
|
|
MutableAttributeSet attr = new SimpleAttributeSet(); |
|
|
|
StyleConstants.setFontFamily(attr, fontName); |
|
|
|
StyleConstants.setFontFamily(attr, fontName); |
|
|
|
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); |
|
|
|
setCharacterAttributes(RichTextToolBar.this.textPane, attr, false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
private ActionListener formulaActionListener = new ActionListener() { |
|
|
|
private ActionListener formulaActionListener = new ActionListener() { |
|
|
@ -363,7 +365,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
return Integer.valueOf(numStr); |
|
|
|
return Integer.valueOf(numStr); |
|
|
@ -372,7 +374,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; |
|
|
|
boolean isItalic = true; |
|
|
|
boolean isItalic = true; |
|
|
|
boolean isUnderline = true; |
|
|
|
boolean isUnderline = true; |
|
|
@ -397,11 +399,15 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
//上标
|
|
|
|
//上标
|
|
|
|
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; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (!(attrs.getAttribute(StyleConstants.FontFamily)).equals(fontName_1st)) { |
|
|
|
|
|
|
|
fontName_1st = null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -412,7 +418,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
//动态显示工具栏上按钮的状态
|
|
|
|
//动态显示工具栏上按钮的状态
|
|
|
|
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); |
|
|
|
underline.setSelected(isUnderline); |
|
|
|
underline.setSelected(isUnderline); |
|
|
@ -427,7 +433,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
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); |
|
|
@ -442,7 +448,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
int end = textPane.getSelectionEnd(); |
|
|
|
int end = textPane.getSelectionEnd(); |
|
|
|
|
|
|
|
|
|
|
|
//如果没有选定字符
|
|
|
|
//如果没有选定字符
|
|
|
|
if(end == start){ |
|
|
|
if (end == start) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -461,7 +467,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
int start = textPane.getSelectionStart(); |
|
|
|
int start = textPane.getSelectionStart(); |
|
|
|
int end = textPane.getSelectionEnd(); |
|
|
|
int end = textPane.getSelectionEnd(); |
|
|
|
|
|
|
|
|
|
|
|
if(start != end){ |
|
|
|
if (start != end) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -469,8 +475,8 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//如果默认不选字符, 那么设置为最后一个字符的样式
|
|
|
|
//如果默认不选字符, 那么设置为最后一个字符的样式
|
|
|
|
private void setToLastCharStyle(int end, StyledDocument doc){ |
|
|
|
private void setToLastCharStyle(int end, StyledDocument doc) { |
|
|
|
if(textPane.isUpdating()){ |
|
|
|
if (textPane.isUpdating()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -490,7 +496,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
* @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); |
|
|
@ -547,11 +553,11 @@ 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); |
|
|
@ -559,7 +565,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将界面上的设置赋值给输入的字符
|
|
|
|
//将界面上的设置赋值给输入的字符
|
|
|
|
private MutableAttributeSet updateStyleFromToolBar(){ |
|
|
|
private MutableAttributeSet updateStyleFromToolBar() { |
|
|
|
final boolean isBold = bold.isSelected(); |
|
|
|
final boolean isBold = bold.isSelected(); |
|
|
|
final boolean isItalic = italic.isSelected(); |
|
|
|
final boolean isItalic = italic.isSelected(); |
|
|
|
final boolean isSub = subPane.isSelected(); |
|
|
|
final boolean isSub = subPane.isSelected(); |
|
|
@ -595,8 +601,8 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
//他会一个个insert进去直接触发inserupdate事件, 而jdk7会直接把所有的塞进来.
|
|
|
|
//他会一个个insert进去直接触发inserupdate事件, 而jdk7会直接把所有的塞进来.
|
|
|
|
//inserupdate那边绑定的是一个个插入的事件, 多个一起插入的放这
|
|
|
|
//inserupdate那边绑定的是一个个插入的事件, 多个一起插入的放这
|
|
|
|
//bug84777 8.0不走if逻辑,改成只有jdk7走if逻辑
|
|
|
|
//bug84777 8.0不走if逻辑,改成只有jdk7走if逻辑
|
|
|
|
if(StableUtils.getMajorJavaVersion() == JDK_7){ |
|
|
|
if (StableUtils.getMajorJavaVersion() == JDK_7) { |
|
|
|
if(isUpdating()){ |
|
|
|
if (isUpdating()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
StyledDocument doc = (StyledDocument) textPane.getDocument(); |
|
|
|
StyledDocument doc = (StyledDocument) textPane.getDocument(); |
|
|
@ -613,7 +619,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//中文输入法, 默认输入字符会被输入法的框截取住, jtextpane得到是一个空格, 此时不做处理
|
|
|
|
//中文输入法, 默认输入字符会被输入法的框截取住, jtextpane得到是一个空格, 此时不做处理
|
|
|
|
if(StringUtils.isBlank(content) || inputLen <= 0){ |
|
|
|
if (StringUtils.isBlank(content) || inputLen <= 0) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
//设置一次性输入多个文字的样式
|
|
|
|
//设置一次性输入多个文字的样式
|
|
|
@ -621,7 +627,7 @@ public class RichTextToolBar extends BasicPane{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void setContentStyle(final int inputLen){ |
|
|
|
private void setContentStyle(final int inputLen) { |
|
|
|
//缓存下Start, 下面要用来设置样式
|
|
|
|
//缓存下Start, 下面要用来设置样式
|
|
|
|
final int _start = inputStart; |
|
|
|
final int _start = inputStart; |
|
|
|
final MutableAttributeSet attr = updateStyleFromToolBar(); |
|
|
|
final MutableAttributeSet attr = updateStyleFromToolBar(); |
|
|
@ -640,24 +646,24 @@ 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) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
inputStart = textPane.getSelectionStart() - 1; |
|
|
|
inputStart = textPane.getSelectionStart() - 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//重置标记状态
|
|
|
|
//重置标记状态
|
|
|
|
private void resetFlag(){ |
|
|
|
private void resetFlag() { |
|
|
|
inputStart = NOT_INITED; |
|
|
|
inputStart = NOT_INITED; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|