summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/actions/AbstractActionDelegate.java
blob: 7ba1f6b51eb87991965f8b145bff36cf42b9da32 (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
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Console is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.console.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.internal.UIPlugin;
import org.gluster.storage.management.console.utils.GlusterLogger;
import org.gluster.storage.management.core.model.Entity;


/**
 * All action delegates in the application should extend from this class. It provides common functionality of grabbing
 * the Window object on initialization and extracting the selected entity in case of selection change on the navigation
 * tree.
 */
@SuppressWarnings("restriction")
public abstract class AbstractActionDelegate implements IWorkbenchWindowActionDelegate {
	protected IWorkbenchWindow window;
	protected static final GlusterLogger logger = GlusterLogger.getInstance();
	
	// the latest selected entity
	protected Entity selectedEntity;

	@Override
	public void run(final IAction action) {
		try {
			performAction(action);
		} catch (final Exception e) {
			final String actionDesc = action.getDescription();
			logger.error("Exception while running action [" + actionDesc + "]",  e);
			showErrorDialog(actionDesc, e.getMessage());
		}
	}

	abstract protected void performAction(final IAction action);

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof StructuredSelection) {
			Entity selectedEntity = (Entity) ((StructuredSelection) selection).getFirstElement();

			if (this.selectedEntity == selectedEntity) {
				// entity selection has not changed. do nothing.
				return;
			}

			if (selectedEntity != null) {
				this.selectedEntity = selectedEntity;
			}
		}
	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
	
	protected Shell getShell() {
		return getWindow().getShell();
	}
	
	protected IWorkbenchWindow getWindow() {
		return window == null ? UIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow() : window;
	}

	protected void showInfoDialog(final String title, final String message) {
		MessageDialog.openInformation(getShell(), title, message);
	}

	protected void showWarningDialog(final String title, final String message) {
		MessageDialog.openWarning(getShell(), title, message);
	}

	protected void showErrorDialog(final String title, final String message) {
		MessageDialog.openError(getShell(), title, message);
	}

	protected boolean showConfirmDialog(final String title, final String message) {
		return MessageDialog.openQuestion(getShell(), title, message);
	}
}