View Javadoc

1   /*
2    *  File: ListComposer.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.ui;
12  
13  import java.util.Iterator;
14  
15  import org.eclipse.jface.viewers.DoubleClickEvent;
16  import org.eclipse.jface.viewers.IContentProvider;
17  import org.eclipse.jface.viewers.IDoubleClickListener;
18  import org.eclipse.jface.viewers.ILabelProvider;
19  import org.eclipse.jface.viewers.IStructuredContentProvider;
20  import org.eclipse.jface.viewers.IStructuredSelection;
21  import org.eclipse.jface.viewers.ListViewer;
22  import org.eclipse.jface.viewers.Viewer;
23  import org.eclipse.jface.viewers.ViewerFilter;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.events.SelectionEvent;
26  import org.eclipse.swt.events.SelectionListener;
27  import org.eclipse.swt.graphics.Color;
28  import org.eclipse.swt.graphics.Point;
29  import org.eclipse.swt.layout.GridData;
30  import org.eclipse.swt.layout.GridLayout;
31  import org.eclipse.swt.layout.RowData;
32  import org.eclipse.swt.layout.RowLayout;
33  import org.eclipse.swt.widgets.Button;
34  import org.eclipse.swt.widgets.Composite;
35  import org.eclipse.swt.widgets.Control;
36  import org.eclipse.swt.widgets.Label;
37  
38  import de.jaret.util.ui.model.IMutableContentProvider;
39  
40  /**
41   * The ListComposer is a widget that is used for copying parts of another list in a new list. The visualization are the
42   * (classical) two lists with buttons for moving the entries. source is supplied as an IContentProvider, destination is
43   * adressed as an IMutableContentProvider(see package model).
44   * 
45   * @author Peter Kliem
46   * @version $Id: ListComposer.java 242 2007-02-11 21:05:07Z olk $
47   */
48  public class ListComposer extends Composite {
49  
50      protected String _srcLabelText = "Source";
51      protected String _destLabelText = "Destination";
52      protected String _headingLabelText = "Heading";
53  
54      protected IMutableContentProvider _destMutableCP;
55      protected IContentProvider _srcContentProvider;
56  
57      protected ILabelProvider _srcLabelProvider;
58      protected ILabelProvider _destLabelProvider;
59  
60      private ListViewer _srcViewer;
61      private ListViewer _destViewer;
62      private Button _remButton;
63      private Button _addButton;
64      private Button _addAllButton;
65      private Button _remAllButton;
66      private static final int VIEWERWIDTHHINT = 50;
67      private Label _headingLabelWidget;
68      private Label _srcLabelWidget;
69      private Label _destLabelWidget;
70  
71      public ListComposer(Composite parent, int style) {
72          super(parent, style);
73          createControls();
74          DefaultLabelProvider dlp = new DefaultLabelProvider();
75          _srcViewer.setLabelProvider(dlp);
76          _destViewer.setLabelProvider(dlp);
77  
78          // setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
79      }
80  
81      @Override
82      public void setBackground(Color bgColor) {
83          super.setBackground(bgColor);
84          Control children[] = getChildren();
85          for (int i = 0; i < children.length; i++) {
86              Control control = children[i];
87              control.setBackground(bgColor);
88          }
89      }
90  
91      private void createControls() {
92          int width = 30;
93  
94          // create the listener for the bunch of buttons
95          SelectionListener buttonListener = new ButtonListener();
96          // create Doubleclicklistener
97          IDoubleClickListener doubleClickListener = new DoubleClickListener();
98  
99          GridLayout gridLayout = new GridLayout();
100         gridLayout.numColumns = 3;
101         this.setLayout(gridLayout);
102 
103         GridData gd = new GridData();
104         gd.horizontalSpan = 3;
105         _headingLabelWidget = new Label(this, SWT.LEFT);
106         _headingLabelWidget.setLayoutData(gd);
107         _headingLabelWidget.setText(_headingLabelText);
108 
109         gd = new GridData();
110         gd.horizontalSpan = 2;
111         _srcLabelWidget = new Label(this, SWT.LEFT);
112         _srcLabelWidget.setLayoutData(gd);
113         _srcLabelWidget.setText(_srcLabelText);
114 
115         gd = new GridData();
116         _destLabelWidget = new Label(this, SWT.LEFT);
117         _destLabelWidget.setLayoutData(gd);
118         _destLabelWidget.setText(_destLabelText);
119 
120         gd = new GridData(GridData.FILL_BOTH);
121         gd.widthHint = VIEWERWIDTHHINT;
122         _srcViewer = new ListViewer(this);
123         _srcViewer.getList().setLayoutData(gd);
124         _srcViewer.addDoubleClickListener(doubleClickListener);
125 
126         Composite buttons = new Composite(this, SWT.NULL);
127         buttons.setLayout(new RowLayout(SWT.VERTICAL));
128 
129         _remButton = new Button(buttons, SWT.PUSH);
130         _remButton.setText("<");
131         Point size = _remButton.computeSize(SWT.DEFAULT, SWT.DEFAULT);
132         RowData rd = new RowData(width, size.y);
133         _remButton.setLayoutData(rd);
134         _remButton.addSelectionListener(buttonListener);
135 
136         _addButton = new Button(buttons, SWT.PUSH);
137         _addButton.setText(">");
138         size = _addButton.computeSize(SWT.DEFAULT, SWT.DEFAULT);
139         rd = new RowData(width, size.y);
140         _addButton.setLayoutData(rd);
141         _addButton.addSelectionListener(buttonListener);
142 
143         _addAllButton = new Button(buttons, SWT.PUSH);
144         _addAllButton.setText(">>");
145         size = _addAllButton.computeSize(SWT.DEFAULT, SWT.DEFAULT);
146         rd = new RowData(width, size.y);
147         _addAllButton.setLayoutData(rd);
148         _addAllButton.addSelectionListener(buttonListener);
149 
150         _remAllButton = new Button(buttons, SWT.PUSH);
151         _remAllButton.setText("<<");
152         size = _remAllButton.computeSize(SWT.DEFAULT, SWT.DEFAULT);
153         rd = new RowData(width, size.y);
154         _remAllButton.setLayoutData(rd);
155         _remAllButton.addSelectionListener(buttonListener);
156 
157         gd = new GridData(GridData.FILL_BOTH);
158         _destViewer = new ListViewer(this);
159         _destViewer.getList().setLayoutData(gd);
160         gd.widthHint = VIEWERWIDTHHINT;
161         _destViewer.addDoubleClickListener(doubleClickListener);
162     }
163 
164     public void setSrcLabelText(String srcLabelText) {
165         _srcLabelText = srcLabelText;
166         _srcLabelWidget.setText(srcLabelText);
167     }
168 
169     public void setDestLabelText(String destLabeltext) {
170         _destLabelText = destLabeltext;
171         _destLabelWidget.setText(destLabeltext);
172     }
173 
174     public void setHeadingLabelText(String heading) {
175         _headingLabelText = heading;
176         _headingLabelWidget.setText(heading);
177     }
178 
179     public void setSrcContentProvider(IContentProvider contentProvider) {
180         _srcContentProvider = contentProvider;
181         _srcViewer.setContentProvider(contentProvider);
182         _srcViewer.addFilter(new Filter());
183     }
184 
185     public IContentProvider getSrcContentProvider() {
186         return _srcContentProvider;
187     }
188 
189     public void setDestContentProvider(IMutableContentProvider contentProvider) {
190         _destMutableCP = contentProvider;
191         _destViewer.setContentProvider(contentProvider);
192     }
193 
194     public IMutableContentProvider getDestContentProvider() {
195         return _destMutableCP;
196     }
197 
198     /**
199      * Sets the input base object for the content providers.
200      * 
201      * @param input the base object for the content providers
202      */
203     public void setInput(Object input) {
204         // first set the input on the destination
205         // this makes the filter for the source effective
206         _destViewer.setInput(input);
207         _srcViewer.setInput(input);
208     }
209 
210     public void setSrcLabelProvider(ILabelProvider labelProvider) {
211         _srcViewer.setLabelProvider(labelProvider);
212     }
213 
214     public void setDestLabelProvider(ILabelProvider labelProvider) {
215         _destViewer.setLabelProvider(labelProvider);
216     }
217 
218     public ListViewer getSrcListViewer() {
219         return _srcViewer;
220     }
221 
222     public ListViewer getDestListViewer() {
223         return _destViewer;
224     }
225 
226     private void addAll() {
227         Object[] all = ((IStructuredContentProvider) _srcViewer.getContentProvider()).getElements(null);
228         for (int i = 0; i < all.length; i++) {
229             _destMutableCP.addToDest(all[i]);
230         }
231         _destViewer.refresh();
232         _srcViewer.refresh();
233     }
234 
235     private void remAll() {
236         Object[] all = _destMutableCP.getElements(null);
237         for (int i = 0; i < all.length; i++) {
238             _destMutableCP.remFromDest(all[i]);
239         }
240         _destViewer.refresh();
241         _srcViewer.refresh();
242     }
243 
244     private void rem() {
245         IStructuredSelection selection = (IStructuredSelection) _destViewer.getSelection();
246         if (!selection.isEmpty()) {
247             Iterator it = selection.iterator();
248             while (it.hasNext()) {
249                 Object item = it.next();
250                 _destMutableCP.remFromDest(item);
251             }
252             _destViewer.refresh();
253             _srcViewer.refresh();
254         }
255     }
256 
257     private void add() {
258         IStructuredSelection selection = (IStructuredSelection) _srcViewer.getSelection();
259         if (!selection.isEmpty()) {
260             Iterator it = selection.iterator();
261             while (it.hasNext()) {
262                 Object item = it.next();
263                 _destMutableCP.addToDest(item);
264             }
265             _destViewer.refresh();
266             _srcViewer.refresh();
267         }
268     }
269 
270     private class ButtonListener implements SelectionListener {
271 
272         /*
273          * (non-Javadoc)
274          * 
275          * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
276          */
277         public void widgetSelected(SelectionEvent selEvent) {
278             if (selEvent.getSource() == _remAllButton) {
279                 remAll();
280             } else if (selEvent.getSource() == _addAllButton) {
281                 addAll();
282             } else if (selEvent.getSource() == _remButton) {
283                 rem();
284             } else if (selEvent.getSource() == _addButton) {
285                 add();
286             }
287         }
288 
289         /*
290          * (non-Javadoc)
291          * 
292          * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
293          */
294         public void widgetDefaultSelected(SelectionEvent arg0) {
295         }
296 
297     }
298 
299     class DoubleClickListener implements IDoubleClickListener {
300 
301         /*
302          * (non-Javadoc)
303          * 
304          * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
305          */
306         public void doubleClick(DoubleClickEvent dcEvent) {
307             Object source = dcEvent.getSource();
308             if (source == _srcViewer) {
309                 add();
310             } else if (source == _destViewer) {
311                 rem();
312             }
313         }
314 
315     }
316 
317     /**
318      * Filter for filtering src removing items already in dest
319      */
320     class Filter extends ViewerFilter {
321 
322         /*
323          * (non-Javadoc)
324          * 
325          * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object,
326          * java.lang.Object)
327          */
328         public boolean select(Viewer viewer, Object parent, Object item) {
329             if (_destMutableCP != null) {
330                 return !_destMutableCP.contains(item);
331             } else {
332                 return true;
333             }
334         }
335     }
336 
337 }