-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the ToolBar in the Find/Replace Overlay accessible #1910
Makes the Find/Replace Overlay options accessible by tabbing through the different option buttons. Implements a new "AccessibleToolBar" class which wraps the ToolBar and allows for being natively accessible by using the normal traversal mechanism provided by SWT. fixes #1910
- Loading branch information
1 parent
fc44255
commit 769d5d1
Showing
3 changed files
with
236 additions
and
79 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
.../org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AccessibleToolBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Vector Informatik GmbH and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Vector Informatik GmbH - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.ui.texteditor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.graphics.Color; | ||
import org.eclipse.swt.layout.GridLayout; | ||
import org.eclipse.swt.widgets.Composite; | ||
import org.eclipse.swt.widgets.ToolBar; | ||
import org.eclipse.swt.widgets.ToolItem; | ||
|
||
import org.eclipse.jface.layout.GridLayoutFactory; | ||
|
||
/** | ||
* This class wraps the ToolBar to make it possible to use tabulator-keys to | ||
* navigate between the buttons of a ToolBar. For this, we simulate a singular | ||
* ToolBar by putting each ToolItem into it's own ToolBar and composing them | ||
* into a Composite. Since the "Enter" keypress could not previously trigger | ||
* activation behavior, we listen for it manually and send according events if | ||
* necessary. | ||
*/ | ||
class AccessibleToolBar extends Composite { | ||
|
||
private List<ToolBar> toolBars = new ArrayList<>(); | ||
|
||
public AccessibleToolBar(Composite parent) { | ||
super(parent, SWT.NONE); | ||
GridLayoutFactory.fillDefaults().numColumns(0).spacing(0, 0).margins(0, 0).applyTo(this); | ||
} | ||
|
||
/** | ||
* Creates a ToolItem handled by this ToolBar and returns it. Will add a | ||
* KeyListener which will handle presses of "Enter". | ||
* | ||
* @param styleBits the StyleBits to apply to the created ToolItem | ||
* @return a newly created ToolItem | ||
*/ | ||
public ToolItem createToolItem(int styleBits) { | ||
ToolBar parent = new ToolBar(this, SWT.FLAT | SWT.HORIZONTAL); | ||
ToolItem toolItem = new ToolItem(parent, styleBits); | ||
|
||
addToolItemTraverseListener(parent, toolItem); | ||
|
||
((GridLayout) getLayout()).numColumns++; | ||
|
||
toolBars.add(parent); | ||
return toolItem; | ||
} | ||
|
||
private void addToolItemTraverseListener(ToolBar parent, ToolItem result) { | ||
parent.addTraverseListener(e -> { | ||
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) { | ||
result.setSelection(!result.getSelection()); | ||
e.doit = false; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void setBackground(Color color) { | ||
super.setBackground(color); | ||
for (ToolBar bar : toolBars) { // some ToolItems (like SWT.SEPARATOR) don't easily inherit the color from the | ||
// parent control. | ||
bar.setBackground(color); | ||
} | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...ipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AccessibleToolItemBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Vector Informatik GmbH and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Vector Informatik GmbH - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.ui.texteditor; | ||
|
||
import java.util.Objects; | ||
|
||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.events.SelectionListener; | ||
import org.eclipse.swt.graphics.Image; | ||
import org.eclipse.swt.widgets.ToolItem; | ||
|
||
/** | ||
* Builder for ToolItems for {@link AccessibleToolBar}. | ||
*/ | ||
class AccessibleToolItemBuilder { | ||
private final AccessibleToolBar accessibleToolBar; | ||
private int styleBits = SWT.NONE; | ||
private Image image; | ||
private String toolTipText; | ||
private SelectionListener selectionListener; | ||
|
||
public AccessibleToolItemBuilder(AccessibleToolBar accessibleToolBar) { | ||
this.accessibleToolBar = Objects.requireNonNull(accessibleToolBar); | ||
} | ||
|
||
public AccessibleToolItemBuilder withStyleBits(int newStyleBits) { | ||
this.styleBits = newStyleBits; | ||
return this; | ||
} | ||
|
||
public AccessibleToolItemBuilder withImage(Image newImage) { | ||
this.image = newImage; | ||
return this; | ||
} | ||
|
||
public AccessibleToolItemBuilder withToolTipText(String newToolTipText) { | ||
this.toolTipText = newToolTipText; | ||
return this; | ||
} | ||
|
||
public AccessibleToolItemBuilder withSelectionListener(SelectionListener newSelectionListener) { | ||
this.selectionListener = newSelectionListener; | ||
return this; | ||
} | ||
|
||
public ToolItem build() { | ||
ToolItem toolItem = accessibleToolBar.createToolItem(styleBits); | ||
|
||
if (image != null) { | ||
toolItem.setImage(image); | ||
} | ||
|
||
if (toolTipText != null) { | ||
toolItem.setToolTipText(toolTipText); | ||
} | ||
|
||
if (selectionListener != null) { | ||
toolItem.addSelectionListener(selectionListener); | ||
} | ||
|
||
return toolItem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters