create MenuButton abstract class and pull up common code from sub classes.
parent
0e9e8e042f
commit
003d9bc73c
|
@ -1,17 +1,10 @@
|
|||
package magic.ui.screen.menu;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.SwingUtilities;
|
||||
import magic.awt.MagicFont;
|
||||
import magic.data.GeneralConfig;
|
||||
import magic.data.MagicIcon;
|
||||
|
@ -23,107 +16,28 @@ import magic.ui.helpers.ImageHelper;
|
|||
import magic.ui.utility.MagicStyle;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class CustomMenuButton extends JButton {
|
||||
class CustomMenuButton extends MenuButton {
|
||||
|
||||
// translatable strings
|
||||
private static final String _S1 = "Close";
|
||||
|
||||
private final static Color COLOR_NORMAL = Color.WHITE;
|
||||
private final static Color COLOR_DISABLED = Color.DARK_GRAY;
|
||||
private static final Font CUSTOM_FONT = MagicFont.JaceBelerenBold.get().deriveFont(32f);
|
||||
private static final Font DEFAULT_FONT = FontsAndBorders.FONT_MENU_BUTTON.deriveFont(30.0f);
|
||||
|
||||
private boolean isRunnable;
|
||||
private boolean hasSeparator;
|
||||
|
||||
CustomMenuButton(final String caption, final AbstractAction action, final String tooltip, final boolean showSeparator) {
|
||||
super(caption);
|
||||
this.isRunnable = (action != null);
|
||||
this.hasSeparator = showSeparator;
|
||||
CustomMenuButton(String caption, AbstractAction action, String tooltip, boolean showSeparator) {
|
||||
super(caption, action, tooltip);
|
||||
setFont(getDisplayFont());
|
||||
setHorizontalAlignment(SwingConstants.CENTER);
|
||||
setForeground(COLOR_NORMAL);
|
||||
setButtonTransparent();
|
||||
setFocusable(true);
|
||||
setToolTipText(tooltip);
|
||||
if (isRunnable) {
|
||||
setMouseAdapter();
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
addActionListener(action);
|
||||
}
|
||||
}
|
||||
CustomMenuButton(final String caption, final AbstractAction action, final String tooltip) {
|
||||
|
||||
CustomMenuButton(String caption, AbstractAction action, String tooltip) {
|
||||
this(caption, action, tooltip, true);
|
||||
}
|
||||
CustomMenuButton(final String caption, final AbstractAction action) {
|
||||
|
||||
CustomMenuButton(String caption, AbstractAction action) {
|
||||
this(caption, action, null);
|
||||
}
|
||||
|
||||
CustomMenuButton() {
|
||||
isRunnable = false;
|
||||
hasSeparator = false;
|
||||
// default to super class.
|
||||
}
|
||||
|
||||
public boolean isRunnable() {
|
||||
return isRunnable;
|
||||
}
|
||||
|
||||
private void setButtonTransparent() {
|
||||
setOpaque(false);
|
||||
setContentAreaFilled(false);
|
||||
setBorderPainted(false);
|
||||
if (!isRunnable) {
|
||||
setBorder(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void setMouseAdapter() {
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(MagicStyle.getRolloverColor());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(Color.WHITE);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (isEnabled() && SwingUtilities.isLeftMouseButton(e)) {
|
||||
setForeground(MagicStyle.getPressedColor());
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(Color.WHITE);
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean b) {
|
||||
super.setEnabled(b);
|
||||
isRunnable = b;
|
||||
setForeground(b ? COLOR_NORMAL : COLOR_DISABLED);
|
||||
}
|
||||
|
||||
public boolean hasSeparator() {
|
||||
return hasSeparator;
|
||||
}
|
||||
|
||||
public void setSeparator(boolean b) {
|
||||
hasSeparator = b;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Static convenience methods.
|
||||
//
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
package magic.ui.screen.menu;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.SwingUtilities;
|
||||
import magic.ui.utility.MagicStyle;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class MenuButton extends JButton {
|
||||
|
||||
// translatable strings
|
||||
protected static final String _S1 = "Close";
|
||||
|
||||
protected static final Color COLOR_NORMAL = Color.WHITE;
|
||||
protected static final Color COLOR_DISABLED = Color.DARK_GRAY;
|
||||
|
||||
protected boolean isRunnable;
|
||||
|
||||
public MenuButton() {
|
||||
isRunnable = false;
|
||||
}
|
||||
|
||||
public MenuButton(String text, AbstractAction action, String tooltip) {
|
||||
super(text);
|
||||
this.isRunnable = (action != null);
|
||||
setHorizontalAlignment(SwingConstants.CENTER);
|
||||
setForeground(COLOR_NORMAL);
|
||||
setButtonTransparent();
|
||||
setFocusable(true);
|
||||
setToolTipText(tooltip);
|
||||
if (isRunnable) {
|
||||
setMouseAdapter();
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
addActionListener(action);
|
||||
}
|
||||
}
|
||||
|
||||
private void setButtonTransparent() {
|
||||
setOpaque(false);
|
||||
setContentAreaFilled(false);
|
||||
setBorderPainted(false);
|
||||
if (!isRunnable) {
|
||||
setBorder(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void setMouseAdapter() {
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(MagicStyle.getRolloverColor());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(Color.WHITE);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (isEnabled() && SwingUtilities.isLeftMouseButton(e)) {
|
||||
setForeground(MagicStyle.getPressedColor());
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(Color.WHITE);
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean b) {
|
||||
super.setEnabled(b);
|
||||
isRunnable = b;
|
||||
setForeground(b ? COLOR_NORMAL : COLOR_DISABLED);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,61 +1,39 @@
|
|||
package magic.ui.screen.widget;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.SwingUtilities;
|
||||
import magic.data.MagicIcon;
|
||||
import magic.translate.MText;
|
||||
import magic.ui.FontsAndBorders;
|
||||
import magic.ui.MagicImages;
|
||||
import magic.ui.ScreenController;
|
||||
import magic.ui.helpers.ImageHelper;
|
||||
import magic.ui.screen.menu.MenuButton;
|
||||
import magic.ui.utility.MagicStyle;
|
||||
import magic.ui.FontsAndBorders;
|
||||
import magic.ui.widget.button.LayoutButton;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class PlainMenuButton extends JButton {
|
||||
public class PlainMenuButton extends MenuButton {
|
||||
|
||||
// translatable strings
|
||||
private static final String _S1 = "Close";
|
||||
private final boolean hasSeparator;
|
||||
|
||||
private final static Color COLOR_NORMAL = Color.WHITE;
|
||||
private final static Color COLOR_DISABLED = Color.DARK_GRAY;
|
||||
|
||||
private boolean isRunnable;
|
||||
private boolean hasSeparator;
|
||||
|
||||
public PlainMenuButton(final String caption, final AbstractAction action, final String tooltip, final boolean showSeparator) {
|
||||
super(caption);
|
||||
this.isRunnable = (action != null);
|
||||
public PlainMenuButton(String caption, AbstractAction action, String tooltip, boolean showSeparator) {
|
||||
super(caption, action, tooltip);
|
||||
this.hasSeparator = showSeparator;
|
||||
setFont(FontsAndBorders.FONT_MENU_BUTTON);
|
||||
setHorizontalAlignment(SwingConstants.CENTER);
|
||||
setForeground(COLOR_NORMAL);
|
||||
setButtonTransparent();
|
||||
setFocusable(true);
|
||||
setToolTipText(tooltip);
|
||||
if (isRunnable) {
|
||||
setMouseAdapter();
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
addActionListener(action);
|
||||
}
|
||||
}
|
||||
public PlainMenuButton(final String caption, final AbstractAction action, final String tooltip) {
|
||||
|
||||
public PlainMenuButton(String caption, AbstractAction action, String tooltip) {
|
||||
this(caption, action, tooltip, true);
|
||||
}
|
||||
public PlainMenuButton(final String caption, final AbstractAction action) {
|
||||
|
||||
public PlainMenuButton(String caption, AbstractAction action) {
|
||||
this(caption, action, null);
|
||||
}
|
||||
protected PlainMenuButton() {
|
||||
isRunnable = false;
|
||||
|
||||
public PlainMenuButton() {
|
||||
hasSeparator = false;
|
||||
}
|
||||
|
||||
|
@ -63,63 +41,10 @@ public class PlainMenuButton extends JButton {
|
|||
return isRunnable;
|
||||
}
|
||||
|
||||
private void setButtonTransparent() {
|
||||
setOpaque(false);
|
||||
setContentAreaFilled(false);
|
||||
setBorderPainted(false);
|
||||
if (!isRunnable) {
|
||||
setBorder(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void setMouseAdapter() {
|
||||
addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(MagicStyle.getRolloverColor());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(Color.WHITE);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (isEnabled() && SwingUtilities.isLeftMouseButton(e)) {
|
||||
setForeground(MagicStyle.getPressedColor());
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
if (isEnabled()) {
|
||||
setForeground(Color.WHITE);
|
||||
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean b) {
|
||||
super.setEnabled(b);
|
||||
isRunnable = b;
|
||||
setForeground(b ? COLOR_NORMAL : COLOR_DISABLED);
|
||||
}
|
||||
|
||||
public boolean hasSeparator() {
|
||||
return hasSeparator;
|
||||
}
|
||||
|
||||
public void setSeparator(boolean b) {
|
||||
hasSeparator = b;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Static convenience methods.
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue