KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > Mementos


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.workingsets;
12
13 import org.eclipse.core.runtime.IAdaptable;
14
15 import org.eclipse.ui.IElementFactory;
16 import org.eclipse.ui.IMemento;
17 import org.eclipse.ui.IPersistableElement;
18 import org.eclipse.ui.PlatformUI;
19
20 public class Mementos {
21
22     public static final String JavaDoc TAG_FACTORY_ID = "factoryID"; //$NON-NLS-1$
23
public static final String JavaDoc TAG_ITEM = "item"; //$NON-NLS-1$
24

25     public static void saveItem(IMemento memento, IAdaptable element) {
26         IPersistableElement persistable= (IPersistableElement)element.getAdapter(IPersistableElement.class);
27         if (persistable != null) {
28             memento.putString(
29                 TAG_FACTORY_ID,
30                 persistable.getFactoryId());
31             persistable.saveState(memento);
32         }
33         
34     }
35     
36     public static IAdaptable restoreItem(IMemento memento) {
37         return restoreItem(memento, TAG_FACTORY_ID);
38     }
39     
40     public static IAdaptable restoreItem(IMemento memento, String JavaDoc factoryTag) {
41         if (memento == null)
42             return null;
43         String JavaDoc factoryID = memento.getString(factoryTag);
44         if (factoryID == null) return null;
45         IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(factoryID);
46         if (factory == null) return null;
47         return factory.createElement(memento);
48     }
49 }
50
Popular Tags