Skip to content

Commit

Permalink
first playable version
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Sep 6, 2015
1 parent 03c51f8 commit 32120e7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 29 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist
.faces-config.xml.jsfdia
.project
.classpath/PFSearchExpressions/
.DS_Store
25 changes: 25 additions & 0 deletions AJAX/src/main/java/de/beyondjava/bootsfaces/examples/AJAXBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class AJAXBean {
private boolean spinning=true;
private boolean readOnly=false;
private boolean disabled=true;
private boolean buttonRed=false;

public boolean isSpinning() {
return spinning;
Expand Down Expand Up @@ -241,11 +242,35 @@ public String standardJSFAction() {
report("Standard JSF action called");
return null; // "landingPage.jsf";
}

public void facetListener(javax.faces.event.AjaxBehaviorEvent event) throws javax.faces.event.AbortProcessingException {
report("f:ajax listener called");
}

public void mouseOverListener(javax.faces.event.AjaxBehaviorEvent event) throws javax.faces.event.AbortProcessingException {
report("f:ajax mouseOver listener called");
buttonRed=true;
}


public void mouseOutListener(javax.faces.event.AjaxBehaviorEvent event) throws javax.faces.event.AbortProcessingException {
report("f:ajax mouseOut listener called");
buttonRed=false;
}


public void standardJSFActionListener() {
report("Standard JSF actionlistener without parameters called");
}
public void standardJSFActionListener(ActionEvent even) {
report("Standard JSF actionlistener called");
}

public boolean isButtonRed() {
return buttonRed;
}

public void setButtonRed(boolean buttonRed) {
this.buttonRed = buttonRed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class ImageGallery implements Serializable {

@ManagedProperty("#{AJAXBean}")
private AJAXBean ajaxBean;
private String singleImage="http://www.11pictures.com/foto/stories/Camargue_2013_Nature/framedPreview.png";

private String singleImage = "http://www.11pictures.com/foto/stories/Camargue_2013_Nature/framedPreview.png";

private List<String> images = new ArrayList<String>() {
{
Expand All @@ -40,6 +40,13 @@ public class ImageGallery implements Serializable {
private static final long serialVersionUID = 1L;
};

private List<String> combinedImagePool = new ArrayList<String>() {
{
addAll(images);
addAll(imagePool);
}
};

public void setAjaxBean(AJAXBean ab) {
this.ajaxBean = ab;
}
Expand All @@ -58,8 +65,15 @@ public void chooseAnotherImage(String image) {
}

public void chooseAnotherImage() {
int random = (int) Math.floor(Math.random() * imagePool.size());
singleImage= imagePool.get(random);
while (true) {
int random = (int) Math.floor(Math.random() * imagePool.size());
String newImage = imagePool.get(random);
if (!newImage.equals(singleImage)) {
singleImage = newImage;
break;
}
}

}

public void chooseEveryImage() {
Expand Down
7 changes: 5 additions & 2 deletions AJAX/src/main/webapp/commandbutton2.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
<b:row>
<b:column col-sm="6">
<b:panel look="primary" title="AJAXified b:commandButton with f:ajax">

<b:commandButton value="Change image" ajax="true"

<h:commandButton value="Change image" action="#{imageGallery.chooseAnotherImage()}">
<f:ajax listener="#{AJAXBean.facetListener}" render="@form"></f:ajax>
</h:commandButton>
<b:commandButton value="Change image" ajax="true" action="#{imageGallery.chooseAnotherImage()}"
update="@form:graphic @form:**:messages">
<f:ajax listener="#{AJAXBean.standardJSFActionListener}" render="@next **:messages"></f:ajax>
</b:commandButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import de.beyondjava.bootsfaces.chess.Exceptions.EndOfGameException;
import de.beyondjava.bootsfaces.chess.common.ChessConstants;
Expand All @@ -27,8 +28,9 @@ public class Board implements Serializable {

private List<Row> rows;

public Board() {
private boolean startOpponentsMove = false;

public Board() {
redraw();
}

Expand All @@ -49,13 +51,14 @@ public void setRows(List<Row> rows) {
}

public String getOpacity(int row, int column) {
if (startOpponentsMove) return "0.7";
if (!isPieceSelected)
return "1.0";
if (selectedPieceColumn == column && selectedPieceRow == row)
return "1.0";
if (chessboard.isMovePossible(selectedPieceRow, selectedPieceColumn, row, column))
return "1.0";
return "0.7";
return "0.8";
}

public void onclick(int row, int column) {
Expand All @@ -69,16 +72,10 @@ public void onclick(int row, int column) {

chessboard = chessboard.moveChessPiece(selectedPieceRow, selectedPieceColumn, row, column,
ChessConstants.W_QUEEN);
selectedPieceRow=0;
selectedPieceColumn=0;
try {
Move move = chessboard.findBestMove();
chessboard = chessboard.moveChessPiece(move.fromRow, move.fromColumn, move.toRow, move.toColumn,
ChessConstants.W_QUEEN);
} catch (EndOfGameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selectedPieceRow = 0;
selectedPieceColumn = 0;
// opponentsMove();
startOpponentsMove = true;
}

} else {
Expand All @@ -94,4 +91,31 @@ public void onclick(int row, int column) {
redraw();
}

public void opponentsMove(ActionEvent event) {
if (startOpponentsMove) {
startOpponentsMove = false;
try {
Move move = chessboard.findBestMove();
chessboard = chessboard.moveChessPiece(move.fromRow, move.fromColumn, move.toRow, move.toColumn,
ChessConstants.W_QUEEN);
} catch (EndOfGameException e) {
e.printStackTrace();
}
redraw();
}
}

public boolean isStartOpponentsMove() {
return startOpponentsMove;
}

public void setStartOpponentsMove(boolean startOppenentsMove) {
this.startOpponentsMove = startOppenentsMove;
}

public void flipSides(ActionEvent event) {
startOpponentsMove=true;
opponentsMove(event);
}

}
28 changes: 17 additions & 11 deletions BootsFacesChess/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@
</b:container>

<b:container id="content">
<h:form>
<ui:repeat id="rows" var="row" value="#{board.rows}"
varStatus="rowStatus">
<ui:repeat id="fields" var="image" value="#{row.images}"
varStatus="columnStatus">
<h:form id="form">
<b:panel title="#{board.startOpponentsMove?'Calculating next move...':'Waiting for your move'}" id="board">
<ui:repeat id="rows" var="row" value="#{board.rows}"
varStatus="rowStatus">
<ui:repeat id="fields" var="image" value="#{row.images}"
varStatus="columnStatus">

<b:image value="#{image}"
style="width:70px;height:70px;opacity:#{board.getOpacity(rowStatus.index,columnStatus.index)}"
onclick="ajax:board.onclick(rowStatus.index,columnStatus.index)"
update="@form" />
<b:image value="#{image}"
style="width:70px;height:70px;opacity:#{board.getOpacity(rowStatus.index,columnStatus.index)}"
onclick="ajax:board.onclick(rowStatus.index,columnStatus.index)"
oncomplete="document.getElementById('form:opponent').click()"
update="@form" />
</ui:repeat>

<br />
</ui:repeat>
<br />
</ui:repeat>
</b:panel>
<b:commandButton value="computer's move" id="opponent" actionListener="#{board.opponentsMove}" ajax="true" update="@form" style="display:none" />
<b:commandButton value="flip sides" actionListener="#{board.flipSides}" ajax="true" update="@form" />
</h:form>
</b:container>
</h:body>
Expand Down

0 comments on commit 32120e7

Please sign in to comment.