Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f9b12fe47a303f432ead7f691bdc8867b052117f (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
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * 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:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.editor.composites;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.m2e.editor.internal.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;


/**
 * List editor composite
 * 
 * @author Eugene Kuleshov
 */
public class ListEditorComposite<T> extends Composite {


  TableViewer viewer;
  
  protected Map<String, Button> buttons = new HashMap<String, Button>(5);
  
  /*
   * Default button keys
   */
  private static final String ADD = "ADD"; //$NON-NLS-1$
  private static final String CREATE = "CREATE"; //$NON-NLS-1$
  private static final String REMOVE = "REMOVE"; //$NON-NLS-1$

  boolean readOnly = false;

  protected FormToolkit toolkit;

  public ListEditorComposite(Composite parent, int style, boolean includeSearch) {
    super(parent, style);
    toolkit = new FormToolkit(parent.getDisplay());

    GridLayout gridLayout = new GridLayout(2, false);
    gridLayout.marginWidth = 1;
    gridLayout.marginHeight = 1;
    gridLayout.verticalSpacing = 1;
    setLayout(gridLayout);

    final Table table = toolkit.createTable(this, SWT.FLAT | SWT.MULTI | style);
    table.setData("name", "list-editor-composite-table"); //$NON-NLS-1$ //$NON-NLS-2$
    final TableColumn column = new TableColumn(table, SWT.NONE);
    table.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent e) {
        column.setWidth(table.getClientArea().width);
      }
    });

    viewer = new TableViewer(table);

    createButtons(includeSearch);
    
    int vSpan = buttons.size();
    GridData viewerData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, vSpan);
    viewerData.widthHint = 100;
    viewerData.heightHint = includeSearch ? 125 : 50;
    viewerData.minimumHeight = includeSearch ? 125 : 50;
    table.setLayoutData(viewerData);
    viewer.setData(FormToolkit.KEY_DRAW_BORDER, Boolean.TRUE);

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
      public void selectionChanged(SelectionChangedEvent event) {
        viewerSelectionChanged();
      }
    });

    toolkit.paintBordersFor(this);
  }

  public ListEditorComposite(Composite parent, int style) {
    this(parent, style, false);
  }

  public void setLabelProvider(ILabelProvider labelProvider) {
    viewer.setLabelProvider(labelProvider);
  }

  public void setContentProvider(ListEditorContentProvider<T> contentProvider) {
    viewer.setContentProvider(contentProvider);
  }

  public void setInput(List<T> input) {
    viewer.setInput(input);
    viewer.setSelection(new StructuredSelection());
  }

  public Object getInput() {
    return viewer.getInput();
  }

  public void setOpenListener(IOpenListener listener) {
    viewer.addOpenListener(listener);
  }

  public void addSelectionListener(ISelectionChangedListener listener) {
    viewer.addSelectionChangedListener(listener);
  }

  public void setAddButtonListener(SelectionListener listener) {
    if(getAddButton() != null) {
      getAddButton().addSelectionListener(listener);
      getAddButton().setEnabled(true);
    }
  }
  
  protected Button getCreateButton() {
    return buttons.get(CREATE);
  }
  
  protected Button getRemoveButton() {
    return buttons.get(REMOVE);
  }

  protected Button getAddButton() {
    return buttons.get(ADD);
  }

  public void setCreateButtonListener(SelectionListener listener) {
    getCreateButton().addSelectionListener(listener);
    getCreateButton().setEnabled(true);
  }

  public void setRemoveButtonListener(SelectionListener listener) {
    getRemoveButton().addSelectionListener(listener);
  }

  public TableViewer getViewer() {
    return viewer;
  }

  public int getSelectionIndex() {
    return viewer.getTable().getSelectionIndex();
  }

  public void setSelectionIndex(int n) {
    viewer.getTable().setSelection(n);
  }

  @SuppressWarnings("unchecked")
  public List<T> getSelection() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    return selection == null ? Collections.emptyList() : selection.toList();
  }

  public void setSelection(List<T> selection) {
    viewer.setSelection(new StructuredSelection(selection), true);
  }

  public void setReadOnly(boolean readOnly) {
    this.readOnly = readOnly;
    for (Map.Entry<String, Button> entry : buttons.entrySet()) {
      if (entry.getKey().equals(REMOVE)) {
        //Special case, as it makes no sense to enable if it there's nothing selected.
        updateRemoveButton();
      } else {
        entry.getValue().setEnabled(!readOnly);
      }
    }
  }

  protected void viewerSelectionChanged() {
    updateRemoveButton();
  }
  
  protected void updateRemoveButton() {
    getRemoveButton().setEnabled(!readOnly && !viewer.getSelection().isEmpty());
  }

  public void refresh() {
    if(!viewer.getTable().isDisposed()) {
      viewer.refresh(true);
    }
  }

  public void setCellModifier(ICellModifier cellModifier) {
    viewer.setColumnProperties(new String[] {"?"}); //$NON-NLS-1$

    TextCellEditor editor = new TextCellEditor(viewer.getTable());
    viewer.setCellEditors(new CellEditor[] {editor});
    viewer.setCellModifier(cellModifier);
  }

  public void setDoubleClickListener(IDoubleClickListener listener) {
    viewer.addDoubleClickListener(listener);
  }

  /**
   * Create the buttons that populate the column to the right of the ListViewer.
   * Subclasses must call the helper method addButton to add each button to the
   * composite.
   * 
   * @param includeSearch true if the search button should be created
   */
  protected void createButtons(boolean includeSearch) {
    if(includeSearch) {
      createAddButton();
    }
    createCreateButton();
    createRemoveButton();
  }
  
  protected void addButton(String key, Button button) {
    buttons.put(key, button);
  }

  protected void createAddButton() {
    addButton(ADD, createButton(Messages.ListEditorComposite_btnAdd));
  }

  protected void createCreateButton() {
    addButton(CREATE, createButton(Messages.ListEditorComposite_btnCreate));
  }

  protected void createRemoveButton() {
    addButton(REMOVE, createButton(Messages.ListEditorComposite_btnRemove));
  }

  protected Button createButton(String text) {
    Button button = toolkit.createButton(this, text, SWT.FLAT);
    GridData gd = new GridData(SWT.FILL, SWT.TOP, false, false);
    gd.verticalIndent = 0;
    button.setLayoutData(gd);
    button.setEnabled(false);
    return button;
  }
}

Back to the top