se.trixon.jota.client.ui.editor.module.task.TaskExcludePanel.java Source code

Java tutorial

Introduction

Here is the source code for se.trixon.jota.client.ui.editor.module.task.TaskExcludePanel.java

Source

/* 
 * Copyright 2016 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.jota.client.ui.editor.module.task;

import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
import se.trixon.jota.shared.task.ExcludeSection;
import se.trixon.jota.shared.task.Task;
import se.trixon.almond.util.Dict;

/**
 *
 * @author Patrik Karlsson
 */
public class TaskExcludePanel extends TaskModule {

    /**
     * Creates new form ModulePanel
     */
    public TaskExcludePanel() {
        initComponents();
        init();
    }

    @Override
    public void loadTask(Task task) {
        ExcludeSection excludeSection = task.getExcludeSection();

        externalFilePanel.setSelected(excludeSection.isManualFileUsed());
        externalFilePanel.setPath(excludeSection.getManualFilePath());
        externalFilePanel.setEnabled(externalFilePanel.isSelected());

        String[] options = StringUtils.splitPreserveAllTokens(excludeSection.getOptions(), " ");
        for (String option : options) {
            for (Object object : dualListPanel.getAvailableListPanel().getModel().toArray()) {
                OptionHandler optionHandler = (OptionHandler) object;
                if (StringUtils.equals(option, optionHandler.getArg())) {
                    dualListPanel.getSelectedListPanel().getModel().addElement(object);
                    dualListPanel.getAvailableListPanel().getModel().removeElement(object);
                    break;
                }
            }
        }

        dualListPanel.getSelectedListPanel().updateModel();
        dualListPanel.getAvailableListPanel().updateModel();
    }

    @Override
    public Task saveTask(Task task) {
        ExcludeSection excludeSection = task.getExcludeSection();

        excludeSection.setManualFileUsed(externalFilePanel.isSelected());
        excludeSection.setManualFilePath(externalFilePanel.getPath());

        ArrayList<String> options = new ArrayList<>();

        for (Object object : dualListPanel.getSelectedListPanel().getModel().toArray()) {
            options.add(((OptionHandler) object).getArg());
        }

        excludeSection.setOptions(StringUtils.join(options, " "));

        return task;
    }

    private void init() {
        mTitle = Dict.EXCLUDE.toString();
        for (ExcludeOption option : ExcludeOption.values()) {
            if (option.isActive()) {
                dualListPanel.getAvailableListPanel().getModel().addElement(option);
            }
        }
        dualListPanel.getAvailableListPanel().updateModel();
    }

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

        dualListPanel = new se.trixon.jota.client.ui.editor.module.DualListPanel();
        externalFilePanel = new se.trixon.almond.util.swing.dialogs.FileChooserPanel();

        externalFilePanel.setCheckBoxMode(true);
        java.util.ResourceBundle bundle = java.util.ResourceBundle
                .getBundle("se/trixon/jota/client/ui/editor/module/task/Bundle"); // NOI18N
        externalFilePanel.setHeader(bundle.getString("TaskExcludePanel.externalFilePanel.header")); // NOI18N

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private se.trixon.jota.client.ui.editor.module.DualListPanel dualListPanel;
    private se.trixon.almond.util.swing.dialogs.FileChooserPanel externalFilePanel;
    // End of variables declaration//GEN-END:variables
}