1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| public void insertNewTable(final int colCount) {
final int selectionStart = getSelectionStart();
final int start = selectionStart;
final StringWriter sw = new StringWriter();
final SHTMLDocument doc = (SHTMLDocument) getDocument();
final SHTMLWriter w = new SHTMLWriter(sw, doc);
// some needed constants
final String table = HTML.Tag.TABLE.toString();
final String tr = HTML.Tag.TR.toString();
final String td = HTML.Tag.TD.toString();
final String th = HTML.Tag.TH.toString();
final String p = HTML.Tag.P.toString();
final boolean insertPlainTable = !Util.preferenceIsTrue("table.insertStyle", "true");
final boolean insertTableHeader = Util.preferenceIsTrue("table.insertHeader");
try {
// the attribute set to use for applying attributes to tags
final SimpleAttributeSet tableAttributeSet = new SimpleAttributeSet();
// build table attribute
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.WIDTH, DEFAULT_TABLE_WIDTH);
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_STYLE, "solid");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_TOP_WIDTH, "0");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_RIGHT_WIDTH, "0");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_BOTTOM_WIDTH, "0");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_LEFT_WIDTH, "0");
tableAttributeSet.addAttribute(HTML.Attribute.BORDER, "0");
w.writeStartTag(table, insertPlainTable ? null : tableAttributeSet);
// get width of each cell according to column count
// build cell width attribute
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.WIDTH,
Integer.toString(100 / colCount) + Util.pct);
tableAttributeSet.addAttribute(HTML.Attribute.VALIGN, DEFAULT_VERTICAL_ALIGN);
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_TOP_WIDTH, "1");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_RIGHT_WIDTH, "1");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_BOTTOM_WIDTH, "1");
Util.styleSheet().addCSSAttribute(tableAttributeSet, CSS.Attribute.BORDER_LEFT_WIDTH, "1");
final SimpleAttributeSet pSet = new SimpleAttributeSet();
Util.styleSheet().addCSSAttribute(pSet, CSS.Attribute.MARGIN_TOP, "1");
Util.styleSheet().addCSSAttribute(pSet, CSS.Attribute.MARGIN_RIGHT, "1");
Util.styleSheet().addCSSAttribute(pSet, CSS.Attribute.MARGIN_BOTTOM, "1");
Util.styleSheet().addCSSAttribute(pSet, CSS.Attribute.MARGIN_LEFT, "1");
tableAttributeSet.removeAttribute(HTML.Attribute.BORDER);
if (insertTableHeader) {
w.writeStartTag(tr, null);
for (int i = 0; i < colCount; i++) {
w.writeStartTag(th, insertPlainTable ? null : tableAttributeSet);
w.writeStartTag(p, insertPlainTable ? null : pSet);
w.writeEndTag(p);
w.writeEndTag(th);
}
w.writeEndTag(tr);
}
w.writeStartTag(tr, null);
for (int i = 0; i < colCount; i++) {
w.writeStartTag(td, insertPlainTable ? null : tableAttributeSet);
w.writeStartTag(p, insertPlainTable ? null : pSet);
w.writeEndTag(p);
w.writeEndTag(td);
}
w.writeEndTag(tr);
w.writeEndTag(table);
// read table html into document
Element para = doc.getParagraphElement(selectionStart);
if (para == null) {
throw new Exception("no text selected");
}
for (Element parent = para.getParentElement(); !parent.getName().equalsIgnoreCase(HTML.Tag.BODY.toString())
&& !parent.getName().equalsIgnoreCase(HTML.Tag.TD.toString()); para = parent, parent = parent
.getParentElement()) {
;
}
if (para != null) {
try {
doc.startCompoundEdit();
doc.insertBeforeStart(para, sw.getBuffer().toString());
}
catch (final Exception e) {
Util.errMsg(null, e.getMessage(), e);
}
finally {
doc.endCompoundEdit();
}
// w.write(para);
//doc.replaceHTML(para, 1, sw.getBuffer().toString());
}
}
catch (final Exception ex) {
Util.errMsg(null, ex.getMessage(), ex);
}
select(start, start);
} |
Partager