Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 87733 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/viewers/TableViewer.java (-6 / +103 lines)
Lines 18-23 Link Here
18
import org.eclipse.jface.util.Assert;
18
import org.eclipse.jface.util.Assert;
19
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.custom.TableEditor;
20
import org.eclipse.swt.custom.TableEditor;
21
import org.eclipse.swt.events.KeyEvent;
22
import org.eclipse.swt.events.KeyListener;
21
import org.eclipse.swt.events.MouseAdapter;
23
import org.eclipse.swt.events.MouseAdapter;
22
import org.eclipse.swt.events.MouseEvent;
24
import org.eclipse.swt.events.MouseEvent;
23
import org.eclipse.swt.graphics.Image;
25
import org.eclipse.swt.graphics.Image;
Lines 256-261 Link Here
256
	private TableColorAndFontNoOp tableColorAndFont = new TableColorAndFontNoOp();
258
	private TableColorAndFontNoOp tableColorAndFont = new TableColorAndFontNoOp();
257
	
259
	
258
	/**
260
	/**
261
	 * Strategy to enable control activation of celleditors
262
	 */
263
	private ICellEditorActivationStrategy cellEditorActivationStrategy;
264
	
265
	/**
266
	 * The last mouse event has to be remembered because the double-click doesn't provide informations
267
	 * about this
268
	 */
269
	private MouseEvent lastMouseEvent;
270
	
271
	/**
272
	 * Prevent from a possible endless-loop 
273
	 */
274
	private MouseEvent ddMouseEvent;
275
	
276
	/**
259
	 * Creates a table viewer on a newly-created table control under the given
277
	 * Creates a table viewer on a newly-created table control under the given
260
	 * parent. The table control is created using the SWT style bits
278
	 * parent. The table control is created using the SWT style bits
261
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
279
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
Lines 662-679 Link Here
662
	public Table getTable() {
680
	public Table getTable() {
663
		return table;
681
		return table;
664
	}
682
	}
665
683
	
666
	/*
684
	/*
667
	 *  (non-Javadoc)
685
	 *  (non-Javadoc)
668
	 * @see org.eclipse.jface.viewers.ContentViewer#hookControl(org.eclipse.swt.widgets.Control)
686
	 * @see org.eclipse.jface.viewers.ContentViewer#hookControl(org.eclipse.swt.widgets.Control)
669
	 */
687
	 */
670
	protected void hookControl(Control control) {
688
	protected void hookControl(Control control) {
671
		super.hookControl(control);
689
		super.hookControl(control);
672
		Table tableControl = (Table) control;
690
		final Table tableControl = (Table) control;
673
		tableControl.addMouseListener(new MouseAdapter() {
691
		tableControl.addMouseListener(new MouseAdapter() {
674
			public void mouseDown(MouseEvent e) {
692
			public void mouseDown(MouseEvent e) {
675
				tableViewerImpl.handleMouseDown(e);
693
				// Avoid overhead when no editors defined
694
				if( getCellEditors() != null && getCellEditors().length > 0 ) {
695
					// Avoid overhead when no activation strategy
696
					if( cellEditorActivationStrategy == null ) {
697
						tableViewerImpl.handleMouseDown(e);
698
					} else {
699
						Item[] items = tableControl.getSelection();
700
						// Do not edit if more than one row is selected.
701
						if (items.length != 1) {
702
			            return;
703
						}
704
			        
705
					
706
						if( cellEditorActivationStrategy == null || cellEditorActivationStrategy.shouldActivateCellEditor(e, items[0].getData(), false) ) {
707
							tableViewerImpl.handleMouseDown(e);
708
						}
709
					
710
						TableViewer.this.lastMouseEvent = e;
711
					}				
712
				}
713
			}
714
		});
715
		
716
		// We need to handle it this way because using after the double click a selection event 
717
		// occurs and the cell-editor would loose focus
718
		addDoubleClickListener(new IDoubleClickListener() {
719
			public void doubleClick(DoubleClickEvent event) {
720
				/*
721
				 * Avoid overhead when no activation strategy or no editors
722
				 * The event check is needed because in some situations it is possible that a selection double click event
723
				 * occurrs without any interaction maybe this is an SWT bug one has to look at
724
				 */
725
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 && lastMouseEvent != ddMouseEvent ) {
726
					ddMouseEvent = lastMouseEvent;
727
					Item[] items = tableControl.getSelection();
728
			        // Do not edit if more than one row is selected.
729
			        if (items.length != 1) {
730
			            return;
731
			        }
732
			        
733
					if( cellEditorActivationStrategy != null && cellEditorActivationStrategy.shouldActivateCellEditor(lastMouseEvent, items[0].getData(), true) ) {
734
						tableViewerImpl.handleMouseDown(lastMouseEvent);
735
					}
736
				}				
737
			}
738
		});
739
		
740
		// Pressing return on a table control results in DoubleClick Event
741
		tableControl.addKeyListener(new KeyListener() {
742
743
			public void keyPressed(KeyEvent e) {
744
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 && e.keyCode == '\r' ) {
745
					Event event = new Event();
746
					event.x = -1;
747
					event.y = -1;
748
					event.widget = tableControl;
749
					
750
					TableItem[] items = tableControl.getSelection();
751
			        // Do not edit if more than one row is selected.
752
			        if (items.length != 1) {
753
			            return;
754
			        }
755
			        
756
			        event.x = items[0].getBounds().x;
757
					event.y = items[0].getBounds().y;
758
					event.button = 1;
759
					
760
					TableViewer.this.lastMouseEvent = new MouseEvent(event);
761
				}
762
			}
763
764
			public void keyReleased(KeyEvent e) {
765
				
676
			}
766
			}
767
			
677
		});
768
		});
678
	}
769
	}
679
770
Lines 1287-1294 Link Here
1287
		Assert.isTrue(provider instanceof IStructuredContentProvider ||
1378
		Assert.isTrue(provider instanceof IStructuredContentProvider ||
1288
				provider instanceof ILazyContentProvider);
1379
				provider instanceof ILazyContentProvider);
1289
	}
1380
	}
1290
	
1381
1291
	
1382
	public ICellEditorActivationStrategy getCellEditorActivationStrategy() {
1292
	
1383
		return cellEditorActivationStrategy;
1384
	}
1385
1386
	public void setCellEditorActivationStrategy(
1387
			ICellEditorActivationStrategy cellEditorActivationStrategy) {
1388
		this.cellEditorActivationStrategy = cellEditorActivationStrategy;
1389
	}
1293
}
1390
}
1294
1391
(-)src/org/eclipse/jface/viewers/TreeViewer.java (-6 / +94 lines)
Lines 19-24 Link Here
19
import org.eclipse.swt.custom.TreeEditor;
19
import org.eclipse.swt.custom.TreeEditor;
20
import org.eclipse.swt.events.DisposeEvent;
20
import org.eclipse.swt.events.DisposeEvent;
21
import org.eclipse.swt.events.DisposeListener;
21
import org.eclipse.swt.events.DisposeListener;
22
import org.eclipse.swt.events.KeyAdapter;
23
import org.eclipse.swt.events.KeyEvent;
22
import org.eclipse.swt.events.MouseAdapter;
24
import org.eclipse.swt.events.MouseAdapter;
23
import org.eclipse.swt.events.MouseEvent;
25
import org.eclipse.swt.events.MouseEvent;
24
import org.eclipse.swt.events.TreeListener;
26
import org.eclipse.swt.events.TreeListener;
Lines 143-148 Link Here
143
	private boolean treeIsDisposed = false;
145
	private boolean treeIsDisposed = false;
144
146
145
	/**
147
	/**
148
	 * Strategy to enable control activation of celleditors
149
	 */
150
	private ICellEditorActivationStrategy cellEditorActivationStrategy;
151
152
	/**
153
	 * The last mouse event has to be remembered because the double-click doesn't provide informations
154
	 * about this
155
	 */
156
	private MouseEvent lastMouseEvent;
157
	
158
	/**
159
	 * Prevent from a possible endless-loop 
160
	 */
161
	private MouseEvent ddMouseEvent;
162
	
163
	/**
146
	 * Creates a tree viewer on a newly-created tree control under the given
164
	 * Creates a tree viewer on a newly-created tree control under the given
147
	 * parent. The tree control is created using the SWT style bits
165
	 * parent. The tree control is created using the SWT style bits
148
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
166
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
Lines 487-498 Link Here
487
	 */
505
	 */
488
	protected void hookControl(Control control) {
506
	protected void hookControl(Control control) {
489
		super.hookControl(control);
507
		super.hookControl(control);
490
		Tree treeControl = (Tree) control;
508
		final Tree treeControl = (Tree) control;
491
		treeControl.addMouseListener(new MouseAdapter() {
509
		
492
			public void mouseDown(MouseEvent e) {
493
				treeViewerImpl.handleMouseDown(e);
494
			}
495
		});
496
		if ((treeControl.getStyle() & SWT.VIRTUAL) != 0) {
510
		if ((treeControl.getStyle() & SWT.VIRTUAL) != 0) {
497
			treeControl.addDisposeListener(new DisposeListener(){
511
			treeControl.addDisposeListener(new DisposeListener(){
498
				public void widgetDisposed(DisposeEvent e) {
512
				public void widgetDisposed(DisposeEvent e) {
Lines 520-525 Link Here
520
				}
534
				}
521
			});
535
			});
522
		}
536
		}
537
		
538
		treeControl.addMouseListener(new MouseAdapter() {
539
			public void mouseDown(MouseEvent e) {
540
				// Avoid overhead when no editors defined
541
				if( getCellEditors() != null && getCellEditors().length > 0 ) {
542
					// Avoid overhead when no activation strategy
543
					if( cellEditorActivationStrategy == null ) {
544
						treeViewerImpl.handleMouseDown(e);
545
					} else {
546
						Item[] items = treeControl.getSelection();
547
				        // Do not edit if more than one row is selected.
548
				        if (items.length != 1) {
549
				            return;
550
				        }
551
				        
552
						if( cellEditorActivationStrategy == null || cellEditorActivationStrategy.shouldActivateCellEditor(e, items[0].getData(), false) ) {
553
							treeViewerImpl.handleMouseDown(e);
554
						}
555
						
556
						TreeViewer.this.lastMouseEvent = e;
557
					}				
558
				}
559
			}
560
		});
561
		
562
		// We need to handle it this way because using after the double click a selection event 
563
		// occurs and the cell-editor would loose focus
564
		addDoubleClickListener(new IDoubleClickListener() {
565
			public void doubleClick(DoubleClickEvent event) {
566
				/*
567
				 * Avoid overhead when no activation strategy or no editors
568
				 * The event check is needed because in some situations it is possible that a selection double click event
569
				 * occurrs without any interaction maybe this is an SWT bug one has to look at
570
				 */
571
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 && lastMouseEvent != ddMouseEvent ) {
572
					ddMouseEvent = lastMouseEvent;
573
					Item[] items = treeControl.getSelection();
574
				    // Do not edit if more than one row is selected.
575
				    if (items.length != 1) {
576
				    	return;
577
				    }
578
				        
579
				    if( cellEditorActivationStrategy.shouldActivateCellEditor(lastMouseEvent, items[0].getData(), true) ) {
580
				    	treeViewerImpl.handleMouseDown(lastMouseEvent);
581
				    }
582
				}
583
			}
584
		});
585
		
586
		// Pressing return on a table control results in DoubleClick Event
587
		treeControl.addKeyListener(new KeyAdapter() {
588
589
			public void keyPressed(KeyEvent e) {
590
				// Avoid overhead when no activation strategy or no editors
591
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 && e.keyCode == '\r' ) {
592
					Event event = new Event();
593
					event.x = -1;
594
					event.y = -1;
595
					event.widget = treeControl;
596
						
597
					TreeItem[] items = treeControl.getSelection();
598
					// Do not edit if more than one row is selected.
599
					if (items.length != 1) {
600
						return;
601
					}
602
				        
603
					event.x = items[0].getBounds().x;
604
					event.y = items[0].getBounds().y;
605
					event.button = 1;
606
						
607
					TreeViewer.this.lastMouseEvent = new MouseEvent(event);
608
				}
609
			}			
610
		});
523
	}
611
	}
524
612
525
	/**
613
	/**
(-)src/org/eclipse/jface/viewers/DoubleClickEditorActivationStrategy.java (+30 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.swt.events.MouseEvent;
15
16
/**
17
 * Activate CellEditors only on DoubleClick
18
 * 
19
 * @since 3.3
20
 */
21
public class DoubleClickEditorActivationStrategy implements ICellEditorActivationStrategy {
22
23
	/* (non-Javadoc)
24
	 * @see org.eclipse.jface.viewers.ICellEditorActivationStrategy#shouldActivateCellEditor(org.eclipse.swt.events.MouseEvent)
25
	 */
26
	public boolean shouldActivateCellEditor(MouseEvent event,Object element,boolean doubleClick) {
27
		return doubleClick;
28
	}
29
	
30
}
(-)src/org/eclipse/jface/viewers/ICellEditorActivationStrategy.java (+37 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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
 ******************************************************************************/
11
12
package org.eclipse.jface.viewers;
13
14
import org.eclipse.swt.events.MouseEvent;
15
16
/**
17
 * 
18
 * Interface for classes who want to provide specialized cell-editor activation
19
 * 
20
 * @since 3.3
21
 * 
22
 */
23
public interface ICellEditorActivationStrategy {
24
	/**
25
	 * Decide whether the actual event should tigger a activation event
26
	 * 
27
	 * @param event
28
	 *            the event
29
	 * @param element
30
	 *            the model element
31
	 * @param doubleClick
32
	 *            is it a double-click
33
	 * @return true if this event should trigger selection
34
	 */
35
	public boolean shouldActivateCellEditor(MouseEvent event, Object element,
36
			boolean doubleClick);
37
}

Return to bug 87733