]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/DefaultKeyListener.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / DefaultKeyListener.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
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;
34
35 /**
36  * The default keyboard handler for {@link GraphExplorer}.
37  * 
38  * <p>
39  * Carriage return presses are handled similarly to mouse double clicks. F2
40  * initiates in-line editing of the selection when possible.
41  * </p>
42  * 
43  * @author Tuukka Lehtonen
44  */
45 public class DefaultKeyListener extends KeyAdapter {
46
47     private final ISessionContextProvider contextProvider;
48     private final GraphExplorer          explorer;
49     private final Function<String[]>      editingColumnResolver;
50
51     /**
52      * @param contextProvider
53      * @param explorer
54      * @param editingColumnResolver
55      */
56     public DefaultKeyListener(ISessionContextProvider contextProvider, GraphExplorer explorer, Function<String[]> editingColumnResolver) {
57         assert contextProvider != null;
58         assert explorer != null;
59         assert editingColumnResolver != null;
60
61         this.contextProvider = contextProvider;
62         this.explorer = explorer;
63         this.editingColumnResolver = editingColumnResolver;
64     }
65
66     @Override
67     public void keyPressed(KeyEvent e) {
68         if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
69             if (explorer.isDisposed())
70                 return;
71
72             ISessionContext sessionContext = contextProvider.getSessionContext();
73             if (sessionContext == null)
74                 return;
75
76             IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
77
78             ISelection input = selectionProvider.getSelection();
79             if (input.isEmpty())
80                 return;
81 //            Resource resource = ResourceAdaptionUtils.toSingleResource(selectionProvider.getSelection());
82 //            if (resource == null)
83 //                return;
84             final String perspectiveId = WorkbenchUtils.getCurrentPerspectiveId();
85             Control control = explorer.getControl();
86             Shell shell = control.getShell();
87
88             // Try the doubleClick-extensions
89             sessionContext.getSession().asyncRequest(new ChooseActionRequest(shell, explorer, input, perspectiveId));
90         } else if (e.keyCode == SWT.F2) {
91
92             IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);
93             NodeContext context = ISelectionUtils.filterSingleSelection(selectionProvider.getSelection(), NodeContext.class);
94             if (context == null || !isEditable(context))
95                 return;
96
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); 
101                 if (error == null)
102                     return;
103             }
104             return;
105         }
106     }
107
108     private boolean isEditable(NodeContext context) {
109         try {
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));
114             return false;
115         }
116     }
117
118 }