SuperTableSample.java
001 /*
002  * Created on Aug 19, 2007
003  
004  * Copyright 2005 CafeSip.org 
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License"); 
007  * you may not use this file except in compliance with the License. 
008  * You may obtain a copy of the License at 
009  *
010  *  http://www.apache.org/licenses/LICENSE-2.0 
011  *
012  * Unless required by applicable law or agreed to in writing, software 
013  * distributed under the License is distributed on an "AS IS" BASIS, 
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
015  * See the License for the specific language governing permissions and 
016  * limitations under the License.
017  *
018  */
019 package org.cafesip.gwtcomp.examples.client;
020 
021 import org.cafesip.gwtcomp.client.ui.Audio;
022 import org.cafesip.gwtcomp.client.ui.ColumnProperty;
023 import org.cafesip.gwtcomp.client.ui.SuperTable;
024 import org.cafesip.gwtcomp.client.ui.SuperTableListener;
025 import org.cafesip.gwtcomp.client.ui.SuperTableProperty;
026 import org.cafesip.gwtcomp.client.ui.TitleBar;
027 import org.cafesip.gwtcomp.client.utils.HTMLHelper;
028 import org.cafesip.gwtcomp.client.utils.NumericComparator;
029 import org.cafesip.gwtcomp.client.utils.StringComparator;
030 
031 import com.google.gwt.user.client.Window;
032 import com.google.gwt.user.client.ui.ClickListener;
033 import com.google.gwt.user.client.ui.FlowPanel;
034 import com.google.gwt.user.client.ui.HTML;
035 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
036 import com.google.gwt.user.client.ui.HorizontalPanel;
037 import com.google.gwt.user.client.ui.Image;
038 import com.google.gwt.user.client.ui.KeyboardListener;
039 import com.google.gwt.user.client.ui.Label;
040 import com.google.gwt.user.client.ui.TextBox;
041 import com.google.gwt.user.client.ui.Widget;
042 
043 /**
044  @author Amit Chatterjee
045  
046  */
047 public class SuperTableSample extends FlowPanel
048 {
049     private static final String SELECT_COLUMN = "Select Column";
050 
051     private SuperTable table;
052 
053     private Label selectedCellLabel;
054 
055     private TextBox selectedCellText;
056 
057     private int selectedCellRow = -1;
058 
059     private int selectedCellCol = -1;
060 
061     private int index = 1;
062 
063     private Image del;
064 
065     /**
066      * A constructor for this class.
067      
068      
069      */
070     public SuperTableSample()
071     {
072         super();
073         setWidth("100%");
074 
075         init();
076     }
077 
078     protected void init()
079     {
080         TitleBar superTableTitle = new TitleBar("Super Table", null, 1);
081         add(superTableTitle);
082 
083         add(new HTML("<p>"));
084         
085         table = new SuperTable();
086         add(table);
087         initListener();
088 
089         initTableHeaders();
090 
091         SuperTableProperty sp = new SuperTableProperty();
092         sp.setCommandBarEnabled(true);
093         sp.setPagingEnabled(true);
094         sp.setRecordsPerPage(25);
095         sp.setRowSelectionEnabled(true);
096         sp.setCellSelectionEnabled(true);
097         sp.setColumnSelectorEnabled(true);
098         sp.setToolbarEnabled(true);
099         table.setTableProperty(sp);
100 
101         table.setWidth("100%");
102 
103         initHeaderRow();
104 
105         initUserBar();
106 
107         initTableContent();
108     }
109 
110     private void initListener()
111     {
112         table.addTableListener(new SuperTableListener()
113         {
114 
115             public void cellSelected(boolean selected, int row, int col,
116                     Widget column)
117             {
118                 if (selected == false)
119                 {
120                     selectedCellRow = -1;
121                     selectedCellCol = -1;
122                     setInitialCellEdit();
123                     return;
124                 }
125 
126                 if (col == 0)
127                 {
128                     Audio.getInstance().playWarning();
129                     setInitialCellEdit();
130                     table.setCellSelected(row, col, false);
131                     return;
132                 }
133 
134                 selectedCellCol = col;
135                 selectedCellRow = row;
136                 Label content = (Labelcolumn;
137                 String heading = ((HTML) ((Widget[]) table.getTableContent()
138                         .get(row))[0]).getText()
139                         " : " + table.getColumnsProperty()[col].getHeader();
140                 selectedCellLabel.setText(heading);
141                 selectedCellText.setText(content.getText());
142                 selectedCellText.setEnabled(true);
143                 selectedCellText.setFocus(true);
144             }
145 
146             public void rowSelected(boolean selected, int row, Widget[] columns)
147             {
148                 del.setVisible(table.getSelectedRowIndices().length > true
149                         false);
150             }
151         });
152     }
153 
154     private void initHeaderRow()
155     {
156         HorizontalPanel panel = new HorizontalPanel();
157         table.addUserHeader(panel);
158         panel.setStyleName("gwtcomp-SuperTableHeader");
159         panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
160 
161         selectedCellLabel = new Label();
162         panel.add(selectedCellLabel);
163         selectedCellText = new TextBox();
164         panel.add(selectedCellText);
165         selectedCellText.setVisibleLength(100);
166         setInitialCellEdit();
167 
168         selectedCellText.addKeyboardListener(new KeyboardListener()
169         {
170 
171             public void onKeyDown(Widget sender, char keyCode, int modifiers)
172             {
173             }
174 
175             public void onKeyPress(Widget sender, char keyCode, int modifiers)
176             {
177                 if (keyCode != KeyboardListener.KEY_ENTER)
178                 {
179                     return;
180                 }
181 
182                 if ((selectedCellCol < 0|| (selectedCellRow < 0))
183                 {
184                     Audio.getInstance().playWarning();
185                     return;
186                 }
187 
188                 if ((selectedCellRow >= table.getFirstRowIndex())
189                         && (selectedCellRow < table.getFirstRowIndex()
190                                 + table.getCurrentPageRecordCount()))
191                 {
192                     // Only allow the change if the row is visible
193                     Label l = new Label(selectedCellText.getText());
194                     table.setCell(l, selectedCellRow, selectedCellCol);
195                     selectedCellText.setText("");
196                 }
197                 else
198                 {
199                     Audio.getInstance().playError();
200                 }
201             }
202 
203             public void onKeyUp(Widget sender, char keyCode, int modifiers)
204             {
205             }
206         });
207     }
208 
209     private void initUserBar()
210     {
211         Image help = new Image("gwtcomp-icons/help.png");
212         help.setTitle("I added my own icons on the user bar");
213         table.addToUserToolbar(help);
214 
215         Image add = new Image("gwtcomp-icons/edit_add.png");
216         add.setTitle("Add an empty row");
217         table.addToUserToolbar(add);
218         add.addClickListener(new ClickListener()
219         {
220             public void onClick(Widget sender)
221             {
222                 table.addRow(new Widget[] {
223                         new HTML(HTMLHelper.bold("Company-" (index++))),
224                         new Label("")new Label("") });
225             }
226         });
227 
228         del = new Image("gwtcomp-icons/editdelete.png");
229         del.setTitle("Delete selected rows");
230         table.addToUserToolbar(del);
231         del.addClickListener(new ClickListener()
232         {
233             public void onClick(Widget sender)
234             {
235                 if (Window
236                         .confirm("Are you sure you want to delete the selected row(s)?"))
237                 {
238                     int index = -1;
239                     while ((index = table.getSelectedRowIndex()) >= 0)
240                     {
241                         table.deleteRow(index);
242                     }
243                 }
244             }
245         });
246 
247     }
248 
249     private void initTableContent()
250     {
251         table.addRow(new Widget[] { new HTML(HTMLHelper.bold("Super Systems")),
252                 new Label("California")new Label("1500.59") });
253         table.addRow(new Widget[] {
254                 new HTML(HTMLHelper.bold("Fancy Auto Parts")),
255                 new Label("South Dakota")new Label("2798.91") });
256         table.addRow(new Widget[] { new HTML(HTMLHelper.bold("CafeSip")),
257                 new Label("North Carolina")new Label("0.00") }true);
258     }
259 
260     private void initTableHeaders()
261     {
262         ColumnProperty[] columns = new ColumnProperty[3];
263 
264         columns[0new ColumnProperty("Company Name"new StringComparator(0,
265                 true)new StringComparator(0false));
266         columns[1new ColumnProperty("Location"new StringComparator(1,
267                 true)new StringComparator(1false));
268         columns[2new ColumnProperty("Annual Revenue (Millions)",
269                 new NumericComparator(2true)new NumericComparator(2false));
270 
271         table.setColumnsProperty(columns);
272     }
273 
274     private void setInitialCellEdit()
275     {
276         selectedCellLabel.setText(SELECT_COLUMN);
277         selectedCellText.setText("");
278         selectedCellText.setEnabled(false);
279     }
280 }