private void createAndShowGUI(String windowTitle) {
this.frame.setTitle(windowTitle);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel preparePanel = new JPanel(); //-----------------------------------------------
final DesignGridLayout prepareLayout = new DesignGridLayout(preparePanel);
final JButton jbutRandomLayout = new JButton();
jbutRandomLayout.setText(L10N.getString("btn.RandomLayout.text"));
this.addKeyBindingTooltip(jbutRandomLayout,
L10N.getString("btn.RandomLayout.acceleratorkey"),
L10N.getString("btn.RandomLayout.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
makeRandomBoardQuadrants();
refreshJComboPlaceRobot();
refreshJlistQuadrants();
}
}
);
for (int i = 0; i < 4; ++i) {
final JList jl = new JList(Board.QUADRANT_NAMES) {
@Override
public JToolTip createToolTip() {
return new QuadrantGoalsToolTip(this, Integer.parseInt(this.getToolTipText()));
};
};
jl.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
list.setToolTipText(String.valueOf(index));
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
};
});
jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jl.setVisibleRowCount(1);
jl.setSelectedIndex(this.board.getQuadrantNum(i));
jl.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (true == doRefreshJlistQuadrants) {
makeBoardQuadrants();
refreshJComboPlaceRobot();
}
}
});
this.jlistQuadrants[i] = jl;
}
final String[] strRobots = { "1", "2", "3", "4", "5" };
this.jcomboRobots.setModel(new DefaultComboBoxModel(strRobots));
this.jcomboRobots.setEditable(false);
this.jcomboRobots.setActionCommand(AC_BOARD_ROBOTS);
this.jcomboRobots.addActionListener(this);
this.refreshJcomboRobots();
final JButton jbutRemoveWalls = new JButton();
jbutRemoveWalls.setText(L10N.getString("btn.RemoveWalls.text"));
jbutRemoveWalls.setToolTipText(L10N.getString("btn.RemoveWalls.tooltip"));
jbutRemoveWalls.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
board.removeWalls();
board.setFreestyleBoard();
refreshBoard();
}
});
final JButton jbutRemoveGoals = new JButton();
jbutRemoveGoals.setText(L10N.getString("btn.RemoveGoals.text"));
jbutRemoveGoals.setToolTipText(L10N.getString("btn.RemoveGoals.tooltip"));
jbutRemoveGoals.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
board.removeGoals();
board.setFreestyleBoard();
refreshBoard();
}
});
final JButton jbutCopyBoardDumpToClipboard = new JButton();
jbutCopyBoardDumpToClipboard.setText(L10N.getString("btn.CopyBoardDumpToClipboard.text"));
this.addKeyBindingTooltip(jbutCopyBoardDumpToClipboard,
L10N.getString("btn.CopyBoardDumpToClipboard.acceleratorkey"),
L10N.getString("btn.CopyBoardDumpToClipboard.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
final Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
String data = board.getGameDump();
clip.setContents(new StringSelection(data), null);
JOptionPane.showMessageDialog(frame,
L10N.getString("msg.CopyBoardDumpToClipboard.OK.message"),
L10N.getString("msg.CopyBoardDumpToClipboard.OK.title"),
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame,
L10N.getString("msg.CopyBoardDumpToClipboard.Error.message") + "\n" + ex.toString(),
L10N.getString("msg.CopyBoardDumpToClipboard.Error.title"),
JOptionPane.ERROR_MESSAGE);
}
}
}
);
final JButton jbutCreateBoardFromDump = new JButton();
jbutCreateBoardFromDump.setText(L10N.getString("btn.CreateBoardFromDump.text"));
this.addKeyBindingTooltip(jbutCreateBoardFromDump,
L10N.getString("btn.CreateBoardFromDump.acceleratorkey"),
L10N.getString("btn.CreateBoardFromDump.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
final String data = JOptionPane.showInputDialog(frame,
L10N.getString("msg.CreateBoardFromDump.input.message"),
L10N.getString("msg.CreateBoardFromDump.input.title"),
JOptionPane.PLAIN_MESSAGE);
if ((null != data) && (0 != data.length())) {
final Board newBoard = Board.createBoardGameDump(data);
if (null != newBoard) {
board = newBoard;
if (board.isFreestyleBoard()) {
jtabEditBoard.setSelectedIndex(1);
}
refreshBoard();
refreshJcomboRobots();
refreshJComboPlaceRobot();
refreshJlistQuadrants();
jspinWidth.getModel().setValue(Integer.valueOf(board.width));
jspinHeight.getModel().setValue(Integer.valueOf(board.height));
} else {
throw new IllegalArgumentException(); //show error message
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame,
L10N.getString("msg.CreateBoardFromDump.Error.message"),
L10N.getString("msg.CreateBoardFromDump.Error.title"),
JOptionPane.ERROR_MESSAGE);
}
}
}
);
final JButton jbutRotateBoardLeft = new JButton();
jbutRotateBoardLeft.setText(getAnticlockwiseArrow(jbutRotateBoardLeft.getFont()));
this.addKeyBindingTooltip(jbutRotateBoardLeft,
L10N.getString("btn.RotateBoardLeft.acceleratorkey"),
L10N.getString("btn.RotateBoardLeft.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
board = board.rotate90(false);
refreshBoard();
refreshJComboPlaceRobot();
refreshJlistQuadrants();
}
}
);
final JButton jbutRotateBoardRight = new JButton();
jbutRotateBoardRight.setText(getClockwiseArrow(jbutRotateBoardRight.getFont()));
this.addKeyBindingTooltip(jbutRotateBoardRight,
L10N.getString("btn.RotateBoardRight.acceleratorkey"),
L10N.getString("btn.RotateBoardRight.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
board = board.rotate90(true);
refreshBoard();
refreshJComboPlaceRobot();
refreshJlistQuadrants();
}
}
);
this.jspinWidth.setModel(new SpinnerNumberModel(Board.WIDTH_STANDARD, Board.WIDTH_MIN, Board.WIDTH_MAX, 1));
this.jspinWidth.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
final int width = ((SpinnerNumberModel)jspinWidth.getModel()).getNumber().intValue();
if (width != board.width) {
makeFreestyleBoard();
}
}
});
this.jspinHeight.setModel(new SpinnerNumberModel(Board.HEIGHT_STANDARD, Board.HEIGHT_MIN, Board.HEIGHT_MAX, 1));
this.jspinHeight.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
final int height = ((SpinnerNumberModel)jspinHeight.getModel()).getNumber().intValue();
if (height != board.height) {
makeFreestyleBoard();
}
}
});
final Vector<String> dataGoalRobots = new Vector<String>();
for (int color = -1; color < 4; ++color) {
dataGoalRobots.add(Board.getColorLongL10N(color));
}
this.jlistGoalRobots.setListData(dataGoalRobots);
this.jlistGoalRobots.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.jlistGoalRobots.setSelectedIndex(0 + 1);
this.jlistGoalRobots.setVisibleRowCount(dataGoalRobots.size());
this.jlistGoalRobots.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
list.setToolTipText((index - 1) + ";" + jlistGoalShapes.getSelectedIndex());
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
};
});
final JScrollPane jscrollGoalRobots = new JScrollPane(this.jlistGoalRobots);
final Vector<String> dataGoalShapes = new Vector<String>();
for (int shape = 0; shape < 4; ++shape) {
dataGoalShapes.add(Board.getGoalShapeL10N(shape));
}
this.jlistGoalShapes.setListData(dataGoalShapes);
this.jlistGoalShapes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.jlistGoalShapes.setSelectedIndex(0);
this.jlistGoalShapes.setVisibleRowCount(dataGoalShapes.size());
this.jlistGoalShapes.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
list.setToolTipText((jlistGoalRobots.getSelectedIndex() - 1) + ";" + index);
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
};
});
final JScrollPane jscrollGoalShapes = new JScrollPane(this.jlistGoalShapes);
final JPanel editBoardOriginalPanel = new JPanel();
final DesignGridLayout editBoardOriginalLayout = new DesignGridLayout(editBoardOriginalPanel);
final JLabel jlabelBoardTiles = new JLabel(L10N.getString("lbl.BoardTiles.text"));
editBoardOriginalLayout.row().left().add(jlabelBoardTiles);
editBoardOriginalLayout.row().grid().add(new JScrollPane(this.jlistQuadrants[0]), new JScrollPane(this.jlistQuadrants[1])).add(jbutRandomLayout, 2);
editBoardOriginalLayout.row().grid().add(new JScrollPane(this.jlistQuadrants[3]), new JScrollPane(this.jlistQuadrants[2])).empty(2);
final JPanel editBoardFreestylePanel = new JPanel();
final DesignGridLayout editBoardFreestyleLayout = new DesignGridLayout(editBoardFreestylePanel);
editBoardFreestyleLayout.row().grid().add(jbutRemoveWalls).add(jbutRemoveGoals);
editBoardFreestyleLayout.emptyRow();
editBoardFreestyleLayout.row().grid().add(new JLabel(L10N.getString("lbl.Width.text"))).addMulti(this.jspinWidth);
editBoardFreestyleLayout.row().grid().add(new JLabel(L10N.getString("lbl.Height.text"))).addMulti(this.jspinHeight);
editBoardFreestyleLayout.emptyRow();
editBoardFreestyleLayout.row().grid().add(new JLabel(L10N.getString("lbl.ListGoalColors.text"))).add(new JLabel(L10N.getString("lbl.ListGoalShapes.text")));
editBoardFreestyleLayout.row().grid().add(jscrollGoalRobots).add(jscrollGoalShapes);
editBoardFreestyleLayout.emptyRow();
editBoardFreestyleLayout.row().grid().add(jbutCopyBoardDumpToClipboard).add(jbutCreateBoardFromDump);
this.jtabEditBoard.addTab(L10N.getString("tab.OriginalBoard.text"),
null, editBoardOriginalPanel, L10N.getString("tab.OriginalBoard.tooltip"));
this.jtabEditBoard.addTab(L10N.getString("tab.FreestyleBoard.text"),
null, editBoardFreestylePanel, L10N.getString("tab.FreestyleBoard.tooltip"));
this.jtabEditBoard.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jcomboGameIDs.setEnabled( ! SwingGUI.this.isFreestyleBoard());
}
});
prepareLayout.row().grid().add(new JLabel(L10N.getString("lbl.NumberOfRobots.text")), 2).add(this.jcomboRobots).empty();
prepareLayout.row().grid().add(new JLabel(L10N.getString("lbl.RotateBoard.text")), 2).add(jbutRotateBoardLeft, jbutRotateBoardRight);
prepareLayout.row().grid().add(this.jtabEditBoard);
final JPanel optionsPanel = new JPanel(); //-----------------------------------------------
final DesignGridLayout optionsLayout = new DesignGridLayout(optionsPanel);
final Solver.SOLUTION_MODE[] solModes = Solver.SOLUTION_MODE.values();
this.jcomboOptSolutionMode.setModel(new DefaultComboBoxModel(solModes));
this.jcomboOptSolutionMode.setSelectedItem(Solver.SOLUTION_MODE.MAXIMUM);
this.jcheckOptAllowRebounds.setText(L10N.getString("chk.AllowReboundMoves.text"));
this.jcheckOptAllowRebounds.setSelected(true);
this.jcheckOptShowColorNames.setText(L10N.getString("chk.ShowColorNames.text"));
this.jcheckOptShowColorNames.setSelected(false);
this.jcheckOptShowColorNames.setActionCommand(AC_SHOW_COLOR_NAMES);
this.jcheckOptShowColorNames.addActionListener(this);
this.jcheckOptShowOnlyActiveGoal.setText(L10N.getString("chk.ShowOnlyActiveGoal.text"));
this.jcheckOptShowOnlyActiveGoal.setSelected(true);
this.jcheckOptShowOnlyActiveGoal.setActionCommand(AC_SHOW_ACTIVE_GOAL);
this.jcheckOptShowOnlyActiveGoal.addActionListener(this);
this.jcheckOptShowSolutions.setText(L10N.getString("chk.ShowSolutions.text"));
this.jcheckOptShowSolutions.setSelected(false);
optionsLayout.row().grid().add(new JLabel(L10N.getString("lbl.SolverOptions.text")));
optionsLayout.emptyRow();
optionsLayout.row().grid().addMulti(new JLabel(L10N.getString("lbl.PreferSolutionWith.text")), this.jcomboOptSolutionMode);
optionsLayout.row().grid().add(new JLabel(L10N.getString("lbl.NumberOfRobotsMoved.text")));
optionsLayout.row().grid().add(new JLabel(" "));
optionsLayout.row().grid().add(this.jcheckOptAllowRebounds);
optionsLayout.emptyRow();
optionsLayout.row().grid().add(new JSeparator());
optionsLayout.emptyRow();
optionsLayout.row().grid().add(new JLabel(L10N.getString("lbl.GUIOptions.text")));
optionsLayout.emptyRow();
optionsLayout.row().grid().add(this.jcheckOptShowColorNames);
optionsLayout.row().grid().add(this.jcheckOptShowOnlyActiveGoal);
optionsLayout.row().grid().add(this.jcheckOptShowSolutions);
final JPanel playPanel = new JPanel(); //-----------------------------------------------
final DesignGridLayout playLayout = new DesignGridLayout(playPanel);
final JButton jbutRandomRobots = new JButton(L10N.getString("btn.RandomRobots.text"));
this.addKeyBindingTooltip(jbutRandomRobots,
L10N.getString("btn.RandomRobots.acceleratorkey"),
L10N.getString("btn.RandomRobots.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
placeRobot = -1;
refreshJComboPlaceRobot();
updateBoardRandomRobots();
}
}
);
final JButton jbutRandomGoal = new JButton(L10N.getString("btn.RandomGoal.text"));
this.addKeyBindingTooltip(jbutRandomGoal,
L10N.getString("btn.RandomGoal.acceleratorkey"),
L10N.getString("btn.RandomGoal.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
selectGoal = false;
updateBoardRandomGoal();
}
}
);
this.refreshJComboPlaceRobot(); //this.jcomboPlaceRobot
this.jcomboPlaceRobot.setEditable(false);
this.jcomboPlaceRobot.setActionCommand(AC_PLACE_ROBOT);
this.jcomboPlaceRobot.addActionListener(this);
String prtt = "<html>" + L10N.getString("cmb.PlaceRobot.tooltip") + " <small><strong>";
final JButton jbutPlaceRobot = new JButton("place robot accelerator keys");
jbutPlaceRobot.setPreferredSize(new Dimension()); //invisible button
prtt += this.addKeyBindingTooltip(jbutPlaceRobot, L10N.getString("cmb.PlaceRobot.1.acceleratorkey"), "",
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (jcomboPlaceRobot.getItemCount() > 1) { jcomboPlaceRobot.setSelectedIndex(1); }
}
}
);
prtt += " " + this.addKeyBindingTooltip(jbutPlaceRobot, L10N.getString("cmb.PlaceRobot.2.acceleratorkey"), "",
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (jcomboPlaceRobot.getItemCount() > 2) { jcomboPlaceRobot.setSelectedIndex(2); }
}
}
);
prtt += " " + this.addKeyBindingTooltip(jbutPlaceRobot, L10N.getString("cmb.PlaceRobot.3.acceleratorkey"), "",
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (jcomboPlaceRobot.getItemCount() > 3) { jcomboPlaceRobot.setSelectedIndex(3); }
}
}
);
prtt += " " + this.addKeyBindingTooltip(jbutPlaceRobot, L10N.getString("cmb.PlaceRobot.4.acceleratorkey"), "",
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (jcomboPlaceRobot.getItemCount() > 4) { jcomboPlaceRobot.setSelectedIndex(4); }
}
}
);
prtt += " " + this.addKeyBindingTooltip(jbutPlaceRobot, L10N.getString("cmb.PlaceRobot.5.acceleratorkey"), "",
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (jcomboPlaceRobot.getItemCount() > 5) { jcomboPlaceRobot.setSelectedIndex(5); }
}
}
);
this.jcomboPlaceRobot.setToolTipText(prtt + "</strong></small></html>");
final JButton jbutSelectGoal = new JButton(L10N.getString("btn.SelectGoal.text"));
this.addKeyBindingTooltip(jbutSelectGoal,
L10N.getString("btn.SelectGoal.acceleratorkey"),
L10N.getString("btn.SelectGoal.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
selectGoal = !selectGoal;
refreshBoard();
}
}
);
this.jcomboGameIDs.setModel(new DefaultComboBoxModel());
this.jcomboGameIDs.setEditable(true);
this.jcomboGameIDs.setActionCommand(AC_GAME_ID);
this.jcomboGameIDs.addActionListener(this);
this.jcomboSelectSolution.setModel(new DefaultComboBoxModel());
this.jcomboSelectSolution.setPrototypeDisplayValue("99) 99/9/#####"); //longest string possible here
this.jcomboSelectSolution.addItem(L10N.getString("cmb.SelectSolution.text"));
this.jcomboSelectSolution.setEditable(false);
this.jcomboSelectSolution.setActionCommand(AC_SELECT_SOLUTION);
this.jcomboSelectSolution.addActionListener(this);
this.jcomboSelectSolution.setToolTipText(L10N.getString("cmb.SelectSolution.tooltip"));
this.jbutSolutionHint.setText(L10N.getString("btn.Hint.text"));
this.addKeyBindingTooltip(this.jbutSolutionHint,
L10N.getString("btn.Hint.acceleratorkey"),
L10N.getString("btn.Hint.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
showHint();
}
}
);
this.jbutNextMove.setText(getRightwardsArrow(this.jbutNextMove.getFont()));
this.addKeyBindingTooltip(this.jbutNextMove,
L10N.getString("btn.NextMove.acceleratorkey"),
L10N.getString("btn.NextMove.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
showNextMove(true);
}
}
);
this.jbutAllMoves.setText(getRightwardsBarArrow(this.jbutAllMoves.getFont()));
this.addKeyBindingTooltip(this.jbutAllMoves,
L10N.getString("btn.AllMoves.acceleratorkey"),
L10N.getString("btn.AllMoves.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
while (jbutNextMove.isEnabled()) {
showNextMove(true);
}
}
}
);
this.jbutPrevMove.setText(getLeftwardsArrow(this.jbutPrevMove.getFont()));
this.addKeyBindingTooltip(this.jbutPrevMove,
L10N.getString("btn.PrevMove.acceleratorkey"),
L10N.getString("btn.PrevMove.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
showPrevMove(true);
}
}
);
this.jbutNoMoves.setText(getLeftwardsBarArrow(this.jbutNoMoves.getFont()));
this.addKeyBindingTooltip(this.jbutNoMoves,
L10N.getString("btn.NoMoves.acceleratorkey"),
L10N.getString("btn.NoMoves.tooltip"),
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
while (jbutPrevMove.isEnabled()) {
showPrevMove(true);
}
}
}
);
this.jtextSolution.setEditable(false);
((DefaultCaret)this.jtextSolution.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); // autoscroll
final JPanel panelSolutionText = new JPanel(new BorderLayout());
panelSolutionText.add(this.jtextSolution, BorderLayout.CENTER);
final JScrollPane scrollSolutionText = new JScrollPane(panelSolutionText, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollSolutionText.setPreferredSize(new Dimension(10, 10)); //workaround for layout problem ?!?!
scrollSolutionText.setMinimumSize(new Dimension(10, 10)); //workaround for layout problem ?!?!
playLayout.row().grid().addMulti(new JLabel(L10N.getString("lbl.SetStartingPosition.text")), jbutPlaceRobot);
playLayout.row().grid().add(jbutRandomRobots).add(jbutRandomGoal);
playLayout.row().grid().add(this.jcomboPlaceRobot).add(jbutSelectGoal);
playLayout.row().grid().add(new JLabel(L10N.getString("lbl.GameID.text"))).add(this.jcomboGameIDs, 3);
playLayout.emptyRow();
playLayout.row().grid().add(new JSeparator());
playLayout.row().grid().add(new JLabel(L10N.getString("lbl.ShowComputedSolutions.text")));
playLayout.row().grid().add(this.jcomboSelectSolution).add(this.jbutSolutionHint);
playLayout.row().grid().add(this.jbutNoMoves).add(this.jbutPrevMove).add(this.jbutNextMove).add(this.jbutAllMoves);
playLayout.row().grid().add(scrollSolutionText);
this.jtabPreparePlay.addTab(L10N.getString("tab.PrepareBoard.text"), preparePanel);
this.jtabPreparePlay.addTab(L10N.getString("tab.Options.text"), optionsPanel);
this.jtabPreparePlay.addTab(L10N.getString("tab.PlayGame.text"), playPanel);