Merge branch 'release/4.0.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / datasource / handler / ChangeConnectionHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.view.datasource.handler;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.ui.handlers.HandlerUtil;
18
19 import eu.etaxonomy.taxeditor.Messages;
20 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
21 import eu.etaxonomy.taxeditor.model.MessagingUtils;
22 import eu.etaxonomy.taxeditor.store.CdmStore;
23 import eu.etaxonomy.taxeditor.view.datasource.CdmMetaDataAwareDataSourceContainer;
24
25 /**
26 * @author n.hoffmann
27 * @created 15.04.2009
28 * @version 1.0
29 */
30 public class ChangeConnectionHandler extends AbstractHandler {
31
32 /** {@inheritDoc} */
33 @Override
34 public Object execute(ExecutionEvent event) {
35 ISelection selection = HandlerUtil.getCurrentSelection(event);
36
37 if(selection != null && selection instanceof IStructuredSelection){
38 CdmMetaDataAwareDataSourceContainer container = (CdmMetaDataAwareDataSourceContainer) ((IStructuredSelection) selection).getFirstElement();
39
40 if(CdmStore.isConnecting()){
41 MessagingUtils.warningDialog(Messages.ChangeConnectionHandler_ALREADY_CONNECTING, this, Messages.ChangeConnectionHandler_CURRENTLY_CONNECTING_ALREADY);
42 return null;
43 }
44
45 try {
46 container.getCdmSource().checkConnection();
47 boolean confirmed = MessagingUtils.confirmDialog(Messages.ChangeConnectionHandler_CONFIRM_DATASOURCE, Messages.ChangeConnectionHandler_REALLY_WANT_TO_CONNECT
48 + container.getCdmSource().getName());
49
50 if(confirmed){
51 CdmDataSourceRepository.changeDataSource(container.getCdmSource());
52 }
53 } catch (Exception e) {
54 MessagingUtils.warningDialog(Messages.ChangeConnectionHandler_DATASOURCE_NOT_AVAILABLE, this, Messages.ChangeConnectionHandler_NOT_AVAILABLE_REASONS);
55 MessagingUtils.warn(getClass(), e);
56 return null;
57 }
58 }
59
60 return null;
61 }
62 }