////////////////////////////////OptionsWindow//////////////////////////////////
package cu.trustGrapher;
import cu.trustGrapher.eventplayer.EventPlayer;
import cu.trustGrapher.eventplayer.PlaybackPanel;
import aohara.utilities.ChatterBox;
import aohara.utilities.PropertyManager;
/**
* This window lets you select options that pertain to the simulator itself, and not the algorithms.
* All otpions will be saved to the TrustGrapher properties file, and if the objects that the options pertains to exists,
* the options will be applied to them. Otherwise, they will be applied when they are created since they check the properties file.
* @author Andrew O'Hara
*/
public class OptionsWindow extends javax.swing.JFrame {
public static final String DELAY = "delay", SCRUB_MODE = "scrubMode";
protected TrustGrapher trustGrapher;
//////////////////////////////////Constructor///////////////////////////////////
public OptionsWindow(TrustGrapher trustGrapher) {
this.trustGrapher = trustGrapher;
initComponents();
PropertyManager config = trustGrapher.getPropertyManager();
if (config.containsKey(DELAY)) { //Load current delay from properties
if (config.getProperty(DELAY).equals("" + EventPlayer.DEFAULT_DELAY)) { //If the current delay is the default delay
defaultDelayButton.doClick();
delayField.setText("");
} else { //Otherwise, the delay is custom
customDelayButton.doClick();
delayField.setText(config.getProperty(DELAY));
}
}
if (config.containsKey(SCRUB_MODE)) { //Load current playbackSlider mode from properties
if (Boolean.parseBoolean(config.getProperty(SCRUB_MODE))) { //If the mode is set to scrub
scrubButton.doClick();
} else { //Othwerise, it must be set to drag & drop
dragDropButton.doClick();
}
}
setVisible(true);
}
////////////////////////////////Static Methods//////////////////////////////////
public static void run(TrustGrapher trustGrapher) {
OptionsWindow optionsWindow = new OptionsWindow(trustGrapher);
}
/////////////////////////////////GUI Components/////////////////////////////////
/** 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() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
defaultDelayButton = new javax.swing.JRadioButton();
customDelayButton = new javax.swing.JRadioButton();
delayField = new javax.swing.JTextField();
delayLabel = new javax.swing.JLabel();
msLabel = new javax.swing.JLabel();
javax.swing.JPanel jPanel2 = new javax.swing.JPanel();
scrubButton = new javax.swing.JRadioButton();
dragDropButton = new javax.swing.JRadioButton();
javax.swing.JButton cancelButton = new javax.swing.JButton();
javax.swing.JButton okButton = new javax.swing.JButton();
setTitle("TrustGrapher Options");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Delay Between each Event"));
buttonGroup1.add(defaultDelayButton);
defaultDelayButton.setSelected(true);
defaultDelayButton.setText("Default Delay (250 ms)");
defaultDelayButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
defaultDelayButtonActionPerformed(evt);
}
});
buttonGroup1.add(customDelayButton);
customDelayButton.setText("Custom Delay");
customDelayButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
customDelayButtonActionPerformed(evt);
}
});
delayField.setColumns(4);
delayField.setText("250");
delayField.setEnabled(false);
delayLabel.setText("Delay: ");
delayLabel.setEnabled(false);
msLabel.setText("ms");
msLabel.setEnabled(false);
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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(delayLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(delayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(msLabel))
.addComponent(defaultDelayButton)
.addComponent(customDelayButton)))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(defaultDelayButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(customDelayButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(delayLabel)
.addComponent(delayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(msLabel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Timeline Slider Mode"));
buttonGroup2.add(scrubButton);
scrubButton.setSelected(true);
scrubButton.setText("Scrub");
scrubButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
scrubButtonActionPerformed(evt);
}
});
buttonGroup2.add(dragDropButton);
dragDropButton.setText("Drag & Drop");
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()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrubButton)
.addComponent(dragDropButton))
.addContainerGap(70, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(scrubButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dragDropButton))
);
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(okButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton)
.addGap(64, 64, 64))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, 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)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(okButton)
.addComponent(cancelButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void defaultDelayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultDelayButtonActionPerformed
//If the defaultDelayButton is pressed, disable the custom delay field and text
delayLabel.setEnabled(false);
delayField.setEnabled(false);
msLabel.setEnabled(false);
}//GEN-LAST:event_defaultDelayButtonActionPerformed
private void customDelayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customDelayButtonActionPerformed
//Id the customDelayButton is pressed, enable the custom delay field and text
delayLabel.setEnabled(true);
delayField.setEnabled(true);
msLabel.setEnabled(true);
}//GEN-LAST:event_customDelayButtonActionPerformed
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
try {
int delay = EventPlayer.DEFAULT_DELAY;
if (customDelayButton.isSelected()) { //Try to parse the custom delay into an int
delay = Integer.parseInt(delayField.getText());
if (delay < 1 || delay > 9000) {
throw new NumberFormatException();
}
}
EventPlayer eventThread = trustGrapher.getEventPlayer();
if (eventThread != null) { //If the eventThread exists
eventThread.setDelay(delay); //Set the new delay
PlaybackPanel playbackPanel = eventThread.getPlaybackPanel();
if (playbackPanel != null) { //If the playbackPanel exists too
playbackPanel.setScrubMode(scrubButton.isSelected()); //Set the scrub mode
}
}
PropertyManager config = trustGrapher.getPropertyManager(); //Set the properties
config.setProperty(DELAY, "" + delay);
config.setProperty(SCRUB_MODE, scrubButton.isSelected() ? "true" : "false");
config.save();
dispose();
//If there is an error with the customDelayField, give an error message, but don't do anything
} catch (NumberFormatException ex) {
ChatterBox.alert("You must enter an integer between 1 and 9 000");
}
}//GEN-LAST:event_okButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
private void scrubButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scrubButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_scrubButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JRadioButton customDelayButton;
private javax.swing.JRadioButton defaultDelayButton;
private javax.swing.JTextField delayField;
private javax.swing.JLabel delayLabel;
private javax.swing.JRadioButton dragDropButton;
private javax.swing.JLabel msLabel;
private javax.swing.JRadioButton scrubButton;
// End of variables declaration//GEN-END:variables
}
////////////////////////////////////////////////////////////////////////////////