Browse Source

Reworked ToolTip demo.

Added ToolTip demo gif.

Signed-off-by: weisj <weisj@arcor.de>
pull/27/head
weisj 5 years ago
parent
commit
6882601c6a
  1. BIN
      img/toolTip.gif
  2. 29
      src/main/java/com/github/weisj/darklaf/components/tooltip/ToolTipAware.java
  3. 16
      src/main/java/com/github/weisj/darklaf/components/tooltip/ToolTipContext.java
  4. 7
      src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java
  5. 7
      src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java
  6. 71
      src/test/java/demo/toolTip/ToolTipDemo.java

BIN
img/toolTip.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

29
src/main/java/com/github/weisj/darklaf/components/tooltip/ToolTipAware.java

@ -0,0 +1,29 @@
/*
* 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.tooltip;
public interface ToolTipAware {
ToolTipContext getToolTipContext();
}

16
src/main/java/com/github/weisj/darklaf/components/tooltip/ToolTipContext.java

@ -522,4 +522,20 @@ public class ToolTipContext implements ToolTipListener {
valid = false;
}
}
public Alignment getAlignment() {
return alignment;
}
public Alignment getCenterAlignment() {
return centerAlignment;
}
public AlignmentStrategy getAlignmentStrategy() {
return alignmentStrategy;
}
public boolean isAlignInside() {
return alignInside;
}
}

7
src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareButton.java

@ -29,7 +29,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class TooltipAwareButton extends JButton {
public class TooltipAwareButton extends JButton implements ToolTipAware {
private final ToolTipContext context = new ToolTipContext(this)
.setAlignment(Alignment.CENTER)
@ -64,4 +64,9 @@ public class TooltipAwareButton extends JButton {
public JToolTip createToolTip() {
return context.getToolTip();
}
@Override
public ToolTipContext getToolTipContext() {
return context;
}
}

7
src/main/java/com/github/weisj/darklaf/components/tooltip/TooltipAwareToggleButton.java

@ -29,7 +29,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class TooltipAwareToggleButton extends JToggleButton {
public class TooltipAwareToggleButton extends JToggleButton implements ToolTipAware {
private final ToolTipContext context = new ToolTipContext(this)
.setAlignment(Alignment.CENTER)
@ -64,4 +64,9 @@ public class TooltipAwareToggleButton extends JToggleButton {
public JToolTip createToolTip() {
return context.getToolTip();
}
@Override
public ToolTipContext getToolTipContext() {
return context;
}
}

71
src/test/java/demo/toolTip/ToolTipDemo.java

@ -23,54 +23,57 @@
*/
package demo.toolTip;
import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.components.alignment.Alignment;
import com.github.weisj.darklaf.components.alignment.AlignmentStrategy;
import com.github.weisj.darklaf.components.tooltip.ToolTipContext;
import com.github.weisj.darklaf.components.tooltip.TooltipAwareButton;
import demo.ComponentDemo;
import demo.DemoPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class ToolTipDemo {
public class ToolTipDemo implements ComponentDemo {
public static void main(final String[] args) {
//Todo Rework Demo
SwingUtilities.invokeLater(() -> {
LafManager.install();
JFrame f = new JFrame();
JPanel p = new JPanel(new GridBagLayout());
p.add(new JButton("Button with very very long text") {
private final ToolTipContext context = new ToolTipContext(this).setAlignment(Alignment.CENTER)
.setCenterAlignment(Alignment.SOUTH_EAST);
{
setToolTipText("ToolTip \n multiline \n third line's a charm");
// setToolTipText("ToolTip");
ComponentDemo.showDemo(new ToolTipDemo());
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(getWidth() / 2, getHeight() / 2, 1, 1);
}
public JComponent createComponent() {
TooltipAwareButton button = new TooltipAwareButton("Demo Button");
DemoPanel panel = new DemoPanel(button);
ToolTipContext context = button.getToolTipContext();
button.setToolTipText("ToolTip demo text!");
@Override
public Point getToolTipLocation(final MouseEvent event) {
return context.getToolTipLocation(event);
}
JPanel controlPanel = panel.getControls();
controlPanel.setLayout(new GridLayout(4, 2));
@Override
public JToolTip createToolTip() {
return context.getToolTip();
controlPanel.add(new JLabel());
controlPanel.add(new JCheckBox("Align inside") {{
setSelected(context.isAlignInside());
addActionListener(e -> context.setAlignInside(isSelected()));
}});
controlPanel.add(new JLabel("Alignment:", JLabel.RIGHT));
controlPanel.add(new JComboBox<Alignment>(Alignment.values()) {{
setSelectedItem(context.getAlignment());
addItemListener(e -> context.setAlignment((Alignment) e.getItem()));
}});
controlPanel.add(new JLabel("Center Alignment:", JLabel.RIGHT));
controlPanel.add(new JComboBox<Alignment>(Alignment.values()) {{
setSelectedItem(context.getCenterAlignment());
addItemListener(e -> context.setCenterAlignment((Alignment) e.getItem()));
}});
controlPanel.add(new JLabel("Alignment Strategy:", JLabel.RIGHT));
controlPanel.add(new JComboBox<AlignmentStrategy>(AlignmentStrategy.values()) {{
setSelectedItem(context.getAlignmentStrategy());
addItemListener(e -> context.setAlignmentStrategy((AlignmentStrategy) e.getItem()));
}});
return panel;
}
});
f.setContentPane(p);
f.setLocationRelativeTo(null);
f.setSize(100, 100);
f.setVisible(true);
});
@Override
public String getTitle() {
return "ToolTip Demo";
}
}

Loading…
Cancel
Save