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