📄 htmleditor.java
字号:
ex.printStackTrace(); } if (dlg.alignCB.getSelectedIndex() > 0) imgTag += " align=\"" + dlg.alignCB.getSelectedItem() + "\" "; imgTag += ">"; if (dlg.urlField.getText().length() > 0) { imgTag = "<a href=\"" + dlg.urlField.getText() + "\">" + imgTag + "</a>"; if (editor.getCaretPosition() == document.getLength()) imgTag += " "; editorKit.insertHTML( document, editor.getCaretPosition(), imgTag, 0, 0, HTML.Tag.A); } else editorKit.insertHTML( document, editor.getCaretPosition(), imgTag, 0, 0, HTML.Tag.IMG); //System.out.println(imgTag); } catch (Exception ex) { ex.printStackTrace(); } } } public void tableActionB_actionPerformed(ActionEvent e) { TableDialog dlg = new TableDialog(null); //dlg.setLocation(tableActionB.getLocationOnScreen()); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = this.getSize(); Point loc = this.getLocationOnScreen(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.setVisible(true); if (dlg.CANCELLED) return; String tableTag = "<table "; String w = dlg.widthField.getText().trim(); if (w.length() > 0) tableTag += " width=\"" + w + "\" "; String h = dlg.heightField.getText().trim(); if (h.length() > 0) tableTag += " height=\"" + h + "\" "; String cp = dlg.cellpadding.getValue().toString(); try { Integer.parseInt(cp, 10); tableTag += " cellpadding=\"" + cp + "\" "; } catch (Exception ex) { ex.printStackTrace(); } String cs = dlg.cellspacing.getValue().toString(); try { Integer.parseInt(cs, 10); tableTag += " cellspacing=\"" + cs + "\" "; } catch (Exception ex) { ex.printStackTrace(); } String b = dlg.border.getValue().toString(); try { Integer.parseInt(b, 10); tableTag += " border=\"" + b + "\" "; } catch (Exception ex) { ex.printStackTrace(); } if (dlg.alignCB.getSelectedIndex() > 0) tableTag += " align=\"" + dlg.alignCB.getSelectedItem() + "\" "; if (dlg.vAlignCB.getSelectedIndex() > 0) tableTag += " valign=\"" + dlg.vAlignCB.getSelectedItem() + "\" "; if (dlg.bgcolorField.getText().length() > 0) tableTag += " bgcolor=\"" + dlg.bgcolorField.getText() + "\" "; tableTag += ">"; int cols = 1; int rows = 1; try { cols = ((Integer) dlg.columns.getValue()).intValue(); } catch (Exception ex) { ex.printStackTrace(); } try { rows = ((Integer) dlg.rows.getValue()).intValue(); } catch (Exception ex) { ex.printStackTrace(); } for (int r = 0; r < rows; r++) { tableTag += "<tr>"; for (int c = 0; c < cols; c++) tableTag += "<td><p></p></td>"; tableTag += "</tr>"; } tableTag += "</table>"; String parentname = document .getParagraphElement(editor.getCaretPosition()) .getParentElement() .getName(); HTML.Tag parentTag = HTML.getTag(parentname); System.out.println(parentTag + ":\n" + tableTag); /* * HTMLEditorKit.InsertHTMLTextAction insertTableAction = new * HTMLEditorKit.InsertHTMLTextAction("insertTABLE",tableTag, * HTML.Tag.BODY, HTML.Tag.TABLE); * */ //insertHTML(tableTag, editor.getCaretPosition()); try { editorKit.insertHTML( document, editor.getCaretPosition(), tableTag, 1, 0, HTML.Tag.TABLE); //removeIfEmpty(document.getParagraphElement(editor.getCaretPosition()-1)); } catch (Exception ex) { ex.printStackTrace(); } } public void linkActionB_actionPerformed(ActionEvent e) { LinkDialog dlg = new LinkDialog(null); //dlg.setLocation(linkActionB.getLocationOnScreen()); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = this.getSize(); Point loc = this.getLocationOnScreen(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); if (editor.getSelectedText() != null) dlg.txtDesc.setText(editor.getSelectedText()); dlg.setVisible(true); if (dlg.CANCELLED) return; String aTag = "<a"; if (dlg.txtURL.getText().length() > 0) aTag += " href=\"" + dlg.txtURL.getText() + "\""; if (dlg.txtName.getText().length() > 0) aTag += " name=\"" + dlg.txtName.getText() + "\""; if (dlg.txtTitle.getText().length() > 0) aTag += " title=\"" + dlg.txtTitle.getText() + "\""; if (dlg.chkNewWin.isSelected()) aTag += " target=\"_blank\""; aTag += ">" + dlg.txtDesc.getText() + "</a>"; if (editor.getCaretPosition() == document.getLength()) aTag += " "; editor.replaceSelection(""); try { editorKit.insertHTML( document, editor.getCaretPosition(), aTag, 0, 0, HTML.Tag.A); } catch (Exception ex) { ex.printStackTrace(); } } void setLinkProperties( Element el, String href, String target, String title, String name) { LinkDialog dlg = new LinkDialog(null); dlg.setLocation(linkActionB.getLocationOnScreen()); dlg.setModal(true); //dlg.descPanel.setVisible(false); dlg.txtURL.setText(href); dlg.txtName.setText(name); dlg.txtTitle.setText(title); try { dlg.txtDesc.setText( document.getText( el.getStartOffset(), el.getEndOffset() - el.getStartOffset())); } catch (Exception ex) { ex.printStackTrace(); } dlg.chkNewWin.setSelected(target.toUpperCase().equals("_BLANK")); dlg.header.setText(Local.getString("Hyperlink properties")); dlg.setTitle(Local.getString("Hyperlink properties")); dlg.setVisible(true); if (dlg.CANCELLED) return; //String p = ""; /* * SimpleAttributeSet attrs = new * SimpleAttributeSet(el.getAttributes()); if * (dlg.urlField.getText().length() >0) { * attrs.addAttribute(HTML.Attribute.HREF, dlg.urlField.getText()); p += * "href="+dlg.urlField.getText()+" "; } if * (dlg.nameField.getText().length() >0) { * attrs.addAttribute(HTML.Attribute.NAME, dlg.nameField.getText()); p += * "name="+dlg.nameField.getText()+" "; } if * (dlg.titleField.getText().length() >0) { * attrs.addAttribute(HTML.Attribute.TITLE, dlg.titleField.getText()); * p += "title="+dlg.titleField.getText()+" "; } if * (dlg.newWinChB.isSelected()) { * attrs.addAttribute(HTML.Attribute.TARGET, "_blank"); p += * "target=_blank "; } attrs.addAttribute(StyleConstants.NameAttribute, * "a"); attrs.addAttribute(HTML.Tag.A, p); */ String aTag = "<a"; if (dlg.txtURL.getText().length() > 0) aTag += " href=\"" + dlg.txtURL.getText() + "\""; if (dlg.txtName.getText().length() > 0) aTag += " name=\"" + dlg.txtName.getText() + "\""; if (dlg.txtTitle.getText().length() > 0) aTag += " title=\"" + dlg.txtTitle.getText() + "\""; if (dlg.chkNewWin.isSelected()) aTag += " target=\"_blank\""; aTag += ">" + dlg.txtDesc.getText() + "</a>"; try { document.setOuterHTML(el, aTag); } catch (Exception ex) { ex.printStackTrace(); } } void setImageProperties( Element el, String src, String alt, String width, String height, String hspace, String vspace, String border, String align) { ImageDialog dlg = new ImageDialog(null); dlg.setLocation(imageActionB.getLocationOnScreen()); dlg.setModal(true); dlg.setTitle(Local.getString("Image properties")); dlg.fileField.setText(src); dlg.altField.setText(alt); dlg.widthField.setText(width); dlg.heightField.setText(height); dlg.hspaceField.setText(hspace); dlg.vspaceField.setText(vspace); dlg.borderField.setText(border); dlg.alignCB.setSelectedItem(align); dlg.updatePreview(); dlg.setVisible(true); if (dlg.CANCELLED) return; String imgTag = "<img src=\"" + dlg.fileField.getText() + "\" alt=\"" + dlg.altField.getText() + "\" "; String w = dlg.widthField.getText(); try { Integer.parseInt(w, 10); imgTag += " width=\"" + w + "\" "; } catch (Exception ex) { ex.printStackTrace(); } String h = dlg.heightField.getText(); try { Integer.parseInt(h, 10); imgTag += " height=\"" + h + "\" "; } catch (Exception ex) { ex.printStackTrace(); } String hs = dlg.hspaceField.getText(); try { Integer.parseInt(hs, 10); imgTag += " hspace=\"" + hs + "\" "; } catch (Exception ex) { ex.printStackTrace(); } String vs = dlg.vspaceField.getText(); try { Integer.parseInt(vs, 10); imgTag += " vspace=\"" + vs + "\" "; } catch (Exception ex) { ex.printStackTrace(); } String b = dlg.borderField.getText(); try { Integer.parseInt(b, 10); imgTag += " border=\"" + b + "\" "; } catch (Exception ex) { ex.printStackTrace(); } if (dlg.alignCB.getSelectedIndex() > 0) imgTag += " align=\"" + dlg.alignCB.getSelectedItem() + "\" "; imgTag += ">"; if (dlg.urlField.getText().length() > 0) { imgTag = "<a href=\"" + dlg.urlField.getText() + "\">" + imgTag + "</a>"; if (editor.getCaretPosition() == document.getLength()) imgTag += " "; } try { document.setOuterHTML(el, imgTag); } catch (Exception ex) { ex.printStackTrace(); } } void setElementProperties(Element el, String id, String cls, String sty) { ElementDialog dlg = new ElementDialog(null); //dlg.setLocation(linkActionB.getLocationOnScreen()); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = this.getSize(); Point loc = this.getLocationOnScreen(); dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.setTitle(Local.getString("Object properties")); dlg.idField.setText(id); dlg.classField.setText(cls); dlg.styleField.setText(sty); // Uncommented, returns a simple p into the header... fix needed ? //dlg.header.setText(el.getName()); dlg.setVisible(true); if (dlg.CANCELLED) return; SimpleAttributeSet attrs = new SimpleAttributeSet(el.getAttributes()); if (dlg.idField.getText().length() > 0) attrs.addAttribute(HTML.Attribute.ID, dlg.idField.getText()); if (dlg.classField.getText().length() > 0) attrs.addAttribute(HTML.Attribute.CLASS, dlg.classField.getText()); if (dlg.styleField.getText().length() > 0) attrs.addAttribute(HTML.Attribute.STYLE, dlg.styleField.getText()); document.setParagraphAttributes(el.getStartOffset(), 0, attrs, true); } void setTableProperties(Element td) { Element tr = td.getParentElement(); Element table = tr.getParentElement(); TdDialog dlg = new TdDialog(null); dlg.setLocation(editor.getLocationOnScreen()); dlg.setModal(true); dlg.setTitle(Local.getString("Table properties")); /** **********PARSE ELEMENTS*********** */ // TD*** AttributeSet tda = td.getAttributes(); if (tda.isDefined(HTML.Attribute.BGCOLOR)) { dlg.tdBgcolorField.setText( tda.getAttribute(HTML.Attribute.BGCOLOR).toString()); Util.setBgcolorField(dlg.tdBgcolorField); } if (tda.isDefined(HTML.Attribute.WIDTH)) dlg.tdWidthField.setText( tda.getAttribute(HTML.Attribute.WIDTH).toString()); if (tda.isDefined(HTML.Attribute.HEIGHT)) dlg.tdHeightField.setText( tda.getAttribute(HTML.Attribute.HEIGHT).toString()); if (tda.isDefined(HTML.Attribute.COLSPAN)) try { Integer i = new Integer( tda.getAttribute(HTML.Attribute.COLSPAN).toString()); dlg.tdColspan.setValue(i); } catch (Exception ex) { ex.printStackTrace(); } if (tda.isDefined(HTML.Attribute.ROWSPAN)) try { Integer i = new Integer( tda.getAttribute(HTML.Attribute.ROWSPAN).toString()); dlg.tdRowspan.setValue(i); } catch (Exception ex) { ex.printStackTrace(); } if (tda.isDefined(HTML.Attribute.ALIGN)) dlg.tdAlignCB.setSelectedItem( tda .getAttribute(HTML.Attribute.ALIGN) .toString() .toLowerCase()); if (tda.isDefined(HTML.Attribute.VALIGN)) dlg.tdValignCB.setSelectedItem( tda .getAttribute(HTML.Attribute.VALIGN) .toString() .toLowerCase()); dlg.tdNowrapChB.setSelected((tda.isDefined(HTML.Attribute.NOWRAP))); //TR **** AttributeSet tra = tr.getAttributes(); if (tra.isDefined(HTML.Attribute.BGCOLOR)) { dlg.trBgcolorField.setText( tra.getAttribute(HTML.Attribute.BGCOLOR).toString()); Util.setBgcolorField(dlg.trBgcolorField); } if (tra.isDefined(HTML.Attribute.ALIGN)) dlg.trAlignCB.setSelectedItem( tra .getAttribute(HTML.Attribute.ALIGN) .toString() .toLowerCase()); if (tra.isDefined(HTML.Attribute.VALIGN)) dlg.trValignCB.setSelectedItem( tra .getAttribute(HTML.Attribute.VALIGN) .toString() .toLowerCase()); //TABLE **** AttributeSet ta = table.getAttributes(); if (ta.isDefi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -