Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2808bff00d8af7fddae5dc1cfdf7aab954fd7227 (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
/*
 * Copyright (c) 2013 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.releng.projectconfig.presentation.handlers;

import org.eclipse.emf.cdo.releng.preferences.PreferenceNode;
import org.eclipse.emf.cdo.releng.preferences.util.PreferencesUtil;
import org.eclipse.emf.cdo.releng.projectconfig.PreferenceFilter;
import org.eclipse.emf.cdo.releng.projectconfig.PreferenceProfile;
import org.eclipse.emf.cdo.releng.projectconfig.Project;
import org.eclipse.emf.cdo.releng.projectconfig.WorkspaceConfiguration;

import org.eclipse.emf.common.ui.viewer.IViewerProvider;
import org.eclipse.emf.edit.provider.IWrapperItemProvider;
import org.eclipse.emf.edit.provider.ItemProvider;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.ISources;

import java.util.ArrayList;
import java.util.List;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class NavigateHandler extends AbstractHandler
{
  protected IStructuredSelection targetSelection;

  protected Viewer viewer;

  public NavigateHandler()
  {
  }

  /**
   * the command has been executed, so extract extract the needed information
   * from the application context.
   */
  public Object execute(ExecutionEvent event) throws ExecutionException
  {
    if (viewer != null)
    {
      viewer.setSelection(targetSelection, true);
    }
    return null;
  }

  @Override
  public void setEnabled(Object evaluationContext)
  {
    IEvaluationContext evaluationContext2 = (IEvaluationContext)evaluationContext;
    Object activeEditorPart = evaluationContext2.getVariable(ISources.ACTIVE_EDITOR_NAME);
    if (activeEditorPart instanceof IViewerProvider)
    {
      viewer = ((IViewerProvider)activeEditorPart).getViewer();
    }
    Object selection = evaluationContext2.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
    if (selection instanceof IStructuredSelection)
    {
      updateSelection((IStructuredSelection)selection);
    }

    setBaseEnabled(viewer != null && !targetSelection.isEmpty());
  }

  public boolean updateSelection(IStructuredSelection selection)
  {
    List<Object> targets = new ArrayList<Object>();
    for (Object object : selection.toArray())
    {
      if (object instanceof WorkspaceConfiguration)
      {
        WorkspaceConfiguration workspaceConfiguration = (WorkspaceConfiguration)object;
        for (Project project : workspaceConfiguration.getProjects())
        {
          targets.addAll(project.getPreferenceProfiles());
        }
      }
      else if (object instanceof Project)
      {
        Project project = (Project)object;
        targets.add(project.getPreferenceNode());
      }
      else if (object instanceof PreferenceProfile)
      {
        PreferenceProfile preferenceProfile = (PreferenceProfile)object;
        targets.addAll(preferenceProfile.getReferentProjects());
      }
      else if (object instanceof PreferenceFilter)
      {
        PreferenceFilter preferenceFilter = (PreferenceFilter)object;
        PreferenceNode preferenceNode = preferenceFilter.getPreferenceNode();
        if (preferenceNode != null)
        {
          targets.add(preferenceNode);
        }
      }
      else if (object instanceof IWrapperItemProvider)
      {
        IWrapperItemProvider wrapperItemProvider = (IWrapperItemProvider)object;
        targets.add(wrapperItemProvider.getValue());
      }
      else if (object instanceof ItemProvider)
      {
        ItemProvider itemProvider = (ItemProvider)object;
        for (Object child : itemProvider.getChildren())
        {
          if (child instanceof IWrapperItemProvider)
          {
            targets.add(((IWrapperItemProvider)child).getValue());
          }
        }
      }
      else if (object instanceof PreferenceNode)
      {
        PreferenceNode preferenceNode = (PreferenceNode)object;
        PreferenceNode ancestor = PreferencesUtil.getAncestor(preferenceNode);
        if (ancestor != null)
        {
          targets.add(ancestor);
        }
      }
    }
    targetSelection = new StructuredSelection(targets);
    return !targetSelection.isEmpty();
  }
}

Back to the top