KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > views > CheatSheetManager


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.ui.internal.cheatsheets.views;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.ui.cheatsheets.*;
22 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
23
24 /**
25  * Cheat sheet manager class. Manages cheat sheet data and optional listner.
26  */

27 public class CheatSheetManager implements ICheatSheetManager {
28
29     private static final String JavaDoc PARENT_PREFIX = "parent."; //$NON-NLS-1$
30
private static final String JavaDoc VARIABLE_END = "}"; //$NON-NLS-1$
31
private static final String JavaDoc VARIABLE_BEGIN = "${"; //$NON-NLS-1$
32
private String JavaDoc cheatsheetID;
33     private List JavaDoc listeners;
34     private Map JavaDoc dataTable = null;
35     private ICheatSheetManager parent;
36     
37     public CheatSheetManager(CheatSheetElement element) {
38         cheatsheetID = element.getID();
39         listeners = new ArrayList JavaDoc();
40         CheatSheetListener listener = element.createListenerInstance();
41         if (listener != null) {
42             addListener(listener);
43         }
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.ui.cheatsheets.ICheatSheetManager#getCheatSheetID()
48      */

49     public String JavaDoc getCheatSheetID() {
50         return cheatsheetID;
51     }
52
53     public void fireEvent(int eventType) {
54         // Send an event to every listener
55
for (Iterator JavaDoc iterator = listeners.iterator();iterator.hasNext();) {
56             ICheatSheetEvent event = new CheatSheetEvent(eventType, cheatsheetID, this);
57             CheatSheetListener listener = (CheatSheetListener)iterator.next();
58             listener.cheatSheetEvent(event);
59         }
60     }
61
62     /**
63      * returns the hashtable with all manager data stored.
64      */

65     public Map JavaDoc getData() {
66         return dataTable;
67     }
68     
69     /**
70      * Initialize all variables
71      * @param data a map containg values for all variables
72      */

73     public void setData(Map JavaDoc data) {
74         dataTable = data;
75     }
76
77     public String JavaDoc getData(String JavaDoc key) {
78         if (dataTable == null)
79             return null;
80         return (String JavaDoc) dataTable.get(key);
81     }
82     
83     /**
84      * Similar to get data except that if the key is prefixed with "parent."
85      * get the data from the parent
86      * @param qualifiedKey
87      * @return The data for this key
88      */

89     public String JavaDoc getDataQualified(String JavaDoc qualifiedKey) {
90         if (qualifiedKey.startsWith(PARENT_PREFIX) && parent != null) {
91             return parent.getData(qualifiedKey.substring(PARENT_PREFIX.length()));
92         } else {
93             return getData(qualifiedKey);
94         }
95     }
96
97     public String JavaDoc getVariableData(String JavaDoc variable) {
98         String JavaDoc result = variable;
99         if(variable != null && variable.startsWith(VARIABLE_BEGIN) && variable.endsWith(VARIABLE_END)) {
100             result = variable.substring(2,variable.length()-1);
101             result = getDataQualified(result);
102         }
103         return result;
104     }
105     
106     /**
107      * Substitute occurences of ${data} with values from the cheatsheetmanager.
108      * @param input The input string
109      * @param csm The cheatsheet manager
110      * @return The input string with substitutions made for any cheatsheet
111      * variables encountered.
112      */

113     public String JavaDoc performVariableSubstitution(String JavaDoc input)
114     {
115         String JavaDoc remaining = input;
116         String JavaDoc output = ""; //$NON-NLS-1$
117
while (remaining.length() > 0) {
118             int varIndex = remaining.indexOf(VARIABLE_BEGIN);
119             int endIndex = remaining.indexOf(VARIABLE_END, varIndex + 1);
120             if (varIndex < 0 || endIndex < 0) {
121                 output += remaining;
122                 remaining = ""; //$NON-NLS-1$
123
} else {
124                 String JavaDoc varName = remaining.substring(varIndex + VARIABLE_BEGIN.length(),
125                                                  endIndex);
126                 String JavaDoc value = getDataQualified(varName);
127                 output += remaining.substring(0, varIndex);
128                 if (value != null) {
129                     output += value;
130                 }
131                 remaining = remaining.substring(endIndex + VARIABLE_END.length());
132             }
133         }
134         return output;
135     }
136
137     /*package*/ void setData(Hashtable JavaDoc data) {
138         dataTable = data;
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.ui.cheatsheets.ICheatSheetManager#setData(java.lang.String, java.lang.String)
143      */

144     public void setData(String JavaDoc key, String JavaDoc data) {
145         if (key == null) {
146             throw new IllegalArgumentException JavaDoc();
147         }
148
149         if(data == null && dataTable != null) {
150             dataTable.remove(key);
151             return;
152         }
153
154         if (dataTable == null) {
155             dataTable = new Hashtable JavaDoc(30);
156         }
157
158         dataTable.put(key, data);
159     }
160     
161     /**
162      * Similar to setData except that if the key is prefixed by "parent."
163      * set the data in the parent.
164      * @param qualifiedKey A key which may be prefixed by parent.
165      * @param data The value to set
166      */

167     public void setDataQualified(String JavaDoc qualifiedKey, String JavaDoc data) {
168         if (qualifiedKey == null) {
169             throw new IllegalArgumentException JavaDoc();
170         }
171         if (qualifiedKey.startsWith(PARENT_PREFIX) && parent != null) {
172             parent.setData(qualifiedKey.substring(PARENT_PREFIX.length()), data);
173         } else {
174             setData(qualifiedKey, data);
175         }
176     }
177
178     /**
179      * Add a listener for cheatsheet events
180      * @param listener
181      */

182     public void addListener(CheatSheetListener listener) {
183         if (listener != null) {
184             listeners.add(listener);
185         }
186     }
187
188     public ICheatSheetManager getParent() {
189         return parent;
190     }
191     
192     public void setParent(ICheatSheetManager parent) {
193         this.parent = parent;
194     }
195
196     public Set JavaDoc getKeySet() {
197         if (dataTable == null) {
198             return new HashSet JavaDoc();
199         } else {
200             return dataTable.keySet();
201         }
202     }
203 }
204
Popular Tags