From a72323cd2db187c4fa47b131a7f12a55c4d56c7b Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Fri, 22 Jun 2018 15:58:11 +0200 Subject: [PATCH] implemented JSearchBar --- .classpath | 2 +- META-INF/MANIFEST.MF | 2 + src/nt.jardesc | 15 +--- src/nt/UI/control/JSearchBar.java | 4 + src/nt/UI/control/JSearchBarController.java | 3 +- src/nt/UI/control/JSearchTextField.java | 51 ++++++++--- .../interfaces/DefaultSearchBarFinder.java | 15 ++++ .../control/interfaces/SearchBarFinder.java | 1 + src/nt/UI/display/ObjTableCellRenderer.java | 88 +++++++++++++++++++ src/nt/UI/tests/UITestMain.java | 4 + 10 files changed, 156 insertions(+), 29 deletions(-) create mode 100644 META-INF/MANIFEST.MF create mode 100644 src/nt/UI/control/interfaces/DefaultSearchBarFinder.java create mode 100644 src/nt/UI/display/ObjTableCellRenderer.java diff --git a/.classpath b/.classpath index fb50116..d0b666e 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,6 @@ - + diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e326397 --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-version: 1.0 +Created-by: Niclas Thobaben (DrBender GmbH) \ No newline at end of file diff --git a/src/nt.jardesc b/src/nt.jardesc index 82c9b60..ac8d88a 100644 --- a/src/nt.jardesc +++ b/src/nt.jardesc @@ -1,25 +1,16 @@ - + - + - - - - - - - - - - + diff --git a/src/nt/UI/control/JSearchBar.java b/src/nt/UI/control/JSearchBar.java index 164cb61..378f4b7 100644 --- a/src/nt/UI/control/JSearchBar.java +++ b/src/nt/UI/control/JSearchBar.java @@ -10,6 +10,7 @@ import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JWindow; +import nt.UI.control.interfaces.DefaultSearchBarFinder; import nt.UI.control.interfaces.SearchBarFinder; public class JSearchBar extends JSearchTextField{ @@ -23,6 +24,9 @@ public class JSearchBar extends JSearchTextField{ protected String lastEntered = ""; private final JSearchBarController controller; + public JSearchBar() { + this(new DefaultSearchBarFinder()); + } public JSearchBar(SearchBarFinder finder) { this.finder = finder; this.dropDown = new SearchDropDown(); diff --git a/src/nt/UI/control/JSearchBarController.java b/src/nt/UI/control/JSearchBarController.java index 94329d7..3f58042 100644 --- a/src/nt/UI/control/JSearchBarController.java +++ b/src/nt/UI/control/JSearchBarController.java @@ -112,8 +112,7 @@ public class JSearchBarController extends KeyAdapter implements DocumentListener if(e.getKeyCode()==KeyEvent.VK_ENTER) { searchBar.txtSearch.setText(list.getSelectedValue()); - System.out.println(list.getSelectedValue()); - System.out.println(searchBar.txtSearch.getText()); + searchBar.finder.acceptedResult(list.getSelectedValue()); } } } diff --git a/src/nt/UI/control/JSearchTextField.java b/src/nt/UI/control/JSearchTextField.java index 159be91..52b4060 100644 --- a/src/nt/UI/control/JSearchTextField.java +++ b/src/nt/UI/control/JSearchTextField.java @@ -2,6 +2,7 @@ package nt.UI.control; import java.awt.Color; import java.awt.Component; +import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionListener; @@ -16,6 +17,10 @@ import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; import nt.UI.NtUIHelper; +import java.awt.Dimension; +import java.awt.GridBagLayout; +import java.awt.GridBagConstraints; +import java.awt.Insets; public class JSearchTextField extends JPanel{ @@ -25,41 +30,60 @@ public class JSearchTextField extends JPanel{ protected JTextField txtSearch; protected JButton btnCancel; private Component horizontalStrut; - private Component horizontalStrut_1; public JSearchTextField() { super(); - setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); Font symbols = NtUIHelper.getSymbolFont().deriveFont(18f); - - horizontalStrut_1 = Box.createHorizontalStrut(20); - add(horizontalStrut_1); + GridBagLayout gridBagLayout = new GridBagLayout(); + gridBagLayout.columnWidths = new int[]{42, 31, 255, 53, 0}; + gridBagLayout.rowHeights = new int[]{29, 0}; + gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE}; + gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE}; + setLayout(gridBagLayout); lblLens = new JLabel("\uE116"); lblLens.setFont(symbols); - add(lblLens); + GridBagConstraints gbc_lblLens = new GridBagConstraints(); + gbc_lblLens.fill = GridBagConstraints.VERTICAL; + gbc_lblLens.insets = new Insets(0, 0, 0, 5); + gbc_lblLens.gridx = 0; + gbc_lblLens.gridy = 0; + add(lblLens, gbc_lblLens); horizontalStrut = Box.createHorizontalStrut(10); - add(horizontalStrut); + GridBagConstraints gbc_horizontalStrut = new GridBagConstraints(); + gbc_horizontalStrut.fill = GridBagConstraints.VERTICAL; + gbc_horizontalStrut.anchor = GridBagConstraints.WEST; + gbc_horizontalStrut.insets = new Insets(0, 0, 0, 5); + gbc_horizontalStrut.gridx = 1; + gbc_horizontalStrut.gridy = 0; + add(horizontalStrut, gbc_horizontalStrut); txtSearch = new JTextField(); txtSearch.setText("search"); txtSearch.setBorder(new EmptyBorder(0, 0, 0, 0)); txtSearch.setBackground(getBackground()); - add(txtSearch); + GridBagConstraints gbc_txtSearch = new GridBagConstraints(); + gbc_txtSearch.fill = GridBagConstraints.BOTH; + gbc_txtSearch.insets = new Insets(0, 0, 0, 5); + gbc_txtSearch.gridx = 2; + gbc_txtSearch.gridy = 0; + add(txtSearch, gbc_txtSearch); txtSearch.setColumns(10); btnCancel = new JButton("\uE10f"); - btnCancel.setHorizontalAlignment(SwingConstants.RIGHT); btnCancel.setBorderPainted(false); btnCancel.setFont(symbols); btnCancel.setOpaque(false); - btnCancel.setVisible(false); - add(btnCancel); + GridBagConstraints gbc_btnCancel = new GridBagConstraints(); + gbc_btnCancel.fill = GridBagConstraints.VERTICAL; + gbc_btnCancel.gridx = 3; + gbc_btnCancel.gridy = 0; + add(btnCancel, gbc_btnCancel); + setMinimumSize(btnCancel.getMinimumSize()); setBorder(new LineBorder(Color.LIGHT_GRAY)); - setPreferredSize(btnCancel.getPreferredSize()); - setMinimumSize(btnCancel.getMinimumSize()); + setPreferredSize(new Dimension(376, 56)); } public void addActionListener(ActionListener l) { @@ -70,7 +94,6 @@ public class JSearchTextField extends JPanel{ } public void showCancelButton(boolean b) { this.btnCancel.setVisible(b); - System.out.println(b); revalidate(); } diff --git a/src/nt/UI/control/interfaces/DefaultSearchBarFinder.java b/src/nt/UI/control/interfaces/DefaultSearchBarFinder.java new file mode 100644 index 0000000..623ee99 --- /dev/null +++ b/src/nt/UI/control/interfaces/DefaultSearchBarFinder.java @@ -0,0 +1,15 @@ +package nt.UI.control.interfaces; + +public class DefaultSearchBarFinder implements SearchBarFinder{ + + @Override + public String[] getSearchResults(String token) { + return new String[0]; + } + + @Override + public void acceptedResult(String result) { + + } + +} diff --git a/src/nt/UI/control/interfaces/SearchBarFinder.java b/src/nt/UI/control/interfaces/SearchBarFinder.java index 0189abd..2e6c7f1 100644 --- a/src/nt/UI/control/interfaces/SearchBarFinder.java +++ b/src/nt/UI/control/interfaces/SearchBarFinder.java @@ -3,5 +3,6 @@ package nt.UI.control.interfaces; public interface SearchBarFinder { public String[] getSearchResults(String token); + public void acceptedResult(String result); } diff --git a/src/nt/UI/display/ObjTableCellRenderer.java b/src/nt/UI/display/ObjTableCellRenderer.java new file mode 100644 index 0000000..66b7ece --- /dev/null +++ b/src/nt/UI/display/ObjTableCellRenderer.java @@ -0,0 +1,88 @@ +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.JTable; +import javax.swing.table.TableCellRenderer; + +import nt.UI.util.IconResizer; +import nt.UI.util.Icons; + +public class ObjTableCellRenderer implements TableCellRenderer{ + + private HashMap, 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 ObjTableCellRenderer() { + 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 getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, + int row, int column) { + JLabel label = new JLabel(value == null? "" : value.toString());; + if (column == 0) { + Class clazz = value.getClass(); + if(clazz == String.class) { + try { + clazz = Class.forName(value.toString()); + } catch (ClassNotFoundException e) { + clazz = value.getClass(); + } + } + Icon icon = mappedIcons.get(clazz); + icon = icon == null ? new ImageIcon(Icons.FILE_SYMBOL_URL) : icon; + + int fHeight = table.getFontMetrics(table.getFont()).getHeight() - 4; + int iWidth = (int) ((fHeight / (float) icon.getIconHeight()) * icon.getIconWidth()); + icon = IconResizer.resizeIcon(icon, iWidth, fHeight); + + label.setIcon(icon); + } + + Color bg = (row % 2)==0?bgEven:bgOdd; + bg = isSelected? bgSelect : bg; + Color fg = isSelected? table.getSelectionForeground() : table.getForeground(); + label.setIconTextGap(10); + label.setFont(table.getFont()); + label.setOpaque(true); + 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; + } + +} diff --git a/src/nt/UI/tests/UITestMain.java b/src/nt/UI/tests/UITestMain.java index 5c777a7..ec865be 100644 --- a/src/nt/UI/tests/UITestMain.java +++ b/src/nt/UI/tests/UITestMain.java @@ -58,6 +58,10 @@ public class UITestMain extends JFrame{ } return results.toArray(new String[0]); } + + @Override + public void acceptedResult(String result) { + } })); DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode("root"));