Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/datatables/FilterManager.java

Diff revisions: vs.
  @@ -1,21 +1,19 @@
1 1 // This Source Code is in the Public Domain per: http://unlicense.org
2 2 package org.litesoft.GWT.client.widgets.datatables;
3 3
4 - import org.litesoft.commonfoundation.typeutils.Objects;
5 - import java.util.*;
6 -
7 4 import org.litesoft.bo.*;
8 5 import org.litesoft.bo.views.*;
6 + import org.litesoft.commonfoundation.typeutils.Objects;
9 7 import org.litesoft.core.util.*;
10 8
11 - public abstract class FilterManager<RowType extends IViewObject> implements FetchRowsDataProviderCallBack<RowType>
12 - {
9 + import java.util.*;
10 +
11 + public abstract class FilterManager<RowType extends IViewObject> implements FetchRowsDataProviderCallBack<RowType> {
13 12 public static final String TOO_MANY_ROWS_MATCHED = "Too Many Rows Matched, Found At Least: ";
14 13 public static final String NO_ROWS_MATCHED = "No Rows Matched";
15 14 public static final String NOTHING_ENTERED = "You must enter something";
16 15
17 - public interface CallBacks<OurRowType extends IViewObject>
18 - {
16 + public interface CallBacks<OurRowType extends IViewObject> {
19 17 public ImmutableArrayList<OurRowType> filterRows( ImmutableArrayList<OurRowType> pRows );
20 18
21 19 public void setNewList( List<OurRowType> pList, String pMsgText );
  @@ -28,85 +26,68 @@
28 26 private List<RowType> mBaseLineRows = null;
29 27 private int mVsFilters, mVsList;
30 28
31 - public FilterManager( BoMetaData pMetaData, CallBacks<RowType> pCallBacks )
32 - {
29 + public FilterManager( BoMetaData pMetaData, CallBacks<RowType> pCallBacks ) {
33 30 Objects.assertNotNull( "MetaData", mMetaData = pMetaData );
34 31 Objects.assertNotNull( "CallBacks", mCallBacks = pCallBacks );
35 32 }
36 33
37 - public void clearCache()
38 - {
34 + public void clearCache() {
39 35 mBaseLineFilters = null;
40 36 mBaseLineRows = null;
41 37 }
42 38
43 - public boolean clear()
44 - {
39 + public boolean clear() {
45 40 boolean rv = !mFilters.isEmpty();
46 - if ( rv )
47 - {
41 + if ( rv ) {
48 42 mFilters.clear();
49 43 mVsFilters++;
50 44 }
51 45 return rv;
52 46 }
53 47
54 - public boolean setFilter( BoAccessorFilterType pFilterType, String pAttributeName, String pValue )
55 - {
48 + public boolean setFilter( BoAccessorFilterType pFilterType, String pAttributeName, String pValue ) {
56 49 boolean zAdded = mFilters.setFilter( pFilterType, pAttributeName, pValue );
57 - if ( zAdded )
58 - {
50 + if ( zAdded ) {
59 51 mVsFilters++;
60 52 }
61 53 return zAdded;
62 54 }
63 55
64 - public boolean isEmpty()
65 - {
56 + public boolean isEmpty() {
66 57 return mFilters.isEmpty();
67 58 }
68 59
69 - public int activeFilters()
70 - {
60 + public int activeFilters() {
71 61 return mFilters.activeFilters();
72 62 }
73 63
74 64 /**
75 65 * @return false if the tables data model is up to date!
76 66 */
77 - public boolean updateList()
78 - {
67 + public boolean updateList() {
79 68 if ( mVsFilters == mVsList ) // Filter & List Match!
80 69 {
81 70 return false;
82 71 }
83 - if ( mFilters.isEmpty() )
84 - {
72 + if ( mFilters.isEmpty() ) {
85 73 setNewList( new ArrayList<RowType>(), NOTHING_ENTERED );
86 - }
87 - else if ( mPendingRequest == null )
88 - {
74 + } else if ( mPendingRequest == null ) {
89 75 requestRows();
90 76 }
91 77 return true;
92 78 }
93 79
94 - private void setNewList( List<RowType> pList, String pMsgText )
95 - {
80 + private void setNewList( List<RowType> pList, String pMsgText ) {
96 81 mVsList = mVsFilters;
97 82 mCallBacks.setNewList( pList, pMsgText );
98 83 }
99 84
100 - private void requestRows()
101 - {
102 - if ( (mBaseLineFilters != null) && !FilterUtil.needsRefresh( mMetaData, mBaseLineFilters, mFilters ) )
103 - {
85 + private void requestRows() {
86 + if ( (mBaseLineFilters != null) && !FilterUtil.needsRefresh( mMetaData, mBaseLineFilters, mFilters ) ) {
104 87 Filter zFilter = FilterUtil.createFilter( mMetaData, mFilters.getFilters() );
105 88 ArrayList<RowType> zNewList = new ArrayList<RowType>();
106 - for ( RowType zType : mBaseLineRows )
107 - {
108 - if ( zFilter.isSatisfiedBy( zType ) )
109 - {
89 + for ( RowType zType : mBaseLineRows ) {
90 + if ( zFilter.isSatisfiedBy( zType ) ) {
110 91 zNewList.add( zType );
111 92 }
112 93 }
  @@ -117,32 +98,27 @@
117 98 requestRowsFromServer( mPendingRequest.getFilters() );
118 99 }
119 100
120 - protected void addBaseline( BoAccessorFilters pFilters, List<RowType> pList )
121 - {
101 + protected void addBaseline( BoAccessorFilters pFilters, List<RowType> pList ) {
122 102 mBaseLineFilters = pFilters; // Simply save it - Not Shared/Used anymore
123 103 mBaseLineRows = new ArrayList<RowType>( pList ); // Save it - Used by others - Copy it
124 104 }
125 105
126 106 abstract protected void requestRowsFromServer( BoAccessorFilters pFilters );
127 107
128 - private static class PendingRequestStructure
129 - {
108 + private static class PendingRequestStructure {
130 109 private int mVsFilters;
131 110 private BoAccessorFilters mFilters;
132 111
133 - public PendingRequestStructure( int pVsFilters, BoAccessorFilters pFilters )
134 - {
112 + public PendingRequestStructure( int pVsFilters, BoAccessorFilters pFilters ) {
135 113 mVsFilters = pVsFilters;
136 114 mFilters = pFilters.copy(); // Snap shot Filters
137 115 }
138 116
139 - public int getVsFilters()
140 - {
117 + public int getVsFilters() {
141 118 return mVsFilters;
142 119 }
143 120
144 - public BoAccessorFilters getFilters()
145 - {
121 + public BoAccessorFilters getFilters() {
146 122 return mFilters;
147 123 }
148 124 }
  @@ -150,12 +126,10 @@
150 126 private PendingRequestStructure mPendingRequest;
151 127
152 128 private void filteredResponse( List<RowType> pList, // !null means that this could be a legitimate base line!
153 - String pMsgText )
154 - {
129 + String pMsgText ) {
155 130 PendingRequestStructure zPendingRequest = mPendingRequest; // Capture & clear the current PendingRequest
156 131 mPendingRequest = null;
157 - if ( pList != null )
158 - {
132 + if ( pList != null ) {
159 133 addBaseline( zPendingRequest.getFilters(), pList );
160 134 }
161 135 if ( mVsList == mVsFilters ) // This should only be possible if the user has cleared the filters while the response was pending
  @@ -171,27 +145,21 @@
171 145 }
172 146
173 147 @Override
174 - public void success( ImmutableArrayList<RowType> pRows )
175 - {
176 - if ( (pRows == null) || pRows.isEmpty() )
177 - {
148 + public void success( ImmutableArrayList<RowType> pRows ) {
149 + if ( (pRows == null) || pRows.isEmpty() ) {
178 150 filteredResponse( new ArrayList<RowType>(), NO_ROWS_MATCHED );
179 - }
180 - else
181 - {
151 + } else {
182 152 filteredResponse( mCallBacks.filterRows( pRows ).getSortedList(), null );
183 153 }
184 154 }
185 155
186 156 @Override
187 - public void tooMany( long pCount )
188 - {
157 + public void tooMany( long pCount ) {
189 158 filteredResponse( null, TOO_MANY_ROWS_MATCHED + pCount );
190 159 }
191 160
192 161 @Override
193 - public void error( String pError )
194 - {
162 + public void error( String pError ) {
195 163 filteredResponse( null, "Error: " + pError );
196 164 }
197 165 }