Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ee12114c26782ccf37fb2eac9e38a0101477a41d (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
 * Copyright (c) 2008 Hallvard Traetteberg.
 * 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:
 *     Hallvard Traetteberg - initial API and implementation
 */
package org.eclipse.emf.ecore.xcore.ui;


import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.TextActionHandler;
import org.eclipse.ui.part.ViewPart;


public abstract class AbstractSelectionView extends ViewPart
{

  protected IEditingDomainProvider editingDomainProvider;

  protected ISelectionProvider selectionProvider;

  @Override
  public void init(IViewSite site) throws PartInitException
  {
    super.init(site);
    getSite().getWorkbenchWindow().getPartService().addPartListener(partListener);
  }

  @Override
  public void dispose()
  {
    getSite().getWorkbenchWindow().getPartService().removePartListener(partListener);
    setEditingDomainProvider(null);
    super.dispose();
  }

  private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener()
    {
      public void selectionChanged(SelectionChangedEvent event)
      {
        AbstractSelectionView.this.selectionChanged(event.getSelection());
      }
    };

  protected void setSelectionProvider(ISelectionProvider selectionProvider)
  {
    if (this.selectionProvider == selectionProvider)
    {
      return;
    }
    if (this.selectionProvider != null)
    {
      this.selectionProvider.removeSelectionChangedListener(selectionChangedListener);
    }
    this.selectionProvider = selectionProvider;
    if (this.selectionProvider != null)
    {
      this.selectionProvider.addSelectionChangedListener(selectionChangedListener);
    }
    selectionChanged(this.selectionProvider != null ? this.selectionProvider.getSelection() : StructuredSelection.EMPTY);
  }

  protected void setEditingDomainProvider(IEditingDomainProvider editingDomainProvider)
  {
    if (this.editingDomainProvider == editingDomainProvider)
    {
      return;
    }
    this.editingDomainProvider = editingDomainProvider;
    updateView();
  }

  protected void updateView()
  {
    // Ignore
  }

  @SuppressWarnings("rawtypes")
  @Override
  public Object getAdapter(Class type)
  {
    Object o = super.getAdapter(type);
    if (o == null && editingDomainProvider instanceof IAdaptable)
    {
      o = ((IAdaptable)editingDomainProvider).getAdapter(type);
    }
    return o;
  }

  private void updateProviders(IWorkbenchPart part)
  {
    setSelectionProvider(getAdapter(part, ISelectionProvider.class));
    setEditingDomainProvider(getAdapter(part, IEditingDomainProvider.class));
  }

  @SuppressWarnings("unchecked")
  protected <T> T getAdapter(IWorkbenchPart part, Class<T> c)
  {
    IWorkbenchPage workbenchPage = getViewSite().getWorkbenchWindow().getActivePage();
    if (workbenchPage == null)
    {
      return null;
    }
    IEditorPart editorPart = workbenchPage.getActiveEditor();
    return (T)(editorPart != null ? editorPart.getAdapter(c) : null);
  }

  private IPartListener partListener = new IPartListener()
    {
      public void partActivated(IWorkbenchPart part)
      {
        updateProviders(part);
      }

      public void partBroughtToTop(IWorkbenchPart part)
      {
        updateProviders(part);
      }

      public void partClosed(IWorkbenchPart part)
      {
        updateProviders(part);
      }

      public void partDeactivated(IWorkbenchPart part)
      {
        updateProviders(part);
      }

      public void partOpened(IWorkbenchPart part)
      {
        updateProviders(part);
      }
    };

  protected Object selection = null;

  public void selectionChanged(SelectionChangedEvent event)
  {
    selectionChanged(event.getSelection());
  }

  protected void selectionChanged(ISelection selection)
  {
    Object oldSelection = this.selection;
    this.selection = null;
    if (!selection.isEmpty() && selection instanceof IStructuredSelection)
    {
      Object o = ((IStructuredSelection)selection).getFirstElement();
      if (isValidSelection(o))
      {
        this.selection = o;
      }
    }
    if (this.selection != oldSelection)
    {
      updateView();
    }
  }

  protected boolean isValidSelection(Object o)
  {
    return true;
  }

  private TextActionHandler textActionHandler;

  void registerTextControl(final Text textControl)
  {
    textControl.addFocusListener(new FocusListener()
      {
        public void focusGained(FocusEvent e)
        {
          textActionHandler.addText(textControl);
        }

        public void focusLost(FocusEvent e)
        {
          textActionHandler.removeText(textControl);
        }
      });
  }

  @Override
  public void createPartControl(Composite parent)
  {
    textActionHandler = new TextActionHandler(getViewSite().getActionBars());
    createActions();
    createMenu();
    createToolbar();
  }

  protected Text createTextControl(Composite parent, int style)
  {
    Text textControl = new Text(parent, style);
    textActionHandler.addText(textControl);
    return textControl;
  }

  protected void disposeTextControl(Text textControl)
  {
    if (!textControl.isDisposed())
    {
      textActionHandler.removeText(textControl);
    }
  }

  protected void createActions()
  {
    // Do nothing.
  }

  private void createMenu()
  {
    createMenu(getViewSite().getActionBars().getMenuManager());
  }

  protected void createMenu(IMenuManager mgr)
  {
    // Do nothing.
  }

  private void createToolbar()
  {
    createToolbar(getViewSite().getActionBars().getToolBarManager());
  }

  protected void createToolbar(IToolBarManager mgr)
  {
    // Do nothing.
  }
}

Back to the top