Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 684cb60f821d197243390fb172c45399757eecc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
 * Copyright (c) 2004 - 2010 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.ui.views;

import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.session.remote.CDORemoteSession;
import org.eclipse.emf.cdo.session.remote.CDORemoteSessionManager;
import org.eclipse.emf.cdo.session.remote.CDORemoteSessionMessage;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.views.ContainerView;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;

/**
 * @author Eike Stepper
 */
public class CDORemoteSessionsView extends ContainerView.Default<CDORemoteSessionManager>
{
  private static final String TYPE_TEXT_MESSAGE = "org.eclipse.emf.cdo.ui.TextMessage";

  private ISelectionListener selectionListener = new ISelectionListener()
  {
    public void selectionChanged(IWorkbenchPart part, ISelection selection)
    {
      if (part != CDORemoteSessionsView.this)
      {
        Object object = UIUtil.getElementIfOne(selection);
        CDOSession session = CDOUtil.getSession(object);
        if (session != null)
        {
          setContainer(session.getRemoteSessionManager());
        }
      }
    }
  };

  private IListener containerListener = new CDORemoteSessionManager.EventAdapter()
  {
    @Override
    protected void onLocalSubscriptionChanged(boolean subscribed)
    {
      getViewer().getControl().setEnabled(subscribed);
    }

    @Override
    protected void onSubscribed(CDORemoteSession remoteSession)
    {
      refreshElement(remoteSession, true);
    }

    @Override
    protected void onUnsubscribed(CDORemoteSession remoteSession)
    {
      refreshElement(remoteSession, true);
    }

    @Override
    protected void onMessageReceived(final CDORemoteSession remoteSession, final CDORemoteSessionMessage message)
    {
      if (TYPE_TEXT_MESSAGE.equals(message.getType()))
      {
        try
        {
          getDisplay().asyncExec(new Runnable()
          {
            public void run()
            {
              try
              {
                MessageDialog.openInformation(getShell(), "Message from " + remoteSession,
                    new String(message.getData()));
              }
              catch (RuntimeException ignore)
              {
              }
            }
          });
        }
        catch (RuntimeException ignore)
        {
        }
      }
    }
  };

  public CDORemoteSessionsView()
  {
  }

  @Override
  public void dispose()
  {
    getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(selectionListener);
    super.dispose();
  }

  @Override
  protected Control createUI(Composite parent)
  {
    Control control = super.createUI(parent);
    getViewer().getControl().setEnabled(false);
    getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(selectionListener);
    return control;
  }

  @Override
  protected IListener getContainerListener()
  {
    return containerListener;
  }

  @Override
  protected Color getElementForeground(Object element)
  {
    if (element instanceof CDORemoteSession)
    {
      CDORemoteSession remoteSession = (CDORemoteSession)element;
      if (!remoteSession.isSubscribed())
      {
        return getDisplay().getSystemColor(SWT.COLOR_GRAY);
      }
    }

    return null;
  }

  @Override
  protected void doubleClicked(Object element)
  {
    if (element instanceof CDORemoteSession)
    {
      CDORemoteSession remoteSession = (CDORemoteSession)element;
      if (remoteSession.isSubscribed())
      {
        InputDialog dlg = new InputDialog(getShell(), "Message to " + remoteSession, "Message:", "", null);
        if (dlg.open() == InputDialog.OK)
        {
          String message = dlg.getValue();
          remoteSession.sendMessage(new CDORemoteSessionMessage(TYPE_TEXT_MESSAGE, message.getBytes()));
        }

        return;
      }
    }

    super.doubleClicked(element);
  }
}

Back to the top