private void showMessage(String msg, boolean isError, boolean showProgress) { if (fBrowser.getControl().isDisposed()) return; StringBuilder html = new StringBuilder(); html.append( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"); //$NON-NLS-1$ html.append("<html>\n"); // $NON-NLS-1$ html.append("<body style=\"overflow: auto; font-family: ") .append(fNewsFontFamily) .append(",Verdanna,sans-serif; ") .append(fNormalFontCSS) .append("\">"); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if (showProgress) html.append( "<img src=\"" + OwlUI.getImageUri("/icons/obj16/progress.gif", "progress.gif") + "\" />"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ if (isError) html.append("<span style=\"color: darkred;\">"); // $NON-NLS-1$ else if (showProgress) html.append("<span style=\"padding-left:3px; vertical-align:top;\">"); // $NON-NLS-1$ html.append(msg); if (isError || showProgress) html.append("</span>"); // $NON-NLS-1$ html.append("</body>\n"); // $NON-NLS-1$ html.append("</html>\n"); // $NON-NLS-1$ fBrowser.getControl().setText(html.toString()); }
/* * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite) */ @Override protected Control createDialogArea(Composite parent) { /* Composite to hold all components */ Composite composite = new Composite(parent, SWT.NONE); composite.setLayout(LayoutUtils.createGridLayout(1, 0, 0)); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); /* Browser to preview News */ fBrowser = new CBrowser(composite, SWT.NONE); fBrowser.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); fBrowser.setScriptDisabled(true); fBrowser .getControl() .addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.character == SWT.ESC) close(); } }); /* Label Provider to produce HTML per News */ fLabelProvider = new NewsBrowserLabelProvider(fBrowser); /* Load and Display the Feed */ loadFeed(); /* Separator */ new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); applyDialogFont(composite); return composite; }
private void showFeed(final IFeed feed) { if (feed != null && !fBrowser.getControl().isDisposed()) { List<INews> news = feed.getNewsByStates(INews.State.getVisible()); int newsCount = news.size(); if (news.size() > MAX_NEWS_SHOWN) news = news.subList(0, MAX_NEWS_SHOWN); /* Start HTML */ StringBuilder html = new StringBuilder(); html.append( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"); //$NON-NLS-1$ /* Windows only: Mark of the Web */ if (Application.IS_WINDOWS) { html.append(IE_MOTW); html.append("\n"); // $NON-NLS-1$ } /* Head */ html.append("<html>\n <head>\n"); // $NON-NLS-1$ /* Append Base URI if available */ URI base = (feed.getBase() != null) ? feed.getBase() : feed.getLink(); if (base != null) { html.append(" <base href=\""); // $NON-NLS-1$ html.append(base); html.append("\">"); // $NON-NLS-1$ } /* Meta */ html.append( "\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); //$NON-NLS-1$ /* CSS */ try { StringWriter writer = new StringWriter(); fLabelProvider.writeCSS(writer, false); html.append(writer.toString()); } catch (IOException e) { /* Will Never Happen */ } /* Open Body */ html.append(" </head>\n <body id=\"owlbody\">\n"); // $NON-NLS-1$ /* Title */ if (StringUtils.isSet(fBookmark.getName())) html.append("<div class=\"group\">") .append(fBookmark.getName()) .append("</div>"); // $NON-NLS-1$ //$NON-NLS-2$ /* Write News */ for (INews item : news) { html.append(fLabelProvider.getText(item, false)); } /* End HTML */ html.append("\n </body>\n</html>"); // $NON-NLS-1$ /* Apply to Browser */ fBrowser.getControl().setText(html.toString()); /* Also Update Status */ if (StringUtils.isSet(fBookmark.getName())) { StringBuilder str = new StringBuilder(); if (feed.getHomepage() != null) { str.append( NLS.bind( Messages.PreviewFeedDialog_FOUND_N_NEWS_HOMEPAGE, newsCount, fBookmark.getName())); fStatusLabel.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { new OpenInBrowserAction(new StructuredSelection(feed.getHomepage())).run(); } }); } else str.append( NLS.bind(Messages.PreviewFeedDialog_FOUND_N_NEWS, newsCount, fBookmark.getName())); fStatusLabel.setText(str.toString()); } } }
private void loadFeed() { /* Show Info that Feed is loading */ if (fLoadedFeed == null || fLoadedFeed.getVisibleNews().isEmpty()) { if (StringUtils.isSet(fBookmark.getName())) showMessage( NLS.bind(Messages.PreviewFeedDialog_LOAD_FEED_N, fBookmark.getName()), false, true); else showMessage(Messages.PreviewFeedDialog_LOAD_FEED, false, true); } /* Load Feed in Background */ JobRunner.runUIUpdater( new UIBackgroundJob(fBrowser.getControl()) { private IFeed feed; private Exception error; @Override protected void runInBackground(IProgressMonitor monitor) { /* First Check if a Feed was already provided */ if (fLoadedFeed != null && !fLoadedFeed.getVisibleNews().isEmpty()) { feed = fLoadedFeed; return; } /* Otherwise Load Feed */ try { /* Resolve Feed if existing */ if (fFeedReference != null) feed = fFeedReference.resolve(); /* Create Temporary Feed */ if (feed == null || feed.getVisibleNews().isEmpty()) { feed = Owl.getModelFactory() .createFeed(null, fBookmark.getFeedLinkReference().getLink()); /* Return if dialog closed */ if (monitor.isCanceled() || getShell().isDisposed() || fBrowser.getControl().isDisposed()) return; /* Retrieve Stream */ IProtocolHandler handler = Owl.getConnectionService().getHandler(feed.getLink()); InputStream inS = handler.openStream(feed.getLink(), monitor, null); /* Return if dialog closed */ if (monitor.isCanceled() || getShell().isDisposed() || fBrowser.getControl().isDisposed()) return; /* Interpret Feed */ Owl.getInterpreter().interpret(inS, feed, null); } } catch (ConnectionException e) { error = e; Activator.safeLogError(e.getMessage(), e); } catch (ParserException e) { error = e; Activator.safeLogError(e.getMessage(), e); } catch (InterpreterException e) { error = e; Activator.safeLogError(e.getMessage(), e); } } @Override protected void runInUI(IProgressMonitor monitor) { if (feed != null && error == null) showFeed(feed); else if (error != null) { String errorMessage = CoreUtils.toMessage(error); if (StringUtils.isSet(errorMessage)) showMessage( NLS.bind(Messages.PreviewFeedDialog_UNABLE_LOAD_FEED, errorMessage), true, false); } } }); }