1 11 package org.eclipse.ui.internal.intro.impl.parts; 12 13 import java.io.*; 14 import java.net.*; 15 16 import org.eclipse.help.*; 17 import org.eclipse.help.internal.context.*; 18 import org.eclipse.jface.resource.*; 19 import org.eclipse.swt.*; 20 import org.eclipse.swt.events.*; 21 import org.eclipse.swt.layout.*; 22 import org.eclipse.swt.widgets.*; 23 import org.eclipse.ui.*; 24 import org.eclipse.ui.forms.*; 25 import org.eclipse.ui.forms.events.*; 26 import org.eclipse.ui.forms.widgets.*; 27 import org.eclipse.ui.help.*; 28 import org.eclipse.ui.internal.intro.impl.*; 29 import org.eclipse.ui.internal.intro.impl.util.*; 30 import org.eclipse.ui.intro.*; 31 import org.eclipse.ui.intro.config.*; 32 33 36 public class ContextHelpStandbyPart implements IStandbyContentPart { 37 38 private ScrolledForm form; 39 private IPartListener2 partListener; 40 private Label title; 41 private Text phraseText; 42 private FormText text; 43 private String defaultText; 44 private static final String HELP_KEY = "org.eclipse.ui.help"; 46 class PartListener implements IPartListener2 { 48 49 54 public void partActivated(IWorkbenchPartReference ref) { 55 handlePartActivation(ref, true); 56 } 57 58 63 public void partBroughtToTop(IWorkbenchPartReference ref) { 64 } 65 66 71 public void partClosed(IWorkbenchPartReference ref) { 72 handlePartActivation(ref, false); 73 } 74 75 80 public void partDeactivated(IWorkbenchPartReference ref) { 81 } 82 83 88 public void partHidden(IWorkbenchPartReference ref) { 89 } 90 91 96 public void partInputChanged(IWorkbenchPartReference ref) { 97 } 98 99 104 public void partOpened(IWorkbenchPartReference ref) { 105 } 106 107 112 public void partVisible(IWorkbenchPartReference ref) { 113 } 114 } 115 116 121 public void init(IIntroPart introPart, IMemento memento) { 122 partListener = new PartListener(); 123 defaultText = IntroPlugin 124 .getString("ContextHelpStandbyPart.defaultText"); ImageUtil.registerImage(ImageUtil.HELP_TOPIC, "help_topic.gif"); } 127 128 public void createPartControl(Composite parent, FormToolkit toolkit) { 129 form = toolkit.createScrolledForm(parent); 131 TableWrapLayout layout = new TableWrapLayout(); 132 form.getBody().setLayout(layout); 133 Composite helpContainer = toolkit.createComposite(form.getBody()); 136 GridLayout glayout = new GridLayout(); 137 glayout.numColumns = 3; 138 glayout.marginWidth = glayout.marginHeight = 1; 139 helpContainer.setLayout(glayout); 140 helpContainer.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 141 toolkit.paintBordersFor(helpContainer); 142 Label label = toolkit.createLabel(helpContainer, IntroPlugin 143 .getString("ContextHelpStandbyPart.search")); label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER)); 145 phraseText = toolkit.createText(helpContainer, ""); phraseText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 147 final Button button = toolkit.createButton(helpContainer, IntroPlugin 148 .getString("ContextHelpStandbyPart.button.go"), SWT.PUSH); button.addSelectionListener(new SelectionAdapter() { 150 151 public void widgetSelected(SelectionEvent e) { 152 doSearch(phraseText.getText()); 153 } 154 }); 155 button.setEnabled(false); 156 phraseText.addModifyListener(new ModifyListener() { 157 158 public void modifyText(ModifyEvent e) { 159 String text = phraseText.getText(); 160 button.setEnabled(text.length() > 0); 161 } 162 }); 163 phraseText.addKeyListener(new KeyAdapter() { 164 165 public void keyReleased(KeyEvent e) { 166 if (e.character == '\r') { 167 if (button.isEnabled()) 168 doSearch(phraseText.getText()); 169 } 170 } 171 }); 172 title = toolkit.createLabel(form.getBody(), null, SWT.WRAP); 173 title.setText(IntroPlugin 174 .getString("ContextHelpStandbyPart.contextHelpArea.Title")); title.setFont(JFaceResources.getHeaderFont()); 176 title.setForeground(toolkit.getColors().getColor(FormColors.TITLE)); 177 text = toolkit.createFormText(form.getBody(), true); 178 text.setImage(ImageUtil.HELP_TOPIC, ImageUtil 179 .getImage(ImageUtil.HELP_TOPIC)); 180 text.addHyperlinkListener(new HyperlinkAdapter() { 181 182 public void linkActivated(HyperlinkEvent e) { 183 openLink(e.getHref()); 184 } 185 }); 186 text.setLayoutData(new TableWrapData(TableWrapData.FILL, 187 TableWrapData.FILL)); 188 text.setText(defaultText, false, false); 189 IWorkbenchWindow window = PlatformUI.getWorkbench() 190 .getActiveWorkbenchWindow(); 191 IPartService service = window.getPartService(); 192 service.addPartListener(partListener); 193 toolkit.paintBordersFor(form.getBody()); 194 } 195 196 public Control getControl() { 197 return form; 198 } 199 200 private void doSearch(String phrase) { 201 try { 202 String ephrase = URLEncoder.encode(phrase, "UTF-8"); String query = "tab=search&searchWord=" + ephrase; WorkbenchHelp.displayHelpResource(query); 205 } catch (UnsupportedEncodingException e) { 206 System.out.println(e); 207 } 208 } 209 210 private void handlePartActivation(IWorkbenchPartReference ref, 211 boolean activated) { 212 if (text.isDisposed()) 213 return; 214 IWorkbenchPart part = ref.getPart(false); 215 String partId = part.getSite().getId(); 216 if (partId.equals("org.eclipse.ui.internal.introview")) return; 219 if (activated) { 220 title.setText(IntroPlugin 221 .getString("ContextHelpStandbyPart.whatIsArea.Title") + " \"" + part.getSite().getRegisteredName() + "\"?"); String helpText = createContextHelp(part); 224 text.setText(helpText != null ? helpText : "", helpText != null, false); 226 } else { 227 title.setText(IntroPlugin 228 .getString("ContextHelpStandbyPart.contextHelpArea.Title")); text.setText(defaultText, false, false); 230 } 231 form.getBody().layout(); 232 form.reflow(true); 233 } 234 235 private String createContextHelp(IWorkbenchPart part) { 236 String text = null; 237 if (part != null) { 238 Display display = part.getSite().getShell().getDisplay(); 239 Control c = display.getFocusControl(); 240 if (c != null && c.isVisible() && !c.isDisposed()) { 241 IContext helpContext = findHelpContext(c); 242 if (helpContext != null) { 243 text = formatHelpContext(helpContext); 244 } 245 } 246 } 247 return text; 248 } 249 250 private IContext findHelpContext(Control c) { 251 String contextId = null; 252 Control node = c; 253 do { 254 contextId = (String ) node.getData(HELP_KEY); 255 if (contextId != null) 256 break; 257 node = node.getParent(); 258 } while (node != null); 259 if (contextId != null) { return HelpSystem.getContext(contextId); } 260 return null; 261 } 262 263 private String formatHelpContext(IContext context) { 264 StringBuffer sbuf = new StringBuffer (); 265 sbuf.append("<form>"); sbuf.append("<p>"); sbuf.append(decodeContextBoldTags(context)); 268 sbuf.append("</p>"); IHelpResource[] links = context.getRelatedTopics(); 270 if (links.length > 0) { 271 for (int i = 0; i < links.length; i++) { 272 IHelpResource link = links[i]; 273 sbuf.append("<li style=\"text\" indent=\"2\">"); sbuf.append("<img HREF=\""); sbuf.append(ImageUtil.HELP_TOPIC); 276 sbuf.append("\"/> "); sbuf.append("<a HREF=\""); sbuf.append(link.getHref()); 279 sbuf.append("\">"); sbuf.append(link.getLabel()); 281 sbuf.append("</a>"); sbuf.append("</li>"); } 284 } 285 sbuf.append("</form>"); return sbuf.toString(); 287 } 288 289 296 private String decodeContextBoldTags(IContext context) { 297 String styledText; 298 if (context instanceof IStyledContext) { 299 styledText = ((IStyledContext) context).getStyledText(); 300 } else { 301 styledText = context.getText(); 302 } 303 String decodedString = styledText.replaceAll("<@#\\$b>", "<b>"); decodedString = decodedString.replaceAll("</@#\\$b>", "</b>"); return decodedString; 306 } 307 308 private void openLink(Object href) { 309 String url = (String ) href; 310 if (url != null) 311 WorkbenchHelp.displayHelpResource(url); 312 } 313 314 public void dispose() { 315 IWorkbenchWindow window = PlatformUI.getWorkbench() 316 .getActiveWorkbenchWindow(); 317 if (window == null) 318 return; 319 IPartService service = window.getPartService(); 320 if (service == null) 321 return; 322 service.removePartListener(partListener); 323 } 324 325 330 public void setFocus() { 331 phraseText.setFocus(); 332 } 333 334 339 public void setInput(Object input) { 340 } 342 343 public void saveState(IMemento memento) { 344 } 345 } | Popular Tags |