X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FDefaultKeyListener.java;fp=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FDefaultKeyListener.java;h=7e5de464a4223ad6f934d264f8a19c6f215fed8a;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java new file mode 100644 index 000000000..7e5de464a --- /dev/null +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * 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.browsing.ui.swt; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.viewers.IPostSelectionProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; +import org.simantics.Simantics; +import org.simantics.browsing.ui.GraphExplorer; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.model.queries.IsNodeContextModifiable; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.management.ISessionContextProvider; +import org.simantics.ui.workbench.action.ChooseActionRequest; +import org.simantics.utils.datastructures.Function; +import org.simantics.utils.ui.ISelectionUtils; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +/** + * The default keyboard handler for {@link GraphExplorer}. + * + *

+ * Carriage return presses are handled similarly to mouse double clicks. F2 + * initiates in-line editing of the selection when possible. + *

+ * + * @author Tuukka Lehtonen + */ +public class DefaultKeyListener extends KeyAdapter { + + private final ISessionContextProvider contextProvider; + private final GraphExplorer explorer; + private final Function editingColumnResolver; + + /** + * @param contextProvider + * @param explorer + * @param editingColumnResolver + */ + public DefaultKeyListener(ISessionContextProvider contextProvider, GraphExplorer explorer, Function editingColumnResolver) { + assert contextProvider != null; + assert explorer != null; + assert editingColumnResolver != null; + + this.contextProvider = contextProvider; + this.explorer = explorer; + this.editingColumnResolver = editingColumnResolver; + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) { + if (explorer.isDisposed()) + return; + + ISessionContext sessionContext = contextProvider.getSessionContext(); + if (sessionContext == null) + return; + + IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class); + + ISelection input = selectionProvider.getSelection(); + if (input.isEmpty()) + return; +// Resource resource = ResourceAdaptionUtils.toSingleResource(selectionProvider.getSelection()); +// if (resource == null) +// return; + final String perspectiveId = WorkbenchUtils.getCurrentPerspectiveId(); + Control control = explorer.getControl(); + Shell shell = control.getShell(); + + // Try the doubleClick-extensions + sessionContext.getSession().asyncRequest(new ChooseActionRequest(shell, explorer, input, perspectiveId)); + } else if (e.keyCode == SWT.F2) { + + IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class); + NodeContext context = ISelectionUtils.filterSingleSelection(selectionProvider.getSelection(), NodeContext.class); + if (context == null || !isEditable(context)) + return; + + for (String column : editingColumnResolver.execute(context)) { + // Prefix column key with '#' to indicate that + // textual editing is preferred if supported. + String error = explorer.startEditing(context, "#" + column); + if (error == null) + return; + } + return; + } + } + + private boolean isEditable(NodeContext context) { + try { + return Simantics.getSession().syncRequest(new IsNodeContextModifiable(context)); + } catch (DatabaseException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Failed to check node context modifiability, see exception for details.", e)); + return false; + } + } + +}