KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > variables > VariableViewToggleAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.debug.internal.ui.views.variables;
12
13
14 import org.eclipse.debug.internal.ui.DebugUIPlugin;
15 import org.eclipse.debug.ui.IDebugView;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.ui.IActionDelegate2;
23 import org.eclipse.ui.IViewActionDelegate;
24 import org.eclipse.ui.IViewPart;
25
26 /**
27  *
28  */

29 public abstract class VariableViewToggleAction implements IViewActionDelegate, IActionDelegate2 {
30     
31     private IViewPart fView;
32     private IAction fAction;
33
34     public VariableViewToggleAction() {
35         super();
36     }
37
38     /* (non-Javadoc)
39      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
40      */

41     public void init(IViewPart view) {
42         fView = view;
43         boolean checked = getPreferenceValue(view);
44         fAction.setChecked(checked);
45         run(fAction);
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
50      */

51     public void init(IAction action) {
52         fAction = action;
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.ui.IActionDelegate2#dispose()
57      */

58     public void dispose() {
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
63      */

64     public void runWithEvent(IAction action, Event event) {
65         run(action);
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
70      */

71     public void run(IAction action) {
72         IPreferenceStore store = getPreferenceStore();
73         String JavaDoc key = getView().getSite().getId() + "." + getPreferenceKey(); //$NON-NLS-1$
74
store.setValue(key, action.isChecked());
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
79      */

80     public void selectionChanged(IAction action, ISelection selection) {
81     }
82
83     protected IPreferenceStore getPreferenceStore() {
84         return DebugUIPlugin.getDefault().getPreferenceStore();
85     }
86     
87     /**
88      * Returns the value of this filters preference (on/off) for the given
89      * view.
90      *
91      * @param part
92      * @return boolean
93      */

94     protected boolean getPreferenceValue(IViewPart part) {
95         String JavaDoc baseKey = getPreferenceKey();
96         String JavaDoc viewKey = part.getSite().getId();
97         String JavaDoc compositeKey = viewKey + "." + baseKey; //$NON-NLS-1$
98
IPreferenceStore store = getPreferenceStore();
99         boolean value = false;
100         if (store.contains(compositeKey)) {
101             value = store.getBoolean(compositeKey);
102         } else {
103             value = store.getBoolean(baseKey);
104         }
105         return value;
106     }
107     
108     /**
109      * Returns the key for this action's preference
110      *
111      * @return String
112      */

113     protected abstract String JavaDoc getPreferenceKey();
114         
115     protected IViewPart getView() {
116         return fView;
117     }
118     
119     protected StructuredViewer getStructuredViewer() {
120         IDebugView view = (IDebugView)getView().getAdapter(IDebugView.class);
121         if (view != null) {
122             Viewer viewer = view.getViewer();
123             if (viewer instanceof StructuredViewer) {
124                 return (StructuredViewer)viewer;
125             }
126         }
127         return null;
128     }
129 }
130
Popular Tags