| 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 | |
| 12 | package org.eclipse.ui.internal.ide.model; |
| 13 | |
| 14 | import org.eclipse.core.resources.IMarker; |
| 15 | import org.eclipse.core.resources.IResource; |
| 16 | import org.eclipse.core.runtime.CoreException; |
| 17 | import org.eclipse.jface.resource.ImageDescriptor; |
| 18 | import org.eclipse.ui.IMarkerActionFilter; |
| 19 | import org.eclipse.ui.actions.SimpleWildcardTester; |
| 20 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
| 21 | import org.eclipse.ui.model.WorkbenchAdapter; |
| 22 | |
| 23 | /** |
| 24 | * Model object for adapting IMarker objects to the IWorkbenchAdapter |
| 25 | * interface. |
| 26 | */ |
| 27 | public class WorkbenchMarker extends WorkbenchAdapter implements |
| 28 | IMarkerActionFilter { |
| 29 | /* |
| 30 | * (non-Javadoc) |
| 31 | * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object) |
| 32 | */ |
| 33 | public ImageDescriptor getImageDescriptor(Object o) { |
| 34 | if (!(o instanceof IMarker)) { |
| 35 | return null; |
| 36 | } |
| 37 | return IDEWorkbenchPlugin.getDefault().getMarkerImageProviderRegistry() |
| 38 | .getImageDescriptor((IMarker) o); |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * (non-Javadoc) |
| 43 | * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object) |
| 44 | */ |
| 45 | public String getLabel(Object o) { |
| 46 | IMarker marker = (IMarker) o; |
| 47 | return marker.getAttribute(IMarker.MESSAGE, "");//$NON-NLS-1$ |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * (non-Javadoc) |
| 52 | * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object) |
| 53 | */ |
| 54 | public Object getParent(Object o) { |
| 55 | return ((IMarker) o).getResource(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns whether the specific attribute matches the state of the target |
| 60 | * object. |
| 61 | * |
| 62 | * @param target the target object |
| 63 | * @param name the attribute name |
| 64 | * @param value the attriute value |
| 65 | * @return <code>true</code> if the attribute matches; <code>false</code> otherwise |
| 66 | */ |
| 67 | public boolean testAttribute(Object target, String name, String value) { |
| 68 | IMarker marker = (IMarker) target; |
| 69 | if (name.equals(TYPE)) { |
| 70 | try { |
| 71 | return value.equals(marker.getType()); |
| 72 | } catch (CoreException e) { |
| 73 | return false; |
| 74 | } |
| 75 | } else if (name.equals(SUPER_TYPE)) { |
| 76 | try { |
| 77 | return marker.isSubtypeOf(value); |
| 78 | } catch (CoreException e) { |
| 79 | return false; |
| 80 | } |
| 81 | } else if (name.equals(PRIORITY)) { |
| 82 | return testIntegerAttribute(marker, IMarker.PRIORITY, value); |
| 83 | } else if (name.equals(SEVERITY)) { |
| 84 | return testIntegerAttribute(marker, IMarker.SEVERITY, value); |
| 85 | } else if (name.equals(MESSAGE)) { |
| 86 | try { |
| 87 | String msg = (String) marker.getAttribute(IMarker.MESSAGE); |
| 88 | if (msg == null) { |
| 89 | return false; |
| 90 | } |
| 91 | return SimpleWildcardTester.testWildcardIgnoreCase(value, msg); |
| 92 | } catch (CoreException e) { |
| 93 | return false; |
| 94 | } |
| 95 | } else if (name.equals(DONE)) { |
| 96 | try { |
| 97 | value = value.toLowerCase(); |
| 98 | Boolean done = (Boolean) marker.getAttribute(IMarker.DONE); |
| 99 | if (done == null) { |
| 100 | return false; |
| 101 | } |
| 102 | return (done.booleanValue() == value.equals("true"));//$NON-NLS-1$ |
| 103 | } catch (CoreException e) { |
| 104 | return false; |
| 105 | } |
| 106 | } else if (name.equals(RESOURCE_TYPE)) { |
| 107 | int desiredType = 0; |
| 108 | |
| 109 | try { |
| 110 | desiredType = Integer.parseInt(value); |
| 111 | } catch (NumberFormatException eNumberFormat) { |
| 112 | } |
| 113 | |
| 114 | if (!(desiredType == IResource.FILE |
| 115 | || desiredType == IResource.FOLDER |
| 116 | || desiredType == IResource.PROJECT || desiredType == IResource.ROOT)) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | return (marker.getResource().getType() & desiredType) > 0; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns whether the specific integer attribute matches a value. |
| 127 | */ |
| 128 | private boolean testIntegerAttribute(IMarker marker, String attrName, |
| 129 | String value) { |
| 130 | Integer i1, i2; |
| 131 | try { |
| 132 | i1 = (Integer) marker.getAttribute(attrName); |
| 133 | if (i1 == null) { |
| 134 | return false; |
| 135 | } |
| 136 | } catch (CoreException e) { |
| 137 | return false; |
| 138 | } |
| 139 | try { |
| 140 | i2 = Integer.valueOf(value); |
| 141 | } catch (NumberFormatException e) { |
| 142 | return false; |
| 143 | } |
| 144 | return i1.equals(i2); |
| 145 | } |
| 146 | } |