]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/DoubleClickExtension.java
Combination of Simantics-platform related changes and fixes for district
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / DoubleClickExtension.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.utils.ui.ExceptionUtils;
18
19 public class DoubleClickExtension implements IDoubleClickExtension {
20
21     /**
22      * A fallback action for replacing {@link #cachedAction} if action
23      * instantiation fails.
24      */
25     private static final IDoubleClickAction NO_ACTION = new IDoubleClickAction() {
26         @Override
27         public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
28         }
29     };
30
31     private final IConfigurationElement config;
32
33     private final String                name;
34
35     private final double                priority;
36
37     private IDoubleClickAction          cachedAction;
38
39     DoubleClickExtension(IConfigurationElement config, String name, double priority) {
40         this.config = config;
41         this.name = name;
42         this.priority = priority;
43     }
44
45     @Override
46     public String getName() {
47         return name;
48     }
49
50     @Override
51     public double getPriority() {
52         return priority;
53     }
54
55     @Override
56     public synchronized IDoubleClickAction getAction() {
57         if (cachedAction == null) {
58             try {
59                 cachedAction = (IDoubleClickAction) config.createExecutableExtension("class");
60             } catch (CoreException e) {
61                 ExceptionUtils.logError("Failed to instantiate " + config.getName()
62                         + " extension with name \"" + config.getAttribute("name") + "\": "
63                         + e.getMessage(), e);
64                 // This prevents informing of an error in the extension.
65                 cachedAction = NO_ACTION;
66             }
67         }
68         return cachedAction;
69     }
70
71     @Override
72     public String toString() {
73         return "DoubleClickExtension [name=" + name + ", class=" + config.getAttribute("class") + ", priority=" + priority + "]";
74     }
75
76 }