tree improvements

This commit is contained in:
bancika
2016-12-27 12:19:58 +01:00
parent 328cf0e9b7
commit fb6b1464a3
10 changed files with 218 additions and 24 deletions
Binary file not shown.
@@ -42,7 +42,7 @@ public class HumbuckerPickup extends AbstractTransparentComponent<String> {
private static Color BODY_COLOR = Color.lightGray;
private static Color POINT_COLOR = Color.darkGray;
public static Color PIN_COLOR = Color.decode("#C3E4ED");
public static Color POLE_COLOR = METAL_COLOR;
private static Size WIDTH = new Size(36.5d, SizeUnit.mm);
private static Size LENGTH = new Size(68.58d, SizeUnit.mm);
private static Size WIDTH_MINI = new Size(29.3d, SizeUnit.mm);
@@ -100,7 +100,7 @@ public class HumbuckerPickup extends AbstractTransparentComponent<String> {
g2d.draw(body[0]);
g2d.draw(body[1]);
if (!outlineMode) {
g2d.setColor(PIN_COLOR);
g2d.setColor(POLE_COLOR);
g2d.fill(body[3]);
g2d.setColor(color.darker());
g2d.draw(body[3]);
@@ -42,7 +42,7 @@ public class P90DogEarPickup extends AbstractTransparentComponent<String> {
private static Color BODY_COLOR = Color.decode("#D8C989");;
private static Color POINT_COLOR = Color.darkGray;
public static Color POLE_COLOR = Color.decode("#C3E4ED");
public static Color POLE_COLOR = METAL_COLOR;
private static Size WIDTH = new Size(41d, SizeUnit.mm);
private static Size LENGTH = new Size(86.9d, SizeUnit.mm);
private static Size TOTAL_LENGTH = new Size(118.7d, SizeUnit.mm);
@@ -52,8 +52,8 @@ public class P90DogEarPickup extends AbstractTransparentComponent<String> {
private static Size POINT_SIZE = new Size(3d, SizeUnit.mm);
private static Size LIP_HOLE_SIZE = new Size(2.5d, SizeUnit.mm);
private static Size LIP_HOLE_SPACING = new Size(97d, SizeUnit.mm);
private static Size POLE_SIZE = new Size(3d, SizeUnit.mm);
private static Size POLE_SPACING = new Size(10.1d, SizeUnit.mm);
private static Size POLE_SIZE = new Size(4d, SizeUnit.mm);
private static Size POLE_SPACING = new Size(11.68d, SizeUnit.mm);
private String value = "";
private Point controlPoint = new Point(0, 0);
@@ -42,14 +42,14 @@ public class P90SoapBarPickup extends AbstractTransparentComponent<String> {
private static Color BODY_COLOR = Color.decode("#D8C989");
private static Color POINT_COLOR = Color.darkGray;
public static Color POLE_COLOR = Color.decode("#C3E4ED");
public static Color POLE_COLOR = METAL_COLOR;
private static Size WIDTH = new Size(35.3d, SizeUnit.mm);
private static Size LENGTH = new Size(85.6d, SizeUnit.mm);
private static Size EDGE_RADIUS = new Size(8d, SizeUnit.mm);
private static Size POINT_MARGIN = new Size(5d, SizeUnit.mm);
private static Size POINT_SIZE = new Size(3d, SizeUnit.mm);
private static Size POLE_SIZE = new Size(3d, SizeUnit.mm);
private static Size POLE_SPACING = new Size(10.1d, SizeUnit.mm);
private static Size POLE_SIZE = new Size(4d, SizeUnit.mm);
private static Size POLE_SPACING = new Size(11.68d, SizeUnit.mm);
private String value = "";
private Point controlPoint = new Point(0, 0);
@@ -41,16 +41,17 @@ public class SingleCoilPickup extends AbstractTransparentComponent<String> {
private static final long serialVersionUID = 1L;
private static Color BODY_COLOR = Color.darkGray;
private static Color BODY_COLOR = Color.gray;
private static Color POINT_COLOR = Color.lightGray;
private static Size WIDTH = new Size(15.5d, SizeUnit.mm);
private static Size LENGTH = new Size(83.82d, SizeUnit.mm);
private static Size LENGTH = new Size(83d, SizeUnit.mm);
private static Size INNER_LENGTH = new Size(70d, SizeUnit.mm);
private static Size LIP_WIDTH = new Size(5d, SizeUnit.mm);
private static Size LIP_LENGTH = new Size(20d, SizeUnit.mm);
private static Size POINT_SIZE = new Size(3d, SizeUnit.mm);
private static Size HOLE_SIZE = new Size(2d, SizeUnit.mm);
private static Size HOLE_MARGIN = new Size(4d, SizeUnit.mm);
private static Size POLE_SIZE = new Size(3d, SizeUnit.mm);
private static Size POLE_SIZE = new Size(4d, SizeUnit.mm);
private static Size POLE_SPACING = new Size(11.68d, SizeUnit.mm);
private String value = "";
@@ -74,6 +75,8 @@ public class SingleCoilPickup extends AbstractTransparentComponent<String> {
g2d.fill(body[0]);
g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR : POINT_COLOR);
g2d.fill(body[1]);
// g2d.setColor(outlineMode ? Constants.TRANSPARENT_COLOR : color);
// g2d.fill(body[3]);
g2d.setComposite(oldComposite);
}
@@ -87,13 +90,18 @@ public class SingleCoilPickup extends AbstractTransparentComponent<String> {
} else {
finalBorderColor =
componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? SELECTION_COLOR
: color.brighter();
: color.darker();
}
g2d.setColor(finalBorderColor);
g2d.draw(body[0]);
g2d.draw(body[3]);
if (!outlineMode) {
g2d.setColor(METAL_COLOR.darker());
g2d.draw(body[2]);
g2d.setColor(METAL_COLOR);
g2d.fill(body[2]);
}
Color finalLabelColor;
@@ -118,12 +126,13 @@ public class SingleCoilPickup extends AbstractTransparentComponent<String> {
@SuppressWarnings("incomplete-switch")
public Shape[] getBody() {
if (body == null) {
body = new Shape[3];
body = new Shape[4];
int x = controlPoint.x;
int y = controlPoint.y;
int width = (int) WIDTH.convertToPixels();
int length = (int) LENGTH.convertToPixels();
int innerLength = (int) INNER_LENGTH.convertToPixels();
int lipWidth = (int) LIP_WIDTH.convertToPixels();
int lipLength = (int) LIP_LENGTH.convertToPixels();
int pointSize = getClosestOdd(POINT_SIZE.convertToPixels());
@@ -132,9 +141,9 @@ public class SingleCoilPickup extends AbstractTransparentComponent<String> {
Area mainArea =
new Area(new RoundRectangle2D.Double(x - length / 2, y - lipWidth / 2 - width, length, width, width, width));
mainArea.add(new Area(new Polygon(new int[] {x - length / 2 + width / 2, x + length / 2 - width / 2,
x + lipLength / 2, x - lipLength / 2}, new int[] {y - lipWidth / 2, y - lipWidth / 2, y + lipWidth / 2,
y + lipWidth / 2}, 4)));
mainArea.add(new Area(new Polygon(new int[] {(int) (x - length / 2 + width * 0.47),
(int) (x + length / 2 - width * 0.47), x + lipLength / 2, x - lipLength / 2}, new int[] {y - lipWidth / 2,
y - lipWidth / 2, y + lipWidth / 2, y + lipWidth / 2}, 4)));
// Cutout holes
mainArea.subtract(new Area(new Ellipse2D.Double(x - length / 2 + holeMargin - holeSize / 2, y - lipWidth / 2
- width / 2 - holeSize / 2, holeSize, holeSize)));
@@ -151,12 +160,15 @@ public class SingleCoilPickup extends AbstractTransparentComponent<String> {
Area poleArea = new Area();
for (int i = 0; i < 6; i++) {
Ellipse2D pole =
new Ellipse2D.Double(x - length / 2 + poleMargin + i * poleSpacing, y - lipWidth / 2 - width / 2 - poleSize
/ 2, poleSize, poleSize);
new Ellipse2D.Double(x - length / 2 + poleMargin + i * poleSpacing - poleSize / 2, y - lipWidth / 2 - width
/ 2 - poleSize / 2, poleSize, poleSize);
poleArea.add(new Area(pole));
}
body[2] = poleArea;
body[3] =
new RoundRectangle2D.Double(x - innerLength / 2, y - lipWidth / 2 - width, innerLength, width, width, width);
// Rotate if needed
if (orientation != Orientation.DEFAULT) {
double theta = 0;
Binary file not shown.
Binary file not shown.
@@ -19,6 +19,8 @@ import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import org.diylc.common.ComponentType;
import org.diylc.common.IPlugInPort;
@@ -33,7 +35,7 @@ import org.diylc.swingframework.openide.DropDownButtonFactory;
*
* @author Branislav Stojkovic
*/
class ComponentButtonFactory {
public class ComponentButtonFactory {
public static int MARGIN = 3;
@@ -127,6 +129,8 @@ class ComponentButtonFactory {
e.consume();
}
});
Border margin = new EmptyBorder(4, 0, 0, 0);
label.setBorder(margin);
item.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
item.add(label);
return item;
@@ -1,5 +1,8 @@
package org.diylc.swing.plugins.tree;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.EnumSet;
import javax.swing.SwingConstants;
@@ -49,6 +52,18 @@ public class ComponentTree implements IPlugIn {
getTreePanel().setVisible(
ConfigurationManager.getInstance().readString(ConfigPlugin.COMPONENT_BROWSER, ConfigPlugin.SEARCHABLE_TREE)
.equals(ConfigPlugin.SEARCHABLE_TREE));
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyChar() == 'q') {
getTreePanel().getSearchField().requestFocusInWindow();
return true;
}
return false;
}
});
}
public TreePanel getTreePanel() {
@@ -8,20 +8,32 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
@@ -29,20 +41,33 @@ import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.appframework.miscutils.IConfigListener;
import org.diylc.common.ComponentType;
import org.diylc.common.IPlugInPort;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Template;
import org.diylc.images.IconLoader;
import org.diylc.presenter.ComponentProcessor;
import org.diylc.swing.plugins.toolbox.ComponentButtonFactory;
public class TreePanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(TreePanel.class);
private DefaultTreeModel treeModel;
private JTree tree;
private JTextField searchField;
private DefaultMutableTreeNode recentNode;
private List<String> recentComponents;
private IPlugInPort plugInPort;
private JPopupMenu popup;
public TreePanel(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
setLayout(new GridBagLayout());
@@ -65,11 +90,63 @@ public class TreePanel extends JPanel {
add(scroll, gbc);
setPreferredSize(new Dimension(240, 200));
ConfigurationManager.getInstance().addConfigListener(IPlugInPort.RECENT_COMPONENTS_KEY, new IConfigListener() {
@SuppressWarnings("unchecked")
@Override
public void valueChanged(String key, Object value) {
List<String> newComponents = (List<String>) value;
if (!new HashSet<String>(newComponents).equals(new HashSet<String>(TreePanel.this.recentComponents))) {
LOG.info("Detected change");
refreshRecentComponentsToolbar(newComponents);
} else
LOG.info("Detected no change");
TreePanel.this.recentComponents = new ArrayList<String>(newComponents);
}
});
getTree().expandRow(0);
}
public DefaultMutableTreeNode getRecentNode() {
if (this.recentNode == null) {
this.recentNode = new DefaultMutableTreeNode(new Payload("(Recently Used)"), true);
@SuppressWarnings("unchecked")
List<String> recent =
(List<String>) ConfigurationManager.getInstance().readObject(IPlugInPort.RECENT_COMPONENTS_KEY, null);
if (recent != null) {
this.recentComponents = new ArrayList<String>(recent);
refreshRecentComponentsToolbar(recent);
} else {
this.recentComponents = new ArrayList<String>();
}
}
return this.recentNode;
}
@SuppressWarnings("unchecked")
private void refreshRecentComponentsToolbar(List<String> recentComponentClassList) {
getRecentNode().removeAllChildren();
for (String componentClassName : recentComponentClassList) {
ComponentType componentType;
try {
componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) Class.forName(componentClassName));
final DefaultMutableTreeNode componentNode = new DefaultMutableTreeNode(new Payload(componentType), false);
getRecentNode().add(componentNode);
} catch (ClassNotFoundException e) {
LOG.error("Could not create recent component button for " + componentClassName, e);
}
}
getTreeModel().nodeStructureChanged(getRecentNode());
}
public DefaultTreeModel getTreeModel() {
if (treeModel == null) {
final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Components", true);
rootNode.add(getRecentNode());
Map<String, List<ComponentType>> componentTypes = plugInPort.getComponentTypes();
List<String> categories = new ArrayList<String>(componentTypes.keySet());
Collections.sort(categories);
@@ -88,12 +165,11 @@ public class TreePanel extends JPanel {
public JTree getTree() {
if (tree == null) {
tree = new JTree(getTreeModel());
tree.setRootVisible(false);
tree.setCellRenderer(new ComponentCellRenderer());
tree.setRowHeight(0);
ToolTipManager.sharedInstance().registerComponent(tree);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
@@ -109,10 +185,62 @@ public class TreePanel extends JPanel {
}
}
});
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
int row = tree.getClosestRowForLocation(e.getX(), e.getY());
tree.setSelectionRow(row);
getPopup().show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
return tree;
}
public JPopupMenu getPopup() {
if (popup == null) {
popup = new JPopupMenu();
popup.add("Loading...");
popup.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
popup.removeAll();
popup.add(new SelectAllAction(plugInPort));
ComponentType componentType = plugInPort.getNewComponentTypeSlot();
if (componentType != null) {
popup.add(new JSeparator());
List<Template> templates = plugInPort.getTemplatesFor(componentType.getCategory(), componentType.getName());
if (templates == null || templates.isEmpty()) {
JMenuItem item = new JMenuItem("<no templates>");
item.setEnabled(false);
popup.add(item);
} else {
for (Template template : templates) {
JMenuItem item = ComponentButtonFactory.createTemplateItem(plugInPort, template, componentType);
popup.add(item);
}
}
}
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {}
});
}
return popup;
}
public JTextField getSearchField() {
if (searchField == null) {
searchField = new JTextField() {
@@ -131,7 +259,7 @@ public class TreePanel extends JPanel {
if (searchField.getText().trim().length() == 0 && !searchField.hasFocus()) {
g2d.setColor(Color.gray);
g2d.setFont(searchField.getFont());
g2d.drawString("Search", 4, 3 + searchField.getFont().getSize());
g2d.drawString("Search (press Q to jump here)", 4, 3 + searchField.getFont().getSize());
}
}
};
@@ -236,6 +364,9 @@ public class TreePanel extends JPanel {
else
setPreferredSize(new Dimension(0, 0));
} else {
setToolTipText("<html><b>" + type.getComponentType().getName() + "</b><br>"
+ type.getComponentType().getDescription() + "<br>Author: " + type.getComponentType().getAuthor()
+ "<br><br>Left click to instantiate this component, right click for more options" + "</html>");
setIcon(type.getComponentType().getIcon());
if (type.isVisible())
setPreferredSize(new Dimension(240, 32));
@@ -282,4 +413,36 @@ public class TreePanel extends JPanel {
return componentType == null ? category : componentType.getName();
}
}
public static class SelectAllAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private IPlugInPort plugInPort;
public SelectAllAction(IPlugInPort plugInPort) {
super();
this.plugInPort = plugInPort;
putValue(AbstractAction.NAME, "Select All");
}
@Override
public void actionPerformed(ActionEvent e) {
LOG.info(getValue(AbstractAction.NAME) + " triggered");
ComponentType componentType = plugInPort.getNewComponentTypeSlot();
if (componentType != null) {
List<IDIYComponent<?>> components = plugInPort.getCurrentProject().getComponents();
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
for (IDIYComponent<?> component : components) {
if (componentType.getInstanceClass().equals(component.getClass())) {
newSelection.add(component);
}
}
plugInPort.updateSelection(newSelection);
plugInPort.setNewComponentTypeSlot(null, null);
plugInPort.refresh();
}
}
}
}