Hi, how can I change tabs from keyboard?
Not directly, but ctrl+shift+e will open a little window where you can select the focused Dockable. The exact combination of keys can be configured with the property-key “DockableSelector.INIT_SELECTION”.
Could you add this functionality? For example. Ctrl+TAB change tab to next.
Hm, there is a listener “CKeyboardListener” which globally catches all KeyEvents, from there it should be easy to select the next tab.
I’ll write something up and post it perhaps on saturday.
Or perhaps today. All you need to do is to copy the “StackTabSwitcher” to your project. Also add some calls to “setFocusComponent”, it’s not necessary but it makes the user interface behave better.
At the moment I use “ctrl+right” and “ctrl+left”, you can of course change that.
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import bibliothek.gui.DockStation;
import bibliothek.gui.Dockable;
import bibliothek.gui.dock.DockElement;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.event.KeyboardListener;
public class Dock16 {
public static void main( String[] args ){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
final CControl control = new CControl( frame );
// We use API from the Core project, it makes things a bit easier
control.getController().getKeyboardController().addListener( new StackTabSwitcher() );
control.setRevertToBasicModes( false );
frame.add( control.getContentArea() );
CGrid grid = new CGrid( control );
grid.add( 0, 0, 1, 1, create( "a", "Aaaa" ) );
grid.add( 0, 0, 1, 1, create( "b", "Bbbb" ) );
grid.add( 0, 0, 1, 1, create( "c", "Cccc" ) );
grid.add( 0, 0, 1, 1, create( "d", "Dddd" ) );
grid.add( 1, 0, 1, 1, create( "e", "Eeee" ) );
grid.add( 1, 0, 1, 1, create( "f", "Ffff" ) );
grid.add( 1, 0, 1, 1, create( "g", "Gggg" ) );
grid.add( 1, 0, 1, 1, create( "h", "Hhhh" ) );
control.getContentArea().deploy( grid );
frame.setBounds( 20, 20, 400, 400 );
frame.setVisible( true );
}
private static SingleCDockable create(String key, String title){
DefaultSingleCDockable dockable = new DefaultSingleCDockable( key, title );
JButton button = new JButton( "Focusable item" );
dockable.add( button );
// Note that we mark an element as being the preferred focus owner.
// Usually the focus owner is set by clicking with the mouse, but if we use
// the keyboard the framework would not know what to do without this hint.
dockable.setFocusComponent( button );
return dockable;
}
public static final class StackTabSwitcher implements KeyboardListener {
@Override
public DockElement getTreeLocation(){
return null;
}
@Override
public boolean keyPressed( DockElement element, KeyEvent event ){
if( event.getKeyCode() == KeyEvent.VK_RIGHT && event.isControlDown() ) {
return shift( element, 1 );
}
if( event.getKeyCode() == KeyEvent.VK_LEFT && event.isControlDown() ) {
return shift( element, -1 );
}
return false;
}
@Override
public boolean keyReleased( DockElement element, KeyEvent event ){
return false;
}
@Override
public boolean keyTyped( DockElement element, KeyEvent event ){
return false;
}
private boolean shift( DockElement element, int delta ){
// 'element' is the DockElement that currently has the focus. After the
// tab changed this could be the DockStation (the parent) itself.
DockStation parent = element.asDockStation();
if( parent == null ){
parent = element.asDockable().getDockParent();
}
// we can make the method more general by not checking explicitely whether
// the parent is a StackDockStation or not
// if( parent instanceof StackDockStation ){
if( parent != null ){
Dockable focused = parent.getFrontDockable();
int index = -1;
for( int i = 0, n = parent.getDockableCount(); i<n; i++ ){
if( parent.getDockable( i ) == focused ){
index = i;
break;
}
}
if( index != -1 ){
index += delta;
index %= parent.getDockableCount();
if( index < 0 ){
index += parent.getDockableCount();
}
Dockable next = parent.getDockable( index );
parent.getController().setFocusedDockable( next, true );
return true;
}
}
return false;
}
}
}
Hi, thank you for example.
Change EclipseTabs is by order of adding tabs. If someone changes the order of tabs may be surprised. Can we read the order of the tabs that the user sees?
Ctrl+Shift+e working?
Hm, that is not possible right now. I would have to add some methods to gain access to that data. I can do that, but it will have to wait until the weekend.
I started up my brain, and used it a little bit. It was hard… “moving” is very close to “removing” and “adding”. Actually is that close, that the internal data structures look the same after both operations.
Additionally in the default implementation the order of the tabs is always the same as the results of “DockStation.getDockable”. There is even code specifically designed to ensure that constraint. So this issue you describe: I think it is not an issue at all.
Please correct me if I’m wrong…