implemented ObjTreeCellRender & ObjListCellRenderer

master
Niclas Thobaben 2018-06-21 14:41:31 +02:00
parent 819167518e
commit 073cf038a4
16 changed files with 260 additions and 87 deletions

View File

@ -2,15 +2,13 @@ package nt.UI;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class NtUIHelper {
public static Font getSymbolFont() {
try {
return Font.createFont(Font.TYPE1_FONT, NtUIHelper.class.getResourceAsStream("/nt/UI/LigatureSymbols-2.11.ttf"));
return Font.createFont(Font.TRUETYPE_FONT, NtUIHelper.class.getResourceAsStream("/nt/UI/LigatureSymbols-2.11.ttf"));
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}

View File

@ -2,22 +2,13 @@ package nt.UI.control;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import nt.UI.control.interfaces.SearchBarFinder;

View File

@ -3,46 +3,33 @@ package nt.UI.control;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import nt.UI.util.DocumentAdapter;
public class JSearchBarController extends KeyAdapter implements DocumentListener, FocusListener, ComponentListener, HierarchyBoundsListener, ListSelectionListener{
private static final String COMPLETION_CANCEL_KEY = "comp.cancel";
private static final String SELECT_KEY = "comp.select";
private static final KeyStroke COMPLETION_CANCEL_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
private static final KeyStroke SELECT_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
private JSearchBar searchBar;

View File

@ -1,25 +1,21 @@
package nt.UI.control;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.text.JTextComponent;
import nt.UI.NtUIHelper;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import nt.UI.NtUIHelper;
public class JSearchTextField extends JPanel{
@ -35,7 +31,7 @@ public class JSearchTextField extends JPanel{
super();
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
Font symbols = NtUIHelper.getSymbolFont();
Font symbols = NtUIHelper.getSymbolFont().deriveFont(18f);
horizontalStrut_1 = Box.createHorizontalStrut(20);
add(horizontalStrut_1);

Binary file not shown.

After

Width:  |  Height:  |  Size: 539 B

View File

@ -0,0 +1,88 @@
package nt.UI.display;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileSystemView;
import javafx.stage.FileChooser;
import nt.UI.util.IconResizer;
import nt.UI.util.Icons;
public class ObjListCellRenderer implements ListCellRenderer<Object>{
private HashMap<Class<?>, Icon> mappedIcons = new HashMap<>();
private Color bgEven = new Color(Integer.decode("#e8e8e8"));
private Color bgOdd = Color.WHITE;
private Color bgSelect = new Color(Integer.decode("#5e87ed"));
public ObjListCellRenderer() {
putIcon(String.class, Icons.STRING_SYMBOL_URL);
putIcon(Long.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Integer.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Short.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Float.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Double.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Object.class, Icons.OBJECT_SYMBOL_URL);
}
@Override
public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
if(value == null) {
return null;
}
Icon icon = mappedIcons.get(value.getClass());
icon = icon==null? new ImageIcon(Icons.FILE_SYMBOL_URL): icon;
int fHeight = list.getFontMetrics(list.getFont()).getHeight() - 4;
int iWidth = (int)((fHeight / (float)icon.getIconHeight()) * icon.getIconWidth());
icon = IconResizer.resizeIcon(icon, iWidth, fHeight);
JLabel label = new JLabel(value.toString());
label.setIconTextGap(10);
label.setFont(list.getFont());
label.setOpaque(true);
label.setIcon(icon);
Color bg = (index % 2)==0?bgEven:bgOdd;
bg = isSelected? bgSelect : bg;
Color fg = isSelected? list.getSelectionForeground() : list.getForeground();
label.setBackground(bg);
label.setForeground(fg);
return label;
}
public void putIcon(Class<?> clazz, Icon icon) {
this.mappedIcons.put(clazz, icon);
}
public void putIcon(Class<?> clazz, URL url) {
putIcon(clazz, new ImageIcon(url));
}
public Color getBackgroundEven() {
return this.bgEven;
}
public void setBackgroundEven(Color bg) {
this.bgEven = bg;
}
public Color getBackgroundOdd() {
return this.bgOdd;
}
public void setBackgroundOdd(Color bg) {
this.bgOdd = bg;
}
}

View File

@ -0,0 +1,69 @@
package nt.UI.display;
import java.awt.Color;
import java.awt.Component;
import java.net.URL;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import nt.UI.util.IconResizer;
import nt.UI.util.Icons;
public class ObjTreeCellRenderer implements TreeCellRenderer{
private HashMap<Class<?>, Icon> mappedIcons = new HashMap<>();
private Color bgSelect = new Color(Integer.decode("#5e87ed"));
public ObjTreeCellRenderer() {
putIcon(String.class, Icons.STRING_SYMBOL_URL);
putIcon(Long.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Integer.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Short.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Float.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Double.class, Icons.NUMBER_SYMBOL_URL);
putIcon(Object.class, Icons.OBJECT_SYMBOL_URL);
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
if(value == null) {
return null;
}
Class<?> valClass = value.getClass();
valClass = valClass==DefaultMutableTreeNode.class? ((DefaultMutableTreeNode)value).getUserObject().getClass() : valClass;
Icon icon = leaf?mappedIcons.get(valClass) : expanded? new ImageIcon(Icons.DIRECTORY_SYMBOL_URL) : new ImageIcon(Icons.SUBMODULE_SYMBOL_URL);
icon = icon==null? new ImageIcon(Icons.FILE_SYMBOL_URL): icon;
int fHeight = tree.getFontMetrics(tree.getFont()).getHeight() - 4;
int iWidth = (int)((fHeight / (float)icon.getIconHeight()) * icon.getIconWidth());
icon = IconResizer.resizeIcon(icon, iWidth, fHeight);
JLabel label = new JLabel(value.toString());
label.setIconTextGap(10);
label.setFont(tree.getFont());
label.setOpaque(true);
label.setIcon(icon);
Color bg = selected? bgSelect : tree.getBackground();
Color fg = tree.getForeground();
label.setBackground(bg);
label.setForeground(fg);
return label;
}
public void putIcon(Class<?> clazz, Icon icon) {
this.mappedIcons.put(clazz, icon);
}
public void putIcon(Class<?> clazz, URL url) {
putIcon(clazz, new ImageIcon(url));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 690 B

View File

@ -1,26 +1,28 @@
package nt.UI.tests;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import nt.UI.NtUIHelper;
import nt.UI.control.JSearchBar;
import nt.UI.control.JSearchTextField;
import nt.UI.control.interfaces.SearchBarFinder;
import javax.swing.BoxLayout;
import java.awt.Component;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import nt.UI.NtUIHelper;
import nt.UI.control.JSearchBar;
import nt.UI.control.interfaces.SearchBarFinder;
import nt.UI.display.ObjTreeCellRenderer;
public class UITestMain extends JFrame{
private static final long serialVersionUID = 1L;
private static final boolean DEBUG_LAF = false;
public static void main(String[] args) {
@ -57,17 +59,35 @@ public class UITestMain extends JFrame{
return results.toArray(new String[0]);
}
}));
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode("root"));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode("Hello"));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode("There"));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode(new Object()));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode(1222));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode(12222222222L));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode(12.232));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode(12.4444f));
((DefaultMutableTreeNode)model.getRoot()).add(new DefaultMutableTreeNode(new TestFolder()));
Component verticalStrut = Box.createVerticalStrut(300);
getContentPane().add(verticalStrut);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane);
JTree tree = new JTree(model);
scrollPane.setViewportView(tree);
tree.setCellRenderer(new ObjTreeCellRenderer());
JButton btnBtn = new JButton("btn");
getContentPane().add(btnBtn);
JSearchTextField searchTextField = new JSearchTextField();
getContentPane().add(searchTextField);
searchTextField.requestFocus();
}
public class TestFolder {
@Override
public String toString() {
return "Category";
}
}
}

View File

@ -1,29 +0,0 @@
package nt.UI.util;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public abstract class DocumentAdapter implements DocumentListener{
public enum DEType {
INSERT, REMOVE, CHANGE
};
@Override
public void insertUpdate(DocumentEvent e) {
documentChanged(e, DEType.INSERT);
}
@Override
public void removeUpdate(DocumentEvent e) {
documentChanged(e, DEType.REMOVE);
}
@Override
public void changedUpdate(DocumentEvent e) {
documentChanged(e, DEType.CHANGE);
}
public abstract void documentChanged(DocumentEvent e, DEType type);
}

View File

@ -0,0 +1,31 @@
package nt.UI.util;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class IconResizer {
public static Icon resizeIcon(Icon icon, int width, int height) {
Icon newIcon = null;
if(icon != null && ImageIcon.class.isInstance(icon)
&& (icon.getIconHeight() != height && icon.getIconWidth() != width)) {
Image img = ((ImageIcon)icon).getImage();
BufferedImage bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bImg.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(img, 0, 0, width, height, null, null);
g2d.dispose();
newIcon = new ImageIcon(bImg);
}else {
newIcon = icon;
}
return newIcon;
}
}

View File

@ -0,0 +1,22 @@
package nt.UI.util;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class Icons {
public static final URL FILE_SYMBOL_URL = Icons.class.getResource("/nt/UI/file_icn.png");
public static final URL DIRECTORY_SYMBOL_URL = Icons.class.getResource("/nt/UI/directory_icn.png");
public static final URL STRING_SYMBOL_URL = Icons.class.getResource("/nt/UI/string_icn.png");
public static final URL NUMBER_SYMBOL_URL = Icons.class.getResource("/nt/UI/number_icn.png");
public static final URL OBJECT_SYMBOL_URL = Icons.class.getResource("/nt/UI/object_icn.png");
public static final URL SUBMODULE_SYMBOL_URL = Icons.class.getResource("/nt/UI/submodule_icn.png");
public static Icon getIcon(URL icn) {
return new ImageIcon(icn);
}
}