KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > FastViewBarContextMenuContribution


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.jface.action.ContributionItem;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.MenuItem;
19 import org.eclipse.ui.IViewReference;
20
21 public class FastViewBarContextMenuContribution extends ContributionItem {
22     private MenuItem orientationItem;
23     private MenuItem restoreItem;
24     private MenuItem closeItem;
25     private FastViewBar bar;
26     private RadioMenu radioButtons;
27     private IViewReference selectedView;
28     private IntModel currentOrientation = new IntModel(SWT.VERTICAL);
29     
30     private IChangeListener orientationChangeListener = new IChangeListener() {
31         public void update(boolean changed) {
32             if (changed && selectedView != null) {
33                 bar.setOrientation(selectedView, currentOrientation.get());
34             }
35         }
36     };
37     
38     public FastViewBarContextMenuContribution(FastViewBar bar) {
39         this.bar = bar;
40         currentOrientation.addChangeListener(orientationChangeListener);
41     }
42
43     public void fill(Menu menu, int index) {
44         // TODO Auto-generated method stub
45
super.fill(menu, index);
46         
47
48         orientationItem = new MenuItem(menu, SWT.CASCADE, index++);
49         {
50             orientationItem.setText(WorkbenchMessages.FastViewBar_view_orientation);
51
52             Menu orientationSwtMenu = new Menu(orientationItem);
53             RadioMenu orientationMenu = new RadioMenu(orientationSwtMenu,
54                     currentOrientation);
55             orientationMenu
56                     .addMenuItem(
57                             WorkbenchMessages.FastViewBar_horizontal, new Integer JavaDoc(SWT.HORIZONTAL));
58             orientationMenu
59                     .addMenuItem(
60                             WorkbenchMessages.FastViewBar_vertical, new Integer JavaDoc(SWT.VERTICAL));
61
62             orientationItem.setMenu(orientationSwtMenu);
63         }
64
65         restoreItem = new MenuItem(menu, SWT.CHECK, index++);
66         restoreItem.setText(WorkbenchMessages.ViewPane_fastView);
67         restoreItem.addSelectionListener(new SelectionAdapter() {
68             public void widgetSelected(SelectionEvent e) {
69                 bar.restoreView(selectedView);
70             }
71         });
72
73         closeItem = new MenuItem(menu, SWT.NONE, index++);
74         closeItem.setText(WorkbenchMessages.WorkbenchWindow_close);
75         closeItem.addSelectionListener(new SelectionAdapter() {
76
77             public void widgetSelected(SelectionEvent e) {
78                 if (selectedView != null) {
79                     WorkbenchPage page = bar.getWindow().getActiveWorkbenchPage();
80                     if (page != null) {
81                         page.hideView(selectedView);
82                     }
83                 }
84             }
85         });
86
87        
88         // Set menu item enablement etc based on whether a view is selected
89
WorkbenchPage page = bar.getWindow().getActiveWorkbenchPage();
90         
91         if (selectedView != null) {
92             restoreItem.setEnabled(page!=null && page.isMoveable(selectedView));
93         } else {
94             restoreItem.setEnabled(false);
95         }
96         restoreItem.setSelection(true);
97         
98         if (selectedView != null) {
99             closeItem
100                     .setEnabled(page != null && page.isCloseable(selectedView));
101         } else {
102             closeItem.setEnabled(false);
103         }
104         
105         orientationItem.setEnabled(selectedView != null);
106         if (selectedView != null) {
107             // Set the new orientation, but avoid re-sending the event to our own
108
// listener
109
currentOrientation.set(bar.getOrientation(selectedView),
110                     orientationChangeListener);
111         }
112     }
113     
114     public void setTarget(IViewReference selectedView) {
115         this.selectedView = selectedView;
116     }
117
118     public boolean isDynamic() {
119         return true;
120     }
121     
122     public void dispose() {
123         if (radioButtons != null) {
124             radioButtons.dispose();
125         }
126         super.dispose();
127     }
128 }
129
Popular Tags