Don’t listen to EagleEye, Just because he is the grand administrator, badass captain and owner of this forum, does not mean he knows what he is speaking of
- Question
There are actions (class CAction or DockAction): code to be executed when the user triggers them, e.g. by clicking a button. Each action is associated with some icons and some text. Actions do not know how they are presented to the user.
There are buttons wich represent actions, these buttons take the text and icon of the actions and paint them somehow onto the screen. In order to change the background and catch the mouse, you need to write your own button. That’s actually easy: take an existing action and make a subclass like the „RedButton“ in the example below. Create a factory like the „RedButtonViewGenerator“ in the example, and connect the factory like in line 132.
- Question
Actions can have a text, and all the built-in actions do have a text, but this text is only shown if they are in a menu. E.g. make a right-click on a tab to see a popup menu with all the actions. Due to limited space, no button class supports any kind of text (but they have tooltips). You would have to write a button with text yourself.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import bibliothek.extension.gui.dock.theme.eclipse.RoundRectButton;
import bibliothek.gui.Dockable;
import bibliothek.gui.dock.action.ActionType;
import bibliothek.gui.dock.action.ButtonDockAction;
import bibliothek.gui.dock.action.view.ActionViewConverter;
import bibliothek.gui.dock.action.view.ViewGenerator;
import bibliothek.gui.dock.action.view.ViewTarget;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.CLocation;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.layout.ThemeMap;
import bibliothek.gui.dock.common.mode.ExtendedMode;
import bibliothek.gui.dock.themes.basic.action.BasicButtonHandler;
import bibliothek.gui.dock.themes.basic.action.BasicButtonModel;
import bibliothek.gui.dock.themes.basic.action.BasicTitleViewItem;
import bibliothek.gui.dock.themes.basic.action.BasicTrigger;
import bibliothek.util.Colors;
public class Dock7 {
/**
* A custom button with our new painting code. RedButton extends from
* Component, so it is possible to add a MouseListener.
*/
public static class RedButton extends RoundRectButton{
public RedButton( BasicTrigger trigger ){
super( trigger );
}
@Override
protected void paintComponent( Graphics g ) {
Color background = getBackground();
BasicButtonModel model = getModel();
Color border = null;
if( model.isMousePressed() ){
border = Color.RED;
background = Colors.darker( border, 0.5 );
}
else if( model.isSelected() || model.isMouseInside() ){
border = Color.YELLOW;
background = Colors.darker( border, 0.5 );
}
else{
border = Color.GREEN;
background = Colors.darker( border, 0.5 );
}
int w = getWidth()-1;
int h = getHeight()-1;
if( border != null ){
g.setColor( background );
g.fillRoundRect( 0, 0, w, h, 4, 4 );
g.setColor( border );
g.drawRoundRect( 0, 0, w, h, 4, 4 );
}
Icon icon = model.getPaintIcon();
if( icon != null ){
icon.paintIcon( this, g, (w +1 - icon.getIconWidth())/2, (h +1 - icon.getIconHeight())/2 );
}
if( hasFocus() && isFocusable() && isEnabled() ){
g.setColor( Colors.diffMirror( background, 0.4 ) );
// top left
g.drawLine( 2, 3, 2, 4 );
g.drawLine( 3, 2, 4, 2 );
// top right
g.drawLine( w-2, 3, w-2, 4 );
g.drawLine( w-3, 2, w-4, 2 );
// bottom left
g.drawLine( 2, h-3, 2, h-4 );
g.drawLine( 3, h-2, 4, h-2 );
// bottom right
g.drawLine( w-2, h-3, w-2, h-4 );
g.drawLine( w-3, h-2, w-4, h-2 );
}
}
}
/**
* This view-generator creates new RedButtons.
*/
public static class RedButtonViewGenerator implements ViewGenerator<ButtonDockAction, BasicTitleViewItem<JComponent>>{
public BasicTitleViewItem<JComponent> create( ActionViewConverter converter, ButtonDockAction action, Dockable dockable ){
/*
* action <--> handler <--> model <--> button
*
* action: the code that is executed when the mouse is pressed
* handler: reads properties from action and updates model with these properties
* model: stores the states of button and is changed by handler
* button: paints a button using a look that reflects the states stored in model
*/
BasicButtonHandler handler = new BasicButtonHandler( action, dockable );
RedButton button = new RedButton( handler );
handler.setModel( button.getModel() );
return handler;
}
}
public static void main( String[] args ){
JFrame frame = new JFrame( "Demo" );
CControl control = new CControl( frame );
control.setTheme( ThemeMap.KEY_ECLIPSE_THEME );
// access the action-view-factories
ActionViewConverter actions = control.intern().getController().getActionViewConverter();
// put a factory for our custom button
actions.putClient( ActionType.BUTTON, ViewTarget.TITLE, new RedButtonViewGenerator() );
frame.add( control.getContentArea(), BorderLayout.CENTER );
CGrid grid = new CGrid( control );
grid.add( 0, 0, 1, 1, createDockable( "Red", Color.RED ) );
grid.add( 0, 1, 1, 1, createDockable( "Green", Color.GREEN ) );
grid.add( 0, 2, 1, 1, createDockable( "Blue", Color.BLUE ) );
grid.add( 1, 0, 1, 1, createDockable( "Cyan", Color.CYAN ) );
grid.add( 1, 1, 1, 1, createDockable( "Magenta", Color.MAGENTA ) );
grid.add( 1, 2, 1, 1, createDockable( "Yellow", Color.YELLOW ) );
control.getContentArea().deploy( grid );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setBounds( 20, 20, 400, 400 );
frame.setVisible( true );
}
public static SingleCDockable createDockable( String title, Color color ){
JPanel panel = new JPanel();
panel.setOpaque( true );
panel.setBackground( color );
DefaultSingleCDockable dockable = new DefaultSingleCDockable( title, title, panel );
dockable.setDefaultLocation(ExtendedMode.MINIMIZED, CLocation.base().minimalEast());
dockable.setDefaultLocation(ExtendedMode.EXTERNALIZED, CLocation.external( 0, 0, 300, 300 ));
return dockable;
}
}