/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 
 * TokenRequestDialog.java
 *
 * Created on 2. huhtikuuta 2008, 12:04
 */

package org.wandora.application.tools.extractors.flickr;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import org.wandora.application.Wandora;

/**
 *
 * @author  anttirt
 */
public class TokenRequestDialog extends javax.swing.JDialog {
    

	private static final long serialVersionUID = 1L;
	
	private boolean mCancelled;
    private String requiredAuth;
    private FlickrState flickrState;
    
    
    /** Creates new form TokenRequestDialog */
    public TokenRequestDialog(java.awt.Frame parent, boolean modal, String authURL, String reqAuth, FlickrState state) {
        super(parent, modal);
        requiredAuth = reqAuth;
        flickrState = state;
        initComponents();
        urlField.setText(authURL);
        mCancelled = false;
        this.setSize(600,280);
        if(parent instanceof Wandora) {
            Wandora w = (Wandora)parent;
            w.centerWindow(this);
        }
    }
    
    
    public boolean cancelled() { 
        return mCancelled; 
    }
    
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelPanel = new javax.swing.JPanel();
        titleLabel = new org.wandora.application.gui.simple.SimpleLabel();
        authInfoPanel = new javax.swing.JPanel();
        authDisplay1 = new AuthDisplay(this.flickrState, this.requiredAuth);
        wandoraLabel2 = new org.wandora.application.gui.simple.SimpleLabel();
        urlPanel = new javax.swing.JPanel();
        urlField = new javax.swing.JTextField();
        clipboardBtn = new org.wandora.application.gui.simple.SimpleButton();
        buttonPanel = new javax.swing.JPanel();
        fillerPanel = new javax.swing.JPanel();
        okButton = new org.wandora.application.gui.simple.SimpleButton();
        cancelButton = new org.wandora.application.gui.simple.SimpleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Flickr authorization required");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        labelPanel.setLayout(new java.awt.GridBagLayout());

        titleLabel.setText("<html><body><p>Wandora requires your authorization before it can read your photos and data from Flickr.</p></body></html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        labelPanel.add(titleLabel, gridBagConstraints);

        authInfoPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));
        authInfoPanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 15);
        authInfoPanel.add(authDisplay1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 0, 5);
        labelPanel.add(authInfoPanel, gridBagConstraints);

        wandoraLabel2.setText("<html><body><p>Authorizing is a simple process which takes place in your web browser. Copy the URL below to your browser and log in. When you're finished, return to this window and complete authorization.</p></body></html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        labelPanel.add(wandoraLabel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
        getContentPane().add(labelPanel, gridBagConstraints);

        urlPanel.setLayout(new java.awt.GridBagLayout());

        urlField.setPreferredSize(new java.awt.Dimension(6, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        urlPanel.add(urlField, gridBagConstraints);

        clipboardBtn.setText("Copy");
        clipboardBtn.setMargin(new java.awt.Insets(2, 2, 2, 2));
        clipboardBtn.setMinimumSize(new java.awt.Dimension(120, 21));
        clipboardBtn.setPreferredSize(new java.awt.Dimension(50, 21));
        clipboardBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clipboardBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        urlPanel.add(clipboardBtn, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 10, 0, 10);
        getContentPane().add(urlPanel, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridBagLayout());

        javax.swing.GroupLayout fillerPanelLayout = new javax.swing.GroupLayout(fillerPanel);
        fillerPanel.setLayout(fillerPanelLayout);
        fillerPanelLayout.setHorizontalGroup(
            fillerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        fillerPanelLayout.setVerticalGroup(
            fillerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        buttonPanel.add(fillerPanel, gridBagConstraints);

        okButton.setText("Complete");
        okButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        okButton.setPreferredSize(new java.awt.Dimension(80, 23));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        buttonPanel.add(okButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.setMinimumSize(new java.awt.Dimension(70, 21));
        cancelButton.setPreferredSize(new java.awt.Dimension(70, 23));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        buttonPanel.add(cancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 10, 10, 10);
        getContentPane().add(buttonPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        setVisible(false);
}//GEN-LAST:event_okButtonActionPerformed

    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        mCancelled = true;
        setVisible(false);
}//GEN-LAST:event_cancelButtonActionPerformed

    
    private void clipboardBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clipboardBtnActionPerformed
        StringSelection sel = new StringSelection(urlField.getText());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(sel, new ClipboardOwner() {
            @Override
            public void lostOwnership(Clipboard clipboard, Transferable contents) {
                
            }
        });
    }//GEN-LAST:event_clipboardBtnActionPerformed
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TokenRequestDialog dialog = new TokenRequestDialog(new javax.swing.JFrame(), true, "lksjgfhlksujehrltkjsdnlgkjhltghlskdjfghliseurhglskjdfhgiluserhglksjlksjgfhlksujehrltkjsdnlgkjhltghlskdjfghliseurhglskjdfhgiluserhglksjlksjgfhlksujehrltkjsdnlgkjhltghlskdjfghliseurhglskjdfhgiluserhglksj");
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
        */
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.wandora.application.tools.extractors.flickr.AuthDisplay authDisplay1;
    private javax.swing.JPanel authInfoPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton clipboardBtn;
    private javax.swing.JPanel fillerPanel;
    private javax.swing.JPanel labelPanel;
    private javax.swing.JButton okButton;
    private org.wandora.application.gui.simple.SimpleLabel titleLabel;
    private javax.swing.JTextField urlField;
    private javax.swing.JPanel urlPanel;
    private org.wandora.application.gui.simple.SimpleLabel wandoraLabel2;
    // End of variables declaration//GEN-END:variables
    
}
