mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-28 07:28:20 -04:00
added icons to tree view
This commit is contained in:
parent
28f6129509
commit
71ee13ad2b
@ -1,7 +1,10 @@
|
||||
package de.neemann.digital.draw.library;
|
||||
|
||||
import de.neemann.digital.core.element.ElementTypeDescription;
|
||||
import de.neemann.digital.draw.elements.VisualElement;
|
||||
import de.neemann.digital.draw.shapes.ShapeFactory;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
@ -17,6 +20,7 @@ public class LibraryNode implements Iterable<LibraryNode> {
|
||||
private final String name;
|
||||
private final DescriptionCreator creator;
|
||||
private ElementTypeDescription description;
|
||||
private ImageIcon icon;
|
||||
|
||||
/**
|
||||
* Creates a new node with the given name.
|
||||
@ -62,7 +66,7 @@ public class LibraryNode implements Iterable<LibraryNode> {
|
||||
* Adds a node.
|
||||
* Throws an exception if this node is a leaf
|
||||
*
|
||||
* @param node
|
||||
* @param node the node to add
|
||||
*/
|
||||
void add(LibraryNode node) {
|
||||
children.add(node);
|
||||
@ -188,4 +192,30 @@ public class LibraryNode implements Iterable<LibraryNode> {
|
||||
public String toString() {
|
||||
return translatedName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the icon.
|
||||
* If icon not available the icon is created
|
||||
*
|
||||
* @param shapeFactory the shape factory to create the icon
|
||||
* @return the icon
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public ImageIcon getIcon(ShapeFactory shapeFactory) throws IOException {
|
||||
getDescription();
|
||||
return getIconOrNull(shapeFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the icon.
|
||||
* If icon not available null is returned
|
||||
*
|
||||
* @param shapeFactory the shape factory to create the icon
|
||||
* @return the icon or null
|
||||
*/
|
||||
public ImageIcon getIconOrNull(ShapeFactory shapeFactory) {
|
||||
if (icon == null && description != null)
|
||||
icon = new VisualElement(description.getName()).setShapeFactory(shapeFactory).createIcon(75);
|
||||
return icon;
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ public final class InsertAction extends ToolTipAction {
|
||||
* @param shapeFactory the shapeFactory to create the icon
|
||||
*/
|
||||
public InsertAction(LibraryNode node, InsertHistory insertHistory, CircuitComponent circuitComponent, ShapeFactory shapeFactory) {
|
||||
super(node.getTranslatedName(), createIcon(node, shapeFactory));
|
||||
super(node.getTranslatedName(), node.getIconOrNull(shapeFactory));
|
||||
this.shapeFactory = shapeFactory;
|
||||
this.node = node;
|
||||
this.insertHistory = insertHistory;
|
||||
@ -47,7 +47,7 @@ public final class InsertAction extends ToolTipAction {
|
||||
if (getIcon() == null) {
|
||||
try {
|
||||
node.getDescription();
|
||||
setIcon(createIcon(node, shapeFactory));
|
||||
setIcon(node.getIcon(shapeFactory));
|
||||
} catch (IOException ex) {
|
||||
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
|
||||
}
|
||||
@ -69,17 +69,4 @@ public final class InsertAction extends ToolTipAction {
|
||||
return node.getName();
|
||||
}
|
||||
|
||||
private static ImageIcon createIcon(LibraryNode node, ShapeFactory shapeFactory) {
|
||||
// doesn't load the description if only the icon is needed
|
||||
// create action without an icon instead
|
||||
if (node.isDescriptionLoaded()) {
|
||||
try {
|
||||
return new VisualElement(node.getDescription().getName()).setShapeFactory(shapeFactory).createIcon(75);
|
||||
} catch (IOException ex) {
|
||||
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,9 @@ import de.neemann.digital.gui.InsertHistory;
|
||||
import de.neemann.digital.gui.components.CircuitComponent;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.tree.DefaultTreeCellRenderer;
|
||||
import javax.swing.tree.TreePath;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.IOException;
|
||||
@ -21,6 +23,8 @@ import java.io.IOException;
|
||||
*/
|
||||
public class SelectTree extends JTree {
|
||||
|
||||
private final ShapeFactory shapeFactory;
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
*
|
||||
@ -31,6 +35,7 @@ public class SelectTree extends JTree {
|
||||
*/
|
||||
public SelectTree(ElementLibrary library, CircuitComponent component, ShapeFactory shapeFactory, InsertHistory insertHistory) {
|
||||
super(new MyTreeModel(library));
|
||||
this.shapeFactory = shapeFactory;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent mouseEvent) {
|
||||
@ -50,6 +55,26 @@ public class SelectTree extends JTree {
|
||||
|
||||
}
|
||||
});
|
||||
setCellRenderer(new MyCellRenderer());
|
||||
}
|
||||
|
||||
private class MyCellRenderer extends DefaultTreeCellRenderer {
|
||||
@Override
|
||||
public Component getTreeCellRendererComponent(JTree tree,
|
||||
Object value,
|
||||
boolean selected,
|
||||
boolean expanded,
|
||||
boolean leaf,
|
||||
int row,
|
||||
boolean hasFocus) {
|
||||
JLabel comp = (JLabel) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
|
||||
|
||||
if (leaf)
|
||||
comp.setIcon(((LibraryNode) value).getIconOrNull(shapeFactory));
|
||||
else
|
||||
comp.setIcon(null);
|
||||
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ public class SelectTreeDialog extends JDialog {
|
||||
* @param insertHistory the insert history
|
||||
*/
|
||||
public SelectTreeDialog(Main main, ElementLibrary library, CircuitComponent component, ShapeFactory shapeFactory, InsertHistory insertHistory) {
|
||||
super(main, Lang.get("select"), false);
|
||||
super(main, Lang.get("menu_elements"), false);
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
|
||||
JTree tree = new SelectTree(library, component, shapeFactory, insertHistory);
|
||||
@ -44,7 +44,5 @@ public class SelectTreeDialog extends JDialog {
|
||||
pack();
|
||||
setSize(getWidth(), main.getHeight());
|
||||
setLocation(main.getLocation().x - getWidth(), main.getLocation().y);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user