Menu entry »Save a Copy as Web Page...«

General discussion on RText.

Moderator: robert

Menu entry »Save a Copy as Web Page...«

Postby theuserbl » Mon May 28, 2012 10:18 pm

I have played a little bit with the menu entry »Save a Copy as Web Page...«:

(File "SaveAsWebPageAction.java" in "org/fife/rtext/actions/")

java code:

package org.fife.rtext.actions;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ResourceBundle;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.fife.rtext.RText;
import org.fife.rtext.RTextEditorPane;
import org.fife.rtext.RTextUtilities;
import org.fife.ui.UIUtil;
import org.fife.ui.app.StandardAction;
import org.fife.ui.rtextfilechooser.RTextFileChooser;
import org.fife.ui.rtextfilechooser.filters.HTMLFileFilter;
import org.fife.ui.rsyntaxtextarea.Token;


/**
* Action used by an <code>RText</code> to save a copy
* of the current document as HTML.
*
* @author Robert Futrell
* @version 1.0
*/
// TODO: Use CSS; have one CSS class per token type and just do
// <div class=\"" + token.type + "\"> + text + "</div>"
class SaveAsWebPageAction extends StandardAction {


/**
* Constructor.
*
* @param owner The parent RText instance.
* @param msg The resource bundle to use for localization.
* @param icon The icon associated with the action.
*/
public SaveAsWebPageAction(RText owner, ResourceBundle msg, Icon icon) {
super(owner, msg, "SaveAsWebPageAction");
setIcon(icon);
}


public void actionPerformed(ActionEvent e) {

RText owner = (RText)getApplication();

RTextFileChooser chooser = new RTextFileChooser();
chooser.setCustomTitle(owner.getString("SaveAsWebPage"));
chooser.addChoosableFileFilter(new HTMLFileFilter());
chooser.setEncoding("UTF-8");

RTextEditorPane editor = owner.getMainView().getCurrentTextArea();
String htmlFileName = editor.getFileFullPath();
int extensionStart = htmlFileName.lastIndexOf('.');
if (extensionStart!=-1) {
htmlFileName = htmlFileName.substring(0,extensionStart) + ".html";
}
else {
htmlFileName += ".html";
}
chooser.setSelectedFile(new File(htmlFileName));
chooser.setComponentOrientation(owner.getComponentOrientation());

if (chooser.showSaveDialog(owner)==JFileChooser.APPROVE_OPTION) {

File chosenFile = chooser.getSelectedFile();
String chosenFilePath = chosenFile.getAbsolutePath();

// Prompt before overwriting file if it already exists.
if (chosenFile.exists()) {
String temp = owner.getString("FileAlreadyExists",
chosenFile.getAbsolutePath());
if (JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(
owner, temp,
owner.getString("ConfDialogTitle"),
JOptionPane.YES_NO_OPTION)) {
return;
}
}

// Ensure that it has an HTML extension.
if (!chosenFilePath.matches("[^\\.]*\\.htm[l]?"))
chosenFilePath = chosenFilePath + ".html";

// Try and write output to the current filename.
try {
saveAs(chosenFilePath);
} catch (IOException ioe) {
String desc = owner.getString("ErrorWritingFile",
chosenFilePath, ioe.getMessage());
JOptionPane.showMessageDialog(owner, desc,
owner.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
owner.setMessages(null, "ERROR: Could not save file!");
ioe.printStackTrace();
}

}

}


private void saveAs(String path) throws IOException {

String[] styles = new String[Token.NUM_TOKEN_TYPES];
StringBuffer temp = new StringBuffer();
StringBuffer sb = new StringBuffer();
RText owner = (RText)getApplication();

PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(path), "UTF-8")));
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<!-- Generated by RText -->");
out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
out.println("<title>[" + owner.getMainView().getCurrentTextArea().getFileName() + "]</title>");

RText rtext = (RText)getApplication();
RTextEditorPane textArea = rtext.getMainView().getCurrentTextArea();
int lineCount = textArea.getLineCount();
sb.append("<div class=\"headbg\">\n");
sb.append("<div class=\"expand\">\n");
sb.append("<div class=\"fc\">\n");
sb.append("<table><tr><td id=\"nums\"><pre><table width=\"100%\">");
for (int i=0; i<lineCount; i++) {
sb.append("<tr><td id=\""+ (i+1) +"\"><a href=\"#"+ (i+1) +"\">"+ (i+1) +"</a></td></tr>\n");
}
sb.append("</table></pre></td><td id=\"lines\"><pre><table>");
for (int i=0; i<lineCount; i++) {
Token token = textArea.getTokenListForLine(i);
sb.append("<tr><td>");
while (token!=null && token.isPaintable()) {
if (styles[token.type]==null) {
temp.setLength(0);
temp.append(".s").append(token.type).append(" {\n");
Font font = textArea.getFontForTokenType(token.type);
if (font.isBold()) {
temp.append("font-weight: bold;\n");
}
if (font.isItalic()) {
temp.append("font-style: italic;\n");
}
Color c = textArea.getForegroundForToken(token);
temp.append("color: ").append(UIUtil.getHTMLFormatForColor(c)).append(";\n");
temp.append("}");
styles[token.type] = temp.toString();
}
sb.append("<span class=\"s" + token.type + "\">");
sb.append(RTextUtilities.escapeForHTML(token.getLexeme(), "\n", true));
sb.append("</span>");
token = token.getNextToken();
}
sb.append("<br></td></tr>\n");
}
sb.append("</table></pre></td></tr></table>\n</div></div></div>\n");

// Print CSS styles
out.println("<style type=\"text/css\">");
for (int i=0; i<styles.length; i++) {
if (styles[i]!=null) {
out.println(styles[i]);
}
}
out.println("\n");
out.println(" body{background:#fff;font:82% arial,sans-serif;margin:3px 0 3px 0;min-width:768px}");
out.println(".headbg{margin:0 8px}");
out.println("");
out.println("");
out.println("/* Style sheet for source browsing pages. */");
out.println("");
out.println("/* File */");
out.println(".fc pre, .fc td,");
out.println(".fc table {border-collapse:collapse; margin:0; padding:0}");
out.println("#nums, #lines, #nums th, #lines th, #nums td, #lines td { vertical-align:top }");
out.println("pre {");
out.println(" font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Lucida Console', monospace;");
out.println(" font-size: 93%;");
out.println("}");
out.println("#nums {padding-right:.5em; width:3.7em}");
out.println("#nums td {text-align:right}");
out.println("#nums a {color:#808080; text-decoration:none} /* line numbers */");
out.println("");
out.println("#lines td {padding-left:4px;}");
out.println("");
out.println("/* Applies only to sb files and issue attachments */");
out.println(".fc #nums, .fc #lines {");
out.println(" padding-top: 0.5em;");
out.println("}");
out.println(".fc #lines {");
out.println(" border-left: 1px solid #dddddd; /* vertial line */");
out.println("}");
out.println("");
out.println("/* IE Whitespace Fix */");
out.println(".prettyprint td.source {");
out.println(" white-space: pre-wrap;");
out.println(" white-space: -moz-pre-wrap;");
out.println(" white-space: -pre-wrap;");
out.println(" white-space: -o-pre-wrap;");
out.println("}");
out.println("</style>");

// Print the body
out.println("</head>");
out.println("<body>");
out.println(sb.toString());
out.println("</body>\n</html>");

out.close();

}


}


The additional CSS-code and HTML-code is taken from the Google-Code side.
But changed, to make it more look like RText and heavily cutted down.

Greatings
theuserbl
theuserbl
 
Posts: 12
Joined: Sun Mar 28, 2010 2:37 pm

Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 1 guest