Skip to content

Commit

Permalink
[Refactoring] Command pattern for ControlDeskView.java
Browse files Browse the repository at this point in the history
  • Loading branch information
jjunjji committed May 30, 2023
1 parent 84a9d48 commit a8b4aca
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 32 deletions.
89 changes: 57 additions & 32 deletions ControlDeskView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class ControlDeskView implements ActionListener, ControlDeskObserver {

private ControlDesk controlDesk;

private ButtonCommand command;

/**
* Displays a GUI representation of the ControlDesk
*
Expand All @@ -50,55 +52,41 @@ public ControlDeskView(ControlDesk controlDesk, int maxMembers) {
colPanel.setLayout(new BorderLayout());

// Controls Panel
JPanel controlsPanel = new JPanel();
controlsPanel.setLayout(new GridLayout(3, 1));
controlsPanel.setBorder(new TitledBorder("Controls"));
JPanel controlsPanel = createPanel("Controls", new GridLayout(3, 1));

addParty = new JButton("Add Party");
JPanel addPartyPanel = new JPanel();
addPartyPanel.setLayout(new FlowLayout());
addParty.addActionListener(this);
addPartyPanel.add(addParty);
addParty = createButton("Add Party", addPartyPanel, this);
controlsPanel.add(addPartyPanel);

assign = new JButton("Assign Lanes");
JPanel assignPanel = new JPanel();
assignPanel.setLayout(new FlowLayout());
assign.addActionListener(this);
assign = createButton("Assign Lanes", assignPanel, this);
assignPanel.add(assign);
// controlsPanel.add(assignPanel);

finished = new JButton("Finished");
JPanel finishedPanel = new JPanel();
finishedPanel.setLayout(new FlowLayout());
finished.addActionListener(this);
finishedPanel.add(finished);
finished = createButton("Finished", finishedPanel, this);
controlsPanel.add(finishedPanel);

// Lane Status Panel
JPanel laneStatusPanel = new JPanel();
laneStatusPanel.setLayout(new GridLayout(numLanes, 1));
laneStatusPanel.setBorder(new TitledBorder("Lane Status"));
JPanel laneStatusPanel = createPanel("Lane Status", new GridLayout(numLanes, 1));

HashSet lanes=controlDesk.getLanes();
Iterator it = lanes.iterator();
HashSet<Lane> lanes=controlDesk.getLanes();
Iterator<Lane> it = lanes.iterator();
int laneCount=0;
while (it.hasNext()) {
Lane curLane = (Lane) it.next();
Lane curLane = it.next();
LaneStatusView laneStat = new LaneStatusView(curLane,(laneCount+1));
curLane.subscribe(laneStat);
((Pinsetter)curLane.getPinsetter()).subscribe(laneStat);
(curLane.getPinsetter()).subscribe(laneStat);
JPanel lanePanel = laneStat.showLane();
lanePanel.setBorder(new TitledBorder("Lane" + ++laneCount ));
laneStatusPanel.add(lanePanel);
}

// Party Queue Panel
JPanel partyPanel = new JPanel();
partyPanel.setLayout(new FlowLayout());
partyPanel.setBorder(new TitledBorder("Party Queue"));
JPanel partyPanel = createPanel("Party Queue", new FlowLayout());

Vector empty = new Vector();
Vector<String> empty = new Vector<>();
empty.add("(Empty)");

partyList = new JList(empty);
Expand Down Expand Up @@ -131,8 +119,23 @@ public void windowClosing(WindowEvent e) {
win.setLocation(
((screenSize.width) / 2) - ((win.getSize().width) / 2),
((screenSize.height) / 2) - ((win.getSize().height) / 2));
win.show();
win.setVisible(true);

}

private JButton createButton(String text, JPanel panel, ActionListener listener) {
JButton button = new JButton(text);
button.addActionListener(listener);
panel.setLayout(new FlowLayout());
panel.add(button);
return button;
}

private JPanel createPanel(String title, LayoutManager mgr){
JPanel panel = new JPanel();
panel.setLayout(mgr);
panel.setBorder(new TitledBorder(title));
return panel;
}

/**
Expand All @@ -144,15 +147,25 @@ public void windowClosing(WindowEvent e) {

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(addParty)) {
AddPartyView addPartyWin = new AddPartyView(this, maxMembers);
setCommand(new ControlDeskViewAddPartyCommand(this));
}
if (e.getSource().equals(assign)) {
controlDesk.assignLane();
else if (e.getSource().equals(assign)) {
setCommand(new ControlDeskViewAssignCommand(this));
}
if (e.getSource().equals(finished)) {
win.hide();
System.exit(0);
else if (e.getSource().equals(finished)) {
setCommand(new ExitProgramCommand(this));
}

buttonPressed();
}

// The Invoker holds a command and can use it to call a method
public void setCommand(ButtonCommand command) {
this.command = command;
}
// Call the execute() method of the command
public void buttonPressed() {
command.execute();
}

/**
Expand All @@ -176,4 +189,16 @@ public void updateAddParty(AddPartyView addPartyView) {
public void receiveControlDeskEvent(PartyQueueEvent pe) {
partyList.setListData(pe.getPartyQueue());
}

public int getMaxMembers() {
return maxMembers;
}

public JFrame getWin() {
return win;
}

public ControlDesk getControlDesk() {
return controlDesk;
}
}
13 changes: 13 additions & 0 deletions ControlDeskViewAddPartyCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

public class ControlDeskViewAddPartyCommand implements ButtonCommand {
private ControlDeskView theView;

public ControlDeskViewAddPartyCommand(ControlDeskView theView) {
this.theView = theView;
}

@Override
public void execute() {
AddPartyView addPartyWin = new AddPartyView(theView, theView.getMaxMembers());
}
}
12 changes: 12 additions & 0 deletions ControlDeskViewAssignCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class ControlDeskViewAssignCommand implements ButtonCommand{
private ControlDeskView theView;

public ControlDeskViewAssignCommand(ControlDeskView theView) {
this.theView = theView;
}

@Override
public void execute() {
theView.getControlDesk().assignLane();
}
}
14 changes: 14 additions & 0 deletions ExitProgramCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class ExitProgramCommand implements ButtonCommand{

private ControlDeskView theView;

public ExitProgramCommand(ControlDeskView theView) {
this.theView = theView;
}

@Override
public void execute() {
theView.getWin().dispose();
System.exit(0);
}
}

0 comments on commit a8b4aca

Please sign in to comment.