KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > model > elements > ElementMementoProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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 package org.eclipse.debug.internal.ui.model.elements;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementCompareRequest;
19 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementMementoProvider;
20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementMementoRequest;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
22 import org.eclipse.ui.IMemento;
23
24 /**
25  * @since 3.3
26  */

27 public abstract class ElementMementoProvider implements IElementMementoProvider {
28
29
30     /* (non-Javadoc)
31      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementMementoProvider#compareElements(org.eclipse.debug.internal.ui.viewers.model.provisional.IElementCompareRequest[])
32      */

33     public void compareElements(final IElementCompareRequest[] requests) {
34         Job job = new Job("compare element") { //$NON-NLS-1$
35
protected IStatus run(IProgressMonitor monitor) {
36                 for (int i = 0; i < requests.length; i++) {
37                     IElementCompareRequest request = requests[i];
38                     try {
39                         request.setEqual(isEqual(request.getElement(), request.getMemento(), request.getPresentationContext()));
40                     } catch (CoreException e) {
41                         request.setStatus(e.getStatus());
42                     }
43                     request.done();
44                 }
45                 return Status.OK_STATUS;
46             }
47         };
48         job.setSystem(true);
49         // TODO: rule
50
job.schedule();
51     }
52
53     /**
54      * Returns whether the memento represents the given element.
55      *
56      * @param element the element to compare to the memento
57      * @param memento memento
58      * @param context the context the compare is in
59      * @return whether the memento represents the given element
60      */

61     protected abstract boolean isEqual(Object JavaDoc element, IMemento memento, IPresentationContext context) throws CoreException;
62
63     /* (non-Javadoc)
64      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IElementMementoProvider#encodeElements(org.eclipse.debug.internal.ui.viewers.model.provisional.IElementMementoRequest[])
65      */

66     public void encodeElements(final IElementMementoRequest[] requests) {
67         Job job = new Job("encode element") { //$NON-NLS-1$
68
protected IStatus run(IProgressMonitor monitor) {
69                 for (int i = 0; i < requests.length; i++) {
70                     IElementMementoRequest request = requests[i];
71                     try {
72                         if (!encodeElement(request.getElement(), request.getMemento(), request.getPresentationContext())) {
73                             request.cancel();
74                         }
75                     } catch (CoreException e) {
76                         request.setStatus(e.getStatus());
77                     }
78                     request.done();
79                 }
80                 return Status.OK_STATUS;
81             }
82         };
83         job.setSystem(true);
84         // TODO: rule
85
job.schedule();
86     }
87     
88     /**
89      * Encodes the specified element into the given memento.
90      * Returns whether the element could be encoded
91      *
92      * @param element the element to encode
93      * @param memento the memento to write to
94      * @param context presentation context
95      * @return false if cancelled/not supported
96      * @throws CoreException
97      */

98     protected abstract boolean encodeElement(Object JavaDoc element, IMemento memento, IPresentationContext context) throws CoreException;
99
100 }
101
Popular Tags