2 * Created on 15 mai 2004
3 * Copyright 2004 Jérôme Nègre
5 package net.sourceforge.phpeclipse.news.view;
7 import java.util.ArrayList;
9 import net.sourceforge.phpeclipse.news.Channel;
10 import net.sourceforge.phpeclipse.news.IconManager;
11 import net.sourceforge.phpeclipse.news.Item;
12 import net.sourceforge.phpeclipse.news.Plugin;
13 import net.sourceforge.phpeclipse.news.RssListener;
14 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IToolBarManager;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.SelectionChangedEvent;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.jface.viewers.TreeViewer;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.IViewPart;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.part.ViewPart;
29 * @author Jérôme Nègre
31 public class ExplorerView extends ViewPart implements RssListener {
33 private TreeViewer treeViewer;
35 private NewsTreeViewerProvider provider;
37 private Action refreshAction;
39 public ExplorerView() {
43 public void dispose() {
44 Plugin.getDefault().removeRssListener(this);
48 public void createPartControl(Composite parent) {
49 treeViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
50 provider = new NewsTreeViewerProvider();
51 treeViewer.setContentProvider(provider);
52 treeViewer.setLabelProvider(provider);
53 treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
54 public void selectionChanged(SelectionChangedEvent event) {
55 Object selected = ((StructuredSelection) event.getSelection()).getFirstElement();
56 if (selected != null) {
57 if (selected instanceof Channel) {
58 Plugin.getDefault().notifyChannelSelected((Channel) selected, ExplorerView.this);
59 } else if (selected instanceof Item) {
60 Plugin.getDefault().notifyItemSelected((Item) selected, ExplorerView.this);
70 Plugin.getDefault().addRssListener(this);
71 treeViewer.setInput(Plugin.getDefault());
74 public void setFocus() {
75 treeViewer.getControl().setFocus();
78 public void onChannelListChanged(ArrayList channels) {
79 treeViewer.setInput(Plugin.getDefault());
82 public void onChannelStatusChanged(final Channel channel) {
83 treeViewer.getControl().getDisplay().asyncExec(new Runnable() {
85 treeViewer.refresh(channel);
90 public void onChannelSelected(Channel channel) {
91 System.out.println("Explorer.onChannelSelected -> " + channel);
94 public void onItemStatusChanged(final Item item) {
95 treeViewer.getControl().getDisplay().asyncExec(new Runnable() {
97 treeViewer.refresh(item);
102 private void createActions() {
104 refreshAction = new Action("Refresh", IconManager.getImageDescriptor(IconManager.ICON_ACTION_REFRESH)) {
106 Plugin.getDefault().update();
109 refreshAction.setToolTipText("Refresh");
112 private void createMenu() {
113 //IMenuManager mgr = getViewSite().getActionBars().getMenuManager();
114 //mgr.add(clearAction);
117 private void createToolBar() {
118 IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
119 mgr.add(refreshAction);
122 private void setUrl(String url) {
123 IWorkbenchPage page = Plugin.getDefault().getActivePage();
125 IViewPart part = page.findView(BrowserView.ID_BROWSER);
127 part = page.showView(BrowserView.ID_BROWSER);
129 page.bringToTop(part);
131 ((BrowserView) part).setUrl(url);
132 } catch (Exception e) {
136 // public void onItemSelected(Item tiem) {
139 public void onItemSelected(Item item) {
140 if (item != null) { // && uiReady && linkAction.isChecked()) {
141 // if(showDescritionAction.isChecked()) {
142 // String desc = item.getDescription();
144 // desc = HTML_NO_DESCRIPTION;
145 // browser.setText(MessageFormat.format(HTML,new String[]{desc, encodeNewLine(desc), item.getUsableLink(),
146 // item.getUsableTitle()}));
148 setUrl(item.getUsableLink());
150 //XXX this is a hack, should be done otherwise
151 boolean channelStatus = item.getChannel().isUnread();
152 item.setReadFlag(true);
153 Plugin.getDefault().notifyItemStatusChanged(item, this);
154 if (channelStatus != item.getChannel().isUnread()) {
155 Plugin.getDefault().notifyChannelStatusChanged(item.getChannel(), this);