Browse Source
* commit 'e49e9ae753700003f14407aea0a0a533c8a6edf5': (79 commits) 改一下国际化 REPORT-7463 繁体设计器:无法取消勾选打印/导出单元格内容/背景 REPORT-7454 切换单元格数据集编辑面板部分配置项可能不能刷新显示的问题;处理部分代码质量,防止只因为切换面板就触发保存 无任务,交互调整 REPORT-7416 参数面板的相关bug REPORT-7418 帮助符号里的帮助符号点不动,描述见下 REPORT-7196 组件集合复用在自适应布局里,里面的子组件没有随外面绝对布局框的大小改变而拉伸 CHART-2144 REPORT-7245 国际化 REPORT-7321 1、绝对布局里两个tab点选选不中 REPORT-7245 @plough:工具栏残留之前打印按钮的痕迹=>兼容老打印方式 XML_TAG REPORT-7343 @plough:国际化 无JIRA任务, 调整代码,去掉NativePrintConfigManager REPORT-7193 使用共享组件后的帮助符号的问题 REPORT-7193 使用共享组件后的帮助符号的问题 REPORT-7245 @plough:工具栏残留之前打印按钮的痕迹 无JIRA任务,使用泛型 REPORT-7267 @plough:交互问题=>打印顺序改名为分页顺序,移回页面设置中 REPORT-7321 多选组件时,先点击Ctrl(shift),再分别点几个组件或者直接框选,选不中 REPORT-7372 9.0启动界面oem支持 ...
74 changed files with 3597 additions and 1723 deletions
@ -0,0 +1,74 @@ |
|||||||
|
package com.fr.design.actions.report; |
||||||
|
|
||||||
|
import com.fr.base.IconManager; |
||||||
|
import com.fr.design.actions.JWorkBookAction; |
||||||
|
import com.fr.design.dialog.BasicDialog; |
||||||
|
import com.fr.design.dialog.DialogActionAdapter; |
||||||
|
import com.fr.design.mainframe.DesignerContext; |
||||||
|
import com.fr.design.mainframe.JWorkBook; |
||||||
|
import com.fr.design.menu.MenuKeySet; |
||||||
|
import com.fr.design.webattr.printsettings.ReportPrintSettingPane; |
||||||
|
import com.fr.general.IOUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.main.TemplateWorkBook; |
||||||
|
import com.fr.base.print.PrintSettingsAttrMark; |
||||||
|
import com.fr.report.core.ReportUtils; |
||||||
|
|
||||||
|
import javax.swing.KeyStroke; |
||||||
|
import java.awt.event.ActionEvent; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by plough on 2018/3/5. |
||||||
|
*/ |
||||||
|
public class ReportPrintSettingAction extends JWorkBookAction { |
||||||
|
|
||||||
|
public ReportPrintSettingAction(JWorkBook jwb) { |
||||||
|
super(jwb); |
||||||
|
this.setMenuKeySet(REPORT_APP_ATTR); |
||||||
|
this.setName(getMenuKeySet().getMenuKeySetName() + "..."); |
||||||
|
this.setMnemonic(getMenuKeySet().getMnemonic()); |
||||||
|
this.setSmallIcon(IOUtils.readIcon(IconManager.PRINT.getPath())); |
||||||
|
this.setSearchText(new ReportPrintSettingPane()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 执行动作 |
||||||
|
* |
||||||
|
* @return 是否执行成功 |
||||||
|
*/ |
||||||
|
public void actionPerformed(ActionEvent evt) { |
||||||
|
final JWorkBook jwb = getEditingComponent(); |
||||||
|
if (jwb == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
final TemplateWorkBook wbTpl = jwb.getTarget(); |
||||||
|
PrintSettingsAttrMark printSettings = ReportUtils.getPrintSettingsFromWorkbook(wbTpl); |
||||||
|
|
||||||
|
final ReportPrintSettingPane reportPrintSettingPane = new ReportPrintSettingPane(); |
||||||
|
reportPrintSettingPane.populate(printSettings); |
||||||
|
BasicDialog dialog = reportPrintSettingPane.showWindow(DesignerContext.getDesignerFrame(), new DialogActionAdapter() { |
||||||
|
@Override |
||||||
|
public void doOk() { |
||||||
|
PrintSettingsAttrMark newPrintSettings = reportPrintSettingPane.updateBean(); |
||||||
|
wbTpl.addAttrMark(newPrintSettings); |
||||||
|
jwb.fireTargetModified(); |
||||||
|
} |
||||||
|
}); |
||||||
|
dialog.setVisible(true); |
||||||
|
} |
||||||
|
|
||||||
|
private static final MenuKeySet REPORT_APP_ATTR = new MenuKeySet() { |
||||||
|
@Override |
||||||
|
public char getMnemonic() { return 'P'; } |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getMenuName() { |
||||||
|
return Inter.getLocText("FR-Designer_Print_Setting"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public KeyStroke getKeyStroke() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,320 @@ |
|||||||
|
package com.fr.design.webattr.printsettings; |
||||||
|
|
||||||
|
import com.fr.base.PaperSize; |
||||||
|
import com.fr.base.Utils; |
||||||
|
import com.fr.base.print.NativePrintAttr; |
||||||
|
import com.fr.design.gui.ibutton.UIRadioButton; |
||||||
|
import com.fr.design.gui.icheckbox.UICheckBox; |
||||||
|
import com.fr.design.gui.icombobox.UIComboBox; |
||||||
|
import com.fr.design.gui.icombobox.UIComboBoxRenderer; |
||||||
|
import com.fr.design.gui.icontainer.UIScrollPane; |
||||||
|
import com.fr.design.gui.ilable.UILabel; |
||||||
|
import com.fr.design.gui.ispinner.UIBasicSpinner; |
||||||
|
import com.fr.design.gui.itextfield.UITextField; |
||||||
|
import com.fr.design.layout.FRGUIPaneFactory; |
||||||
|
import com.fr.design.layout.TableLayout; |
||||||
|
import com.fr.design.layout.TableLayoutHelper; |
||||||
|
import com.fr.design.utils.gui.GUICoreUtils; |
||||||
|
import com.fr.general.ComparatorUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.report.stable.ReportConstants; |
||||||
|
|
||||||
|
import javax.print.DocFlavor; |
||||||
|
import javax.print.PrintService; |
||||||
|
import javax.print.PrintServiceLookup; |
||||||
|
import javax.swing.BorderFactory; |
||||||
|
import javax.swing.ButtonGroup; |
||||||
|
import javax.swing.JList; |
||||||
|
import javax.swing.JPanel; |
||||||
|
import javax.swing.SpinnerNumberModel; |
||||||
|
import java.awt.BorderLayout; |
||||||
|
import java.awt.Component; |
||||||
|
import java.awt.Dimension; |
||||||
|
import java.awt.FlowLayout; |
||||||
|
import java.awt.event.ItemEvent; |
||||||
|
import java.awt.event.ItemListener; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by plough on 2018/3/5. |
||||||
|
*/ |
||||||
|
public class NativePrintSettingPane extends JPanel { |
||||||
|
private UICheckBox isShowDialogCheck; |
||||||
|
private UIComboBox printerComboBox; |
||||||
|
private UIBasicSpinner copySpinner; // 份数
|
||||||
|
private UIRadioButton allPageRadioButton; |
||||||
|
private UIRadioButton currentPageRadioButton; |
||||||
|
private UIRadioButton customPageRadioButton; |
||||||
|
private UITextField specifiedAreaField; |
||||||
|
private UIComboBox predefinedPaperSizeComboBox; |
||||||
|
private UICheckBox inheritPagePaperSettingCheck; |
||||||
|
private UICheckBox inheritPageLayoutSettingCheck; |
||||||
|
private UICheckBox inheritPageMarginSettingCheck; |
||||||
|
private UICheckBox fitPaperSizeCheck; // 缩放
|
||||||
|
private UIRadioButton portraitRadioButton; |
||||||
|
private UIRadioButton landscapeRadioButton; |
||||||
|
private PageMarginSettingPane pageMarginSettingPane; |
||||||
|
|
||||||
|
public NativePrintSettingPane() { |
||||||
|
initComponents(); |
||||||
|
initListeners(); |
||||||
|
} |
||||||
|
|
||||||
|
private void initComponents() { |
||||||
|
JPanel printPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||||
|
|
||||||
|
JPanel northPane = FRGUIPaneFactory.createNColumnGridInnerContainer_Pane(1, 0, 15); |
||||||
|
UILabel tipDownload = GUICoreUtils.createTipLabel(Inter.getLocText("FR-Designer_Tip_Native_Print_Need_Client")); |
||||||
|
northPane.add(tipDownload); |
||||||
|
isShowDialogCheck = new UICheckBox(Inter.getLocText("FR-Engine_Show_Print_Setting_Window_When_Printing")); |
||||||
|
isShowDialogCheck.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); |
||||||
|
UILabel tipCheck = GUICoreUtils.createTipLabel(Inter.getLocText("FR-Designer_Tip_Use_Default_Settings")); |
||||||
|
JPanel checkPane = GUICoreUtils.createFlowPane(new Component[] { |
||||||
|
isShowDialogCheck, tipCheck}, FlowLayout.LEFT); |
||||||
|
northPane.add(checkPane); |
||||||
|
northPane.setBorder(BorderFactory.createEmptyBorder(3, 10, 10, 0)); |
||||||
|
|
||||||
|
printPane.add(northPane, BorderLayout.NORTH); |
||||||
|
|
||||||
|
JPanel centerPane = FRGUIPaneFactory.createTitledBorderPane(Inter.getLocText("FR-Designer_Default_Settings")); |
||||||
|
|
||||||
|
UIScrollPane scrollPane = new UIScrollPane(getNativePrintMainSettingPane()); |
||||||
|
scrollPane.setBorder(null); |
||||||
|
scrollPane.setPreferredSize(new Dimension(600, 340)); |
||||||
|
centerPane.add(scrollPane); |
||||||
|
|
||||||
|
printPane.add(centerPane, BorderLayout.CENTER); |
||||||
|
|
||||||
|
this.setLayout(new BorderLayout()); |
||||||
|
this.add(printPane, BorderLayout.CENTER); |
||||||
|
} |
||||||
|
|
||||||
|
private void initListeners() { |
||||||
|
allPageRadioButton.addItemListener(getPageRaidoListener()); |
||||||
|
currentPageRadioButton.addItemListener(getPageRaidoListener()); |
||||||
|
customPageRadioButton.addItemListener(getPageRaidoListener()); |
||||||
|
} |
||||||
|
|
||||||
|
private ItemListener getPageRaidoListener() { |
||||||
|
return new ItemListener() { |
||||||
|
@Override |
||||||
|
public void itemStateChanged(ItemEvent e) { |
||||||
|
specifiedAreaField.setEnabled(customPageRadioButton.isSelected()); |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
private JPanel getNativePrintMainSettingPane() { |
||||||
|
// 打印机
|
||||||
|
String[] printerArray = getAllPrinterNames(); |
||||||
|
printerComboBox = new UIComboBox(printerArray); |
||||||
|
printerComboBox.setPreferredSize(new Dimension(200, printerComboBox.getPreferredSize().height)); |
||||||
|
JPanel printerPane = FRGUIPaneFactory.createLeftFlowZeroGapBorderPane(); |
||||||
|
printerPane.add(printerComboBox); |
||||||
|
|
||||||
|
// 份数
|
||||||
|
copySpinner = new UIBasicSpinner(new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1)); |
||||||
|
GUICoreUtils.setColumnForSpinner(copySpinner, 5); |
||||||
|
JPanel copyPane = FRGUIPaneFactory.createLeftFlowZeroGapBorderPane(); |
||||||
|
copyPane.add(copySpinner); |
||||||
|
|
||||||
|
// 继承页面纸张设置
|
||||||
|
inheritPagePaperSettingCheck = GUICoreUtils.createNoBorderCheckBox(Inter.getLocText("FR-Designer_Inherit_Page_Paper_Setting")); |
||||||
|
JPanel paperSettingPane = getPaperSettingPane(); |
||||||
|
JPanel paperSettingCheckPane = GUICoreUtils.createCheckboxAndDynamicPane(inheritPagePaperSettingCheck, paperSettingPane, true); |
||||||
|
|
||||||
|
// 继承页面布局设置
|
||||||
|
inheritPageLayoutSettingCheck = GUICoreUtils.createNoBorderCheckBox(Inter.getLocText("FR-Designer_Inherit_Page_Layout_Setting")); |
||||||
|
JPanel layoutSettingPane = getLayoutSettingPane(); |
||||||
|
JPanel layoutSettingCheckPane = GUICoreUtils.createCheckboxAndDynamicPane(inheritPageLayoutSettingCheck, layoutSettingPane, true); |
||||||
|
|
||||||
|
// 页码标签
|
||||||
|
UILabel printAreaLabel = new UILabel(Inter.getLocText("FR-Engine-Page_Number") + ":"); |
||||||
|
JPanel printAreaLabelPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||||
|
printAreaLabelPane.add(printAreaLabel, BorderLayout.NORTH); |
||||||
|
printAreaLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); |
||||||
|
|
||||||
|
// 边距
|
||||||
|
inheritPageMarginSettingCheck = GUICoreUtils.createNoBorderCheckBox(Inter.getLocText("FR-Designer_Inherit_Page_Margin_Setting")); |
||||||
|
pageMarginSettingPane = new PageMarginSettingPane(); |
||||||
|
pageMarginSettingPane.setBorder(BorderFactory.createEmptyBorder(10, -10, 0, 0)); |
||||||
|
JPanel pageMarginCheckPane = GUICoreUtils.createCheckboxAndDynamicPane(inheritPageMarginSettingCheck, pageMarginSettingPane, true); |
||||||
|
|
||||||
|
// 缩放
|
||||||
|
fitPaperSizeCheck = GUICoreUtils.createNoBorderCheckBox(Inter.getLocText("FR-Designer_Print_To_Fit_Paper_Size")); |
||||||
|
|
||||||
|
// TableLayout
|
||||||
|
double p = TableLayout.PREFERRED; |
||||||
|
double[] rowSize = {p, p, p, p, p, p}; |
||||||
|
double[] columnSize = {60, p}; |
||||||
|
Component[][] components = { |
||||||
|
{new UILabel(Inter.getLocText("FR-Designer_Printer") + ":"), printerPane}, |
||||||
|
{new UILabel(Inter.getLocText("FR-Designer_Copy_Number") + ":"), copyPane}, |
||||||
|
{printAreaLabelPane, getPrintAreaPane()}, |
||||||
|
{getTopAlignLabelPane(Inter.getLocText("FR-Designer_Paper") + ":"), paperSettingCheckPane}, |
||||||
|
{getTopAlignLabelPane(Inter.getLocText("FR-Designer_Layout") + ":"), layoutSettingCheckPane}, |
||||||
|
{getTopAlignLabelPane(Inter.getLocText("FR-Designer_Margin") + ":"), pageMarginCheckPane} |
||||||
|
// 此功能暂时不做,在界面上隐藏缩放选项
|
||||||
|
// {new UILabel(Inter.getLocText("FR-Designer_Scale_EnlargeOrReduce") + ":"), fitPaperSizeCheck},
|
||||||
|
}; |
||||||
|
return TableLayoutHelper.createGapTableLayoutPane(components, rowSize, columnSize, 0, 15); |
||||||
|
} |
||||||
|
|
||||||
|
private String[] getAllPrinterNames() { |
||||||
|
PrintService[] printServices = PrintServiceLookup.lookupPrintServices( |
||||||
|
DocFlavor.INPUT_STREAM.AUTOSENSE, null); |
||||||
|
Set<String> allPrinterName = new HashSet<String>(); |
||||||
|
|
||||||
|
for (int i = 0, len = printServices.length; i < len; i++) { |
||||||
|
allPrinterName.add(printServices[i].getName()); |
||||||
|
} |
||||||
|
|
||||||
|
return allPrinterName.toArray(new String[allPrinterName.size()]); |
||||||
|
} |
||||||
|
|
||||||
|
private JPanel getPaperSettingPane() { |
||||||
|
predefinedPaperSizeComboBox = new UIComboBox(); |
||||||
|
for (int i = 0; i < ReportConstants.PaperSizeNameSizeArray.length; i++) { |
||||||
|
Object[] tmpPaperSizeNameArray = ReportConstants.PaperSizeNameSizeArray[i]; |
||||||
|
predefinedPaperSizeComboBox.addItem(tmpPaperSizeNameArray[1]); |
||||||
|
} |
||||||
|
predefinedPaperSizeComboBox.setRenderer(new UIComboBoxRenderer() { |
||||||
|
@Override |
||||||
|
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||||||
|
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
||||||
|
|
||||||
|
if (value instanceof PaperSize) { |
||||||
|
PaperSize paperSize = (PaperSize) value; |
||||||
|
for (int i = 0; i < ReportConstants.PaperSizeNameSizeArray.length; i++) { |
||||||
|
Object[] tmpPaperSizeNameArray = ReportConstants.PaperSizeNameSizeArray[i]; |
||||||
|
|
||||||
|
if (ComparatorUtils.equals(paperSize, tmpPaperSizeNameArray[1])) { |
||||||
|
String sbuf = tmpPaperSizeNameArray[0].toString() + " [" + |
||||||
|
Utils.convertNumberStringToString(paperSize.getWidth().toMMValue4Scale2()) + |
||||||
|
'x' + |
||||||
|
Utils.convertNumberStringToString(paperSize.getHeight().toMMValue4Scale2()) + |
||||||
|
' ' + |
||||||
|
Inter.getLocText("PageSetup-mm") + |
||||||
|
']'; |
||||||
|
this.setText(sbuf); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return this; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
JPanel panel = FRGUIPaneFactory.createLeftFlowZeroGapBorderPane(); |
||||||
|
panel.add(predefinedPaperSizeComboBox); |
||||||
|
panel.setBorder(BorderFactory.createEmptyBorder(8, 0, 0, 0)); |
||||||
|
return panel; |
||||||
|
} |
||||||
|
|
||||||
|
private JPanel getLayoutSettingPane() { |
||||||
|
JPanel layoutSettingPane = FRGUIPaneFactory.createLeftFlowZeroGapBorderPane(); |
||||||
|
layoutSettingPane.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0)); |
||||||
|
portraitRadioButton = new UIRadioButton(Inter.getLocText("PageSetup-Portrait")); |
||||||
|
portraitRadioButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); |
||||||
|
landscapeRadioButton = new UIRadioButton(Inter.getLocText("PageSetup-Landscape")); |
||||||
|
layoutSettingPane.add(portraitRadioButton); |
||||||
|
layoutSettingPane.add(landscapeRadioButton); |
||||||
|
|
||||||
|
ButtonGroup layoutButtonGroup = new ButtonGroup(); |
||||||
|
layoutButtonGroup.add(portraitRadioButton); |
||||||
|
layoutButtonGroup.add(landscapeRadioButton); |
||||||
|
|
||||||
|
portraitRadioButton.setSelected(true); |
||||||
|
return layoutSettingPane; |
||||||
|
} |
||||||
|
|
||||||
|
// 页码范围
|
||||||
|
private JPanel getPrintAreaPane() { |
||||||
|
allPageRadioButton = new UIRadioButton(Inter.getLocText("FR-Designer_All_Pages")); |
||||||
|
currentPageRadioButton = new UIRadioButton(Inter.getLocText("FR-Designer_Current_Page")); |
||||||
|
customPageRadioButton = new UIRadioButton(Inter.getLocText("FR-Engine_HJS-Specified_Pages")); |
||||||
|
ButtonGroup group = new ButtonGroup(); |
||||||
|
group.add(allPageRadioButton); |
||||||
|
group.add(currentPageRadioButton); |
||||||
|
group.add(customPageRadioButton); |
||||||
|
allPageRadioButton.setSelected(true); |
||||||
|
|
||||||
|
specifiedAreaField = new UITextField(20); |
||||||
|
UILabel areaFieldTip = GUICoreUtils.createTipLabel(Inter.getLocText("FR-Designer_Print_Area_Tip")); |
||||||
|
|
||||||
|
// TableLayout
|
||||||
|
double p = TableLayout.PREFERRED; |
||||||
|
double[] rowSize = {p, p, p}; |
||||||
|
double[] columnSize = {p, p, p}; |
||||||
|
Component[][] components = { |
||||||
|
{allPageRadioButton, null, null}, |
||||||
|
{currentPageRadioButton, null, null}, |
||||||
|
{customPageRadioButton, specifiedAreaField, areaFieldTip} |
||||||
|
}; |
||||||
|
return TableLayoutHelper.createGapTableLayoutPane(components, rowSize, columnSize, 0, 0); |
||||||
|
} |
||||||
|
|
||||||
|
// 返回包含一个标签的 panel,标签始终位于 panel 顶部
|
||||||
|
private JPanel getTopAlignLabelPane(String labelText) { |
||||||
|
JPanel labelPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||||
|
labelPane.add(new UILabel(labelText), BorderLayout.NORTH); |
||||||
|
return labelPane; |
||||||
|
} |
||||||
|
|
||||||
|
public void populate(NativePrintAttr nativePrintAttr) { |
||||||
|
isShowDialogCheck.setSelected(nativePrintAttr.isShowDialog()); |
||||||
|
printerComboBox.setSelectedItem(nativePrintAttr.getPrinterName()); |
||||||
|
copySpinner.setValue(nativePrintAttr.getCopy()); |
||||||
|
|
||||||
|
if (nativePrintAttr.getPageType().equals(NativePrintAttr.PageType.ALL_PAGES)) { |
||||||
|
allPageRadioButton.setSelected(true); |
||||||
|
} else if (nativePrintAttr.getPageType().equals(NativePrintAttr.PageType.CURRENT_PAGE)) { |
||||||
|
currentPageRadioButton.setSelected(true); |
||||||
|
} else { |
||||||
|
customPageRadioButton.setSelected(true); |
||||||
|
specifiedAreaField.setText(nativePrintAttr.getArea()); |
||||||
|
} |
||||||
|
specifiedAreaField.setEnabled(customPageRadioButton.isSelected()); |
||||||
|
|
||||||
|
inheritPagePaperSettingCheck.setSelected(nativePrintAttr.isInheritPagePaperSetting()); |
||||||
|
predefinedPaperSizeComboBox.setSelectedItem(nativePrintAttr.getPaperSize()); |
||||||
|
inheritPageLayoutSettingCheck.setSelected(nativePrintAttr.isInheritPageLayoutSetting()); |
||||||
|
if (nativePrintAttr.getOrientation() == ReportConstants.PORTRAIT) { |
||||||
|
portraitRadioButton.setSelected(true); |
||||||
|
} else { |
||||||
|
landscapeRadioButton.setSelected(true); |
||||||
|
} |
||||||
|
inheritPageMarginSettingCheck.setSelected(nativePrintAttr.isInheritPageMarginSetting()); |
||||||
|
pageMarginSettingPane.populate(nativePrintAttr.getMargin()); |
||||||
|
fitPaperSizeCheck.setSelected(nativePrintAttr.isFitPaperSize()); |
||||||
|
} |
||||||
|
|
||||||
|
public void update(NativePrintAttr nativePrintAttr) { |
||||||
|
nativePrintAttr.setShowDialog(isShowDialogCheck.isSelected()); |
||||||
|
if (printerComboBox.getSelectedItem() != null) { |
||||||
|
nativePrintAttr.setPrinterName(printerComboBox.getSelectedItem().toString()); |
||||||
|
} |
||||||
|
nativePrintAttr.setCopy((int)copySpinner.getValue()); |
||||||
|
|
||||||
|
// 页码
|
||||||
|
if (allPageRadioButton.isSelected()) { |
||||||
|
nativePrintAttr.setPageType(NativePrintAttr.PageType.ALL_PAGES); |
||||||
|
} else if (currentPageRadioButton.isSelected()) { |
||||||
|
nativePrintAttr.setPageType(NativePrintAttr.PageType.CURRENT_PAGE); |
||||||
|
} else { |
||||||
|
nativePrintAttr.setPageType(NativePrintAttr.PageType.SPECIFIED_PAGES); |
||||||
|
nativePrintAttr.setArea(specifiedAreaField.getText()); |
||||||
|
} |
||||||
|
|
||||||
|
nativePrintAttr.setInheritPagePaperSetting(inheritPagePaperSettingCheck.isSelected()); |
||||||
|
nativePrintAttr.setPaperSize((PaperSize) predefinedPaperSizeComboBox.getSelectedItem()); |
||||||
|
nativePrintAttr.setInheritPageLayoutSetting(inheritPageLayoutSettingCheck.isSelected()); |
||||||
|
nativePrintAttr.setOrientation(portraitRadioButton.isSelected() ? |
||||||
|
ReportConstants.PORTRAIT : ReportConstants.LANDSCAPE); |
||||||
|
nativePrintAttr.setInheritPageMarginSetting(inheritPageMarginSettingCheck.isSelected()); |
||||||
|
nativePrintAttr.setMargin(pageMarginSettingPane.updateBean()); |
||||||
|
nativePrintAttr.setFitPaperSize(fitPaperSizeCheck.isSelected()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,86 @@ |
|||||||
|
package com.fr.design.webattr.printsettings; |
||||||
|
|
||||||
|
import com.fr.design.gui.icheckbox.UICheckBox; |
||||||
|
import com.fr.design.gui.ilable.UILabel; |
||||||
|
import com.fr.design.layout.FRGUIPaneFactory; |
||||||
|
import com.fr.design.layout.TableLayout; |
||||||
|
import com.fr.design.layout.TableLayoutHelper; |
||||||
|
import com.fr.design.utils.gui.GUICoreUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.base.print.NoClientPrintAttr; |
||||||
|
|
||||||
|
import javax.swing.BorderFactory; |
||||||
|
import javax.swing.JPanel; |
||||||
|
import java.awt.BorderLayout; |
||||||
|
import java.awt.Component; |
||||||
|
import java.awt.FlowLayout; |
||||||
|
|
||||||
|
/** |
||||||
|
* 零客户端打印设置面板 |
||||||
|
* Created by plough on 2018/3/5. |
||||||
|
*/ |
||||||
|
public class NoClientPrintSettingPane extends JPanel { |
||||||
|
private UICheckBox setMarginWhenPrintCheck; |
||||||
|
private UICheckBox inheritPageMarginSettingCheck; // 继承页面边距设置
|
||||||
|
|
||||||
|
private PageMarginSettingPane pageMarginSettingPane; |
||||||
|
|
||||||
|
public NoClientPrintSettingPane() { |
||||||
|
initComponents(); |
||||||
|
} |
||||||
|
|
||||||
|
private void initComponents() { |
||||||
|
JPanel printPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||||
|
|
||||||
|
setMarginWhenPrintCheck = new UICheckBox(Inter.getLocText("FR-Engine_Set_Margin_When_Printing")); |
||||||
|
setMarginWhenPrintCheck.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); |
||||||
|
UILabel tipLabel = GUICoreUtils.createTipLabel(Inter.getLocText("FR-Designer_Tip_Use_Default_Print_Margin")); |
||||||
|
JPanel northPane = GUICoreUtils.createFlowPane(new Component[] { |
||||||
|
setMarginWhenPrintCheck, tipLabel}, FlowLayout.LEFT); |
||||||
|
northPane.setBorder(BorderFactory.createEmptyBorder(8, 10, 10, 0)); |
||||||
|
|
||||||
|
printPane.add(northPane, BorderLayout.NORTH); |
||||||
|
|
||||||
|
JPanel centerPane = FRGUIPaneFactory.createTitledBorderPane(Inter.getLocText("FR-Designer_Default_Settings")); |
||||||
|
|
||||||
|
inheritPageMarginSettingCheck = GUICoreUtils.createNoBorderCheckBox(Inter.getLocText("FR-Designer_Inherit_Page_Margin_Setting")); |
||||||
|
pageMarginSettingPane = new PageMarginSettingPane(); |
||||||
|
pageMarginSettingPane.setBorder(BorderFactory.createEmptyBorder(10, -10, 0, 0)); |
||||||
|
JPanel pageMarginCheckPane = GUICoreUtils.createCheckboxAndDynamicPane(inheritPageMarginSettingCheck, pageMarginSettingPane, true); |
||||||
|
|
||||||
|
// TableLayout
|
||||||
|
double p = TableLayout.PREFERRED; |
||||||
|
double[] rowSize = {p}; |
||||||
|
double[] columnSize = {60, p}; |
||||||
|
Component[][] components = { |
||||||
|
{getTopAlignLabelPane(Inter.getLocText("FR-Designer_Margin") + ":"), pageMarginCheckPane} |
||||||
|
}; |
||||||
|
JPanel panel = TableLayoutHelper.createGapTableLayoutPane(components, rowSize, columnSize, 0, 15); |
||||||
|
|
||||||
|
centerPane.add(panel); |
||||||
|
|
||||||
|
printPane.add(centerPane, BorderLayout.CENTER); |
||||||
|
|
||||||
|
this.setLayout(new BorderLayout()); |
||||||
|
this.add(printPane, BorderLayout.CENTER); |
||||||
|
} |
||||||
|
|
||||||
|
// 返回包含一个标签的 panel,标签始终位于 panel 顶部
|
||||||
|
private JPanel getTopAlignLabelPane(String labelText) { |
||||||
|
JPanel labelPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||||
|
labelPane.add(new UILabel(labelText), BorderLayout.NORTH); |
||||||
|
return labelPane; |
||||||
|
} |
||||||
|
|
||||||
|
public void populate(NoClientPrintAttr noClientPrintAttr) { |
||||||
|
setMarginWhenPrintCheck.setSelected(noClientPrintAttr.isSetMarginOnPrint()); |
||||||
|
inheritPageMarginSettingCheck.setSelected(noClientPrintAttr.isInheritPageMarginSetting()); |
||||||
|
pageMarginSettingPane.populate(noClientPrintAttr.getMargin()); |
||||||
|
} |
||||||
|
|
||||||
|
public void update(NoClientPrintAttr noClientPrintAttr) { |
||||||
|
noClientPrintAttr.setSetMarginOnPrint(setMarginWhenPrintCheck.isSelected()); |
||||||
|
noClientPrintAttr.setInheritPageMarginSetting(inheritPageMarginSettingCheck.isSelected()); |
||||||
|
noClientPrintAttr.setMargin(pageMarginSettingPane.updateBean()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,79 @@ |
|||||||
|
package com.fr.design.webattr.printsettings; |
||||||
|
|
||||||
|
import com.fr.base.Margin; |
||||||
|
import com.fr.design.gui.ilable.UILabel; |
||||||
|
import com.fr.design.layout.FRGUIPaneFactory; |
||||||
|
import com.fr.design.report.UnitFieldPane; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.stable.Constants; |
||||||
|
|
||||||
|
import javax.swing.JPanel; |
||||||
|
import java.awt.BorderLayout; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by plough on 2018/3/5. |
||||||
|
*/ |
||||||
|
public class PageMarginSettingPane extends JPanel { |
||||||
|
private UnitFieldPane marginTopUnitFieldPane; |
||||||
|
private UnitFieldPane marginBottomUnitFieldPane; |
||||||
|
private UnitFieldPane marginLeftUnitFieldPane; |
||||||
|
private UnitFieldPane marginRightUnitFieldPane; |
||||||
|
|
||||||
|
public PageMarginSettingPane() { |
||||||
|
initComponents(); |
||||||
|
} |
||||||
|
private void initComponents() { |
||||||
|
// 页边距设置面板
|
||||||
|
JPanel marginPane = FRGUIPaneFactory.createX_AXISBoxInnerContainer_M_Pane(); |
||||||
|
// left
|
||||||
|
JPanel marginLeftPane = FRGUIPaneFactory.createY_AXISBoxInnerContainer_M_Pane(); |
||||||
|
marginPane.add(marginLeftPane); |
||||||
|
|
||||||
|
JPanel marginLeftTextPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); |
||||||
|
marginLeftPane.add(marginLeftTextPane); |
||||||
|
marginLeftTextPane.add(new UILabel(Inter.getLocText("Top") + ":")); |
||||||
|
marginTopUnitFieldPane = new UnitFieldPane(Constants.UNIT_MM); |
||||||
|
marginLeftTextPane.add(marginTopUnitFieldPane); |
||||||
|
JPanel marginLeftUnitPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); |
||||||
|
marginLeftPane.add(marginLeftUnitPane); |
||||||
|
marginLeftUnitPane.add(new UILabel(Inter.getLocText("Bottom") + ":")); |
||||||
|
marginBottomUnitFieldPane = new UnitFieldPane(Constants.UNIT_MM); |
||||||
|
marginLeftUnitPane.add(marginBottomUnitFieldPane); |
||||||
|
|
||||||
|
// right
|
||||||
|
JPanel marginRightPane = FRGUIPaneFactory.createY_AXISBoxInnerContainer_M_Pane(); |
||||||
|
marginPane.add(marginRightPane); |
||||||
|
|
||||||
|
// peter:这个一个垂直的上下的字符panel.
|
||||||
|
JPanel marginRightTextPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); |
||||||
|
marginRightPane.add(marginRightTextPane); |
||||||
|
marginRightTextPane.add(new UILabel(Inter.getLocText("Left") + ":")); |
||||||
|
marginLeftUnitFieldPane = new UnitFieldPane(Constants.UNIT_MM); |
||||||
|
marginRightTextPane.add(marginLeftUnitFieldPane); |
||||||
|
|
||||||
|
JPanel marginRightUnitPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); |
||||||
|
marginRightPane.add(marginRightUnitPane); |
||||||
|
marginRightUnitPane.add(new UILabel(Inter.getLocText("Right") + ":")); |
||||||
|
marginRightUnitFieldPane = new UnitFieldPane(Constants.UNIT_MM); |
||||||
|
marginRightUnitPane.add(marginRightUnitFieldPane); |
||||||
|
|
||||||
|
this.setLayout(new BorderLayout()); |
||||||
|
this.add(marginPane, BorderLayout.CENTER); |
||||||
|
} |
||||||
|
|
||||||
|
public void populate(Margin margin) { |
||||||
|
marginTopUnitFieldPane.setUnitValue(margin.getTop()); |
||||||
|
marginLeftUnitFieldPane.setUnitValue(margin.getLeft()); |
||||||
|
marginBottomUnitFieldPane.setUnitValue(margin.getBottom()); |
||||||
|
marginRightUnitFieldPane.setUnitValue(margin.getRight()); |
||||||
|
} |
||||||
|
|
||||||
|
public Margin updateBean() { |
||||||
|
Margin margin = new Margin(); |
||||||
|
margin.setTop(marginTopUnitFieldPane.getUnitValue()); |
||||||
|
margin.setLeft(marginLeftUnitFieldPane.getUnitValue()); |
||||||
|
margin.setBottom(marginBottomUnitFieldPane.getUnitValue()); |
||||||
|
margin.setRight(marginRightUnitFieldPane.getUnitValue()); |
||||||
|
return margin; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,111 @@ |
|||||||
|
package com.fr.design.webattr.printsettings; |
||||||
|
|
||||||
|
import com.fr.design.dialog.BasicPane; |
||||||
|
import com.fr.design.gui.ibutton.UIRadioButton; |
||||||
|
import com.fr.design.layout.FRGUIPaneFactory; |
||||||
|
import com.fr.design.utils.gui.GUICoreUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.base.print.PrintSettingsAttrMark; |
||||||
|
|
||||||
|
import javax.swing.BorderFactory; |
||||||
|
import javax.swing.ButtonGroup; |
||||||
|
import javax.swing.JPanel; |
||||||
|
import java.awt.BorderLayout; |
||||||
|
import java.awt.CardLayout; |
||||||
|
import java.awt.Component; |
||||||
|
import java.awt.FlowLayout; |
||||||
|
import java.awt.event.ItemEvent; |
||||||
|
import java.awt.event.ItemListener; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by plough on 2018/3/1. |
||||||
|
*/ |
||||||
|
public class PrintSettingPane extends BasicPane { |
||||||
|
private UIRadioButton noClientPrintRadioButton = new UIRadioButton(Inter.getLocText("FR-Engine_No_Client_Print")); |
||||||
|
private UIRadioButton nativePrintRadioButton = new UIRadioButton(Inter.getLocText("FR-Engine_Native_Print")); |
||||||
|
|
||||||
|
private NoClientPrintSettingPane noClientPrintSettingPane; |
||||||
|
private NativePrintSettingPane nativePrintSettingPane; |
||||||
|
private CardLayout printCard; |
||||||
|
private JPanel printPane; |
||||||
|
|
||||||
|
public PrintSettingPane() { |
||||||
|
initComponents(); |
||||||
|
initListener(); |
||||||
|
} |
||||||
|
|
||||||
|
private void initComponents() { |
||||||
|
this.setLayout(FRGUIPaneFactory.createBorderLayout()); |
||||||
|
JPanel allPanel = FRGUIPaneFactory.createBorderLayout_L_Pane(); |
||||||
|
this.add(allPanel, BorderLayout.CENTER); |
||||||
|
JPanel north = FRGUIPaneFactory.createVerticalFlowLayout_S_Pane(true); |
||||||
|
allPanel.add(north, BorderLayout.NORTH); |
||||||
|
ButtonGroup buttonGroup = new ButtonGroup(); |
||||||
|
noClientPrintRadioButton.setSelected(true); |
||||||
|
buttonGroup.add(noClientPrintRadioButton); |
||||||
|
buttonGroup.add(nativePrintRadioButton); |
||||||
|
noClientPrintRadioButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 50)); |
||||||
|
JPanel radioGroupPane = GUICoreUtils.createFlowPane(new Component[] { |
||||||
|
noClientPrintRadioButton, nativePrintRadioButton}, FlowLayout.LEFT, 0, 0); |
||||||
|
north.add(radioGroupPane); |
||||||
|
|
||||||
|
noClientPrintSettingPane = new NoClientPrintSettingPane(); |
||||||
|
nativePrintSettingPane = new NativePrintSettingPane(); |
||||||
|
printCard = new CardLayout(); |
||||||
|
printPane = new JPanel(); |
||||||
|
printPane.setLayout(printCard); |
||||||
|
printPane.add(noClientPrintRadioButton.getText(), noClientPrintSettingPane); |
||||||
|
printPane.add(nativePrintRadioButton.getText(), nativePrintSettingPane); |
||||||
|
|
||||||
|
north.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0)); |
||||||
|
allPanel.add(printPane, BorderLayout.CENTER); |
||||||
|
} |
||||||
|
|
||||||
|
private void initListener() { |
||||||
|
noClientPrintRadioButton.addItemListener(new ItemListener() { |
||||||
|
@Override |
||||||
|
public void itemStateChanged(ItemEvent e) { |
||||||
|
if (e.getStateChange() == ItemEvent.SELECTED) { |
||||||
|
printCard.show(printPane, noClientPrintRadioButton.getText()); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
nativePrintRadioButton.addItemListener(new ItemListener() { |
||||||
|
@Override |
||||||
|
public void itemStateChanged(ItemEvent e) { |
||||||
|
if (e.getStateChange() == ItemEvent.SELECTED) { |
||||||
|
printCard.show(printPane, nativePrintRadioButton.getText()); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected String title4PopupWindow() { |
||||||
|
return Inter.getLocText("FR-Designer_Print_Setting"); |
||||||
|
} |
||||||
|
|
||||||
|
public void populate(PrintSettingsAttrMark printSettings) { |
||||||
|
if (printSettings.getPrintType() == PrintSettingsAttrMark.NO_CLIENT_PRINT) { |
||||||
|
noClientPrintRadioButton.setSelected(true); |
||||||
|
} else { |
||||||
|
nativePrintRadioButton.setSelected(true); |
||||||
|
} |
||||||
|
noClientPrintSettingPane.populate(printSettings.getNoClientPrintAttr()); |
||||||
|
nativePrintSettingPane.populate(printSettings.getNativePrintAttr()); |
||||||
|
} |
||||||
|
|
||||||
|
public PrintSettingsAttrMark updateBean() { |
||||||
|
PrintSettingsAttrMark printSettings = new PrintSettingsAttrMark(); |
||||||
|
printSettings.setPrintType(noClientPrintRadioButton.isSelected() ? |
||||||
|
PrintSettingsAttrMark.NO_CLIENT_PRINT : PrintSettingsAttrMark.NATIVE_PRINT); |
||||||
|
if (noClientPrintRadioButton.isSelected()) { |
||||||
|
printSettings.setPrintType(PrintSettingsAttrMark.NO_CLIENT_PRINT); |
||||||
|
noClientPrintSettingPane.update(printSettings.getNoClientPrintAttr()); |
||||||
|
} else { |
||||||
|
printSettings.setPrintType(PrintSettingsAttrMark.NATIVE_PRINT); |
||||||
|
nativePrintSettingPane.update(printSettings.getNativePrintAttr()); |
||||||
|
} |
||||||
|
return printSettings; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,97 @@ |
|||||||
|
package com.fr.design.webattr.printsettings; |
||||||
|
|
||||||
|
import com.fr.design.dialog.BasicPane; |
||||||
|
import com.fr.design.gui.icombobox.UIComboBox; |
||||||
|
import com.fr.design.gui.ilable.UILabel; |
||||||
|
import com.fr.design.utils.gui.GUICoreUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.base.print.PrintSettingsAttrMark; |
||||||
|
import com.fr.report.core.ReportUtils; |
||||||
|
|
||||||
|
import javax.swing.BorderFactory; |
||||||
|
import javax.swing.JPanel; |
||||||
|
import java.awt.BorderLayout; |
||||||
|
import java.awt.Component; |
||||||
|
import java.awt.FlowLayout; |
||||||
|
import java.awt.event.ItemEvent; |
||||||
|
import java.awt.event.ItemListener; |
||||||
|
|
||||||
|
/** |
||||||
|
* 模版->打印设置 |
||||||
|
* Created by plough on 2018/3/6. |
||||||
|
*/ |
||||||
|
public class ReportPrintSettingPane extends BasicPane { |
||||||
|
private static final String[] CHOOSEITEM = new String[] { |
||||||
|
Inter.getLocText("FR-Designer_I_Want_To_Set_Single"), |
||||||
|
Inter.getLocText("FR-Designer_Using_Server_Report_View_Settings") |
||||||
|
}; |
||||||
|
private static final int SINGLE_SET = 0; |
||||||
|
private static final int SERVER_SET = 1; |
||||||
|
|
||||||
|
private UIComboBox chooseComboBox; |
||||||
|
private PrintSettingPane printSettingPane; |
||||||
|
|
||||||
|
public ReportPrintSettingPane() { |
||||||
|
initComponents(); |
||||||
|
} |
||||||
|
|
||||||
|
private void initComponents() { |
||||||
|
chooseComboBox = new UIComboBox(CHOOSEITEM); |
||||||
|
chooseComboBox.addItemListener(itemListener); |
||||||
|
UILabel belowSetLabel = new UILabel(Inter.getLocText("FR-Designer_Blow_set") + ":"); |
||||||
|
JPanel buttonPane = GUICoreUtils.createFlowPane(new Component[] { |
||||||
|
belowSetLabel, chooseComboBox}, FlowLayout.LEFT, 0, 0); |
||||||
|
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 20, 0, 0)); |
||||||
|
|
||||||
|
printSettingPane = new PrintSettingPane(); |
||||||
|
this.setLayout(new BorderLayout()); |
||||||
|
this.add(buttonPane, BorderLayout.NORTH); |
||||||
|
this.add(printSettingPane, BorderLayout.CENTER); |
||||||
|
} |
||||||
|
|
||||||
|
private ItemListener itemListener = new ItemListener() { |
||||||
|
@Override |
||||||
|
public void itemStateChanged(ItemEvent e) { |
||||||
|
if (e.getStateChange() == ItemEvent.SELECTED) { |
||||||
|
if (chooseComboBox.getSelectedIndex() == 0) { |
||||||
|
checkEnabled(true); |
||||||
|
} else { |
||||||
|
populateServerSettings(); |
||||||
|
checkEnabled(false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
private void checkEnabled(boolean isSelected) { |
||||||
|
GUICoreUtils.setEnabled(printSettingPane, isSelected); |
||||||
|
} |
||||||
|
|
||||||
|
private void populateServerSettings() { |
||||||
|
PrintSettingsAttrMark printSettings = ReportUtils.getPrintSettingsFromServerConfig(); |
||||||
|
printSettingPane.populate(printSettings); |
||||||
|
} |
||||||
|
|
||||||
|
public void populate(PrintSettingsAttrMark printSettings) { |
||||||
|
if (!printSettings.isValid()) { // 采用服务器配置
|
||||||
|
chooseComboBox.setSelectedIndex(SERVER_SET); |
||||||
|
populateServerSettings(); |
||||||
|
return; |
||||||
|
} |
||||||
|
chooseComboBox.setSelectedIndex(SINGLE_SET); |
||||||
|
printSettingPane.populate(printSettings); |
||||||
|
} |
||||||
|
|
||||||
|
public PrintSettingsAttrMark updateBean() { |
||||||
|
PrintSettingsAttrMark printSettings = printSettingPane.updateBean(); |
||||||
|
if (chooseComboBox.getSelectedIndex() == SERVER_SET) { |
||||||
|
printSettings.setValid(false); |
||||||
|
} |
||||||
|
return printSettings; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected String title4PopupWindow() { |
||||||
|
return Inter.getLocText("FR-Designer_Print_Setting"); |
||||||
|
} |
||||||
|
} |
@ -1,122 +0,0 @@ |
|||||||
package com.fr.design.gui.frpane; |
|
||||||
|
|
||||||
import com.fr.base.BaseUtils; |
|
||||||
import com.fr.base.background.ImageBackground; |
|
||||||
import com.fr.design.dialog.BasicPane; |
|
||||||
import com.fr.design.gui.ibutton.UIButton; |
|
||||||
import com.fr.design.gui.ilable.UILabel; |
|
||||||
import com.fr.design.layout.FRGUIPaneFactory; |
|
||||||
import com.fr.design.style.background.image.ImageFileChooser; |
|
||||||
import com.fr.design.style.background.image.ImagePreviewPane; |
|
||||||
import com.fr.general.Inter; |
|
||||||
|
|
||||||
import javax.swing.*; |
|
||||||
import javax.swing.event.ChangeEvent; |
|
||||||
import javax.swing.event.ChangeListener; |
|
||||||
import java.awt.*; |
|
||||||
import java.awt.event.ActionEvent; |
|
||||||
import java.awt.event.ActionListener; |
|
||||||
import java.io.File; |
|
||||||
|
|
||||||
public class ImgChoosePane extends BasicPane { |
|
||||||
|
|
||||||
private ImagePreviewPane previewPane; |
|
||||||
private UIButton chooseButton; |
|
||||||
private UIButton clearButton; |
|
||||||
private UILabel imgSizeLabel; |
|
||||||
private ImageFileChooser imageFileChooser; |
|
||||||
|
|
||||||
public ImgChoosePane() { |
|
||||||
this.initComponents(); |
|
||||||
} |
|
||||||
|
|
||||||
protected void initComponents() { |
|
||||||
this.setLayout(FRGUIPaneFactory.createBorderLayout()); |
|
||||||
|
|
||||||
JPanel previewContainner = new JPanel(); |
|
||||||
previewContainner.setBorder(BorderFactory.createTitledBorder(Inter.getLocText("Preview"))); |
|
||||||
|
|
||||||
previewPane = new ImagePreviewPane(); |
|
||||||
previewContainner.add(previewPane); |
|
||||||
previewPane.addChangeListener(new ChangeListener() { |
|
||||||
@Override |
|
||||||
public void stateChanged(ChangeEvent evt) { |
|
||||||
Image image = ((ImagePreviewPane) evt.getSource()).getImage(); |
|
||||||
|
|
||||||
if (image == null) { |
|
||||||
imgSizeLabel.setText(""); |
|
||||||
} else { |
|
||||||
// imgSizeLabel.setText(Inter.getLocText("Size") + ": " + image.getWidth(null) + "px"
|
|
||||||
// + image.getHeight(null) + Inter.getLocText("px"));
|
|
||||||
imgSizeLabel.setText(Inter.getLocText(new String[]{"Size", "px", "px"}, |
|
||||||
new String[]{": " + image.getWidth(null), image.getHeight(null) + ""})); |
|
||||||
} |
|
||||||
} |
|
||||||
}); |
|
||||||
|
|
||||||
this.add(previewContainner, BorderLayout.CENTER); |
|
||||||
|
|
||||||
JPanel choosePane = new JPanel(new BorderLayout(0, 10)); |
|
||||||
choosePane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0)); |
|
||||||
JPanel choosePane1 = new JPanel(new BorderLayout(0, 10)); |
|
||||||
|
|
||||||
initButton(); |
|
||||||
choosePane.add(chooseButton, BorderLayout.NORTH); |
|
||||||
|
|
||||||
choosePane1.add(clearButton,BorderLayout.NORTH); |
|
||||||
choosePane.add(choosePane1,BorderLayout.CENTER); |
|
||||||
|
|
||||||
imgSizeLabel = new UILabel(); |
|
||||||
imgSizeLabel.setHorizontalAlignment(SwingConstants.CENTER); |
|
||||||
choosePane1.add(imgSizeLabel,BorderLayout.CENTER); |
|
||||||
this.add(choosePane,BorderLayout.EAST); |
|
||||||
|
|
||||||
imageFileChooser = new ImageFileChooser(); |
|
||||||
} |
|
||||||
|
|
||||||
private void initButton() { |
|
||||||
chooseButton = new UIButton(Inter.getLocText("Image-Select_Picture")); |
|
||||||
chooseButton.addActionListener(new ActionListener() { |
|
||||||
public void actionPerformed(ActionEvent e) { |
|
||||||
|
|
||||||
int returnVal = imageFileChooser.showOpenDialog(ImgChoosePane.this); |
|
||||||
if (returnVal != JFileChooser.CANCEL_OPTION) { |
|
||||||
File selectedFile = imageFileChooser.getSelectedFile(); |
|
||||||
if (selectedFile != null && selectedFile.isFile()) { |
|
||||||
Image image = BaseUtils.readImage(selectedFile.getPath()); |
|
||||||
previewPane.setImage(image); |
|
||||||
previewPane.repaint(); |
|
||||||
} else { |
|
||||||
previewPane.setImage(null); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
}); |
|
||||||
|
|
||||||
clearButton = new UIButton(Inter.getLocText("Clear")); |
|
||||||
clearButton.addActionListener(new ActionListener() { |
|
||||||
public void actionPerformed(ActionEvent e) { |
|
||||||
previewPane.setImage(null); |
|
||||||
previewPane.repaint(); |
|
||||||
} |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
public void populate(ImageBackground background) { |
|
||||||
if(background != null && background.getImage() != null) { |
|
||||||
previewPane.setImage(background.getImage()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public ImageBackground update() { |
|
||||||
if(previewPane.getImage() == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
return new ImageBackground(previewPane.getImage()); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
protected String title4PopupWindow() { |
|
||||||
return Inter.getLocText("Image"); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,145 @@ |
|||||||
|
package com.fr.design.gui.frpane; |
||||||
|
|
||||||
|
import com.fr.base.BaseUtils; |
||||||
|
import com.fr.base.Style; |
||||||
|
import com.fr.design.gui.ilable.UILabel; |
||||||
|
import com.fr.design.style.background.image.ImageFileChooser; |
||||||
|
import com.fr.design.style.background.image.ImagePreviewer; |
||||||
|
import com.fr.design.utils.ImageUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.stable.CoreGraphHelper; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
|
||||||
|
import javax.swing.JFileChooser; |
||||||
|
import javax.swing.SwingWorker; |
||||||
|
import javax.swing.event.ChangeEvent; |
||||||
|
import javax.swing.event.ChangeListener; |
||||||
|
import java.awt.Image; |
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
/** |
||||||
|
* 图片选择框包装类 |
||||||
|
* Created by zack on 2018/3/9. |
||||||
|
*/ |
||||||
|
public class ImgChooseWrapper { |
||||||
|
private ImagePreviewer previewPane = null; |
||||||
|
|
||||||
|
private ImageFileChooser imageFileChooser = null; |
||||||
|
|
||||||
|
private Style imageStyle = null; |
||||||
|
|
||||||
|
private SwingWorker<Void, Void> imageWorker; |
||||||
|
private ChangeListener changeListener; |
||||||
|
|
||||||
|
private transient Image selectImage; |
||||||
|
private UILabel imageSizeLabel; |
||||||
|
|
||||||
|
public static ImgChooseWrapper getInstance(ImagePreviewer previewPane, ImageFileChooser imageFileChooser, Style imageStyle) { |
||||||
|
return getInstance(previewPane, imageFileChooser, imageStyle, null); |
||||||
|
} |
||||||
|
|
||||||
|
public static ImgChooseWrapper getInstance(ImagePreviewer previewPane, ImageFileChooser imageFileChooser, Style imageStyle, ChangeListener changeListener) { |
||||||
|
return new ImgChooseWrapper(previewPane, imageFileChooser, imageStyle, changeListener, null, null); |
||||||
|
} |
||||||
|
|
||||||
|
public static ImgChooseWrapper getInstance(Image selectImage, UILabel imageSizeLabel, ImageFileChooser imageFileChooser) { |
||||||
|
return new ImgChooseWrapper(null, imageFileChooser, null, null, selectImage, imageSizeLabel); |
||||||
|
} |
||||||
|
|
||||||
|
private ImgChooseWrapper(ImagePreviewer previewPane, ImageFileChooser imageFileChooser, Style imageStyle, ChangeListener changeListener, Image selectImage, UILabel imageSizeLabel) { |
||||||
|
this.previewPane = previewPane; |
||||||
|
this.imageFileChooser = imageFileChooser; |
||||||
|
this.imageStyle = imageStyle; |
||||||
|
this.changeListener = changeListener; |
||||||
|
this.selectImage = selectImage; |
||||||
|
this.imageSizeLabel = imageSizeLabel; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public void setPreviewPane(ImagePreviewer previewPane) { |
||||||
|
this.previewPane = previewPane; |
||||||
|
} |
||||||
|
|
||||||
|
public ImageFileChooser getImageFileChooser() { |
||||||
|
return imageFileChooser; |
||||||
|
} |
||||||
|
|
||||||
|
public void setImageFileChooser(ImageFileChooser imageFileChooser) { |
||||||
|
this.imageFileChooser = imageFileChooser; |
||||||
|
} |
||||||
|
|
||||||
|
public Style getImageStyle() { |
||||||
|
return imageStyle; |
||||||
|
} |
||||||
|
|
||||||
|
public void setImageStyle(Style imageStyle) { |
||||||
|
this.imageStyle = imageStyle; |
||||||
|
} |
||||||
|
|
||||||
|
public SwingWorker<Void, Void> getImageWorker() { |
||||||
|
return imageWorker; |
||||||
|
} |
||||||
|
|
||||||
|
public void setImageWorker(SwingWorker<Void, Void> imageWorker) { |
||||||
|
this.imageWorker = imageWorker; |
||||||
|
} |
||||||
|
|
||||||
|
public void dealWithImageFile(int returnVal) { |
||||||
|
if (returnVal != JFileChooser.CANCEL_OPTION) { |
||||||
|
final File selectedFile = imageFileChooser.getSelectedFile(); |
||||||
|
|
||||||
|
if (selectedFile != null && selectedFile.isFile()) { |
||||||
|
if (previewPane != null) { |
||||||
|
previewPane.showLoading(); |
||||||
|
} |
||||||
|
if (imageWorker != null && !imageWorker.isDone()) { |
||||||
|
imageWorker = null; |
||||||
|
} |
||||||
|
imageWorker = new SwingWorker<Void, Void>() { |
||||||
|
@Override |
||||||
|
protected Void doInBackground() throws Exception { |
||||||
|
Image image = imageFileChooser.isCheckSelected() ? ImageUtils.defaultImageCompress(selectedFile) : BaseUtils.readImage(selectedFile.getPath()); |
||||||
|
CoreGraphHelper.waitForImage(image); |
||||||
|
|
||||||
|
if (previewPane != null) { |
||||||
|
previewPane.setImageStyle(imageStyle); |
||||||
|
previewPane.setImage(image); |
||||||
|
previewPane.repaint(); |
||||||
|
} |
||||||
|
checkLabelText(); |
||||||
|
fireChangeListener(); |
||||||
|
return null; |
||||||
|
} |
||||||
|
}; |
||||||
|
imageWorker.execute(); |
||||||
|
} else { |
||||||
|
if (previewPane != null) { |
||||||
|
previewPane.setImage(null); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
if (previewPane != null) { |
||||||
|
previewPane.repaint(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void fireChangeListener() { |
||||||
|
if (this.changeListener != null) { |
||||||
|
ChangeEvent evt = new ChangeEvent(this); |
||||||
|
this.changeListener.stateChanged(evt); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void checkLabelText() { |
||||||
|
if (imageSizeLabel == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
if (selectImage == null) { |
||||||
|
imageSizeLabel.setText(StringUtils.EMPTY); |
||||||
|
} else { |
||||||
|
imageSizeLabel.setText(selectImage.getWidth(null) + "x" |
||||||
|
+ selectImage.getHeight(null) + Inter.getLocText("px")); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Before Width: | Height: | Size: 442 B After Width: | Height: | Size: 314 B |
@ -0,0 +1,196 @@ |
|||||||
|
package com.fr.design.style.background.image; |
||||||
|
|
||||||
|
import com.fr.base.BaseUtils; |
||||||
|
import com.fr.design.DesignerEnvManager; |
||||||
|
import com.fr.design.gui.ibutton.UIButton; |
||||||
|
import com.fr.design.gui.icheckbox.UICheckBox; |
||||||
|
import com.fr.design.layout.FRGUIPaneFactory; |
||||||
|
import com.fr.design.utils.gui.GUICoreUtils; |
||||||
|
import com.fr.general.Inter; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
|
||||||
|
import javax.swing.JDialog; |
||||||
|
import javax.swing.JFileChooser; |
||||||
|
import javax.swing.JPanel; |
||||||
|
import javax.swing.plaf.metal.MetalFileChooserUI; |
||||||
|
import java.awt.BorderLayout; |
||||||
|
import java.awt.Component; |
||||||
|
import java.awt.Container; |
||||||
|
import java.awt.Dimension; |
||||||
|
import java.awt.Insets; |
||||||
|
import java.awt.LayoutManager; |
||||||
|
import java.awt.event.ActionListener; |
||||||
|
|
||||||
|
/** |
||||||
|
* 扩展的文件选择框(底部控制区域扩展一个复选框) |
||||||
|
* Created by zack on 2018/3/8. |
||||||
|
*/ |
||||||
|
public class ExpandFileChooser extends JFileChooser { |
||||||
|
private JDialog dialog; |
||||||
|
private UICheckBox checkBox;//选择框底部的复选按钮
|
||||||
|
private int retVal = ERROR_OPTION; |
||||||
|
private UIButton approve; |
||||||
|
private UIButton cancel; |
||||||
|
private static final int DEFAULT_WIDTH = 520; |
||||||
|
|
||||||
|
public ExpandFileChooser() { |
||||||
|
this(StringUtils.EMPTY, StringUtils.EMPTY); |
||||||
|
} |
||||||
|
|
||||||
|
public ExpandFileChooser(String checkBoxText, String approveButtonText) { |
||||||
|
JPanel previewContainerPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||||
|
MetalFileChooserUI chooserUI = (MetalFileChooserUI) getUI(); |
||||||
|
String approveText = StringUtils.isEmpty(approveButtonText) ? chooserUI.getApproveButtonText(this) : approveButtonText; |
||||||
|
dialog = new JDialog(); |
||||||
|
|
||||||
|
dialog.setSize(new Dimension(DEFAULT_WIDTH, 362)); |
||||||
|
dialog.add(previewContainerPane); |
||||||
|
dialog.setIconImage(BaseUtils.readImage("/com/fr/base/images/oem/logo.png")); |
||||||
|
dialog.setTitle(approveText); |
||||||
|
previewContainerPane.add(this, BorderLayout.CENTER); |
||||||
|
|
||||||
|
|
||||||
|
JPanel bottomControlPanel = new JPanel(); |
||||||
|
bottomControlPanel.setLayout(new ImageAreaLayout()); |
||||||
|
bottomControlPanel.setPreferredSize(new Dimension(DEFAULT_WIDTH, 40)); |
||||||
|
|
||||||
|
approve = new UIButton(approveText); |
||||||
|
cancel = new UIButton(Inter.getLocText("FR-Designer_Button-Cancel")); |
||||||
|
if (StringUtils.isNotEmpty(checkBoxText)) { |
||||||
|
checkBox = new UICheckBox(checkBoxText); |
||||||
|
checkBox.setSelected(DesignerEnvManager.getEnvManager().isImageCompress()); |
||||||
|
bottomControlPanel.add(checkBox); |
||||||
|
checkBox.addActionListener(checkAction()); |
||||||
|
} |
||||||
|
bottomControlPanel.add(approve); |
||||||
|
approve.addActionListener(chooserUI.getApproveSelectionAction()); |
||||||
|
cancel.addActionListener(chooserUI.getCancelSelectionAction()); |
||||||
|
bottomControlPanel.add(cancel); |
||||||
|
previewContainerPane.add(bottomControlPanel, BorderLayout.SOUTH); |
||||||
|
GUICoreUtils.centerWindow(dialog); |
||||||
|
} |
||||||
|
|
||||||
|
public ActionListener checkAction() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isCheckSelected() { |
||||||
|
if (checkBox != null) { |
||||||
|
return checkBox.isSelected(); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public int showDialog(Component parent, String approveButtonText) { |
||||||
|
dialog.setModal(true); |
||||||
|
dialog.setVisible(true); |
||||||
|
return retVal; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void approveSelection() { |
||||||
|
retVal = APPROVE_OPTION; |
||||||
|
if (dialog != null) { |
||||||
|
dialog.setVisible(false); |
||||||
|
} |
||||||
|
fireActionPerformed(APPROVE_SELECTION); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void cancelSelection() { |
||||||
|
retVal = CANCEL_OPTION; |
||||||
|
if (dialog != null) { |
||||||
|
dialog.setVisible(false); |
||||||
|
} |
||||||
|
fireActionPerformed(CANCEL_SELECTION); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean getControlButtonsAreShown() { |
||||||
|
return false;//隐藏默认的控制按钮(打开/取消)
|
||||||
|
} |
||||||
|
|
||||||
|
private class ImageAreaLayout implements LayoutManager { |
||||||
|
private static final int TEN = 10; |
||||||
|
private int hGap = 5; |
||||||
|
private int topMargin = TEN; |
||||||
|
private int leftMargin = TEN; |
||||||
|
private int leftStart = 8; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void addLayoutComponent(String string, Component comp) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void layoutContainer(Container container) { |
||||||
|
Component[] children = container.getComponents(); |
||||||
|
|
||||||
|
if (children != null && children.length > 0) { |
||||||
|
int numChildren = children.length; |
||||||
|
Dimension[] sizes = new Dimension[numChildren]; |
||||||
|
Insets insets = container.getInsets(); |
||||||
|
int yLocation = insets.top + topMargin; |
||||||
|
int maxWidth = 0; |
||||||
|
|
||||||
|
for (int counter = 0; counter < numChildren; counter++) { |
||||||
|
sizes[counter] = children[counter].getPreferredSize(); |
||||||
|
maxWidth = Math.max(maxWidth, sizes[counter].width); |
||||||
|
} |
||||||
|
int xLocation, xOffset; |
||||||
|
if (container.getComponentOrientation().isLeftToRight()) { |
||||||
|
xLocation = container.getSize().width - insets.left - maxWidth - leftMargin; |
||||||
|
xOffset = hGap + maxWidth; |
||||||
|
} else { |
||||||
|
xLocation = insets.left; |
||||||
|
xOffset = -(hGap + maxWidth); |
||||||
|
} |
||||||
|
//单独设置图片压缩按钮的位置
|
||||||
|
children[0].setBounds(leftStart, yLocation, |
||||||
|
maxWidth, sizes[0].height); |
||||||
|
|
||||||
|
for (int counter = numChildren - 1; counter >= 1; counter--) { |
||||||
|
children[counter].setBounds(xLocation, yLocation, |
||||||
|
maxWidth, sizes[counter].height); |
||||||
|
xLocation -= xOffset; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Dimension minimumLayoutSize(Container c) { |
||||||
|
if (c != null) { |
||||||
|
Component[] children = c.getComponents(); |
||||||
|
|
||||||
|
if (children != null && children.length > 0) { |
||||||
|
int numChildren = children.length; |
||||||
|
int height = 0; |
||||||
|
Insets cInsets = c.getInsets(); |
||||||
|
int extraHeight = topMargin + cInsets.top + cInsets.bottom; |
||||||
|
int extraWidth = cInsets.left + cInsets.right; |
||||||
|
int maxWidth = 0; |
||||||
|
|
||||||
|
for (int counter = 0; counter < numChildren; counter++) { |
||||||
|
Dimension aSize = children[counter].getPreferredSize(); |
||||||
|
height = Math.max(height, aSize.height); |
||||||
|
maxWidth = Math.max(maxWidth, aSize.width); |
||||||
|
} |
||||||
|
return new Dimension(extraWidth + numChildren * maxWidth + |
||||||
|
(numChildren - 1) * hGap, |
||||||
|
extraHeight + height); |
||||||
|
} |
||||||
|
} |
||||||
|
return new Dimension(0, 0); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Dimension preferredLayoutSize(Container c) { |
||||||
|
return minimumLayoutSize(c); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void removeLayoutComponent(Component c) { |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
package com.fr.design.style.background.image; |
||||||
|
|
||||||
|
import com.fr.base.Style; |
||||||
|
|
||||||
|
import java.awt.Image; |
||||||
|
|
||||||
|
/** |
||||||
|
* 图片预览接口(由于子类上层父类差别较大,无奈的接口) |
||||||
|
* Created by zack on 2018/3/10. |
||||||
|
*/ |
||||||
|
public interface ImagePreviewer { |
||||||
|
/** |
||||||
|
* 设置图片样式(平铺,拉伸) |
||||||
|
* @param style 样式 |
||||||
|
*/ |
||||||
|
void setImageStyle(Style style); |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置图片 |
||||||
|
* @param image 图片 |
||||||
|
*/ |
||||||
|
void setImage(Image image); |
||||||
|
|
||||||
|
/** |
||||||
|
* 显示正在加载 |
||||||
|
*/ |
||||||
|
void showLoading(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 重绘 |
||||||
|
*/ |
||||||
|
void repaint(); |
||||||
|
} |
@ -0,0 +1,178 @@ |
|||||||
|
package com.fr.design.utils; |
||||||
|
|
||||||
|
import com.fr.base.BaseUtils; |
||||||
|
import com.fr.general.ComparatorUtils; |
||||||
|
import com.fr.general.FRLogger; |
||||||
|
import com.fr.stable.StringUtils; |
||||||
|
|
||||||
|
import javax.imageio.IIOImage; |
||||||
|
import javax.imageio.ImageIO; |
||||||
|
import javax.imageio.ImageReader; |
||||||
|
import javax.imageio.ImageWriteParam; |
||||||
|
import javax.imageio.ImageWriter; |
||||||
|
import javax.imageio.stream.ImageInputStream; |
||||||
|
import javax.imageio.stream.ImageOutputStream; |
||||||
|
import java.awt.Graphics; |
||||||
|
import java.awt.image.BufferedImage; |
||||||
|
import java.io.ByteArrayInputStream; |
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Iterator; |
||||||
|
|
||||||
|
/** |
||||||
|
* 设计器部分的图片处理工具类 |
||||||
|
* Created by zack on 2018/3/8. |
||||||
|
*/ |
||||||
|
public class ImageUtils { |
||||||
|
|
||||||
|
/** |
||||||
|
* 默认压缩算法,采用75%质量压缩 |
||||||
|
* |
||||||
|
* @param imageFile 原文件 |
||||||
|
* @return 压缩后的BufferedImage对象 |
||||||
|
*/ |
||||||
|
public static BufferedImage defaultImageCompress(File imageFile) { |
||||||
|
try { |
||||||
|
return imageCompress(imageFile, 0.75f);//默认75%质量
|
||||||
|
} catch (IOException e) { |
||||||
|
FRLogger.getLogger().info("image compress failed!"); |
||||||
|
|
||||||
|
} |
||||||
|
return BaseUtils.readImage(imageFile.getPath()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 图片压缩 |
||||||
|
* |
||||||
|
* @param imageFile 原文件 |
||||||
|
* @param quality 质量系数(0-1) |
||||||
|
* @return 压缩后的图片 |
||||||
|
* @throws IOException |
||||||
|
*/ |
||||||
|
public static BufferedImage imageCompress(File imageFile, float quality) throws IOException { |
||||||
|
BufferedImage result = BaseUtils.readImage(imageFile.getPath()); |
||||||
|
if (imageFile == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
if (canbeCompressedToJPEG(imageFile)) { |
||||||
|
return jpegCompress(result, quality); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean canbeCompressedToJPEG(File imageFile) { |
||||||
|
if (ComparatorUtils.equals(getImageType(imageFile), "JPEG")) {//JPEG大写
|
||||||
|
return true; |
||||||
|
} |
||||||
|
if (ComparatorUtils.equals(getImageType(imageFile), "png")) {//png小写
|
||||||
|
return !isAlphaAreaOverload(imageFile);//少量透明度系数的png直接压缩jpg
|
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* JPEG格式图片压缩 |
||||||
|
* |
||||||
|
* @param image 压缩源图片 |
||||||
|
* @param quality 压缩质量,在0-1之间, |
||||||
|
* @return 返回的字节数组 |
||||||
|
*/ |
||||||
|
public static BufferedImage jpegCompress(BufferedImage image, float quality) throws IOException { |
||||||
|
if (image == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
// 得到指定Format图片的writer
|
||||||
|
Iterator<ImageWriter> iter = ImageIO |
||||||
|
.getImageWritersByFormatName("jpeg");// 得到迭代器
|
||||||
|
ImageWriter writer = iter.next(); |
||||||
|
|
||||||
|
// 得到指定writer的输出参数设置(ImageWriteParam )
|
||||||
|
ImageWriteParam iwp = writer.getDefaultWriteParam(); |
||||||
|
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 设置可否压缩
|
||||||
|
iwp.setCompressionQuality(quality); // 设置压缩质量参数
|
||||||
|
iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED); |
||||||
|
|
||||||
|
// 开始打包图片,写入byte[]
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
||||||
|
ImageOutputStream ios = null; |
||||||
|
ByteArrayInputStream in = null; |
||||||
|
try { |
||||||
|
image = copy(image, BufferedImage.TYPE_INT_RGB); |
||||||
|
ios = ImageIO.createImageOutputStream(byteArrayOutputStream); |
||||||
|
writer.setOutput(ios); |
||||||
|
writer.write(null, new IIOImage(image, null, null), iwp); |
||||||
|
in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); |
||||||
|
if (ios == null || in == null) { |
||||||
|
throw new IOException("The image file cannot be compressed"); |
||||||
|
} |
||||||
|
return ImageIO.read(in); |
||||||
|
} finally { |
||||||
|
writer.dispose(); |
||||||
|
ios.close(); |
||||||
|
byteArrayOutputStream.close(); |
||||||
|
in.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 判断图片中是否包含多于5%的透明区域,这个5%随便定的 |
||||||
|
* |
||||||
|
* @param imageFile 目标图片 |
||||||
|
* @return 含透明度像素占比 |
||||||
|
*/ |
||||||
|
public static boolean isAlphaAreaOverload(File imageFile) { |
||||||
|
if (imageFile == null || !imageFile.isFile()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
BufferedImage bufferedImage = BaseUtils.readImage(imageFile.getPath()); |
||||||
|
int width = bufferedImage.getWidth(); |
||||||
|
int height = bufferedImage.getHeight(); |
||||||
|
long alphaCount = 0; |
||||||
|
for (int i = 0; i < width; i++) { |
||||||
|
for (int j = 0; j < height; j++) { |
||||||
|
int rgb = bufferedImage.getRGB(i, j); |
||||||
|
int a = (0xff & (rgb >> 24)); |
||||||
|
if (a != 0xff) { |
||||||
|
alphaCount++; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return alphaCount / (double) (width * height) > 0.05; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取图片类型 |
||||||
|
* |
||||||
|
* @param imageFile 图片文件 |
||||||
|
* @return 图片类型(JPEG, PNG, GIF) |
||||||
|
*/ |
||||||
|
public static String getImageType(File imageFile) { |
||||||
|
try { |
||||||
|
ImageInputStream iis = ImageIO.createImageInputStream(imageFile); |
||||||
|
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); |
||||||
|
if (!iter.hasNext()) { |
||||||
|
return StringUtils.EMPTY; |
||||||
|
} |
||||||
|
ImageReader reader = iter.next(); |
||||||
|
iis.close(); |
||||||
|
return reader.getFormatName(); |
||||||
|
} catch (IOException ignore) { |
||||||
|
} |
||||||
|
return StringUtils.EMPTY; |
||||||
|
} |
||||||
|
|
||||||
|
private static BufferedImage copy(BufferedImage img, int imageType) { |
||||||
|
int width = img.getWidth(); |
||||||
|
int height = img.getHeight(); |
||||||
|
|
||||||
|
BufferedImage newImage = new BufferedImage(width, height, imageType); |
||||||
|
Graphics g = newImage.createGraphics(); |
||||||
|
|
||||||
|
g.drawImage(img, 0, 0, null); |
||||||
|
|
||||||
|
g.dispose(); |
||||||
|
|
||||||
|
return newImage; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue