KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > Persistence


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.contexts;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.ui.IMemento;
19 import org.eclipse.ui.internal.util.Util;
20
21 final class Persistence {
22
23     final static String JavaDoc PACKAGE_BASE = "contexts"; //$NON-NLS-1$
24

25     final static String JavaDoc PACKAGE_FULL = "org.eclipse.ui.contexts"; //$NON-NLS-1$
26

27     final static String JavaDoc PACKAGE_PREFIX = "org.eclipse.ui"; //$NON-NLS-1$
28

29     final static String JavaDoc TAG_CONTEXT = "context"; //$NON-NLS-1$
30

31     final static String JavaDoc TAG_ID = "id"; //$NON-NLS-1$
32

33     final static String JavaDoc TAG_NAME = "name"; //$NON-NLS-1$
34

35     /**
36      * Equivalent to <code>TAG_PARENT_ID</code>.
37      *
38      * @deprecated This is needed for deprecation support for the "scope"
39      * elements in the "commands" extension point.
40      */

41     final static String JavaDoc TAG_PARENT = "parent"; //$NON-NLS-1$
42

43     final static String JavaDoc TAG_PARENT_ID = "parentId"; //$NON-NLS-1$
44

45     /**
46      * Equivalent to <code>TAG_PARENT_ID</code>.
47      *
48      * @deprecated This is needed for deprecation support for
49      * "acceleratorScopes".
50      */

51     final static String JavaDoc TAG_PARENT_SCOPE = "parentScope"; //$NON-NLS-1$
52

53     final static String JavaDoc TAG_SOURCE_ID = "sourceId"; //$NON-NLS-1$
54

55     static ContextDefinition readContextDefinition(IMemento memento,
56             String JavaDoc sourceIdOverride) {
57         if (memento == null) throw new NullPointerException JavaDoc();
58
59         String JavaDoc id = memento.getString(TAG_ID);
60         String JavaDoc name = memento.getString(TAG_NAME);
61         String JavaDoc parentId = memento.getString(TAG_PARENT_ID);
62         String JavaDoc sourceId = sourceIdOverride != null ? sourceIdOverride : memento
63                 .getString(TAG_SOURCE_ID);
64         
65         /* TODO DEPRECATED Support for the old "commands" extension point way
66          * of specifying parents, and for the old "acceleratorScopes" extension
67          * point way of specifying parents.
68          */

69         if (parentId == null) {
70             // "acceleratorScopes" support
71
parentId = memento.getString(TAG_PARENT_SCOPE);
72         }
73         if (parentId == null) {
74             // "commands" support
75
parentId = memento.getString(TAG_PARENT);
76         }
77         // TODO DEPRECATED END
78

79         return new ContextDefinition(id, name, parentId, sourceId);
80     }
81
82     static List JavaDoc readContextDefinitions(IMemento memento, String JavaDoc name,
83             String JavaDoc sourceIdOverride) {
84         if (memento == null || name == null) throw new NullPointerException JavaDoc();
85
86         IMemento[] mementos = memento.getChildren(name);
87
88         if (mementos == null) throw new NullPointerException JavaDoc();
89
90         List JavaDoc list = new ArrayList JavaDoc(mementos.length);
91
92         for (int i = 0; i < mementos.length; i++)
93             list.add(readContextDefinition(mementos[i], sourceIdOverride));
94
95         return list;
96     }
97
98     static void writeContextDefinition(IMemento memento,
99             ContextDefinition contextDefinition) {
100         if (memento == null || contextDefinition == null)
101                 throw new NullPointerException JavaDoc();
102
103         memento.putString(TAG_ID, contextDefinition.getId());
104         memento.putString(TAG_NAME, contextDefinition.getName());
105         memento.putString(TAG_PARENT_ID, contextDefinition.getParentId());
106         memento.putString(TAG_SOURCE_ID, contextDefinition.getSourceId());
107     }
108
109     static void writeContextDefinitions(IMemento memento, String JavaDoc name,
110             List JavaDoc contextDefinitions) {
111         if (memento == null || name == null || contextDefinitions == null)
112                 throw new NullPointerException JavaDoc();
113
114         contextDefinitions = new ArrayList JavaDoc(contextDefinitions);
115         Iterator JavaDoc iterator = contextDefinitions.iterator();
116
117         while (iterator.hasNext())
118             Util.assertInstance(iterator.next(), ContextDefinition.class);
119
120         iterator = contextDefinitions.iterator();
121
122         while (iterator.hasNext())
123             writeContextDefinition(memento.createChild(name),
124                     (ContextDefinition) iterator.next());
125     }
126
127     /**
128      * Constructs a new instance of <code>Persistence</code>. This class
129      * should never be instantiated.
130      */

131     private Persistence() {
132         // This class should not be instantiated.
133
}
134 }
135
Popular Tags