Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e187919b30f9d1c5a1c7c0344ad1f8e774b7e00f (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
/*
 * Copyright (c) 2007, 2009, 2011, 2012 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
 *    Christian W. Damus (CEA LIST) - bug 399306 - adapted from SafeActionDelegate
 */
package org.eclipse.net4j.util.ui.handlers;

import org.eclipse.net4j.util.internal.ui.bundle.OM;
import org.eclipse.net4j.util.internal.ui.messages.Messages;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISources;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * @author Eike Stepper
 * @author Christian W. Damus (CEA LIST)
 * 
 * @since 3.4
 */
public abstract class SafeHandler extends AbstractHandler
{

  private Command command;

  private ISelection selection;

  public SafeHandler()
  {
  }

  public Command getCommand()
  {
    return command;
  }

  public ISelection getSelection()
  {
    return selection;
  }

  public Object execute(ExecutionEvent event) throws ExecutionException
  {
    Object result = null;

    try
    {
      extractEventDetails(event);

      result = safeExecute(event);
    }
    catch (ExecutionException ex)
    {
      throw ex;
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
      MessageDialog.openError(null, getText(),
          ex.getLocalizedMessage() + "\n" + Messages.getString("SafeActionDelegate_0")); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return result;
  }

  @Override
  public void setEnabled(Object evaluationContext)
  {
    Object variable = HandlerUtil.getVariable(evaluationContext, ISources.ACTIVE_CURRENT_SELECTION_NAME);
    selection = variable instanceof ISelection ? (ISelection)variable : StructuredSelection.EMPTY;
    setBaseEnabled(updateSelection(selection));
  }

  /**
   * Extracts details from the {@code event} that we may need later,
   * for example on a background thread when the original execution
   * context is no longer valid.
   */
  protected void extractEventDetails(ExecutionEvent event)
  {
    setEnabled(event.getApplicationContext());
    command = event.getCommand();
  }

  protected abstract Object safeExecute(ExecutionEvent event) throws Exception;

  protected String getText()
  {
    try
    {
      return command == null ? Messages.getString("SafeActionDelegate_1") : command.getName(); //$NON-NLS-1$
    }
    catch (NotDefinedException e)
    {
      return Messages.getString("SafeActionDelegate_1"); //$NON-NLS-1$
    }
  }

  protected boolean updateSelection(ISelection selection)
  {
    return true;
  }
}

Back to the top