KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ColumnViewerEditorActivationStrategy


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
11  * - fix for bug 187817
12  *******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import org.eclipse.swt.events.KeyEvent;
17 import org.eclipse.swt.events.KeyListener;
18
19 /**
20  * This class is responsible to determine if a cell selection event is triggers
21  * an editor activation. Implementors can extend and overwrite to implement
22  * custom editing behavior
23  *
24  * @since 3.3
25  */

26 public class ColumnViewerEditorActivationStrategy {
27     private ColumnViewer viewer;
28
29     private KeyListener keyboardActivationListener;
30
31     /**
32      * @param viewer
33      * the viewer the editor support is attached to
34      */

35     public ColumnViewerEditorActivationStrategy(ColumnViewer viewer) {
36         this.viewer = viewer;
37     }
38
39     /**
40      * @param event
41      * the event triggering the action
42      * @return <code>true</code> if this event should open the editor
43      */

44     protected boolean isEditorActivationEvent(
45             ColumnViewerEditorActivationEvent event) {
46         boolean singleSelect = ((IStructuredSelection)viewer.getSelection()).size() == 1;
47         return singleSelect && (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION
48                 || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
49                 || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL);
50     }
51
52     /**
53      * @return the cell holding the current focus
54      */

55     private ViewerCell getFocusCell() {
56         return viewer.getColumnViewerEditor().getFocusCell();
57     }
58
59     /**
60      * @return the viewer
61      */

62     public ColumnViewer getViewer() {
63         return viewer;
64     }
65
66     /**
67      * Enable activation of cell editors by keyboard
68      *
69      * @param enable
70      * <code>true</code> to enable
71      */

72     public void setEnableEditorActivationWithKeyboard(boolean enable) {
73         if (enable) {
74             if (keyboardActivationListener == null) {
75                 keyboardActivationListener = new KeyListener() {
76
77                     public void keyPressed(KeyEvent e) {
78                         ViewerCell cell = getFocusCell();
79
80                         if (cell != null) {
81                             viewer
82                                     .triggerEditorActivationEvent(new ColumnViewerEditorActivationEvent(
83                                             cell, e));
84                         }
85                     }
86
87                     public void keyReleased(KeyEvent e) {
88
89                     }
90
91                 };
92                 viewer.getControl().addKeyListener(keyboardActivationListener);
93             }
94         } else {
95             if (keyboardActivationListener != null) {
96                 viewer.getControl().removeKeyListener(
97                         keyboardActivationListener);
98                 keyboardActivationListener = null;
99             }
100         }
101     }
102
103 }
104
Popular Tags