Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic accessibility #1240

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/qz/ui/component/EmLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
import java.util.Map;

/**
* Create a label at the multiplier of it's normal size, similar to CSS's "em" tag
* Create a label at the multiplier of its normal size, similar to CSS's "em" tag
*/
public class EmLabel extends JLabel {
public EmLabel() {}
public EmLabel(String text) {
super(text);
}
public EmLabel(String text, float multiplier) {
this(text, multiplier, true);
}
public EmLabel(String text, float multiplier, boolean underline) {
super(text);
Font template = getFont().deriveFont(multiplier * getFont().getSize());
stylizeComponent(this, multiplier, underline);
}

public static void stylizeComponent(Component j, float multiplier, boolean underline) {
Font template = j.getFont().deriveFont(multiplier * j.getFont().getSize());
if (!underline) {
Map<TextAttribute,Object> attributes = new HashMap<>(template.getAttributes());
attributes.remove(TextAttribute.UNDERLINE);
setFont(template.deriveFont(attributes));
j.setFont(template.deriveFont(attributes));
} else {
setFont(template);
j.setFont(template);
}
}
}
100 changes: 18 additions & 82 deletions src/qz/ui/component/LinkLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@
import qz.ui.Themeable;
import qz.utils.ShellUtilities;

import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextAttribute;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* Creates a JButton which visually appears as a clickable link
*
* TODO: Rename this class. Since switching from JLabel to a JButton, this class now has a misleading name.
*
* Created by Tres on 2/19/2015.
*/
public class LinkLabel extends EmLabel implements Themeable {
public class LinkLabel extends JButton implements Themeable {

private static final Logger log = LogManager.getLogger(LinkLabel.class);

private ArrayList<ActionListener> actionListeners;

public LinkLabel() {
super();
initialize();
Expand All @@ -39,7 +37,8 @@ public LinkLabel(String text) {
}

public LinkLabel(String text, float multiplier, boolean underline) {
super(text, multiplier, underline);
super(text);
EmLabel.stylizeComponent(this, multiplier, underline);
initialize();
}

Expand All @@ -53,15 +52,12 @@ public void setLinkLocation(final String url) {
}

public void setLinkLocation(final URL location) {
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
Desktop.getDesktop().browse(location.toURI());
}
catch(Exception e) {
log.error("", e);
}
addActionListener(ae -> {
try {
Desktop.getDesktop().browse(location.toURI());
}
catch(Exception e) {
log.error("", e);
}
});
}
Expand All @@ -74,76 +70,16 @@ private void initialize() {
Map<TextAttribute,Object> attributes = new HashMap<>(getFont().getAttributes());
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
setFont(getFont().deriveFont(attributes));
setFocusable(true);

actionListeners = new ArrayList<>();

this.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("released SPACE"), "pressed");
this.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("pressed ENTER"), "pressed");
this.getActionMap().put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
for(ActionListener actionListener : actionListeners) {
actionListener.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "keyClicked"));
}
}
});

addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
for(ActionListener actionListener : actionListeners) {
actionListener.actionPerformed(new ActionEvent(e.getSource(), e.getID(), "mouseClicked"));
}
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

@Override
public void mouseExited(MouseEvent e) {
setCursor(Cursor.getDefaultCursor());
}
});

refresh();
}

@Override
public void refresh() {
setForeground(Constants.TRUSTED_COLOR);
}

public void addActionListener(ActionListener action) {
if (!actionListeners.contains(action)) {
actionListeners.add(action);
}
}

public void removeActionListener(ActionListener action) {
actionListeners.remove(action);
}

@Override
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleLinkLabel();
}
return accessibleContext;
}

protected class AccessibleLinkLabel extends AccessibleJLabel {
@Override
public AccessibleRole getAccessibleRole() {
return AccessibleRole.HYPERLINK;
}
setBorderPainted(false);
setBorder(null);
setOpaque(false);
setFocusable(true);
tresf marked this conversation as resolved.
Show resolved Hide resolved
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
}
Loading