david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 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 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
david_williams | f3680f0 | 2005-04-13 22:43:54 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.ui.internal.taginfo; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.StringTokenizer; |
| 17 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 18 | import org.eclipse.jface.preference.IPreferenceStore; |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 19 | import org.eclipse.jface.text.ITextHover; |
nitind | 5da8920 | 2005-08-24 14:57:10 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.sse.ui.internal.Logger; |
david_williams | b9da93e | 2005-04-13 02:38:05 +0000 | [diff] [blame] | 21 | import org.eclipse.wst.sse.ui.internal.SSEUIMessages; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.sse.ui.internal.SSEUIPlugin; |
david_williams | 9bbeecc | 2005-03-10 19:47:16 +0000 | [diff] [blame] | 23 | import org.eclipse.wst.sse.ui.internal.preferences.EditorPreferenceNames; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 24 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Manages text hovers for Structured Text editors |
| 27 | * |
| 28 | * @author amywu |
| 29 | */ |
| 30 | public class TextHoverManager { |
| 31 | /** |
| 32 | * Contains description of a text hover |
| 33 | */ |
| 34 | public class TextHoverDescriptor { |
| 35 | private String fDescription; |
| 36 | private boolean fEnabled; |
| 37 | private String fId; |
| 38 | private String fLabel; |
| 39 | private String fModifierString; |
| 40 | |
| 41 | /** |
| 42 | * @param id |
| 43 | * @param label |
| 44 | * @param desc |
| 45 | */ |
| 46 | public TextHoverDescriptor(String id, String label, String desc) { |
| 47 | fId = id; |
| 48 | fLabel = label; |
| 49 | fDescription = desc; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param id |
| 54 | * @param label |
| 55 | * @param desc |
| 56 | * @param enabled |
| 57 | * @param modifierString |
| 58 | */ |
| 59 | public TextHoverDescriptor(String id, String label, String desc, boolean enabled, String modifierString) { |
| 60 | fId = id; |
| 61 | fLabel = label; |
| 62 | fDescription = desc; |
| 63 | fEnabled = enabled; |
| 64 | fModifierString = modifierString; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return Returns the fDescription. |
| 69 | */ |
| 70 | public String getDescription() { |
| 71 | return fDescription; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return Returns the fId. |
| 76 | */ |
| 77 | public String getId() { |
| 78 | return fId; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return Returns the fLabel |
| 83 | */ |
| 84 | public String getLabel() { |
| 85 | return fLabel; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return Returns the fModifierString. |
| 90 | */ |
| 91 | public String getModifierString() { |
| 92 | return fModifierString; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return Returns the fEnabled. |
| 97 | */ |
| 98 | public boolean isEnabled() { |
| 99 | return fEnabled; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param enabled |
| 104 | * The fEnabled to set. |
| 105 | */ |
| 106 | public void setEnabled(boolean enabled) { |
| 107 | fEnabled = enabled; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param modifierString |
| 112 | * The fModifierString to set. |
| 113 | */ |
| 114 | public void setModifierString(String modifierString) { |
| 115 | fModifierString = modifierString; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public static final String ANNOTATION_HOVER = "annotationHover"; //$NON-NLS-1$ |
| 120 | |
| 121 | // list of different types of Source editor hovers |
| 122 | public static final String COMBINATION_HOVER = "combinationHover"; //$NON-NLS-1$ |
| 123 | // hover descriptions are in .properties file with the key in the form of |
david_williams | b9da93e | 2005-04-13 02:38:05 +0000 | [diff] [blame] | 124 | // "[id]_desc" |
| 125 | private static final String DESCRIPTION_KEY = "_desc"; //$NON-NLS-1$ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 126 | public static final String DOCUMENTATION_HOVER = "documentationHover"; //$NON-NLS-1$ |
| 127 | public static final String HOVER_ATTRIBUTE_SEPARATOR = "|"; //$NON-NLS-1$ |
| 128 | public static final String HOVER_SEPARATOR = ";"; //$NON-NLS-1$ |
| 129 | |
| 130 | // hover labels are in .properties file with the key in the form of |
david_williams | b9da93e | 2005-04-13 02:38:05 +0000 | [diff] [blame] | 131 | // "[id]_label" |
| 132 | private static final String LABEL_KEY = "_label"; //$NON-NLS-1$ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 133 | |
| 134 | public static final String NO_MODIFIER = "0"; //$NON-NLS-1$ |
| 135 | public static final String PROBLEM_HOVER = "problemHover"; //$NON-NLS-1$ |
| 136 | public static final String[] TEXT_HOVER_IDS = new String[]{COMBINATION_HOVER, PROBLEM_HOVER, DOCUMENTATION_HOVER, ANNOTATION_HOVER}; |
| 137 | /** |
| 138 | * Current list of Structured Text editor text hovers |
| 139 | */ |
| 140 | private TextHoverDescriptor[] fTextHovers; |
| 141 | |
| 142 | public TextHoverManager() { |
| 143 | super(); |
| 144 | } |
| 145 | |
| 146 | /** |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 147 | * Create a best match hover with the give text hover as the documentation |
| 148 | * hover |
| 149 | * |
| 150 | * @param infoHover |
| 151 | * @return ITextHover |
| 152 | */ |
| 153 | public ITextHover createBestMatchHover(ITextHover infoHover) { |
| 154 | return new BestMatchHover(infoHover); |
| 155 | } |
| 156 | |
| 157 | /** |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 158 | * Generate a list of text hover descriptors from the given delimited |
| 159 | * string |
| 160 | * |
| 161 | * @param textHoverStrings |
| 162 | * @return |
| 163 | */ |
| 164 | public TextHoverDescriptor[] generateTextHoverDescriptors(String textHoverStrings) { |
| 165 | StringTokenizer st = new StringTokenizer(textHoverStrings, HOVER_SEPARATOR); |
| 166 | |
| 167 | // read from preference and load id-descriptor mapping to a hash table |
| 168 | HashMap idToModifier = new HashMap(st.countTokens()); |
| 169 | while (st.hasMoreTokens()) { |
| 170 | String textHoverString = st.nextToken(); |
| 171 | StringTokenizer st2 = new StringTokenizer(textHoverString, HOVER_ATTRIBUTE_SEPARATOR); |
| 172 | if (st2.countTokens() == 3) { |
| 173 | String id = st2.nextToken(); |
| 174 | boolean enabled = Boolean.valueOf(st2.nextToken()).booleanValue(); |
| 175 | String modifierString = st2.nextToken(); |
| 176 | if (modifierString.equals(NO_MODIFIER)) |
| 177 | modifierString = ""; //$NON-NLS-1$ |
| 178 | |
nitind | 5da8920 | 2005-08-24 14:57:10 +0000 | [diff] [blame] | 179 | String label = null; |
| 180 | String description = null; |
| 181 | try { |
| 182 | label = SSEUIMessages.getResourceBundle().getString(id + LABEL_KEY); |
| 183 | description = SSEUIMessages.getResourceBundle().getString(id + DESCRIPTION_KEY); |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 184 | } |
| 185 | catch (Exception e) { |
nitind | 5da8920 | 2005-08-24 14:57:10 +0000 | [diff] [blame] | 186 | Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 187 | } |
| 188 | TextHoverDescriptor descriptor = new TextHoverDescriptor(id, label, description, enabled, modifierString); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 189 | // should check to see if ids appear more than once |
| 190 | idToModifier.put(id, descriptor); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // go through all defined text hovers and match with their preference |
| 195 | TextHoverDescriptor[] descriptors = new TextHoverDescriptor[TEXT_HOVER_IDS.length]; |
| 196 | for (int i = 0; i < TEXT_HOVER_IDS.length; i++) { |
| 197 | TextHoverDescriptor desc = (TextHoverDescriptor) idToModifier.get(TEXT_HOVER_IDS[i]); |
| 198 | if (desc != null) { |
| 199 | descriptors[i] = desc; |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 200 | } |
| 201 | else { |
nitind | 5da8920 | 2005-08-24 14:57:10 +0000 | [diff] [blame] | 202 | String label = null; |
| 203 | String description = null; |
| 204 | try { |
| 205 | label = SSEUIMessages.getResourceBundle().getString(TEXT_HOVER_IDS[i] + LABEL_KEY); |
| 206 | description = SSEUIMessages.getResourceBundle().getString(TEXT_HOVER_IDS[i] + DESCRIPTION_KEY); |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 207 | } |
| 208 | catch (Exception e) { |
nitind | 5da8920 | 2005-08-24 14:57:10 +0000 | [diff] [blame] | 209 | Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 210 | } |
| 211 | descriptors[i] = new TextHoverDescriptor(TEXT_HOVER_IDS[i], label, description); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | return descriptors; |
| 215 | } |
| 216 | |
| 217 | private IPreferenceStore getPreferenceStore() { |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 218 | return SSEUIPlugin.getDefault().getPreferenceStore(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Returns the text hovers for Structured Text editor. If fTextHover has |
| 224 | * not been initialied, it will be initialized. |
| 225 | * |
| 226 | * @return Returns the fTextHovers. |
| 227 | */ |
| 228 | public TextHoverDescriptor[] getTextHovers() { |
| 229 | if (fTextHovers == null) { |
david_williams | 9bbeecc | 2005-03-10 19:47:16 +0000 | [diff] [blame] | 230 | String textHoverStrings = getPreferenceStore().getString(EditorPreferenceNames.EDITOR_TEXT_HOVER_MODIFIERS); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 231 | fTextHovers = generateTextHoverDescriptors(textHoverStrings); |
| 232 | } |
| 233 | return fTextHovers; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Sets fTextHovers to null so that next time getTextHovers is called, |
| 238 | * fTextHovers will be populated with the latest preferences. |
| 239 | */ |
| 240 | public void resetTextHovers() { |
| 241 | fTextHovers = null; |
| 242 | } |
| 243 | } |