Skip to content

Commit

Permalink
- development of TableComponentAsXlsHandler to export Wicket table
Browse files Browse the repository at this point in the history
components to Xls files
- added a link exemplifying the new handler in inmethod-grid and
minis example project
  • Loading branch information
pedrosans committed Apr 11, 2011
1 parent eed5569 commit 5b5b1e0
Show file tree
Hide file tree
Showing 12 changed files with 614 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>inmethod-grid</artifactId>
</dependency>
<dependency>
<groupId>org.wicketstuff</groupId>
<artifactId>minis</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wicket:extend>
<a wicket:id="exportToExcel">Export to Excel</a>
<div wicket:id="grid">
</div>
</wicket:extend>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.cycle.RequestCycle;
import org.wicketstuff.minis.component.excel.TableComponentAsXlsHandler;

import com.inmethod.grid.IGridColumn;
import com.inmethod.grid.column.PropertyColumn;
Expand All @@ -16,25 +20,49 @@
*
* @author Matej Knopp
*/
public class SimpleDataGridPage extends BaseExamplePage {
public class SimpleDataGridPage extends BaseExamplePage
{

private static final long serialVersionUID = 1L;

/**
* Constructor.
*/
public SimpleDataGridPage() {
public SimpleDataGridPage()
{
List<IGridColumn> columns = new ArrayList<IGridColumn>();

columns.add(new PropertyColumn(new ResourceModel("id"), "id"));
columns.add(new PropertyColumn(new ResourceModel("firstName"), "firstName", "firstName"));
columns.add(new PropertyColumn(new ResourceModel("lastName"), "lastName", "lastName"));
columns.add(new PropertyColumn(new ResourceModel("homePhone"), "homePhone"));
columns.add(new PropertyColumn(new ResourceModel("cellPhone"), "cellPhone"));
DataGrid grid = new DefaultDataGrid("grid", new ContactDataSource(), columns);

final DataGrid grid = new DefaultDataGrid("grid", new ContactDataSource(), columns);

add(grid);

Link<Void> exportToExcel = new Link<Void>("exportToExcel")
{
/** */
private static final long serialVersionUID = 1L;

@Override
public void onClick()
{
try
{
IRequestHandler handler = new TableComponentAsXlsHandler(
grid.get("form:bodyContainer:body"), "example.xls");
RequestCycle.get().scheduleRequestHandlerAfterCurrent(handler);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
add(exportToExcel);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-->
<context-param>
<param-name>configuration</param-name>
<param-value>deployment</param-value>
<param-value>development</param-value>
</context-param>

<filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package com.inmethod.grid.examples.pages.datagrid;

import java.io.IOException;

import junit.framework.TestCase;

import org.apache.wicket.util.tester.WicketTester;

import com.inmethod.grid.examples.WicketApplication;

/**
* @author Pedro Santos
*/
public class SimpleDataGridPageTest extends TestCase
{
public void testExportToExcel() throws IOException
{
WicketTester tester = new WicketTester(new WicketApplication());
tester.startPage(SimpleDataGridPage.class);
tester.clickLink("exportToExcel");
assertTrue(tester.getLastResponse().getHeader("Content-Disposition").contains(".xls"));
// File f = new File(System.getProperty("user.home") + "\\test.xls");
// f.delete();
// f.createNewFile();
// FileOutputStream out = new FileOutputStream(f);
// out.write(tester.getLastResponse().getBinaryContent());
// out.close();
// Desktop d = Desktop.getDesktop();
// d.open(f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</tr>
</table>
<input type="submit" value="Submit" />
<a wicket:id="exportToExcel">Export to Excel</a>
</form>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.wicketstuff.minis.component.ListViewFormComponentReuseManager;
import org.wicketstuff.minis.component.excel.TableComponentAsXlsHandler;

/**
* @author <a href="http://sebthom.de/">Sebastian Thomschke</a>
Expand All @@ -57,7 +61,6 @@ public ListViewFormComponentReuseManagerPage(final PageParameters parameters)
{
final Form<List<Row>> form = new Form<List<Row>>("rowsForm");
add(form);

form.add(new Button("addRowButton")
{
@Override
Expand All @@ -66,7 +69,16 @@ public void onSubmit()
rows.add(new Row());
}
}.setDefaultFormProcessing(false));

form.add(new Link<Void>("exportToExcel")
{
@Override
public void onClick()
{
IRequestHandler handler = new TableComponentAsXlsHandler(form.get("rowsList"),
"example.xls");
RequestCycle.get().scheduleRequestHandlerAfterCurrent(handler);
}
});
form.add(new ListView<Row>("rowsList", new PropertyModel<List<Row>>(this, "rows"))
{
@Override
Expand All @@ -82,10 +94,10 @@ public Integer getObject()
return item.getIndex() + 1;
}
}));
ListViewFormComponentReuseManager.addOrReuse(item, new RequiredTextField<String>("key",
new PropertyModel<String>(row, "key")));
ListViewFormComponentReuseManager.addOrReuse(item, new RequiredTextField<String>(
"key", new PropertyModel<String>(row, "key")));
ListViewFormComponentReuseManager.addOrReuse(item, new TextField<String>("value",
new PropertyModel<String>(row, "value")));
new PropertyModel<String>(row, "value")));

item.add(new SubmitLink("removeRowLink")
{
Expand Down
5 changes: 5 additions & 0 deletions jdk-1.5-parent/minis-parent/minis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8-beta1</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.wicketstuff.minis.component.excel;

import java.text.ParseException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.wicket.Component;
import org.apache.wicket.markup.parser.XmlPullParser;
import org.apache.wicket.markup.parser.XmlTag;

/**
* Function interface to access the component table data and set in the
* {@link Cell}. A general purpose one is provided, but it can be customized by
* setting {@link TableComponentAsXlsHandler#setCellExporter(CellExporter)}
*
* @author Pedro Santos
*/
public interface CellExporter
{
/**
* @param tag
* <td>HTML tag
* @param parser
* the {@link XmlPullParser} in use, can be useful to parse
* nested elements
* @param cell
* the target cell, where the value needs to be set
* @param tableComponent
* the table components being parsed
* @throws ParseException
*/
public void exportCell(XmlTag tag, XmlPullParser parser, Cell cell, Component tableComponent)
throws ParseException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.wicketstuff.minis.component.excel;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.wicket.Component;
import org.apache.wicket.markup.parser.XmlPullParser;
import org.apache.wicket.markup.parser.XmlTag;

/**
* Fist try to get the most nested component in the table data tag, if not
* possible set the most nested value in markup as a {@link String}.
*
* @author Pedro Santos
*/
public class GeneralPurposeExporter implements CellExporter
{

public void exportCell(XmlTag tag, XmlPullParser parser, Cell cell, Component gridComponent)
throws ParseException
{
XmlTag firstMostNestedTag = tag;
// find the most inner tag value
while ((tag = parser.nextTag()).getName().equals("td") == false)
{
if (tag.isOpen() || tag.isOpenClose())
{
firstMostNestedTag = tag;
}
else
{
break;
}
}
CharSequence possibleComponentReference = firstMostNestedTag
.getAttribute(TableComponentAsXlsHandler.OutputPathBehavior.PATH_ATTRIBUTE);
if (possibleComponentReference != null)
{
Component firstMostNestedComponent = gridComponent.getPage().get(
possibleComponentReference.toString());
Object modelValue = firstMostNestedComponent.getDefaultModelObject();
if (modelValue != null)
{
if (modelValue instanceof Number)
{
cell.setCellValue(((Number)modelValue).doubleValue());
}
else if (modelValue instanceof CharSequence)
{
cell.setCellValue(modelValue.toString());
}
else if (modelValue instanceof CharSequence)
{
cell.setCellValue(modelValue.toString());
}
else if (modelValue instanceof Boolean)
{
cell.setCellValue((Boolean)modelValue);
}
else if (modelValue instanceof Calendar)
{
cell.setCellValue((Calendar)modelValue);
}
else if (modelValue instanceof Date)
{
cell.setCellValue((Date)modelValue);
}
else
{
cell.setCellValue(modelValue.toString());
}
}
}
else
{
// simply set the first most nested tag value
String value = parser.getInput(
firstMostNestedTag.getPos() + firstMostNestedTag.getLength(), tag.getPos())
.toString();
cell.setCellValue(value);
}
}
}
Loading

0 comments on commit 5b5b1e0

Please sign in to comment.