001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.statushandlers;
011:
012: import java.util.HashMap;
013:
014: import org.eclipse.core.runtime.IAdaptable;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.QualifiedName;
017: import org.eclipse.ui.PlatformUI;
018:
019: /**
020: * <p>
021: * The StatusAdapter wraps an instance of IStatus subclass and can hold
022: * additional information either by using properties or by adding a new adapter. Used during
023: * status handling process.
024: * </p>
025: *
026: * @since 3.3
027: */
028: public class StatusAdapter implements IAdaptable {
029:
030: /**
031: * Common prefix for properties defined in this class.
032: */
033: static final String PROPERTY_PREFIX = PlatformUI.PLUGIN_ID
034: + ".workbench.statusHandlers.adapters"; //$NON-NLS-1$
035:
036: /**
037: * This property is used to add title to the adapter. If the adapter is
038: * shown in a dialog, this property is used to create title of the dialog.
039: */
040: public static final QualifiedName TITLE_PROPERTY = new QualifiedName(
041: PROPERTY_PREFIX, "title"); //$NON-NLS-1$
042:
043: /**
044: * This property is used to add timestamp to the adapter. If the adapter is
045: * shown in the UI, this property can be used for sorting and showing
046: * information about the time of status creation.
047: */
048: public static final QualifiedName TIMESTAMP_PROPERTY = new QualifiedName(
049: PROPERTY_PREFIX, "timestamp"); //$NON-NLS-1$
050:
051: private IStatus status;
052:
053: private HashMap properties;
054:
055: private HashMap adapters;
056:
057: /**
058: * Creates an instance of this class.
059: *
060: * @param status
061: * the status to wrap. May not be <code>null</code>.
062: */
063: public StatusAdapter(IStatus status) {
064: this .status = status;
065: }
066:
067: /**
068: * Associates new object which is an instance of the given class with this
069: * adapter. object will be returned when {@link IAdaptable#getAdapter(Class)}
070: * is called on the receiver with {@link Class} adapter as a parameter.
071: *
072: * @param adapter
073: * the adapter class
074: * @param object
075: * the adapter instance
076: */
077: public void addAdapter(Class adapter, Object object) {
078: if (adapters == null) {
079: adapters = new HashMap();
080: }
081: adapters.put(adapter, object);
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
088: */
089: public Object getAdapter(Class adapter) {
090: if (adapters == null) {
091: return null;
092: }
093: return adapters.get(adapter);
094: }
095:
096: /**
097: * Returns the wrapped status.
098: *
099: * @return the wrapped status set in the constructor or in
100: * <code>setStatus(IStatus)</code>. Will not be <code>null</code>.
101: */
102: public IStatus getStatus() {
103: return status;
104: }
105:
106: /**
107: * Sets a new status for this adapter.
108: *
109: * @param status
110: * the status to set. May not be <code>null</code>.
111: */
112: public void setStatus(IStatus status) {
113: this .status = status;
114: }
115:
116: /**
117: * Returns the value of the adapter's property identified by the given key,
118: * or <code>null</code> if this adapter has no such property.
119: *
120: * @param key
121: * the qualified name of the property
122: * @return the value of the property, or <code>null</code> if this adapter
123: * has no such property
124: */
125: public Object getProperty(QualifiedName key) {
126: if (properties == null) {
127: return null;
128: }
129: return properties.get(key);
130: }
131:
132: /**
133: * Sets the value of the receiver's property identified by the given key.
134: *
135: * @param key
136: * the qualified name of the property
137: * @param value
138: * the value of the property
139: */
140: public void setProperty(QualifiedName key, Object value) {
141: if (properties == null) {
142: properties = new HashMap();
143: }
144: properties.put(key, value);
145: }
146: }
|