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 / +93 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
	/**
259
	 * Creates a table viewer on a newly-created table control under the given
272
	 * 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
273
	 * parent. The table control is created using the SWT style bits
261
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
274
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
Lines 662-680 Link Here
662
	public Table getTable() {
675
	public Table getTable() {
663
		return table;
676
		return table;
664
	}
677
	}
665
678
	
666
	/*
679
	/*
667
	 *  (non-Javadoc)
680
	 *  (non-Javadoc)
668
	 * @see org.eclipse.jface.viewers.ContentViewer#hookControl(org.eclipse.swt.widgets.Control)
681
	 * @see org.eclipse.jface.viewers.ContentViewer#hookControl(org.eclipse.swt.widgets.Control)
669
	 */
682
	 */
670
	protected void hookControl(Control control) {
683
	protected void hookControl(Control control) {
671
		super.hookControl(control);
684
		super.hookControl(control);
672
		Table tableControl = (Table) control;
685
		final Table tableControl = (Table) control;
673
		tableControl.addMouseListener(new MouseAdapter() {
686
		tableControl.addMouseListener(new MouseAdapter() {
674
			public void mouseDown(MouseEvent e) {
687
			public void mouseDown(MouseEvent e) {
675
				tableViewerImpl.handleMouseDown(e);
688
				// Avoid overhead when no editors defined
689
				if( getCellEditors() != null && getCellEditors().length > 0 ) {
690
					// Avoid overhead when no activation strategy
691
					if( cellEditorActivationStrategy == null ) {
692
						tableViewerImpl.handleMouseDown(e);
693
					} else {
694
						Item[] items = tableControl.getSelection();
695
						// Do not edit if more than one row is selected.
696
						if (items.length != 1) {
697
			            return;
698
						}
699
			        
700
					
701
						if( cellEditorActivationStrategy == null || cellEditorActivationStrategy.shouldActivateCellEditor(e, items[0].getData(), false) ) {
702
							tableViewerImpl.handleMouseDown(e);
703
						}
704
					
705
						TableViewer.this.lastMouseEvent = e;
706
					}				
707
				}
708
			}
709
		});
710
		
711
		// We need to handle it this way because using after the double click a selection event 
712
		// occurs and the cell-editor would loose focus
713
		addDoubleClickListener(new IDoubleClickListener() {
714
			public void doubleClick(DoubleClickEvent event) {
715
				// Avoid overhead when no activation strategy or no editors
716
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 ) {
717
					Item[] items = tableControl.getSelection();
718
			        // Do not edit if more than one row is selected.
719
			        if (items.length != 1) {
720
			            return;
721
			        }
722
			        
723
					if( cellEditorActivationStrategy != null && cellEditorActivationStrategy.shouldActivateCellEditor(lastMouseEvent, items[0].getData(), true) ) {
724
						tableViewerImpl.handleMouseDown(lastMouseEvent);
725
					}
726
				}				
676
			}
727
			}
677
		});
728
		});
729
		
730
		// Pressing return on a table control results in DoubleClick Event
731
		tableControl.addKeyListener(new KeyListener() {
732
733
			public void keyPressed(KeyEvent e) {
734
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 && e.keyCode == '\r' ) {
735
					Event event = new Event();
736
					event.x = -1;
737
					event.y = -1;
738
					event.widget = tableControl;
739
					
740
					TableItem[] items = tableControl.getSelection();
741
			        // Do not edit if more than one row is selected.
742
			        if (items.length != 1) {
743
			            return;
744
			        }
745
			        
746
			        event.x = items[0].getBounds().x;
747
					event.y = items[0].getBounds().y;
748
					event.button = 1;
749
					
750
					TableViewer.this.lastMouseEvent = new MouseEvent(event);
751
				}
752
			}
753
754
			public void keyReleased(KeyEvent e) {
755
				
756
			}
757
			
758
		});
678
	}
759
	}
679
760
680
	/*
761
	/*
Lines 1287-1294 Link Here
1287
		Assert.isTrue(provider instanceof IStructuredContentProvider ||
1368
		Assert.isTrue(provider instanceof IStructuredContentProvider ||
1288
				provider instanceof ILazyContentProvider);
1369
				provider instanceof ILazyContentProvider);
1289
	}
1370
	}
1290
	
1371
1291
	
1372
	public ICellEditorActivationStrategy getCellEditorActivationStrategy() {
1292
	
1373
		return cellEditorActivationStrategy;
1374
	}
1375
1376
	public void setCellEditorActivationStrategy(
1377
			ICellEditorActivationStrategy cellEditorActivationStrategy) {
1378
		this.cellEditorActivationStrategy = cellEditorActivationStrategy;
1379
	}
1293
}
1380
}
1294
1381
(-)src/org/eclipse/jface/viewers/TreeViewer.java (-6 / +84 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
	/**
146
	 * Creates a tree viewer on a newly-created tree control under the given
159
	 * 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
160
	 * parent. The tree control is created using the SWT style bits
148
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
161
	 * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
Lines 487-498 Link Here
487
	 */
500
	 */
488
	protected void hookControl(Control control) {
501
	protected void hookControl(Control control) {
489
		super.hookControl(control);
502
		super.hookControl(control);
490
		Tree treeControl = (Tree) control;
503
		final Tree treeControl = (Tree) control;
491
		treeControl.addMouseListener(new MouseAdapter() {
504
		
492
			public void mouseDown(MouseEvent e) {
493
				treeViewerImpl.handleMouseDown(e);
494
			}
495
		});
496
		if ((treeControl.getStyle() & SWT.VIRTUAL) != 0) {
505
		if ((treeControl.getStyle() & SWT.VIRTUAL) != 0) {
497
			treeControl.addDisposeListener(new DisposeListener(){
506
			treeControl.addDisposeListener(new DisposeListener(){
498
				public void widgetDisposed(DisposeEvent e) {
507
				public void widgetDisposed(DisposeEvent e) {
Lines 520-525 Link Here
520
				}
529
				}
521
			});
530
			});
522
		}
531
		}
532
		
533
		treeControl.addMouseListener(new MouseAdapter() {
534
			public void mouseDown(MouseEvent e) {
535
				// Avoid overhead when no editors defined
536
				if( getCellEditors() != null && getCellEditors().length > 0 ) {
537
					// Avoid overhead when no activation strategy
538
					if( cellEditorActivationStrategy == null ) {
539
						treeViewerImpl.handleMouseDown(e);
540
					} else {
541
						Item[] items = treeControl.getSelection();
542
				        // Do not edit if more than one row is selected.
543
				        if (items.length != 1) {
544
				            return;
545
				        }
546
				        
547
						if( cellEditorActivationStrategy == null || cellEditorActivationStrategy.shouldActivateCellEditor(e, items[0].getData(), false) ) {
548
							treeViewerImpl.handleMouseDown(e);
549
						}
550
						
551
						TreeViewer.this.lastMouseEvent = e;
552
					}				
553
				}
554
			}
555
		});
556
		
557
		// We need to handle it this way because using after the double click a selection event 
558
		// occurs and the cell-editor would loose focus
559
		addDoubleClickListener(new IDoubleClickListener() {
560
			public void doubleClick(DoubleClickEvent event) {
561
				// Avoid overhead when no activation strategy or no editors
562
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 ) {
563
					Item[] items = treeControl.getSelection();
564
				    // Do not edit if more than one row is selected.
565
				    if (items.length != 1) {
566
				    	return;
567
				    }
568
				        
569
				    if( cellEditorActivationStrategy.shouldActivateCellEditor(lastMouseEvent, items[0].getData(), true) ) {
570
				    	treeViewerImpl.handleMouseDown(lastMouseEvent);
571
				    }
572
				}
573
			}
574
		});
575
		
576
		// Pressing return on a table control results in DoubleClick Event
577
		treeControl.addKeyListener(new KeyAdapter() {
578
579
			public void keyPressed(KeyEvent e) {
580
				// Avoid overhead when no activation strategy or no editors
581
				if( cellEditorActivationStrategy != null && getCellEditors() != null && getCellEditors().length > 0 && e.keyCode == '\r' ) {
582
					Event event = new Event();
583
					event.x = -1;
584
					event.y = -1;
585
					event.widget = treeControl;
586
						
587
					TreeItem[] items = treeControl.getSelection();
588
					// Do not edit if more than one row is selected.
589
					if (items.length != 1) {
590
						return;
591
					}
592
				        
593
					event.x = items[0].getBounds().x;
594
					event.y = items[0].getBounds().y;
595
					event.button = 1;
596
						
597
					TreeViewer.this.lastMouseEvent = new MouseEvent(event);
598
				}
599
			}			
600
		});
523
	}
601
	}
524
602
525
	/**
603
	/**
(-)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