package org.gephi.ui.project;

import org.gephi.desktop.project.ProjectControllerUIImpl;
import org.gephi.project.api.Workspace;
import org.gephi.project.api.WorkspaceMetaData;
import org.netbeans.validation.api.builtin.stringvalidation.StringValidators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.swing.ValidationPanel;
import org.openide.util.Lookup;

public class WorkspacePropertiesEditor extends javax.swing.JPanel {

    /**
     * Creates new form WorkspacePropertiesEditor
     */
    public WorkspacePropertiesEditor() {
        initComponents();
    }

    public static ValidationPanel createValidationPanel(WorkspacePropertiesEditor innerPanel) {
        ValidationPanel validationPanel = new ValidationPanel();
        validationPanel.setInnerComponent(innerPanel);
        ValidationGroup group = validationPanel.getValidationGroup();

        //Make sure components have names
        innerPanel.nameTextField.setName(innerPanel.labelName.getText().replace(":", ""));

        group.add(innerPanel.nameTextField, StringValidators.REQUIRE_NON_EMPTY_STRING);

        return validationPanel;
    }

    public void setup(Workspace workspace) {
        WorkspaceMetaData info = workspace.getWorkspaceMetadata();
        descriptionTextArea.setText(info.getDescription());
        titleTextField.setText(info.getTitle());
        nameTextField.setText(workspace.getName());
    }

    public void unsetup(Workspace workspace) {
        WorkspaceMetaData info = workspace.getWorkspaceMetadata();
        if (!descriptionTextArea.getText().isEmpty() && !descriptionTextArea.getText().equals(info.getDescription())) {
            info.setDescription(descriptionTextArea.getText());
        }

        if (!nameTextField.getText().isEmpty() && !nameTextField.getText().equals(workspace.getName())) {
            Lookup.getDefault().lookup(ProjectControllerUIImpl.class).renameWorkspace(nameTextField.getText());
        }

        if (!titleTextField.getText().isEmpty() && !titleTextField.getText().equals(info.getTitle())) {
            info.setTitle(titleTextField.getText());
        }
    }

    /**
     * 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() {

        innerPanel = new javax.swing.JPanel();
        nameTextField = new javax.swing.JTextField();
        descriptionScrollPane = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        labelName = new javax.swing.JLabel();
        labelDescription = new javax.swing.JLabel();
        labelTitle = new javax.swing.JLabel();
        titleTextField = new javax.swing.JTextField();

        innerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
            org.openide.util.NbBundle.getMessage(WorkspacePropertiesEditor.class,
                "WorkspacePropertiesEditor.innerPanel.border.title"))); // NOI18N

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(3);
        descriptionScrollPane.setViewportView(descriptionTextArea);

        labelName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(labelName,
            org.openide.util.NbBundle.getMessage(WorkspacePropertiesEditor.class,
                "WorkspacePropertiesEditor.labelName.text")); // NOI18N

        labelDescription.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(labelDescription,
            org.openide.util.NbBundle.getMessage(WorkspacePropertiesEditor.class,
                "WorkspacePropertiesEditor.labelDescription.text")); // NOI18N

        labelTitle.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(labelTitle,
            org.openide.util.NbBundle.getMessage(WorkspacePropertiesEditor.class,
                "WorkspacePropertiesEditor.labelTitle.text")); // NOI18N

        javax.swing.GroupLayout innerPanelLayout = new javax.swing.GroupLayout(innerPanel);
        innerPanel.setLayout(innerPanelLayout);
        innerPanelLayout.setHorizontalGroup(
            innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(innerPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(innerPanelLayout.createSequentialGroup()
                            .addComponent(labelName, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(nameTextField))
                        .addGroup(innerPanelLayout.createSequentialGroup()
                            .addGroup(
                                innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(labelDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 82,
                                        Short.MAX_VALUE)
                                    .addComponent(labelTitle, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(titleTextField)
                                .addComponent(descriptionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 235,
                                    Short.MAX_VALUE))))
                    .addContainerGap())
        );
        innerPanelLayout.setVerticalGroup(
            innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(innerPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(labelName))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelTitle)
                        .addComponent(titleTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(innerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(innerPanelLayout.createSequentialGroup()
                            .addComponent(labelDescription)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(descriptionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 150,
                            Short.MAX_VALUE))
                    .addGap(17, 17, 17))
        );

        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()
                    .addComponent(innerPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(innerPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane descriptionScrollPane;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JPanel innerPanel;
    private javax.swing.JLabel labelDescription;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelTitle;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTextField titleTextField;
    // End of variables declaration//GEN-END:variables
}
