KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > operations > ObjectUndoContext


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core.commands.operations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * <p>
18  * An undo context that can be used to represent any given object. Clients
19  * can add matching contexts to this context. This class may be instantiated
20  * by clients.
21  * </p>
22  *
23  * @since 3.1
24  */

25 public final class ObjectUndoContext extends UndoContext {
26
27     private Object JavaDoc object;
28
29     private String JavaDoc label;
30
31     private List JavaDoc children = new ArrayList JavaDoc();
32
33     /**
34      * Construct an operation context that represents the given object.
35      *
36      * @param object
37      * the object to be represented.
38      */

39     public ObjectUndoContext(Object JavaDoc object) {
40         this(object, null);
41     }
42
43     /**
44      * Construct an operation context that represents the given object and has a
45      * specialized label.
46      *
47      * @param object
48      * the object to be represented.
49      * @param label
50      * the label for the context
51      */

52     public ObjectUndoContext(Object JavaDoc object, String JavaDoc label) {
53         super();
54         this.object = object;
55         this.label = label;
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.eclipse.core.commands.operations.IUndoContext#getLabel()
62      */

63     public String JavaDoc getLabel() {
64         if (label != null) {
65             return label;
66         }
67         if (object != null) {
68             return object.toString();
69         }
70         return super.getLabel();
71     }
72
73     /**
74      * Return the object that is represented by this context.
75      *
76      * @return the object represented by this context.
77      */

78     public Object JavaDoc getObject() {
79         return object;
80     }
81
82     /**
83      * Add the specified context as a match of this context. Contexts added as
84      * matches of this context will be interpreted as a match of this context
85      * when the history is filtered for a particular context. Adding a match
86      * allows components to create their own contexts for implementing
87      * specialized behavior, yet have their operations appear in a more
88      * global context.
89      *
90      * @param context
91      * the context to be added as a match of this context
92      */

93     public void addMatch(IUndoContext context) {
94         children.add(context);
95     }
96
97     /**
98      * Remove the specified context as a match of this context. The context will
99      * no longer be interpreted as a match of this context when the history is
100      * filtered for a particular context. This method has no effect if the
101      * specified context was never previously added as a match.
102      *
103      * @param context
104      * the context to be removed from the list of matches for this
105      * context
106      */

107     public void removeMatch(IUndoContext context) {
108         children.remove(context);
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.eclipse.core.commands.operations.IUndoContext#matches(IUndoContext
115      * context)
116      */

117     public boolean matches(IUndoContext context) {
118         // Check first for explicit matches that have been assigned.
119
if (children.contains(context)) {
120             return true;
121         }
122         // Contexts for equal objects are considered matching
123
if (context instanceof ObjectUndoContext && getObject() != null) {
124             return getObject().equals(((ObjectUndoContext)context).getObject());
125         }
126         // Use the normal matching implementation
127
return super.matches(context);
128     }
129     
130     /**
131      * The string representation of this operation. Used for debugging purposes only.
132      * This string should not be shown to an end user.
133      *
134      * @return The string representation.
135      */

136     public String JavaDoc toString() {
137         return getLabel();
138     }
139
140
141 }
142
Popular Tags