com.qualixium.executor.ToolbarPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.qualixium.executor.ToolbarPanel.java

Source

/*
 * Copyright 2016 Qualixium.com
 *
 * 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 com.qualixium.executor;

import com.fasterxml.jackson.databind.JsonNode;
import com.qualixium.executor.command.Command;
import com.qualixium.executor.command.CommandsConfigurationDialog;
import static com.qualixium.executor.command.CommandsConfigurationDialog.COLUMN_COMMAND;
import static com.qualixium.executor.command.CommandsConfigurationDialog.COLUMN_NAME;
import static com.qualixium.executor.command.CommandsConfigurationDialog.CUSTOM_COMMANDS;
import static com.qualixium.executor.command.CommandsConfigurationDialog.MAPPER;
import com.qualixium.executor.util.BoundsPopupMenuListener;
import java.io.IOException;
import javax.swing.DefaultComboBoxModel;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.api.extexecution.ExecutionService;
import org.netbeans.api.extexecution.ExternalProcessBuilder;
import org.openide.LifecycleManager;
import org.openide.util.Exceptions;
import org.openide.util.NbPreferences;

/**
 *
 * @author pedro
 */
public class ToolbarPanel extends javax.swing.JPanel {

    public ToolbarPanel() {
        initComponents();
        myOwnInitComponents();
    }

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

        btnCommandConfig = new javax.swing.JButton();
        cbxCommands = new javax.swing.JComboBox();

        btnCommandConfig.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/com/qualixium/executor/ShellDataIcon.gif"))); // NOI18N
        btnCommandConfig.setToolTipText("Edit my custom shell commands");
        btnCommandConfig.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        btnCommandConfig.setMaximumSize(new java.awt.Dimension(24, 24));
        btnCommandConfig.setMinimumSize(new java.awt.Dimension(24, 24));
        btnCommandConfig.setPreferredSize(new java.awt.Dimension(30, 24));
        btnCommandConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCommandConfigActionPerformed(evt);
            }
        });

        cbxCommands.setToolTipText("Execute a shell command");
        cbxCommands.setMaximumSize(new java.awt.Dimension(20, 24));
        cbxCommands.setMinimumSize(new java.awt.Dimension(20, 24));
        cbxCommands.setPreferredSize(new java.awt.Dimension(20, 24));
        cbxCommands.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxCommandsActionPerformed(evt);
            }
        });

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

    private void btnCommandConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCommandConfigActionPerformed
        CommandsConfigurationDialog dialog = new CommandsConfigurationDialog(null, true, this);
        dialog.setVisible(true);
    }//GEN-LAST:event_btnCommandConfigActionPerformed

    private void cbxCommandsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxCommandsActionPerformed
        try {
            Command command = (Command) cbxCommands.getSelectedItem();

            ExecutionDescriptor descriptor = new ExecutionDescriptor().frontWindow(true).showProgress(true)
                    .inputVisible(true).controllable(true)
                    .preExecution(() -> LifecycleManager.getDefault().saveAll());

            String[] commandStringArray = command.command.split(" ");
            //TODO ExternalProcessBuilder is deprecated. Remove it
            ExternalProcessBuilder processBuilder = new ExternalProcessBuilder(commandStringArray[0]);

            if (commandStringArray.length > 1) {
                for (String commandString : commandStringArray) {
                    if (!commandString.equals(commandStringArray[0])) {
                        processBuilder = processBuilder.addArgument(commandString);
                    }

                }
            }

            ExecutionService service = ExecutionService.newService(processBuilder, descriptor, command.name);

            service.run();
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }//GEN-LAST:event_cbxCommandsActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCommandConfig;
    private javax.swing.JComboBox cbxCommands;
    // End of variables declaration//GEN-END:variables
    BoundsPopupMenuListener popupMenuListener = new BoundsPopupMenuListener(true, true, 500, false);

    private void myOwnInitComponents() {
        initializeCbxCommands();
    }

    public void initializeCbxCommands() {
        DefaultComboBoxModel<Command> modelCommands = new DefaultComboBoxModel<>();
        String jsonCustomCommands = NbPreferences.forModule(CommandsConfigurationDialog.class).get(CUSTOM_COMMANDS,
                "");

        if (!jsonCustomCommands.isEmpty()) {
            try {
                JsonNode jsonData = MAPPER.readTree(jsonCustomCommands);
                for (JsonNode node : jsonData) {
                    modelCommands.addElement(
                            new Command(node.get(COLUMN_NAME).textValue(), node.get(COLUMN_COMMAND).textValue()));
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        cbxCommands.setModel(modelCommands);
        cbxCommands.addPopupMenuListener(popupMenuListener);
        cbxCommands.setPrototypeDisplayValue("scrot -cd 5 /home/pedro/Desktop/file1.png");
    }
}