KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > MatchFilterAction


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.search2.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IAction;
17
18 import org.eclipse.ui.texteditor.IUpdate;
19
20 import org.eclipse.search.ui.text.AbstractTextSearchResult;
21 import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
22 import org.eclipse.search.ui.text.MatchFilter;
23
24
25 public class MatchFilterAction extends Action implements IUpdate {
26     
27     private MatchFilter fFilter;
28     private AbstractTextSearchViewPage fPage;
29     
30     public MatchFilterAction(AbstractTextSearchViewPage page, MatchFilter filter) {
31         super(filter.getActionLabel(), IAction.AS_CHECK_BOX);
32         fPage= page;
33         fFilter= filter;
34         setId("MatchFilterAction." + filter.getID()); //$NON-NLS-1$
35
setChecked(isActiveMatchFilter());
36     }
37
38     public void run() {
39         AbstractTextSearchResult input= fPage.getInput();
40         if (input == null) {
41             return;
42         }
43         ArrayList JavaDoc newFilters= new ArrayList JavaDoc();
44         MatchFilter[] activeMatchFilters= input.getActiveMatchFilters();
45         if (activeMatchFilters == null) {
46             return;
47         }
48         
49         for (int i= 0; i < activeMatchFilters.length; i++) {
50             if (!activeMatchFilters[i].equals(fFilter)) {
51                 newFilters.add(activeMatchFilters[i]);
52             }
53         }
54         boolean newState= isChecked();
55         if (newState) {
56             newFilters.add(fFilter);
57         }
58         input.setActiveMatchFilters((MatchFilter[]) newFilters.toArray(new MatchFilter[newFilters.size()]));
59     }
60
61     public MatchFilter getFilter() {
62         return fFilter;
63     }
64     
65     private boolean isActiveMatchFilter() {
66         AbstractTextSearchResult input= fPage.getInput();
67         if (input != null) {
68             MatchFilter[] activeMatchFilters= input.getActiveMatchFilters();
69             for (int i= 0; i < activeMatchFilters.length; i++) {
70                 if (fFilter.equals(activeMatchFilters[i])) {
71                     return true;
72                 }
73             }
74         }
75         return false;
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ui.texteditor.IUpdate#update()
80      */

81     public void update() {
82         setChecked(isActiveMatchFilter());
83     }
84 }
Popular Tags