summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/utils/binding/editors/controls/AbstractControlActionsManager.java
blob: 155b55c12ab4aaf6d69736edff09852f9022db5b (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * 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:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.utils.binding.editors.controls;

import com.google.common.collect.Lists;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionInfo;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.ISources;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerActivation;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;

import java.util.List;

/**
 * Manager for installing/unistalling global handlers for {@link Control} actions commands.
 *
 * @author sablin_aa
 * @author mitin_aa
 */
public abstract class AbstractControlActionsManager {
  @SuppressWarnings("deprecation")
  protected final Object[] COMMAND_HANDLER_IDS = new Object[]{
      IWorkbenchActionDefinitionIds.COPY,
      IWorkbenchActionDefinitionIds.CUT,
      IWorkbenchActionDefinitionIds.PASTE,
      IWorkbenchActionDefinitionIds.DELETE,
      IWorkbenchActionDefinitionIds.SELECT_ALL,
      IWorkbenchActionDefinitionIds.UNDO,
      IWorkbenchActionDefinitionIds.REDO};
  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  protected final Control m_control;
  private boolean m_active = false;

  public AbstractControlActionsManager(final Control control) {
    m_control = control;
    m_control.addFocusListener(new FocusListener() {
      @Override
    public void focusGained(FocusEvent e) {
        activateHandlers();
        m_active = true;
      }

      @Override
    public void focusLost(FocusEvent e) {
        deactivateHandlers();
        m_active = false;
      }
    });
    m_control.addDisposeListener(new DisposeListener() {
      @Override
    public void widgetDisposed(DisposeEvent e) {
        if (m_active) {
          // deactivate on dispose
          deactivateHandlers();
        }
      }
    });
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Handlers
  //
  ////////////////////////////////////////////////////////////////////////////
  protected static final IHandler EMPTY_HANDLER = new AbstractHandler() {
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
      // do nothing
      return null;
    }

    @Override
    public boolean isEnabled() {
      // of course, it is enabled ;)
      return true;
    }

    @Override
    public boolean isHandled() {
      // we do not handle the actions; since action not handled, its' underlying SWT event
      // would not be filtered by workbench, so it get a chance to be handled by SWT code
      // or to be passed to the OS; see WorkbenchKeyboard.press() method.
      return false;
    }
  };

  /**
   * @returns the global handler service.
   */
  public static IHandlerService getHandlerService() {
    return (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
  }

  /**
   * Activates the handlers for list of commands (see COMMAND_HANDLERS) with:<br>
   * 1. The empty handler (except 'selectAll'), so underlying SWT event wouldn't be filtered by the
   * workbench;<br>
   * 2. Highest priority {@link Expression}, so this handler has a chance to be set.
   */
  protected void activateHandlers() {
    IHandlerService service = getHandlerService();
    for (int i = 0; i < COMMAND_HANDLER_IDS.length; ++i) {
      String actionName = (String) COMMAND_HANDLER_IDS[i];
      IHandler handler = getHandlerFor(actionName);
      activateHandler(actionName, service, handler, new Expression() {
        @Override
        public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
          return EvaluationResult.TRUE;
        }

        @Override
        public void collectExpressionInfo(ExpressionInfo info) {
          // get the highest priority
          // note, if someone else has such priority, there will be key-binding conflicts logged.
          info.markSystemPropertyAccessed();
          info.markDefaultVariableAccessed();
          info.addVariableNameAccess(ISources.ACTIVE_MENU_NAME);
        }
      });
    }
  }

  protected IHandler getHandlerFor(String actionName) {
    return EMPTY_HANDLER;
  }

  /**
   * Activates handler and stores it into a collection for further deactivation.
   */
  private final List<IHandlerActivation> m_activations = Lists.newLinkedList();

  private void activateHandler(String actionName,
      IHandlerService service,
      IHandler handler,
      Expression highPriorityExpression) {
    // activate handler and store it into a collection for further deactivation
    m_activations.add(service.activateHandler(actionName, handler, highPriorityExpression));
  }

  /**
   * Deactivates all handlers and clears handlers collection.
   */
  protected void deactivateHandlers() {
    getHandlerService().deactivateHandlers(m_activations);
    m_activations.clear();
  }
}