upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / util / DeleteIProblemMarkerAction.java
1 /*******************************************************************************
2  * Copyright (c) 2005, 2008 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 - Initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.util;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.ui.IObjectActionDelegate;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.actions.ActionDelegate;
26
27 import org.eclipse.cdt.core.model.ICModelMarker;
28
29 /**
30  * @author Bogdan Gheorghe
31  */
32 public class DeleteIProblemMarkerAction extends ActionDelegate implements IObjectActionDelegate {
33
34     private IStructuredSelection  selection;
35
36         /**
37          * @see ActionDelegate#run(IAction)
38          */
39         @Override
40         public void run(IAction action) {
41
42                 if (selection != null) {
43                         if (selection.isEmpty()) {
44                                 return;
45                         }
46                         try {
47                                 List<?> list = selection.toList();
48                                 List<IMarker> listMarkers = new ArrayList<IMarker>();
49                                 Iterator<?> iterator = list.iterator();
50                                 while (iterator.hasNext()) {
51                                         IMarker marker = (IMarker)iterator.next();
52                                         if (marker.isSubtypeOf(ICModelMarker.INDEXER_MARKER)) {
53                                                 listMarkers.add(marker);
54                                         }
55                                 }
56                                 // Bail out early
57                                 if (listMarkers.isEmpty()) {
58                                         return;
59                                 }
60                                 IMarker[] markers = new IMarker[listMarkers.size()];
61                                 listMarkers.toArray(markers);
62                                 // be sure to only invoke one workspace operation
63                                 ResourcesPlugin.getWorkspace().deleteMarkers(markers);
64                                 selection = null;
65                         } catch (CoreException e) {
66                         }
67                 }
68         }
69
70         /**
71          * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
72          */
73         public void setActivePart(IAction action, IWorkbenchPart targetPart) {
74         }
75
76         @Override
77         public void selectionChanged(IAction action, ISelection selection) {
78                 boolean enable = false;
79                 if (selection instanceof IStructuredSelection) {
80                         Object object = ((IStructuredSelection) selection).getFirstElement();
81                         if (object instanceof IMarker) {
82                                 try {
83                                         IMarker marker = (IMarker) object;
84                                         if (marker.isSubtypeOf(ICModelMarker.INDEXER_MARKER)) {
85                                                         enable = true;
86                                         }
87                                         this.selection = (IStructuredSelection)selection;
88                                         action.setEnabled(enable);
89                                 } catch (CoreException e) {
90                                 }
91                         }
92                 }
93         }
94
95 }