KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > statushandlers > StatusAdapter


1 /*******************************************************************************
2  * Copyright (c) 2007 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.statushandlers;
13
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.QualifiedName;
19 import org.eclipse.ui.PlatformUI;
20
21 /**
22  * <p>
23  * The StatusAdapter wraps an instance of IStatus subclass and can hold
24  * additional information either by using properties or by adding a new adapter. Used during
25  * status handling process.
26  * </p>
27  *
28  * @since 3.3
29  */

30 public class StatusAdapter implements IAdaptable {
31
32     /**
33      * Common prefix for properties defined in this class.
34      */

35     static final String JavaDoc PROPERTY_PREFIX = PlatformUI.PLUGIN_ID
36             + ".workbench.statusHandlers.adapters"; //$NON-NLS-1$
37

38     /**
39      * This property is used to add title to the adapter. If the adapter is
40      * shown in a dialog, this property is used to create title of the dialog.
41      */

42     public static final QualifiedName TITLE_PROPERTY = new QualifiedName(
43             PROPERTY_PREFIX, "title"); //$NON-NLS-1$
44

45     /**
46      * This property is used to add timestamp to the adapter. If the adapter is
47      * shown in the UI, this property can be used for sorting and showing
48      * information about the time of status creation.
49      */

50     public static final QualifiedName TIMESTAMP_PROPERTY = new QualifiedName(
51             PROPERTY_PREFIX, "timestamp"); //$NON-NLS-1$
52

53     private IStatus status;
54
55     private HashMap JavaDoc properties;
56
57     private HashMap JavaDoc adapters;
58
59     /**
60      * Creates an instance of this class.
61      *
62      * @param status
63      * the status to wrap. May not be <code>null</code>.
64      */

65     public StatusAdapter(IStatus status) {
66         this.status = status;
67     }
68
69     /**
70      * Associates new object which is an instance of the given class with this
71      * adapter. object will be returned when {@link IAdaptable#getAdapter(Class)}
72      * is called on the receiver with {@link Class} adapter as a parameter.
73      *
74      * @param adapter
75      * the adapter class
76      * @param object
77      * the adapter instance
78      */

79     public void addAdapter(Class JavaDoc adapter, Object JavaDoc object) {
80         if (adapters == null) {
81             adapters = new HashMap JavaDoc();
82         }
83         adapters.put(adapter, object);
84     }
85
86     /*
87      * (non-Javadoc)
88      *
89      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
90      */

91     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
92         if (adapters == null) {
93             return null;
94         }
95         return adapters.get(adapter);
96     }
97
98     /**
99      * Returns the wrapped status.
100      *
101      * @return the wrapped status set in the constructor or in
102      * <code>setStatus(IStatus)</code>. Will not be <code>null</code>.
103      */

104     public IStatus getStatus() {
105         return status;
106     }
107
108     /**
109      * Sets a new status for this adapter.
110      *
111      * @param status
112      * the status to set. May not be <code>null</code>.
113      */

114     public void setStatus(IStatus status) {
115         this.status = status;
116     }
117
118     /**
119      * Returns the value of the adapter's property identified by the given key,
120      * or <code>null</code> if this adapter has no such property.
121      *
122      * @param key
123      * the qualified name of the property
124      * @return the value of the property, or <code>null</code> if this adapter
125      * has no such property
126      */

127     public Object JavaDoc getProperty(QualifiedName key) {
128         if (properties == null) {
129             return null;
130         }
131         return properties.get(key);
132     }
133
134     /**
135      * Sets the value of the receiver's property identified by the given key.
136      *
137      * @param key
138      * the qualified name of the property
139      * @param value
140      * the value of the property
141      */

142     public void setProperty(QualifiedName key, Object JavaDoc value) {
143         if (properties == null) {
144             properties = new HashMap JavaDoc();
145         }
146         properties.put(key, value);
147     }
148 }
149
Popular Tags