add popup to building blocks in the toolbar

This commit is contained in:
bancika
2021-01-01 19:25:10 +01:00
parent 607f963ba7
commit 4144956a09
6 changed files with 42 additions and 27 deletions
Binary file not shown.
@@ -67,7 +67,7 @@ public enum IconLoader {
JarBeanInto("jar_bean_into.png"), GraphEdgeDirected("graph_edge_directed.png"), LaserPointer("laserpointer.png"), TapeMeasure("tape_measure1.png"), JarBeanInto("jar_bean_into.png"), GraphEdgeDirected("graph_edge_directed.png"), LaserPointer("laserpointer.png"), TapeMeasure("tape_measure1.png"),
Elements("elements1.png"), GraphNodes("graph_nodes.png"), User("user1.png"), FlexibleLeads("flexible_leads.png"), Pads("pads.png"), Lock("lock.png"), Elements("elements1.png"), GraphNodes("graph_nodes.png"), User("user1.png"), FlexibleLeads("flexible_leads.png"), Pads("pads.png"), Lock("lock.png"),
Unlock("lock_open.png"), Earth("earth.png"), Loadline("loadline.png"), LoadlineAdd("loadline_add.png"), SnapToGrid("snap_to_grid.png"), Unlock("lock_open.png"), Earth("earth.png"), Loadline("loadline.png"), LoadlineAdd("loadline_add.png"), SnapToGrid("snap_to_grid.png"),
SnapToComponents("snap_to_components.png"), SnapToNone("snap_to_none.png"), TraceProximity("trace_proximity.png"); SnapToComponents("snap_to_components.png"), SnapToNone("snap_to_none.png"), TraceProximity("trace_proximity.png"), ComponentLarge("component_large.png");
protected String name; protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

@@ -197,11 +197,11 @@ public class ComponentButtonFactory {
return item; return item;
} }
public static JButton createBuildingBlockButton(final IPlugInPort plugInPort, final String blockName) { public static JButton createBuildingBlockButton(final IPlugInPort plugInPort, final String blockName, final JPopupMenu menu) {
JButton button = new JButton(blockName, IconLoader.Component.getIcon()); JButton button = DropDownButtonFactory.createDropDownButton(blockName, IconLoader.ComponentLarge.getIcon(), menu);
button.setVerticalTextPosition(SwingConstants.BOTTOM); button.setVerticalTextPosition(SwingConstants.CENTER);
button.setHorizontalTextPosition(SwingConstants.CENTER); button.setHorizontalTextPosition(SwingConstants.LEFT);
button.setBorder(BorderFactory.createEmptyBorder(MARGIN + 1, MARGIN + 1, MARGIN, MARGIN)); button.setBorder(BorderFactory.createEmptyBorder(MARGIN + 1, MARGIN + 1, MARGIN, MARGIN));
button.setToolTipText("<html><b>" + blockName + "</b><br> building block</html>"); button.setToolTipText("<html><b>" + blockName + "</b><br> building block</html>");
@@ -37,6 +37,7 @@ import javax.swing.JMenuItem;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem; import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
@@ -55,8 +56,10 @@ import org.diylc.core.Template;
import org.diylc.lang.LangUtil; import org.diylc.lang.LangUtil;
import org.diylc.presenter.ComparatorFactory; import org.diylc.presenter.ComparatorFactory;
import org.diylc.presenter.ComponentProcessor; import org.diylc.presenter.ComponentProcessor;
import org.diylc.swing.ISwingUI;
import org.diylc.swing.images.IconLoader; import org.diylc.swing.images.IconLoader;
import org.diylc.swing.plugins.tree.TreePanel; import org.diylc.swing.plugins.tree.TreePanel;
import org.diylc.swing.plugins.tree.TreePanel.DeleteBlockAction;
import org.diylc.swing.plugins.tree.TreePanel.SelectAllAction; import org.diylc.swing.plugins.tree.TreePanel.SelectAllAction;
/** /**
@@ -73,6 +76,8 @@ class ComponentTabbedPane extends JTabbedPane {
public static String LAST_SELECTED_TAB = "lastSelectedTab"; public static String LAST_SELECTED_TAB = "lastSelectedTab";
private final IPlugInPort plugInPort; private final IPlugInPort plugInPort;
private ISwingUI swingUI;
private Container recentToolbar; private Container recentToolbar;
private Container buildingBlocksToolbar; private Container buildingBlocksToolbar;
private Container favoritesToolbar; private Container favoritesToolbar;
@@ -81,11 +86,12 @@ class ComponentTabbedPane extends JTabbedPane {
private List<Favorite> favorites; private List<Favorite> favorites;
private List<String> pendingRecentComponents = null; private List<String> pendingRecentComponents = null;
private List<String> blocks; private List<String> blocks;
public ComponentTabbedPane(IPlugInPort plugInPort) { public ComponentTabbedPane(IPlugInPort plugInPort, ISwingUI swingUI) {
super(); super();
this.plugInPort = plugInPort; this.plugInPort = plugInPort;
this.swingUI = swingUI;
initialize(); initialize();
@@ -221,7 +227,7 @@ class ComponentTabbedPane extends JTabbedPane {
for (ComponentType componentType : componentTypes) { for (ComponentType componentType : componentTypes) {
try { try {
Component button = ComponentButtonFactory.create(plugInPort, componentType, Component button = ComponentButtonFactory.create(plugInPort, componentType,
createVariantPopup(componentType)); createVariantPopup(componentType, null));
toolbar.add(button); toolbar.add(button);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Could not create recent component button for " + componentType.getName(), e); LOG.error("Could not create recent component button for " + componentType.getName(), e);
@@ -287,7 +293,7 @@ class ComponentTabbedPane extends JTabbedPane {
componentType = ComponentProcessor.getInstance().extractComponentTypeFrom( componentType = ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) Class.forName(componentClassName)); (Class<? extends IDIYComponent<?>>) Class.forName(componentClassName));
Component button = ComponentButtonFactory.create(plugInPort, componentType, Component button = ComponentButtonFactory.create(plugInPort, componentType,
createVariantPopup(componentType)); createVariantPopup(componentType, null));
toolbar.add(button); toolbar.add(button);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Could not create recent component button for " + componentClassName, e); LOG.error("Could not create recent component button for " + componentClassName, e);
@@ -299,7 +305,7 @@ class ComponentTabbedPane extends JTabbedPane {
Container toolbar = getBuildingBlocksToolbar(); Container toolbar = getBuildingBlocksToolbar();
toolbar.removeAll(); toolbar.removeAll();
for (String block : this.blocks) { for (String block : this.blocks) {
Component button = ComponentButtonFactory.createBuildingBlockButton(plugInPort, block); Component button = ComponentButtonFactory.createBuildingBlockButton(plugInPort, block, createVariantPopup(null, block));
toolbar.add(button); toolbar.add(button);
} }
} }
@@ -310,17 +316,17 @@ class ComponentTabbedPane extends JTabbedPane {
for (Favorite fav : this.favorites) { for (Favorite fav : this.favorites) {
Component button; Component button;
if (fav.getType() == FavoriteType.Block) { if (fav.getType() == FavoriteType.Block) {
button = ComponentButtonFactory.createBuildingBlockButton(plugInPort, fav.getName()); button = ComponentButtonFactory.createBuildingBlockButton(plugInPort, fav.getName(), createVariantPopup(null, fav.getName()));
} else { } else {
ComponentType componentType = this.typesByClass.get(fav.getName()); ComponentType componentType = this.typesByClass.get(fav.getName());
button = ComponentButtonFactory.create(plugInPort, componentType, button = ComponentButtonFactory.create(plugInPort, componentType,
createVariantPopup(componentType)); createVariantPopup(componentType, null));
} }
toolbar.add(button); toolbar.add(button);
} }
} }
private JPopupMenu createVariantPopup(final ComponentType componentType) { private JPopupMenu createVariantPopup(final ComponentType componentType, final String blockName) {
final JPopupMenu variantPopup = new JPopupMenu(); final JPopupMenu variantPopup = new JPopupMenu();
variantPopup.add("Loading..."); variantPopup.add("Loading...");
variantPopup.addPopupMenuListener(new PopupMenuListener() { variantPopup.addPopupMenuListener(new PopupMenuListener() {
@@ -330,7 +336,8 @@ class ComponentTabbedPane extends JTabbedPane {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
variantPopup.removeAll(); variantPopup.removeAll();
final String identifier = componentType.getInstanceClass().getCanonicalName(); final String identifier = componentType == null ? "block:" + blockName
: componentType.getInstanceClass().getCanonicalName();
JMenu shortcutSubmenu = new JMenu("Assign Shortcut"); JMenu shortcutSubmenu = new JMenu("Assign Shortcut");
final JRadioButtonMenuItem noneItem = new JRadioButtonMenuItem ("None"); final JRadioButtonMenuItem noneItem = new JRadioButtonMenuItem ("None");
@@ -398,7 +405,9 @@ class ComponentTabbedPane extends JTabbedPane {
} }
final Favorite fav = final Favorite fav =
new Favorite(FavoriteType.Component, componentType.getInstanceClass().getCanonicalName()); new Favorite(componentType == null ? FavoriteType.Block : FavoriteType.Component,
componentType == null ? blockName
: componentType.getInstanceClass().getCanonicalName());
final boolean isFavorite = favorites != null && favorites.indexOf(fav) >= 0; final boolean isFavorite = favorites != null && favorites.indexOf(fav) >= 0;
final JMenuItem favoritesItem = final JMenuItem favoritesItem =
new JMenuItem(isFavorite ? "Remove From Favorites" : "Add To Favorites", new JMenuItem(isFavorite ? "Remove From Favorites" : "Add To Favorites",
@@ -419,20 +428,26 @@ class ComponentTabbedPane extends JTabbedPane {
}); });
variantPopup.add(favoritesItem); variantPopup.add(favoritesItem);
variantPopup.add(new SelectAllAction(plugInPort, componentType)); if (componentType != null) {
variantPopup.add(shortcutSubmenu); variantPopup.add(new SelectAllAction(plugInPort, componentType));
variantPopup.add(shortcutSubmenu);
variantPopup.add(new JSeparator());
List<Template> variants = plugInPort.getVariantsFor(componentType); List<Template> templates = plugInPort.getVariantsFor(componentType);
if (variants == null || variants.isEmpty()) { if (templates == null || templates.isEmpty()) {
JMenuItem item = new JMenuItem("<no variants>"); JMenuItem item = new JMenuItem("<no variants>");
item.setEnabled(false); item.setEnabled(false);
variantPopup.add(item);
} else {
for (Template variant : variants) {
JMenuItem item =
ComponentButtonFactory.createVariantItem(plugInPort, variant, componentType);
variantPopup.add(item); variantPopup.add(item);
} else {
for (Template template : templates) {
JMenuItem item =
ComponentButtonFactory.createVariantItem(plugInPort, template, componentType);
variantPopup.add(item);
}
} }
} else {
variantPopup.add(shortcutSubmenu);
variantPopup.add(new DeleteBlockAction(plugInPort, swingUI, blockName));
} }
} }
@@ -74,7 +74,7 @@ public class ToolBox implements IPlugIn {
public ComponentTabbedPane getComponentTabbedPane() { public ComponentTabbedPane getComponentTabbedPane() {
if (componentTabbedPane == null) { if (componentTabbedPane == null) {
componentTabbedPane = new ComponentTabbedPane(plugInPort); componentTabbedPane = new ComponentTabbedPane(plugInPort, swingUI);
} }
return componentTabbedPane; return componentTabbedPane;
} }