package com.fr.plugin.external.design; import com.fr.base.BaseUtils; import com.fr.base.Style; import com.fr.design.border.UIRoundedBorder; import com.fr.design.constants.UIConstants; import com.fr.design.event.UIObserverListener; import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.ibutton.UIButtonGroup; import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.mainframe.DesignerContext; import com.fr.design.mainframe.backgroundpane.BackgroundQuickPane; import com.fr.design.mainframe.backgroundpane.ImagePreviewPane; import com.fr.general.Background; import com.fr.general.GeneralUtils; import com.fr.plugin.external.ExternalImageBackground; import com.fr.plugin.external.ImageFileChooser; import com.fr.plugin.external.ImageManager; import com.fr.plugin.external.ImageUtils; import com.fr.stable.Constants; import com.fr.stable.CoreGraphHelper; import com.fr.stable.StringUtils; import javax.swing.JFileChooser; import javax.swing.JPanel; import javax.swing.SwingWorker; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.lang.reflect.Method; import java.util.UUID; /** * @author zhou * @since 2012-5-29下午1:12:06 */ public class ExternalImageBackgroundQuickPane extends BackgroundQuickPane { private ImagePreviewPane previewPane; private Style imageStyle = null; private ChangeListener changeListener = null; private ImageFileChooser imageFileChooser; private UIButtonGroup imageLayoutPane; private String cachedId; private SwingWorker imageWorker; public ExternalImageBackgroundQuickPane() { this(true); } public ExternalImageBackgroundQuickPane(boolean hasImageLayout) { this.setLayout(new BorderLayout(0, 4)); String[] nameArray = {com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Background_Image_Default"), com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Background_Image_Titled"), com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Background_Image_Extend"), com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Background_Image_Adjust")}; Byte[] valueArray = {Constants.IMAGE_CENTER, Constants.IMAGE_TILED, Constants.IMAGE_EXTEND, Constants.IMAGE_ADJUST}; imageLayoutPane = new UIButtonGroup(nameArray, valueArray); imageLayoutPane.setSelectedIndex(0); previewPane = new ImagePreviewPane(); JPanel borderPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); borderPane.setBorder(new UIRoundedBorder(UIConstants.LINE_COLOR, 1, 5)); borderPane.setPreferredSize(new Dimension(0, 145)); borderPane.add(previewPane, BorderLayout.CENTER); this.add(borderPane, BorderLayout.NORTH); previewPane.addChangeListener(imageSizeChangeListener); JPanel southPane = new JPanel(new BorderLayout(0, 4)); JPanel contentPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); contentPane.add(southPane, BorderLayout.NORTH); this.add(contentPane, BorderLayout.CENTER); JPanel selectFilePane = new JPanel(new GridLayout(0, 2)); UIButton selectPictureButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Image_Select_Picture")); selectFilePane.add(new JPanel()); selectFilePane.add(selectPictureButton); selectPictureButton.addActionListener(selectPictureActionListener); if (hasImageLayout) { southPane.add(imageLayoutPane, BorderLayout.CENTER); } southPane.add(selectFilePane, BorderLayout.SOUTH); imageLayoutPane.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { imageStyle = Style.DEFAULT_STYLE.deriveImageLayout(imageLayoutPane.getSelectedItem()); previewPane.setImageStyle(imageStyle); previewPane.repaint(); } }); } /** * Select picture. */ ActionListener selectPictureActionListener = new ActionListener() { public void actionPerformed(ActionEvent evt) { if (imageFileChooser == null) { String compressText = com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Image_Compress"); try { GeneralUtils.classForName("com.fr.design.utils.ImageUtils"); } catch (ClassNotFoundException e) { compressText = StringUtils.EMPTY; } imageFileChooser = new ImageFileChooser(compressText); imageFileChooser.setMultiSelectionEnabled(false); } int returnVal = imageFileChooser.showOpenDialog(DesignerContext.getDesignerFrame()); if (returnVal != JFileChooser.CANCEL_OPTION) { final File selectedFile = imageFileChooser.getSelectedFile(); if (selectedFile != null && selectedFile.isFile()) { if (previewPane != null) { Class cls = null; try { cls = Class.forName("com.fr.design.mainframe.backgroundpane.ImagePreviewPane"); Method loading = cls.getMethod("showLoading"); loading.invoke(previewPane); } catch (Exception ignore) { } } if (imageWorker != null && !imageWorker.isDone()) { imageWorker = null; } imageWorker = new SwingWorker() { @Override protected Void doInBackground() throws Exception { Image image = imageFileChooser.isCompressSelected() ? ImageUtils.defaultImageCompress(selectedFile) : BaseUtils.readImage(selectedFile.getPath()); CoreGraphHelper.waitForImage(image); // if (cachedId == null) { // cachedId = UUID.randomUUID().toString(); // } // pioneer:POB-197,POB-388 复制的报表块修改外置背景图的时候用的同一个id,导致复制报表块和原始报表块背景被同步修改了。 // 这里去掉cachedId的null判断,让每次编辑都生成一个新的cachedId避免这个问题 cachedId = UUID.randomUUID().toString(); previewPane.setImage(image); imageStyle = Style.DEFAULT_STYLE.deriveImageLayout(imageLayoutPane.getSelectedItem()); previewPane.setImageStyle(imageStyle); previewPane.repaint(); fireChagneListener(); return null; } }; imageWorker.execute(); } else { previewPane.setImage(null); } } } }; public void populateBean(Background background) { ExternalImageBackground imageBackground = (ExternalImageBackground) background; this.cachedId = imageBackground.getId(); imageLayoutPane.setSelectedItem((byte) imageBackground.getLayout()); Style.DEFAULT_STYLE.deriveImageLayout(imageBackground.getLayout()); previewPane.setImageStyle(ExternalImageBackgroundQuickPane.this.imageStyle); previewPane.setImage(imageBackground.getImage()); previewPane.repaint(); } public Background updateBean() { ExternalImageBackground imageBackground = new ExternalImageBackground(previewPane.getImage(), this.cachedId); imageStyle = Style.DEFAULT_STYLE.deriveImageLayout(imageLayoutPane.getSelectedItem()); imageBackground.setLayout(imageStyle.getImageLayout()); if (this.cachedId != null) { ImageManager.getInstance().updateImage(imageBackground); } return imageBackground; } /** * 给组件登记一个观察者监听事件 * * @param listener 观察者监听事件 */ public void registerChangeListener(final UIObserverListener listener) { changeListener = new ChangeListener() { public void stateChanged(ChangeEvent e) { listener.doChange(); } }; imageLayoutPane.addChangeListener(changeListener); } private void fireChagneListener() { if (this.changeListener != null) { ChangeEvent evt = new ChangeEvent(this); this.changeListener.stateChanged(evt); } } ChangeListener imageSizeChangeListener = new ChangeListener() { public void stateChanged(ChangeEvent evt) { if (imageLayoutPane.getSelectedItem() != null) { imageStyle = Style.DEFAULT_STYLE.deriveImageLayout(imageLayoutPane.getSelectedItem()); previewPane.setImageStyle(imageStyle); previewPane.repaint(); } } }; /** * 判断是否是图片背景 * * @param background 背景 * @return 判断是否是图片背景 */ public boolean accept(Background background) { return background instanceof ExternalImageBackground; } /** * 标题 * * @return 标题 */ public String title4PopupWindow() { return "外置图片"; } public void reset() { this.cachedId = null; imageLayoutPane.setSelectedIndex(0); previewPane.setImage(null); } }