Author: jdonnerstag
Date: Mon Jul 26 10:10:13 2010
New Revision: 979220
URL: http://svn.apache.org/viewvc?rev=979220&view=rev
Log:
fixed: org.apache.wicket.util.value.Count: add decrement method
Issue: WICKET-2967
Modified:
wicket/branches/wicket-1.4.x/wicket-examples/src/main/java/org/apache/wicket/examples/compref/LinkPage.java
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/value/Count.java
Modified:
wicket/branches/wicket-1.4.x/wicket-examples/src/main/java/org/apache/wicket/examples/compref/LinkPage.java
URL:
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket-examples/src/main/java/org/apache/wicket/examples/compref/LinkPage.java?rev=979220&r1=979219&r2=979220&view=diff
==============================================================================
---
wicket/branches/wicket-1.4.x/wicket-examples/src/main/java/org/apache/wicket/examples/compref/LinkPage.java
(original)
+++
wicket/branches/wicket-1.4.x/wicket-examples/src/main/java/org/apache/wicket/examples/compref/LinkPage.java
Mon Jul 26 10:10:13 2010
@@ -17,12 +17,12 @@
package org.apache.wicket.examples.compref;
import org.apache.wicket.AttributeModifier;
-import org.apache.wicket.IClusterable;
import org.apache.wicket.examples.WicketExamplePage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
+import org.apache.wicket.util.value.Count;
import org.apache.wicket.version.undo.Change;
@@ -41,31 +41,31 @@ public class LinkPage extends WicketExam
// power to the annonymous classes!
// first create a simple value holder object
- final ClickCount count1 = new ClickCount();
+ final Count count1 = new Count();
- // add a link which, when clicked, increases our counter
- // when a link is clicked, its onClick method is called
+ // add a link which, when clicked, increases our counter when a
link is clicked, its onClick
+ // method is called
Link link1 = new Link("link1")
{
@Override
public void onClick()
{
- count1.clicks++;
+ count1.increment();
}
};
add(link1);
- // add a counter label to the link so that we can display it in
the body
- // of the link
+
+ // add a counter label to the link so that we can display it in
the body of the link
link1.add(new Label("label1", new Model<String>()
{
@Override
public String getObject()
{
- return Integer.toString(count1.clicks);
+ return Integer.toString(count1.getCount());
}
}));
- final ClickCount count2 = new ClickCount();
+ final Count count2 = new Count();
// Same idea as above, but now we record a state change. Note
that the
// URL will change because of this, and pressing the back
button and
// clicking the link again would revert to the older value.
@@ -77,34 +77,31 @@ public class LinkPage extends WicketExam
@Override
public void onClick()
{
- final int count = count1.clicks;
- count2.clicks++;
+ final int count = count2.getCount();
+ count2.increment();
addStateChange(new Change()
{
@Override
public void undo()
{
// revert
- count2.clicks = count;
+ count2.decrement();
}
});
}
};
add(linkWithStateChange);
- linkWithStateChange.add(new Label("label", new
PropertyModel<Integer>(count2, "clicks")));
+ linkWithStateChange.add(new Label("label", new
PropertyModel<Integer>(count2, "count")));
- // we can attach Link components to any HTML tag we want. If it
is an
- // anchor (<a href...),
- // the url to this component is put in the href attribute. For
other
- // components, a
- // onclick javascript event handler is created that triggers
the round
- // trip
+ // we can attach Link components to any HTML tag we want. If it
is an anchor (<a href...),
+ // the url to this component is put in the href attribute. For
other components, a onclick
+ // javascript event handler is created that triggers the round
trip
// it is of course possible to - instead of the above approach
- hide as
// much of the component as possible within a class.
class CustomLink extends Link
{
- final ClickCount count2;
+ final Count count2;
/**
* Construct.
@@ -114,24 +111,23 @@ public class LinkPage extends WicketExam
public CustomLink(String id)
{
super(id);
- count2 = new ClickCount();
+ count2 = new Count();
add(new ClickCountLabel("label2", count2));
}
@Override
public void onClick()
{
- count2.clicks++;
+ count2.increment();
}
}
add(new CustomLink("link2"));
- // and if we know we are going to attach it to a <input
type="button>
- // tag, we shouldn't
- // use a label, but an AttributeModifier instead.
+ // and if we know we are going to attach it to a <input
type="button> tag, we shouldn't use
+ // a label, but an AttributeModifier instead.
class ButtonLink extends Link
{
- final ClickCount count3;
+ final Count count3;
/**
* Construct.
@@ -141,18 +137,15 @@ public class LinkPage extends WicketExam
public ButtonLink(String id)
{
super(id);
- count3 = new ClickCount();
+ count3 = new Count();
add(new AttributeModifier("value", new
Model<String>()
{
@Override
public String getObject()
{
- // we just replace the whole
string. You could use
- // custom
- // AttributeModifiers to e.g.
just replace one part of
- // the
- // string if you want
- return "this button is clicked
" + count3.clicks + " times";
+ // we just replace the whole
string. You could use custom AttributeModifiers
+ // to e.g. just replace one
part of the string if you want
+ return "this button is clicked
" + count3.getCount() + " times";
}
}));
}
@@ -160,22 +153,13 @@ public class LinkPage extends WicketExam
@Override
public void onClick()
{
- count3.clicks++;
+ count3.increment();
}
}
add(new ButtonLink("link3"));
}
/**
- * Simple class to holds the number of clicks.
- */
- private static class ClickCount implements IClusterable
- {
- /** number of clicks. */
- private int clicks = 0;
- }
-
- /**
* Simple custom label that displays the link click count.
*/
private static class ClickCountLabel extends Label
@@ -188,7 +172,7 @@ public class LinkPage extends WicketExam
* @param clickCount
* the count object
*/
- public ClickCountLabel(String id, final ClickCount clickCount)
+ public ClickCountLabel(String id, final Count clickCount)
{
// call super with a simple annonymous class model that
displays the
// current number of clicks
@@ -197,7 +181,7 @@ public class LinkPage extends WicketExam
@Override
public String getObject()
{
- return
Integer.toString(clickCount.clicks);
+ return
Integer.toString(clickCount.getCount());
}
});
}
@@ -205,13 +189,13 @@ public class LinkPage extends WicketExam
// ----------
- final ClickCount count1 = new ClickCount(); // simple counter object
+ final Count count1 = new Count(); // simple counter object
Link link1 = new Link("link1")
{
@Override
public void onClick()
{
- count1.clicks++;
+ count1.increment();
}
};
@@ -222,20 +206,18 @@ public class LinkPage extends WicketExam
protected void explain()
{
String html = "<a href=\"#\" wicket:id=\"link1\">this link is
clicked <span wicket:id=\"label1\">n</span> times</a>";
- String code =
" final ClickCount count1 = new
ClickCount(); // simple counter object\n"
+ String code =
" final Count count1 = new
Count(); // simple counter object\n"
+ " Link
link1 = new Link(\"link1\") {\n"
+
" public
void onClick() {\n"
- +
" count1.clicks++;\n"
+ +
" count1.increment();\n"
+
" }\n"
+ " };\n"
+
" link1.add(new
Label(\"label1\", new Model<String>() {\n"
+
" public
Object getObject() {\n"
- +
" return
Integer.toString(count1.clicks);\n"
+ +
" return
Integer.toString(count1.getCount());\n"
+
" }\n"
+
" }));\n"
+
" add(link1);";
add(new ExplainPanel(html, code));
-
}
-
}
\ No newline at end of file
Modified:
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/value/Count.java
URL:
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/value/Count.java?rev=979220&r1=979219&r2=979220&view=diff
==============================================================================
---
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/value/Count.java
(original)
+++
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/value/Count.java
Mon Jul 26 10:10:13 2010
@@ -48,4 +48,21 @@ public final class Count implements IClu
{
count++;
}
+
+ /**
+ * Decreases the count value by one.
+ */
+ public void decrement()
+ {
+ count--;
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return Integer.toString(count);
+ }
}