Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1d037a0072ffbc3e2a2d48d8b18ce1d04eff225d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - Initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *******************************************************************************/
package org.eclipse.wst.common.ui.internal.viewers;

import org.eclipse.swt.events.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;

import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.custom.TableTreeItem;
import org.eclipse.jface.viewers.*;


  /**
   * Adds a TableCursor to a StructuredViewer - for keyboard navigation of the table
   * The intent of this class is to provide the standard listeners for using F2 to
   * activate cell editors.
   *
   * Due to a current bug in the TableCursor, TableViewers using this class must make
   * a call similar to the TableNavigator method moveCellEditorsAbove(cellEditors)
   * whenever a setCellEditors call is made in the StructuredViewer.  This is so that the
   * cell editor control shows up above the table cursor control.
   */

public class TableNavigator extends TableCursor
{
  private static final String TABLETREEITEM_ID = "TableTreeItemID";

   final Table table;

   public TableNavigator(Table table, StructuredViewer viewer)
   {
      super(table, SWT.NONE);
      this.table = table;
      final Table currentTable = table;
      final StructuredViewer sViewer = viewer;

      // Linux index out of bounds fix.  See defect 253429, 253433, and more
      setVisible(false);

      addPaintListener(viewer);
      addKeyListeners(viewer);
      addMouseListeners(viewer);
      addSelectionListener(new SelectionAdapter()
      {
      	/**
		 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(SelectionEvent)
		 */
		public void widgetSelected(SelectionEvent e) {
			super.widgetSelected(e);
			     if (sViewer instanceof TableTreeViewer)
                 {
                   TableTreeItem tableTreeItem = (TableTreeItem)getRow().getData(TABLETREEITEM_ID);
                   StructuredSelection selection = new StructuredSelection(tableTreeItem.getData());
                   sViewer.setSelection(selection, true);
                 }
		}


      });
      addFocusListener(new FocusAdapter(){
         public void focusGained(FocusEvent e)
         {
         	// if e.source is not a child of the table then set selection - this is for tab into viewer
         	Object eventSource = e.getSource();
         	if (eventSource instanceof Control)
         	{
         	if (!isChild(currentTable, (Control)eventSource))
         	{
         	  if (currentTable.getItemCount() > 0 && currentTable.getSelectionCount() <= 0)
              {
                 if (sViewer instanceof TableTreeViewer)
                 {
                   TableTreeItem tableTreeItem = (TableTreeItem)getRow().getData(TABLETREEITEM_ID);
                   StructuredSelection selection = new StructuredSelection(tableTreeItem.getData());
                   sViewer.setSelection(selection, true);
                 }
                 else
                 {
               	   currentTable.setSelection(0); 
               	   setSelection(0,0);
                 }              
              }              
       	    }
       	    else 
       	    {
       	      if (currentTable.getItems().length > 0)
       	      {
       	      	 // cursor can end up on a non-existent table row 
       	         //   currently no way to get the current table cursor row 
       	      	 //   so for now just catch the exception since it doesn't
       	      	 //   cause any side effects.
       	      	 try
       	      	 {
                   setVisible(true);
       	      	 }
       	      	 catch (Exception ee)
       	      	 {     	            	      	
       	      		currentTable.setSelection(0);       	      		         
       	      		setSelection(0,0);
       	      	 }
       	       }       	    
       	       else  // do not show table cursor if there are no elements in the table - avoid repaint
       	       {
       	         setVisible(false);
       	       }
              }
         	}          
          }
          
          protected boolean isChild(Control parent, Control child)
          {
            Control tempChild = child;
            while (tempChild != null)
            {
              if (tempChild == parent)
              {
                return true;
              }
              tempChild = tempChild.getParent();
            }
            return false;
          }
          
          /**
           * @see org.eclipse.swt.events.FocusAdapter#focusLost(FocusEvent)
           */
          public void focusLost(FocusEvent e)
          {
            // Set the table navigator to be not visible if the the table
            // is not in focus and a child of the table is not in focus
            // note that we do this asynchronously so we don't mess up the
            // current focus handling.
            Display.getDefault().asyncExec(new Runnable()
            { 
              /**
               * @see java.lang.Runnable#run()
               */
              public void run()
              {
                if (currentTable != null && !currentTable.isDisposed() && !currentTable.isFocusControl() &&
                    !isChild(currentTable, Display.getDefault().getFocusControl()))
                {
                  setVisible(false);
                }
              }
            });
          }

      }); 
      
     
      

      table.addFocusListener(new FocusAdapter()
      {
        /**
         * @see org.eclipse.swt.events.FocusListener#focusGained(FocusEvent)
         */
        public void focusGained(FocusEvent e)
        {
          // only display navigator if there are items in the table 
          // and if the focus wasn't gained from our own table navigator
          // (ie focus came from outside)
          if (currentTable.getItemCount() > 0 &&
              (Display.getDefault().getFocusControl() != null) &&
              !Display.getDefault().getFocusControl().equals(TableNavigator.this))
          {
            // note that we do this asynchronously so we don't mess up the
            // current focus handling.
            Display.getDefault().asyncExec(new Runnable()            
            {
              /**
               * @see java.lang.Runnable#run()
               */
              public void run()
              {
                if (!isVisible())
                {
                  try
                  {
                    setVisible(true);
                    setFocus();
                  }
                  catch (Exception e)
                  {
                      // catch IllegalArgumentExceptions here - index out of bounds on tableviewer
                      if (currentTable.getItemCount() > 0)
                      {
                      currentTable.setSelection(0);                     
                      setSelection(0,0);                   
                      }
                      else  // do not show table cursor if there are no elements in the table - avoid repaint
                      {
                        setVisible(false);
                      }
                  }
                }
              }
            });
          }
        }
      });           
   }

   public Table getTable()
   {
     return table;
   }

    public void addPaintListener(StructuredViewer viewer)
    {
     
      addPaintListener(new PaintListener() 
      {
         public void paintControl(PaintEvent e)
         {
         	TableItem[] selection = table.getSelection();
            final TableItem row = (selection.length == 0) ? table.getItem(table.getTopIndex()) : selection[0];
            final String cellText = row.getText(getColumn());
            final Image cellImage = row.getImage(getColumn());
            final int col = getColumn();

            Display.getCurrent().asyncExec(new Runnable()
            {
               public void run()
               {
                  if (!row.isDisposed())
                  {
                     String newText = row.getText(getColumn());
                     if (!newText.equals(cellText) || !(row.getImage(col) == cellImage)) 
                     {
                       redraw();
                     }
                  }
                }
             });
           }
       });
    }


    public SelectionKeyAdapter getKeyAdapter(StructuredViewer viewer)
    {
        if (keyAdapter == null)
        {
           return new SelectionKeyAdapter(viewer);
        }
        else return keyAdapter;
    }

    public void setKeyAdapter(SelectionKeyAdapter kAdapter)
    {
       keyAdapter = kAdapter;
    }

    protected SelectionKeyAdapter keyAdapter = null;

    public class SelectionKeyAdapter extends KeyAdapter
    {
       StructuredViewer structuredViewer;

       public SelectionKeyAdapter(StructuredViewer viewer)
       {
          super();
          this.structuredViewer = viewer;
       }

       int lastKeyPressed = -1;  // used to cache the last key for key combos

       public void keyPressed(KeyEvent e) 
       {       	
               TableItem row = getRow();
               int column = getColumn();                

               // hack to emulate SHIFT+F10 popup menu - otherwise table cursor
               //   obscures the table popup mechanism and it doesn't work.
               if (lastKeyPressed == SWT.SHIFT && e.keyCode == SWT.F10)
               {
                  Menu popup = getTable().getMenu();
                  popup.setVisible(true);
               }
               lastKeyPressed = e.keyCode;
               
               //jvh - look for + or - key
               // column == 0
               if (row.getData(TABLETREEITEM_ID) instanceof TableTreeItem)
               {
	               if (column == 0 && e.character == '+') 
	               {
               	  	  TableTreeItem tableTreeItem = (TableTreeItem)row.getData(TABLETREEITEM_ID);	               	
	               	  ((TableTreeViewer)structuredViewer).setExpandedState(tableTreeItem.getData(), true);                       
	               	  refresh();
	               }
	               else if (column == 0 && e.character == '-') 
	               {
	               	  TableTreeItem tableTreeItem = (TableTreeItem)row.getData(TABLETREEITEM_ID);	               	
	               	  ((TableTreeViewer)structuredViewer).setExpandedState(tableTreeItem.getData(), false);                       
                      refresh();
	               }               
               }
               // use F2 to invoke editing for a cell
               if (e.keyCode == SWT.F2)     
               {
               	  if (structuredViewer instanceof TableViewer)
               	  {
                    ((TableViewer)structuredViewer).editElement(row.getData(), column);   
               	  }
               	  else if (structuredViewer instanceof TableTreeViewer)
               	  {  
               	  	  TableTreeItem tableTreeItem = (TableTreeItem)row.getData(TABLETREEITEM_ID);
               	  	 ((TableTreeViewer)structuredViewer).editElement(tableTreeItem.getData(), column);   
               	  }
               }
        }
    }

    public void addKeyListeners(StructuredViewer viewer)
    {
      final StructuredViewer structuredViewer = viewer;
                     
      addKeyListener(getKeyAdapter(structuredViewer));
    }      
    
   public void addMouseListeners(StructuredViewer viewer)
   {
      final StructuredViewer structuredViewer = viewer;

      addMouseListener(new MouseAdapter()
      {

         public void mouseUp(MouseEvent e) 
         {        
               TableItem row = getRow();
               int column = getColumn(); 
         
               // use mouse button 1 to invoke editing for a cell
               if (e.button == 1)     
               {
                  if (structuredViewer instanceof TableViewer)
                        {
                     ((TableViewer)structuredViewer).editElement(row.getData(), column);   
                        }
                        else if (structuredViewer instanceof TableTreeViewer && column == 1)
                        {
                                 TableTreeItem tableTreeItem = (TableTreeItem)row.getData(TABLETREEITEM_ID);
                                ((TableTreeViewer)structuredViewer).editElement(tableTreeItem.getData(), column);   
                        }                                               
               
                 if (structuredViewer instanceof TableTreeViewer && row.getData(TABLETREEITEM_ID) instanceof TableTreeItem)
                 {              
                                   if (column == 0)
                                   {
                                    TableTreeItem tableTreeItem = (TableTreeItem)row.getData(TABLETREEITEM_ID);                             
                                          boolean expandState = tableTreeItem.getExpanded();
                       ((TableTreeViewer)structuredViewer).setExpandedState(tableTreeItem.getData(), !expandState);
                       refresh();
                    }
                 }
               }          
            }
      });
     }


  /**
   * Ensure that cell editor control shows up above the table cursor control.
   * Should be called whenever the table viewer makes a new call to setCellEditors
   * i.e. in constructor and in refreshCellEditors
   *
   * @param - array of cell editors for the StructuredViewer
   */

  public void moveCellEditorsAbove(CellEditor[] editorArray)
  {
    for (int i = 0; i < editorArray.length ; i++)
    {
      CellEditor cEd = editorArray[i];
      if (cEd != null && cEd.getControl() != null)
      {
        cEd.getControl().moveAbove(null);
      }
    }
  }

  public void refresh()
  {
     Display.getCurrent().asyncExec(new Runnable()
     {
        public void run()
        {
           if (!isDisposed() && isVisible())
             redraw();
        }
     });
  }
}

Back to the top