/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.frames;
import pdfdb.gui.customcomponents.MultilineLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import pdfdb.app.EventController;
/** Frame for requesting password from user.
* @author ug22cmg */
public class PasswordRequestFrame extends AppDialog
{
private BottomPanel bottomPanel;
private TopPanel topPanel;
private static final String MESSAGE = "This file is " +
"password protected. To search and use the file in this " +
"application, you must provide a password. This is the same " +
"password you would enter into the program you use to view " +
"file, eg. in Adobe Reader.\n\nIf you do not " +
"provide a password it will be ignored and will not appear " +
"in your search results. If you " +
"do choose to save the password it will not be encrypted and " +
"will be saved in plain text. If you get a virus or install " +
"mallicious software onto your computer, the password could be " +
"read without having to crack any encryption.\n\nIf you have " +
"installed search " +
"plugins, they may be able to access the password of your PDF " +
"file, so you should ensure that you trust all of them before " +
"saving your password.\n\nNever save the password of " +
"confidential files that may include sensitive data.";
/** Initializes with a path, and frame.
* @param path The path to decrypt.
* @param frame The parent frame. */
public PasswordRequestFrame(String path, Frame frame)
{
super(frame);
topPanel.setFileName(path);
}
/** Initializes the UI components */
@Override
protected void initializeComponents()
{
JPanel pane = (JPanel) super.getContentPane();
this.topPanel = new TopPanel();
this.bottomPanel = new BottomPanel();
super.setTitle("Password Request");
super.setSize(new Dimension(450, 350));
super.setPreferredSize(new Dimension(450, 350));
super.setResizable(false);
pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
pane.setLayout(new BorderLayout());
pane.add(new JScrollPane(new MultilineLabel(MESSAGE)),
BorderLayout.CENTER);
pane.add(this.topPanel, BorderLayout.NORTH);
pane.add(this.bottomPanel, BorderLayout.SOUTH);
bottomPanel.getPasswordField().setText("");
setIsRetry(false);
}
/** Sets whether the request is a retry.
* @param retry Whether the operation is a retry. */
public void setIsRetry(boolean retry)
{
if (retry)
{
this.topPanel.setTitle("Re-enter your password");
this.topPanel.setIsRed(true);
}
else
{
this.topPanel.setTitle("Please enter your password");
this.topPanel.setIsRed(false);
}
}
/** Performs the cancel operation. */
public void cancel()
{
setResult(REJECTED);
setVisible(false);
}
/** Performs the ok operation. */
public void confirm()
{
setResult(APPROVED);
setVisible(false);
}
/** Gets the password that the user has entered.
* @return The password. */
public String getPassword()
{
return bottomPanel.getPasswordField().getText();
}
/** The bottom panel */
private class BottomPanel extends JPanel
{
private JTextField passwordField = new JPasswordField();
private ButtonPanel buttonPanel = new ButtonPanel();
/** Initializes the bottom panel */
public BottomPanel()
{
super.setLayout(new BorderLayout());
super.add(passwordField, BorderLayout.NORTH);
super.add(buttonPanel, BorderLayout.CENTER);
}
/** Gets the password field.
* @return The text field instance. */
public JTextField getPasswordField()
{
return passwordField;
}
}
/** Button panel */
private class ButtonPanel extends JPanel
{
private JButton okButton = new JButton("OK");
private JButton cancelButton = new JButton("Cancel");
private final Dimension dim = new Dimension(-1, 50);
private Border bdr = BorderFactory.createEmptyBorder(10, 10, 10, 10);
/** Initializes the button panel */
public ButtonPanel()
{
String comm1 = String.valueOf(EventController.OP_PASSWORD_CONFIRM);
String comm2 = String.valueOf(EventController.OP_PASSWORD_REJECT);
super.setLayout(new GridLayout(1, 4));
super.setBorder(bdr);
super.setPreferredSize(dim);
super.add(new JPanel());
super.add(new JPanel());
this.okButton.setActionCommand(comm1);
this.okButton.addActionListener(EventController.getActionListener());
this.cancelButton.addActionListener(EventController.
getActionListener());
this.cancelButton.setActionCommand(comm2);
super.add(okButton);
super.add(cancelButton);
}
/** Removes the listeners on collection. */
@Override
public void finalize()
{
ActionListener a = EventController.getActionListener();
if (a != null)
{
okButton.removeActionListener(a);
cancelButton.removeActionListener(a);
}
}
}
/** Top panel containing title */
private class TopPanel extends JPanel
{
private final Font font = new Font("Arial", Font.PLAIN, 24);
private MultilineLabel fLabel = new MultilineLabel();
private MultilineLabel titleLabel = new MultilineLabel();
/** Initializes the top panel */
public TopPanel()
{
Dimension dim = new Dimension(-1, 50);
titleLabel.setFont(font);
super.setLayout(new GridLayout(2, 1));
super.add(titleLabel);
super.add(fLabel);
super.setSize(dim);
super.setPreferredSize(dim);
super.setBorder(BorderFactory.createEmptyBorder());
}
/** Sets the filename title.
* @param fileName The new filename. */
public void setFileName(String fileName)
{
fLabel.setText("For File: " + fileName);
repaint();
}
/** Sets whether the text is red or not.
* @param red True for red, False for black. */
public void setIsRed(boolean red)
{
titleLabel.setForeground(red ? Color.RED : Color.BLACK);
repaint();
}
/** Sets the new title.
* @param title THe title.*/
public void setTitle(String title)
{
titleLabel.setText(title);
repaint();
}
}
/** Test method for isolating a windows specific bug.
* @param args unused*/
public static void main(String[] args)
{
new PasswordRequestFrame("c:\\program files\\adobe\\acrobat.pdf", null).
setVisible(true);
}
}