X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Feditor%2FOpenDefaultEditor.java;fp=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Feditor%2FOpenDefaultEditor.java;h=a38515ad088f08aadffacf37b21ef56669056cd6;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/OpenDefaultEditor.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/OpenDefaultEditor.java new file mode 100644 index 000000000..a38515ad0 --- /dev/null +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/OpenDefaultEditor.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.ui.workbench.editor; + +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.ui.DoubleClickEvent; +import org.simantics.ui.IDoubleClickAction; +import org.simantics.ui.workbench.action.IWorkbenchActionHints; +import org.simantics.ui.workbench.action.ResourceEditorAdapterAction; + +/** + * @author Tuukka Lehtonen + */ +public class OpenDefaultEditor implements IDoubleClickAction { + + @Override + public void doubleClickEvent(DoubleClickEvent e) { + ReadGraph g = e.getGraph(); + Object r = e.getResource(); + + Boolean remember = e.getHintContext().getHint(IWorkbenchActionHints.KEY_REMEMBER); + final boolean rememberChoice = remember != null ? remember : false; + + Boolean ask = e.getHintContext().getHint(IWorkbenchActionHints.KEY_ALWAYS_ASK); + boolean alwaysAsk = ask != null ? ask : false; + + EditorAdapter primaryAdapter = EditorRegistry.getInstance().getMappings().get(r); + if (!alwaysAsk && primaryAdapter != null) { + e.add(new ResourceEditorAdapterAction(primaryAdapter, r)); + } else { + try { + EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(g, r); + for (final EditorAdapter a : editorAdapters) { + e.add(new ResourceEditorAdapterAction(a, r) { + @Override + protected void safeRun() throws Exception { + super.safeRun(); + + if (rememberChoice) { + // Make this choice the default for the next time. + EditorRegistry.getInstance().getMappings().put(getResource(), getAdapter()); + } + } + }); + } + } catch (DatabaseException ex) { + } + } + } + +}