Skip to content

Commit

Permalink
Rearrange DefectPanel to be cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsmieja committed Aug 29, 2014
1 parent 6870631 commit 67b8e3d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,11 @@ public enum Mode {
/** The Defect displayed in this panel */
private Defect model;

/*
* Form elements
*/
protected JTextField txtTitle;
protected JTextArea txtDescription;
protected JComboBox cmbStatus;
protected JLabel txtCreatedDate;
protected JLabel txtModifiedDate;
protected JTextField txtCreator;
protected JTextField txtAssignee;
protected JLabel lblCreatedDate;
protected JLabel lblModifiedDate;
/** A flag indicating if input is enabled on the form */
protected boolean inputEnabled;

/** An enum indicating if the form is in create mode or edit mode */
private Mode mode;

/** The panel containing GUI components for adding tags to defects */
protected TagPanel tagPanel;
Expand All @@ -79,19 +72,26 @@ public enum Mode {
protected SpringLayout layout;

/*
* Listeners for the form fields
* Form elements
*/
protected final TextUpdateListener txtTitleListener;
protected final TextUpdateListener txtDescriptionListener;
protected final ComboUpdateListener cmbStatusListener;
protected final TextUpdateListener txtCreatorListener;
protected final TextUpdateListener txtAssigneeListener;

/** A flag indicating if input is enabled on the form */
protected boolean inputEnabled;
protected JTextField txtTitle;
protected JTextArea txtDescription;
protected JComboBox cmbStatus;
protected JLabel txtCreatedDate;
protected JLabel txtModifiedDate;
protected JTextField txtCreator;
protected JTextField txtAssignee;
protected JLabel lblCreatedDate;
protected JLabel lblModifiedDate;

/** An enum indicating if the form is in create mode or edit mode */
private Mode mode;
/*
* Listeners for the form fields
*/
protected TextUpdateListener txtTitleListener;
protected TextUpdateListener txtDescriptionListener;
protected ComboUpdateListener cmbStatusListener;
protected TextUpdateListener txtCreatorListener;
protected TextUpdateListener txtAssigneeListener;

/*
* Constants used to layout the form
Expand Down Expand Up @@ -121,43 +121,13 @@ public DefectPanel(DefectView parent, Defect model, Mode mode) {
this.setLayout(layout);

// Add all components to this panel
addComponents();

// Add TextUpdateListeners. These check if the text component's text differs from the panel's Defect
// model and highlights them accordingly every time a key is pressed.
txtTitleListener = new TextUpdateListener(this, txtTitle);
txtTitle.addKeyListener(txtTitleListener);
this.addComponents();

txtDescriptionListener = new TextUpdateListener(this, txtDescription);
txtDescription.addKeyListener(txtDescriptionListener);

cmbStatusListener = new ComboUpdateListener(this, cmbStatus);
cmbStatus.addItemListener(cmbStatusListener);

txtCreatorListener = new TextUpdateListener(this, txtCreator);
txtCreator.addKeyListener(txtCreatorListener);

txtAssigneeListener = new TextUpdateListener(this, txtAssignee);
txtAssignee.addKeyListener(txtAssigneeListener);

// prevent tab key from inserting tab characters into the description field
txtDescription.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_TAB) {
if (event.getModifiers() == 0) {
txtDescription.transferFocus();
}
else {
txtDescription.transferFocusBackward();
}
event.consume();
}
}
});
//Set up listeners for events
this.setListeners();

// Populate the form with the contents of the Defect model and update the TextUpdateListeners.
updateFields();
this.updateFields();
}

/**
Expand Down Expand Up @@ -359,7 +329,7 @@ protected void updateModel(Defect defect, Mode mode) {
/**
* Updates the DefectPanel's fields to match those in the current model.
*/
private void updateFields() {
protected void updateFields() {
txtTitle.setText(model.getTitle());
txtDescription.setText(model.getDescription());
for (int i = 0; i < cmbStatus.getItemCount(); i++) {
Expand Down Expand Up @@ -468,4 +438,39 @@ public JTextField getCreatorField() {
public DefectEventListModel getDefectEventListModel() {
return defectEventListModel;
}

protected void setListeners() {
// Add TextUpdateListeners. These check if the text component's text differs from the panel's Defect
// model and highlights them accordingly every time a key is pressed.
txtTitleListener = new TextUpdateListener(this, txtTitle);
txtTitle.addKeyListener(txtTitleListener);

txtDescriptionListener = new TextUpdateListener(this, txtDescription);
txtDescription.addKeyListener(txtDescriptionListener);

cmbStatusListener = new ComboUpdateListener(this, cmbStatus);
cmbStatus.addItemListener(cmbStatusListener);

txtCreatorListener = new TextUpdateListener(this, txtCreator);
txtCreator.addKeyListener(txtCreatorListener);

txtAssigneeListener = new TextUpdateListener(this, txtAssignee);
txtAssignee.addKeyListener(txtAssigneeListener);

// prevent tab key from inserting tab characters into the description field
txtDescription.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_TAB) {
if (event.getModifiers() == 0) {
txtDescription.transferFocus();
}
else {
txtDescription.transferFocusBackward();
}
event.consume();
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.wpi.cs.wpisuitetng.modules.defecttracker.defect;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
Expand All @@ -16,6 +17,8 @@ public class TestDefectPanel {

@Test
public void testConstructor() {
assertNotNull(panel);

assertEquals(view, panel.getParent());
assertEquals(defect, panel.getModel());
assertEquals(Mode.CREATE, panel.getMode());
Expand Down

0 comments on commit 67b8e3d

Please sign in to comment.