Trim code
[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.datasource.CdmDataSourceRepository;
20 import eu.etaxonomy.taxeditor.model.MessagingUtils;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22 import eu.etaxonomy.taxeditor.view.datasource.CdmMetaDataAwareDataSourceContainer;
23
24 /**
25 * @author n.hoffmann
26 * @created 15.04.2009
27 * @version 1.0
28 */
29 public class ChangeConnectionHandler extends AbstractHandler {
30
31 /** {@inheritDoc} */
32 @Override
33 public Object execute(ExecutionEvent event) {
34 ISelection selection = HandlerUtil.getCurrentSelection(event);
35
36 if(selection != null && selection instanceof IStructuredSelection){
37 CdmMetaDataAwareDataSourceContainer container = (CdmMetaDataAwareDataSourceContainer) ((IStructuredSelection) selection).getFirstElement();
38
39 if(CdmStore.isConnecting()){
40 MessagingUtils.warningDialog("Already connecting", this, "You are currently connecting to a different datasource already.");
41 return null;
42 }
43
44 try {
45 container.getCdmSource().checkConnection();
46 boolean confirmed = MessagingUtils.confirmDialog("Confirm Datasource Connection", "Do you really want to connect to this datasource?\n\n"
47 + container.getCdmSource().getName());
48
49 if(confirmed){
50 CdmDataSourceRepository.changeDataSource(container.getCdmSource());
51 }
52 } catch (Exception e) {
53 MessagingUtils.warningDialog("Chosen datasource is not available", this, "This could mean that either the database server is " +
54 "not running or the machine is not reachable.\n\n" +
55 "Please also make sure that you are connected to the network when trying to connect to a remote datasource.");
56 MessagingUtils.warn(getClass(), e);
57 return null;
58 }
59 }
60
61 return null;
62 }
63 }