Skip to content

Commit

Permalink
Add some fix
Browse files Browse the repository at this point in the history
- Fixed a JSpinner actions
- Fix JtextFielad and Jpassword
- Fix jOptionPane
- Added support at list
- refactoring in MaterialLookAndFeel and adding new input event
- adding new functionality for painted JScrollBar
- redimension border into JComboBoxItem and RadioItem
  • Loading branch information
vincenzopalazzo committed Apr 19, 2019
1 parent 31e6bb3 commit 2a3b065
Show file tree
Hide file tree
Showing 9 changed files with 607 additions and 330 deletions.
223 changes: 204 additions & 19 deletions src/main/java/mdlaf/MaterialLookAndFeel.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* MIT License
* Copyright (c) 2018 atharva washimkar
* Copyright (c) 2019 Vincent Palazzo [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package mdlaf;

import mdlaf.components.button.MaterialButtonUI;
Expand All @@ -7,6 +30,7 @@
import mdlaf.components.filechooser.MaterialFileChooserUI;
import mdlaf.components.formattertextfield.MaterialFormattedTextFieldUI;
import mdlaf.components.label.MaterialLabelUI;
import mdlaf.components.list.MaterialListUI;
import mdlaf.components.menu.MaterialMenuUI;
import mdlaf.components.menubar.MaterialMenuBarUI;
import mdlaf.components.menuitem.MaterialMenuItemUI;
Expand Down Expand Up @@ -36,12 +60,13 @@
import mdlaf.utils.MaterialFonts;
import mdlaf.utils.MaterialImages;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.*;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.text.DefaultEditorKit;
import java.awt.Color;
import java.lang.reflect.Method;

public class MaterialLookAndFeel extends MetalLookAndFeel {

Expand Down Expand Up @@ -77,6 +102,18 @@ public class MaterialLookAndFeel extends MetalLookAndFeel {
private static final String taskPaneUI = MaterialTaskPaneUI.class.getCanonicalName ();
private static final String optionPaneUI = MaterialOptionPaneUI.class.getCanonicalName();
private static final String formattedTextFieldUI = MaterialFormattedTextFieldUI.class.getCanonicalName();
private static final String listUI = MaterialListUI.class.getCanonicalName();

private BasicLookAndFeel basicLookAndFeel;

public MaterialLookAndFeel() {
try {
basicLookAndFeel = new MetalLookAndFeel();
MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
} catch (Exception ignore) {
//do nothing
}
}

@Override
public String getName () {
Expand Down Expand Up @@ -135,9 +172,11 @@ protected void initClassDefaults (UIDefaults table) {
table.put ("SeparatorUI", separatorUI);
table.put ("FileChooserUI", fileChooserUI);
table.put ("ToolTipUI", toolTipUI);
table.put ("swingx/TaskPaneUI", taskPaneUI);
table.put("OptionPaneUI", optionPaneUI);
table.put("FormattedTextFieldUI", formattedTextFieldUI);
table.put("ListUI", listUI);

table.put ("swingx/TaskPaneUI", taskPaneUI);
}

@Override
Expand Down Expand Up @@ -227,13 +266,14 @@ protected void initComponentDefaults (UIDefaults table) {
table.put("Spinner.mouseHoverColor", MaterialColors.GRAY_400);

table.put ("ScrollBar.font", MaterialFonts.REGULAR);
table.put ("ScrollBar.track", MaterialColors.GRAY_200);
table.put ("ScrollBar.thumb", MaterialColors.GRAY_300);
table.put ("ScrollBar.thumbDarkShadow", MaterialColors.GRAY_300);
table.put ("ScrollBar.thumbHighlight", MaterialColors.GRAY_300);
table.put ("ScrollBar.thumbShadow", MaterialColors.GRAY_300);
table.put ("ScrollBar.arrowButtonBackground", MaterialColors.GRAY_300);
table.put("ScrollBar.arrowButtonBorder", MaterialBorders.LIGHT_LINE_BORDER);
table.put("ScrollBar.track", MaterialColors.GRAY_200);
table.put("ScrollBar.thumb", MaterialColors.GRAY_500);
table.put("ScrollBar.thumbDarkShadow", MaterialColors.GRAY_500);
table.put("ScrollBar.thumbHighlight", MaterialColors.GRAY_500);
table.put("ScrollBar.thumbShadow", MaterialColors.GRAY_500);
table.put("ScrollBar.arrowButtonBackground", MaterialColors.GRAY_200);
table.put("ScrollBar.enableArrow", true);
table.put("ScrollBar.arrowButtonBorder", BorderFactory.createEmptyBorder());
table.put("ScrollBar[MouseHover].enable", true);
table.put("ScrollBar[MouseHover].color", MaterialColors.GRAY_400);
table.put("ScrollBar[OnClick].color", MaterialColors.GRAY_500);
Expand Down Expand Up @@ -276,11 +316,19 @@ protected void initComponentDefaults (UIDefaults table) {
table.put ("TextArea.border", BorderFactory.createEmptyBorder ());
table.put ("TextArea.foreground", Color.BLACK);

table.put ("TextField.inactiveForeground", MaterialColors.GRAY_800);
table.put ("TextField.inactiveBackground", MaterialColors.GRAY_200);
table.put ("TextField.selectionBackground", MaterialColors.LIGHT_BLUE_400);
table.put ("TextField.selectionForeground", Color.BLACK);
table.put ("TextField.border", BorderFactory.createEmptyBorder(3, 5, 2, 5));
table.put("TextField.inactiveForeground", MaterialColors.GRAY_800);
table.put("TextField.inactiveBackground", MaterialColors.GRAY_200);
table.put("TextField.selectionBackground", MaterialColors.LIGHT_BLUE_400);
table.put("TextField.selectionForeground", MaterialColors.BLACK);
table.put("TextField.border", BorderFactory.createEmptyBorder(3, 5, 2, 5));
table.put("TextField.focusInputMap", fieldInputMap); //install shortcut

table.put("PasswordField.inactiveForeground", MaterialColors.GRAY_800);
table.put("PasswordField.inactiveBackground", MaterialColors.GRAY_200);
table.put("PasswordField.selectionBackground", MaterialColors.LIGHT_BLUE_400);
table.put("PasswordField.selectionForeground", MaterialColors.BLACK);
table.put("PasswordField.border", BorderFactory.createEmptyBorder(3, 5, 2, 5));
table.put("PasswordField.focusInputMap", fieldInputMap); //install shortcut

table.put ("ToggleButton.border", BorderFactory.createEmptyBorder ());
table.put ("ToggleButton.font", MaterialFonts.REGULAR);
Expand Down Expand Up @@ -310,7 +358,7 @@ protected void initComponentDefaults (UIDefaults table) {
//If it changes the background of the menuitem it must change this too, irrespective of its setting
table.put ("RadioButtonMenuItem.background", UIManager.getColor ("MenuItem.background"));
table.put ("RadioButtonMenuItem.selectionBackground", MaterialColors.GRAY_200);
table.put ("RadioButtonMenuItem.border", BorderFactory.createEmptyBorder (5, 5, 5, 5));
table.put ("RadioButtonMenuItem.border", BorderFactory.createEmptyBorder(5, 0, 5, 0));
table.put ("RadioButtonMenuItem.checkIcon", new ImageIcon (MaterialImages.RADIO_BUTTON_OFF));
table.put ("RadioButtonMenuItem.selectedCheckIcon", new ImageIcon (MaterialImages.RADIO_BUTTON_ON));

Expand All @@ -319,7 +367,7 @@ protected void initComponentDefaults (UIDefaults table) {
table.put ("CheckBoxMenuItem.selectionBackground", MaterialColors.GRAY_200);
table.put ("CheckBoxMenuItem.foreground", Color.BLACK);
table.put ("CheckBoxMenuItem.selectionForeground", Color.BLACK);
table.put ("CheckBoxMenuItem.border", BorderFactory.createEmptyBorder (5, 5, 5, 5));
table.put ("CheckBoxMenuItem.border", BorderFactory.createEmptyBorder(5, 0, 5, 0));
table.put ("CheckBoxMenuItem.checkIcon", new ImageIcon (MaterialImages.UNCHECKED_BOX));
table.put ("CheckBoxMenuItem.selectedCheckIcon", new ImageIcon (MaterialImages.PAINTED_CHECKED_BOX));

Expand Down Expand Up @@ -370,5 +418,142 @@ protected void initComponentDefaults (UIDefaults table) {
table.put ("FormattedTextField.selectionBackground", MaterialColors.LIGHT_BLUE_400);
table.put ("FormattedTextField.selectionForeground", Color.BLACK);
table.put("FormattedTextField.border", BorderFactory.createEmptyBorder(3, 5, 2, 5));

table.put("List.background", MaterialColors.WHITE);
table.put("List.foreground", MaterialColors.BLACK);
table.put("List.border", MaterialBorders.LIGHT_SHADOW_BORDER);
table.put("List.font", MaterialFonts.MEDIUM);
table.put("List.selectionBackground", MaterialColors.GRAY_400);
table.put("List.selectionForeground", MaterialColors.BLACK);
table.put("List.focusable", true);
}

@Override
public UIDefaults getDefaults() {
try {
final Method superMethod = BasicLookAndFeel.class.getDeclaredMethod("getDefaults");
superMethod.setAccessible(true);
final UIDefaults defaults = (UIDefaults)superMethod.invoke(basicLookAndFeel);
initClassDefaults(defaults);
initComponentDefaults(defaults);
defaults.put("OptionPane.warningIcon", new ImageIcon(MaterialImages.WARNING));
defaults.put("OptionPane.errorIcon", new ImageIcon(MaterialImages.ERROR));
defaults.put("OptionPane.questionIcon", new ImageIcon(MaterialImages.QUESTION));
defaults.put("OptionPane.informationIcon", new ImageIcon(MaterialImages.INFORMATION));
return defaults;
}
catch (Exception ignore) {
}
return super.getDefaults();
}

/*Shortcut for filed input*/
Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[]{
"ctrl C", DefaultEditorKit.copyAction,
"ctrl V", DefaultEditorKit.pasteAction,
"ctrl X", DefaultEditorKit.cutAction,
"COPY", DefaultEditorKit.copyAction,
"PASTE", DefaultEditorKit.pasteAction,
"CUT", DefaultEditorKit.cutAction,
"control INSERT", DefaultEditorKit.copyAction,
"shift INSERT", DefaultEditorKit.pasteAction,
"shift DELETE", DefaultEditorKit.cutAction,
"shift LEFT", DefaultEditorKit.selectionBackwardAction,
"shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,
"shift RIGHT", DefaultEditorKit.selectionForwardAction,
"shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,
"ctrl LEFT", DefaultEditorKit.previousWordAction,
"ctrl KP_LEFT", DefaultEditorKit.previousWordAction,
"ctrl RIGHT", DefaultEditorKit.nextWordAction,
"ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,
"ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl A", DefaultEditorKit.selectAllAction,
"HOME", DefaultEditorKit.beginLineAction,
"END", DefaultEditorKit.endLineAction,
"shift HOME", DefaultEditorKit.selectionBeginLineAction,
"shift END", DefaultEditorKit.selectionEndLineAction,
"BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"ctrl H", DefaultEditorKit.deletePrevCharAction,
"DELETE", DefaultEditorKit.deleteNextCharAction,
"ctrl DELETE", DefaultEditorKit.deleteNextWordAction,
"ctrl BACK_SPACE", DefaultEditorKit.deletePrevWordAction,
"RIGHT", DefaultEditorKit.forwardAction,
"LEFT", DefaultEditorKit.backwardAction,
"KP_RIGHT", DefaultEditorKit.forwardAction,
"KP_LEFT", DefaultEditorKit.backwardAction,
"ENTER", JTextField.notifyAction,
"ctrl BACK_SLASH", "unselect"/*DefaultEditorKit.unselectAction*/,
"control shift O", "toggle-componentOrientation"/*DefaultEditorKit.toggleComponentOrientation*/
});


/*Shortcut for multiline input*/
Object multilineInputMap = new UIDefaults.LazyInputMap(new Object[]{
"ctrl C", DefaultEditorKit.copyAction,
"ctrl V", DefaultEditorKit.pasteAction,
"ctrl X", DefaultEditorKit.cutAction,
"COPY", DefaultEditorKit.copyAction,
"PASTE", DefaultEditorKit.pasteAction,
"CUT", DefaultEditorKit.cutAction,
"control INSERT", DefaultEditorKit.copyAction,
"shift INSERT", DefaultEditorKit.pasteAction,
"shift DELETE", DefaultEditorKit.cutAction,
"shift LEFT", DefaultEditorKit.selectionBackwardAction,
"shift KP_LEFT", DefaultEditorKit.selectionBackwardAction,
"shift RIGHT", DefaultEditorKit.selectionForwardAction,
"shift KP_RIGHT", DefaultEditorKit.selectionForwardAction,
"ctrl LEFT", DefaultEditorKit.previousWordAction,
"ctrl KP_LEFT", DefaultEditorKit.previousWordAction,
"ctrl RIGHT", DefaultEditorKit.nextWordAction,
"ctrl KP_RIGHT", DefaultEditorKit.nextWordAction,
"ctrl shift LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift KP_LEFT", DefaultEditorKit.selectionPreviousWordAction,
"ctrl shift RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl shift KP_RIGHT", DefaultEditorKit.selectionNextWordAction,
"ctrl A", DefaultEditorKit.selectAllAction,
"HOME", DefaultEditorKit.beginLineAction,
"END", DefaultEditorKit.endLineAction,
"shift HOME", DefaultEditorKit.selectionBeginLineAction,
"shift END", DefaultEditorKit.selectionEndLineAction,

"UP", DefaultEditorKit.upAction,
"KP_UP", DefaultEditorKit.upAction,
"DOWN", DefaultEditorKit.downAction,
"KP_DOWN", DefaultEditorKit.downAction,
"PAGE_UP", DefaultEditorKit.pageUpAction,
"PAGE_DOWN", DefaultEditorKit.pageDownAction,
"shift PAGE_UP", "selection-page-up",
"shift PAGE_DOWN", "selection-page-down",
"ctrl shift PAGE_UP", "selection-page-left",
"ctrl shift PAGE_DOWN", "selection-page-right",
"shift UP", DefaultEditorKit.selectionUpAction,
"shift KP_UP", DefaultEditorKit.selectionUpAction,
"shift DOWN", DefaultEditorKit.selectionDownAction,
"shift KP_DOWN", DefaultEditorKit.selectionDownAction,
"ENTER", DefaultEditorKit.insertBreakAction,
"BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"shift BACK_SPACE", DefaultEditorKit.deletePrevCharAction,
"ctrl H", DefaultEditorKit.deletePrevCharAction,
"DELETE", DefaultEditorKit.deleteNextCharAction,
"ctrl DELETE", DefaultEditorKit.deleteNextWordAction,
"ctrl BACK_SPACE", DefaultEditorKit.deletePrevWordAction,
"RIGHT", DefaultEditorKit.forwardAction,
"LEFT", DefaultEditorKit.backwardAction,
"KP_RIGHT", DefaultEditorKit.forwardAction,
"KP_LEFT", DefaultEditorKit.backwardAction,
"TAB", DefaultEditorKit.insertTabAction,
"ctrl BACK_SLASH", "unselect"/*DefaultEditorKit.unselectAction*/,
"ctrl HOME", DefaultEditorKit.beginAction,
"ctrl END", DefaultEditorKit.endAction,
"ctrl shift HOME", DefaultEditorKit.selectionBeginAction,
"ctrl shift END", DefaultEditorKit.selectionEndAction,
"ctrl T", "next-link-action",
"ctrl shift T", "previous-link-action",
"ctrl SPACE", "activate-link-action",
"control shift O", "toggle-componentOrientation"/*DefaultEditorKit.toggleComponentOrientation*/
});
}
56 changes: 56 additions & 0 deletions src/main/java/mdlaf/components/list/MaterialListUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* MIT License
* Copyright (c) 2018 atharva washimkar
* Copyright (c) 2019 Vincent Palazzo [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package mdlaf.components.list;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicListUI;
import java.awt.*;

/**
* @author https://github.com/vincenzopalazzo
*/
public class MaterialListUI extends BasicListUI {

@SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"})
public static ComponentUI createUI(JComponent c) {
return new MaterialListUI();
}

@Override
public void installUI(JComponent c) {
super.installUI(c);

list = (JList) c;
list.setBackground(UIManager.getColor("List.background"));
list.setForeground(UIManager.getColor("List.foreground"));
list.setBorder(UIManager.getBorder("List.border"));
list.setFont(UIManager.getFont("List.font"));
list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
list.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
list.setFocusable(UIManager.getBoolean("List.focusable"));
}

}
Loading

0 comments on commit 2a3b065

Please sign in to comment.