/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bs.bs2d.gui.views;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import org.apache.commons.io.FilenameUtils;
/**
*
* @author Djen
*/
public class GCodeViewControls extends javax.swing.JPanel {
private double objectHeight;
private int maxX, maxY;
public GCodeViewControls(ActionListener alUpdate, ActionListener alGCode, ActionListener alSave,
File[] prints, File[] printers, File[] filaments) {
initComponents();
btnUpdate.addActionListener(alUpdate);
btnGCode.addActionListener(alGCode);
btnSave.addActionListener(alSave);
objectHeight = 10;
ListCellRenderer renderer = new FileCmbRenderer();
cmbPrint.setModel(new DefaultComboBoxModel(prints));
cmbPrint.setRenderer(renderer);
cmbPrinter.setModel(new DefaultComboBoxModel(printers));
cmbPrinter.setRenderer(renderer);
cmbFilament.setModel(new DefaultComboBoxModel(filaments));
cmbFilament.setRenderer(renderer);
}
/**
* 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() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
btnUpdate = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
txtHeight = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
cmbPrint = new javax.swing.JComboBox();
jLabel5 = new javax.swing.JLabel();
cmbPrinter = new javax.swing.JComboBox();
jLabel6 = new javax.swing.JLabel();
cmbFilament = new javax.swing.JComboBox();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
lblDensity = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jLabel7 = new javax.swing.JLabel();
txtParts = new javax.swing.JTextField();
lblMaxTiles = new javax.swing.JLabel();
chkPairs = new javax.swing.JCheckBox();
radUniform = new javax.swing.JRadioButton();
radOptimized = new javax.swing.JRadioButton();
btnGCode = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
setMinimumSize(new java.awt.Dimension(250, 380));
setPreferredSize(new java.awt.Dimension(250, 380));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Print Settings"));
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
jLabel2.setText("Object height:");
txtHeight.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
txtHeight.setText("10");
txtHeight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtHeightActionPerformed(evt);
}
});
txtHeight.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
txtHeightFocusLost(evt);
}
});
jLabel3.setText("mm");
jLabel4.setText("Print:");
jLabel5.setText("Printer:");
jLabel6.setText("Filament:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(107, 107, 107)
.addComponent(btnUpdate))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmbPrint, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmbPrinter, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cmbFilament, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(cmbPrint, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(cmbPrinter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(cmbFilament, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUpdate))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Info"));
jLabel1.setText("Uniform Infill density:");
lblDensity.setText("0.0");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblDensity, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(lblDensity))
);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Object Assembly"));
jLabel7.setText("Parts:");
txtParts.setText("3");
txtParts.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtPartsActionPerformed(evt);
}
});
lblMaxTiles.setText("(Max. 4 x 5)");
chkPairs.setText("Print pairs");
buttonGroup2.add(radUniform);
radUniform.setText("Uniform");
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, chkPairs, org.jdesktop.beansbinding.ELProperty.create("${!selected}"), radUniform, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
buttonGroup2.add(radOptimized);
radOptimized.setSelected(true);
radOptimized.setText("Optimized");
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, chkPairs, org.jdesktop.beansbinding.ELProperty.create("${!selected}"), radOptimized, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
btnGCode.setText("Generate G-Code!");
btnGCode.setEnabled(false);
btnGCode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGCodeActionPerformed(evt);
}
});
btnSave.setText("Save to File...");
btnSave.setEnabled(false);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(btnGCode)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnSave)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtParts))
.addComponent(radOptimized))
.addGap(18, 18, 18)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(radUniform)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(chkPairs))
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(lblMaxTiles)
.addGap(0, 0, Short.MAX_VALUE)))))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(txtParts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblMaxTiles))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(radUniform)
.addComponent(radOptimized)
.addComponent(chkPairs))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnGCode)
.addComponent(btnSave)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, 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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(88, Short.MAX_VALUE))
);
bindingGroup.bind();
}// </editor-fold>//GEN-END:initComponents
private void txtHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHeightActionPerformed
try{
objectHeight = Double.parseDouble(txtHeight.getText());
} catch (NumberFormatException ex){
Toolkit.getDefaultToolkit().beep();
}
}//GEN-LAST:event_txtHeightActionPerformed
private void txtHeightFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtHeightFocusLost
txtHeightActionPerformed(null);
}//GEN-LAST:event_txtHeightFocusLost
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
btnGCode.setEnabled(true);
}//GEN-LAST:event_btnUpdateActionPerformed
private void btnGCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGCodeActionPerformed
btnSave.setEnabled(true);
}//GEN-LAST:event_btnGCodeActionPerformed
private void txtPartsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPartsActionPerformed
int x;
try{
x = Integer.parseInt(txtParts.getText());
} catch (NumberFormatException ex){
x = 0;
}
txtParts.setText("" + Math.min(x, maxX*maxY));
}//GEN-LAST:event_txtPartsActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnGCode;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnUpdate;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JCheckBox chkPairs;
private javax.swing.JComboBox cmbFilament;
private javax.swing.JComboBox cmbPrint;
private javax.swing.JComboBox cmbPrinter;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel lblDensity;
private javax.swing.JLabel lblMaxTiles;
private javax.swing.JRadioButton radOptimized;
private javax.swing.JRadioButton radUniform;
private javax.swing.JTextField txtHeight;
private javax.swing.JTextField txtParts;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration//GEN-END:variables
public boolean getPrintOptimized() {
return radOptimized.isSelected();
}
public int getPartCount(){
txtPartsActionPerformed(null);
return Integer.parseInt(txtParts.getText());
}
public double getObjectHeight() {
return objectHeight;
}
public boolean getPrintPairs(){
return chkPairs.isSelected();
}
public File getPrint(){
return (File) cmbPrint.getSelectedItem();
}
public File getPrinter(){
return (File) cmbPrinter.getSelectedItem();
}
public File getFilament(){
return (File) cmbFilament.getSelectedItem();
}
public void setResultingDensity(double density){
density = (int)(density * 1000) / 1000.0;
lblDensity.setText("" + density);
}
public void setMaxTiles(int x, int y){
maxX = x;
maxY = y;
lblMaxTiles.setText("(Max " + x + " x " + y + ")");
}
private class FileCmbRenderer extends DefaultListCellRenderer{
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel rc = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
String fpath = ((File) value).getPath();
String name = FilenameUtils.getBaseName(fpath);
String path = FilenameUtils.getPath(fpath);
if(path.contains("Slic3r"))
name = "[Slic3r] " + name;
rc.setText(name);
return rc;
}
}
}