summaryrefslogtreecommitdiff
path: root/src/plugins/logger.collector/src/com/motorola/studio/android/logger/collector/util/WidgetsFactory.java
blob: 40c679c4c34a894650190f51bffb295526465187 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorola.studio.android.logger.collector.util;

import org.eclipse.jface.dialogs.IDialogConstants;
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.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
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.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Class factory to create widgets.
 */
public class WidgetsFactory
{

    /**
     * Create a new composite with two columns.
     * 
     * @param parent The parent composite.
     * @return A composite with two columns.
     */
    public static Composite createComposite(Composite parent)
    {
        Composite toReturn = new Composite(parent, SWT.NULL);
        toReturn.setLayout(createGridLayout());
        toReturn.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));

        return toReturn;
    }

    /**
     * Create a new composite with the given column count.
     * 
     * @param parent The parent composite.
     * @param numColumns The column count for the new composite.
     * @return A composite with the given column count.
     */
    public static Composite createComposite(Composite parent, int numColumns)
    {
        Composite toReturn = createComposite(parent);
        ((GridLayout) toReturn.getLayout()).numColumns = numColumns;

        return toReturn;
    }

    /**
     * Creates a new line.
     * 
     * @param parent The parent composite.
     * @return A label with a line.
     */
    public static Label createLine(Composite parent)
    {
        Label toReturn = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.BOLD);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = ((GridLayout) parent.getLayout()).numColumns;
        toReturn.setLayoutData(gridData);

        return toReturn;
    }

    /**
     * Creates a new GridLayout with two columns.
     * 
     * @return A new GridLayout with two columns.
     */
    public static GridLayout createGridLayout()
    {
        GridLayout toReturn = new GridLayout();
        toReturn.numColumns = 2;
        toReturn.makeColumnsEqualWidth = false;
        toReturn.marginWidth = 0;
        toReturn.marginHeight = 0;

        return toReturn;
    }

    /**
     * Creates a new GridLayout with the given number of columns.
     * 
     * @return A new GridLayout with the given number of columns.
     */
    public static GridLayout createGridLayout(int numColumns)
    {
        GridLayout toReturn = new GridLayout();
        toReturn.numColumns = numColumns;
        toReturn.makeColumnsEqualWidth = false;
        toReturn.marginWidth = 0;
        toReturn.marginHeight = 0;

        return toReturn;
    }

    /**
     * Creates a new GridLayout with the given number of columns for the given
     * composite.
     * 
     * @return A new GridLayout with the given number of columns for the given
     *         composite.
     */
    public static GridLayout createGridLayout(int numColumns, Composite composite)
    {
        GridLayout toReturn = new GridLayout();
        toReturn.numColumns = numColumns;
        toReturn.makeColumnsEqualWidth = false;
        toReturn.marginWidth = 0;
        toReturn.marginHeight = 0;

        composite.setLayout(toReturn);
        return toReturn;
    }

    /**
     * Creates a new label.
     * 
     * @param parent The parent composite.
     * @param text Text used in label.
     * @return A new label
     */
    public static Label createLabel(Composite parent, String text)
    {
        return createLabel(parent, text, SWT.NONE);
    }

    /**
     * Creates a new label.
     * 
     * @param parent The parent composite.
     * @param text Text used in label.
     * @param style The style used in label.
     * @return A new label
     */
    public static Label createLabel(Composite parent, String text, int style)
    {
        Label toReturn = new Label(parent, style);
        if (text != null)
        {
            toReturn.setText(text);
        }
        toReturn.setFont(parent.getFont());

        return toReturn;
    }

    /**
     * Creates a new combo.
     * 
     * @param parent The parent composite.
     * @return The new combo
     */
    public static Combo createCombo(Composite parent)
    {
        Combo toReturn = new Combo(parent, SWT.READ_ONLY);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
        data.horizontalSpan = 1;
        toReturn.setLayoutData(data);

        return toReturn;
    }

    /**
     * Creates two labels used to show a read only value in screens.
     * 
     * @param parent The parent composite.
     * @param text The text used in both labels.
     * @return The label that will show a read only value.
     */
    public static Label createValueLabel(Composite parent, String text)
    {
        createLabel(parent, text);

        return createLabel(parent, null);
    }

    /**
     * Creates a new button.
     * 
     * @param parent The parent composite.
     * @param text The text of the button.
     * @return A new button
     */
    public static Button createButton(Composite parent, String text)
    {
        Button toReturn = new Button(parent, SWT.PUSH);
        toReturn.setFont(parent.getFont());
        toReturn.setText(text);
        toReturn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        return toReturn;
    }

    /**
     * Creates a new table widget.
     * 
     * @param parent The parent composite.
     * @return The new table
     */
    public static Table createTable(Composite parent)
    {
        Table toReturn =
                new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION
                        | SWT.BORDER);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
        data.heightHint = toReturn.getItemHeight();
        data.horizontalSpan = 1;
        toReturn.setLayoutData(data);

        return toReturn;
    }

    /**
     * Creates a new table widget.
     * 
     * @param parent The parent composite.
     * @return The new table
     */
    public static Table createTableMultiSelection(Composite parent)
    {
        Table toReturn =
                new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER
                        | SWT.MULTI);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
        data.heightHint = toReturn.getItemHeight();
        data.horizontalSpan = 1;
        toReturn.setLayoutData(data);

        return toReturn;
    }

    /**
     * Creates a new table column.
     * 
     * @param table The table of the column.
     * @param text The column text.
     * @return A new table column
     */
    public static TableColumn createTableColumn(Table table, String text)
    {
        TableColumn toReturn = new TableColumn(table, SWT.NONE);
        toReturn.setText(text);

        return toReturn;
    }

    /**
     * Creates a new table item.
     * 
     * @param table The table of the table item.
     * @param image The image of the item.
     * @param s The text of the item.
     * @return The new table item.
     */
    public static TableItem createTableItem(Table table, Image image, String s)
    {
        TableItem toReturn = new TableItem(table, SWT.NONE);
        toReturn.setText(s);
        if (image != null)
        {
            toReturn.setImage(image);
        }

        return toReturn;
    }

    /**
     * Creates a collection of table items.
     * 
     * @param table The table of the table item.
     * @param image The image of the item.
     * @param s The text of the item.
     * @return The new table item.
     */
    public static TableItem createTableItem(Table table, Image image, String... s)
    {
        TableItem toReturn = new TableItem(table, SWT.NONE);
        toReturn.setText(s);
        if (image != null)
        {
            toReturn.setImage(image);
        }

        return toReturn;
    }

    /**
     * Creates a new text.
     * 
     * @param parent The parent composite.
     * @return The new text.
     */
    public static Text createText(Composite parent)
    {
        Text toReturn = new Text(parent, SWT.BORDER);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
        toReturn.setLayoutData(data);

        return toReturn;
    }

    /**
     * Creates a new tree item. If the text parameter is null, it will appear is
     * after the field parameter.
     * 
     * @param itemParent The parent tree item
     * @param image The image of the tree item.
     * @param field The field.
     * @param text The text of the tree item.
     * @return The new tree item.
     */
    public static TreeItem createTreeItem(TreeItem itemParent, Image image, String field,
            String text)
    {
        TreeItem toReturn = new TreeItem(itemParent, 0);
        toReturn.setText((text == null) ? field : field + " (" + text + ")"); //$NON-NLS-1$ //$NON-NLS-2$
        toReturn.setImage(image);
        toReturn.setExpanded(true);

        return toReturn;
    }

    /**
     * Creates a new horizontal tool bar.
     * 
     * @param parent The parent tool bar item.
     * @return A new tool bar item.
     */
    public static ToolBar createHorizontalToolBar(Composite parent)
    {
        return new ToolBar(parent, SWT.HORIZONTAL);
    }

    /**
     * Creates a new tool item like a push button
     * 
     * @param toolBar The parent tool bar item.
     * @return A new tool item.
     */
    public static ToolItem createPushToolItem(ToolBar toolBar)
    {
        return new ToolItem(toolBar, SWT.PUSH);
    }

    /**
     * Creates a titled group.
     * 
     * @param parent The parent composite
     * @param title The desired title.
     * @return The titled group
     */
    public static Group createTitledGroup(Composite parent, String title)
    {
        Group toReturn = new Group(parent, SWT.SHADOW_NONE);
        toReturn.setText(title);
        toReturn.setLayout(new GridLayout());
        toReturn.setLayoutData(new GridData(GridData.FILL_BOTH));
        return toReturn;
    }

    /**
     * Creates a titled group with the given number of columns.
     * 
     * @param parent The parent composite.
     * @param title The group title.
     * @param numColumns The number of columns.
     * @return The created group.
     */
    public static Group createTitledGroup(Composite parent, String title, int numColumns)
    {
        Group toReturn = new Group(parent, SWT.SHADOW_NONE);
        toReturn.setText(title);
        GridLayout layout = createGridLayout(numColumns);
        toReturn.setLayout(layout);
        return toReturn;
    }

    /**
     * Creates a group
     * 
     * @param parent The parent composite
     * @return The group
     */
    public static Group createGroup(Composite parent)
    {
        Group toReturn = new Group(parent, SWT.SHADOW_NONE);
        return toReturn;
    }

    /**
     * Creates a radio button
     * 
     * @param parent The parent composite
     * @param text The given text.
     * @return The radio button
     */
    public static Button createRadioButton(Composite parent, String text)
    {
        Button toReturn = new Button(parent, SWT.RADIO);
        toReturn.setText(text);
        toReturn.setLayoutData(new GridData(GridData.FILL_BOTH));
        return toReturn;
    }

    /**
     * Creates a list.
     * 
     * @param parent The parent composite.
     * @return The list.
     */
    public static List createList(Composite parent)
    {
        return new List(parent, SWT.BORDER | SWT.SINGLE);
    }

}