/*
* Copyright 2007 Sfeir, www.sfeir.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
public ArrayList generateArrayRows(int nbOfRows) {
ArrayList arrayRows = new ArrayList();
for (int i = 0; i < nbOfRows; i++) {
PersonRow person = new PersonRow();
person.setFirstName("first name " + i);
person.setLastName("last name " + i);
person.setBirthday(new Date(12345678l + i));
person.setPhoneNumber("012 345 67" + i);
person.setEmail("firstname" + i + ".lastname" + i + "@gmail.com");
arrayRows.add(person);
}
public static final String FIRSTNAME_PROPERTY = "first name"; public static final String LASTNAME_PROPERTY = "last name"; public static final String NAME_PROPERTY = "name"; public static final String BIRTHDAY_PROPERTY = "birthday"; public static final String PHONENUMBER_PROPERTY = "phone number"; public static final String EMAIL_PROPERTY = "e-mail";
public Object getProperty(String property) {
Object result = null; if (FIRSTNAME_PROPERTY.equals(property)) {
result = firstName;
} else if (LASTNAME_PROPERTY.equals(property)) {
result = lastName;
} else if (NAME_PROPERTY.equals(property)) {
result = firstName + " " + lastName;
} else if (BIRTHDAY_PROPERTY.equals(property)) {
result = birthday.toString();
} else if (PHONENUMBER_PROPERTY.equals(property)) {
result = phoneNumber;
} else if (EMAIL_PROPERTY.equals(property)) {
result = email;
} return result;
}
public Date getBirthday() { return birthday;
}
public String getEmail() { return email;
}
public String getFirstName() { return firstName;
}
public String getLastName() { return lastName;
}
public String getPhoneNumber() { return phoneNumber;
}
public void setBirthday(Date birthday) { this.birthday = birthday;
}
public void setEmail(String email) { this.email = email;
}
public void setFirstName(String firstName) { this.firstName = firstName;
}
public void setLastName(String lastName) { this.lastName = lastName;
}
public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber;
}
}
/**
* SimpleTable is the basic implementation of Table.
* This table should be used with small amount of data only.
*
* @author [email protected]
*/ class SimpleTable extends Table {
/**
* AbsolutePanel defining an invisible layer preventing user from selecting text from this table.
*/ private AbsolutePanel layer;
public SimpleTable() { super();
layer = new AbsolutePanel();
layer.setStyleName("transparent");
} public void draw() { if (arrayColumns != null && arrayRows != null) {
headersContainer.clear();
rowsContainer.clear();
tableWidth = 0;
protected void drawColumn(Column column) {
// The column is not drawn if not visible. if (column.isVisible()) {
// Useful variables for drawing.
HorizontalAlignmentConstant alignment = column.getAlignment();
String property = column.getProperty();
// int width = column.getWidth(); int width = calculateWidth(column); int top = 0; int left = tableWidth;
// Drawing the header.
Label header = new Label(column.getText(), false);
header.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
header.setPixelSize(width, ROWS_HEIGHT);
header.setStyleName("header");
headersContainer.add(header, left, 0);
// Drawing the rows.
Row row;
Object data; for (int rowIndex = 0; rowIndex < arrayRows.size(); rowIndex++) {
row = (Row) arrayRows.get(rowIndex);
data = row.getProperty(property);
Label cell = new Label("", false); if (data != null) {
cell.setText(data.toString());
}
cell.setHorizontalAlignment(alignment);
cell.setPixelSize(width, ROWS_HEIGHT);
cell.setStyleName("cell"); if (rowIndex%2 == 0) {
cell.addStyleName("evenRow");
} else {
cell.addStyleName("oddRow");
}
rowsContainer.add(cell, left, top);
top += ROWS_HEIGHT;
}
tableWidth += width;
}
}
}
/*
* Copyright 2007 Sfeir, www.sfeir.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/**
* Table defines a table based on GWT's AbsolutePanel.
*
* @author [email protected] and [email protected]
*/ abstract class Table extends AbsolutePanel {
/**
* int defining an average size for characters.
*/ public static int CHARACTER_SIZE;
/**
* int defining the height of rows in this table (every row has the same height)
*/ public static int ROWS_HEIGHT;
public static void setCHARACTER_SIZE(int character_size) {
CHARACTER_SIZE = character_size;
}
/**
* ArrayList defining the columns of this table.
*/ protected ArrayList arrayColumns;
/**
* ArrayList defining the content of this table.
*/ protected ArrayList arrayRows;
// headers and rows are separated for scrolling purpose.
/**
* AbsolutePanel containing the headers.
*/ protected AbsolutePanel headersContainer;
/**
* AbsolutePanel containing the rows.
*/ protected AbsolutePanel rowsContainer;
/**
* int defining the width of this table
*/ protected int tableWidth;
/**
* Creates an empty Table.
*/ public Table() { super();
CHARACTER_SIZE = 10;
ROWS_HEIGHT = CHARACTER_SIZE * 3;
headersContainer = new AbsolutePanel();
rowsContainer = new AbsolutePanel();
arrayColumns = new ArrayList();
setStyleName("table");
}
public void add(Column column) {
arrayColumns.add(column);
}
/**
* Calculates the optimal width of this table.
*/ public int calculateWidth() { int width = 0;
Column column; for (Iterator c = arrayColumns.iterator(); c.hasNext();) {
column = (Column) c.next(); if (column.isVisible()) {
width += calculateWidth(column);
}
} return width;
}
/**
* Draws the table.
*/ public abstract void draw();
public void setArrayRows(ArrayList arrayRows) { this.arrayRows = arrayRows;
}
public static void setROWS_HEIGHT(int rows_height) {
ROWS_HEIGHT = rows_height;
}
/**
* Calculates the optimal width of the specified column considering an average CHARACTER_SIZE;
*/ protected int calculateWidth(Column column) { int width = column.getText().length();
String property = column.getProperty();
Row row;
Object data; for (Iterator r = arrayRows.iterator(); r.hasNext();) {
row = (Row) r.next();
data = row.getProperty(property); if (data != null && data.toString().length() > width) {
width = data.toString().length();
}
} return width * CHARACTER_SIZE;
}
/**
* Draws the specified column of this table.
*/ protected abstract void drawColumn(Column column);
}
/*
* Copyright 2007 Sfeir, www.sfeir.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/**
* Column defines a column in Table.
*
* @author [email protected]
*/ class Column {
/**
* String defining the alignment of this column.
*/ private HorizontalAlignmentConstant alignment;
/**
* boolean defining if this column is sortable.
*/ private boolean isSortable;
/**
* boolean defining if this column is visible.
*/ private boolean isVisible;
/**
* String defining the property of this column.
*/ private String property;
/**
* String defining the text of this column's header.
*/ private String text;
/**
* int defining the width of this column.
*/ private int width;
/**
* Creates an empty Column.
*/ public Column() {
alignment = HasHorizontalAlignment.ALIGN_CENTER;
isSortable = false;
isVisible = true;
text = "column";
width = 100;
}
public HorizontalAlignmentConstant getAlignment() { return alignment;
}
public String getProperty() { return property;
}
public String getText() { return text;
}
public int getWidth() { return width;
}
public boolean isSortable() { return isSortable;
}
public boolean isVisible() { return isVisible;
}
public void setAlignment(HorizontalAlignmentConstant horizontalAlignement) { this.alignment = horizontalAlignement;
}
public void setProperty(String property) { this.property = property;
}
public void setSortable(boolean isSortable) { this.isSortable = isSortable;
}
public void setText(String text) { this.text = text;
}
public void setVisible(boolean isVisible) { this.isVisible = isVisible;
}
public void setWidth(int width) { this.width = width;
}
}
/*
* Copyright 2007 Sfeir, www.sfeir.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/**
* Gets Row's element associated with property.
*
* @param property the property whose associated object is to be retrieved
* @return the object associated with property
*/ public abstract Object getProperty(String property);