Incremental commit.

This commit is contained in:
Jonathan Bernard 2010-08-28 07:21:07 -05:00
parent abc8f72beb
commit 73284c6fea
3 changed files with 97 additions and 59 deletions

View File

@ -0,0 +1,19 @@
package com.jdbernard.teammaker
public class PlayerPanel {
/* ======== MODEL ======== */
// GUI Components
class View {
def panel
}
View view
class Model {
}
Model model
/* ======== VIEW ======== */

View File

@ -11,7 +11,7 @@ import javax.swing.JFrame
import javax.swing.JOptionPane import javax.swing.JOptionPane
import org.apache.log4j.Logger import org.apache.log4j.Logger
public class TeamMaker { public class ProfessionalPickup {
/* ======== MODEL ======== */ /* ======== MODEL ======== */
@ -19,6 +19,7 @@ public class TeamMaker {
public static final String version = "0.4" public static final String version = "0.4"
// GUI Components // GUI Components
class View {
def frame // main frame def frame // main frame
def team1List // JList for team 1 def team1List // JList for team 1
def team2List // JList for team 2 def team2List // JList for team 2
@ -29,16 +30,14 @@ public class TeamMaker {
def inGamePlayerDropMenu def inGamePlayerDropMenu
def sittingPlayerDropMenu def sittingPlayerDropMenu
}
def config View view = new View()
protected Logger log = Logger.getLogger(getClass()) protected Logger log = Logger.getLogger(getClass())
PlayerChooser playerChooser = new WeightedChooser(hardLimit: 2)
OddsCalculator oddsCalculator = new OddsCalculator()
// Data components // Data components
class Observables { class Model {
@Bindable List team1Players = [] @Bindable List team1Players = []
@Bindable List team2Players = [] @Bindable List team2Players = []
@Bindable List sittingPlayers = [] @Bindable List sittingPlayers = []
@ -46,16 +45,19 @@ public class TeamMaker {
@Bindable int teamSize = 4 @Bindable int teamSize = 4
@Bindable List popupPlayers = null @Bindable List popupPlayers = null
@Bindable int popupTeam; @Bindable int popupTeam;
@Bindable PlayerChooser playerChooser = new WeightedChooser(hardLimit: 2)
@Bindable OddsCalculator oddsCalculator = new OddsCalculator()
@Bindable Properties config
} }
def model = new Observables() Model model = new Model()
/* ======== VIEW ======== */ /* ======== VIEW ======== */
/* /*
+--------------------------------------------------+ +--------------------------------------------------+
| JDB TeamMaker v0.2 _[]X | | JDB Professional Pickup v0.5 _[]X |
+--------------------------------------------------+ +--------------------------------------------------+
| | | |
| +-Current-Game--------------+ +-Sitting-Players+ | | +-Current-Game--------------+ +-Sitting-Players+ |
@ -79,13 +81,13 @@ public class TeamMaker {
private void initGUI() { private void initGUI() {
inGamePlayerDropMenu = swing.popupMenu(/*'Player Options'*/) { view.inGamePlayerDropMenu = swing.popupMenu(/*'Player Options'*/) {
menuItem('Bench Player',/* enabled: bind { !model.inGame },*/ menuItem('Bench Player',/* enabled: bind { !model.inGame },*/
actionPerformed: { benchPlayer(model.popupPlayers, actionPerformed: { benchPlayer(model.popupPlayers,
model.popupTeam) }) model.popupTeam) })
} }
sittingPlayerDropMenu = swing.popupMenu(/*'Player Options'*/) { view.sittingPlayerDropMenu = swing.popupMenu(/*'Player Options'*/) {
menuItem('Assign to Team A', /*enabled: bind { !model.inGame },*/ menuItem('Assign to Team A', /*enabled: bind { !model.inGame },*/
actionPerformed: { actionPerformed: {
model.popupPlayers.each { assignPlayer(it, 1) }}) model.popupPlayers.each { assignPlayer(it, 1) }})
@ -102,7 +104,7 @@ public class TeamMaker {
// TODO // TODO
// settingsDialog = new SettingsDialog(main: this) // settingsDialog = new SettingsDialog(main: this)
frame = swing.frame(title: "JDB Professional Pickup v$version", view.frame = swing.frame(title: "JDB Professional Pickup v$version",
iconImages: [swing.imageIcon("/bb128.png").image, iconImages: [swing.imageIcon("/bb128.png").image,
swing.imageIcon("/bb64.png").image, swing.imageIcon("/bb64.png").image,
swing.imageIcon("/bb48.png").image, swing.imageIcon("/bb48.png").image,
@ -144,7 +146,7 @@ public class TeamMaker {
def teamListRenderer = new PlayerListCellRenderer(this, def teamListRenderer = new PlayerListCellRenderer(this,
showStats: false, colored: false) showStats: false, colored: false)
team1List = list(cellRenderer: teamListRenderer, view.team1List = list(cellRenderer: teamListRenderer,
constraints: gbc(gridx: 0, gridy: 1, fill: GBC.BOTH, constraints: gbc(gridx: 0, gridy: 1, fill: GBC.BOTH,
insets: [5, 5, 0, 0], weightx: 2, weighty: 2), insets: [5, 5, 0, 0], weightx: 2, weighty: 2),
model: new DefaultListModel(), model: new DefaultListModel(),
@ -153,13 +155,13 @@ public class TeamMaker {
showInGamePlayerDropMenu(evt) showInGamePlayerDropMenu(evt)
}) })
team1WinsButton = button('Team A Wins', view.team1WinsButton = button('Team A Wins',
constraints: gbc(gridx: 0, gridy: 2, anchor: constraints: gbc(gridx: 0, gridy: 2, anchor:
GBC.CENTER, insets: [5, 5, 5, 0]), GBC.CENTER, insets: [5, 5, 5, 0]),
enabled: bind { model.inGame }, enabled: bind { model.inGame },
actionPerformed: { declareLoser(2)}) actionPerformed: { declareLoser(2)})
team2List = list(cellRenderer: teamListRenderer, view.team2List = list(cellRenderer: teamListRenderer,
constraints: gbc(gridx: 1, gridy: 1, fill: GBC.BOTH, constraints: gbc(gridx: 1, gridy: 1, fill: GBC.BOTH,
insets: [5, 5, 0, 5], weightx: 2, weighty: 2), insets: [5, 5, 0, 5], weightx: 2, weighty: 2),
model: new DefaultListModel(), model: new DefaultListModel(),
@ -168,7 +170,7 @@ public class TeamMaker {
showInGamePlayerDropMenu(evt) showInGamePlayerDropMenu(evt)
}) })
team2WinsButton = button('Team B Wins', view.team2WinsButton = button('Team B Wins',
constraints: gbc(gridx: 1, gridy: 2, constraints: gbc(gridx: 1, gridy: 2,
anchor: GBC.CENTER, insets: [5, 5, 5, 5]), anchor: GBC.CENTER, insets: [5, 5, 5, 5]),
enabled: bind { model.inGame }, enabled: bind { model.inGame },
@ -180,7 +182,7 @@ public class TeamMaker {
fill: GBC.BOTH, insets: [5, 5, 0, 5], weighty: 1), fill: GBC.BOTH, insets: [5, 5, 0, 5], weighty: 1),
border: titledBorder(title: 'Sitting Players')) { border: titledBorder(title: 'Sitting Players')) {
sittingList = list(cellRenderer: new PlayerListCellRenderer( view.sittingList = list(cellRenderer: new PlayerListCellRenderer(
this, showStats: true, colored: true), this, showStats: true, colored: true),
model: new SortedPlayerModel(), model: new SortedPlayerModel(),
mouseClicked: { evt -> mouseClicked: { evt ->
@ -198,11 +200,11 @@ public class TeamMaker {
anchor: GBC.CENTER, insets: [5, 5, 5, 0]), anchor: GBC.CENTER, insets: [5, 5, 5, 0]),
actionPerformed: { addPlayer() }) actionPerformed: { addPlayer() })
newGameButton = button('Delete Player', view.newGameButton = button('Delete Player',
constraints: gbc(gridx: 2, gridy: 1, anchor: GBC.CENTER, constraints: gbc(gridx: 2, gridy: 1, anchor: GBC.CENTER,
insets: [5, 5, 5, 5]), insets: [5, 5, 5, 5]),
actionPerformed: { actionPerformed: {
model.sittingPlayers.remove(sittingList.selectedValues) model.sittingPlayers.remove(view.sittingList.selectedValues)
refreshGUI() }) refreshGUI() })
} }
@ -216,12 +218,12 @@ public class TeamMaker {
/* ======== CONTROLLER ======== */ /* ======== CONTROLLER ======== */
public static void main(String[] args) { public static void main(String[] args) {
def inst = new TeamMaker() def inst = new ProfessionalPickup()
inst.frame.show() inst.view.frame.show()
} }
TeamMaker() { ProfessionalPickup() {
config = new Properties() model.config = new Properties()
// look for team maker config file // look for team maker config file
File teammakerrc = new File(System.getProperty('user.home'), File teammakerrc = new File(System.getProperty('user.home'),
@ -231,12 +233,12 @@ public class TeamMaker {
.withInputStream { is -> teammakerrc.text = is.text } .withInputStream { is -> teammakerrc.text = is.text }
// load config // load config
teammakerrc.withInputStream { config.load(is) } teammakerrc.withInputStream { model.config.load(is) }
// set configured properties // set configured properties
Game.BASE_POINT_VALUE = config.getProperty('base-point-value', Game.BASE_POINT_VALUE = model.config.getProperty('base-point-value',
'1').toInteger() '1').toInteger()
model.teamSize = config.getProperty('team.size', '4').toInteger() model.teamSize = model.config.getProperty('team.size', '4').toInteger()
initGUI() initGUI()
} }
@ -251,18 +253,18 @@ public class TeamMaker {
} }
private void setTeamSize() { private void setTeamSize() {
def newSize = JOptionPane.showMessageDialog(frame, def newSize = JOptionPane.showMessageDialog(view.frame,
"Enter the new team size: ", "Set Team Size...", "Enter the new team size: ", "Set Team Size...",
JOptionPane.QUESTION_MESSAGE) JOptionPane.QUESTION_MESSAGE)
if (newSize.isInteger()) model.teamSize = newSize.toInteger() if (newSize.isInteger()) model.teamSize = newSize.toInteger()
else JOptionPane.showMessageDialog(frame, else JOptionPane.showMessageDialog(view.frame,
"$newSize is not a valid team size (it is not a number...).", "$newSize is not a valid team size (it is not a number...).",
"Set Team Size...", JOptionPane.ERROR_MESSAGE) "Set Team Size...", JOptionPane.ERROR_MESSAGE)
} }
private void addPlayer() { private void addPlayer() {
def name = JOptionPane.showInputDialog(frame, def name = JOptionPane.showInputDialog(view.frame,
"Enter the new player's name: ", "New Player...", "Enter the new player's name: ", "New Player...",
JOptionPane.QUESTION_MESSAGE) JOptionPane.QUESTION_MESSAGE)
@ -312,15 +314,15 @@ public class TeamMaker {
log.debug("team2Players: ${model.team2Players}") log.debug("team2Players: ${model.team2Players}")
} }
team1List.model = new DefaultListModel() view.team1List.model = new DefaultListModel()
team2List.model = new DefaultListModel() view.team2List.model = new DefaultListModel()
sittingList.model = new SortedPlayerModel() view.sittingList.model = new SortedPlayerModel()
model.team1Players.each { team1List.model.addElement(it) } model.team1Players.each { view.team1List.model.addElement(it) }
model.team2Players.each { team2List.model.addElement(it) } model.team2Players.each { view.team2List.model.addElement(it) }
model.sittingPlayers.each { sittingList.model.addElement(it) } model.sittingPlayers.each { view.sittingList.model.addElement(it) }
frame.repaint() view.frame.repaint()
recalculateOdds() recalculateOdds()
@ -329,8 +331,8 @@ public class TeamMaker {
private void recalculateOdds() { private void recalculateOdds() {
def players = model.sittingPlayers def players = model.sittingPlayers
swing.doOutside { swing.doOutside {
oddsCalculator.recalculate(players, getSpotsOpen(), playerChooser) model.oddsCalculator.recalculate(players, getSpotsOpen(), model.playerChooser)
swing.edt { sittingList.repaint() } swing.edt { view.sittingList.repaint() }
} }
} }
@ -338,7 +340,7 @@ public class TeamMaker {
private void populate(int teamNum) { private void populate(int teamNum) {
def teamPlayers = playersForTeam(teamNum) def teamPlayers = playersForTeam(teamNum)
while (teamPlayers.size() < model.teamSize) { while (teamPlayers.size() < model.teamSize) {
def player = playerChooser.choose(model.sittingPlayers) def player = model.playerChooser.choose(model.sittingPlayers)
teamPlayers << player teamPlayers << player
model.sittingPlayers.remove(player) model.sittingPlayers.remove(player)
} }
@ -361,15 +363,15 @@ public class TeamMaker {
} }
private void showSittingPlayerDropMenu(def evt) { private void showSittingPlayerDropMenu(def evt) {
int index = sittingList.locationToIndex(evt.point) int index = view.sittingList.locationToIndex(evt.point)
if (sittingList.isSelectedIndex(index)) if (view.sittingList.isSelectedIndex(index))
this.model.popupPlayers = sittingList.selectedValues this.model.popupPlayers = view.sittingList.selectedValues
else { else {
sittingList.selectedIndex = index view.sittingList.selectedIndex = index
this.model.popupPlayers = [sittingList.model.getElementAt(index)] this.model.popupPlayers = [view.sittingList.model.getElementAt(index)]
} }
sittingPlayerDropMenu.show(sittingList, evt.point.@x, evt.point.@y) view.sittingPlayerDropMenu.show(view.sittingList, evt.point.@x, evt.point.@y)
} }
private void showInGamePlayerDropMenu(def evt) { private void showInGamePlayerDropMenu(def evt) {
@ -382,13 +384,13 @@ public class TeamMaker {
model.popupPlayers = [teamList.model.getElementAt(index)] model.popupPlayers = [teamList.model.getElementAt(index)]
} }
this.model.popupTeam = (teamList == team1List ? 1 : 2) this.model.popupTeam = (teamList == view.team1List ? 1 : 2)
inGamePlayerDropMenu.show(teamList, evt.point.@x, evt.point.@y) view.inGamePlayerDropMenu.show(teamList, evt.point.@x, evt.point.@y)
} }
private void setGamesSat(Player p) { private void setGamesSat(Player p) {
String value = JOptionPane.showInputDialog(frame, String value = JOptionPane.showInputDialog(view.frame,
"How many games has ${p.name} sat?", "Set Games Sat.", "How many games has ${p.name} sat?", "Set Games Sat.",
JOptionPane.QUESTION_MESSAGE) JOptionPane.QUESTION_MESSAGE)
int newGames int newGames
@ -396,7 +398,7 @@ public class TeamMaker {
if (!value) return if (!value) return
try { newGames = value.toInteger() } try { newGames = value.toInteger() }
catch (Throwable t) { catch (Throwable t) {
JOptionPane.showMessageDialog(frame, JOptionPane.showMessageDialog(view.frame,
"The value $value is not a valid number.", "The value $value is not a valid number.",
"Invalid Input", JOptionPane.ERROR_MESSAGE) "Invalid Input", JOptionPane.ERROR_MESSAGE)
setGamesSat(p) setGamesSat(p)
@ -414,7 +416,7 @@ public class TeamMaker {
} }
private List listForTeam(int teamNum) { private List listForTeam(int teamNum) {
if (teamNum == 1) return team1List if (teamNum == 1) return view.team1List
else return team2List else return view.team2List
} }
} }

View File

@ -2,5 +2,22 @@ package com.jdbernard.teammaker
public class TeamPanel { public class TeamPanel {
/* ======== MODEL ======== */
// GUI components
class View {
def panel
}
View view = new View()
class Model {
@Bindable String name
}
Model model = new Model()
/* ======== VIEW ======== */
view.panel = swing.panel(border: titledBorder(model.name)) {
} }