Added ability to lock and unlock individual components from the context

menu
This commit is contained in:
bancika
2020-04-19 19:32:50 +02:00
parent 7754922711
commit 7df07c2bd1
9 changed files with 168 additions and 28 deletions
@@ -384,4 +384,9 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
* @param editor
*/
void applyEditor(IProjectEditor editor);
/**
* Locks or unlocks a single component.
*/
void lockComponent(IDIYComponent<?> c, boolean locked);
}
@@ -52,9 +52,10 @@ public interface ISelectionProcessor {
* depends on the current zoom level.
*
* @param point
* @param includeLocked
* @return
*/
List<IDIYComponent<?>> findComponentsAt(Point point);
List<IDIYComponent<?>> findComponentsAt(Point point, boolean includeLocked);
/**
* Expands the current selection to include surrounding components. Options are controlled with
@@ -68,6 +68,7 @@ public class Project implements Serializable, Cloneable {
private List<IDIYComponent<?>> components;
private Set<Set<IDIYComponent<?>>> groups;
private Set<Integer> lockedLayers;
private Set<IDIYComponent<?>> lockedComponents;
private Set<Integer> hiddenLayers;
private Font font = DEFAULT_FONT;
@@ -76,6 +77,7 @@ public class Project implements Serializable, Cloneable {
groups = new HashSet<Set<IDIYComponent<?>>>();
lockedLayers = new HashSet<Integer>();
hiddenLayers = new HashSet<Integer>();
lockedComponents = new HashSet<IDIYComponent<?>>();
title = DEFAULT_TITLE;
author = System.getProperty("user.name");
width = DEFAULT_WIDTH;
@@ -167,6 +169,12 @@ public class Project implements Serializable, Cloneable {
return groups;
}
public Set<IDIYComponent<?>> getLockedComponents() {
if (lockedComponents == null)
lockedComponents = new HashSet<IDIYComponent<?>>();
return lockedComponents;
}
public Set<Integer> getLockedLayers() {
return lockedLayers;
}
@@ -289,6 +297,11 @@ public class Project implements Serializable, Cloneable {
return false;
} else if (!lockedLayers.equals(other.lockedLayers))
return false;
if (getLockedComponents() == null) {
if (other.lockedComponents != null)
return false;
} else if (!getLockedComponents().equals(other.lockedComponents))
return false;
if (hiddenLayers == null) {
if (other.hiddenLayers != null)
return false;
@@ -350,6 +363,10 @@ public class Project implements Serializable, Cloneable {
}
project.groups.add(cloneGroup);
}
for (IDIYComponent<?> component : this.getLockedComponents()) {
project.lockedComponents.add(cloneMap.get(component));
}
return project;
}
}
@@ -529,12 +529,12 @@ public class Presenter implements IPlugInPort {
*
* @return
*/
public List<IDIYComponent<?>> findComponentsAtScaled(Point point) {
public List<IDIYComponent<?>> findComponentsAtScaled(Point point, boolean includeLocked) {
List<IDIYComponent<?>> components = drawingManager.findComponentsAt(point, currentProject);
Iterator<IDIYComponent<?>> iterator = components.iterator();
while (iterator.hasNext()) {
IDIYComponent<?> component = iterator.next();
if (isComponentLocked(component) || !isComponentVisible(component)) {
if ((includeLocked ? isComponentLayerLocked(component) : isComponentLocked(component)) || !isComponentVisible(component)) {
iterator.remove();
}
}
@@ -542,9 +542,9 @@ public class Presenter implements IPlugInPort {
}
@Override
public List<IDIYComponent<?>> findComponentsAt(Point point) {
public List<IDIYComponent<?>> findComponentsAt(Point point, boolean includeLocked) {
Point scaledPoint = scalePoint(point);
List<IDIYComponent<?>> components = findComponentsAtScaled(scaledPoint);
List<IDIYComponent<?>> components = findComponentsAtScaled(scaledPoint, includeLocked);
return components;
}
@@ -654,7 +654,7 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.REPAINT);
} else {
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>(selectedComponents);
List<IDIYComponent<?>> components = findComponentsAtScaled(scaledPoint);
List<IDIYComponent<?>> components = findComponentsAtScaled(scaledPoint, false);
// If there's nothing under mouse cursor deselect all.
if (components.isEmpty()) {
if (!ctrlDown) {
@@ -1087,7 +1087,7 @@ public class Presenter implements IPlugInPort {
this.preDragProject = currentProject.clone();
Point scaledPoint = scalePoint(point);
this.previousDragPoint = scaledPoint;
List<IDIYComponent<?>> components = forceSelectionRect ? null : findComponentsAtScaled(scaledPoint);
List<IDIYComponent<?>> components = forceSelectionRect ? null : findComponentsAtScaled(scaledPoint, false);
if (controlPointMap != null && !this.controlPointMap.isEmpty()) {
// If we're dragging control points reset selection.
updateSelection(new ArrayList<IDIYComponent<?>>(this.controlPointMap.keySet()));
@@ -2766,8 +2766,40 @@ public class Presenter implements IPlugInPort {
return lockedComponents;
}
@Override
public void lockComponent(IDIYComponent<?> c, boolean locked) {
LOG.debug(String.format("lockComponent(%s, %s)", c.getName(), locked));
Project oldProject = currentProject.clone();
try {
if (locked) {
currentProject.getLockedComponents().add(c);
selectedComponents.remove(c);
} else {
currentProject.getLockedComponents().remove(c);
}
} finally {
// Notify the listeners.
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
locked ? "Lock Component" : "Unlock Component");
projectFileManager.notifyFileChange();
}
messageDispatcher.dispatchMessage(EventType.REPAINT);
if (locked)
updateSelection(selectedComponents);
}
}
@SuppressWarnings("unchecked")
private boolean isComponentLocked(IDIYComponent<?> component) {
ComponentType componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass());
return currentProject.getLockedLayers().contains((int) Math.round(componentType.getZOrder())) || currentProject.getLockedComponents().contains(component);
}
@SuppressWarnings("unchecked")
private boolean isComponentLayerLocked(IDIYComponent<?> component) {
ComponentType componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass());
@@ -3002,10 +3002,18 @@
<changeType>NEW_FEATURE</changeType>
<description>"Add Flexible Leads" action added in the context menu that replaces straight leads of leaded components with flexible leads and adds leds to some components that do not have them (e.g. Transistors, Tube Sockets, Potentiometers, etc)</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Added ability to lock and unlock individual components from the context menu</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Added "Rotary Selector Switch" component</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Added support for automated regression testing of core functionalities which should reduce regression bugs in the long run</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Standardize how folded transistors are displayed - updated TO-92 and TO-1 packages</description>
@@ -65,7 +65,8 @@ public enum IconLoader {
"splash_electrolytic.png"), SplashFilm("splash_film.png"), Splash("splash.png"), Help("help2.png"), ScrollInformation("scroll_information.png"),
Node("node.png"), Web("web.png"), Guitar("guitar.png"), Scientist("scientist.png"), StarGrey("star_grey.png"), StarBlue("star_blue.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");
Elements("elements1.png"), GraphNodes("graph_nodes.png"), User("user1.png"), FlexibleLeads("flexible_leads.png"), Pads("pads.png"), Lock("lock.png"),
Unlock("lock_open.png");
protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 764 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 815 B

@@ -40,6 +40,7 @@ import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
@@ -82,6 +83,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
private JMenu expandMenu;
private JMenu transformMenu;
private JMenu applyTemplateMenu;
private JMenu lockMenu;
private JMenu unlockMenu;
private ActionFactory.CutAction cutAction;
private ActionFactory.CopyAction copyAction;
@@ -216,7 +219,9 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
@Override
public void valueChanged(String key, Object value) {
getScrollPane().setInSubdivision(IPlugInPort.RULER_IN_SUBDIVISION_10.equalsIgnoreCase(value == null ? null : value.toString()) ? InchSubdivision.BASE_10 : InchSubdivision.BASE_2);
getScrollPane().setInSubdivision(IPlugInPort.RULER_IN_SUBDIVISION_10
.equalsIgnoreCase(value == null ? null : value.toString()) ? InchSubdivision.BASE_10
: InchSubdivision.BASE_2);
}
});
@@ -331,12 +336,15 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
private RulerScrollPane getScrollPane() {
if (scrollPane == null) {
String subdivision = ConfigurationManager.getInstance().readString(IPlugInPort.RULER_IN_SUBDIVISION_KEY, IPlugInPort.RULER_IN_SUBDIVISION_DEFAULT);
String subdivision = ConfigurationManager.getInstance().readString(
IPlugInPort.RULER_IN_SUBDIVISION_KEY, IPlugInPort.RULER_IN_SUBDIVISION_DEFAULT);
scrollPane = new RulerScrollPane(getCanvasPanel(),
new ProjectDrawingProvider(plugInPort, true, false, true),
new Size(1d, SizeUnit.cm).convertToPixels(), new Size(1d, SizeUnit.in).convertToPixels(),
IPlugInPort.RULER_IN_SUBDIVISION_10.equalsIgnoreCase(subdivision) ? InchSubdivision.BASE_10 : InchSubdivision.BASE_2);
IPlugInPort.RULER_IN_SUBDIVISION_10.equalsIgnoreCase(subdivision)
? InchSubdivision.BASE_10
: InchSubdivision.BASE_2);
boolean metric = ConfigurationManager.getInstance().readBoolean(IPlugInPort.METRIC_KEY, true);
boolean useHardwareAcceleration =
@@ -434,6 +442,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
private void showPopupAt(int x, int y) {
updateSelectionMenu(x, y);
updateApplyTemplateMenu();
updateLock(x, y);
getPopupMenu().show(canvasPanel, x, y);
}
@@ -455,6 +464,9 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
popupMenu.add(getSaveAsBlockAction());
popupMenu.add(getExpandMenu());
popupMenu.addSeparator();
popupMenu.add(getLockMenu());
popupMenu.add(getUnlockMenu());
popupMenu.addSeparator();
popupMenu.add(getFlexibleLeadsAction());
popupMenu.addSeparator();
popupMenu.add(ActionFactory.getInstance().createEditProjectAction(plugInPort));
@@ -481,6 +493,22 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
return expandMenu;
}
public JMenu getLockMenu() {
if (lockMenu == null) {
lockMenu = new JMenu("Lock");
lockMenu.setIcon(IconLoader.Lock.getIcon());
}
return lockMenu;
}
public JMenu getUnlockMenu() {
if (unlockMenu == null) {
unlockMenu = new JMenu("Unlock");
unlockMenu.setIcon(IconLoader.Unlock.getIcon());
}
return unlockMenu;
}
public JMenu getTransformMenu() {
if (transformMenu == null) {
transformMenu = new JMenu("Transform Selection");
@@ -512,7 +540,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
private void updateSelectionMenu(int x, int y) {
getSelectionMenu().removeAll();
for (IDIYComponent<?> component : plugInPort.findComponentsAt(new Point(x, y))) {
for (IDIYComponent<?> component : plugInPort.findComponentsAt(new Point(x, y), false)) {
JMenuItem item = new JMenuItem(component.getName());
final IDIYComponent<?> finalComponent = component;
item.addActionListener(new ActionListener() {
@@ -529,6 +557,30 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
}
}
private void updateLock(int x, int y) {
getLockMenu().removeAll();
getUnlockMenu().removeAll();
List<IDIYComponent<?>> componentsAt = plugInPort.findComponentsAt(new Point(x, y), true);
if (componentsAt.size() == 0) {
getLockMenu().setEnabled(false);
getUnlockMenu().setEnabled(false);
} else {
boolean hasLocked = false;
boolean hasUnlocked = false;
for (IDIYComponent<?> c : componentsAt) {
if (plugInPort.getCurrentProject().getLockedComponents().contains(c)) {
getUnlockMenu().add(new LockAction(c, false));
hasLocked = true;
} else {
getLockMenu().add(new LockAction(c, true));
hasUnlocked = true;
}
}
getLockMenu().setEnabled(hasUnlocked);
getUnlockMenu().setEnabled(hasLocked);
}
}
private void updateApplyTemplateMenu() {
getApplyTemplateMenu().removeAll();
List<Template> templates = null;
@@ -716,7 +768,8 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return EnumSet.of(EventType.PROJECT_LOADED, EventType.ZOOM_CHANGED, EventType.REPAINT, EventType.SCROLL_TO);
return EnumSet.of(EventType.PROJECT_LOADED, EventType.ZOOM_CHANGED, EventType.REPAINT,
EventType.SCROLL_TO);
}
@SuppressWarnings("incomplete-switch")
@@ -773,8 +826,10 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
Rectangle visibleRect2 = canvasPanel.getVisibleRect();
Rectangle2D targetRect = (Rectangle2D) params[0];
if (targetRect != null)
canvasPanel.scrollRectToVisible(new Rectangle((int)(targetRect.getCenterX() - visibleRect2.width / 2),
(int)(targetRect.getCenterY() - visibleRect2.height / 2), visibleRect2.width, visibleRect2.height));
canvasPanel.scrollRectToVisible(
new Rectangle((int) (targetRect.getCenterX() - visibleRect2.width / 2),
(int) (targetRect.getCenterY() - visibleRect2.height / 2), visibleRect2.width,
visibleRect2.height));
break;
}
// }
@@ -812,4 +867,25 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
double extraSpace = CanvasPlugin.this.plugInPort.getExtraSpace();
getScrollPane().setZeroLocation(new Point2D.Double(extraSpace, extraSpace));
}
class LockAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private IDIYComponent<?> component;
private boolean locked;
public LockAction(IDIYComponent<?> component, boolean locked) {
super();
this.locked = locked;
this.component = component;
putValue(AbstractAction.NAME, component.getName());
}
@Override
public void actionPerformed(ActionEvent e) {
plugInPort.lockComponent(component, locked);
}
}
}