Browse Source

Make sure custom tab components are disabled if tab is disabled.

Added option to make tabs closable on a per tab basis for ClosableTabbedPane.
Fixed NPE when dragging first tab in WrapTabLayout.
Fixed tab not switching when dragging in WrapTabLayout.
Fixed visuals of NewTabButton and MoreTabsButton.
pull/188/head
weisj 5 years ago
parent
commit
dad0ae5385
  1. 72
      core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java
  2. 40
      core/src/main/java/com/github/weisj/darklaf/components/ClosableTabbedPane.java
  3. 2
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java
  4. 4
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java
  5. 2
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/NewTabButton.java
  6. 32
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneLayout.java
  7. 4
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneTransferHandler.java
  8. 2
      core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneUtil.java
  9. 5
      core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java
  10. 2
      core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties
  11. 198
      core/src/test/java/ui/tabbedPane/AbstractTabbedPaneDemo.java
  12. 18
      core/src/test/java/ui/tabbedPane/ClosableTabbedPaneDemo.java
  13. 166
      core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java
  14. 7
      utils/src/main/java/com/github/weisj/darklaf/util/LogUtil.java

72
core/src/main/java/com/github/weisj/darklaf/components/ClosableTabComponent.java

@ -26,6 +26,7 @@ package com.github.weisj.darklaf.components;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.util.function.BiFunction;
import javax.swing.*; import javax.swing.*;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
@ -39,6 +40,8 @@ public class ClosableTabComponent extends JPanel {
private JTabbedPane pane; private JTabbedPane pane;
private final Component component; private final Component component;
private boolean closable;
private final CloseButton closeButton;
public ClosableTabComponent(final JTabbedPane pane) { public ClosableTabComponent(final JTabbedPane pane) {
this(pane, null); this(pane, null);
@ -51,23 +54,52 @@ public class ClosableTabComponent extends JPanel {
throw new NullPointerException("TabbedPane is null."); throw new NullPointerException("TabbedPane is null.");
} }
this.pane = pane; this.pane = pane;
closable = true;
closeButton = new TabCloseButton(this);
setOpaque(false); setOpaque(false);
add(this.component); add(getTabComponent());
add(new TabCloseButton(this)); add(getCloseButton());
} }
public Component getTabComponent() { public Component getTabComponent() {
return component; return component;
} }
public boolean hasCustomTabComponent() { public CloseButton getCloseButton() {
return !(component instanceof TabLabel); return closeButton;
}
public boolean isClosable() {
return closable;
}
public void setClosable(final boolean closable) {
if (this.closable != closable) {
this.closable = closable;
if (!closable) {
remove(closeButton);
} else {
add(closeButton);
}
doLayout();
}
} }
public void setTabbedPane(final JTabbedPane pane) { public void setTabbedPane(final JTabbedPane pane) {
this.pane = pane; this.pane = pane;
} }
public int getIndexInTabbedPane() {
return pane.indexOfTabComponent(this);
}
@Override
public void setEnabled(final boolean enabled) {
super.setEnabled(enabled);
getTabComponent().setEnabled(enabled);
getCloseButton().setEnabled(enabled);
}
protected static class TabLabel extends JLabel { protected static class TabLabel extends JLabel {
private final ClosableTabComponent tabComponent; private final ClosableTabComponent tabComponent;
@ -77,14 +109,34 @@ public class ClosableTabComponent extends JPanel {
setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
} }
protected <T> T getTabProperty(final BiFunction<JTabbedPane, Integer, T> mapper, final T fallback) {
if (tabComponent != null) {
int i = tabComponent.getIndexInTabbedPane();
if (i != -1) {
return mapper.apply(tabComponent.pane, i);
}
}
return fallback;
}
@Override
public Icon getIcon() {
return getTabProperty(JTabbedPane::getIconAt, null);
}
@Override
public Icon getDisabledIcon() {
return getTabProperty(JTabbedPane::getDisabledIconAt, null);
}
@Override
public int getDisplayedMnemonic() {
return getTabProperty(JTabbedPane::getDisplayedMnemonicIndexAt, -1);
}
@Override @Override
public String getText() { public String getText() {
if (tabComponent == null) return ""; return getTabProperty(JTabbedPane::getTitleAt, "");
int i = tabComponent.pane.indexOfTabComponent(tabComponent);
if (i != -1) {
return tabComponent.pane.getTitleAt(i);
}
return "";
} }
} }

40
core/src/main/java/com/github/weisj/darklaf/components/ClosableTabbedPane.java

@ -30,6 +30,8 @@ import java.beans.VetoableChangeListener;
import javax.swing.*; import javax.swing.*;
import com.github.weisj.darklaf.util.DarkUIUtil;
/** /**
* @author Jannis Weis * @author Jannis Weis
*/ */
@ -70,6 +72,39 @@ public class ClosableTabbedPane extends JTabbedPane {
} }
} }
/**
* Returns the {@link ClosableTabComponent} at the given index or null
* if no tab component is set.
*
* @param index the index.
* @return the {@link ClosableTabComponent} at the index.
*/
public ClosableTabComponent getClosableTabComponent(final int index) {
return DarkUIUtil.nullableCast(ClosableTabComponent.class, super.getTabComponentAt(index));
}
/**
* Sets whether a given tab is closable.
*
* @param index the index of the tab.
* @param closable true if closable.
*/
public void setTabClosable(final int index, final boolean closable) {
ClosableTabComponent tab = getClosableTabComponent(index);
if (tab != null) tab.setClosable(closable);
}
/**
* Sets whether a given tab is closable.
*
* @param index the tab index.
* @return true if closable.
*/
public boolean isTabClosable(final int index) {
ClosableTabComponent tab = getClosableTabComponent(index);
return tab != null && tab.isClosable();
}
private void checkIndex(final int index) { private void checkIndex(final int index) {
int tabCount = getTabCount(); int tabCount = getTabCount();
if (index < 0 || index >= tabCount) { if (index < 0 || index >= tabCount) {
@ -89,6 +124,11 @@ public class ClosableTabbedPane extends JTabbedPane {
return false; return false;
} }
@Override
public void setEnabledAt(final int index, final boolean enabled) {
super.setEnabledAt(index, enabled);
}
private void notifyTabListeners(final TabEvent event) { private void notifyTabListeners(final TabEvent event) {
TabListener[] listeners = listenerList.getListeners(TabListener.class); TabListener[] listeners = listenerList.getListeners(TabListener.class);
switch (event.getID()) { switch (event.getID()) {

2
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java

@ -662,6 +662,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
continue; continue;
} }
getTabBounds(i, rect); getTabBounds(i, rect);
c.setEnabled(tabPane.isEnabledAt(i));
// Adjust dragged component position. // Adjust dragged component position.
if (i == dropSourceIndex) { if (i == dropSourceIndex) {
@ -678,6 +679,7 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge {
} }
Dimension preferredSize = c.getPreferredSize(); Dimension preferredSize = c.getPreferredSize();
int width = Math.min(preferredSize.width, rect.width); int width = Math.min(preferredSize.width, rect.width);
int height = Math.min(preferredSize.height, rect.height); int height = Math.min(preferredSize.height, rect.height);

4
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/MoreTabsButton.java

@ -45,11 +45,13 @@ public class MoreTabsButton extends DarkTabAreaButton {
this.ui = ui; this.ui = ui;
icon = ui.getMoreTabsIcon(); icon = ui.getMoreTabsIcon();
pad = UIManager.getInt("TabbedPane.moreTabsButton.pad"); pad = UIManager.getInt("TabbedPane.moreTabsButton.pad");
int fontSize = UIManager.getInt("TabbedPane.moreTabsButton.fontSize");
setIcon(EmptyIcon.create(icon.getIconWidth(), icon.getIconHeight())); setIcon(EmptyIcon.create(icon.getIconWidth(), icon.getIconHeight()));
putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, true); putClientProperty(DarkButtonUI.KEY_NO_BACKGROUND, true);
putClientProperty(DarkButtonUI.KEY_VARIANT, DarkButtonUI.VARIANT_BORDERLESS_RECTANGULAR);
putClientProperty(DarkButtonUI.KEY_SQUARE, true); putClientProperty(DarkButtonUI.KEY_SQUARE, true);
setFocusable(false); setFocusable(false);
setFont(getFont().deriveFont(8f)); setFont(getFont().deriveFont((float) fontSize));
} }
@Override @Override

2
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/NewTabButton.java

@ -61,8 +61,6 @@ public class NewTabButton extends JPanel implements UIResource {
button.putClientProperty(DarkButtonUI.KEY_THIN, Boolean.TRUE); button.putClientProperty(DarkButtonUI.KEY_THIN, Boolean.TRUE);
button.setRolloverEnabled(true); button.setRolloverEnabled(true);
button.setOpaque(false); button.setOpaque(false);
Insets margin = UIManager.getInsets("TabbedPane.newTabButton.insets");
button.setMargin(new Insets(margin.top, margin.left, margin.bottom, margin.right));
return button; return button;
} }

32
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneLayout.java

@ -187,37 +187,7 @@ public abstract class TabbedPaneLayout implements LayoutManager {
ui.isRunsDirty = false; ui.isRunsDirty = false;
} }
protected void layoutTabComponents() { protected abstract void layoutTabComponents();
if (ui.tabContainer == null) {
return;
}
Rectangle rect = new Rectangle();
Point delta = new Point(-ui.tabContainer.getX(), -ui.tabContainer.getY());
if (ui.scrollableTabLayoutEnabled()) {
ui.translatePointToTabPanel(0, 0, delta);
}
for (int i = 0; i < ui.tabPane.getTabCount(); i++) {
Component c = ui.tabPane.getTabComponentAt(i);
if (c == null) {
continue;
}
ui.getTabBounds(i, rect);
Dimension preferredSize = c.getPreferredSize();
Insets insets = ui.getTabInsets(ui.tabPane.getTabPlacement(), i);
int outerX = rect.x + insets.left + delta.x;
int outerY = rect.y + insets.top + delta.y;
int outerWidth = rect.width - insets.left - insets.right;
int outerHeight = rect.height - insets.top - insets.bottom;
// centralize component
int x = outerX + (outerWidth - preferredSize.width) / 2;
int y = outerY + (outerHeight - preferredSize.height) / 2;
int tabPlacement = ui.tabPane.getTabPlacement();
boolean isSeleceted = i == ui.tabPane.getSelectedIndex();
c.setBounds(x + ui.getTabLabelShiftX(tabPlacement, i, isSeleceted),
y + ui.getTabLabelShiftY(tabPlacement, i, isSeleceted),
preferredSize.width, preferredSize.height);
}
}
/** /**
* Calculate the tab rectangles. * Calculate the tab rectangles.

4
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneTransferHandler.java

@ -160,10 +160,6 @@ public class TabbedPaneTransferHandler extends TransferHandler implements DropTa
createDragImage(tabPane, ui); createDragImage(tabPane, ui);
ui.setSourceIndicator(currentTransferable.transferData.tabIndex); ui.setSourceIndicator(currentTransferable.transferData.tabIndex);
} }
if ((ui != null && !ui.scrollableTabLayoutEnabled())
|| tabPane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
tabPane.setSelectedIndex(currentTransferable.transferData.tabIndex);
}
return currentTransferable; return currentTransferable;
} }

2
core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/TabbedPaneUtil.java

@ -192,7 +192,7 @@ public class TabbedPaneUtil implements SwingConstants {
} else { } else {
int placement = destTabbedPane.getTabPlacement(); int placement = destTabbedPane.getTabPlacement();
Rectangle b = ui.getTabAreaBounds(); Rectangle b = ui.getTabAreaBounds();
Rectangle prev = destTabbedPane.getBoundsAt(tab - 1); Rectangle prev = tab > 0 ? destTabbedPane.getBoundsAt(tab - 1) : EMPTY_RECT;
if (placement == LEFT || placement == RIGHT) { if (placement == LEFT || placement == RIGHT) {
DarkUIUtil.rotateRectangle(tabBounds); DarkUIUtil.rotateRectangle(tabBounds);

5
core/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java

@ -505,4 +505,9 @@ public final class DarkUIUtil {
} }
return (size != null) ? size : component.getPreferredSize(); return (size != null) ? size : component.getPreferredSize();
} }
public static <T> T nullableCast(final Class<T> type, final Object o) {
if (type != null && type.isInstance(o)) return type.cast(o);
return null;
}
} }

2
core/src/main/resources/com/github/weisj/darklaf/properties/ui/tabbedPane.properties

@ -55,7 +55,7 @@ TabbedPane.selectedLabelShift = 0
TabbedPane.selectedTabPadInsets = 0,0,0,0 TabbedPane.selectedTabPadInsets = 0,0,0,0
TabbedPane.tabAreaInsets = 0,0,0,0 TabbedPane.tabAreaInsets = 0,0,0,0
TabbedPane.newTabButton.insets = 2,2,2,2 TabbedPane.moreTabsButton.fontSize = 8
TabbedPane.moreTabsButton.pad = 2 TabbedPane.moreTabsButton.pad = 2
#Icons #Icons

198
core/src/test/java/ui/tabbedPane/AbstractTabbedPaneDemo.java

@ -0,0 +1,198 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* 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 ui.tabbedPane;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.UIResource;
import ui.ComponentDemo;
import ui.DemoPanel;
import com.github.weisj.darklaf.ui.tabbedpane.DarkTabbedPaneUI;
import com.github.weisj.darklaf.util.StringUtil;
public abstract class AbstractTabbedPaneDemo<T extends JTabbedPane> implements ComponentDemo {
@Override
public JComponent createComponent() {
T tabbedPane = createTabbedPane();
setupTabbedPane(tabbedPane);
DemoPanel panel = new DemoPanel(tabbedPane, new BorderLayout(), 0);
JPanel controlPanel = panel.addControls();
controlPanel.add(new JLabel("TabLayoutPolicy:", JLabel.RIGHT));
controlPanel.add(new JComboBox<String>() {
{
Map<String, Integer> mapping = new HashMap<String, Integer>() {
{
put("SCROLL_TAB_LAYOUT", JTabbedPane.SCROLL_TAB_LAYOUT);
put("WRAP_TAB_LAYOUT", JTabbedPane.WRAP_TAB_LAYOUT);
}
};
addItem("SCROLL_TAB_LAYOUT");
addItem("WRAP_TAB_LAYOUT");
setSelectedItem("SCROLL_TAB_LAYOUT");
addItemListener(e -> tabbedPane.setTabLayoutPolicy(mapping.get(e.getItem().toString())));
}
}, "sgx");
controlPanel.add(new JLabel("TabPlacement:", JLabel.RIGHT));
controlPanel.add(new JComboBox<String>() {
{
Map<String, Integer> mapping = new HashMap<String, Integer>() {
{
put("TOP", JTabbedPane.TOP);
put("BOTTOM", JTabbedPane.BOTTOM);
put("LEFT", JTabbedPane.LEFT);
put("RIGHT", JTabbedPane.RIGHT);
}
};
addItem("TOP");
addItem("BOTTOM");
addItem("LEFT");
addItem("RIGHT");
setSelectedItem("TOP");
addItemListener(e -> tabbedPane.setTabPlacement(mapping.get(e.getItem().toString())));
}
}, "sgx");
controlPanel = panel.addControls();
controlPanel.add(new JCheckBox("enabled") {
{
setSelected(tabbedPane.isEnabled());
addActionListener(e -> tabbedPane.setEnabled(isSelected()));
}
});
controlPanel.add(new JCheckBox("LeftToRight") {
{
setSelected(tabbedPane.getComponentOrientation().isLeftToRight());
addActionListener(e -> tabbedPane.setComponentOrientation(isSelected()
? ComponentOrientation.LEFT_TO_RIGHT
: ComponentOrientation.RIGHT_TO_LEFT));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_CENTER_TABS) {
{
setSelected(false);
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_CENTER_TABS, isSelected()));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_DND) {
{
setSelected(false);
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_DND, isSelected()));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_SHOW_NEW_TAB_BUTTON) {
{
setSelected(false);
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_SHOW_NEW_TAB_BUTTON,
isSelected()));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_LEADING_COMP) {
{
setSelected(false);
JLabel leading = new PlaceholderLabel("Leading");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_LEADING_COMP,
isSelected() ? leading : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_TRAILING_COMP) {
{
setSelected(false);
JLabel trailing = new PlaceholderLabel("Trailing");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_TRAILING_COMP,
isSelected() ? trailing : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_NORTH_COMP) {
{
setSelected(false);
JLabel north = new PlaceholderLabel("North");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_NORTH_COMP,
isSelected() ? north : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_EAST_COMP) {
{
setSelected(false);
JLabel east = new PlaceholderLabel("East");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_EAST_COMP,
isSelected() ? east : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_SOUTH_COMP) {
{
setSelected(false);
JLabel south = new PlaceholderLabel("South");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_SOUTH_COMP,
isSelected() ? south : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_WEST_COMP) {
{
setSelected(false);
JLabel west = new PlaceholderLabel("West");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_WEST_COMP,
isSelected() ? west : null));
}
});
return panel;
}
protected abstract int getTabCount();
protected void setupTabbedPane(final T tabbedPane) {
tabbedPane.setName("DemoTabbedPane");
for (int i = 0; i < getTabCount(); i++) {
JTextPane editor = new JTextPane();
editor.setText(StringUtil.repeat("Demo Content" + "\n", i + 1));
tabbedPane.addTab("Tab (" + i + ")", editor);
}
JLabel label = new JLabel("Custom Tab");
label.setForeground(Color.RED);
tabbedPane.setTabComponentAt(0, label);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected abstract T createTabbedPane();
private static class PlaceholderLabel extends JLabel implements UIResource {
private PlaceholderLabel(final String title) {
super(title);
setBorder(new CompoundBorder(new LineBorder(Color.RED),
new EmptyBorder(5, 5, 5, 5)));
}
}
}

18
core/src/test/java/ui/tabbedPane/ClosableTabbedPaneDemo.java

@ -24,26 +24,36 @@
*/ */
package ui.tabbedPane; package ui.tabbedPane;
import javax.swing.*;
import ui.ComponentDemo; import ui.ComponentDemo;
import com.github.weisj.darklaf.components.ClosableTabbedPane; import com.github.weisj.darklaf.components.ClosableTabbedPane;
import com.github.weisj.darklaf.components.TabEvent; import com.github.weisj.darklaf.components.TabEvent;
import com.github.weisj.darklaf.components.TabListener; import com.github.weisj.darklaf.components.TabListener;
public class ClosableTabbedPaneDemo extends TabbedPaneDemo implements TabListener { public class ClosableTabbedPaneDemo extends AbstractTabbedPaneDemo<ClosableTabbedPane> implements TabListener {
public static void main(final String[] args) { public static void main(final String[] args) {
ComponentDemo.showDemo(new ClosableTabbedPaneDemo()); ComponentDemo.showDemo(new ClosableTabbedPaneDemo());
} }
protected JTabbedPane createTabbedPane() { protected ClosableTabbedPane createTabbedPane() {
ClosableTabbedPane tabbedPane = new ClosableTabbedPane(); ClosableTabbedPane tabbedPane = new ClosableTabbedPane();
tabbedPane.addTabListener(this); tabbedPane.addTabListener(this);
return tabbedPane; return tabbedPane;
} }
@Override
protected void setupTabbedPane(final ClosableTabbedPane tabbedPane) {
super.setupTabbedPane(tabbedPane);
tabbedPane.setEnabledAt(2, false);
tabbedPane.setTabClosable(1, false);
}
@Override
protected int getTabCount() {
return 4;
}
@Override @Override
public String getTitle() { public String getTitle() {
return "ClosableTabbPane Demo"; return "ClosableTabbPane Demo";

166
core/src/test/java/ui/tabbedPane/TabbedPaneDemo.java

@ -24,181 +24,27 @@
*/ */
package ui.tabbedPane; package ui.tabbedPane;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.UIResource;
import ui.ComponentDemo; import ui.ComponentDemo;
import ui.DemoPanel;
import com.github.weisj.darklaf.ui.tabbedpane.DarkTabbedPaneUI;
import com.github.weisj.darklaf.util.StringUtil;
public class TabbedPaneDemo implements ComponentDemo { public class TabbedPaneDemo extends AbstractTabbedPaneDemo<JTabbedPane> {
public static void main(final String[] args) { public static void main(final String[] args) {
ComponentDemo.showDemo(new TabbedPaneDemo()); ComponentDemo.showDemo(new TabbedPaneDemo());
} }
@Override
public JComponent createComponent() {
JTabbedPane tabbedPane = createTabbedPane();
tabbedPane.setName("DemoTabbedPane");
for (int i = 0; i < 4; i++) {
JTextPane editor = new JTextPane();
editor.setText(StringUtil.repeat("Demo Content" + "\n", i + 1));
tabbedPane.addTab("Tab (" + i + ")", editor);
}
JLabel label = new JLabel("Custom Tab");
label.setForeground(Color.RED);
tabbedPane.setTabComponentAt(0, label);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
DemoPanel panel = new DemoPanel(tabbedPane, new BorderLayout(), 0);
JPanel controlPanel = panel.addControls();
controlPanel.add(new JLabel("TabLayoutPolicy:", JLabel.RIGHT));
controlPanel.add(new JComboBox<String>() {
{
Map<String, Integer> mapping = new HashMap<String, Integer>() {
{
put("SCROLL_TAB_LAYOUT", JTabbedPane.SCROLL_TAB_LAYOUT);
put("WRAP_TAB_LAYOUT", JTabbedPane.WRAP_TAB_LAYOUT);
}
};
addItem("SCROLL_TAB_LAYOUT");
addItem("WRAP_TAB_LAYOUT");
setSelectedItem("SCROLL_TAB_LAYOUT");
addItemListener(e -> tabbedPane.setTabLayoutPolicy(mapping.get(e.getItem().toString())));
}
}, "sgx");
controlPanel.add(new JLabel("TabPlacement:", JLabel.RIGHT));
controlPanel.add(new JComboBox<String>() {
{
Map<String, Integer> mapping = new HashMap<String, Integer>() {
{
put("TOP", JTabbedPane.TOP);
put("BOTTOM", JTabbedPane.BOTTOM);
put("LEFT", JTabbedPane.LEFT);
put("RIGHT", JTabbedPane.RIGHT);
}
};
addItem("TOP");
addItem("BOTTOM");
addItem("LEFT");
addItem("RIGHT");
setSelectedItem("TOP");
addItemListener(e -> tabbedPane.setTabPlacement(mapping.get(e.getItem().toString())));
}
}, "sgx");
controlPanel = panel.addControls();
controlPanel.add(new JCheckBox("enabled") {
{
setSelected(tabbedPane.isEnabled());
addActionListener(e -> tabbedPane.setEnabled(isSelected()));
}
});
controlPanel.add(new JCheckBox("LeftToRight") {
{
setSelected(tabbedPane.getComponentOrientation().isLeftToRight());
addActionListener(e -> tabbedPane.setComponentOrientation(isSelected()
? ComponentOrientation.LEFT_TO_RIGHT
: ComponentOrientation.RIGHT_TO_LEFT));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_CENTER_TABS) {
{
setSelected(false);
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_CENTER_TABS, isSelected()));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_DND) {
{
setSelected(false);
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_DND, isSelected()));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_SHOW_NEW_TAB_BUTTON) {
{
setSelected(false);
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_SHOW_NEW_TAB_BUTTON,
isSelected()));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_LEADING_COMP) {
{
setSelected(false);
JLabel leading = new PlaceholderLabel("Leading");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_LEADING_COMP,
isSelected() ? leading : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_TRAILING_COMP) {
{
setSelected(false);
JLabel trailing = new PlaceholderLabel("Trailing");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_TRAILING_COMP,
isSelected() ? trailing : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_NORTH_COMP) {
{
setSelected(false);
JLabel north = new PlaceholderLabel("North");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_NORTH_COMP,
isSelected() ? north : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_EAST_COMP) {
{
setSelected(false);
JLabel east = new PlaceholderLabel("East");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_EAST_COMP,
isSelected() ? east : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_SOUTH_COMP) {
{
setSelected(false);
JLabel south = new PlaceholderLabel("South");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_SOUTH_COMP,
isSelected() ? south : null));
}
});
controlPanel.add(new JCheckBox(DarkTabbedPaneUI.KEY_WEST_COMP) {
{
setSelected(false);
JLabel west = new PlaceholderLabel("West");
addActionListener(e -> tabbedPane.putClientProperty(DarkTabbedPaneUI.KEY_WEST_COMP,
isSelected() ? west : null));
}
});
return panel;
}
protected JTabbedPane createTabbedPane() { protected JTabbedPane createTabbedPane() {
return new JTabbedPane(); return new JTabbedPane();
} }
@Override @Override
public String getTitle() { protected int getTabCount() {
return "TabbPane Demo"; return 10;
} }
private static class PlaceholderLabel extends JLabel implements UIResource { @Override
private PlaceholderLabel(final String title) { public String getTitle() {
super(title); return "TabbPane Demo";
setBorder(new CompoundBorder(new LineBorder(Color.RED),
new EmptyBorder(5, 5, 5, 5)));
}
} }
} }

7
utils/src/main/java/com/github/weisj/darklaf/util/LogUtil.java

@ -38,6 +38,8 @@ public class LogUtil {
static { static {
LOG_HANDLER.setFormatter(new LogFormatter()); LOG_HANDLER.setFormatter(new LogFormatter());
PARENT.setUseParentHandlers(false);
PARENT.addHandler(LOG_HANDLER);
} }
public static Logger getLogger(final Class<?> clazz) { public static Logger getLogger(final Class<?> clazz) {
@ -51,4 +53,9 @@ public class LogUtil {
PARENT.setLevel(level); PARENT.setLevel(level);
LOG_HANDLER.setLevel(level); LOG_HANDLER.setLevel(level);
} }
public static <T> T log(final T obj) {
PARENT.info(String.valueOf(obj));
return obj;
}
} }

Loading…
Cancel
Save