Added a button in the action toolbar to toggle between snap modes (none,

grid, component)
This commit is contained in:
bancika
2020-12-13 11:12:45 +01:00
parent cad1160ce7
commit 9dada76406
19 changed files with 160 additions and 15 deletions
@@ -1705,7 +1705,7 @@ public class ActionFactory {
}
}
public static class ToggleAction extends AbstractAction {
public static class ToggleAction extends AbstractAction implements IConfigListener {
private static final long serialVersionUID = 1L;
@@ -1720,6 +1720,8 @@ public class ActionFactory {
.readString(configKey, defaultValue).equalsIgnoreCase(title));
if (icon != null)
putValue(AbstractAction.SMALL_ICON, icon);
// keep track of config changes and update the state accordingly
ConfigurationManager.getInstance().addConfigListener(configKey, this);
}
@Override
@@ -1727,6 +1729,11 @@ public class ActionFactory {
LOG.info(getValue(AbstractAction.NAME) + " toggle triggered");
ConfigurationManager.getInstance().writeValue(configKey, getValue(AbstractAction.NAME));
}
@Override
public void valueChanged(String key, Object value) {
putValue(AbstractAction.SELECTED_KEY, getValue(AbstractAction.NAME).toString().equalsIgnoreCase(value.toString()));
}
}
public static class ThemeAction extends AbstractAction {
@@ -26,7 +26,9 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
@@ -42,11 +44,10 @@ import org.diylc.common.EventType;
import org.diylc.common.IComponentTransformer;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.swing.images.IconLoader;
import org.diylc.lang.LangUtil;
import org.diylc.swing.ActionFactory;
import org.diylc.swing.ISwingUI;
import org.diylc.swing.plugins.canvas.CanvasPlugin;
import org.diylc.swing.images.IconLoader;
import org.diylc.swing.plugins.help.HelpMenuPlugin;
/**
@@ -152,6 +153,13 @@ public class ActionBarPlugin implements IPlugIn {
public ConfigToolbar getConfigToolbar() {
if (configToolbar == null) {
configToolbar = new ConfigToolbar();
List<ToggleItem> toggleItems = new ArrayList<ToggleItem>();
toggleItems.add(new ToggleItem(IPlugInPort.SNAP_TO_NONE, IPlugInPort.SNAP_TO_NONE, IconLoader.SnapToNone.getIcon()));
toggleItems.add(new ToggleItem(IPlugInPort.SNAP_TO_GRID, IPlugInPort.SNAP_TO_GRID, IconLoader.SnapToGrid.getIcon()));
toggleItems.add(new ToggleItem(IPlugInPort.SNAP_TO_COMPONENTS, IPlugInPort.SNAP_TO_COMPONENTS, IconLoader.SnapToComponents.getIcon()));
configToolbar.addToggleLabel(LangUtil.translate("Snap To"), IPlugInPort.SNAP_TO_KEY, IPlugInPort.SNAP_TO_DEFAULT, toggleItems);
configToolbar.add(LangUtil.translate("Continuous Creation"), IPlugInPort.CONTINUOUS_CREATION_KEY, IconLoader.Elements.getIcon(), false);
configToolbar.add(LangUtil.translate("Highlight Connected Areas"), IPlugInPort.HIGHLIGHT_CONTINUITY_AREA, IconLoader.LaserPointer.getIcon(), false);
configToolbar.add(LangUtil.translate("Sticky Points"), IPlugInPort.STICKY_POINTS_KEY, IconLoader.GraphNodes.getIcon(), true);
@@ -25,6 +25,7 @@ import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Icon;
@@ -75,6 +76,11 @@ public class ConfigToolbar extends JPanel {
});
}
public void addToggleLabel(String text, String configKey, String defaultValue, List<ToggleItem> items) {
ToggleConfigLabel label = new ToggleConfigLabel(text, configKey, defaultValue, items);
add(label);
}
@Override
public void setEnabled(boolean enabled) {
for (Component c : getComponents())
@@ -0,0 +1,85 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC 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.
DIYLC 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 DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.swing.gui.actionbar;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.appframework.miscutils.IConfigListener;
import org.diylc.lang.LangUtil;
public class ToggleConfigLabel extends JLabel implements IConfigListener {
private static final long serialVersionUID = 1L;
private List<ToggleItem> items;
private String configItem;
private String title;
private int itemIndex = 0;
public ToggleConfigLabel(String title, String configKey, String defaultValue, List<ToggleItem> items) {
super();
this.title = title;
this.configItem = configKey;
this.items = items;
setHorizontalTextPosition(SwingConstants.LEFT);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
valueChanged(configKey, ConfigurationManager.getInstance().readString(configKey, defaultValue));
ConfigurationManager.getInstance().addConfigListener(configKey, this);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
itemIndex++;
if (itemIndex >= items.size())
itemIndex = 0;
ConfigurationManager.getInstance().writeValue(configKey, items.get(itemIndex).getConfigValue());
}
});
}
@Override
public void valueChanged(String key, Object value) {
if (!this.configItem.equalsIgnoreCase(key) || value == null)
return;
this.itemIndex = 0;
for (int i = 0; i < items.size(); i++) {
ToggleItem item = items.get(i);
if (item.getConfigValue().equalsIgnoreCase(value.toString()))
{
this.itemIndex = i;
setIcon(item.getIcon());
setToolTipText(title + " " + item.getConfigValue() + ". " + LangUtil.translate("Click to toggle to the next option"));
break;
}
}
}
}
@@ -0,0 +1,28 @@
package org.diylc.swing.gui.actionbar;
import javax.swing.Icon;
public class ToggleItem {
private String name;
private String configValue;
private Icon icon;
public ToggleItem(String name, String configValue, Icon icon) {
super();
this.name = name;
this.configValue = configValue;
this.icon = icon;
}
public String getName() {
return name;
}
public String getConfigValue() {
return configValue;
}
public Icon getIcon() {
return icon;
}
}
@@ -66,7 +66,8 @@ public enum IconLoader {
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"), Lock("lock.png"),
Unlock("lock_open.png"), Earth("earth.png"), Loadline("loadline.png"), LoadlineAdd("loadline_add.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");
protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

@@ -36,7 +36,6 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.image.VolatileImage;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
@@ -56,7 +56,7 @@ public class ConfigPlugin implements IPlugIn {
private static final String CONFIG_MENU = "Config";
private static final String THEME_MENU = "Theme";
private static final String RULER_MENU = "Ruler Inch Subdivision...";
private static final String SNAP_MENU = "Snap To...";
private static final String SNAP_MENU = "Snap To";
private static final String COMPONENT_BROWSER_MENU = "Toolbox";
public static final String COMPONENT_BROWSER = "componentBrowser";
public static final String SEARCHABLE_TREE = "Searchable Tree";