Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2da24ef6b251d73ff078558dd627155b22c818d0 (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
package org.eclipse.tcf.internal.cdt.ui.breakpoints;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ICellEditorListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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.Table;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

public class TCFContextQueryExpressionDialog extends SelectionDialog {

    private String expression;
    final private String originalExpression;
    final private String[] attributeList;
    private ParameterDataModel[] parameterData;
    final String[] columnNames = new String[] {"Parameter","Value"};

    protected TCFContextQueryExpressionDialog(Shell parentShell, String[] attributes, String initialExpression) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE);
        expression = initialExpression;
        originalExpression = initialExpression;
        attributeList = attributes;
    }

    String getParameterInitialValue (String comparator, int initIndex){
       if (expression != null && expression.length() > 0) {
           int indexExpr = expression.indexOf (comparator, initIndex);
           if (indexExpr != -1){
               // Make sure we didn't partial match to another parameter name.
               if (indexExpr != 0) {
                   String testChar = expression.substring(indexExpr-1, indexExpr);
                   if (!testChar.equals(",")) {
                       return getParameterInitialValue(comparator,indexExpr+1);
                   }
               }
               int startOfVal = expression.indexOf ('=', indexExpr);
               if (startOfVal != -1) {
                   startOfVal += 1;
                   int endOfVal = -1;
                   if (startOfVal != 0) {
                       endOfVal = expression.indexOf (',', startOfVal);
                       if (endOfVal == -1) {
                           endOfVal = expression.length();
                       }
                   }
                   return expression.substring(startOfVal, endOfVal);
               }
           }
       }
       return null;
   }

    /**
    *
    * ParameterDataModel - Data to populate table view.
    *
    */
   public static class ParameterDataModel {

       private String attribute;
       private String value;

       public ParameterDataModel(String label) {
           attribute = label;
           value ="";
         }

       public String getLabel() {
         return attribute;
       }

       public void setLabel(String label) {
         attribute = label;
       }

       public String getData() {
         return value;
       }

       public void setData(Object data) {
           if (data != null) {
               value = (String)data;
           }
           else {
               value = "";
           }
       }
     }

   ParameterDataModel[] setupTableList() {
       parameterData = new ParameterDataModel[attributeList.length];
       for (int i = 0; i < attributeList.length; i++) {
           parameterData[i] = new ParameterDataModel(attributeList[i]);

           String initialValue = getParameterInitialValue((String)attributeList[i], 0);
           if (initialValue!= null){
              parameterData[i].setData(initialValue);
          }
       }
       return parameterData;
   }

   public final class ParameterTableLabelProvider extends LabelProvider implements ITableLabelProvider {
       public Image getColumnImage(Object element, int columnIndex) {
         return null;
       }
       public String getColumnText(Object element, int columnIndex) {
         ParameterDataModel data = (ParameterDataModel) element;
         switch (columnIndex) {
           case 0:
             return data.getLabel();
           case 1:
             return data.getData();
           default:
             return "";
           }
       }
     }

   public class ValueCellEditor extends TextCellEditor {
       private Object tableElement;
       public ValueCellEditor(Composite parent) {
           super(parent);
           tableElement = null;
       }
       public ValueCellEditor(Composite parent, int style) {
           super(parent, style);
           tableElement = null;
       }
       public void setTableElement( Object element) {
           tableElement = element;
       }
       public Object getTableElement() {
           return tableElement;
       }
   }

   int findParameter(String comparator, int initIndex) {
       int indexExpr = -1;
       if (expression != null && expression.length() > 0) {
           indexExpr = expression.indexOf (comparator, initIndex);
           if (indexExpr != -1){
               // Make sure we didn't partial match to another parameter name.
               if (indexExpr != 0) {
                   String testChar = expression.substring(indexExpr-1, indexExpr);
                   if (!testChar.equals(",")) {
                       return findParameter(comparator,indexExpr+1);
                   }
               }
           }
       }
       return indexExpr;
   }

   private boolean replaceParameter(String parameter, String replaceString, int index) {
       if (index != 0) {
           String testChar = expression.substring(index+parameter.length(), index+1+parameter.length());
           if (!testChar.equals("=")) {
               return false;
           }
           testChar = expression.substring(index-1, index);
           if (!testChar.equals(",")) {
               return false;
           }
           else {
               index-=1;
           }
       }
       int endLocation = expression.indexOf(',', index+1);
       if (endLocation == -1) {
           endLocation = expression.length();
       }
       else if (index == 0 && replaceString.length() == 0) {
           endLocation++;
       }
       String removeStr = expression.substring(index, endLocation);
       expression = expression.replace(removeStr, replaceString);

       return true;
   }

   public final class CellEditorListener implements ICellEditorListener {
       private ValueCellEditor fcellEditor;
       public CellEditorListener(ValueCellEditor cellEditor) {
           fcellEditor = cellEditor;
       }
       public void applyEditorValue() {
           String cellString = null;
           Object obj = fcellEditor.getValue();
           ParameterDataModel param = (ParameterDataModel)fcellEditor.getTableElement();
           String paramName = param.getLabel();
           if (obj != null) {
               cellString = (String)obj;
           }
           if (cellString == null) {
               return;
           }
           cellString = cellString.trim();
           if (cellString.length() != 0) {
               if (expression == null || expression.length() == 0) {
                   expression = new String(paramName + "=" + cellString);
                   }
               else {
                   String nameValuePair = paramName + "=" + cellString;
                   int strIndex = findParameter(paramName, 0);
                   if (strIndex == -1) {
                       expression += "," + nameValuePair;
                   }
                   else {
                       if (strIndex != 0) {
                           nameValuePair = "," + nameValuePair;
                       }
                       if (!replaceParameter(paramName,nameValuePair,strIndex)) {
                           getButton(IDialogConstants.OK_ID).setEnabled(false);
                       }
                   }
               }
               param.setData(cellString);
           }
           else if (expression != null && expression.length() != 0){
               fcellEditor.setValue(cellString);
               int strIndex = findParameter(paramName, 0);
               if (strIndex != -1) {
                   if (!replaceParameter(paramName,"",strIndex)) {
                       getButton(IDialogConstants.OK_ID).setEnabled(false);
                   }
                   param.setData("");
               }
           }
           if (expression == null || expression.length() == 0 || originalExpression.contentEquals(expression)) {
               getButton(IDialogConstants.OK_ID).setEnabled(false);
           }
           else {
               getButton(IDialogConstants.OK_ID).setEnabled(true);
           }
      }

      public void cancelEditor() {
      }
      public void editorValueChanged(boolean oldValidState, boolean newValidState) {
      }
  }

   public final class ExpressionEditingSupport extends EditingSupport {

       private ValueCellEditor cellEditor = null;
       private ColumnViewer fviewer;

       private ExpressionEditingSupport(ColumnViewer viewer) {
           super(viewer);
           fviewer = viewer;
           cellEditor = new ValueCellEditor((Composite) getViewer().getControl(), SWT.NONE);
           cellEditor.addListener(new CellEditorListener(cellEditor));
       }

       @Override
       protected CellEditor getCellEditor(Object element) {
           cellEditor.setTableElement(element);
           return cellEditor;
       }

       @Override
       protected boolean canEdit(Object element) {
           return true;
       }

       @Override
       protected Object getValue(Object element) {
           if (element instanceof ParameterDataModel) {
               ParameterDataModel data = (ParameterDataModel)element;
               return data.getData();
           }
           return null;
       }

       @Override
       protected void setValue(Object element, Object value) {
           if (element instanceof ParameterDataModel) {
               ParameterDataModel data = (ParameterDataModel) element;
               data.setData(value);
               fviewer.update(element, null);
           }
       }
   }

    protected Control createDialogArea(Composite parent) {
        Composite page = new Composite(parent, SWT.NONE);
        GridLayout gridLayout = new GridLayout(1, true);
        page.setLayout(gridLayout);
        page.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        TableLayout tableLayout = new TableLayout();
        tableLayout.addColumnData(new ColumnWeightData(1));
        tableLayout.addColumnData(new ColumnWeightData(1));
        Table table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        table.setLayout(tableLayout);
        TableViewer tableViewer = new TableViewer(table);
        tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        Control cntrl = tableViewer.getControl();
        cntrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        TableViewerColumn labelColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        labelColumn.getColumn().setText(columnNames[0]);
        TableViewerColumn valueColumn = new TableViewerColumn(tableViewer, SWT.Modify);
        valueColumn.getColumn().setText(columnNames[1]);
        tableViewer.setContentProvider(new ArrayContentProvider());
        tableViewer.setLabelProvider(new ParameterTableLabelProvider());
        valueColumn.setEditingSupport(new ExpressionEditingSupport(valueColumn.getViewer()));
        tableViewer.setInput(setupTableList());
        tableViewer.setComparator(new ViewerComparator() {
            public int compare(Viewer viewer, Object e1, Object e2) {
                ParameterDataModel t1 = (ParameterDataModel) e1;
                ParameterDataModel t2 = (ParameterDataModel) e2;
                    return t1.getLabel().compareTo(t2.getLabel());
            };
        });
        return parent;
    }

    public String getExpression() {
        return expression;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets
     * .Shell)
     */
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Select Expression Parameters");
    }

    public void create() {
        super.create();
        getButton(IDialogConstants.OK_ID).setEnabled(false);
    }
}

Back to the top