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