]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/action/ResourceEditorAdapterAction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / action / ResourceEditorAdapterAction.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.workbench.action;
13
14 import org.eclipse.core.runtime.SafeRunner;
15 import org.eclipse.jface.util.SafeRunnable;
16 import org.simantics.db.Resource;
17 import org.simantics.ui.workbench.editor.EditorAdapter;
18 import org.simantics.utils.ui.action.IPriorityAction;
19 import org.simantics.utils.ui.action.PriorityAction;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public class ResourceEditorAdapterAction extends PriorityAction {
25
26     private static final boolean DEBUG = false;
27
28     protected EditorAdapter      adapter;
29
30     protected Object             resource;
31
32     public ResourceEditorAdapterAction(EditorAdapter adapter, Object r) {
33         super(makeName(adapter), adapter.getIcon());
34         this.adapter = adapter;
35         this.resource = r;
36     }
37
38     public static String makeName(EditorAdapter adapter) {
39         if (DEBUG) {
40             return adapter.getName() + " (" + adapter.getClass().getName() + ")[" + adapter.getPriority() + "]";
41         }
42         return adapter.getName();
43     }
44
45     @Override
46     public final void run() {
47         SafeRunner.run(new SafeRunnable() {
48             @Override
49             public void run() throws Exception {
50                 safeRun();
51             }
52         });
53     }
54
55     protected void safeRun() throws Exception {
56         adapter.openEditor(resource);
57     }
58
59     public EditorAdapter getAdapter() {
60         return adapter;
61     }
62
63     public Object getResource() {
64         return resource;
65     }
66
67     @Override
68     public int getPriority() {
69         return adapter.getPriority();
70     }
71
72     public static IPriorityAction[] toActions(EditorAdapter[] adapters, Resource forResource) {
73         IPriorityAction[] actions = new IPriorityAction[adapters.length];
74         for (int i = 0; i < adapters.length; ++i) {
75             actions[i] = new ResourceEditorAdapterAction(adapters[i], forResource);
76         }
77         return actions;
78     }
79
80 }