You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2005/01/21 15:43:39 UTC
DO NOT REPLY [Bug 27333] -
HSSFCellStyle does not implement Cloneable
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=27333>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=27333
amolweb@yahoo.com changed:
What |Removed |Added
----------------------------------------------------------------------------
OS/Version| |All
------- Additional Comments From amolweb@yahoo.com 2005-01-21 15:43 -------
(In reply to comment #2)
> Sorry Andy, I made a mistake with my original request. You are right that there
> is no point in cloning a reference. What I needed to achieve is a deep copy of
> the HSSFCellStyle which I mistakenly thought implementing the clone method would
> do (sorry my mistake).
>
> I have successfully implemented a deep copy via serialisation of the
> HSSFCellStyle object. However to do this I have had to make the following
> classes implement the "java.io.Serializable" class:
>
> org.apache.poi.hssf.usermodel.HSSFCell
> org.apache.poi.hssf.usermodel.HSSFCellStyle
> org.apache.poi.hssf.record.Record
>
> The last class "org.apache.poi.hssf.record.Record" I altered so that all Record
> sub-classes would also be serialisable as a result although the only class in
> the "org.apache.poi.hssf.record" package I actually needed to be serialisable
> for my purposes is "org.apache.poi.hssf.record.ExtendedFormatRecord".
>
> Would it be acceptable to add these changes to the next release, again it is
> very little work and would be a great help to me.
>
> Cheers,
>
> Tim
>
>
>
Another way to tackle it outside of POI source is to create javabean classes for
HSSFCellStyle, HSSFFont and HSSFCellFormat. Make these javabean classes have the
same set/get methods as the corresponding hssf classes but make them Cloneable,
Serializable etc. And provide a method on each of these that takes in a
HSSFWorkbook and returns a new javabean object. (I have avoided Cloneable since
it is considered to be a flawed method, I use explicity method named makeCopy()
that returns a new copy). Make a factory that returns preconfigured instances of
style/font/format javabeans.
Finally, extend HSSFWorkbook class (create a decorator) to cache the actual
styles created in a Map, and provide a method geetOrCreateCellStyle(String name)
etc. which first checks in the cache for styles by this name previously created
in this workbook and returns the same or creates one using javabean instances
from the factory if map lookup fails.
Foll is the outline of this code:
//-- Wrapper for hssfcellstyle
public class CellStyleBean implements Serializable {
protected short borderBottom;
// all other similar fields
protected short preferredCellWidth;
public HSSFCellStyle toCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle retval = workbook.createCellStyle();
retval.setAlignment(alignment);
// other setters
retval.setWrapText(wrapText);
return retval;
}
public CellStyleProps makeCopy() {
CellStyleProps retval = new CellStyleProps();
retval.setAlignment(alignment);
// other setters here
retval.setPreferredCellWidth(preferredCellWidth);
return retval;
}
public short getAlignment() {
return alignment;
}
public void setAlignment(short alignment) {
this.alignment = alignment;
}
// other get/set
} // end of CellStyleBean
// --
// -- HSSFWorkbookDecorator class
// --
public class HSSFWorkbookDecorator extends HSSFWorkbook {
protected Map cellStylesMap = new HashMap();
protected Map fontsMap = new HashMap();
protected Map dataFormatsMap = new HashMap();
// provide all required constructors
// and delegate all to super class
public HSSFCellStyle getOrCreateCellStyle(String name) {
HSSFCellStyle retval = (HSSFCellStyle) cellStylesMap.get(name);
if (retval == null) {
CellStyleProps cellStyleProps =
FormatFactory.getInstance().getCellStyleProps(name);
if (cellStyleProps==null) {
cellStyleProps = new CellStyleProps();
}
retval = cellStyleProps.toCellStyle(this);
cellStylesMap.put(name, retval);
}
return retval;
}
// simlar to above provide getOrCreateXYZ for font and dataformat
}
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/