1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.viewers.IPostSelectionProvider;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.KeyAdapter;
20 import org.eclipse.swt.events.KeyEvent;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Shell;
23 import org.simantics.Simantics;
24 import org.simantics.browsing.ui.GraphExplorer;
25 import org.simantics.browsing.ui.NodeContext;
26 import org.simantics.browsing.ui.model.queries.IsNodeContextModifiable;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.management.ISessionContext;
29 import org.simantics.db.management.ISessionContextProvider;
30 import org.simantics.ui.workbench.action.ChooseActionRequest;
31 import org.simantics.utils.datastructures.Function;
32 import org.simantics.utils.ui.ISelectionUtils;
33 import org.simantics.utils.ui.workbench.WorkbenchUtils;
36 * The default keyboard handler for {@link GraphExplorer}.
39 * Carriage return presses are handled similarly to mouse double clicks. F2
40 * initiates in-line editing of the selection when possible.
43 * @author Tuukka Lehtonen
45 public class DefaultKeyListener extends KeyAdapter {
47 private final ISessionContextProvider contextProvider;
48 private final GraphExplorer explorer;
49 private final Function<String[]> editingColumnResolver;
52 * @param contextProvider
54 * @param editingColumnResolver
56 public DefaultKeyListener(ISessionContextProvider contextProvider, GraphExplorer explorer, Function<String[]> editingColumnResolver) {
57 assert contextProvider != null;
58 assert explorer != null;
59 assert editingColumnResolver != null;
61 this.contextProvider = contextProvider;
62 this.explorer = explorer;
63 this.editingColumnResolver = editingColumnResolver;
67 public void keyPressed(KeyEvent e) {
68 if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
69 if (explorer.isDisposed())
72 ISessionContext sessionContext = contextProvider.getSessionContext();
73 if (sessionContext == null)
76 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
78 ISelection input = selectionProvider.getSelection();
81 // Resource resource = ResourceAdaptionUtils.toSingleResource(selectionProvider.getSelection());
82 // if (resource == null)
84 final String perspectiveId = WorkbenchUtils.getCurrentPerspectiveId();
85 Control control = explorer.getControl();
86 Shell shell = control.getShell();
88 // Try the doubleClick-extensions
89 sessionContext.getSession().asyncRequest(new ChooseActionRequest(shell, explorer, input, perspectiveId));
90 } else if (e.keyCode == SWT.F2) {
92 IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
93 NodeContext context = ISelectionUtils.filterSingleSelection(selectionProvider.getSelection(), NodeContext.class);
94 if (context == null || !isEditable(context))
97 for (String column : editingColumnResolver.execute(context)) {
98 // Prefix column key with '#' to indicate that
99 // textual editing is preferred if supported.
100 String error = explorer.startEditing(context, "#" + column);
108 private boolean isEditable(NodeContext context) {
110 return Simantics.getSession().syncRequest(new IsNodeContextModifiable(context));
111 } catch (DatabaseException e) {
112 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
113 "Failed to check node context modifiability, see exception for details.", e));