forked from wicketstuff/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- development of TableComponentAsXlsHandler to export Wicket table
components to Xls files - added a link exemplifying the new handler in inmethod-grid and minis example project
- Loading branch information
Showing
12 changed files
with
614 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...-examples/src/main/java/com/inmethod/grid/examples/pages/datagrid/SimpleDataGridPage.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...mples/src/test/java/com/inmethod/grid/examples/pages/datagrid/SimpleDataGridPageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../minis-parent/minis/src/main/java/org/wicketstuff/minis/component/excel/CellExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
101 changes: 101 additions & 0 deletions
101
...ent/minis/src/main/java/org/wicketstuff/minis/component/excel/GeneralPurposeExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.