KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > navigator > JavaNavigatorViewActionProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.jdt.internal.ui.navigator;
12
13 import java.util.Arrays JavaDoc;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16
17 import org.eclipse.ui.IActionBars;
18 import org.eclipse.ui.IMemento;
19 import org.eclipse.ui.actions.ActionContext;
20 import org.eclipse.ui.navigator.CommonActionProvider;
21 import org.eclipse.ui.navigator.ICommonActionExtensionSite;
22 import org.eclipse.ui.navigator.IExtensionActivationListener;
23 import org.eclipse.ui.navigator.IExtensionStateModel;
24 import org.eclipse.ui.navigator.INavigatorActivationService;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27 import org.eclipse.jdt.internal.ui.navigator.IExtensionStateConstants.Values;
28
29 /**
30  * Contributes the following actions to the menu on behalf of the JDT content
31  * extension.
32  *
33  * <ul>
34  * <li>{@link CommonLayoutActionGroup}. Contributes the "Package Presentation>" submenu in the View's drop down menu (not right-click).</li>
35  * </ul>
36  */

37 public class JavaNavigatorViewActionProvider extends CommonActionProvider {
38
39     private static final int HIERARCHICAL_LAYOUT= 0x1;
40
41     private static final int FLAT_LAYOUT= 0x2;
42
43     private static final String JavaDoc TAG_LAYOUT= "org.eclipse.jdt.internal.ui.navigator.layout"; //$NON-NLS-1$
44

45     private IExtensionStateModel fStateModel;
46
47     private CommonLayoutActionGroup fLayoutActionGroup;
48
49     private ICommonActionExtensionSite fExtensionSite;
50
51     private String JavaDoc fExtensionId;
52
53     private IActionBars fActionBars;
54
55     private boolean fEnabled= false;
56
57     private IExtensionActivationListener fMenuUpdater= new IExtensionActivationListener() {
58
59         public void onExtensionActivation(String JavaDoc viewerId, String JavaDoc[] theNavigatorExtensionIds, boolean isCurrentlyActive) {
60
61             if (fExtensionSite != null && fActionBars != null) {
62
63                 int search= Arrays.binarySearch(theNavigatorExtensionIds, fExtensionId);
64                 if (search > -1) {
65                     if (isMyViewer(viewerId)) {
66                         if (wasEnabled(isCurrentlyActive))
67                             fLayoutActionGroup.fillActionBars(fActionBars);
68
69                         else
70                             if (wasDisabled(isCurrentlyActive))
71                                 fLayoutActionGroup.unfillActionBars(fActionBars);
72                         // else no change
73
}
74                     fEnabled= isCurrentlyActive;
75                 }
76             }
77
78         }
79
80         private boolean isMyViewer(String JavaDoc viewerId) {
81             String JavaDoc myViewerId= fExtensionSite.getViewSite().getId();
82             return myViewerId != null && myViewerId.equals(viewerId);
83         }
84
85         private boolean wasDisabled(boolean isActive) {
86             return fEnabled && !isActive;
87         }
88
89         private boolean wasEnabled(boolean isActive) {
90             return !fEnabled && isActive;
91         }
92     };
93
94
95     public void fillActionBars(IActionBars actionBars) {
96         fActionBars= actionBars;
97         fLayoutActionGroup.fillActionBars(actionBars);
98     }
99
100     public void init(ICommonActionExtensionSite site) {
101
102         fExtensionSite= site;
103
104         fStateModel= fExtensionSite.getExtensionStateModel();
105         fLayoutActionGroup= new CommonLayoutActionGroup(fExtensionSite.getStructuredViewer(), fStateModel);
106
107         INavigatorActivationService activationService= fExtensionSite.getContentService().getActivationService();
108         activationService.addExtensionActivationListener(fMenuUpdater);
109
110         fExtensionId= fExtensionSite.getExtensionId();
111
112         fEnabled= true;
113
114     }
115
116     public void dispose() {
117         super.dispose();
118         fExtensionSite.getContentService().getActivationService().removeExtensionActivationListener(fMenuUpdater);
119     }
120
121     public void setContext(ActionContext context) {
122         super.setContext(context);
123     }
124
125     public void restoreState(IMemento memento) {
126         boolean isCurrentLayoutFlat= true;
127         Integer JavaDoc state= null;
128         if (memento != null)
129             state= memento.getInteger(TAG_LAYOUT);
130
131         // If no memento try an restore from preference store
132
if (state == null) {
133             IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
134             state= new Integer JavaDoc(store.getInt(TAG_LAYOUT));
135         }
136
137         if (state.intValue() == FLAT_LAYOUT)
138             isCurrentLayoutFlat= true;
139         else
140             if (state.intValue() == HIERARCHICAL_LAYOUT)
141                 isCurrentLayoutFlat= false;
142
143         fStateModel.setBooleanProperty(Values.IS_LAYOUT_FLAT, isCurrentLayoutFlat);
144         fLayoutActionGroup.setFlatLayout(isCurrentLayoutFlat);
145     }
146
147     public void saveState(IMemento aMemento) {
148         super.saveState(aMemento);
149         IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
150         if (fStateModel.getBooleanProperty(Values.IS_LAYOUT_FLAT))
151             store.setValue(TAG_LAYOUT, FLAT_LAYOUT);
152         else
153             store.setValue(TAG_LAYOUT, HIERARCHICAL_LAYOUT);
154
155     }
156 }
157
Popular Tags