/* * $Header: /cvs/CVS_LEBA/common/jstd/src/java/com/obrctm/app/console/TextWindow.java,v 1.5 2003/10/29 13:13:00 pitek Exp $ * * Copyright 2003 OBR Centrum Techniki Morskiej, All rights reserved. */ package com.obrctm.app.util; import com.obrctm.app.console.Console; import java.util.Date; import java.io.File; import java.io.FileOutputStream; import java.io.BufferedOutputStream; import java.io.IOException; import java.awt.Font; import com.obrctm.app.App; import com.obrctm.app.printing.IPrinting; /** * Okno do prezentacji tekstu i dokumentów HTML. *

* Jest to w pełni zlokalizowany komponent. Można potraktować go * jako początek tematu lokalizacji wszystkich apliakcji. * * @version $Header: /cvs/CVS_LEBA/common/jstd/src/java/com/obrctm/app/console/TextWindow.java,v 1.5 2003/10/29 13:13:00 pitek Exp $ * @author pitek * @author jacos */ public class TextWindow extends javax.swing.JDialog { private static java.util.ResourceBundle i18n = I18N.getBundle(TextWindow.class); public final static String TEXT_PLAIN = "text/plain"; public final static String TEXT_HTML = "text/html"; public final static String TEXT_RTF = "text/rtf"; /** Tworzy puste okno tekstowe */ public TextWindow(java.awt.Frame parent, boolean modal, String title) { super(parent, modal); this.disposeOnClose = true; this.font = new Font("Courier New", 0, 12); initComponents(); setTitle(title); if (App.module("printing") == null) { jButtonPrint.setVisible(false); } jEditorPane.setContentType(TEXT_PLAIN); jEditorPane.setEditable(false); } /** * Wyświetla w oknie podany tekst, bez żadnego formatowania, używając * fontu monospaced. * @param text tekst do wyświetlenia. */ public void setText(String text) { jEditorPane.setContentType(TEXT_PLAIN); jEditorPane.setFont(this.font); jEditorPane.setText(text); jEditorPane.setCaretPosition(0); } /** * Wyświetla w oknie podany tekst w formacie HTML * @param html sformatowany tekst do wyświetlenia. */ public void setHtml(String html) { jEditorPane.setContentType(TEXT_HTML); jEditorPane.setText(html); jEditorPane.setCaretPosition(0); } /** * Wyświetla w oknie podaną stronę HTML * @param url adres strony do wyświetlenia. */ public void openHtmlPage(String url) { try { jEditorPane.setContentType(TEXT_HTML); jEditorPane.setPage(url); jEditorPane.setCaretPosition(0); } catch (IOException e) { App.log(e); setText(i18n.getString("L_TextWindow_wrongPage")+ ": " + url); } } public void setFont(Font font) { if (font == null) { throw new NullPointerException("Font can not be null!"); } this.font = font; } public void setDefaultSaveDirectory(String defaultSaveDirectory) { this.defaultSaveDirectory = defaultSaveDirectory; } public void setProposedFileName(String proposedFileName) { this.proposedFileName = proposedFileName; } public void setDisposeOnClose(boolean disposeOnClose) { this.disposeOnClose = disposeOnClose; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents jScrollPane = new javax.swing.JScrollPane(); jEditorPane = new javax.swing.JEditorPane(); jPanel = new javax.swing.JPanel(); jButtonClipboard = new javax.swing.JButton(); jButtonSave = new javax.swing.JButton(); jButtonPrint = new javax.swing.JButton(); jButtonClose = new javax.swing.JButton(); setTitle("Text window"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closeDialog(evt); } }); jScrollPane.setPreferredSize(new java.awt.Dimension(530, 283)); jScrollPane.setViewportView(jEditorPane); getContentPane().add(jScrollPane, java.awt.BorderLayout.CENTER); jPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); jButtonClipboard.setMnemonic(i18n.getString("L_TextWindow_toClipboardMnem").charAt(0)); jButtonClipboard.setText(i18n.getString("L_TextWindow_toClipboarad")); jButtonClipboard.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonClipboardActionPerformed(evt); } }); jPanel.add(jButtonClipboard); jButtonSave.setMnemonic(i18n.getString("L_TextWindow_saveAsMnem").charAt(0)); jButtonSave.setText(i18n.getString("L_TextWindow_saveAs")); jButtonSave.setToolTipText(""); jButtonSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSaveActionPerformed(evt); } }); jPanel.add(jButtonSave); jButtonPrint.setMnemonic(i18n.getString("L_TextWindow_printMnem").charAt(0)); jButtonPrint.setText(i18n.getString("L_TextWindow_print")); jButtonPrint.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonPrintActionPerformed(evt); } }); jPanel.add(jButtonPrint); jButtonClose.setMnemonic(i18n.getString("L_TextWindow_closeMnem").charAt(0)); jButtonClose.setText(i18n.getString("L_TextWindow_close")); jButtonClose.setToolTipText(""); jButtonClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonCloseActionPerformed(evt); } }); jPanel.add(jButtonClose); getContentPane().add(jPanel, java.awt.BorderLayout.SOUTH); pack(); }//GEN-END:initComponents private void jButtonClipboardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClipboardActionPerformed String contentType = jEditorPane.getContentType(); if (contentType.equals(TEXT_PLAIN)) { ClipboardUtil.putString(jEditorPane.getText()); } else if (contentType.equals(TEXT_HTML)) { ClipboardUtil.putHtml(jEditorPane.getText()); } else if (contentType.equals(TEXT_RTF)) { assert false : "Unsupported content type " + contentType; } else { assert false : "Unsupported content type " + contentType; } }//GEN-LAST:event_jButtonClipboardActionPerformed private void jButtonPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPrintActionPerformed IPrinting module = (IPrinting)App.module("printing"); if (module == null) { return; } String dialogContent = jEditorPane.getText(); if (dialogContent == null) { dialogContent = ""; } String contentType = jEditorPane.getContentType(); if (contentType.equals(TEXT_PLAIN)) { module.printString(dialogContent, true); } else { assert false : "Unsupported content type " + contentType; } }//GEN-LAST:event_jButtonPrintActionPerformed private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed String title = null; String extension = null; String description = null; String contentType = jEditorPane.getContentType(); if (contentType.equals(TEXT_PLAIN)) { title = i18n.getString("L_TextWindow_saveTextFile"); extension = ".txt"; description = i18n.getString("L_TextWindow_textFile"); } else if (contentType.equals(TEXT_HTML)) { title = i18n.getString("L_TextWindow_saveHtmlFile"); extension = ".html"; description = i18n.getString("L_TextWindow_htmlFile"); } else { assert false : "Unsupported content type " + contentType; } File outputFile = Console.displaySaveFileDialog(defaultSaveDirectory, proposedFileName , title, extension, description, true); if (outputFile == null) { return; } try { byte[] fileContent = jEditorPane.getText().getBytes(); BufferedOutputStream textFile = new BufferedOutputStream(new FileOutputStream(outputFile)); textFile.write(fileContent); textFile.close(); } catch (IOException e){ App.log(e); Console.displayErrorMessage(i18n.getString("L_TextWindow_cantSave") + ":\n" + outputFile.getAbsolutePath(), title); } }//GEN-LAST:event_jButtonSaveActionPerformed private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed closeDialog(); }//GEN-LAST:event_jButtonCloseActionPerformed /** Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog closeDialog(); }//GEN-LAST:event_closeDialog private void closeDialog() { setVisible(false); if (disposeOnClose) { dispose(); } } private boolean disposeOnClose; private String defaultSaveDirectory; private String proposedFileName; private Font font; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonClipboard; private javax.swing.JButton jButtonClose; private javax.swing.JButton jButtonPrint; private javax.swing.JButton jButtonSave; private javax.swing.JEditorPane jEditorPane; private javax.swing.JPanel jPanel; private javax.swing.JScrollPane jScrollPane; // End of variables declaration//GEN-END:variables // public static void main(String[] args) { // TextWindow w = new TextWindow(null,false,"aaa"); // w.openHtmlPage("http://www.openmap.orgs"); // //"testddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); // w.setVisible(true); // } } //class App { // static void log(Object o){ // System.out.println("App:" + o); // } // static Object module(String name) { // return null; // } // //}