package org.eclipse.twipse.views;
import java.util.Collection;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.part.*;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.twipse.TwipsePlugin;
import org.eclipse.twipse.core.ITwitterClient;
import org.eclipse.twipse.core.TwitterClient;
import org.eclipse.twipse.model.Status;
import org.eclipse.twipse.model.StatusList;
/**
*
*/
public class UpdatesView extends ViewPart implements Observer{
private TableViewer viewer;
private Action action1; // @jve:decl-index=0:
private Action action2; // @jve:decl-index=0:
private Action doubleClickAction; // @jve:decl-index=0:
private Collection<Status> statusList; // @jve:decl-index=0:
private Combo textStatus;
private ISelectionListener mylynListener;
/*
* The content provider class is responsible for
* providing objects to the view. It can wrap
* existing objects in adapters or simply return
* objects as-is. These objects may be sensitive
* to the current input of the view, or ignore
* it and always show the same content
* (like Task List, for example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if(statusList==null)
statusList=TwipsePlugin.getDefault().getStatusList().getStatuses();
return statusList.toArray();
}
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object obj, int index) {
Status s = (Status)obj;
/*switch(index){
case 0:
return s.getUser().getScreen_name();
case 1:
return s.getText();
case 2:
return s.getCreated_at();
default:
return "";
}*/
return s.toString();
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().
getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
}
}
public UpdatesView() {
}
/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
public void createPartControl(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
TwipsePlugin.getDefault().getStatusList().addObserver(this);
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
hookMylynTaskView();
}
private void hookMylynTaskView() {
mylynListener = new ISelectionListener(){
public void selectionChanged(IWorkbenchPart part, ISelection sel) {
if (!(sel instanceof IStructuredSelection))
return;
IStructuredSelection ss = (IStructuredSelection) sel;
Object o = ss.getFirstElement();
if (o instanceof AbstractTask){
Display.getDefault().asyncExec(new Thread(new Runnable(){
public void run() {
AbstractTask t = TasksUiPlugin.getTaskListManager().
getTaskList().getActiveTask();
if(t!=null){
TwipsePlugin.getDefault().
getClient().postStatusUpdate(
new Status(t.getSummary()+" - "+t.getUrl()));
}
}
}));
}
}
};
getSite().getPage().addSelectionListener(mylynListener);
}
private void hookContextMenu() {
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
UpdatesView.this.fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr, viewer);
}
private void contributeToActionBars() {
IActionBars bars = getViewSite().getActionBars();
fillLocalPullDown(bars.getMenuManager());
fillLocalToolBar(bars.getToolBarManager());
}
private void fillLocalPullDown(IMenuManager manager) {
manager.add(action1);
manager.add(new Separator());
manager.add(action2);
}
private void fillContextMenu(IMenuManager manager) {
manager.add(action1);
//manager.add(action2);
// Other plug-ins can contribute there actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
private void fillLocalToolBar(IToolBarManager manager) {
//manager.add(new UpdateStatusContributionItem("id"));
manager.add(action1);
//manager.add(action2);
}
private void makeActions() {
action1 = new Action() {
public void run() {
InputDialog d = new InputDialog(
Display.getDefault().getActiveShell(),
"Update your Twitterjjjj Status",
"What are you doing?",
"",
new IInputValidator(){
public String isValid(String arg0) {
if(arg0!=null&&!arg0.isEmpty())return null;
return "";
}
}
);
if(d.open()==Window.OK){
TwipsePlugin.getDefault().getClient().postStatusUpdate(
new Status(d.getValue()));
}
}
};
action1.setText("Update your Twitter status");
action1.setToolTipText("Update your Twitter status");
action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
action2 = new Action() {
public void run() {
showMessage("Action 2 executed");
}
};
action2.setText("Action 2");
action2.setToolTipText("Action 2 tooltip");
action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
doubleClickAction = new Action() {
public void run() {
ISelection selection = viewer.getSelection();
Object obj = ((IStructuredSelection)selection).getFirstElement();
showMessage("Double-click detected on "+obj.toString());
}
};
}
private void hookDoubleClickAction() {
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
doubleClickAction.run();
}
});
}
private void showMessage(String message) {
MessageDialog.openInformation(
viewer.getControl().getShell(),
"TwipsePlugin",
message);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
public void update(Observable o, Object arg) {
System.out.println("update");
Display.getDefault().syncExec(new Thread(new Runnable(){
public void run(){
statusList = TwipsePlugin.getDefault().getStatusList().getStatuses();
viewer.refresh();
}
}));
}
public void dispose() {
TwipsePlugin.getDefault().getStatusList().deleteObserver(this);
getSite().getPage().removeSelectionListener(mylynListener);
}
}