KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > ViewFilterAction


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

32 public abstract class ViewFilterAction extends ViewerFilter implements IViewActionDelegate, IActionDelegate2 {
33         
34     private IViewPart fView;
35     private IAction fAction;
36     private IPropertyChangeListener fListener = new Updater();
37     
38     class Updater implements IPropertyChangeListener {
39
40         /* (non-Javadoc)
41          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
42          */

43         public void propertyChange(PropertyChangeEvent event) {
44             if (event.getProperty().equals(getPreferenceKey()) ||
45                     event.getProperty().equals(getCompositeKey())) {
46                 fAction.setChecked(getPreferenceValue());
47             }
48             
49         }
50         
51     }
52
53     public ViewFilterAction() {
54         super();
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
59      */

60     public void init(IViewPart view) {
61         fView = view;
62         fAction.setChecked(getPreferenceValue());
63         run(fAction);
64         getPreferenceStore().addPropertyChangeListener(fListener);
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
69      */

70     public void init(IAction action) {
71         fAction = action;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.ui.IActionDelegate2#dispose()
76      */

77     public void dispose() {
78         getPreferenceStore().removePropertyChangeListener(fListener);
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
83      */

84     public void runWithEvent(IAction action, Event event) {
85         run(action);
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
90      */

91     public void run(IAction action) {
92         StructuredViewer viewer = getStructuredViewer();
93         ViewerFilter[] filters = viewer.getFilters();
94         ViewerFilter filter = null;
95         for (int i = 0; i < filters.length; i++) {
96             if (filters[i] == this) {
97                 filter = filters[i];
98                 break;
99             }
100         }
101         if (filter == null) {
102             viewer.addFilter(this);
103         } else {
104             // only refresh is removing - adding will refresh automatically
105
viewer.refresh();
106         }
107         IPreferenceStore store = getPreferenceStore();
108         String JavaDoc key = getView().getSite().getId() + "." + getPreferenceKey(); //$NON-NLS-1$
109
store.setValue(key, action.isChecked());
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
114      */

115     public void selectionChanged(IAction action, ISelection selection) {
116     }
117
118     protected IPreferenceStore getPreferenceStore() {
119         return JDIDebugUIPlugin.getDefault().getPreferenceStore();
120     }
121     
122     /**
123      * Returns the value of this filters preference (on/off) for the given
124      * view.
125      *
126      * @param part
127      * @return boolean
128      */

129     protected boolean getPreferenceValue() {
130         String JavaDoc key = getCompositeKey();
131         IPreferenceStore store = getPreferenceStore();
132         boolean value = false;
133         if (store.contains(key)) {
134             value = store.getBoolean(key);
135         } else {
136             value = store.getBoolean(getPreferenceKey());
137         }
138         return value;
139     }
140     
141     /**
142      * Returns the key for this action's preference
143      *
144      * @return String
145      */

146     protected abstract String JavaDoc getPreferenceKey();
147
148     /**
149      * Returns the key used by this action to store its preference value/setting.
150      * Based on a base key (suffix) and part id (prefix).
151      *
152      * @return preference store key
153      */

154     protected String JavaDoc getCompositeKey() {
155         String JavaDoc baseKey = getPreferenceKey();
156         String JavaDoc viewKey = getView().getSite().getId();
157         return viewKey + "." + baseKey; //$NON-NLS-1$
158
}
159     
160     protected IViewPart getView() {
161         return fView;
162     }
163     
164     protected StructuredViewer getStructuredViewer() {
165         IDebugView view = (IDebugView)getView().getAdapter(IDebugView.class);
166         if (view != null) {
167             Viewer viewer = view.getViewer();
168             if (viewer instanceof StructuredViewer) {
169                 return (StructuredViewer)viewer;
170             }
171         }
172         return null;
173     }
174     
175     /**
176      * Returns whether this action is selected/checked.
177      *
178      * @return whether this action is selected/checked
179      */

180     protected boolean getValue() {
181         return fAction.isChecked();
182     }
183 }
184
Popular Tags