mirror of https://github.com/weisJ/darklaf.git
Browse Source
Respect border insets when calculating bounds for the togglebutton slider variant.pull/187/head
weisj
5 years ago
10 changed files with 402 additions and 14 deletions
@ -0,0 +1,64 @@
|
||||
/* |
||||
* 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 com.github.weisj.darklaf.components.togglebuttonlist; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import com.github.weisj.darklaf.ui.togglebutton.DarkToggleButtonUI; |
||||
|
||||
/** |
||||
* Each item in the CheckBxoList will be an instance of this class
|
||||
* |
||||
* @author Naveed Quadri |
||||
*/ |
||||
public class CheckBoxListItem extends JCheckBox { |
||||
|
||||
private Object value = null; |
||||
|
||||
public CheckBoxListItem(final Object itemValue, final boolean selected) { |
||||
super("", selected); |
||||
setValue(itemValue); |
||||
putClientProperty(DarkToggleButtonUI.KEY_IS_TABLE_EDITOR, true); |
||||
} |
||||
|
||||
public Object getValue() { |
||||
return value; |
||||
} |
||||
|
||||
/** |
||||
* The value of the JCheckbox label |
||||
* |
||||
* @param value the new value. |
||||
*/ |
||||
public void setValue(final Object value) { |
||||
this.value = value; |
||||
setText(value == null ? "" : "" + value); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return value.toString(); |
||||
} |
||||
} |
@ -0,0 +1,158 @@
|
||||
/* |
||||
* 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 com.github.weisj.darklaf.components.togglebuttonlist; |
||||
|
||||
import java.awt.*; |
||||
import java.awt.event.ActionEvent; |
||||
import java.awt.event.MouseAdapter; |
||||
import java.awt.event.MouseEvent; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
public class JToggleButtonList extends JList<JToggleButton> { |
||||
|
||||
public JToggleButtonList() { |
||||
super.setModel(new DefaultListModel<>()); |
||||
super.setCellRenderer(new ToggleButtonListCellRenderer()); |
||||
|
||||
addMouseListener(new MouseAdapter() { |
||||
|
||||
@Override |
||||
public void mousePressed(final MouseEvent e) { |
||||
int index = locationToIndex(e.getPoint()); |
||||
Rectangle bounds = getCellBounds(index, index); |
||||
if (index != -1) { |
||||
JToggleButton toggleButton = getModel().getElementAt(index); |
||||
// check if the click is on the togglebutton.
|
||||
Point p = e.getPoint(); |
||||
boolean inside = toggleButton.contains(p.x - bounds.x, p.y - bounds.y); |
||||
|
||||
if (e.getClickCount() >= 2 || inside) { |
||||
toggleButton.setSelected(!toggleButton.isSelected()); |
||||
fireSelectionValueChanged(index, index, inside); |
||||
} |
||||
repaint(); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
InputMap map = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
||||
|
||||
map.put(KeyStroke.getKeyStroke("released SPACE"), "toggle_togglebutton"); |
||||
getActionMap().put("toggle_togglebutton", new AbstractAction("toggle_togglebutton") { |
||||
@Override |
||||
public void actionPerformed(final ActionEvent e) { |
||||
int leadIndex = getSelectionModel().getLeadSelectionIndex(); |
||||
if (leadIndex >= 0) setSelected(leadIndex, !JToggleButtonList.this.isSelected(leadIndex)); |
||||
} |
||||
}); |
||||
super.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
||||
} |
||||
|
||||
public void addToggleButton(final String text) { |
||||
addToggleButton(text, false); |
||||
} |
||||
|
||||
public void addToggleButton(final String text, final boolean selected) { |
||||
addToggleButton(getModel().getSize(), text, selected); |
||||
} |
||||
|
||||
public void addToggleButton(final int index, final String text) { |
||||
addToggleButton(index, text, false); |
||||
} |
||||
|
||||
public void addToggleButton(final int index, final String text, final boolean selected) { |
||||
addToggleButton(index, new CheckBoxListItem(text, selected)); |
||||
} |
||||
|
||||
public void addToggleButton(final JToggleButton item) { |
||||
addToggleButton(getModel().getSize(), item); |
||||
} |
||||
|
||||
public void addToggleButton(final int index, final JToggleButton item) { |
||||
gettogglebuttonModel().add(index, item); |
||||
} |
||||
|
||||
public boolean isSelected(final int index) { |
||||
return gettogglebuttonModel().get(index).isSelected(); |
||||
} |
||||
|
||||
public void setSelected(final int index, final boolean selected) { |
||||
gettogglebuttonModel().get(index).setSelected(selected); |
||||
repaint(getCellBounds(index, index)); |
||||
} |
||||
|
||||
private DefaultListModel<JToggleButton> gettogglebuttonModel() { |
||||
return ((DefaultListModel<JToggleButton>) getModel()); |
||||
} |
||||
|
||||
/** |
||||
* Gets all the indices that are checked. |
||||
* |
||||
* @return Checked Indices |
||||
*/ |
||||
public int[] getCheckedIndices() { |
||||
List<Integer> list = new ArrayList<>(); |
||||
ListModel<JToggleButton> dlm = getModel(); |
||||
for (int i = 0; i < dlm.getSize(); ++i) { |
||||
JToggleButton togglebutton = getModel().getElementAt(i); |
||||
if (togglebutton.isSelected()) { |
||||
list.add(i); |
||||
} |
||||
} |
||||
return list.stream().mapToInt(Integer::intValue).toArray(); |
||||
} |
||||
|
||||
/** |
||||
* Gets Checked Items |
||||
* |
||||
* @return Checked Items |
||||
*/ |
||||
public List<JToggleButton> getCheckedItems() { |
||||
ArrayList<JToggleButton> list = new ArrayList<>(); |
||||
ListModel<JToggleButton> dlm = getModel(); |
||||
for (int i = 0; i < dlm.getSize(); ++i) { |
||||
JToggleButton togglebutton = dlm.getElementAt(i); |
||||
if (togglebutton.isSelected()) { |
||||
list.add(togglebutton); |
||||
} |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
@Override |
||||
public void setModel(final ListModel<JToggleButton> model) {} |
||||
|
||||
@Override |
||||
public void setSelectionMode(final int selectionMode) {} |
||||
|
||||
@Override |
||||
public void setSelectionModel(final ListSelectionModel selectionModel) {} |
||||
|
||||
@Override |
||||
public void setCellRenderer(final ListCellRenderer<? super JToggleButton> cellRenderer) {} |
||||
} |
@ -0,0 +1,49 @@
|
||||
/* |
||||
* 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 com.github.weisj.darklaf.components.togglebuttonlist; |
||||
|
||||
import java.awt.*; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
public class ToggleButtonListCellRenderer implements ListCellRenderer<JToggleButton> { |
||||
|
||||
@Override |
||||
public Component getListCellRendererComponent(final JList<? extends JToggleButton> list, |
||||
final JToggleButton value, final int index, |
||||
final boolean isSelected, final boolean cellHasFocus) { |
||||
value.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground()); |
||||
value.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground()); |
||||
value.setEnabled(list.isEnabled()); |
||||
value.setFont(list.getFont()); |
||||
value.setFocusPainted(cellHasFocus); |
||||
value.setBorderPainted(true); |
||||
value.setBorder(isSelected |
||||
? UIManager.getBorder("List.focusCellHighlightBorder") |
||||
: UIManager.getBorder("List.cellNoFocusBorder")); |
||||
|
||||
return value; |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
/* |
||||
* 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 com.github.weisj.darklaf.ui.togglebutton; |
||||
|
||||
import java.awt.*; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import com.github.weisj.darklaf.ui.button.DarkButtonBorder; |
||||
|
||||
public class DarkToggleButtonBorder extends DarkButtonBorder { |
||||
|
||||
@Override |
||||
public Insets getBorderInsets(final Component c) { |
||||
if (c instanceof JToggleButton && ToggleButtonConstants.isSlider((JComponent) c)) { |
||||
int borderSize = getBorderSize(); |
||||
return new Insets(borderSize, borderSize, borderSize, borderSize); |
||||
} |
||||
return super.getBorderInsets(c); |
||||
} |
||||
} |
@ -0,0 +1,66 @@
|
||||
/* |
||||
* 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.list; |
||||
|
||||
import java.awt.*; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import ui.ComponentDemo; |
||||
import ui.DemoPanel; |
||||
|
||||
import com.github.weisj.darklaf.components.OverlayScrollPane; |
||||
import com.github.weisj.darklaf.components.togglebuttonlist.JToggleButtonList; |
||||
import com.github.weisj.darklaf.ui.togglebutton.DarkToggleButtonUI; |
||||
import com.github.weisj.darklaf.ui.togglebutton.ToggleButtonConstants; |
||||
|
||||
public class ToggleButtonListDemo implements ComponentDemo { |
||||
|
||||
public static void main(final String[] args) { |
||||
ComponentDemo.showDemo(new ToggleButtonListDemo()); |
||||
} |
||||
|
||||
@Override |
||||
public JComponent createComponent() { |
||||
JToggleButtonList list = new JToggleButtonList(); |
||||
list.addToggleButton(new JCheckBox("CheckBox")); |
||||
list.addToggleButton(new JRadioButton("RadioButton")); |
||||
JToggleButton toggleButton = new JToggleButton("Slider ToggleButton"); |
||||
toggleButton.putClientProperty(ToggleButtonConstants.KEY_VARIANT, DarkToggleButtonUI.VARIANT_SLIDER); |
||||
list.addToggleButton(toggleButton); |
||||
for (int i = 0; i < 20; i++) { |
||||
list.addToggleButton("Item " + i); |
||||
} |
||||
JPanel panel = new JPanel(new BorderLayout()); |
||||
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
||||
panel.add(new OverlayScrollPane(list)); |
||||
return new DemoPanel(panel, new BorderLayout(), 0); |
||||
} |
||||
|
||||
@Override |
||||
public String getTitle() { |
||||
return "Checkbox List Demo"; |
||||
} |
||||
} |
Loading…
Reference in new issue