From a6010ec64fea5089468f38d67f395e7e064735e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Sun, 2 Mar 2014 12:00:49 +0100 Subject: [PATCH] Issue #361 FFDec icon is not visible on application start --- .../decompiler/flash/gui/MainFrameRibbon.java | 135 +++++++++--------- .../decompiler/flash/gui/MyResizableIcon.java | 95 ++++++++++++ .../gui/MyRibbonApplicationMenuButtonUI.java | 68 ++++----- .../MyRibbonApplicationMenuPopupPanelUI.java | 1 - .../com/jpexs/decompiler/flash/gui/View.java | 11 ++ 5 files changed, 200 insertions(+), 110 deletions(-) create mode 100644 trunk/src/com/jpexs/decompiler/flash/gui/MyResizableIcon.java diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/MainFrameRibbon.java b/trunk/src/com/jpexs/decompiler/flash/gui/MainFrameRibbon.java index a581e0fce..b533a4e88 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/MainFrameRibbon.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/MainFrameRibbon.java @@ -159,81 +159,80 @@ public final class MainFrameRibbon extends AppRibbonFrame implements MainFrame { public void setVisible(boolean b) { super.setVisible(b); - final MainFrameRibbon t = this; + /* final MainFrameRibbon t = this; - //TODO: Handle this better. This is awful :-( - new Timer().schedule(new TimerTask() { - @Override - public void run() { - List mbuttons = new ArrayList<>(); - getApplicationMenuButtons(t, mbuttons); - if (mbuttons.size() < 2) { - //return, task will run again - return; - } + //TODO: Handle this better. This is awful :-( + new Timer().schedule(new TimerTask() { + @Override + public void run() { + List mbuttons = new ArrayList<>(); + getApplicationMenuButtons(t, mbuttons); + if (mbuttons.size() < 2) { + //return, task will run again + return; + } - for (final JRibbonApplicationMenuButton mbutton : mbuttons) { - mbutton.setIcon(View.getResizableIcon("buttonicon_256")); - mbutton.setDisplayState(new CommandButtonDisplayState( - "My Ribbon Application Menu Button", mbutton.getSize().width) { - @Override - public CommandButtonLayoutManager createLayoutManager( - AbstractCommandButton commandButton) { - return new CommandButtonLayoutManager() { - @Override - public int getPreferredIconSize() { - return mbutton.getSize().width; - } + for (final JRibbonApplicationMenuButton mbutton : mbuttons) { + mbutton.setIcon(View.getResizableIcon("buttonicon_256")); + mbutton.setDisplayState(new CommandButtonDisplayState( + "My Ribbon Application Menu Button", mbutton.getSize().width) { + @Override + public CommandButtonLayoutManager createLayoutManager( + AbstractCommandButton commandButton) { + return new CommandButtonLayoutManager() { + @Override + public int getPreferredIconSize() { + return mbutton.getSize().width; + } - @Override - public CommandButtonLayoutManager.CommandButtonLayoutInfo getLayoutInfo( - AbstractCommandButton commandButton, Graphics g) { - CommandButtonLayoutManager.CommandButtonLayoutInfo result = new CommandButtonLayoutManager.CommandButtonLayoutInfo(); - result.actionClickArea = new Rectangle(0, 0, 0, 0); - result.popupClickArea = new Rectangle(0, 0, commandButton - .getWidth(), commandButton.getHeight()); - result.popupActionRect = new Rectangle(0, 0, 0, 0); - ResizableIcon icon = commandButton.getIcon(); - icon.setDimension(new Dimension(commandButton.getWidth(), commandButton.getHeight())); - result.iconRect = new Rectangle( - 0, - 0, - commandButton.getWidth(), commandButton.getHeight()); - result.isTextInActionArea = false; - return result; - } + @Override + public CommandButtonLayoutManager.CommandButtonLayoutInfo getLayoutInfo( + AbstractCommandButton commandButton, Graphics g) { + CommandButtonLayoutManager.CommandButtonLayoutInfo result = new CommandButtonLayoutManager.CommandButtonLayoutInfo(); + result.actionClickArea = new Rectangle(0, 0, 0, 0); + result.popupClickArea = new Rectangle(0, 0, commandButton + .getWidth(), commandButton.getHeight()); + result.popupActionRect = new Rectangle(0, 0, 0, 0); + ResizableIcon icon = commandButton.getIcon(); + icon.setDimension(new Dimension(commandButton.getWidth(), commandButton.getHeight())); + result.iconRect = new Rectangle( + 0, + 0, + commandButton.getWidth(), commandButton.getHeight()); + result.isTextInActionArea = false; + return result; + } - @Override - public Dimension getPreferredSize( - AbstractCommandButton commandButton) { - return new Dimension(40, 40); - } + @Override + public Dimension getPreferredSize( + AbstractCommandButton commandButton) { + return new Dimension(40, 40); + } - @Override - public void propertyChange(PropertyChangeEvent evt) { - } + @Override + public void propertyChange(PropertyChangeEvent evt) { + } - @Override - public Point getKeyTipAnchorCenterPoint( - AbstractCommandButton commandButton) { - // dead center - return new Point(commandButton.getWidth() / 2, - commandButton.getHeight() / 2); - } - }; - } - }); - - MyRibbonApplicationMenuButtonUI mui = (MyRibbonApplicationMenuButtonUI) mbutton.getUI(); - mui.setHoverIcon(View.getResizableIcon("buttonicon_hover_256")); - mui.setNormalIcon(View.getResizableIcon("buttonicon_256")); - mui.setClickIcon(View.getResizableIcon("buttonicon_down_256")); - mbutton.repaint(); - } - cancel(); //cancel task so it does not run again - } - }, 1, 50); + @Override + public Point getKeyTipAnchorCenterPoint( + AbstractCommandButton commandButton) { + // dead center + return new Point(commandButton.getWidth() / 2, + commandButton.getHeight() / 2); + } + }; + } + }); + MyRibbonApplicationMenuButtonUI mui = (MyRibbonApplicationMenuButtonUI) mbutton.getUI(); + mui.setHoverIcon(View.getResizableIcon("buttonicon_hover_256")); + mui.setNormalIcon(View.getResizableIcon("buttonicon_256")); + mui.setClickIcon(View.getResizableIcon("buttonicon_down_256")); + mbutton.repaint(); + } + cancel(); //cancel task so it does not run again + } + }, 1, 50);*/ panel.setVisible(b); } diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/MyResizableIcon.java b/trunk/src/com/jpexs/decompiler/flash/gui/MyResizableIcon.java new file mode 100644 index 000000000..564fe6f78 --- /dev/null +++ b/trunk/src/com/jpexs/decompiler/flash/gui/MyResizableIcon.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2010-2014 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.decompiler.flash.gui; + +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.Map; +import javax.swing.Icon; +import org.pushingpixels.flamingo.internal.utils.FlamingoUtilities; + +/** + * + * @author JPEXS + */ +public class MyResizableIcon implements Icon { + + protected BufferedImage originalImage; + protected Map cachedImages = new HashMap<>(); + + public MyResizableIcon(BufferedImage originalImage) { + this.originalImage = originalImage; + width = originalImage.getWidth(); + height = originalImage.getHeight(); + } + + protected int width; + protected int height; + protected BufferedImage image; + + public void setDimension(Dimension dim) { + setIconSize(dim.width, dim.height); + } + + public void setIconSize(int renderWidth, int renderHeight) { + width = renderWidth; + height = renderHeight; + String key = renderWidth + ":" + renderHeight; + if (this.cachedImages.containsKey(key)) { + image = this.cachedImages.get(key); + return; + } + + BufferedImage result = originalImage; + float scaleX = (float) originalImage.getWidth() + / (float) renderWidth; + float scaleY = (float) originalImage.getHeight() + / (float) renderHeight; + + float scale = Math.max(scaleX, scaleY); + if (scale > 1.0f) { + int finalWidth = (int) (originalImage.getWidth() / scale); + result = FlamingoUtilities.createThumbnail(originalImage, + finalWidth); + } + cachedImages.put(renderWidth + ":" + renderHeight, result); + image = result; + } + + @Override + public void paintIcon(Component c, Graphics g, int x, int y) { + if (image != null) { + int dx = (this.width - image.getWidth()) / 2; + int dy = (this.height - image.getHeight()) / 2; + g.drawImage(image, x + dx, y + dy, null); + } + } + + @Override + public int getIconWidth() { + return width; + } + + @Override + public int getIconHeight() { + return height; + } + +} diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuButtonUI.java b/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuButtonUI.java index 3a8471897..cab07b99e 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuButtonUI.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuButtonUI.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.gui; +import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; @@ -24,8 +25,6 @@ import java.awt.image.BufferedImage; import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.plaf.ComponentUI; -import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon; -import org.pushingpixels.flamingo.api.common.icon.ResizableIcon; import org.pushingpixels.flamingo.api.common.model.PopupButtonModel; import org.pushingpixels.flamingo.internal.ui.ribbon.appmenu.BasicRibbonApplicationMenuButtonUI; import org.pushingpixels.flamingo.internal.ui.ribbon.appmenu.JRibbonApplicationMenuButton; @@ -54,33 +53,21 @@ import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities; public class MyRibbonApplicationMenuButtonUI extends BasicRibbonApplicationMenuButtonUI implements ActionPopupTransitionAwareUI { - private ImageWrapperResizableIcon hoverIcon = null; - private ImageWrapperResizableIcon clickIcon = null; - private ImageWrapperResizableIcon normalIcon = null; + private MyResizableIcon hoverIcon = null; + private MyResizableIcon clickIcon = null; + private MyResizableIcon normalIcon = null; - public void setNormalIcon(ImageWrapperResizableIcon normalIcon) { - this.normalIcon = normalIcon; - } - - public ImageWrapperResizableIcon getHoverIcon() { - return hoverIcon; - } - - public ImageWrapperResizableIcon getClickIcon() { + public MyResizableIcon getClickIcon() { return clickIcon; } - public ImageWrapperResizableIcon getNormalIcon() { - return normalIcon; + public MyRibbonApplicationMenuButtonUI() { + super(); + hoverIcon = View.getMyResizableIcon("buttonicon_hover_256"); + normalIcon = View.getMyResizableIcon("buttonicon_256"); + clickIcon = View.getMyResizableIcon("buttonicon_down_256"); } - public void setHoverIcon(ImageWrapperResizableIcon hoverIcon) { - this.hoverIcon = hoverIcon; - } - - public void setClickIcon(ImageWrapperResizableIcon clickIcon) { - this.clickIcon = clickIcon; - } /** * Model change listener for ghost image effects. */ @@ -128,6 +115,19 @@ public class MyRibbonApplicationMenuButtonUI extends BasicRibbonApplicationMenuB super.uninstallListeners(); } + private void updateIcons(JComponent c) { + + Dimension dim = c.getPreferredSize(); + + //Add border: + dim.width += 2; + dim.height += 2; + + hoverIcon.setDimension(dim); + clickIcon.setDimension(dim); + normalIcon.setDimension(dim); + } + /* * (non-Javadoc) * @@ -138,18 +138,9 @@ public class MyRibbonApplicationMenuButtonUI extends BasicRibbonApplicationMenuB public void paint(Graphics g, JComponent c) { JRibbonApplicationMenuButton b = (JRibbonApplicationMenuButton) c; - if (hoverIcon != null) { - hoverIcon.setPreferredSize(b.getSize()); - } - if (clickIcon != null) { - clickIcon.setPreferredSize(b.getSize()); - } - if (normalIcon != null) { - normalIcon.setPreferredSize(b.getSize()); - } + updateIcons(c); - this.layoutInfo = this.layoutManager.getLayoutInfo(this.commandButton, - g); + this.layoutInfo = this.layoutManager.getLayoutInfo(this.commandButton, g); commandButton.putClientProperty("icon.bounds", layoutInfo.iconRect); Graphics2D g2d = (Graphics2D) g.create(); @@ -163,12 +154,7 @@ public class MyRibbonApplicationMenuButtonUI extends BasicRibbonApplicationMenuB g2d.drawImage(fullAlphaBackground, 0, 0, null); // Paint the icon - ResizableIcon icon = getCurrentIcon(b); - /*if(icon instanceof ImageWrapperResizableIcon){ - ImageWrapperResizableIcon iw=(ImageWrapperResizableIcon)icon; - iw.setPreferredSize(b.getSize()); - iw.setDimension(b.getSize()); - }*/ + Icon icon = getCurrentIcon(b); if (icon != null) { int iconWidth = icon.getIconWidth(); @@ -181,7 +167,7 @@ public class MyRibbonApplicationMenuButtonUI extends BasicRibbonApplicationMenuB g2d.dispose(); } - private ResizableIcon getCurrentIcon(JRibbonApplicationMenuButton button) { + private Icon getCurrentIcon(JRibbonApplicationMenuButton button) { PopupButtonModel mod = button.getPopupModel(); if (mod.isPopupShowing()) { if (clickIcon != null) { diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuPopupPanelUI.java b/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuPopupPanelUI.java index c93c5cb74..561919a39 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuPopupPanelUI.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/MyRibbonApplicationMenuPopupPanelUI.java @@ -93,7 +93,6 @@ public class MyRibbonApplicationMenuPopupPanelUI extends BasicRibbonApplicationM rendererButton.setPopupKeyTip(appMenuButton.getPopupKeyTip()); rendererButton.getPopupModel().setPopupShowing(true); rendererButton.setDisplayState(appMenuButton.getDisplayState()); - rendererButton.setIcon(((MyRibbonApplicationMenuButtonUI) appMenuButton.getUI()).getClickIcon()); rendererButton.getPopupModel().setRollover(false); rendererButton.getPopupModel().setPressed(true); diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/View.java b/trunk/src/com/jpexs/decompiler/flash/gui/View.java index 830cf4a9f..409fd8f81 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/View.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/View.java @@ -28,11 +28,13 @@ import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import javax.imageio.ImageIO; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; @@ -277,6 +279,15 @@ public class View { return ImageWrapperResizableIcon.getIcon(View.class.getResource("/com/jpexs/decompiler/flash/gui/graphics/" + resource + ".png"), new Dimension(256, 256)); } + public static MyResizableIcon getMyResizableIcon(String resource) { + try { + return new MyResizableIcon(ImageIO.read(View.class.getResourceAsStream("/com/jpexs/decompiler/flash/gui/graphics/" + resource + ".png"))); + } catch (IOException ex) { + Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex); + return null; + } + } + public static void execInEventDispatch(Runnable r) { if (SwingUtilities.isEventDispatchThread()) { r.run();