View Javadoc

1   /*
2    *  File: ConfigureColumnsAction.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.table.util.action;
12  
13  import java.util.ArrayList;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.eclipse.jface.action.Action;
19  import org.eclipse.jface.dialogs.Dialog;
20  import org.eclipse.jface.viewers.CheckboxTableViewer;
21  import org.eclipse.jface.viewers.ILabelProviderListener;
22  import org.eclipse.jface.viewers.IStructuredContentProvider;
23  import org.eclipse.jface.viewers.ITableLabelProvider;
24  import org.eclipse.jface.viewers.Viewer;
25  import org.eclipse.swt.SWT;
26  import org.eclipse.swt.events.SelectionEvent;
27  import org.eclipse.swt.events.SelectionListener;
28  import org.eclipse.swt.graphics.Image;
29  import org.eclipse.swt.layout.RowLayout;
30  import org.eclipse.swt.widgets.Button;
31  import org.eclipse.swt.widgets.Composite;
32  import org.eclipse.swt.widgets.Control;
33  import org.eclipse.swt.widgets.Display;
34  import org.eclipse.swt.widgets.Event;
35  import org.eclipse.swt.widgets.Label;
36  import org.eclipse.swt.widgets.Listener;
37  import org.eclipse.swt.widgets.Table;
38  import org.eclipse.swt.widgets.TableColumn;
39  import org.eclipse.swt.widgets.TableItem;
40  
41  import de.jaret.util.ui.table.JaretTable;
42  import de.jaret.util.ui.table.model.IColumn;
43  import de.jaret.util.ui.table.model.ITableViewState;
44  
45  /***
46   * Action for configuring column display. Showing a dialog to reorder and change the visibility of rows. The action can
47   * be parametrized to disallow manipulating the positions and visibility of fixed columns. The table will be
48   * manipoulated instantly. Values will be saved to allow cancelling the configuration.
49   * 
50   * @author Peter Kliem
51   * @version $Id: ConfigureColumnsAction.java 355 2007-04-09 13:54:05Z olk $
52   */
53  public class ConfigureColumnsAction extends Action {
54      /*** table the action is operating on. */
55      protected JaretTable _table;
56      /*** the table viewstate. */
57      protected ITableViewState _tvs;
58      /*** checkbox table viewer used to display the columns. */
59      protected CheckboxTableViewer _chkBoxViewer;
60      /*** saved order for doing a proper cancel operation. */
61      protected List<IColumn> _saveOrder;
62      /*** saved visibility for the columns for proper cancel action. */
63      protected Map<IColumn, Boolean> _saveVisibility;
64      /*** if true fixed columns can be shifted or changed in visibility. */
65      protected boolean _allowFixedColumns;
66  
67      /***
68       * Construct the action.
69       * 
70       * @param table table to operate on
71       * @param allowFixedColumns if true fixed columns can be changed in visibility and position (moving them out of the
72       * fixed position)
73       */
74      public ConfigureColumnsAction(JaretTable table, boolean allowFixedColumns) {
75          setTable(table);
76          _allowFixedColumns = allowFixedColumns;
77      }
78  
79      /***
80       * Construct the action (allowFixedColumns defaults to true).
81       * 
82       * @param table table to operate on
83       */
84      public ConfigureColumnsAction(JaretTable table) {
85          this(table, true);
86      }
87  
88      /***
89       * Set the table to operate on.
90       * 
91       * @param table table
92       */
93      public void setTable(JaretTable table) {
94          _table = table;
95          _tvs = _table.getTableViewState();
96      }
97  
98      /***
99       * {@inheritDoc}
100      */
101     public void run() {
102         save();
103 
104         Dialog confColsDialog = new Dialog(Display.getCurrent().getActiveShell()) {
105             @Override
106             protected Control createDialogArea(Composite parent) {
107                 return createColumnControlPanel(parent);
108             }
109 
110         };
111         int result = confColsDialog.open();
112         if (result == Dialog.CANCEL) {
113             restore();
114         }
115     }
116 
117     /***
118      * Save the current properties of the viewstate.
119      */
120     private void save() {
121         _saveOrder = new ArrayList<IColumn>();
122         _saveOrder.addAll(_tvs.getSortedColumns());
123         _saveVisibility = new HashMap<IColumn, Boolean>();
124         for (int i = 0; i < _table.getTableModel().getColumnCount(); i++) {
125             IColumn col = _table.getTableModel().getColumn(i);
126             _saveVisibility.put(col, _tvs.getColumnVisible(col));
127         }
128     }
129 
130     /***
131      * Restore viewstate to previously saved state.
132      */
133     private void restore() {
134         _tvs.setSortedColumns(_saveOrder);
135         for (int i = 0; i < _table.getTableModel().getColumnCount(); i++) {
136             IColumn col = _table.getTableModel().getColumn(i);
137             boolean visible = _saveVisibility.get(col);
138             _tvs.setColumnVisible(col, visible);
139         }
140 
141     }
142 
143     /***
144      * {@inheritDoc}
145      */
146     public String getText() {
147         return "Configure columns";
148     }
149 
150     /***
151      * Create the dialog area. TODO can be done much nicer ... but works for the first draft
152      * 
153      * @param parent parent composite
154      * @return initialized control
155      */
156     private Control createColumnControlPanel(Composite parent) {
157         Composite panel = new Composite(parent, SWT.NULL);
158         panel.setLayout(new RowLayout());
159 
160         Label l = new Label(panel, SWT.NULL);
161         l.setText("Configure the columns");
162 
163         Table table = new Table(parent, SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL);
164         _chkBoxViewer = new CheckboxTableViewer(table);
165         _chkBoxViewer.setContentProvider(new ColTableContentProvider());
166         _chkBoxViewer.setLabelProvider(new ColTableLabelProvider());
167 
168         TableColumn column = new TableColumn(_chkBoxViewer.getTable(), SWT.LEFT);
169         column.setText("Column");
170         column.setWidth(100);
171 
172         _chkBoxViewer.getTable().setHeaderVisible(true);
173         _chkBoxViewer.setInput("x");
174 
175         final int firstColIdx = _allowFixedColumns ? 0 : _table.getFixedColumns();
176 
177         for (int i = 0; i < _table.getTableModel().getColumnCount(); i++) {
178             IColumn col = _table.getTableModel().getColumn(i);
179             _chkBoxViewer.setChecked(col, _tvs.getColumnVisible(col));
180         }
181 
182         table.getColumn(0).pack();
183 
184         table.addListener(SWT.Selection, new Listener() {
185             public void handleEvent(Event event) {
186                 if (event.detail == SWT.CHECK) {
187                     TableItem item = (TableItem) event.item;
188                     IColumn col = (IColumn) item.getData();
189                     int idx = _tvs.getSortedColumns().indexOf(col);
190                     if (_allowFixedColumns || idx >= _table.getFixedColumns()) {
191                         _tvs.setColumnVisible(col, item.getChecked());
192                     } else {
193                         _chkBoxViewer.setChecked(col, _tvs.getColumnVisible(col));
194                     }
195                 }
196             }
197         });
198 
199         Button upButton = new Button(panel, SWT.PUSH);
200         upButton.setText("up");
201         upButton.addSelectionListener(new SelectionListener() {
202 
203             public void widgetSelected(SelectionEvent arg0) {
204                 if (_chkBoxViewer.getTable().getSelectionCount() > 0) {
205                     TableItem item = _chkBoxViewer.getTable().getItem(_chkBoxViewer.getTable().getSelectionIndex());
206                     IColumn col = (IColumn) item.getData();
207                     int idx = _tvs.getSortedColumns().indexOf(col);
208                     if (idx > firstColIdx) {
209                         _tvs.getSortedColumns().remove(col);
210                         _tvs.getSortedColumns().add(idx - 1, col);
211                         _table.updateColumnList();
212                         _table.redraw();
213                         _chkBoxViewer.refresh();
214                     }
215                 }
216             }
217 
218             public void widgetDefaultSelected(SelectionEvent arg0) {
219             }
220 
221         });
222         Button downButton = new Button(panel, SWT.PUSH);
223         downButton.setText("down");
224         downButton.addSelectionListener(new SelectionListener() {
225 
226             public void widgetSelected(SelectionEvent arg0) {
227                 if (_chkBoxViewer.getTable().getSelectionCount() > 0) {
228                     TableItem item = _chkBoxViewer.getTable().getItem(_chkBoxViewer.getTable().getSelectionIndex());
229                     IColumn col = (IColumn) item.getData();
230                     int idx = _tvs.getSortedColumns().indexOf(col);
231                     if (idx < _tvs.getSortedColumns().size() - 1) {
232                         _tvs.getSortedColumns().remove(col);
233                         _tvs.getSortedColumns().add(idx + 1, col);
234                         _table.updateColumnList();
235                         _table.redraw();
236                         _chkBoxViewer.refresh();
237                     }
238                 }
239             }
240 
241             public void widgetDefaultSelected(SelectionEvent arg0) {
242             }
243 
244         });
245 
246         return panel;
247     }
248 
249     /***
250      * Content provider for the table viewer.
251      * 
252      * @author kliem
253      * @version $Id: ConfigureColumnsAction.java 355 2007-04-09 13:54:05Z olk $
254      */
255     public class ColTableContentProvider implements IStructuredContentProvider {
256         /***
257          * {@inheritDoc}
258          */
259         public Object[] getElements(Object element) {
260             Object[] kids = null;
261             java.util.List l = _table.getTableViewState().getSortedColumns();
262             kids = l.toArray();
263             return kids;
264         }
265 
266         /***
267          * {@inheritDoc}
268          */
269         public void dispose() {
270         }
271 
272         /***
273          * {@inheritDoc}
274          */
275         public void inputChanged(Viewer viewer, Object oldObject, Object newObject) {
276         }
277     }
278 
279     /***
280      * Labelprovider for the table viewer.
281      * 
282      * @author kliem
283      * @version $Id: ConfigureColumnsAction.java 355 2007-04-09 13:54:05Z olk $
284      */
285     public class ColTableLabelProvider implements ITableLabelProvider {
286         /***
287          * {@inheritDoc}
288          */
289         public String getColumnText(Object obj, int i) {
290             String result;
291             IColumn column = (IColumn) obj;
292             int idx = _tvs.getSortedColumns().indexOf(column);
293             switch (i) {
294             case 0:
295                 result = column.getHeaderLabel();
296                 if (!_allowFixedColumns && idx < _table.getFixedColumns()) {
297                     result+="(fixed)";
298                 }
299                 break;
300             default:
301                 result = "error - unknow column";
302                 break;
303             }
304             return result;
305         }
306 
307         /***
308          * {@inheritDoc}
309          */
310         public void addListener(ILabelProviderListener ilabelproviderlistener) {
311         }
312 
313         /***
314          * {@inheritDoc}
315          */
316         public void dispose() {
317         }
318 
319         /***
320          * {@inheritDoc}
321          */
322         public boolean isLabelProperty(Object obj, String s) {
323             return false;
324         }
325 
326         /***
327          * {@inheritDoc}
328          */
329         public void removeListener(ILabelProviderListener ilabelproviderlistener) {
330         }
331 
332         /***
333          * {@inheritDoc}
334          */
335         public Image getColumnImage(Object arg0, int arg1) {
336             return null;
337         }
338     }
339 
340 }