/** * Copyright (C) 2015 drrb * * 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/>. */ package com.github.drrb.rust.netbeans.configuration; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import javax.swing.JFileChooser; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; final class RustPanel extends javax.swing.JPanel { private final RustOptionsPanelController controller; private final RustConfiguration configuration; RustPanel(RustOptionsPanelController controller) { this.controller = controller; this.configuration = RustConfiguration.get(); initComponents(); this.cargoPathField.addKeyListener(new ChangeListener()); this.librariesPathsTableModel.addTableModelListener(new ChangeListener()); } /** * 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() { librariesPathsTableModel = new com.github.drrb.rust.netbeans.swing.PathsTableModel(); cargoPathLabel = new javax.swing.JLabel(); librariesPathsLabel = new javax.swing.JLabel(); cargoPathField = new javax.swing.JTextField(); cargoPathBrowseButton = new javax.swing.JButton(); librariesPathsTableScrollPane = new javax.swing.JScrollPane(); librariesPathsTable = new javax.swing.JTable(); librariesPathsAddButton = new javax.swing.JButton(); librariesPathsRemoveButton = new javax.swing.JButton(); org.openide.awt.Mnemonics.setLocalizedText(cargoPathLabel, org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.cargoPathLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(librariesPathsLabel, org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.librariesPathsLabel.text")); // NOI18N librariesPathsLabel.setToolTipText(org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.librariesPathsLabel.toolTipText")); // NOI18N cargoPathField.setText(org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.cargoPathField.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(cargoPathBrowseButton, org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.cargoPathBrowseButton.text")); // NOI18N cargoPathBrowseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cargoPathBrowseButtonActionPerformed(evt); } }); librariesPathsTable.setModel(librariesPathsTableModel); librariesPathsTable.setTableHeader(null); librariesPathsTableScrollPane.setViewportView(librariesPathsTable); org.openide.awt.Mnemonics.setLocalizedText(librariesPathsAddButton, org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.librariesPathsAddButton.text")); // NOI18N librariesPathsAddButton.setToolTipText(org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.librariesPathsAddButton.toolTipText")); // NOI18N librariesPathsAddButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { librariesPathsAddButtonActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(librariesPathsRemoveButton, org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.librariesPathsRemoveButton.text")); // NOI18N librariesPathsRemoveButton.setToolTipText(org.openide.util.NbBundle.getMessage(RustPanel.class, "RustPanel.librariesPathsRemoveButton.toolTipText")); // NOI18N librariesPathsRemoveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { librariesPathsRemoveButtonActionPerformed(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() .addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(librariesPathsLabel) .addComponent(cargoPathLabel)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(librariesPathsTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(cargoPathField, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(cargoPathBrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE) .addComponent(librariesPathsAddButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(librariesPathsRemoveButton, javax.swing.GroupLayout.Alignment.TRAILING, 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cargoPathLabel) .addComponent(cargoPathField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cargoPathBrowseButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(librariesPathsLabel) .addContainerGap()) .addComponent(librariesPathsTableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(librariesPathsAddButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(librariesPathsRemoveButton) .addGap(0, 0, Short.MAX_VALUE)))) ); }// </editor-fold>//GEN-END:initComponents private void cargoPathBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargoPathBrowseButtonActionPerformed JFileChooser fileChooser = new JFileChooser(new File(configuration.getCargoPath()).getParent()); fileChooser.setDialogTitle("Select Path to Cargo Executable"); //TODO: what will happen if the file doesn't exist? fileChooser.setSelectedFile(new File(configuration.getCargoPath())); int result = fileChooser.showDialog(this, "Select"); if(result == JFileChooser.APPROVE_OPTION) { cargoPathField.setText(fileChooser.getSelectedFile().getAbsolutePath()); } }//GEN-LAST:event_cargoPathBrowseButtonActionPerformed private void librariesPathsAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_librariesPathsAddButtonActionPerformed JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Add Path to Libraries"); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int result = fileChooser.showDialog(this, "Add"); if(result == JFileChooser.APPROVE_OPTION) { librariesPathsTableModel.addPath(fileChooser.getSelectedFile().getAbsolutePath()); } }//GEN-LAST:event_librariesPathsAddButtonActionPerformed private void librariesPathsRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_librariesPathsRemoveButtonActionPerformed librariesPathsTableModel.removePathsAt(librariesPathsTable.getSelectedRows()); }//GEN-LAST:event_librariesPathsRemoveButtonActionPerformed void load() { cargoPathField.setText(configuration.getCargoPath()); librariesPathsTableModel.setPaths(configuration.getLibrariesPaths()); } void store() { configuration.setCargoPath(cargoPathField.getText()); configuration.setLibrariesPaths(librariesPathsTableModel.getPaths()); } boolean valid() { // TODO check whether form is consistent and complete return true; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cargoPathBrowseButton; private javax.swing.JTextField cargoPathField; private javax.swing.JLabel cargoPathLabel; private javax.swing.JButton librariesPathsAddButton; private javax.swing.JLabel librariesPathsLabel; private javax.swing.JButton librariesPathsRemoveButton; private javax.swing.JTable librariesPathsTable; private com.github.drrb.rust.netbeans.swing.PathsTableModel librariesPathsTableModel; private javax.swing.JScrollPane librariesPathsTableScrollPane; // End of variables declaration//GEN-END:variables private class ChangeListener implements KeyListener, TableModelListener { @Override public void keyTyped(KeyEvent e) { controller.changed(); } @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override public void tableChanged(TableModelEvent e) { controller.changed(); } } }