/* 
 * Copyright 2015 Patrik Karlsson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.trixon.bivi.db.options;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.DialogDescriptor;
import org.openide.NotificationLineSupport;
import se.trixon.almond.dialogs.FileChooserPanel;
import se.trixon.almond.dictionary.Dict;
import se.trixon.bivi.db.api.AlbumRoot;

/**
 *
 * @author Patrik Karlsson
 */
public class AlbumRootPanel extends javax.swing.JPanel {

    private DialogDescriptor mDialogDescriptor;
    private NotificationLineSupport mNotificationLineSupport;
    private AlbumRoot mAlbumRoot;
    private DocumentListener mDocumentListener;

    /**
     * Creates new form CollectionPanel
     */
    public AlbumRootPanel() {
        initComponents();
        init();
    }

    AlbumRoot getAlbumRoot() {
        saveAlbumRoot();
        return mAlbumRoot;
    }

    void setAlbumRoot(AlbumRoot albumRoot) {
        mAlbumRoot = albumRoot;
        loadAlbumRoot();
        validateInput();
    }

    void setDialogDescriptor(DialogDescriptor dialogDescriptor) {
        mNotificationLineSupport = dialogDescriptor.createNotificationLineSupport();
        mDialogDescriptor = dialogDescriptor;
    }

    private void init() {
        DocumentListener documentListener = new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                validateInput();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                validateInput();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                validateInput();
            }
        };

        nameTextField.getDocument().addDocumentListener(documentListener);
        dirChooserPanel.getTextField().getDocument().addDocumentListener(documentListener);
        dirChooserPanel.setDropMode(FileChooserPanel.DropMode.SINGLE);
        dirChooserPanel.setMode(JFileChooser.DIRECTORIES_ONLY);
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        dirChooserPanel = new se.trixon.almond.dialogs.FileChooserPanel();

        org.openide.awt.Mnemonics.setLocalizedText(nameLabel, Dict.NAME.getString());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameTextField)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(dirChooserPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dirChooserPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private se.trixon.almond.dialogs.FileChooserPanel dirChooserPanel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    // End of variables declaration//GEN-END:variables

    private void loadAlbumRoot() {
        nameTextField.setText(mAlbumRoot.getLabel());
        dirChooserPanel.setPath(mAlbumRoot.getSpecificPath());
    }

    private void saveAlbumRoot() {
        mAlbumRoot.setLabel(nameTextField.getText());
        mAlbumRoot.setSpecificPath(dirChooserPanel.getPath());
    }

    private void validateInput() {
        File dir = new File(dirChooserPanel.getPath());
        boolean valid = nameTextField.getText().isEmpty() == false && dir.isDirectory();
        mDialogDescriptor.setValid(valid);
    }
}

@@ -1074,18 +1074,72 @@
 .JPanel 
+implements FileChooserPanel.FileChooserButtonListener 
 %7B%0A
-
 %0A    pri
@@ -1455,24 +1455,827 @@
 t();%0A    %7D%0A%0A
+    @Override%0A    public void onFileChooserCancel(FileChooserPanel fileChooserPanel) %7B%0A        // nvm%0A    %7D%0A%0A    @Override%0A    public void onFileChooserCheckBoxChange(FileChooserPanel fileChooserPanel, boolean isSelected) %7B%0A        // nvm%0A    %7D%0A%0A    @Override%0A    public void onFileChooserDrop(FileChooserPanel fileChooserPanel) %7B%0A        if (nameTextField.getText().isEmpty()) %7B%0A            nameTextField.setText(new File(fileChooserPanel.getPath()).getName());%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onFileChooserOk(FileChooserPanel fileChooserPanel, File file) %7B%0A        if (nameTextField.getText().isEmpty()) %7B%0A            nameTextField.setText(file.getName());%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onFileChooserPreSelect(FileChooserPanel fileChooserPanel) %7B%0A        // nvm%0A    %7D%0A%0A
     AlbumRoo
@@ -3414,16 +3414,16 @@
 INGLE);%0A
-
         
@@ -3478,16 +3478,65 @@
 _ONLY);%0A
+        dirChooserPanel.setButtonListener(this);%0A
     %7D%0A%0A 

Set directory name as default name