mirror of https://github.com/weisJ/darklaf.git
Browse Source
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
14 changed files with 340 additions and 214 deletions
@ -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))); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue