Added/Changed custom UI components

MaterialTabs:

- Added a second constructor for use cases where the programmer
does not want the screen switching functionality, only the tab
selection functionality (think of them as fancy radio buttons
- Tweaked the border on the material tab so that the highlight overlays
instead of appending to the bottom.

IconTextField:
- Fixed copyright link
- Refactored to include the new FlatTextField (code was taken from this
class to produce the new class)

FlatTextField:
- Added this new component, it is pretty much the same as IconTextField
but used for just text input purposes, no icons.
This commit is contained in:
Ruben Amendoeira
2018-05-21 18:31:07 +01:00
parent 48368019e6
commit 84213d1eff
4 changed files with 232 additions and 43 deletions

View File

@@ -0,0 +1,170 @@
/*
* Copyright (c) 2018, Psikoi <https://github.com/psikoi>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.ui.components;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;
import lombok.Getter;
import net.runelite.client.ui.ColorScheme;
/**
* This component is a JTextField with a flat design look.
*/
public class FlatTextField extends JPanel
{
@Getter
private final JTextField textField;
//the default background color, this needs to be stored for hover effects
@Getter
private Color backgroundColor = ColorScheme.DARKER_GRAY_COLOR;
//the default hover background color, this needs to be stored for hover effects
@Getter
private Color hoverBackgroundColor;
// the input can be blocked (no clicking, no editing, no hover effects)
@Getter
private boolean blocked;
public FlatTextField()
{
setLayout(new BorderLayout());
setBorder(new EmptyBorder(0, 10, 0, 0));
this.textField = new JTextField();
this.textField.setBorder(null);
this.textField.setOpaque(false);
this.textField.setSelectedTextColor(Color.WHITE);
this.textField.setSelectionColor(ColorScheme.BRAND_ORANGE_TRANSPARENT);
add(textField, BorderLayout.CENTER);
textField.addMouseListener(new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent mouseEvent)
{
if (blocked)
{
return;
}
if (hoverBackgroundColor != null)
{
setBackground(hoverBackgroundColor, false);
}
}
@Override
public void mouseExited(MouseEvent mouseEvent)
{
setBackground(backgroundColor);
}
});
}
public void addActionListener(ActionListener actionListener)
{
textField.addActionListener(actionListener);
}
public String getText()
{
return textField.getText();
}
public void setText(String text)
{
textField.setText(text);
}
public void addInputKeyListener(KeyListener l)
{
textField.addKeyListener(l);
}
public void removeInputKeyListener(KeyListener l)
{
textField.removeKeyListener(l);
}
@Override
public void setBackground(Color color)
{
setBackground(color, true);
}
public void setBackground(Color color, boolean saveColor)
{
if (color == null)
{
return;
}
super.setBackground(color);
if (saveColor)
{
this.backgroundColor = color;
}
}
public void setHoverBackgroundColor(Color color)
{
if (color == null)
{
return;
}
this.hoverBackgroundColor = color;
}
public void setEditable(boolean editable)
{
this.blocked = !editable;
textField.setEditable(editable);
textField.setFocusable(editable);
if (!editable)
{
super.setBackground(backgroundColor);
}
}
public Document getDocument()
{
return textField.getDocument();
}
}

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* Copyright (c) 2018, Psikoi <Adam@sigterm.info>
* Copyright (c) 2018, Psikoi <https://github.com/psikoi>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -33,31 +33,23 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.Document;
import net.runelite.client.ui.ColorScheme;
/**
* This component is a JTextField with an icon on its left side.
* This component is a FlatTextField with an icon on its left side.
*/
public class IconTextField extends JPanel
{
private final JTextField textField;
private final FlatTextField textField;
//to support gifs, the icon needs to be wrapped in a JLabel
private final JLabel iconWrapperLabel;
//the default background color, this needs to be stored for hover effects
private Color backgroundColor = ColorScheme.DARKER_GRAY_COLOR;
//the default hover background color, this needs to be stored for hover effects
private Color hoverBackgroundColor;
// the input can be blocked (no clicking, no editing, no hover effects)
private boolean blocked;
public IconTextField()
{
setLayout(new BorderLayout());
@@ -67,35 +59,44 @@ public class IconTextField extends JPanel
this.iconWrapperLabel.setVerticalAlignment(JLabel.CENTER);
this.iconWrapperLabel.setHorizontalAlignment(JLabel.CENTER);
this.textField = new JTextField();
this.textField.setBorder(null);
this.textField.setOpaque(false);
textField = new FlatTextField();
textField.setBorder(null);
add(iconWrapperLabel, BorderLayout.WEST);
add(textField, BorderLayout.CENTER);
JTextField innerTxt = textField.getTextField();
innerTxt.removeMouseListener(innerTxt.getMouseListeners()[innerTxt.getMouseListeners().length - 1]);
textField.addMouseListener(new MouseAdapter()
MouseListener hoverEffect = new MouseAdapter()
{
@Override
public void mouseEntered(MouseEvent mouseEvent)
{
if (blocked)
if (textField.isBlocked())
{
return;
}
if (hoverBackgroundColor != null)
Color hoverColor = textField.getHoverBackgroundColor();
if (hoverColor != null)
{
IconTextField.super.setBackground(hoverBackgroundColor);
IconTextField.super.setBackground(hoverColor);
textField.setBackground(hoverColor, false);
}
}
@Override
public void mouseExited(MouseEvent mouseEvent)
{
IconTextField.super.setBackground(backgroundColor);
setBackground(textField.getBackgroundColor());
}
});
};
textField.addMouseListener(hoverEffect);
innerTxt.addMouseListener(hoverEffect);
add(iconWrapperLabel, BorderLayout.WEST);
add(textField, BorderLayout.CENTER);
}
public void addActionListener(ActionListener actionListener)
@@ -125,8 +126,23 @@ public class IconTextField extends JPanel
{
return;
}
super.setBackground(color);
this.backgroundColor = color;
if (textField != null)
{
textField.setBackground(color);
}
}
public void setHoverBackgroundColor(Color hoverBackgroundColor)
{
if (hoverBackgroundColor == null)
{
return;
}
this.textField.setHoverBackgroundColor(hoverBackgroundColor);
}
public void addInputKeyListener(KeyListener l)
@@ -139,23 +155,12 @@ public class IconTextField extends JPanel
textField.removeKeyListener(l);
}
public void setHoverBackgroundColor(Color hoverBackgroundColor)
{
if (hoverBackgroundColor == null)
{
return;
}
this.hoverBackgroundColor = hoverBackgroundColor;
}
public void setEditable(boolean editable)
{
this.blocked = !editable;
textField.setEditable(editable);
textField.setFocusable(editable);
if (!editable)
{
super.setBackground(backgroundColor);
super.setBackground(textField.getBackgroundColor());
}
}
@@ -163,7 +168,7 @@ public class IconTextField extends JPanel
public boolean requestFocusInWindow()
{
super.requestFocusInWindow();
return textField.requestFocusInWindow();
return textField.requestFocusInWindow();
}
public Document getDocument()

View File

@@ -48,7 +48,7 @@ public class MaterialTab extends JLabel
{
private static final Border SELECTED_BORDER = new CompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, ColorScheme.BRAND_ORANGE),
BorderFactory.createEmptyBorder(5, 10, 5, 10));
BorderFactory.createEmptyBorder(5, 10, 4, 10));
private static final Border UNSELECTED_BORDER = BorderFactory
.createEmptyBorder(5, 10, 5, 10);

View File

@@ -58,11 +58,19 @@ public class MaterialTabGroup extends JPanel
public MaterialTabGroup(JPanel display)
{
this.display = display;
this.display.setLayout(new BorderLayout());
if (display != null)
{
this.display.setLayout(new BorderLayout());
}
setLayout(new FlowLayout(FlowLayout.CENTER, 8, 0));
setOpaque(false);
}
public MaterialTabGroup()
{
this(null);
}
/* Returns the tab on a certain index. */
public MaterialTab getTab(int index)
{
@@ -93,16 +101,22 @@ public class MaterialTabGroup extends JPanel
return false;
}
display.removeAll();
if (display != null)
{
display.removeAll();
}
for (MaterialTab tab : tabs)
{
if (tab.equals(selectedTab))
{
tab.select();
display.add(tab.getContent());
display.revalidate();
display.repaint();
if (display != null)
{
display.add(tab.getContent());
display.revalidate();
display.repaint();
}
}
else
{