Added
Link Here
|
1 |
|
2 |
package org.eclipse.jdt.internal.ui.packageview; |
3 |
|
4 |
import java.util.ArrayList; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
|
8 |
import org.eclipse.swt.SWT; |
9 |
import org.eclipse.swt.custom.SashForm; |
10 |
import org.eclipse.swt.events.ControlAdapter; |
11 |
import org.eclipse.swt.events.ControlEvent; |
12 |
import org.eclipse.swt.events.FocusAdapter; |
13 |
import org.eclipse.swt.events.FocusEvent; |
14 |
import org.eclipse.swt.graphics.Point; |
15 |
import org.eclipse.swt.layout.FillLayout; |
16 |
import org.eclipse.swt.widgets.Composite; |
17 |
|
18 |
import org.eclipse.jface.action.Action; |
19 |
import org.eclipse.jface.action.IAction; |
20 |
//import org.eclipse.jface.action.IMenuListener; |
21 |
import org.eclipse.jface.action.IMenuManager; |
22 |
import org.eclipse.jface.action.Separator; |
23 |
import org.eclipse.jface.util.IPropertyChangeListener; |
24 |
import org.eclipse.jface.util.PropertyChangeEvent; |
25 |
import org.eclipse.jface.viewers.ISelection; |
26 |
import org.eclipse.jface.viewers.TreeViewer; |
27 |
|
28 |
import org.eclipse.ui.IActionBars; |
29 |
import org.eclipse.ui.IMemento; |
30 |
import org.eclipse.ui.IViewSite; |
31 |
import org.eclipse.ui.PartInitException; |
32 |
import org.eclipse.ui.part.ISetSelectionTarget; |
33 |
import org.eclipse.ui.part.IShowInTarget; |
34 |
import org.eclipse.ui.part.ShowInContext; |
35 |
import org.eclipse.ui.part.ViewPart; |
36 |
|
37 |
import org.eclipse.jdt.internal.ui.JavaPluginImages; |
38 |
import org.eclipse.jdt.internal.ui.viewsupport.IViewPartInputProvider; |
39 |
import org.eclipse.jdt.ui.IPackagesViewPart; |
40 |
|
41 |
|
42 |
public class SplitPackageExplorerPart extends ViewPart |
43 |
implements ISetSelectionTarget, IShowInTarget, |
44 |
IPackagesViewPart, IPropertyChangeListener, IViewPartInputProvider { |
45 |
// orientations |
46 |
static final int VIEW_ORIENTATION_VERTICAL = 0; |
47 |
static final int VIEW_ORIENTATION_HORIZONTAL = 1; |
48 |
static final int VIEW_ORIENTATION_AUTOMATIC = 2; |
49 |
|
50 |
/** |
51 |
* The current orientation; either <code>VIEW_ORIENTATION_HORIZONTAL</code> |
52 |
* <code>VIEW_ORIENTATION_VERTICAL</code>, or <code>VIEW_ORIENTATION_AUTOMATIC</code>. |
53 |
*/ |
54 |
int orientation = VIEW_ORIENTATION_AUTOMATIC; |
55 |
/** |
56 |
* The current orientation; either <code>VIEW_ORIENTATION_HORIZONTAL</code> |
57 |
* <code>VIEW_ORIENTATION_VERTICAL</code>. |
58 |
*/ |
59 |
private int currentOrientation = VIEW_ORIENTATION_AUTOMATIC; |
60 |
|
61 |
|
62 |
private IMemento memento; |
63 |
|
64 |
private List splits = new ArrayList(); |
65 |
private PackageExplorerPart currentPart; |
66 |
|
67 |
private SashForm form; |
68 |
private ControlAdapter formControlListener; |
69 |
|
70 |
private IAction addSplitPartAction; |
71 |
private IAction removeSplitPartAction; |
72 |
private ToggleOrientationAction[] toggleOrientationActions; |
73 |
|
74 |
|
75 |
public SplitPackageExplorerPart() { |
76 |
} |
77 |
|
78 |
// ViewPart |
79 |
public void init( IViewSite site, IMemento memento) throws PartInitException { |
80 |
super.init(site); |
81 |
this.memento = memento; |
82 |
} |
83 |
|
84 |
// ViewPart |
85 |
public void createPartControl( Composite parent) { |
86 |
addSplitPartAction = new AddSplitPartAction(this); |
87 |
removeSplitPartAction = new RemoveSplitPartAction(this); |
88 |
|
89 |
formControlListener = new ControlAdapter() { |
90 |
public void controlResized(ControlEvent e) { |
91 |
computeOrientation(); |
92 |
} |
93 |
}; |
94 |
form = new SashForm( parent, SWT.VERTICAL); |
95 |
form.setLayout( new FillLayout()); |
96 |
form.addControlListener(formControlListener); |
97 |
|
98 |
toggleOrientationActions = new ToggleOrientationAction[] { |
99 |
new ToggleOrientationAction(VIEW_ORIENTATION_VERTICAL), |
100 |
new ToggleOrientationAction(VIEW_ORIENTATION_HORIZONTAL), |
101 |
new ToggleOrientationAction(VIEW_ORIENTATION_AUTOMATIC)}; |
102 |
setOrientation(VIEW_ORIENTATION_AUTOMATIC); |
103 |
|
104 |
// getViewSite().getActionBars().getMenuManager().addMenuListener( new IMenuListener() { |
105 |
// public void menuAboutToShow( IMenuManager manager) { |
106 |
// // System.err.println( "### SplitPackageExplorerPart.menuAboutToShow()"); |
107 |
// } |
108 |
// }); |
109 |
|
110 |
createPackageExplorerPart(); |
111 |
|
112 |
addSplitActions(); |
113 |
} |
114 |
|
115 |
void computeOrientation() { |
116 |
if (currentOrientation == VIEW_ORIENTATION_AUTOMATIC) { |
117 |
Point size = form.getSize(); |
118 |
if (size.x != 0 && size.y != 0) { |
119 |
setOrientation( size.x > size.y ? VIEW_ORIENTATION_HORIZONTAL : VIEW_ORIENTATION_VERTICAL); |
120 |
} |
121 |
} else { |
122 |
setOrientation( currentOrientation); |
123 |
} |
124 |
} |
125 |
|
126 |
private void setOrientation(int orientation) { |
127 |
form.setOrientation((orientation == VIEW_ORIENTATION_HORIZONTAL) ? SWT.HORIZONTAL : SWT.VERTICAL); |
128 |
|
129 |
for (int i = 0; i < toggleOrientationActions.length; ++i) { |
130 |
toggleOrientationActions[i].setChecked(orientation == toggleOrientationActions[i].getOrientation()); |
131 |
} |
132 |
|
133 |
// GridLayout layout= (GridLayout) fCounterComposite.getLayout(); |
134 |
// setCounterColumns(layout); |
135 |
form.layout(); |
136 |
} |
137 |
|
138 |
|
139 |
private void createPackageExplorerPart() { |
140 |
PackageExplorerPart part = new PackageExplorerPart(); |
141 |
try { |
142 |
part.init( getViewSite(), this.memento); |
143 |
} catch( PartInitException ex) { |
144 |
throw new RuntimeException( ex.toString()); |
145 |
} |
146 |
part.createPartControl(form); |
147 |
|
148 |
splits.add(part); |
149 |
|
150 |
setCurrentPart(part); |
151 |
|
152 |
part.getTreeViewer().getTree().addFocusListener( new SplitFocusListener( this, part)); |
153 |
} |
154 |
|
155 |
// ViewPart |
156 |
public void setFocus() { |
157 |
if(currentPart!=null) { |
158 |
currentPart.setFocus(); |
159 |
} |
160 |
} |
161 |
|
162 |
|
163 |
// ISetSelectionTarget |
164 |
public void selectReveal( ISelection selection) { |
165 |
currentPart.selectReveal(selection); |
166 |
} |
167 |
|
168 |
// IMenuListener |
169 |
// public void menuAboutToShow( IMenuManager manager) { |
170 |
// System.err.println( "### SplitPackageExplorerPart.menuAboutToShow()"); |
171 |
// // TODO any cleanup? |
172 |
// currentPart.menuAboutToShow(manager); |
173 |
// } |
174 |
|
175 |
// IShowInTarget |
176 |
public boolean show( ShowInContext context) { |
177 |
return currentPart.show(context); |
178 |
} |
179 |
|
180 |
// IPackagesViewPart (require smart selection!) |
181 |
public void selectAndReveal( Object element) { |
182 |
currentPart.selectAndReveal(element); |
183 |
} |
184 |
|
185 |
// IPackagesViewPart (current viewer) |
186 |
public TreeViewer getTreeViewer() { |
187 |
return currentPart.getTreeViewer(); |
188 |
} |
189 |
|
190 |
// IPropertyChangeListener |
191 |
public void propertyChange( PropertyChangeEvent event) { |
192 |
currentPart.propertyChange(event); |
193 |
} |
194 |
|
195 |
// IViewPartInputProvider |
196 |
public Object getViewPartInput() { |
197 |
return currentPart.getViewPartInput(); |
198 |
} |
199 |
|
200 |
|
201 |
public void saveState( IMemento memento) { |
202 |
((PackageExplorerPart) this.splits.get( 0)).saveState(memento); |
203 |
} |
204 |
|
205 |
public Object getAdapter( Class key) { |
206 |
return currentPart.getAdapter( key); |
207 |
} |
208 |
|
209 |
public void dispose() { |
210 |
for( Iterator it = splits.iterator(); it.hasNext();) { |
211 |
PackageExplorerPart part = ( PackageExplorerPart) it.next(); |
212 |
part.dispose(); |
213 |
} |
214 |
|
215 |
form.removeControlListener(formControlListener); |
216 |
form.dispose(); |
217 |
|
218 |
super.dispose(); |
219 |
} |
220 |
|
221 |
public String getTitleToolTip() { |
222 |
return currentPart.getTitleToolTip(); |
223 |
} |
224 |
|
225 |
|
226 |
// Splits |
227 |
|
228 |
public void setCurrentPart( PackageExplorerPart currentPart) { |
229 |
this.currentPart = currentPart; |
230 |
} |
231 |
|
232 |
public void addSplitActions() { |
233 |
IMenuManager menu = getViewSite().getActionBars().getMenuManager(); |
234 |
menu.add(new Separator()); |
235 |
menu.add(addSplitPartAction); |
236 |
menu.add(removeSplitPartAction); |
237 |
|
238 |
menu.add(new Separator()); |
239 |
for (int i = 0; i < toggleOrientationActions.length; ++i) { |
240 |
menu.add(toggleOrientationActions[i]); |
241 |
} |
242 |
} |
243 |
|
244 |
public void activatePart(PackageExplorerPart currentPart) { |
245 |
if(this.currentPart==currentPart) { |
246 |
return; |
247 |
} |
248 |
|
249 |
IActionBars actionBars = getViewSite().getActionBars(); |
250 |
actionBars.getToolBarManager().removeAll(); |
251 |
actionBars.getMenuManager().removeAll(); |
252 |
currentPart.getActionSet().fillActionBars(actionBars); |
253 |
|
254 |
setCurrentPart(currentPart); |
255 |
|
256 |
addSplitActions(); |
257 |
currentPart.setFocus(); |
258 |
|
259 |
getSite().setSelectionProvider(currentPart.getTreeViewer()); |
260 |
} |
261 |
|
262 |
public void addSplit() { |
263 |
if(currentPart!=null) { |
264 |
IActionBars actionBars = currentPart.getViewSite().getActionBars(); |
265 |
actionBars.getToolBarManager().removeAll(); |
266 |
actionBars.getMenuManager().removeAll(); |
267 |
} |
268 |
|
269 |
createPackageExplorerPart(); |
270 |
form.layout(); |
271 |
} |
272 |
|
273 |
public void removeSplit() { |
274 |
splits.remove(currentPart); |
275 |
|
276 |
currentPart.getTreeViewer().getTree().dispose(); |
277 |
currentPart.dispose(); |
278 |
currentPart = null; |
279 |
|
280 |
form.layout(); |
281 |
|
282 |
PackageExplorerPart part = ( PackageExplorerPart) splits.get( 0); |
283 |
activatePart(part); |
284 |
} |
285 |
|
286 |
|
287 |
private static final class SplitFocusListener extends FocusAdapter { |
288 |
private final SplitPackageExplorerPart splitPart; |
289 |
private final PackageExplorerPart currentPart; |
290 |
|
291 |
public SplitFocusListener( SplitPackageExplorerPart splitPart, PackageExplorerPart currentPart) { |
292 |
this.splitPart = splitPart; |
293 |
this.currentPart = currentPart; |
294 |
} |
295 |
|
296 |
public void focusGained( FocusEvent e) { |
297 |
splitPart.activatePart(currentPart); |
298 |
} |
299 |
|
300 |
} |
301 |
|
302 |
|
303 |
private class ToggleOrientationAction extends Action { |
304 |
private final int actionOrientation; |
305 |
|
306 |
public ToggleOrientationAction(int orientation) { |
307 |
super("", AS_RADIO_BUTTON); //$NON-NLS-1$ |
308 |
|
309 |
if (orientation == VIEW_ORIENTATION_HORIZONTAL) { |
310 |
setText( "Horizontal View Orientation"); //$NON-NLS-1$ |
311 |
JavaPluginImages.setLocalImageDescriptors( this, "th_horizontal.gif"); //$NON-NLS-1$ |
312 |
} else if (orientation == VIEW_ORIENTATION_VERTICAL) { |
313 |
setText( "Vertical View Orientation"); //$NON-NLS-1$ |
314 |
JavaPluginImages.setLocalImageDescriptors( this, "th_vertical.gif"); //$NON-NLS-1$ |
315 |
} else if (orientation == VIEW_ORIENTATION_AUTOMATIC) { |
316 |
setText( "Automatic View Orientation"); //$NON-NLS-1$ |
317 |
JavaPluginImages.setLocalImageDescriptors( this, "th_automatic.gif"); //$NON-NLS-1$ |
318 |
} |
319 |
actionOrientation = orientation; |
320 |
// WorkbenchHelp.setHelp(this, IJUnitHelpContextIds.RESULTS_VIEW_TOGGLE_ORIENTATION_ACTION); |
321 |
} |
322 |
|
323 |
public int getOrientation() { |
324 |
return actionOrientation; |
325 |
} |
326 |
|
327 |
public void run() { |
328 |
if (isChecked()) { |
329 |
currentOrientation = actionOrientation; |
330 |
computeOrientation(); |
331 |
} |
332 |
} |
333 |
} |
334 |
|
335 |
|
336 |
|
337 |
} |