1   /*
2    * Created on Aug 19, 2007
3    * 
4    * Copyright 2005 CafeSip.org 
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    *
10   *	http://www.apache.org/licenses/LICENSE-2.0 
11   *
12   * Unless required by applicable law or agreed to in writing, software 
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License.
17   *
18   */
19  package org.cafesip.gwtcomp.examples.client;
20  
21  import org.cafesip.gwtcomp.client.ui.Audio;
22  import org.cafesip.gwtcomp.client.ui.ColumnProperty;
23  import org.cafesip.gwtcomp.client.ui.SuperTable;
24  import org.cafesip.gwtcomp.client.ui.SuperTableListener;
25  import org.cafesip.gwtcomp.client.ui.SuperTableProperty;
26  import org.cafesip.gwtcomp.client.ui.TitleBar;
27  import org.cafesip.gwtcomp.client.utils.HTMLHelper;
28  import org.cafesip.gwtcomp.client.utils.NumericComparator;
29  import org.cafesip.gwtcomp.client.utils.StringComparator;
30  
31  import com.google.gwt.event.dom.client.ClickEvent;
32  import com.google.gwt.event.dom.client.ClickHandler;
33  import com.google.gwt.event.dom.client.KeyCodes;
34  import com.google.gwt.event.dom.client.KeyPressEvent;
35  import com.google.gwt.event.dom.client.KeyPressHandler;
36  import com.google.gwt.user.client.Window;
37  import com.google.gwt.user.client.ui.FlowPanel;
38  import com.google.gwt.user.client.ui.HTML;
39  import com.google.gwt.user.client.ui.HasHorizontalAlignment;
40  import com.google.gwt.user.client.ui.HorizontalPanel;
41  import com.google.gwt.user.client.ui.Image;
42  import com.google.gwt.user.client.ui.Label;
43  import com.google.gwt.user.client.ui.TextBox;
44  import com.google.gwt.user.client.ui.Widget;
45  
46  /**
47   * @author Amit Chatterjee
48   * 
49   */
50  public class SuperTableSample extends FlowPanel
51  {
52      private static final String SELECT_COLUMN = "Select Column";
53  
54      private SuperTable table;
55  
56      private Label selectedCellLabel;
57  
58      private TextBox selectedCellText;
59  
60      private int selectedCellRow = -1;
61  
62      private int selectedCellCol = -1;
63  
64      private int index = 1;
65  
66      private Image del;
67  
68      /**
69       * A constructor for this class.
70       * 
71       * 
72       */
73      public SuperTableSample()
74      {
75          super();
76          setWidth("100%");
77  
78          init();
79      }
80  
81      protected void init()
82      {
83          TitleBar superTableTitle = new TitleBar("Super Table", null, 1);
84          add(superTableTitle);
85  
86          add(new HTML("<p>"));
87  
88          table = new SuperTable();
89          add(table);
90          initListener();
91  
92          initTableHeaders();
93  
94          SuperTableProperty sp = new SuperTableProperty();
95          sp.setCommandBarEnabled(true);
96          sp.setPagingEnabled(true);
97          sp.setRecordsPerPage(25);
98          sp.setRowSelectionEnabled(true);
99          sp.setCellSelectionEnabled(true);
100         sp.setColumnSelectorEnabled(true);
101         sp.setToolbarEnabled(true);
102         table.setTableProperty(sp);
103 
104         table.setWidth("100%");
105 
106         initHeaderRow();
107 
108         initUserBar();
109 
110         initTableContent();
111     }
112 
113     private void initListener()
114     {
115         table.addTableListener(new SuperTableListener()
116         {
117             public void cellSelected(boolean selected, int row, int col,
118                     Widget column)
119             {
120                 if (selected == false)
121                 {
122                     selectedCellRow = -1;
123                     selectedCellCol = -1;
124                     setInitialCellEdit();
125                     return;
126                 }
127 
128                 if (col == 0)
129                 {
130                     Audio.getInstance().playWarning();
131                     setInitialCellEdit();
132                     table.setCellSelected(row, col, false);
133                     return;
134                 }
135 
136                 selectedCellCol = col;
137                 selectedCellRow = row;
138                 HTML content = (HTML) column;
139                 String heading = ((HTML) ((Widget[]) table.getTableContent()
140                         .get(row))[0]).getText()
141                         + " : " + table.getColumnsProperty()[col].getHeader();
142                 selectedCellLabel.setText(heading);
143                 selectedCellText.setText(content.getHTML());
144                 selectedCellText.setEnabled(true);
145                 selectedCellText.setFocus(true);
146             }
147 
148             public void rowSelected(boolean selected, int row, Widget[] columns)
149             {
150                 del.setVisible(table.getSelectedRowIndices().length > 0 ? true
151                         : false);
152             }
153         });
154     }
155 
156     private void initHeaderRow()
157     {
158         HorizontalPanel panel = new HorizontalPanel();
159         table.addUserHeader(panel);
160         panel.setStyleName("gwtcomp-SuperTableHeader");
161         panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
162 
163         selectedCellLabel = new Label();
164         panel.add(selectedCellLabel);
165         selectedCellText = new TextBox();
166         panel.add(selectedCellText);
167         selectedCellText.setVisibleLength(100);
168         setInitialCellEdit();
169 
170         selectedCellText.addKeyPressHandler(new KeyPressHandler()
171         {
172             public void onKeyPress(KeyPressEvent event)
173             {
174                 if (event.getCharCode() != KeyCodes.KEY_ENTER)
175                 {
176                     return;
177                 }
178 
179                 if ((selectedCellCol < 0) || (selectedCellRow < 0))
180                 {
181                     Audio.getInstance().playWarning();
182                     return;
183                 }
184 
185                 if ((selectedCellRow >= table.getFirstRowIndex())
186                         && (selectedCellRow < table.getFirstRowIndex()
187                                 + table.getCurrentPageRecordCount()))
188                 {
189                     // Only allow the change if the row is visible
190                     HTML l = new HTML(selectedCellText.getText());
191                     table.setCell(l, selectedCellRow, selectedCellCol);
192                     selectedCellText.setText("");
193                 }
194                 else
195                 {
196                     Audio.getInstance().playError();
197                 }
198 
199             }
200         });
201     }
202 
203     private void initUserBar()
204     {
205         Image help = new Image("gwtcomp-icons/help.png");
206         help.setTitle("I added my own icons on the user bar");
207         table.addToUserToolbar(help);
208 
209         Image add = new Image("gwtcomp-icons/edit_add.png");
210         add.setTitle("Add an empty row");
211         table.addToUserToolbar(add);
212 
213         add.addClickHandler(new ClickHandler()
214         {
215 
216             public void onClick(ClickEvent event)
217             {
218                 table.addRow(new Widget[] {
219                         new HTML(HTMLHelper.bold("Company-" + (index++))),
220                         new HTML(""), new HTML("") });
221 
222             }
223         });
224 
225         del = new Image("gwtcomp-icons/editdelete.png");
226         del.setTitle("Delete selected rows");
227         table.addToUserToolbar(del);
228 
229         del.addClickHandler(new ClickHandler()
230         {
231 
232             public void onClick(ClickEvent event)
233             {
234                 if (Window
235                         .confirm("Are you sure you want to delete the selected row(s)?"))
236                 {
237                     int index = -1;
238                     while ((index = table.getSelectedRowIndex()) >= 0)
239                     {
240                         table.deleteRow(index);
241                     }
242                 }
243             }
244         });
245 
246     }
247 
248     private void initTableContent()
249     {
250         table.addRow(new Widget[] { new HTML(HTMLHelper.bold("Super Systems")),
251                 new HTML("San Diego<br>California"), new HTML("1500.59") });
252         table.addRow(new Widget[] {
253                 new HTML(HTMLHelper.bold("Fancy Auto Parts")),
254                 new HTML("Sioux Falls<br>South Dakota"), new HTML("2798.91") });
255         table
256                 .addRow(
257                         new Widget[] { new HTML(HTMLHelper.bold("CafeSip")),
258                                 new HTML("Raleigh<br>North Carolina"),
259                                 new HTML("0.00") }, true);
260     }
261 
262     private void initTableHeaders()
263     {
264         ColumnProperty[] columns = new ColumnProperty[3];
265 
266         columns[0] = new ColumnProperty("Company Name", new StringComparator(0,
267                 true), new StringComparator(0, false));
268         columns[1] = new ColumnProperty("Location", new StringComparator(1,
269                 true), new StringComparator(1, false));
270         columns[2] = new ColumnProperty("Annual Revenue (Millions)",
271                 new NumericComparator(2, true), new NumericComparator(2, false));
272         columns[2].setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
273         table.setColumnsProperty(columns);
274     }
275 
276     private void setInitialCellEdit()
277     {
278         selectedCellLabel.setText(SELECT_COLUMN);
279         selectedCellText.setText("");
280         selectedCellText.setEnabled(false);
281     }
282 }