--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench.editor;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.ui.DoubleClickEvent;\r
+import org.simantics.ui.IDoubleClickAction;\r
+import org.simantics.ui.workbench.action.IWorkbenchActionHints;\r
+import org.simantics.ui.workbench.action.ResourceEditorAdapterAction;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class OpenDefaultEditor implements IDoubleClickAction {\r
+\r
+ @Override\r
+ public void doubleClickEvent(DoubleClickEvent e) {\r
+ ReadGraph g = e.getGraph();\r
+ Object r = e.getResource();\r
+\r
+ Boolean remember = e.getHintContext().getHint(IWorkbenchActionHints.KEY_REMEMBER);\r
+ final boolean rememberChoice = remember != null ? remember : false;\r
+\r
+ Boolean ask = e.getHintContext().getHint(IWorkbenchActionHints.KEY_ALWAYS_ASK);\r
+ boolean alwaysAsk = ask != null ? ask : false;\r
+\r
+ EditorAdapter primaryAdapter = EditorRegistry.getInstance().getMappings().get(r);\r
+ if (!alwaysAsk && primaryAdapter != null) {\r
+ e.add(new ResourceEditorAdapterAction(primaryAdapter, r));\r
+ } else {\r
+ try {\r
+ EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(g, r);\r
+ for (final EditorAdapter a : editorAdapters) {\r
+ e.add(new ResourceEditorAdapterAction(a, r) {\r
+ @Override\r
+ protected void safeRun() throws Exception {\r
+ super.safeRun();\r
+\r
+ if (rememberChoice) {\r
+ // Make this choice the default for the next time.\r
+ EditorRegistry.getInstance().getMappings().put(getResource(), getAdapter());\r
+ }\r
+ }\r
+ });\r
+ }\r
+ } catch (DatabaseException ex) {\r
+ }\r
+ }\r
+ }\r
+\r
+}\r