001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
011: * - fix for bug 187817
012: *******************************************************************************/package org.eclipse.jface.viewers;
013:
014: import org.eclipse.swt.events.KeyEvent;
015: import org.eclipse.swt.events.KeyListener;
016:
017: /**
018: * This class is responsible to determine if a cell selection event is triggers
019: * an editor activation. Implementors can extend and overwrite to implement
020: * custom editing behavior
021: *
022: * @since 3.3
023: */
024: public class ColumnViewerEditorActivationStrategy {
025: private ColumnViewer viewer;
026:
027: private KeyListener keyboardActivationListener;
028:
029: /**
030: * @param viewer
031: * the viewer the editor support is attached to
032: */
033: public ColumnViewerEditorActivationStrategy(ColumnViewer viewer) {
034: this .viewer = viewer;
035: }
036:
037: /**
038: * @param event
039: * the event triggering the action
040: * @return <code>true</code> if this event should open the editor
041: */
042: protected boolean isEditorActivationEvent(
043: ColumnViewerEditorActivationEvent event) {
044: boolean singleSelect = ((IStructuredSelection) viewer
045: .getSelection()).size() == 1;
046: return singleSelect
047: && (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION
048: || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL);
049: }
050:
051: /**
052: * @return the cell holding the current focus
053: */
054: private ViewerCell getFocusCell() {
055: return viewer.getColumnViewerEditor().getFocusCell();
056: }
057:
058: /**
059: * @return the viewer
060: */
061: public ColumnViewer getViewer() {
062: return viewer;
063: }
064:
065: /**
066: * Enable activation of cell editors by keyboard
067: *
068: * @param enable
069: * <code>true</code> to enable
070: */
071: public void setEnableEditorActivationWithKeyboard(boolean enable) {
072: if (enable) {
073: if (keyboardActivationListener == null) {
074: keyboardActivationListener = new KeyListener() {
075:
076: public void keyPressed(KeyEvent e) {
077: ViewerCell cell = getFocusCell();
078:
079: if (cell != null) {
080: viewer
081: .triggerEditorActivationEvent(new ColumnViewerEditorActivationEvent(
082: cell, e));
083: }
084: }
085:
086: public void keyReleased(KeyEvent e) {
087:
088: }
089:
090: };
091: viewer.getControl().addKeyListener(
092: keyboardActivationListener);
093: }
094: } else {
095: if (keyboardActivationListener != null) {
096: viewer.getControl().removeKeyListener(
097: keyboardActivationListener);
098: keyboardActivationListener = null;
099: }
100: }
101: }
102:
103: }
|