|
Lines 1-4
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2010 EclipseSource and others. All rights reserved. This |
2 |
* Copyright (c) 2008, 2010 EclipseSource and others. All rights reserved. This |
| 3 |
* program and the accompanying materials are made available under the terms of |
3 |
* program and the accompanying materials are made available under the terms of |
| 4 |
* the Eclipse Public License v1.0 which accompanies this distribution, and is |
4 |
* the Eclipse Public License v1.0 which accompanies this distribution, and is |
|
Lines 6-96
Link Here
|
| 6 |
* |
6 |
* |
| 7 |
* Contributors: |
7 |
* Contributors: |
| 8 |
* EclipseSource - initial API and implementation |
8 |
* EclipseSource - initial API and implementation |
| 9 |
*******************************************************************************/ |
9 |
*******************************************************************************/ |
| 10 |
package org.eclipse.rap.internal.design.example.managers; |
10 |
package org.eclipse.rap.internal.design.example.managers; |
| 11 |
|
11 |
|
| 12 |
import java.util.ArrayList; |
12 |
import java.util.ArrayList; |
| 13 |
import java.util.Iterator; |
13 |
import java.util.Iterator; |
| 14 |
import java.util.List; |
|
|
| 15 |
|
| 16 |
import org.eclipse.jface.action.IContributionItem; |
14 |
import org.eclipse.jface.action.IContributionItem; |
| 17 |
import org.eclipse.jface.internal.provisional.action.ToolBarManager2; |
15 |
import org.eclipse.jface.internal.provisional.action.ToolBarManager2; |
| 18 |
import org.eclipse.rwt.lifecycle.WidgetUtil; |
16 |
import org.eclipse.rwt.lifecycle.WidgetUtil; |
| 19 |
import org.eclipse.swt.SWT; |
17 |
import org.eclipse.swt.SWT; |
| 20 |
import org.eclipse.swt.widgets.Composite; |
18 |
import org.eclipse.swt.widgets.Composite; |
| 21 |
import org.eclipse.swt.widgets.Control; |
19 |
import org.eclipse.swt.widgets.Control; |
| 22 |
import org.eclipse.swt.widgets.Menu; |
|
|
| 23 |
import org.eclipse.swt.widgets.ToolBar; |
20 |
import org.eclipse.swt.widgets.ToolBar; |
| 24 |
import org.eclipse.swt.widgets.ToolItem; |
21 |
import org.eclipse.swt.widgets.ToolItem; |
| 25 |
|
22 |
|
| 26 |
|
23 |
|
| 27 |
public class ViewToolBarManager extends ToolBarManager2 { |
24 |
public class ViewToolBarManager extends ToolBarManager2 { |
| 28 |
|
|
|
| 29 |
private static final String STYLING_VARIANT = "viewToolbar"; //$NON-NLS-1$ |
| 30 |
private ToolBar toolBar; |
| 31 |
private ToolBar fakeToolbar; |
| 32 |
|
25 |
|
|
|
26 |
private static final String STYLING_VARIANT = "viewToolbar"; //$NON-NLS-1$ |
| 27 |
private ToolBar fakeToolBar; |
| 33 |
|
28 |
|
| 34 |
public ToolBar createControl(Composite parent) { |
29 |
public ToolBar createControl( final Composite parent ) { |
| 35 |
if( !toolBarExist() && parent != null ) { |
30 |
fakeToolBar = new ToolBar( parent, SWT.NONE ); |
| 36 |
toolBar = new ToolBar( parent, SWT.NONE ); |
31 |
fakeToolBar.setVisible( false ); |
| 37 |
toolBar.setData( WidgetUtil.CUSTOM_VARIANT, STYLING_VARIANT ); |
32 |
ToolBar toolBar = super.createControl( parent ); |
| 38 |
toolBar.setMenu( getContextMenuControl() ); |
33 |
toolBar.setData( WidgetUtil.CUSTOM_VARIANT, STYLING_VARIANT ); |
| 39 |
// create the fake Toolbar |
|
|
| 40 |
fakeToolbar = new ToolBar( parent, SWT.NONE ); |
| 41 |
fakeToolbar.setVisible( false ); |
| 42 |
update( true ); |
| 43 |
} |
| 44 |
return toolBar; |
| 45 |
} |
| 46 |
|
| 47 |
public ToolBar getControl() { |
| 48 |
return toolBar; |
34 |
return toolBar; |
| 49 |
} |
35 |
} |
| 50 |
|
36 |
|
| 51 |
private Menu getContextMenuControl() { |
|
|
| 52 |
Menu result = null; |
| 53 |
if( ( getContextMenuManager() != null ) && ( toolBar != null ) ) { |
| 54 |
Menu menuWidget = getContextMenuManager().getMenu(); |
| 55 |
if( ( menuWidget == null ) || ( menuWidget.isDisposed() ) ) { |
| 56 |
menuWidget = getContextMenuManager().createContextMenu( toolBar); |
| 57 |
} |
| 58 |
result = menuWidget; |
| 59 |
} |
| 60 |
return result; |
| 61 |
} |
| 62 |
|
| 63 |
private boolean toolBarExist() { |
| 64 |
return toolBar != null && !toolBar.isDisposed(); |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* (non-Javadoc) Method declared on IContributionManager. |
| 69 |
*/ |
| 70 |
public void update( boolean force ) { |
37 |
public void update( boolean force ) { |
| 71 |
if( isDirty() || force ) { |
38 |
if( isDirty() || force ) { |
| 72 |
if( toolBarExist() ) { |
39 |
if( toolBarExist() ) { |
| 73 |
|
40 |
ToolBar toolBar = getControl(); |
| 74 |
// clean contains all active items without separators |
41 |
// clean contains all active items without double separators |
| 75 |
IContributionItem[] items = getItems(); |
42 |
IContributionItem[] items = getItems(); |
| 76 |
ArrayList clean = new ArrayList( items.length ); |
43 |
ArrayList clean = new ArrayList(items.length); |
|
|
44 |
IContributionItem separator = null; |
| 77 |
for( int i = 0; i < items.length; ++i ) { |
45 |
for( int i = 0; i < items.length; ++i ) { |
| 78 |
IContributionItem ci = items[ i ]; |
46 |
IContributionItem ci = items[ i ]; |
| 79 |
if( !ci.isSeparator() ) { |
47 |
if( ci.isSeparator() ) { |
|
|
48 |
separator = ci; |
| 49 |
} else { |
| 50 |
if( separator != null ) { |
| 51 |
if( clean.size() > 0 ) { |
| 52 |
clean.add( separator ); |
| 53 |
} |
| 54 |
separator = null; |
| 55 |
} |
| 80 |
clean.add( ci ); |
56 |
clean.add( ci ); |
| 81 |
} |
57 |
} |
| 82 |
} |
58 |
} |
| 83 |
// determine obsolete items (removed or non active) |
59 |
// determine obsolete items (removed or non active) |
| 84 |
ToolItem[] mi = toolBar.getItems(); |
60 |
ToolItem[] mi = toolBar.getItems(); |
| 85 |
ArrayList toRemove = new ArrayList(); |
61 |
ArrayList toRemove = new ArrayList( mi.length ); |
| 86 |
for( int i = 0; i < mi.length; i++ ) { |
62 |
for( int i = 0; i < mi.length; i++ ) { |
| 87 |
toRemove.add( mi[ i ] ); |
63 |
// there may be null items in a toolbar |
| 88 |
} |
64 |
if( mi[ i ] == null ) { |
| 89 |
// add fake toolbar items to the items to remove |
65 |
continue; |
| 90 |
for( int i = 0; i < fakeToolbar.getItemCount(); i++ ) { |
66 |
} |
| 91 |
toRemove.add( fakeToolbar.getItem( i ) ); |
67 |
Object data = mi[ i ].getData(); |
|
|
68 |
if( data == null |
| 69 |
|| !clean.contains( data ) |
| 70 |
|| ( data instanceof IContributionItem && ( ( IContributionItem ) data) |
| 71 |
.isDynamic() ) ) { |
| 72 |
toRemove.add( mi[ i ] ); |
| 73 |
} |
| 92 |
} |
74 |
} |
| 93 |
|
|
|
| 94 |
// Turn redraw off if the number of items to be added |
75 |
// Turn redraw off if the number of items to be added |
| 95 |
// is above a certain threshold, to minimize flicker, |
76 |
// is above a certain threshold, to minimize flicker, |
| 96 |
// otherwise the toolbar can be seen to redraw after each item. |
77 |
// otherwise the toolbar can be seen to redraw after each item. |
|
Lines 102-111
Link Here
|
| 102 |
if( useRedraw ) { |
83 |
if( useRedraw ) { |
| 103 |
toolBar.setRedraw( false ); |
84 |
toolBar.setRedraw( false ); |
| 104 |
} |
85 |
} |
| 105 |
|
|
|
| 106 |
// remove obsolete items |
86 |
// remove obsolete items |
| 107 |
for( int i = toRemove.size(); --i >= 0; ) { |
87 |
for (int i = toRemove.size(); --i >= 0;) { |
| 108 |
ToolItem item = ( ToolItem ) toRemove.get(i); |
88 |
ToolItem item = ( ToolItem )toRemove.get( i ); |
| 109 |
if( !item.isDisposed() ) { |
89 |
if( !item.isDisposed() ) { |
| 110 |
Control ctrl = item.getControl(); |
90 |
Control ctrl = item.getControl(); |
| 111 |
if( ctrl != null ) { |
91 |
if( ctrl != null ) { |
|
Lines 115-169
Link Here
|
| 115 |
item.dispose(); |
95 |
item.dispose(); |
| 116 |
} |
96 |
} |
| 117 |
} |
97 |
} |
| 118 |
|
|
|
| 119 |
// add new items |
98 |
// add new items |
| 120 |
IContributionItem src, dest; |
99 |
IContributionItem src, dest; |
| 121 |
mi = toolBar.getItems(); |
100 |
mi = toolBar.getItems(); |
| 122 |
int srcIx = 0; |
101 |
int srcIx = 0; |
| 123 |
int destIx = 0; |
102 |
int destIx = 0; |
| 124 |
|
|
|
| 125 |
for( Iterator e = clean.iterator(); e.hasNext(); ) { |
103 |
for( Iterator e = clean.iterator(); e.hasNext(); ) { |
| 126 |
src = ( IContributionItem )e.next(); |
104 |
src = ( IContributionItem )e.next(); |
| 127 |
|
|
|
| 128 |
// get corresponding item in SWT widget |
105 |
// get corresponding item in SWT widget |
| 129 |
if( srcIx < mi.length ) { |
106 |
if( srcIx < mi.length ) { |
| 130 |
dest = ( IContributionItem ) mi[ srcIx ].getData(); |
107 |
dest = ( IContributionItem )mi[ srcIx ].getData(); |
| 131 |
} else { |
108 |
} else { |
| 132 |
dest = null; |
109 |
dest = null; |
| 133 |
} |
110 |
} |
| 134 |
|
|
|
| 135 |
if( dest != null && src.equals( dest ) ) { |
111 |
if( dest != null && src.equals( dest ) ) { |
| 136 |
srcIx++; |
112 |
srcIx++; |
| 137 |
destIx++; |
113 |
destIx++; |
| 138 |
continue; |
114 |
continue; |
| 139 |
} |
115 |
} |
| 140 |
|
116 |
if( dest != null && dest.isSeparator() && src.isSeparator() ) { |
| 141 |
if( dest != null && dest.isSeparator() |
|
|
| 142 |
&& src.isSeparator() ) |
| 143 |
{ |
| 144 |
mi[ srcIx ].setData( src ); |
117 |
mi[ srcIx ].setData( src ); |
| 145 |
srcIx++; |
118 |
srcIx++; |
| 146 |
destIx++; |
119 |
destIx++; |
| 147 |
continue; |
120 |
continue; |
| 148 |
} |
121 |
} |
| 149 |
|
122 |
int start = toolBar.getItemCount(); |
| 150 |
// fill item if visible, if not fill it into the fake toolbar |
123 |
src.fill( toolBar, destIx ); |
| 151 |
ToolBar tempToolBar = null; |
124 |
int newItems = toolBar.getItemCount() - start; |
| 152 |
if( src.isVisible() ) { |
125 |
for( int i = 0; i < newItems; i++ ) { |
| 153 |
src.fill( toolBar, destIx ); |
126 |
ToolItem item = toolBar.getItem( destIx++ ); |
| 154 |
tempToolBar = toolBar; |
127 |
item.setData( src ); |
| 155 |
} else { |
|
|
| 156 |
src.fill( fakeToolbar, destIx ); |
| 157 |
tempToolBar = fakeToolbar; |
| 158 |
} |
| 159 |
// check necessary for bug 306741 |
| 160 |
if( destIx >= 0 && destIx < tempToolBar.getItemCount() - 1 ) { |
| 161 |
ToolItem toolItem = tempToolBar.getItem( destIx ); |
| 162 |
toolItem.setData( src ); |
| 163 |
toolItem.setData( WidgetUtil.CUSTOM_VARIANT, STYLING_VARIANT ); |
| 164 |
} |
128 |
} |
| 165 |
} |
129 |
} |
| 166 |
|
|
|
| 167 |
// remove any old tool items not accounted for |
130 |
// remove any old tool items not accounted for |
| 168 |
for( int i = mi.length; --i >= srcIx; ) { |
131 |
for( int i = mi.length; --i >= srcIx; ) { |
| 169 |
ToolItem item = mi[ i ]; |
132 |
ToolItem item = mi[ i ]; |
|
Lines 176-183
Link Here
|
| 176 |
item.dispose(); |
139 |
item.dispose(); |
| 177 |
} |
140 |
} |
| 178 |
} |
141 |
} |
| 179 |
|
142 |
setDirty(false); |
| 180 |
setDirty( false ); |
143 |
updateFakeToolBar(); |
|
|
144 |
disposeInvisibleItems(); |
| 181 |
} finally { |
145 |
} finally { |
| 182 |
// turn redraw back on if we turned it off above |
146 |
// turn redraw back on if we turned it off above |
| 183 |
if( useRedraw ) { |
147 |
if( useRedraw ) { |
|
Lines 187-202
Link Here
|
| 187 |
} |
151 |
} |
| 188 |
} |
152 |
} |
| 189 |
} |
153 |
} |
| 190 |
|
154 |
|
| 191 |
public List getToolItems() { |
155 |
public ToolItem[] getToolItems() { |
| 192 |
List result = new ArrayList(); |
156 |
return fakeToolBar.getItems(); |
| 193 |
for( int i = 0; i < toolBar.getItemCount(); i++ ) { |
157 |
} |
| 194 |
result.add( toolBar.getItem( i ) ); |
158 |
|
|
|
159 |
private boolean toolBarExist() { |
| 160 |
ToolBar toolBar = getControl(); |
| 161 |
return toolBar != null && !toolBar.isDisposed(); |
| 162 |
} |
| 163 |
|
| 164 |
private void updateFakeToolBar() { |
| 165 |
ToolBar toolBar = getControl(); |
| 166 |
ToolItem[] items = fakeToolBar.getItems(); |
| 167 |
for( int i = 0; i < items.length; i++ ) { |
| 168 |
items[ i ].dispose(); |
| 195 |
} |
169 |
} |
| 196 |
for( int i = 0; i < fakeToolbar.getItemCount(); i++ ) { |
170 |
items = toolBar.getItems(); |
| 197 |
result.add( fakeToolbar.getItem( i ) ); |
171 |
for( int i = 0; i < items.length; i++ ) { |
|
|
172 |
ToolItem item = new ToolItem( fakeToolBar, items[ i ].getStyle() ); |
| 173 |
item.setText( items[ i ].getText() ); |
| 174 |
item.setToolTipText( items[ i ].getToolTipText() ); |
| 175 |
item.setImage( items[ i ].getImage() ); |
| 176 |
item.setData( items[ i ].getData() ); |
| 198 |
} |
177 |
} |
| 199 |
return result; |
|
|
| 200 |
} |
178 |
} |
| 201 |
|
179 |
|
|
|
180 |
private void disposeInvisibleItems() { |
| 181 |
ToolBar toolBar = getControl(); |
| 182 |
ToolItem[] items = toolBar.getItems(); |
| 183 |
for( int i = 0; i < items.length; i++ ) { |
| 184 |
ToolItem item = items[ i ]; |
| 185 |
if( !item.isDisposed() ) { |
| 186 |
Object data = item.getData(); |
| 187 |
if( data instanceof IContributionItem ) { |
| 188 |
IContributionItem itemData = ( IContributionItem )data; |
| 189 |
if( !itemData.isVisible() ) { |
| 190 |
Control ctrl = item.getControl(); |
| 191 |
if( ctrl != null ) { |
| 192 |
item.setControl( null ); |
| 193 |
ctrl.dispose(); |
| 194 |
} |
| 195 |
item.dispose(); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 202 |
} |
201 |
} |