|
Lines 7-41
Link Here
|
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* Konstantin Komissarchik - initial implementation and ongoing maintenance |
9 |
* Konstantin Komissarchik - initial implementation and ongoing maintenance |
|
|
10 |
* Greg Amerson - [342771] Support "image+label" hint for when actions are presented in a toolbar |
| 10 |
******************************************************************************/ |
11 |
******************************************************************************/ |
| 11 |
|
12 |
|
| 12 |
package org.eclipse.sapphire.ui.swt.renderer; |
13 |
package org.eclipse.sapphire.ui.swt.renderer; |
| 13 |
|
14 |
|
| 14 |
import static org.eclipse.sapphire.modeling.util.MiscUtil.equal; |
15 |
import org.eclipse.jface.action.ControlContribution; |
| 15 |
|
|
|
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.jface.action.ActionContributionItem; |
| 19 |
import org.eclipse.jface.action.IAction; |
| 20 |
import org.eclipse.jface.action.IContributionItem; |
| 21 |
import org.eclipse.jface.action.IToolBarManager; |
16 |
import org.eclipse.jface.action.IToolBarManager; |
| 22 |
import org.eclipse.jface.action.Separator; |
|
|
| 23 |
import org.eclipse.jface.action.SubContributionItem; |
| 24 |
import org.eclipse.jface.action.SubToolBarManager; |
17 |
import org.eclipse.jface.action.SubToolBarManager; |
| 25 |
import org.eclipse.jface.action.ToolBarManager; |
18 |
import org.eclipse.jface.action.ToolBarManager; |
| 26 |
import org.eclipse.sapphire.modeling.CapitalizationType; |
|
|
| 27 |
import org.eclipse.sapphire.modeling.localization.LabelTransformer; |
| 28 |
import org.eclipse.sapphire.ui.ISapphirePart; |
19 |
import org.eclipse.sapphire.ui.ISapphirePart; |
| 29 |
import org.eclipse.sapphire.ui.SapphireAction; |
|
|
| 30 |
import org.eclipse.sapphire.ui.SapphireActionGroup; |
20 |
import org.eclipse.sapphire.ui.SapphireActionGroup; |
| 31 |
import org.eclipse.sapphire.ui.SapphireActionHandler; |
|
|
| 32 |
import org.eclipse.sapphire.ui.SapphireRenderingContext; |
21 |
import org.eclipse.sapphire.ui.SapphireRenderingContext; |
| 33 |
import org.eclipse.sapphire.ui.def.SapphireActionType; |
22 |
import org.eclipse.swt.SWT; |
| 34 |
import org.eclipse.swt.graphics.Cursor; |
23 |
import org.eclipse.swt.graphics.Cursor; |
| 35 |
import org.eclipse.swt.graphics.Rectangle; |
24 |
import org.eclipse.swt.widgets.Composite; |
|
|
25 |
import org.eclipse.swt.widgets.Control; |
| 36 |
import org.eclipse.swt.widgets.Shell; |
26 |
import org.eclipse.swt.widgets.Shell; |
| 37 |
import org.eclipse.swt.widgets.ToolBar; |
27 |
import org.eclipse.swt.widgets.ToolBar; |
| 38 |
import org.eclipse.swt.widgets.ToolItem; |
|
|
| 39 |
|
28 |
|
| 40 |
/** |
29 |
/** |
| 41 |
* @author <a href="mailto:konstantin.komissarchik@oracle.com">Konstantin Komissarchik</a> |
30 |
* @author <a href="mailto:konstantin.komissarchik@oracle.com">Konstantin Komissarchik</a> |
|
Lines 47-56
Link Here
|
| 47 |
|
36 |
|
| 48 |
{ |
37 |
{ |
| 49 |
private IToolBarManager toolbar; |
38 |
private IToolBarManager toolbar; |
|
|
39 |
private SapphireToolBarContribution sapphireToolbar; |
| 40 |
private SapphireToolBarActionPresentation delegate; |
| 50 |
|
41 |
|
| 51 |
public SapphireToolBarManagerActionPresentation( final SapphireActionPresentationManager manager ) |
42 |
public SapphireToolBarManagerActionPresentation( final SapphireActionPresentationManager manager ) |
| 52 |
{ |
43 |
{ |
| 53 |
super( manager ); |
44 |
super( manager ); |
|
|
45 |
|
| 46 |
this.delegate = new SapphireToolBarActionPresentation( manager ); |
| 54 |
} |
47 |
} |
| 55 |
|
48 |
|
| 56 |
public SapphireToolBarManagerActionPresentation( final ISapphirePart part, |
49 |
public SapphireToolBarManagerActionPresentation( final ISapphirePart part, |
|
Lines 68-259
Link Here
|
| 68 |
public void setToolBarManager( final IToolBarManager toolbar ) |
61 |
public void setToolBarManager( final IToolBarManager toolbar ) |
| 69 |
{ |
62 |
{ |
| 70 |
this.toolbar = toolbar; |
63 |
this.toolbar = toolbar; |
| 71 |
} |
|
|
| 72 |
|
| 73 |
public void render() |
| 74 |
{ |
| 75 |
setCursor( this.toolbar, null ); |
| 76 |
|
| 77 |
final SapphireRenderingContext context = getManager().getContext(); |
| 78 |
|
| 79 |
boolean first = true; |
| 80 |
String lastGroup = null; |
| 81 |
|
| 82 |
for( final SapphireAction action : getActions() ) |
| 83 |
{ |
| 84 |
final String group = action.getGroup(); |
| 85 |
|
| 86 |
if( ! first && ! equal( lastGroup, group ) ) |
| 87 |
{ |
| 88 |
this.toolbar.add( new Separator() ); |
| 89 |
} |
| 90 |
|
| 91 |
first = false; |
| 92 |
lastGroup = group; |
| 93 |
|
| 94 |
final List<SapphireActionHandler> handlers = action.getActiveHandlers(); |
| 95 |
final org.eclipse.jface.action.Action a; |
| 96 |
|
| 97 |
if( action.getType() == SapphireActionType.PUSH ) |
| 98 |
{ |
| 99 |
a = new org.eclipse.jface.action.Action( null, org.eclipse.jface.action.Action.AS_PUSH_BUTTON ) |
| 100 |
{ |
| 101 |
@Override |
| 102 |
public void run() |
| 103 |
{ |
| 104 |
if( handlers.size() == 1 ) |
| 105 |
{ |
| 106 |
handlers.get( 0 ).execute( context ); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
displayActionHandlerChoice( action ); |
| 111 |
} |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
registerHotSpot( action, new JFaceActionHotSpot( a ) ); |
| 116 |
} |
| 117 |
else if( action.getType() == SapphireActionType.TOGGLE ) |
| 118 |
{ |
| 119 |
a = new org.eclipse.jface.action.Action( null, org.eclipse.jface.action.Action.AS_CHECK_BOX ) |
| 120 |
{ |
| 121 |
@Override |
| 122 |
public void run() |
| 123 |
{ |
| 124 |
handlers.get( 0 ).execute( context ); |
| 125 |
} |
| 126 |
}; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
throw new IllegalStateException(); |
| 131 |
} |
| 132 |
|
| 133 |
a.setImageDescriptor( action.getImage( 16 ) ); |
| 134 |
a.setToolTipText( LabelTransformer.transform( action.getLabel(), CapitalizationType.TITLE_STYLE, false ) ); |
| 135 |
|
| 136 |
this.toolbar.add( a ); |
| 137 |
|
| 138 |
final Runnable updateActionEnablementStateOp = new Runnable() |
| 139 |
{ |
| 140 |
public void run() |
| 141 |
{ |
| 142 |
a.setEnabled( action.isEnabled() ); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
final Runnable updateActionCheckedStateOp = new Runnable() |
| 147 |
{ |
| 148 |
public void run() |
| 149 |
{ |
| 150 |
a.setChecked( action.isChecked() ); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
action.addListener |
| 155 |
( |
| 156 |
new SapphireAction.Listener() |
| 157 |
{ |
| 158 |
@Override |
| 159 |
public void handleEvent( final SapphireAction.Event event ) |
| 160 |
{ |
| 161 |
final String type = event.getType(); |
| 162 |
|
| 163 |
if( type.equals( SapphireAction.EVENT_ENABLEMENT_STATE_CHANGED ) ) |
| 164 |
{ |
| 165 |
updateActionEnablementStateOp.run(); |
| 166 |
} |
| 167 |
else if( type.equals( SapphireAction.EVENT_CHECKED_STATE_CHANGED ) ) |
| 168 |
{ |
| 169 |
updateActionCheckedStateOp.run(); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
); |
| 174 |
|
| 175 |
updateActionEnablementStateOp.run(); |
| 176 |
updateActionCheckedStateOp.run(); |
| 177 |
} |
| 178 |
|
64 |
|
| 179 |
this.toolbar.update( true ); |
65 |
this.sapphireToolbar = new SapphireToolBarContribution("sapphire"); |
| 180 |
} |
66 |
} |
| 181 |
|
67 |
|
| 182 |
private static void setCursor( final IToolBarManager toolBarManager, |
68 |
public void render() |
| 183 |
final Cursor cursor ) |
|
|
| 184 |
{ |
| 185 |
if( toolBarManager instanceof ToolBarManager ) |
| 186 |
{ |
| 187 |
( (ToolBarManager) toolBarManager ).getControl().setCursor( null ); |
| 188 |
} |
| 189 |
else if( toolBarManager instanceof SubToolBarManager ) |
| 190 |
{ |
| 191 |
setCursor( (IToolBarManager) ( (SubToolBarManager) toolBarManager ).getParent(), cursor ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private final class JFaceActionHotSpot |
| 196 |
|
| 197 |
extends HotSpot |
| 198 |
|
| 199 |
{ |
69 |
{ |
| 200 |
private final org.eclipse.jface.action.Action action; |
70 |
setCursor ( this.toolbar, null ); |
| 201 |
|
71 |
|
| 202 |
public JFaceActionHotSpot( final org.eclipse.jface.action.Action item ) |
72 |
this.toolbar.add(this.sapphireToolbar); |
| 203 |
{ |
73 |
|
| 204 |
this.action = item; |
74 |
this.toolbar.update( true ); // call update so our embedded toolbar will get created |
| 205 |
} |
75 |
} |
| 206 |
|
76 |
|
| 207 |
@Override |
77 |
private static void setCursor(final IToolBarManager toolBarManager, |
| 208 |
public Rectangle getBounds() |
78 |
final Cursor cursor) |
| 209 |
{ |
79 |
{ |
| 210 |
ToolItem actionToolItem = null; |
80 |
if (toolBarManager instanceof ToolBarManager) |
| 211 |
|
81 |
{ |
| 212 |
for( ToolItem toolItem : getToolBar( getToolBarManager() ).getItems() ) |
82 |
((ToolBarManager) toolBarManager).getControl().setCursor(null); |
| 213 |
{ |
83 |
} |
| 214 |
final Object data = toolItem.getData(); |
84 |
else if (toolBarManager instanceof SubToolBarManager) |
| 215 |
|
85 |
{ |
| 216 |
if( data instanceof IContributionItem && getJFaceAction( ( (IContributionItem) data ) ) == this.action ) |
86 |
setCursor((IToolBarManager) ((SubToolBarManager) toolBarManager).getParent(), cursor); |
| 217 |
{ |
87 |
} |
| 218 |
actionToolItem = toolItem; |
88 |
} |
| 219 |
break; |
89 |
|
| 220 |
} |
90 |
private final class SapphireToolBarContribution extends ControlContribution |
| 221 |
} |
91 |
{ |
| 222 |
|
92 |
private ToolBar embeddedToolbar; |
| 223 |
return toDisplay( actionToolItem.getParent(), actionToolItem.getBounds() ); |
93 |
|
| 224 |
} |
94 |
protected SapphireToolBarContribution(String id) |
| 225 |
|
95 |
{ |
| 226 |
private IAction getJFaceAction( final IContributionItem item ) |
96 |
super(id); |
| 227 |
{ |
97 |
} |
| 228 |
if( item instanceof ActionContributionItem ) |
98 |
|
| 229 |
{ |
99 |
@Override |
| 230 |
return ( (ActionContributionItem) item ).getAction(); |
100 |
public boolean isDynamic() |
| 231 |
} |
101 |
{ |
| 232 |
else if( item instanceof SubContributionItem ) |
102 |
return true; |
| 233 |
{ |
103 |
} |
| 234 |
return getJFaceAction( ( (SubContributionItem) item ).getInnerItem() ); |
104 |
|
| 235 |
} |
105 |
@Override |
| 236 |
else |
106 |
protected Control createControl(Composite parent) |
| 237 |
{ |
107 |
{ |
| 238 |
return null; |
108 |
this.embeddedToolbar = new ToolBar(parent, SWT.FLAT | SWT.RIGHT); |
| 239 |
} |
109 |
this.embeddedToolbar.setBackground(null); |
| 240 |
} |
110 |
this.embeddedToolbar.setForeground(parent.getForeground()); |
| 241 |
|
111 |
|
| 242 |
private ToolBar getToolBar( final IToolBarManager manager ) |
112 |
SapphireToolBarManagerActionPresentation.this.delegate.setToolBar(this.embeddedToolbar); |
| 243 |
{ |
113 |
|
| 244 |
if( manager instanceof ToolBarManager ) |
114 |
SapphireToolBarManagerActionPresentation.this.delegate.render(); |
| 245 |
{ |
115 |
|
| 246 |
return ( (ToolBarManager) manager ).getControl(); |
116 |
return this.embeddedToolbar; |
| 247 |
} |
117 |
} |
| 248 |
else if( manager instanceof SubToolBarManager ) |
118 |
|
| 249 |
{ |
|
|
| 250 |
return getToolBar( (IToolBarManager) ( (SubToolBarManager) manager ).getParent() ); |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
throw new IllegalStateException( manager.getClass().getName() ); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
119 |
} |
| 258 |
|
|
|
| 259 |
} |
120 |
} |