diff --git a/jar library/swing-glasspane-popup-1.3.0.jar b/jar library/swing-glasspane-popup-1.4.0.jar similarity index 57% rename from jar library/swing-glasspane-popup-1.3.0.jar rename to jar library/swing-glasspane-popup-1.4.0.jar index dcd86dc..f0c928b 100644 Binary files a/jar library/swing-glasspane-popup-1.3.0.jar and b/jar library/swing-glasspane-popup-1.4.0.jar differ diff --git a/pom.xml b/pom.xml index 16958a2..1426b00 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ raven.popup swing-glasspane-popup - 1.3.0 + 1.4.0 1.8 diff --git a/src/main/java/raven/drawer/component/SimpleDrawerBuilder.java b/src/main/java/raven/drawer/component/SimpleDrawerBuilder.java index f7e76df..dc561d5 100644 --- a/src/main/java/raven/drawer/component/SimpleDrawerBuilder.java +++ b/src/main/java/raven/drawer/component/SimpleDrawerBuilder.java @@ -78,7 +78,6 @@ public int getDrawerWidth() { } public void build(DrawerPanel drawerPanel) { - } public void rebuildMenu() { diff --git a/src/main/java/raven/drawer/component/menu/MenuEvent.java b/src/main/java/raven/drawer/component/menu/MenuEvent.java index 52eb06d..c075c99 100644 --- a/src/main/java/raven/drawer/component/menu/MenuEvent.java +++ b/src/main/java/raven/drawer/component/menu/MenuEvent.java @@ -2,5 +2,5 @@ public interface MenuEvent { - public void selected(MenuAction action, int index, int subIndex); + public void selected(MenuAction action, int[] index); } diff --git a/src/main/java/raven/drawer/component/menu/MenuLayout.java b/src/main/java/raven/drawer/component/menu/MenuLayout.java index c7ee284..8e20445 100644 --- a/src/main/java/raven/drawer/component/menu/MenuLayout.java +++ b/src/main/java/raven/drawer/component/menu/MenuLayout.java @@ -1,8 +1,5 @@ package raven.drawer.component.menu; -import com.formdev.flatlaf.util.UIScale; -import raven.drawer.DrawerOption; - import java.awt.*; public class MenuLayout implements LayoutManager { @@ -19,7 +16,7 @@ public void removeLayoutComponent(Component comp) { public Dimension preferredLayoutSize(Container parent) { synchronized (parent.getTreeLock()) { Insets insets = parent.getInsets(); - // use parent width to avoid the issues right-to-left scroll pane + // Use parent width to avoid the issues right-to-left scroll pane int width = parent.getParent().getWidth(); int height = insets.top + insets.bottom; int count = parent.getComponentCount(); diff --git a/src/main/java/raven/drawer/component/menu/MenuValidation.java b/src/main/java/raven/drawer/component/menu/MenuValidation.java index dc796a6..2cd4588 100644 --- a/src/main/java/raven/drawer/component/menu/MenuValidation.java +++ b/src/main/java/raven/drawer/component/menu/MenuValidation.java @@ -14,7 +14,7 @@ public MenuValidation(boolean keepMenuValidationIndex, boolean removeLabelWhenEm this.removeLabelWhenEmptyMenu = removeLabelWhenEmptyMenu; } - public boolean menuValidation(int index, int subIndex) { + public boolean menuValidation(int[] index) { return true; } diff --git a/src/main/java/raven/drawer/component/menu/SimpleMenu.java b/src/main/java/raven/drawer/component/menu/SimpleMenu.java index e227c0a..06dd7b4 100644 --- a/src/main/java/raven/drawer/component/menu/SimpleMenu.java +++ b/src/main/java/raven/drawer/component/menu/SimpleMenu.java @@ -4,11 +4,15 @@ import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.util.ColorFunctions; import com.formdev.flatlaf.util.UIScale; +import raven.drawer.component.menu.data.Item; +import raven.drawer.component.menu.data.MenuItem; +import raven.popup.GlassPanePopup; import raven.utils.FlatLafStyleUtils; import javax.swing.*; import java.awt.*; import java.awt.geom.Path2D; +import java.util.Arrays; public class SimpleMenu extends JPanel { @@ -33,38 +37,53 @@ private void init() { } private void buildMenu() { - String menus[][] = simpleMenuOption.menus; + MenuItem[] menus = simpleMenuOption.menus; if (menus != null) { int index = 0; int validationIndex = -1; for (int i = 0; i < menus.length; i++) { - String menu[] = menus[i]; - if (menu.length > 0) { - String label = checkLabel(menu); - if (label != null) { - if (checkLabelValidation(i, index)) { - add(createLabel(label)); + MenuItem menuItem = menus[i]; + if (menuItem.isMenu()) { + Item item = (Item) menuItem; + int[] arrIndex = {index}; + if (item.isSubmenuAble()) { + // Create submenu + int[] arrValidationIndex = {++validationIndex}; + boolean validation = simpleMenuOption.menuValidation.menuValidation(copyArray(arrValidationIndex)); + if (validation) { + add(createSubmenuItem(item, arrIndex, arrValidationIndex, 0)); + } + if (validation || simpleMenuOption.menuValidation.keepMenuValidationIndex) { + index++; } } else { - boolean validation = simpleMenuOption.menuValidation.menuValidation(++validationIndex, 0); + // Create single menu item + int[] arrValidationIndex = {++validationIndex}; + boolean validation = simpleMenuOption.menuValidation.menuValidation(arrValidationIndex); if (validation) { - if (menu.length == 1) { - JButton button = createMenuItem(menu[0], index); - applyMenuEvent(button, index, 0); - add(button); - } else { - add(createSubmenuItem(menu, index, validationIndex)); - } + JButton button = createMenuItem(item.getName(), item.getIcon(), arrIndex, 0); + applyMenuEvent(button, arrIndex); + add(button); } if (validation || simpleMenuOption.menuValidation.keepMenuValidationIndex) { index++; } } + } else { + // Create label + if (checkLabelValidation(i, index)) { + Item.Label label = (Item.Label) menuItem; + add(createLabel(label.getName())); + } } } } } + private int[] copyArray(int[] arr) { + return Arrays.copyOf(arr, arr.length); + } + private String getBasePath() { if (simpleMenuOption.baseIconPath == null) { return ""; @@ -76,18 +95,31 @@ private String getBasePath() { } } - protected JButton createMenuItem(String name, int index) { - JButton button = new JButton(name); - if (simpleMenuOption.icons != null && index < simpleMenuOption.icons.length) { + protected Icon getIcon(String icon, int menuLevel) { + if (icon != null) { String path = getBasePath(); - Icon icon = simpleMenuOption.buildMenuIcon(path + simpleMenuOption.icons[index], simpleMenuOption.iconScale); - if (icon != null) { - button.setIcon(icon); + float iconScale; + if (menuLevel < simpleMenuOption.iconScale.length) { + iconScale = simpleMenuOption.iconScale[menuLevel]; + } else { + iconScale = simpleMenuOption.iconScale[simpleMenuOption.iconScale.length - 1]; } + Icon iconObject = simpleMenuOption.buildMenuIcon(path + icon, iconScale); + return iconObject; + } else { + return null; + } + } + + protected JButton createMenuItem(String name, String icon, int[] index, int menuLevel) { + JButton button = new JButton(name); + Icon iconObject = getIcon(icon, menuLevel); + if (iconObject != null) { + button.setIcon(iconObject); } button.setHorizontalAlignment(JButton.LEADING); if (simpleMenuOption.simpleMenuStyle != null) { - simpleMenuOption.simpleMenuStyle.styleMenuItem(button, index); + simpleMenuOption.simpleMenuStyle.styleMenuItem(button, copyArray(index)); } FlatLafStyleUtils.appendStyleIfAbsent(button, "" + "arc:0;" + @@ -100,51 +132,42 @@ protected JButton createMenuItem(String name, int index) { return button; } - protected void applyMenuEvent(JButton button, int index, int subIndex) { + protected void applyMenuEvent(JButton button, int[] index) { button.addActionListener(e -> { - MenuAction action = runEvent(index, subIndex); + MenuAction action = runEvent(index); if (action != null) { - // Next update on action menu selected + // Next update on action menu selected } }); } - private MenuAction runEvent(int index, int subIndex) { + private MenuAction runEvent(int[] index) { if (!simpleMenuOption.events.isEmpty()) { MenuAction action = new MenuAction(); if (simpleMenuOption.menuItemAutoSelect) { action.selected(); } for (MenuEvent event : simpleMenuOption.events) { - event.selected(action, index, subIndex); + event.selected(action, copyArray(index)); } return action; } return null; } - protected Component createSubmenuItem(String menu[], int index, int validationIndex) { - JPanel panelItem = new SubMenuItem(menu, index, validationIndex); + protected Component createSubmenuItem(Item menu, int index[], int[] validationIndex, int menuLevel) { + JPanel panelItem = new SubMenuItem(menu, index, validationIndex, menuLevel); return panelItem; } - protected String checkLabel(String menu[]) { - String label = menu[0]; - if (label.startsWith("~") && label.endsWith("~")) { - return label.substring(1, label.length() - 1); - } else { - return null; - } - } - protected boolean checkLabelValidation(int labelIndex, int menuIndex) { if (simpleMenuOption.menuValidation.labelValidation(labelIndex)) { if (simpleMenuOption.menuValidation.removeLabelWhenEmptyMenu) { boolean fondMenu = false; for (int i = labelIndex + 1; i < simpleMenuOption.menus.length; i++) { - String label = checkLabel(simpleMenuOption.menus[i]); - if (label == null) { - if (simpleMenuOption.menuValidation.menuValidation(menuIndex, 0)) { + MenuItem menuItem = simpleMenuOption.menus[i]; + if (menuItem.isMenu()) { + if (simpleMenuOption.menuValidation.menuValidation(new int[]{menuIndex})) { fondMenu = true; break; } @@ -180,46 +203,66 @@ public SimpleMenuOption getSimpleMenuOption() { protected class SubMenuItem extends JPanel { + private int menuLevel; + private int levelSpace = 18; private SubmenuLayout menuLayout; private boolean menuShow; - private final String menu[]; - private final int index; - private final int validationIndex; + private final Item menu; + private final int[] index; + private final int[] validationIndex; private int iconWidth; public void setAnimate(float animate) { menuLayout.setAnimate(animate); } - public SubMenuItem(String menu[], int index, int validationIndex) { + public SubMenuItem(Item menu, int[] index, int[] validationIndex, int menuLevel) { this.menu = menu; this.index = index; this.validationIndex = validationIndex; + this.menuLevel = menuLevel; init(); } private void init() { menuLayout = new SubmenuLayout(); setLayout(menuLayout); + // Use opaque true on the first submenu panel to fix g2d draw arrow line + setOpaque(menuLevel == 0); putClientProperty(FlatClientProperties.STYLE, "" + "background:null"); iconWidth = 22; int index = 0; int validationIndex = -1; - for (int i = 0; i < menu.length; i++) { - boolean validation = simpleMenuOption.menuValidation.menuValidation(this.validationIndex, ++validationIndex); + int nextMenuLevel = menuLevel + 1; + // Create menu item + JButton mainButton; + if (menuLevel == 0) { + // Create first level menu item + mainButton = createMenuItem(menu.getName(), menu.getIcon(), this.index, menuLevel); + } else { + int addSpace = menuLevel > 1 ? (menuLevel - 1) * levelSpace : 0; + mainButton = createSubMenuItem(menu.getName(), this.index, iconWidth + addSpace); + } + if (mainButton.getIcon() != null) { + iconWidth = UIScale.unscale(mainButton.getIcon().getIconWidth()); + } + createMainMenuEvent(mainButton); + applyMenuEvent(mainButton, this.index); + add(mainButton); + for (int i = 0; i < menu.getSubMenu().size(); i++) { + int[] arrIndex = createArrayIndex(this.index, index); + int[] arrValidationIndex = createArrayIndex(this.validationIndex, ++validationIndex); + boolean validation = simpleMenuOption.menuValidation.menuValidation(copyArray(arrValidationIndex)); if (validation) { - if (i == 0) { - JButton button = createMenuItem(menu[i], this.index); - if (button.getIcon() != null) { - iconWidth = UIScale.unscale(button.getIcon().getIconWidth()); - } - createMainMenuEvent(button); - applyMenuEvent(button, this.index, index); - add(button); + Item item = menu.getSubMenu().get(i); + if (item.isSubmenuAble()) { + add(createSubmenuItem(item, arrIndex, arrValidationIndex, nextMenuLevel)); } else { - JButton button = createSubMenuItem(menu[i], index, iconWidth); - applyMenuEvent(button, this.index, index); + // Create single menu item + int addSpace = menuLevel * levelSpace; + JButton button = createSubMenuItem(item.getName(), arrIndex, iconWidth + addSpace); + applyMenuEvent(button, arrIndex); add(button); } } @@ -229,6 +272,15 @@ private void init() { } } + private int[] createArrayIndex(int[] index, int subIndex) { + int[] newArr = new int[index.length + 1]; + for (int i = 0; i < index.length; i++) { + newArr[i] = index[i]; + } + newArr[newArr.length - 1] = subIndex; + return newArr; + } + private void createMainMenuEvent(JButton button) { button.addActionListener(e -> { menuShow = !menuShow; @@ -237,15 +289,17 @@ private void createMainMenuEvent(JButton button) { } - protected JButton createSubMenuItem(String name, int index, int gap) { + protected JButton createSubMenuItem(String name, int[] index, int gap) { JButton button = new JButton(name); button.setHorizontalAlignment(JButton.LEADING); if (simpleMenuOption.simpleMenuStyle != null) { - simpleMenuOption.simpleMenuStyle.styleSubMenuItem(button, this.index, index); + simpleMenuOption.simpleMenuStyle.styleMenuItem(button, copyArray(index)); } + boolean ltr = GlassPanePopup.isInit() == false || GlassPanePopup.getMainFrame().getComponentOrientation().isLeftToRight(); + String margin = ltr ? ("7," + (gap + 25) + ",7,30") : ("7,30,7," + (gap + 25)); FlatLafStyleUtils.appendStyleIfAbsent(button, "" + "arc:0;" + - "margin:7," + (gap + 25) + ",7," + (gap + 25) + ";" + + "margin:" + margin + ";" + "borderWidth:0;" + "focusWidth:0;" + "innerFocusWidth:0;" + @@ -264,10 +318,10 @@ protected void paintChildren(Graphics g) { int height = getHeight(); Graphics2D g2 = (Graphics2D) g.create(); FlatUIUtils.setRenderingHints(g2); - // Create submenu line + // Create submenu line int last = getLastLocation(); int round = UIScale.scale(8); - int gap = UIScale.scale(20 + (iconWidth / 2)); + int gap = UIScale.scale((20 + (iconWidth / 2)) + (levelSpace * menuLevel)); Path2D.Double p = new Path2D.Double(); int x = ltr ? gap : width - gap; p.moveTo(x, menuHeight); @@ -275,14 +329,19 @@ protected void paintChildren(Graphics g) { int count = getComponentCount(); for (int i = 1; i < count; i++) { Component com = getComponent(i); - int y = com.getY() + (com.getHeight() / 2); + int y; + if (com instanceof SubMenuItem) { + y = com.getY() + ((SubMenuItem) com).getFirstItemLocation(); + } else { + y = com.getY() + (com.getHeight() / 2); + } p.append(createCurve(round, x, y, ltr), false); } Color color = ColorFunctions.mix(getBackground(), foreground, 0.7f); g2.setColor(color); g2.setStroke(new BasicStroke(UIScale.scale(1f))); g2.draw(p); - // Create arrow + // Create arrow paintArrow(g2, width, menuHeight, menuLayout.getAnimate(), ltr); g2.dispose(); } @@ -290,7 +349,19 @@ protected void paintChildren(Graphics g) { private int getLastLocation() { Component com = getComponent(getComponentCount() - 1); - return com.getY() + com.getHeight() / 2; + if (com instanceof SubMenuItem) { + SubMenuItem subMenuItem = (SubMenuItem) com; + return com.getY() + subMenuItem.getFirstItemLocation(); + } else { + return com.getY() + com.getHeight() / 2; + } + } + + private int getFirstItemLocation() { + if (getComponentCount() == 0) { + return 0; + } + return getComponent(0).getHeight() / 2; } private Shape createCurve(int round, int x, int y, boolean ltr) { diff --git a/src/main/java/raven/drawer/component/menu/SimpleMenuOption.java b/src/main/java/raven/drawer/component/menu/SimpleMenuOption.java index 145fae2..9066819 100644 --- a/src/main/java/raven/drawer/component/menu/SimpleMenuOption.java +++ b/src/main/java/raven/drawer/component/menu/SimpleMenuOption.java @@ -1,6 +1,7 @@ package raven.drawer.component.menu; import com.formdev.flatlaf.extras.FlatSVGIcon; +import raven.drawer.component.menu.data.MenuItem; import javax.swing.*; import java.util.ArrayList; @@ -11,23 +12,18 @@ public class SimpleMenuOption { protected List events = new ArrayList<>(); protected MenuValidation menuValidation = new MenuValidation(); protected SimpleMenuStyle simpleMenuStyle; - protected String menus[][]; - protected String icons[]; - protected float iconScale = 1f; + protected MenuItem menus[]; + protected float iconScale[] = {1f}; + protected String baseIconPath; protected boolean menuItemAutoSelect = true; - public SimpleMenuOption setMenus(String menus[][]) { + public SimpleMenuOption setMenus(MenuItem menus[]) { this.menus = menus; return this; } - public SimpleMenuOption setIcons(String icons[]) { - this.icons = icons; - return this; - } - - public SimpleMenuOption setIconScale(float iconScale) { + public SimpleMenuOption setIconScale(float... iconScale) { this.iconScale = iconScale; return this; } diff --git a/src/main/java/raven/drawer/component/menu/SimpleMenuStyle.java b/src/main/java/raven/drawer/component/menu/SimpleMenuStyle.java index 498f4e7..9ce006e 100644 --- a/src/main/java/raven/drawer/component/menu/SimpleMenuStyle.java +++ b/src/main/java/raven/drawer/component/menu/SimpleMenuStyle.java @@ -7,10 +7,7 @@ public abstract class SimpleMenuStyle { public void styleMenu(JComponent component) { } - public void styleMenuItem(JButton menu, int index) { - } - - public void styleSubMenuItem(JButton subMenu, int index, int subIndex) { + public void styleMenuItem(JButton menu, int[] index) { } public void styleLabel(JLabel label) { diff --git a/src/main/java/raven/drawer/component/menu/data/Item.java b/src/main/java/raven/drawer/component/menu/data/Item.java new file mode 100644 index 0000000..1cf3175 --- /dev/null +++ b/src/main/java/raven/drawer/component/menu/data/Item.java @@ -0,0 +1,76 @@ +package raven.drawer.component.menu.data; + +import java.util.ArrayList; +import java.util.List; + +public class Item implements MenuItem { + + public String getName() { + return name; + } + + public String getIcon() { + return icon; + } + + public List getSubMenu() { + return subMenu; + } + + private final String name; + private String icon; + private List subMenu; + + public Item(String name) { + this.name = name; + } + + public Item(String name, String icon) { + this.name = name; + this.icon = icon; + } + + public Item subMenu(Item item) { + addSubMenu(item); + return this; + } + + public Item subMenu(String name) { + addSubMenu(new Item(name)); + return this; + } + + private void addSubMenu(Item item) { + if (subMenu == null) { + subMenu = new ArrayList<>(); + } + subMenu.add(item); + } + + public boolean isSubmenuAble() { + return subMenu != null; + } + + @Override + public boolean isMenu() { + return true; + } + + public static class Label implements MenuItem { + + public String getName() { + return name; + } + + private String name; + + public Label(String name) { + this.name = name; + } + + @Override + public boolean isMenu() { + return false; + } + } +} diff --git a/src/main/java/raven/drawer/component/menu/data/MenuItem.java b/src/main/java/raven/drawer/component/menu/data/MenuItem.java new file mode 100644 index 0000000..df13356 --- /dev/null +++ b/src/main/java/raven/drawer/component/menu/data/MenuItem.java @@ -0,0 +1,6 @@ +package raven.drawer.component.menu.data; + +public interface MenuItem { + + boolean isMenu(); +} diff --git a/src/main/java/raven/popup/DefaultOption.java b/src/main/java/raven/popup/DefaultOption.java index 353b350..70d1ade 100644 --- a/src/main/java/raven/popup/DefaultOption.java +++ b/src/main/java/raven/popup/DefaultOption.java @@ -1,6 +1,7 @@ package raven.popup; import com.formdev.flatlaf.FlatLaf; +import com.formdev.flatlaf.util.UIScale; import java.awt.*; @@ -37,7 +38,7 @@ public boolean blockBackground() { @Override public Color background() { - return FlatLaf.isLafDark()?new Color(50, 50, 50):new Color(20, 20, 20); + return FlatLaf.isLafDark() ? new Color(50, 50, 50) : new Color(20, 20, 20); } @Override diff --git a/src/main/java/raven/popup/GlassPanePopup.java b/src/main/java/raven/popup/GlassPanePopup.java index cdfe7ff..ebc57b1 100644 --- a/src/main/java/raven/popup/GlassPanePopup.java +++ b/src/main/java/raven/popup/GlassPanePopup.java @@ -11,6 +11,7 @@ public class GlassPanePopup { private static GlassPanePopup instance; + private JFrame mainFrame; protected WindowSnapshots windowSnapshots; protected JLayeredPane layerPane; protected Container contentPane; @@ -50,6 +51,7 @@ private void updateLayout() { public static void install(JFrame frame) { instance = new GlassPanePopup(); + instance.mainFrame = frame; instance.windowSnapshots = new WindowSnapshots(frame); instance.contentPane = frame.getContentPane(); frame.setGlassPane(instance.layerPane); @@ -197,6 +199,14 @@ public static int getPopupCount() { return instance.layerPane.getComponentCount(); } + public static JFrame getMainFrame() { + return instance.mainFrame; + } + + public static boolean isInit() { + return !(instance == null || instance.mainFrame == null); + } + protected synchronized void removePopup(Component popup) { layerPane.remove(popup); if (layerPane.getComponentCount() == 0) { diff --git a/src/main/java/raven/popup/GlassPopup.java b/src/main/java/raven/popup/GlassPopup.java index 0dbf2dc..57ff16d 100644 --- a/src/main/java/raven/popup/GlassPopup.java +++ b/src/main/java/raven/popup/GlassPopup.java @@ -109,7 +109,7 @@ public void end() { } } }); - animator.setInterpolator(CubicBezierEasing.EASE_IN_OUT); + animator.setInterpolator(CubicBezierEasing.EASE); } public void setShowPopup(boolean show) { diff --git a/src/main/java/raven/popup/component/ComponentLayer.java b/src/main/java/raven/popup/component/ComponentLayer.java index f8a3827..6dc1471 100644 --- a/src/main/java/raven/popup/component/ComponentLayer.java +++ b/src/main/java/raven/popup/component/ComponentLayer.java @@ -4,6 +4,7 @@ import com.formdev.flatlaf.util.Animator; import com.formdev.flatlaf.util.CubicBezierEasing; import com.formdev.flatlaf.util.UIScale; +import net.miginfocom.swing.MigLayout; import javax.swing.*; import java.awt.*; @@ -21,7 +22,6 @@ public class ComponentLayer extends JPanel { private Image image; private Image nextImage; - private PopupLayout popupLayout; private Stack componentStack; private boolean push; @@ -38,8 +38,8 @@ public ComponentLayer(GlassPaneChild component) { } private void init() { - popupLayout = new PopupLayout(); - setLayout(popupLayout); + MigLayout layout = new MigLayout("insets 0,fill", "fill", "fill"); + setLayout(layout); if (component.getRoundBorder() > 0) { setOpaque(false); component.setOpaque(false); @@ -49,29 +49,17 @@ private void init() { } private void initAnimator() { - animator = new Animator(250, new Animator.TimingTarget() { - - private boolean layoutChang; + animator = new Animator(350, new Animator.TimingTarget() { @Override public void timingEvent(float v) { animate = v; - if (layoutChang) { - revalidate(); - repaint(); - } else { - repaint(); - } + repaint(); } @Override public void begin() { nextImage = ComponentImageUtils.createImage(nextComponent); - popupLayout.snapshotLayout(false); - Dimension from = component.getPreferredSize(); - Dimension target = nextComponent.getPreferredSize(); - layoutChang = from.width != target.width || from.height != target.height; - popupLayout.set(from, target); } @Override @@ -87,7 +75,7 @@ public void end() { } } }); - animator.setInterpolator(CubicBezierEasing.EASE_IN_OUT); + animator.setInterpolator(CubicBezierEasing.EASE); } private void startAnimate() { @@ -129,26 +117,18 @@ private void showComponent(GlassPaneChild component) { component.setVisible(false); remove(this.component); add(component); - popupLayout.snapshotLayout(true); + revalidate(); startAnimate(); } - public void showSnapshot() { if (animator != null && animator.isRunning()) { animator.stop(); } simpleSnapshot = true; - revalidate(); - if (component.isVisible()) { - image = ComponentImageUtils.createImage(component, component.getRoundBorder()); - component.setVisible(false); - } else { - EventQueue.invokeLater(() -> { - image = ComponentImageUtils.createImage(component, component.getRoundBorder()); - component.setVisible(false); - }); - } + doLayout(); + image = ComponentImageUtils.createImage(component, component.getRoundBorder()); + component.setVisible(false); } public void hideSnapshot() { @@ -220,66 +200,4 @@ public void paint(Graphics g) { public GlassPaneChild getComponent() { return component; } - - private class PopupLayout implements LayoutManager { - - private Dimension from; - private Dimension target; - private boolean snapshotLayout; - - public void snapshotLayout(boolean snapshotLayout) { - this.snapshotLayout = snapshotLayout; - revalidate(); - } - - public void set(Dimension from, Dimension target) { - this.from = from; - this.target = target; - } - - private Dimension getSize() { - double width = from.width + ((target.width - from.width) * animate); - double height = from.height + ((target.height - from.height) * animate); - return new Dimension((int) width, (int) height); - } - - @Override - public void addLayoutComponent(String name, Component comp) { - } - - @Override - public void removeLayoutComponent(Component comp) { - } - - @Override - public Dimension preferredLayoutSize(Container parent) { - synchronized (parent.getTreeLock()) { - if (animate != 0) { - return getSize(); - } else { - return component.getPreferredSize(); - } - } - } - - @Override - public Dimension minimumLayoutSize(Container parent) { - synchronized (parent.getTreeLock()) { - return new Dimension(0, 0); - } - } - - @Override - public void layoutContainer(Container parent) { - synchronized (parent.getTreeLock()) { - int width = parent.getWidth(); - int height = parent.getHeight(); - int count = parent.getComponentCount(); - for (int i = 0; i < count; i++) { - Component component = parent.getComponent(i); - component.setBounds(0, 0, width, height); - } - } - } - } } diff --git a/src/test/java/test/Demo.java b/src/test/java/test/Demo.java index f46c5a4..ee956dd 100644 --- a/src/test/java/test/Demo.java +++ b/src/test/java/test/Demo.java @@ -3,7 +3,6 @@ import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.fonts.roboto.FlatRobotoFont; import com.formdev.flatlaf.themes.FlatMacDarkLaf; -import com.formdev.flatlaf.themes.FlatMacLightLaf; import net.miginfocom.swing.MigLayout; import raven.drawer.Drawer; import raven.popup.GlassPanePopup; @@ -14,7 +13,7 @@ public class Demo extends JFrame { public Demo() { - // applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); + applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); GlassPanePopup.install(this); MyDrawerBuilder myDrawerBuilder = new MyDrawerBuilder(); Drawer.getInstance().setDrawerBuilder(myDrawerBuilder); @@ -33,7 +32,7 @@ public static void main(String[] args) { FlatRobotoFont.install(); FlatLaf.registerCustomDefaultsSource("themes"); UIManager.put("defaultFont", new Font(FlatRobotoFont.FAMILY, Font.PLAIN, 13)); - FlatMacLightLaf.setup(); + FlatMacDarkLaf.setup(); EventQueue.invokeLater(() -> new Demo().setVisible(true)); } } diff --git a/src/test/java/test/MyDrawerBuilder.java b/src/test/java/test/MyDrawerBuilder.java index 080f715..c978c2d 100644 --- a/src/test/java/test/MyDrawerBuilder.java +++ b/src/test/java/test/MyDrawerBuilder.java @@ -9,10 +9,13 @@ import raven.drawer.component.header.SimpleHeaderStyle; import raven.drawer.component.menu.*; import raven.drawer.component.SimpleDrawerBuilder; +import raven.drawer.component.menu.data.Item; +import raven.drawer.component.menu.data.MenuItem; import raven.swing.AvatarIcon; import javax.swing.*; import java.awt.*; +import java.util.Arrays; public class MyDrawerBuilder extends SimpleDrawerBuilder { @@ -64,32 +67,60 @@ public void styleDescription(JLabel label) { @Override public SimpleMenuOption getSimpleMenuOption() { - String menus[][] = { - {"~MAIN~"}, - {"Dashboard"}, - {"~WEB APP~"}, - {"Email", "Inbox", "Read", "Compost"}, - {"Chat"}, - {"Calendar"}, - {"~COMPONENT~"}, - {"Advanced UI", "Cropper", "Owl Carousel", "Sweet Alert"}, - {"Forms", "Basic Elements", "Advanced Elements", "SEditors", "Wizard"}, - {"~OTHER~"}, - {"Charts", "Apex", "Flot", "Sparkline"}, - {"Icons", "Feather Icons", "Flag Icons", "Mdi Icons"}, - {"Special Pages", "Blank page", "Faq", "Invoice", "Profile", "Pricing", "Timeline"}, - }; - String icons[] = { - "dashboard.svg", - "email.svg", - "chat.svg", - "calendar.svg", - "ui.svg", - "forms.svg", - "chart.svg", - "icon.svg", - "page.svg", + MenuItem items[] = new MenuItem[]{ + new Item.Label("MAIN"), + new Item("Dashboard", "dashboard.svg"), + new Item.Label("WEB APP"), + new Item("Email", "email.svg") + .subMenu("Inbox") + .subMenu( + new Item("Group Read") + .subMenu("Read 1") + .subMenu("Read 2") + .subMenu( + new Item("Group Item") + .subMenu("Item 1") + .subMenu("Item 2") + .subMenu("Item 3") + .subMenu("Item 4") + .subMenu("Item 5") + .subMenu("Item 6") + ) + .subMenu("Read 3") + .subMenu("Read 4") + .subMenu("Read 5") + ) + .subMenu("Compost"), + new Item("Chat", "chat.svg"), + new Item("Calendar", "calendar.svg"), + new Item.Label("COMPONENT"), + new Item("Advanced UI", "ui.svg") + .subMenu("Cropper") + .subMenu("Owl Carousel") + .subMenu("Sweet Alert"), + new Item("Forms", "forms.svg") + .subMenu("Basic Elements") + .subMenu("Advanced Elements") + .subMenu("SEditors") + .subMenu("Wizard"), + new Item.Label("OTHER"), + new Item("Charts", "chart.svg") + .subMenu("Apex") + .subMenu("Flot") + .subMenu("Sparkline"), + new Item("Icons", "icon.svg") + .subMenu("Feather Icons") + .subMenu("Flag Icons") + .subMenu("Mdi Icons"), + new Item("Special Pages", "page.svg") + .subMenu("Blank page") + .subMenu("Faq") + .subMenu("Invoice") + .subMenu("Profile") + .subMenu("Pricing") + .subMenu("Timeline") }; + SimpleMenuOption simpleMenuOption = new SimpleMenuOption() { @Override public Icon buildMenuIcon(String path, float scale) { @@ -102,23 +133,16 @@ public Icon buildMenuIcon(String path, float scale) { }; simpleMenuOption.addMenuEvent(new MenuEvent() { @Override - public void selected(MenuAction action, int index, int subIndex) { - System.out.println("Drawer menu selected " + index + " " + subIndex); + public void selected(MenuAction action, int[] index) { + System.out.println("Drawer menu selected " + Arrays.toString(index)); } }); simpleMenuOption.setMenuStyle(new SimpleMenuStyle() { @Override - public void styleMenuItem(JButton menu, int index) { + public void styleMenuItem(JButton menu, int[] index) { menu.putClientProperty(FlatClientProperties.STYLE, "" + "[light]foreground:#FAFAFA;" + - "arc:10"); - } - - @Override - public void styleSubMenuItem(JButton subMenu, int index, int subIndex) { - subMenu.putClientProperty(FlatClientProperties.STYLE, "" + - "[light]foreground:#FAFAFA;" + - "arc:10"); + "arc:0"); } @Override @@ -135,22 +159,26 @@ public void styleLabel(JLabel label) { } }); + simpleMenuOption.setMenuValidation(new MenuValidation() { @Override - public boolean menuValidation(int index, int subIndex) { - if (index == 3) { - return false; - } else if (index == 4 || index == 5) { - return false; - } else { - return true; + public boolean menuValidation(int[] index) { + if (index.length == 1) { + // Hide Calendar + if (index[0] == 3) { + return false; + } + } else if (index.length == 3) { + // Hide Read 4 + if (index[0] == 1 && index[1] == 1 && index[2] == 4) { + return false; + } } + return true; } }); - - simpleMenuOption.setMenus(menus) - .setIcons(icons) + simpleMenuOption.setMenus(items) .setBaseIconPath("icon") .setIconScale(0.45f); return simpleMenuOption; diff --git a/src/test/java/test/Test.java b/src/test/java/test/Test.java new file mode 100644 index 0000000..c194851 --- /dev/null +++ b/src/test/java/test/Test.java @@ -0,0 +1,57 @@ +package test; + +import raven.drawer.component.menu.data.Item; +import raven.drawer.component.menu.data.MenuItem; + +public class Test { + + public static void main(String[] args) { + + MenuItem items[] = new MenuItem[]{ + new Item.Label("MAIN"), + new Item("Dashboard"), + new Item.Label("WEB APP"), + new Item("Email") + .subMenu("Inbox") + .subMenu( + new Item("Read") + .subMenu("Read 1") + .subMenu("Read 2") + .subMenu( + new Item("Item") + .subMenu("Item 1") + .subMenu("Item 2") + ) + ) + .subMenu("Compost"), + new Item("Chat"), + new Item("Calendar"), + new Item.Label("COMPONENT"), + new Item("Advanced UI") + .subMenu("Cropper") + .subMenu("Owl Carousel") + .subMenu("Sweet Alert"), + new Item("Forms") + .subMenu("Basic Elements") + .subMenu("Advanced Elements") + .subMenu("SEditors") + .subMenu("Wizard"), + new Item.Label("OTHER"), + new Item("Charts") + .subMenu("Apex") + .subMenu("Flot") + .subMenu("Sparkline"), + new Item("Icons") + .subMenu("Feather Icons") + .subMenu("Flag Icons") + .subMenu("Mdi Icons"), + new Item("Special Pages") + .subMenu("Blank page") + .subMenu("Faq") + .subMenu("Invoice") + .subMenu("Profile") + .subMenu("Pricing") + .subMenu("Timeline") + }; + } +} diff --git a/src/test/java/test/TestSampleMessage.java b/src/test/java/test/TestSampleMessage.java index cb1f854..2cafb64 100644 --- a/src/test/java/test/TestSampleMessage.java +++ b/src/test/java/test/TestSampleMessage.java @@ -32,14 +32,110 @@ public TestSampleMessage() { "[light]foreground:lighten(@foreground,30%);" + "[dark]foreground:darken(@foreground,30%)"); txt.setEditable(false); - String actions[] = new String[]{"Cancel", "Ok"}; - GlassPanePopup.showPopup(new SimplePopupBorder(txt, "Sample Message",new SimplePopupBorderOption().useScroll(), actions, (controller, action) -> { - controller.closePopup(); - })); + String actions[] = new String[]{"Cancel", "Next"}; + GlassPanePopup.showPopup(new SimplePopupBorder(txt, "Sample Message", new SimplePopupBorderOption().useScroll(), actions, (controller, action) -> { + if (action == 1) { + String actions1[] = new String[]{"Back", "OK"}; + GlassPanePopup.push(new SimplePopupBorder(createSample(), "Test", new SimplePopupBorderOption().useScroll(),actions1, (controller1, action1) -> { + if (action1 == 0) { + GlassPanePopup.pop("pop"); + } + }), "pop"); + } else { + controller.closePopup(); + } + }), "pop"); }); add(cmd); } + private Component createSample() { + JPanel panel = new JPanel(new MigLayout("fill", "fill", "")); + JTextPane txt = new JTextPane(); + txt.setText("Apple\n" + + "Banana\n" + + "Orange\n" + + "Grapes\n" + + "Watermelon\n" + + "Pineapple\n" + + "Strawberry\n" + + "Blueberry\n" + + "Raspberry\n" + + "Mango\n" + + "Kiwi\n" + + "Papaya\n" + + "Peach\n" + + "Pear\n" + + "Plum\n" + + "Cherry\n" + + "Apricot\n" + + "Coconut\n" + + "Avocado\n" + + "Lemon\n" + + "Lime\n" + + "Grapefruit\n" + + "Cantaloupe\n" + + "Honeydew\n" + + "Fig\n" + + "Dates\n" + + "Pomegranate\n" + + "Cranberry\n" + + "Blackberry\n" + + "Goji berries\n" + + "Passionfruit\n" + + "Dragonfruit\n" + + "Starfruit\n" + + "Lychee\n" + + "Guava\n" + + "Persimmon\n" + + "Jackfruit\n" + + "Ackee\n" + + "Breadfruit\n" + + "Plantain\n" + + "Pine nuts\n" + + "Almonds\n" + + "Cashews\n" + + "Walnuts\n" + + "Pistachios\n" + + "Pecans\n" + + "Hazelnuts\n" + + "Macadamia nuts\n" + + "Chestnuts\n" + + "Brazil nuts\n" + + "Peanuts\n" + + "Sunflower seeds\n" + + "Pumpkin seeds\n" + + "Chia seeds\n" + + "Flaxseeds\n" + + "Sesame seeds\n" + + "Quinoa\n" + + "Bulgur\n" + + "Farro\n" + + "Barley\n" + + "Millet\n" + + "Buckwheat\n" + + "Oats\n" + + "Rice\n" + + "Couscous\n" + + "Wheatberries\n" + + "Spelt\n" + + "Teff\n" + + "Rye\n" + + "Corn\n" + + "Lentils\n" + + "Chickpeas\n" + + "Black beans\n" + + "Kidney beans\n" + + "Navy beans"); + txt.putClientProperty(FlatClientProperties.STYLE, "" + + "border:10,25,10,25;" + + "[light]foreground:lighten(@foreground,30%);" + + "[dark]foreground:darken(@foreground,30%)"); + txt.setEditable(false); + panel.add(txt); + return panel; + } + public static void main(String[] args) { FlatRobotoFont.install(); FlatLaf.registerCustomDefaultsSource("themes"); diff --git a/src/test/resources/icon/icon.png b/src/test/resources/icon/icon.png new file mode 100644 index 0000000..1ffd67c Binary files /dev/null and b/src/test/resources/icon/icon.png differ