Skip to content

Commit

Permalink
Merge pull request #5873 from thc202/flatlaf
Browse files Browse the repository at this point in the history
Add FlatLaf L&F
  • Loading branch information
psiinon committed Feb 25, 2020
2 parents 380b7b8 + fc3ac09 commit 53679eb
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 21 deletions.
1 change: 1 addition & 0 deletions LEGALNOTICE.md
Expand Up @@ -50,6 +50,7 @@ and subject to their respective licenses.
| commons-validator-1.6.jar | Apache 2.0 |
| delight-nashorn-sandbox-0.1.26.jar | Apache 2.0 |
| ezmorph-1.0.6.jar | Apache 2.0 |
| flatlaf-0.27.jar | Apache 2.0 |
| harlib-1.1.2.jar | Apache 2.0 |
| hsqldb-2.5.0.jar | BSD |
| ice4j-1.0.jar | Apache 2.0 |
Expand Down
Expand Up @@ -42,11 +42,14 @@
// ZAP: 2018/06/11 Added options for Work Panels Font.
// ZAP: 2019/06/01 Normalise line endings.
// ZAP: 2019/06/05 Normalise format/style.
// ZAP: 2020/02/24 Persist the class of the selected look and feel.
package org.parosproxy.paros.extension.option;

import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.swing.UIManager.LookAndFeelInfo;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.common.AbstractParam;
import org.parosproxy.paros.control.Control.Mode;
Expand Down Expand Up @@ -97,6 +100,14 @@ public class OptionsParamView extends AbstractParam {
public static final String SCALE_IMAGES = "view.scaleImages";
public static final String SHOW_DEV_WARNING = "view.showDevWarning";
public static final String LOOK_AND_FEEL = "view.lookAndFeel";
public static final String LOOK_AND_FEEL_CLASS = "view.lookAndFeelClass";

/**
* The default look and feel, has empty name and class.
*
* @since TODO add version
*/
public static final LookAndFeelInfo DEFAULT_LOOK_AND_FEEL = new LookAndFeelInfo("", "");

private static final String CONFIRM_REMOVE_PROXY_EXCLUDE_REGEX_KEY =
"view.confirmRemoveProxyExcludeRegex";
Expand Down Expand Up @@ -142,7 +153,7 @@ public class OptionsParamView extends AbstractParam {
private int largeResponseSize = LargeResponseUtil.DEFAULT_MIN_CONTENT_LENGTH;
private boolean scaleImages = true;
private boolean showDevWarning = true;
private String lookAndFeel = "";
private LookAndFeelInfo lookAndFeelInfo = DEFAULT_LOOK_AND_FEEL;

private boolean confirmRemoveProxyExcludeRegex;
private boolean confirmRemoveScannerExcludeRegex;
Expand Down Expand Up @@ -204,7 +215,9 @@ protected void parse() {

scaleImages = getBoolean(SCALE_IMAGES, true);
showDevWarning = getBoolean(SHOW_DEV_WARNING, true);
lookAndFeel = getString(LOOK_AND_FEEL, "");
lookAndFeelInfo =
new LookAndFeelInfo(
getString(LOOK_AND_FEEL, ""), getString(LOOK_AND_FEEL_CLASS, ""));

// Special cases - set via static methods
LargeRequestUtil.setMinContentLength(largeRequestSize);
Expand Down Expand Up @@ -535,13 +548,52 @@ public void setFontName(FontUtils.FontType fontType, String fontName) {
getConfig().setProperty(getFontNameConfKey(fontType), fontName);
}

/**
* Gets the the name of the selected look and feel.
*
* @return the name, might be {@code null} or empty if none selected (i.e. using default).
* @see #getLookAndFeelInfo()
* @since 2.8.0
*/
public String getLookAndFeel() {
return this.lookAndFeel;
return this.lookAndFeelInfo.getName();
}

/**
* Sets the name of the selected look and feel.
*
* @param lookAndFeel the name.
* @since 2.8.0
* @deprecated (TODO add version) Use {@link #setLookAndFeelInfo(LookAndFeelInfo)} instead,
* which preserves the class of the look and feel.
*/
@Deprecated
public void setLookAndFeel(String lookAndFeel) {
this.lookAndFeel = lookAndFeel;
getConfig().setProperty(LOOK_AND_FEEL, lookAndFeel);
setLookAndFeelInfo(new LookAndFeelInfo(lookAndFeel, ""));
}

/**
* Gets the info of the selected look and feel.
*
* @return the info of the look and feel.
* @since TODO add version
* @see #getLookAndFeel()
*/
public LookAndFeelInfo getLookAndFeelInfo() {
return this.lookAndFeelInfo;
}

/**
* Sets the info of the selected look and feel.
*
* @param lookAndFeelInfo the info of the look and feel.
* @throws NullPointerException if the given parameter is null.
* @since TODO add version
*/
public void setLookAndFeelInfo(LookAndFeelInfo lookAndFeelInfo) {
this.lookAndFeelInfo = Objects.requireNonNull(lookAndFeelInfo);
getConfig().setProperty(LOOK_AND_FEEL, lookAndFeelInfo.getName());
getConfig().setProperty(LOOK_AND_FEEL_CLASS, lookAndFeelInfo.getClassName());
}

public boolean isScaleImages() {
Expand Down
Expand Up @@ -34,6 +34,7 @@
// ZAP: 2018/06/11 Added options for Work Panels Font.
// ZAP: 2019/06/01 Normalise line endings.
// ZAP: 2019/06/05 Normalise format/style.
// ZAP: 2020/02/24 Use LookAndFeelInfo when setting the look and feel option.
package org.parosproxy.paros.extension.option;

import java.awt.BorderLayout;
Expand All @@ -48,6 +49,7 @@
import java.awt.event.ItemEvent;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Predicate;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBox;
Expand All @@ -57,6 +59,7 @@
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
Expand Down Expand Up @@ -107,7 +110,7 @@ public class OptionsViewPanel extends AbstractParamPanel {
private JComboBox<String> displaySelect = null;
private JComboBox<ResponsePanelPositionUI> responsePanelPositionComboBox;
private JComboBox<String> timeStampsFormatSelect = null;
private JComboBox<String> lookAndFeel = null;
private JComboBox<LookAndFeelInfoUi> lookAndFeel = null;

private ZapNumberSpinner largeRequestSize = null;
private ZapNumberSpinner largeResponseSize = null;
Expand Down Expand Up @@ -701,14 +704,14 @@ private JCheckBox getScaleImages() {
return scaleImages;
}

private JComboBox<String> getLookAndFeelSelect() {
private JComboBox<LookAndFeelInfoUi> getLookAndFeelSelect() {
if (lookAndFeel == null) {
lookAndFeel = new JComboBox<String>();
lookAndFeel = new JComboBox<>();
lookAndFeel.setMaximumRowCount(5);
UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
lookAndFeel.addItem(""); // Default look
lookAndFeel.addItem(new LookAndFeelInfoUi(OptionsParamView.DEFAULT_LOOK_AND_FEEL));
for (UIManager.LookAndFeelInfo look : looks) {
lookAndFeel.addItem(look.getName());
lookAndFeel.addItem(new LookAndFeelInfoUi(look));
}
}
return lookAndFeel;
Expand All @@ -721,7 +724,10 @@ public void initParam(Object obj) {
getShowSplashScreen().setSelected(options.getViewParam().isShowSplashScreen());
getChkProcessImages().setSelected(options.getViewParam().getProcessImages() > 0);
displaySelect.setSelectedIndex(options.getViewParam().getDisplayOption());
selectResponsePanelPosition(options.getViewParam().getResponsePanelPosition());
String panelPosition = options.getViewParam().getResponsePanelPosition();
selectItem(
getResponsePanelPositionComboBox(),
item -> item.getPosition().name().equals(panelPosition));
brkPanelViewSelect.setSelectedIndex(options.getViewParam().getBrkPanelViewOption());
getChkShowMainToolbar().setSelected(options.getViewParam().isShowMainToolbar());
chkAdvancedView.setSelected(options.getViewParam().getAdvancedViewOption() > 0);
Expand All @@ -741,20 +747,23 @@ public void initParam(Object obj) {
}

getScaleImages().setSelected(options.getViewParam().isScaleImages());
getLookAndFeelSelect().setSelectedItem(options.getViewParam().getLookAndFeel());
String nameLaf = options.getViewParam().getLookAndFeelInfo().getName();
selectItem(
getLookAndFeelSelect(),
item -> item.getLookAndFeelInfo().getName().equals(nameLaf));
}

private void selectResponsePanelPosition(String positionName) {
for (int i = 0; i < getResponsePanelPositionComboBox().getItemCount(); i++) {
ResponsePanelPositionUI item = getResponsePanelPositionComboBox().getItemAt(i);
if (item.getPosition().name().equals(positionName)) {
getResponsePanelPositionComboBox().setSelectedIndex(i);
private static <T> void selectItem(JComboBox<T> comboBox, Predicate<T> predicate) {
for (int i = 0; i < comboBox.getItemCount(); i++) {
T item = comboBox.getItemAt(i);
if (predicate.test(item)) {
comboBox.setSelectedIndex(i);
break;
}
}

if (getResponsePanelPositionComboBox().getSelectedIndex() == -1) {
getResponsePanelPositionComboBox().setSelectedIndex(0);
if (comboBox.getSelectedIndex() == -1) {
comboBox.setSelectedIndex(0);
}
}

Expand Down Expand Up @@ -788,7 +797,10 @@ public void saveParam(Object obj) throws Exception {
.setFontName(fontType, (String) getFontName(fontType).getSelectedItem());
}
options.getViewParam().setScaleImages(getScaleImages().isSelected());
options.getViewParam().setLookAndFeel((String) getLookAndFeelSelect().getSelectedItem());
options.getViewParam()
.setLookAndFeelInfo(
((LookAndFeelInfoUi) getLookAndFeelSelect().getSelectedItem())
.getLookAndFeelInfo());
}

@Override
Expand Down Expand Up @@ -838,4 +850,22 @@ public String toString() {
return name;
}
}

private static class LookAndFeelInfoUi {

private final LookAndFeelInfo lookAndFeelInfo;

LookAndFeelInfoUi(LookAndFeelInfo lookAndFeelInfo) {
this.lookAndFeelInfo = lookAndFeelInfo;
}

LookAndFeelInfo getLookAndFeelInfo() {
return lookAndFeelInfo;
}

@Override
public String toString() {
return lookAndFeelInfo.getName();
}
}
}
3 changes: 2 additions & 1 deletion zap/src/main/java/org/zaproxy/zap/GuiBootstrap.java
Expand Up @@ -342,7 +342,8 @@ private void setupLookAndFeel() {

OptionsParam options = Model.getSingleton().getOptionsParam();

if (setLookAndFeel(getLookAndFeelClassname(options.getViewParam().getLookAndFeel()))) {
if (setLookAndFeel(getLookAndFeelClassname(options.getViewParam().getLookAndFeel()))
|| setLookAndFeel(options.getViewParam().getLookAndFeelInfo().getClassName())) {
return;
}

Expand Down
Expand Up @@ -20,12 +20,16 @@
package org.zaproxy.zap.extension.uiutils;

import java.awt.EventQueue;
import java.util.Arrays;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import org.apache.log4j.Logger;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.control.Control.Mode;
import org.parosproxy.paros.extension.ExtensionAdaptor;
import org.parosproxy.paros.extension.ExtensionHook;
import org.parosproxy.paros.extension.SessionChangedListener;
import org.parosproxy.paros.extension.ViewDelegate;
import org.parosproxy.paros.model.Session;
import org.parosproxy.paros.view.MainFrame;

Expand All @@ -52,6 +56,16 @@ public String getUIName() {
return Constant.messages.getString("uiutils.name");
}

@Override
public void initView(ViewDelegate view) {
Arrays.asList(
new LookAndFeelInfo("Flat Light", "com.formdev.flatlaf.FlatLightLaf"),
new LookAndFeelInfo("Flat Dark", "com.formdev.flatlaf.FlatDarkLaf"),
new LookAndFeelInfo("Flat IntelliJ", "com.formdev.flatlaf.FlatIntelliJLaf"),
new LookAndFeelInfo("Flat Darcula", "com.formdev.flatlaf.FlatDarculaLaf"))
.forEach(UIManager::installLookAndFeel);
}

@Override
public void hook(ExtensionHook extensionHook) {
super.hook(extensionHook);
Expand Down
1 change: 1 addition & 0 deletions zap/zap.gradle.kts
Expand Up @@ -66,6 +66,7 @@ dependencies {
setTransitive(false)
}
implementation("org.javadelight:delight-nashorn-sandbox:0.1.26")
implementation("com.formdev:flatlaf:0.27")

runtimeOnly("commons-jxpath:commons-jxpath:1.3")
runtimeOnly("commons-logging:commons-logging:1.2")
Expand Down

0 comments on commit 53679eb

Please sign in to comment.