Datasheet functionality added

This commit is contained in:
Bane Stojkovic
2022-11-04 18:01:52 +01:00
parent bee06d489d
commit a9f0c9b82c
28 changed files with 2404 additions and 337 deletions
@@ -221,7 +221,7 @@ public class CanvasPanel extends JComponent implements Autoscroll {
@Override
public void actionPerformed(ActionEvent e) {
CanvasPanel.this.plugInPort.setNewComponentTypeSlot(null, null, false);
CanvasPanel.this.plugInPort.setNewComponentTypeSlot(null, null, null, false);
}
});
@@ -241,7 +241,7 @@ public class CanvasPanel extends JComponent implements Autoscroll {
for (Map.Entry<String, List<ComponentType>> entry : componentTypes.entrySet()) {
for (ComponentType type : entry.getValue()) {
if (type.getInstanceClass().getCanonicalName().equals(clazz)) {
CanvasPanel.this.plugInPort.setNewComponentTypeSlot(type, null, false);
CanvasPanel.this.plugInPort.setNewComponentTypeSlot(type, null, null, false);
// hack: fake mouse movement to repaint
CanvasPanel.this.plugInPort.mouseMoved(getMousePosition(), false, false, false);
return;
@@ -275,7 +275,7 @@ public class CanvasPanel extends JComponent implements Autoscroll {
LOG.error("Could not find type: " + typeName);
return;
}
this.plugInPort.setNewComponentTypeSlot(type, null, false);
this.plugInPort.setNewComponentTypeSlot(type, null, null, false);
}
// hack: fake mouse movement to repaint
@@ -17,6 +17,7 @@
*/
package org.diylc.swing.plugins.toolbox;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
@@ -28,11 +29,15 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
@@ -40,6 +45,8 @@ import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.Utils;
import org.diylc.common.ComponentType;
@@ -57,24 +64,25 @@ import org.diylc.swingframework.openide.DropDownButtonFactory;
* @author Branislav Stojkovic
*/
public class ComponentButtonFactory {
private static final Logger LOG = Logger.getLogger(ComponentButtonFactory.class);
public static int MARGIN = 3;
public static JButton create(final IPlugInPort plugInPort, final ComponentType componentType, final JPopupMenu menu) {
public static JButton create(final IPlugInPort plugInPort, final ComponentType componentType,
final JPopupMenu menu) {
JButton button = DropDownButtonFactory.createDropDownButton(componentType.getIcon(), menu);
button.setBorder(BorderFactory.createEmptyBorder(MARGIN + 1, MARGIN + 1, MARGIN, MARGIN));
button.setToolTipText("<html><b>" + componentType.getName() + "</b><br>" + componentType.getDescription()
+ "<br>Author: " + componentType.getAuthor() + "<br><br>Right click to select all components of this type"
+ "</html>");
button.setToolTipText("<html><b>" + componentType.getName() + "</b><br>"
+ componentType.getDescription() + "<br>Author: " + componentType.getAuthor()
+ "<br><br>Right click to select all components of this type" + "</html>");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
plugInPort.setNewComponentTypeSlot(componentType, null, false);
plugInPort.setNewComponentTypeSlot(componentType, null, null, false);
}
});
@@ -95,7 +103,7 @@ public class ComponentButtonFactory {
newSelection.addAll(plugInPort.getSelectedComponents());
}
plugInPort.updateSelection(newSelection);
plugInPort.setNewComponentTypeSlot(null, null, false);
plugInPort.setNewComponentTypeSlot(null, null, null, false);
plugInPort.refresh();
}
}
@@ -105,26 +113,27 @@ public class ComponentButtonFactory {
@Override
public void keyPressed(KeyEvent e) {
plugInPort.keyPressed(e.getKeyCode(), Utils.isMac() ? e.isControlDown() : e.isMetaDown(), e.isShiftDown(), e.isAltDown());
plugInPort.keyPressed(e.getKeyCode(), Utils.isMac() ? e.isControlDown() : e.isMetaDown(),
e.isShiftDown(), e.isAltDown());
}
});
return button;
}
private static final Pattern contributedPattern = Pattern.compile("^(.*)\\[(.*)\\]");
public static JMenuItem createVariantItem(final IPlugInPort plugInPort, final Template variant,
final ComponentType componentType) {
String display = variant.getName();
Matcher match = contributedPattern.matcher(display);
if (match.find()) {
String name = match.group(1);
String owner = match.group(2);
display = "<html>" + name + "<font color='gray'>[" + owner + "]</font></html>";
display = "<html>" + name + "<font color='gray'>[" + owner + "]</font></html>";
}
final JMenuItem item = new JMenuItem(display) {
private static final long serialVersionUID = 1L;
@@ -139,14 +148,17 @@ public class ComponentButtonFactory {
@Override
public void actionPerformed(ActionEvent e) {
plugInPort.setNewComponentTypeSlot(componentType, variant, false);
plugInPort.setNewComponentTypeSlot(componentType, variant, null, false);
}
});
String defaultVariant = plugInPort.getDefaultVariant(componentType);
JLabel label = new JLabel(variant.getName().equals(defaultVariant) ? IconLoader.PinGreen.getIcon() : IconLoader.PinGrey.getIcon());
label.setToolTipText(variant.getName().equals(defaultVariant) ? "Remove default variant" : "Set default variant");
JLabel label =
new JLabel(variant.getName().equals(defaultVariant) ? IconLoader.PinGreen.getIcon()
: IconLoader.PinGrey.getIcon());
label.setToolTipText(variant.getName().equals(defaultVariant) ? "Remove default variant"
: "Set default variant");
label.addMouseListener(new MouseAdapter() {
@Override
@@ -178,9 +190,9 @@ public class ComponentButtonFactory {
JPopupMenu m = (JPopupMenu) c;
m.setVisible(false);
}
int result =
JOptionPane.showConfirmDialog(SwingUtilities.getRoot(item), "Are you sure you want to delete variant \""
+ variant.getName() + "\"", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
int result = JOptionPane.showConfirmDialog(SwingUtilities.getRoot(item),
"Are you sure you want to delete variant \"" + variant.getName() + "\"", "Delete",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (result != JOptionPane.YES_OPTION) {
return;
}
@@ -194,10 +206,87 @@ public class ComponentButtonFactory {
item.add(label);
return item;
}
public static JButton createBuildingBlockButton(final IPlugInPort plugInPort, final String blockName, final JPopupMenu menu) {
JButton button = DropDownButtonFactory.createDropDownButton(blockName, IconLoader.ComponentLarge.getIcon(), menu);
public static List<Component> createDatasheetItems(final IPlugInPort plugInPort,
final ComponentType componentType, List<String> path) {
int depth = path.size();
List<String[]> datasheet = componentType.getDatasheet();
if (datasheet == null) {
return null;
}
Stream<String[]> stream = datasheet.stream();
for (int i = 0; i < depth; i++) {
final int finalI = i;
stream = stream.filter(line -> line[finalI].equals(path.get(finalI)));
}
List<String[]> filteredDatasheet = stream.collect(Collectors.toList());
return filteredDatasheet.stream().map(x -> x[depth]).distinct().map(value -> {
if (depth < componentType.getDatasheetCreationStepCount() - 1) {
final JMenu submenu = new JMenu(value) {
private static final long serialVersionUID = 1L;
// Customize item size to fit the delete button
public java.awt.Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
return new Dimension(d.width + 32, d.height);
}
};
submenu.add(new JMenuItem("Loading..."));
submenu.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
submenu.removeAll();
List<String> newPath = new ArrayList<String>(path);
newPath.add(value);
List<Component> childItems = createDatasheetItems(plugInPort, componentType, newPath);
for (Component childItem : childItems) {
submenu.add(childItem);
}
}
@Override
public void menuDeselected(MenuEvent e) {}
@Override
public void menuCanceled(MenuEvent e) {}
});
return (Component) submenu;
}
JMenuItem item = new JMenuItem(value);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Optional<String[]> findFirst =
filteredDatasheet.stream().filter(line -> line[depth] == value).findFirst();
if (findFirst.isPresent()) {
plugInPort.setNewComponentTypeSlot(componentType, null, findFirst.get(),
false);
} else {
LOG.error("Could not find datasheet item for: " + value);
}
}
});
return (Component) item;
}).collect(Collectors.toList());
}
public static JButton createBuildingBlockButton(final IPlugInPort plugInPort,
final String blockName, final JPopupMenu menu) {
JButton button = DropDownButtonFactory.createDropDownButton(blockName,
IconLoader.ComponentLarge.getIcon(), menu);
button.setVerticalTextPosition(SwingConstants.CENTER);
button.setHorizontalTextPosition(SwingConstants.LEFT);
@@ -214,13 +303,14 @@ public class ComponentButtonFactory {
LOG.error("Error loading building block", e1);
}
}
});
});
button.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
plugInPort.keyPressed(e.getKeyCode(), Utils.isMac() ? e.isControlDown() : e.isMetaDown(), e.isShiftDown(), e.isAltDown());
plugInPort.keyPressed(e.getKeyCode(), Utils.isMac() ? e.isControlDown() : e.isMetaDown(),
e.isShiftDown(), e.isAltDown());
}
});
return button;
@@ -115,7 +115,7 @@ class ComponentTabbedPane extends JTabbedPane {
@Override
public void stateChanged(ChangeEvent e) {
ConfigurationManager.getInstance().writeValue(LAST_SELECTED_TAB, getSelectedIndex());
ComponentTabbedPane.this.plugInPort.setNewComponentTypeSlot(null, null, false);
ComponentTabbedPane.this.plugInPort.setNewComponentTypeSlot(null, null, null, false);
// Refresh recent components if needed
if (pendingRecentComponents != null) {
refreshRecentComponentsToolbar(pendingRecentComponents);
@@ -124,7 +124,7 @@ public class CustomTreeModel implements TreeModel {
@Override
public void mouseClicked(MouseEvent e) {
plugInPort.setNewComponentTypeSlot(type, null, false);
plugInPort.setNewComponentTypeSlot(type, null, null, false);
}
}));
}
@@ -161,7 +161,7 @@ public class CustomTreeModel implements TreeModel {
@Override
public void mouseClicked(MouseEvent e) {
plugInPort.setNewComponentTypeSlot(type, null, false);
plugInPort.setNewComponentTypeSlot(type, null, null, false);
}
}));
}
@@ -202,7 +202,7 @@ public class CustomTreeModel implements TreeModel {
@Override
public void mouseClicked(MouseEvent e) {
plugInPort.setNewComponentTypeSlot(type, null, false);
plugInPort.setNewComponentTypeSlot(type, null, null, false);
}
}));
this.visibleLeaves.put(category, visibleTypes);
@@ -67,7 +67,7 @@ class TreeGestureListener implements DragGestureListener {
}
}
else {
presenter.setNewComponentTypeSlot(type, null, true);
presenter.setNewComponentTypeSlot(type, null, null, true);
start = true;
}
@@ -351,9 +351,18 @@ public class TreePanel extends JPanel {
}
if (componentType != null) {
popup.add(new SelectAllAction(plugInPort, componentType));
popup.add(new SelectAllAction(plugInPort, componentType));
popup.add(shortcutSubmenu);
popup.add(new JSeparator());
if (componentType.getDatasheet() != null) {
List<Component> items = ComponentButtonFactory.createDatasheetItems(plugInPort, componentType, new ArrayList<String>());
for (Component item : items) {
popup.add(item);
}
popup.add(new JSeparator());
}
List<Template> templates = plugInPort.getVariantsFor(componentType);
if (templates == null || templates.isEmpty()) {
@@ -489,13 +498,19 @@ public class TreePanel extends JPanel {
}
}
if (payload.getComponentType() != null) {
List<Template> variants = plugInPort.getVariantsFor(payload.getComponentType());
int count = 0;
if (payload.getComponentType().getDatasheet() != null) {
count += payload.getComponentType().getDatasheet().size();
}
if (variants != null && !variants.isEmpty()) {
count += variants.size();
}
if (count > 0) {
variantsHtml =
" <a style=\"text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; background-color: #D7FFC6; color: #666666;\">[+"
+ variants.size() + "]</a>";
+ count + "]</a>";
}
}
@@ -528,7 +543,7 @@ public class TreePanel extends JPanel {
public void actionPerformed(ActionEvent e) {
LOG.info(getValue(AbstractAction.NAME) + " triggered");
if (componentType != null) {
plugInPort.setNewComponentTypeSlot(null, null, false);
plugInPort.setNewComponentTypeSlot(null, null, null, false);
List<IDIYComponent<?>> components = plugInPort.getCurrentProject().getComponents();
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
for (IDIYComponent<?> component : components) {
@@ -56,7 +56,7 @@ class TreeTargetListener implements DropTargetListener {
@Override
public void drop(DropTargetDropEvent dtde) {
presenter.setNewComponentTypeSlot(null, null, false);
presenter.setNewComponentTypeSlot(null, null, null, false);
}
@Override