]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/OpenDefaultEditor.java
OpenDefaultEditor should not cache adapters that cannot be opened
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / editor / OpenDefaultEditor.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.editor;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.ui.DoubleClickEvent;
17 import org.simantics.ui.IDoubleClickAction;
18 import org.simantics.ui.workbench.action.IWorkbenchActionHints;
19 import org.simantics.ui.workbench.action.ResourceEditorAdapterAction;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public class OpenDefaultEditor implements IDoubleClickAction {
25
26     @Override
27     public void doubleClickEvent(DoubleClickEvent e) {
28         ReadGraph g = e.getGraph();
29         Object r = e.getResource();
30
31         Boolean remember = e.getHintContext().getHint(IWorkbenchActionHints.KEY_REMEMBER);
32         final boolean rememberChoice = remember != null ? remember : false;
33
34         Boolean ask = e.getHintContext().getHint(IWorkbenchActionHints.KEY_ALWAYS_ASK);
35         boolean alwaysAsk = ask != null ? ask : false;
36
37         EditorAdapter primaryAdapter = EditorRegistry.getInstance().getMappings().get(r);
38         if (!alwaysAsk && primaryAdapter != null) {
39             e.add(new ResourceEditorAdapterAction(primaryAdapter, r));
40         } else {
41             try {
42                 EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(g, r);
43                 for (final EditorAdapter a : editorAdapters) {
44                     e.add(new ResourceEditorAdapterAction(a, r) {
45                         @Override
46                         protected void safeRun() throws Exception {
47                             super.safeRun();
48
49                             if (rememberChoice && a.getPriority() >= 0) {
50                                 // Make this choice the default for the next time.
51                                 EditorRegistry.getInstance().getMappings().put(getResource(), getAdapter());
52                             }
53                         }
54                     });
55                 }
56             } catch (DatabaseException ex) {
57             }
58         }
59     }
60
61 }