001: package gruntspud.ui;
002:
003: import gruntspud.Constants;
004:
005: import java.awt.BorderLayout;
006: import java.awt.Color;
007: import java.awt.Component;
008: import java.awt.Dialog;
009: import java.awt.Dimension;
010: import java.awt.FlowLayout;
011: import java.awt.Frame;
012: import java.awt.GridBagConstraints;
013: import java.awt.Window;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.io.BufferedReader;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.InputStreamReader;
020: import java.net.URL;
021:
022: import javax.swing.BorderFactory;
023: import javax.swing.ButtonGroup;
024: import javax.swing.JButton;
025: import javax.swing.JDialog;
026: import javax.swing.JOptionPane;
027: import javax.swing.JPanel;
028: import javax.swing.JRadioButton;
029: import javax.swing.JScrollPane;
030: import javax.swing.JTextArea;
031: import javax.swing.SwingConstants;
032: import javax.swing.SwingUtilities;
033: import javax.swing.text.BadLocationException;
034:
035: public class LicensePanel extends JPanel {
036:
037: private JTextArea licenseText;
038: private JRadioButton accept, decline;
039:
040: public LicensePanel() {
041: super (new BorderLayout());
042: JPanel t = new JPanel(new BorderLayout());
043: t.add(new FolderBar("Gruntspud Licenses", UIUtil
044: .getCachedIcon(Constants.GRUNTSPUD_LOGO_STATIC)),
045: BorderLayout.NORTH);
046: MultilineLabel l = new MultilineLabel(
047: "Please read and accept or decline the licenses below.\n"
048: + "Gruntspud contains components from a number of sources that\n"
049: + "may be made available under licenses other than the GPL.");
050: l.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
051: t.add(l, BorderLayout.SOUTH);
052: add(t, BorderLayout.NORTH);
053: licenseText = new JTextArea();
054: licenseText.setEditable(false);
055: JScrollPane scroller = new JScrollPane(licenseText);
056: scroller.setBorder(BorderFactory.createCompoundBorder(
057: BorderFactory.createEmptyBorder(4, 4, 4, 4),
058: BorderFactory.createLineBorder(Color.black)));
059: add(scroller, BorderLayout.CENTER);
060: ButtonGroup bg = new ButtonGroup();
061: accept = new JRadioButton(
062: "I accept these terms and conditions and wish to continue");
063: accept.setMnemonic('a');
064: accept.setHorizontalAlignment(JRadioButton.LEFT);
065: bg.add(accept);
066: decline = new JRadioButton(
067: "I decline these terms and conditions and wish to exit");
068: decline.setMnemonic('d');
069: decline.setHorizontalAlignment(JRadioButton.LEFT);
070: bg.add(decline);
071: JPanel p = new JPanel(new BorderLayout());
072: p.add(accept, BorderLayout.NORTH);
073: p.add(decline, BorderLayout.SOUTH);
074: add(p, BorderLayout.SOUTH);
075: }
076:
077: public void addLicenseText(URL resource) throws IOException {
078: InputStream in = null;
079: try {
080: in = resource.openStream();
081: BufferedReader reader = new BufferedReader(
082: new InputStreamReader(in));
083: String line = null;
084: while ((line = reader.readLine()) != null) {
085: licenseText.getDocument().insertString(
086: licenseText.getDocument().getLength(),
087: line + "\n", null);
088: }
089: } catch (BadLocationException ble) {
090: throw new IOException("Could not add license.");
091: } finally {
092: if (in != null) {
093: try {
094: in.close();
095: } catch (IOException ioe) {
096: }
097: }
098: }
099: }
100:
101: public boolean showLicense(Component parent) {
102: licenseText.setCaretPosition(0);
103: licenseText.scrollRectToVisible(licenseText.getVisibleRect());
104: Window w = (Window) SwingUtilities.getAncestorOfClass(
105: Window.class, parent);
106: JDialog dialog = null;
107: if (w instanceof Dialog) {
108: dialog = new JDialog((Dialog) w, "License", true);
109: } else {
110: dialog = new JDialog((Frame) w, "License", true);
111: }
112: JPanel b = new JPanel(new FlowLayout(FlowLayout.RIGHT));
113: JButton next = new JButton("Continue");
114: final JDialog d = dialog;
115: next.addActionListener(new ActionListener() {
116: public void actionPerformed(ActionEvent evt) {
117: if (!accept.isSelected() && !decline.isSelected()) {
118: JOptionPane
119: .showMessageDialog(LicensePanel.this ,
120: "You must select accept or decline license.");
121: } else {
122: d.dispose();
123: }
124: }
125: });
126: b.add(next);
127: dialog.getContentPane().add(this , BorderLayout.CENTER);
128: dialog.getContentPane().add(b, BorderLayout.SOUTH);
129: dialog.setSize(new Dimension(420, 480));
130: UIUtil.positionComponent(SwingConstants.CENTER, dialog);
131: dialog.setVisible(true);
132: return accept.isSelected();
133: }
134: }
|