Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 279d6d6f436cb2fc6cb6f756f278f31082031732 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package org.eclipse.emf.cdo.internal.ui.views;

import org.eclipse.emf.cdo.CDOConstants;
import org.eclipse.emf.cdo.container.CDOContainerAdapter;
import org.eclipse.emf.cdo.internal.ui.bundle.CDOUI;

import org.eclipse.net4j.container.Container;
import org.eclipse.net4j.container.ContainerManager;
import org.eclipse.net4j.transport.ConnectorException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

public class CDOSessionsView extends ViewPart
{
  private static final Container CONTAINER = ContainerManager.INSTANCE.getContainer();

  private static final CDOContainerAdapter CDO_ADAPTER = (CDOContainerAdapter)CONTAINER.getAdapter(CDOConstants.TYPE);

  private static final ItemProvider ITEM_PROVIDER = new CDOSessionsItemProvider();

  private StructuredViewer viewer;

  // private DrillDownAdapter drillDownAdapter;

  private Action doubleClickAction = new DoubleClickAction();

  private Action openSessionAction = new OpenSessionAction();

  private Action addConnectorAction;

  public CDOSessionsView()
  {
  }

  public void createPartControl(Composite parent)
  {
    viewer = new TreeViewer(parent, (SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL));
    viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));

    // drillDownAdapter = new DrillDownAdapter(viewer);
    viewer.setContentProvider(ITEM_PROVIDER);
    viewer.setLabelProvider(ITEM_PROVIDER);
    viewer.setSorter(new CDOSessionsNameSorter());
    viewer.setInput(CDO_ADAPTER);

    hookContextMenu();
    hookDoubleClickAction();
    contributeToActionBars();
  }

  private void hookContextMenu()
  {
    MenuManager menuMgr = new MenuManager("#PopupMenu");
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.addMenuListener(new IMenuListener()
    {
      public void menuAboutToShow(IMenuManager manager)
      {
        CDOSessionsView.this.fillContextMenu(manager);
      }
    });

    Menu menu = menuMgr.createContextMenu(getCurrentViewer().getControl());
    getCurrentViewer().getControl().setMenu(menu);
    getSite().registerContextMenu(menuMgr, getCurrentViewer());
  }

  private StructuredViewer getCurrentViewer()
  {
    return viewer;
  }

  private void contributeToActionBars()
  {
    IActionBars bars = getViewSite().getActionBars();
    fillLocalPullDown(bars.getMenuManager());
    fillLocalToolBar(bars.getToolBarManager());
  }

  private void fillLocalPullDown(IMenuManager manager)
  {
    addContribution(manager, openSessionAction);
    addContribution(manager, addConnectorAction);
    // manager.add(new Separator());
    // manager.add(action2);
  }

  private void fillContextMenu(IMenuManager manager)
  {
    // manager.add(action2);
    // manager.add(new Separator());
    // drillDownAdapter.addNavigationActions(manager);

    // Other plug-ins can contribute there actions here
    manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
  }

  private void fillLocalToolBar(IToolBarManager manager)
  {
    addContribution(manager, openSessionAction);
    addContribution(manager, addConnectorAction);
    // manager.add(new Separator());
    // drillDownAdapter.addNavigationActions(manager);
  }

  protected void addContribution(IContributionManager manager, IContributionItem item)
  {
    if (manager != null && item != null)
    {
      manager.add(item);
    }
  }

  protected void addContribution(IContributionManager manager, IAction action)
  {
    if (manager != null && action != null)
    {
      manager.add(action);
    }
  }

  private void hookDoubleClickAction()
  {
    getCurrentViewer().addDoubleClickListener(new IDoubleClickListener()
    {
      public void doubleClick(DoubleClickEvent event)
      {
        doubleClickAction.run();
      }
    });
  }

  protected void showMessage(String message)
  {
    MessageDialog.openInformation(getCurrentViewer().getControl().getShell(), "Net4j Explorer", message);
  }

  public void setFocus()
  {
    getCurrentViewer().getControl().setFocus();
  }

  /**
   * @author Eike Stepper
   */
  private final class DoubleClickAction extends Action
  {
    public void run()
    {
      ISelection selection = getCurrentViewer().getSelection();
      Object obj = ((IStructuredSelection)selection).getFirstElement();
      showMessage("Double-click detected on " + obj.toString());
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class OpenSessionAction extends Action
  {
    public OpenSessionAction()
    {
      setText("Open Session");
      setToolTipText("Open a CDO session");
      setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
          ISharedImages.IMG_TOOL_NEW_WIZARD));
    }

    public void run()
    {
      InputDialog dialog = new InputDialog(getCurrentViewer().getControl().getShell(), "CDO Sessions",
          "Enter a session description:", null, null);
      if (dialog.open() == InputDialog.OK)
      {
        String description = dialog.getValue();

        try
        {
          CDO_ADAPTER.getSession(description);
        }
        catch (ConnectorException ex)
        {
          CDOUI.LOG.error(ex);
          showMessage("Error while creating session for description " + description);
        }
      }
    }
  }
}

Back to the top