Skip to main content
summaryrefslogtreecommitdiffstats
blob: 95405780f7578cd3394faa03c1ea247e1b93873c (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.admin.dbtabletab;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.ui.admin.AdminView;
import org.eclipse.osee.framework.ui.admin.dbtabletab.DbDescribe.Describe;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.skynet.widgets.cellEditor.UniversalCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

public class DbTableViewer {

   private final DbTableTab dbTab;

   // private Shell shell;
   private Table table;
   private TableViewer tableViewer;

   private List<Describe> describeList;
   private DbTaskList dbTaskList;
   private DbDescribe dbDescribe;
   private final DbItem dbItem;

   /**
    * @param parent
    * @throws OseeDataStoreException
    */
   public DbTableViewer(Composite parent, int numColumns, DbTableTab dbTab, DbItem dbItem) throws OseeDataStoreException {
      this.dbTab = dbTab;
      this.dbItem = dbItem;
      getTableDescription();
      this.addChildControls(parent, numColumns);
   }

   public void save() {
      for (DbModel dbModel : getTasks()) {
         if (dbModel.isNeedSave()) {
            dbItem.save(dbDescribe, dbModel);
         }
         dbModel.setNeedSave(false);
         dbModel.setColumnChanged(null);
      }
      refresh();
   }

   public void addRecord() {
      for (DbModel dbModel : getTasks()) {
         if (dbModel.isNeedSave()) {
            AWorkbench.popup("ERROR", "Save Table Before Adding Record");
            return;
         }
      }
      // Send in the first record as an example
      DbModel dbModel = dbItem.createNewRow(getTasks().get(0));
      dbTaskList.addTask(dbModel);
      refresh();
   }

   public void add(DbModel a) {
      dbTaskList.addTask(a);
   }

   public List<String> getColumnNames() {
      ArrayList<String> list = new ArrayList<String>();
      for (Describe d : describeList) {
         list.add(d.name);
      }
      return list;
   }

   public String[] getColumnNameArray() {
      return getColumnNames().toArray(new String[describeList.size()]);
   }

   /**
    * Run and wait for a close event
    * 
    * @param shell Instance of Shell
    */
   @SuppressWarnings("unused")
   private void run(Shell shell) {
      Display display = shell.getDisplay();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
   }

   public void removeSelectedTasks() {
      IStructuredSelection sel = (IStructuredSelection) tableViewer.getSelection();
      Iterator<?> iter = sel.iterator();
      while (iter.hasNext()) {
         DbModel model = (DbModel) iter.next();
         if (model != null) {
            dbTaskList.removeTask(model);
         }
      }
   }

   /**
    * Release resources
    */
   public void dispose() {
      // Tell the label provider to release its resources
      tableViewer.getLabelProvider().dispose();
      tableViewer.getTable().dispose();
   }

   public void setSaveNeeded(boolean needed) {
      AdminView.setSaveNeeded(needed);
   }

   /**
    * Create a new shell, add the widgets, open the shell
    * 
    * @param composite - a Composite
    * @param numColumns - number of columns.
    */
   private void addChildControls(Composite composite, int numColumns) {
      // Create the table
      createTable(composite, numColumns);
      // Create and setup the TableViewer
      createTableViewer();
      tableViewer.setContentProvider(new ExampleContentProvider());
      tableViewer.setLabelProvider(new DbLabelProvider(getColumnNameArray()));
   }

   public Vector<DbModel> getTasks() {
      return dbTaskList.getTasks();
   }

   public void addDisposeListener(DisposeListener dl) {
      table.addDisposeListener(dl);
   }

   /**
    * Create the Table
    */
   private void createTable(Composite parent, int numColumns) {
      table =
         new Table(parent,
            SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
      GridData gridData = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
      gridData.horizontalSpan = numColumns;
      table.setLayoutData(gridData);
      table.setLinesVisible(true);
      table.setHeaderVisible(true);

      int x = 0;
      for (Describe d : describeList) {
         TableColumn column = new TableColumn(table, SWT.LEFT, x);
         column.setText(d.name);
         int width = dbItem.getColumnWidth(d.name);
         if (width == 0) {
            column.setWidth(50);
         } else {
            column.setWidth(width);
         }
         final int fx = x;
         column.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
               tableViewer.setSorter(new DbTableSorter(fx));
            }
         });
         x++;
      }

   }

   public void getTableDescription() throws OseeDataStoreException {
      dbDescribe = new DbDescribe(dbItem);
      describeList = dbDescribe.getDescription();
   }

   /**
    * Load table with administration items
    * 
    * @throws OseeDataStoreException
    */
   public void load() throws OseeDataStoreException {
      dbTaskList = dbDescribe.getDbTaskList(describeList);
      tableViewer.setInput(dbTaskList);
      refresh();
   }

   public void refresh() {
      tableViewer.refresh();
   }

   /**
    * Create the TableViewer
    */
   private void createTableViewer() {
      tableViewer = new TableViewer(table) {
      };
      tableViewer.setUseHashlookup(true);

      if (dbItem.isWriteAccess()) {
         String columnNames[] = new String[describeList.size()];
         int x = 0;
         for (Describe d : describeList) {
            columnNames[x++] = d.name;
         }
         tableViewer.setColumnProperties(columnNames);
         tableViewer.setCellModifier(new DbCellModifier(this, dbItem));

         // Create the cell editors; must be number of columns regardless
         // that we only use the first column as an editor
         CellEditor[] editors = new CellEditor[columnNames.length];
         x = 0;
         for (Describe d : describeList) {
            // ID Column of SITE_TOOLS is un-editable
            if (dbItem.isWriteable(d.name)) {
               editors[x] = new UniversalCellEditor(table);
            }
            x++;
         }

         // Assign the cell editors to the viewer
         tableViewer.setCellEditors(editors);
      }

      // Set the default sorter for the viewer
      tableViewer.setSorter(new DbTableSorter(0));

      table.addListener(SWT.MouseDoubleClick, new Listener() {
         @Override
         public void handleEvent(Event event) {
            TableItem[] items = table.getSelection();
            if (items != null && items.length > 0) {
               TableItem item = items[0];
               for (int index = 0; index < table.getColumnCount(); index++) {
                  if (item.getBounds(index).contains(event.x, event.y)) {
                     Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
                     CellTextDialog dialog =
                        new CellTextDialog(shell, getAdminView().getSelectedDbItem().getTableName(), table.getColumn(
                           index).getText(), item.getText(index));
                     dialog.open();
                     break;
                  }
               }

            }
         }
      });
   }

   /*
    * Close the window and dispose of resources
    */
   public void close() {
      Shell shell = table.getShell();
      if (shell != null && !shell.isDisposed()) {
         shell.dispose();
      }
   }

   /**
    * InnerClass that acts as a proxy for the ExampleTaskList providing content for the Table. It implements the
    * ITaskListViewer interface since it must register changeListeners with the ExampleTaskList
    */
   class ExampleContentProvider implements IStructuredContentProvider, ITaskListViewer {

      @Override
      public void inputChanged(Viewer v, Object oldInput, Object newInput) {
         if (newInput != null) {
            ((DbTaskList) newInput).addChangeListener(this);
         }
         if (oldInput != null) {
            ((DbTaskList) oldInput).removeChangeListener(this);
         }
      }

      @Override
      public void dispose() {
         dbTaskList.removeChangeListener(this);
      }

      // Return the tasks as an array of Objects
      @Override
      public Object[] getElements(Object parent) {
         return dbTaskList.getTasks().toArray();
      }

      @Override
      public void addTask(DbModel task) {
         tableViewer.add(task);
      }

      @Override
      public void removeTask(DbModel task) {
         tableViewer.remove(task);
      }

      @Override
      public void updateTask(DbModel task) {
         tableViewer.update(task, null);
      }
   }

   /**
    * @return currently selected item
    */
   public ISelection getSelection() {
      return tableViewer.getSelection();
   }

   /**
    * Return the ExampleTaskList
    */
   public DbTaskList getTaskList() {
      return dbTaskList;
   }

   /**
    * Return the parent composite
    */
   public Control getControl() {
      return table.getParent();
   }

   public DbTableTab getAdminView() {
      return dbTab;
   }

   private static final class CellTextDialog extends MessageDialog {

      private final String dialogMessage;

      public CellTextDialog(Shell parentShell, String dialogTitle, String dialogMessage, String dialogText) {
         super(parentShell, dialogTitle, PlatformUI.getWorkbench().getSharedImages().getImage(
            ISharedImages.IMG_OBJS_INFO_TSK), dialogMessage, MessageDialog.INFORMATION,
            new String[] {IDialogConstants.OK_LABEL}, 0);
         this.dialogMessage = dialogText;
         this.setShellStyle(this.getShellStyle() | SWT.RESIZE);
      }

      @Override
      protected Control createCustomArea(Composite parent) {
         Composite composite = new Composite(parent, SWT.NONE);
         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
         composite.setLayout(new GridLayout());
         composite.setFont(parent.getFont());

         Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
         GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
         text.setLayoutData(data);
         text.setEditable(false);
         text.setText(dialogMessage);
         text.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_WHITE));
         text.setFont(parent.getFont());
         return composite;
      }
   }
}

Back to the top