/* * CryptoFileNative.java * * * ==================================================== Professional Data Security (PDS) http://crypto.brettlee.com ==================================================== Copyright (c) 2009-2011, Brett Lee All rights reserved. Portions Copyright (C) 1995-2008, Sun Microsystems, Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the ORGANIZATION nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ============================================================================= */ package com.brettlee.crypto; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Insets; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextPane; import javax.swing.text.StyledEditorKit; import java.util.HashMap; public class CryptoFileNative extends CryptoFile { String newline = System.getProperty("line.separator"); boolean isStyledDocument = false; MyDefaultStyledDocument document; MyDefaultStyledDocument savedDocument; // constructor public CryptoFileNative () { cryptoFileId = StateObject.random.nextInt(); document = new MyDefaultStyledDocument(); savedDocument = new MyDefaultStyledDocument(); JTextArea area = new JTextArea(); area.setDocument( document ); } // Getters and setters // MyDefaultStyledDocument getActiveDocument() { return document; } void setActiveDocument(MyDefaultStyledDocument doc) { document = doc; } MyDefaultStyledDocument getSavedDocument() { return savedDocument; } boolean setSavedDocument() { try { savedDocument.remove(0, savedDocument.getLength()); savedDocument.insertString(0, document.getText(0, document.getLength()), null); } catch (Exception ex) { System.out.println("Error setting savedDocument text: " + ex ); return false; } document.setDocumentChanged(false); return true; } boolean getIsStyledDocument() { return isStyledDocument; } void setIsStyledDocument( boolean truth ) { isStyledDocument = truth; } /** * Open a CryptoFile for editing. * */ boolean editFile( CryptoFileNative cryptoFile ) { // System.out.println(newline + "Beginning CryptoFileNative().editFile()..."); // System.out.println("File to edit: " + cryptoFile.getFullyPathedFileName() ); JFrame frame = new JFrame(); new AuthDialog(frame, true, 6, cryptoFile); // Was the Auth Dialog action canceled - yes, we need a getAuthCancel() if ( StateObject.getFileChooserCancel() ) { return false; } // System.out.println("After AuthDialog 1"); // System.out.println("CryptoFile File : " + cryptoFile.getFullyPathedFileName() ); // System.out.println("CryptoFile KeyStore Name : " + cryptoFile.getKsName() ); // System.out.print("KeyStore Passphrase: "); // if ( cryptoFile.getKsPass() != null ) { // for ( int i=0; i < cryptoFile.getKsPass().length; i++ ) { // System.out.print( cryptoFile.getKsPass()[i] ); // } // } // System.out.println(); cryptoFile.setAction( 1 ); // System.out.println("Action to perform (0=encrypt, 1=decrypt) : " + cryptoFile.getAction() ); try { if (! new CryptoEngine().decrypt ( cryptoFile )) { return false; } } catch (Exception e) { System.out.println("Error caught in CryptoFileNative().editFile()."); System.out.println("Error decrypting: " + e ); return false; } if ( ! cryptoFile.setSavedDocument() ) { System.out.println("Unable to create Document savedDocument"); return false; } return true; } /** * Open a CryptoFile for viewing. * */ boolean viewFile( CryptoFileNative cryptoFile ) { // System.out.println("In CryptoFileNative().viewFile()..."); // System.out.println("File to view: " + cryptoFile.getFullyPathedFileName() ); JFrame frame = new JFrame(); new AuthDialog(frame, true, 6, cryptoFile); // Was the Auth Dialog action canceled - yes, we need a getAuthCancel() if ( StateObject.getFileChooserCancel() ) { StateObject.setFileChooserCancel(false); return true; } // System.out.println("After AuthDialog 1"); // System.out.println("CryptoFile File : " + cryptoFile.getFullyPathedFileName() ); // System.out.println("CryptoFile KeyStore Name : " + cryptoFile.getKsName() ); // System.out.print("KeyStore Passphrase: "); // if ( cryptoFile.getKsPass() != null ) { // for ( int i=0; i < cryptoFile.getKsPass().length; i++ ) { // System.out.print( cryptoFile.getKsPass()[i] ); // } // } // System.out.println(); cryptoFile.setAction( 1 ); // System.out.println("Action to perform (0=encrypt, 1=decrypt) : " + cryptoFile.getAction() ); try { if (! new CryptoEngine().decrypt ( cryptoFile ) ) { return false; } } catch (Exception e) { System.out.println("Error caught in CryptoFileNative().viewFile()..."); System.out.println("Error decrypting: " + e ); return false; } new ViewDoc( cryptoFile ); return true; } class ViewDoc extends JFrame { HashMap actions; public ViewDoc ( CryptoFileNative cryptoFile ) { super(); JTextPane textPane = new JTextPane(); textPane.setDocument( cryptoFile.getActiveDocument() ); textPane.setBackground( new Color( 211,211,211) ); textPane.setMargin( new Insets(10, 10, 10, 10) ); textPane.setEditable(false); textPane.setOpaque(true); textPane.setCaretPosition(0); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(640,240)); add(scrollPane); // Set up the menu bar. JMenu fontMenu = createFontMenu(); JMenuBar menuBar = new JMenuBar(); menuBar.add(fontMenu); setJMenuBar(menuBar); setTitle(cryptoFile.getFileName() + " - (Read only) - " + cryptoFile.getFullyPathedFileName() ); JLabel footerLabel = new JLabel("Data Encryption - Security you can Trust", JLabel.CENTER); add(footerLabel, BorderLayout.PAGE_END); pack(); setLocationRelativeTo(null); setVisible(true); } // The rest of this class is from the java Swing Tutorials - TextComponentDemo.java // // Create the font menu. protected JMenu createFontMenu() { JMenu menu = new JMenu("Text Format"); Action action = new StyledEditorKit.BoldAction(); action.putValue(Action.NAME, "Bold"); menu.add(action); action = new StyledEditorKit.ItalicAction(); action.putValue(Action.NAME, "Italic"); menu.add(action); action = new StyledEditorKit.UnderlineAction(); action.putValue(Action.NAME, "Underline"); menu.add(action); menu.addSeparator(); menu.add(new StyledEditorKit.FontSizeAction("10", 10)); menu.add(new StyledEditorKit.FontSizeAction("11", 11)); menu.add(new StyledEditorKit.FontSizeAction("12", 12)); menu.add(new StyledEditorKit.FontSizeAction("14", 14)); menu.add(new StyledEditorKit.FontSizeAction("16", 16)); menu.add(new StyledEditorKit.FontSizeAction("18", 18)); menu.add(new StyledEditorKit.FontSizeAction("20", 20)); menu.addSeparator(); menu.add(new StyledEditorKit.FontFamilyAction("Arial", "Arial")); menu.add(new StyledEditorKit.FontFamilyAction("Arial Narrow", "Arial Narrow")); menu.add(new StyledEditorKit.FontFamilyAction("Courier", "Courier")); menu.add(new StyledEditorKit.FontFamilyAction("Times New Roman", "Times New Roman")); menu.add(new StyledEditorKit.FontFamilyAction("Verdana", "Verdana")); menu.addSeparator(); menu.add(new StyledEditorKit.ForegroundAction("Red", Color.red)); menu.add(new StyledEditorKit.ForegroundAction("Green", Color.green)); menu.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue)); menu.add(new StyledEditorKit.ForegroundAction("Black", Color.black)); return menu; } } }