View Javadoc

1   /*
2    *  File: DefaultAutoFilter.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.filter;
12  
13  import java.util.Set;
14  
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.custom.CCombo;
17  import org.eclipse.swt.events.SelectionEvent;
18  import org.eclipse.swt.events.SelectionListener;
19  import org.eclipse.swt.widgets.Control;
20  
21  import de.jaret.util.ui.table.model.IColumn;
22  import de.jaret.util.ui.table.model.IRow;
23  
24  /***
25   * Default implementation of the IAutofilter interface rendering a combobox with a simple selection mechanism.
26   * 
27   * @author kliem
28   * @version $Id: DefaultAutoFilter.java 389 2007-04-30 14:06:51Z olk $
29   */
30  public class DefaultAutoFilter extends AbstractAutoFilter implements IAutoFilter, SelectionListener {
31      /*** maximal length of an object list in a autofilter combox (-1 for unlimited). */
32      private static final int MAX_AUTOFILTERLENGTH = -1;
33  
34      /*** text: to be externalized. */
35      protected static final String TEXT_FILTER_ALL = "(all)";
36      /*** text: to be externalized. */
37      protected static final String TEXT_FILTER_EMPTY = "(empty)";
38      /*** text: to be externalized. */
39      protected static final String TEXT_FILTER_NONEMPTY = "(non-empty)";
40  
41      /*** control used for the filter -> coombobox. */
42      protected CCombo _combo;
43  
44      /***
45       * {@inheritDoc}
46       */
47      public void dispose() {
48          if (_combo != null) {
49              _combo.dispose();
50          }
51      }
52  
53      /***
54       * {@inheritDoc}
55       */
56      public Control getControl() {
57          return _combo;
58      }
59  
60      /***
61       * {@inheritDoc}
62       */
63      public boolean isInResult(IRow row) {
64          String filter = _combo.getText();
65          Object value = _column.getValue(row);
66          String valString = value != null ? value.toString() : "";
67          if (!filter.equals(TEXT_FILTER_ALL)) {
68              if (filter.equals(TEXT_FILTER_EMPTY) && valString.trim().length() > 0) {
69                  return false;
70              }
71              if (filter.equals(TEXT_FILTER_NONEMPTY) && valString.trim().length() == 0) {
72                  return false;
73              }
74              if (!filter.equals(TEXT_FILTER_ALL) && !filter.equals(TEXT_FILTER_EMPTY)
75                      && !filter.equals(TEXT_FILTER_NONEMPTY)) {
76                  if (!filter.equals(valString)) {
77                      return false;
78                  }
79              }
80          }
81          return true;
82      }
83  
84      /***
85       * {@inheritDoc}
86       */
87      public void update() {
88          if (_combo == null) {
89              _combo = new CCombo(_table, SWT.BORDER | SWT.READ_ONLY);
90              _combo.addSelectionListener(this);
91          }
92          IColumn col = _column;
93  
94          Set<String> colFilterStrings = getColFilterStrings(col, MAX_AUTOFILTERLENGTH);
95  
96          String[] items = new String[colFilterStrings.size() + 3];
97          int idx = 0;
98          items[idx++] = TEXT_FILTER_ALL;
99          items[idx++] = TEXT_FILTER_EMPTY;
100         items[idx++] = TEXT_FILTER_NONEMPTY;
101         for (String s : colFilterStrings) {
102             items[idx++] = s;
103         }
104 
105         _combo.setItems(items);
106         _combo.select(0);
107     }
108 
109     /***
110      * {@inheritDoc}
111      */
112     public void reset() {
113         _combo.select(0);
114         firePropertyChange("FILTER", null, "x");
115     }
116     
117     /***
118      * {@inheritDoc}
119      */
120     public void widgetDefaultSelected(SelectionEvent e) {
121     }
122 
123     /***
124      * {@inheritDoc} Inform everyone the filter changed.
125      */
126     public void widgetSelected(SelectionEvent e) {
127         firePropertyChange("FILTER", null, "x");
128     }
129 
130 }