edittext1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
selStart = edittext1.getSelectionStart();
selEnd= edittext1.getSelectionEnd();
lengthBefore = edittext1.getText().toString().length();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
int lengthAfter = edittext1.getText().toString().length() - lengthBefore;
if(!(lengthAfter < 0))
{
String codedBefore = editTextCoded.getText().toString().substring(0, selStart);
String codedAfter;
if(selEnd>1) {
codedAfter = editTextCoded.getText().toString().substring(selEnd);
}
else
{
codedAfter = "";
}
editTextCoded.setText(codedBefore);
//Zakodowanie nowego
String toBeCoded = edittext1.getText().toString().substring(selStart, selEnd+ lengthAfter);
for(int i=0; i<toBeCoded.length(); i++)
{
newChar = toBeCoded.charAt(i);
editTextCoded.setText(editTextCoded.getText().toString() + Character.toString(newChar));
}
//Dopisanie końcówki tłumaczenia
editTextCoded.setText(editTextCoded.getText().toString() + codedAfter);
}
else
{
String codedBefore = editTextCoded.getText().toString().substring(0, selStart + lengthAfter);
String codedAfter = editTextCoded.getText().toString().substring(selStart, editTextCoded.length());
editTextCoded.setText(codedBefore + codedAfter);
}
}
});
after this operation editTextCoded and edittext1 should show the same text, yet when I copy part of the text, then select another (smaller) part and paste it replacing the selected part, the text in editTextCoded and edittext1 differs.
Anyone could please help me? I have no ideas left what to do with it.
Related
I am currently new to interfaces and I encountered this problem regarding my getArea(), I would love if you guys can point out the error on my code
while this is my Triangle abstract class, I am not sure whether I putted the Semi-perimeter(sp) right or not:
private int sideA;
private int sideB;
private int sideC;
int sp = (sideA+sideB+sideC)/2;
double area = Math.sqrt(sp*(sp-sideA)*(sp-sideB)*(sp-sideC));
public Triangle() {
}
public Triangle(int sideA, int sideB, int sideC, int sp, double area) {
this.sideA=sideA;
this.sideB=sideB;
this.sideC=sideC;
}
public int getSideA() {
return sideA;
}
public void setSideA(int sideA) {
this.sideA = sideA;
}
public int getSideB() {
return sideB;
}
public void setSideB(int sideB) {
this.sideB = sideB;
}
public int getSideC() {
return sideC;
}
public void setSideC(int sideC) {
this.sideC = sideC;
}
public double getPerimeter() {
return sideA+sideB+sideC;
}
public double getArea() {
return getArea();
}
public void getDetails() {
System.out.println("\nType: Triangle" + "\nSides: " +this.sideA +", " +this.sideB+", "+this.sideC + "\nPerimeter: "
+ getPerimeter() + "\nArea: " + getArea() + "\ns: "+sp);
}
}```
This is my testfragment where i build the list based on a switch:
public class TestFragment extends Fragment {
public String Workshop;
public int NumberOfQuestions;
public Button SaveButton;
public TextView GradeView;
public String Grades;
private OnFragmentInteractionListener mListener;
public TestFragment() {}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle bundle){
View rootView = inflater.inflate(R.layout.questionscomplete,
container, false);
Workshop = getArguments().getString("Workshoptitle");
GradeView = ((TextView) rootView.findViewById(R.id.GradeView));
// Inflate the layout for this fragment
if (!Workshop.equals("Doodlz Drawing")) {
rootView = inflater.inflate(R.layout.fragment_test,
container, false);
final ListView lv1 = (ListView) rootView.findViewById(R.id.listview);
ArrayList image_details = getListData();
lv1.setAdapter(new CustomListAdapter(getActivity(), image_details));
SaveButton = ((Button) rootView.findViewById(R.id.Save));
SaveButton.setOnClickListener(Grading);
}
return rootView;
}
private ArrayList getListData() {
ArrayList<QuestionItem> results = new ArrayList<QuestionItem>();
switch (Workshop) {
case "Android Studio":
Workshop = "Android Studio";
QuestionItem[] Item1 = new QuestionItem[3];
QItem WS1 = new QItem();
NumberOfQuestions = Item1.length;
String[] WS1Q = WS1.getQuestions(Workshop, NumberOfQuestions);
String[] WS1QA = WS1.getAnswers(Workshop);
for (int i = 0; i < NumberOfQuestions; i++) {
Item1[i] = new QuestionItem();
Item1[i].setQuestion(WS1Q[i]);
Item1[i].setAnswer1(WS1QA[i * 10 + 0]);
Item1[i].setAnswer2(WS1QA[i * 10 + 1]);
Item1[i].setAnswer3(WS1QA[i * 10 + 2]);
Item1[i].setAnswer4(WS1QA[i * 10 + 3]);
// Toast.makeText(getActivity(), Item1[i].GetAnswer(),
// Toast.LENGTH_LONG).show();
results.add(Item1[i]);
}
break;
case "Java Basics":
Workshop = "Java Basics";
QuestionItem[] Item2 = new QuestionItem[5];
QItem WS2 = new QItem();
NumberOfQuestions = Item2.length;
String[] WS2Q = WS2.getQuestions(Workshop, NumberOfQuestions);
String[] WS2QA = WS2.getAnswers(Workshop);
for (int i = 0; i < NumberOfQuestions; i++) {
Item2[i] = new QuestionItem();
Item2[i].setQuestion(WS2Q[i]);
Item2[i].setAnswer1(WS2QA[i * 10 + 0]);
Item2[i].setAnswer2(WS2QA[i * 10 + 1]);
Item2[i].setAnswer3(WS2QA[i * 10 + 2]);
Item2[i].setAnswer4(WS2QA[i * 10 + 3]);
results.add(Item2[i]);
// Toast.makeText(getActivity(), Item2[i].GetAnswer(),
// Toast.LENGTH_LONG).show();
}
break;
My listadapter:
public class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public String Answer;
public String[] Answers;
private ArrayList<QuestionItem> listData;
public CustomListAdapter(Context aContext, ArrayList<QuestionItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v = convertView;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.questions, null);
holder = new ViewHolder(
);
holder.QuestionView = (TextView) convertView.findViewById(R.id.q);
holder.Answer1View = (TextView) convertView.findViewById(R.id.qa1);
holder.Answer2View = (TextView) convertView.findViewById(R.id.qa2);
holder.Answer3View = (TextView) convertView.findViewById(R.id.qa3);
holder.Answer4View = (TextView) convertView.findViewById(R.id.qa4);
//
holder.Answer1Button = (RadioButton) convertView.findViewById(R.id.qa1);
holder.Answer2Button = (RadioButton) convertView.findViewById(R.id.qa2);
holder.Answer3Button = (RadioButton) convertView.findViewById(R.id.qa3);
holder.Answer4Button = (RadioButton) convertView.findViewById(R.id.qa4);
holder.ButtonGroupView = (RadioGroup) convertView.findViewById(R.id.group);
convertView.setTag(holder);
holder.ButtonGroupView.getCheckedRadioButtonId();
holder.ButtonGroupView
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group,
int checkedId) {
Integer pos = (Integer) group.getTag(); // To identify the Model object i get from the RadioGroup with getTag()
// an integer representing the actual position
QuestionItem element = listData.get(pos);
switch (checkedId) { //set the Model to hold the answer the user picked
case R.id.qa1:
element.setAnswer("1");
"Log.d(listData.get(pos).toString(), element.GetAnswer());
break;
case R.id.qa2:
element.setAnswer("2");
"Log.d(listData.get(pos).toString(), element.GetAnswer());
break;
case R.id.qa3:
element.setAnswer("3");
"Log.d(listData.get(pos).toString(), element.GetAnswer());
break;
case R.id.qa4:
element.setAnswer("4");
"Log.d(listData.get(pos).toString(), element.GetAnswer());
break;
default:
element.setAnswer(null); // Something was wrong set to the default
}
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
QuestionItem element = (QuestionItem) getItem(position);
holder.QuestionView.setText(listData.get(position).getQuestion());
holder.Answer1View.setText(listData.get(position).Getanswer1());
holder.Answer2View.setText(listData.get(position).Getanswer2());
holder.Answer3View.setText(listData.get(position).Getanswer3());
holder.Answer4View.setText(listData.get(position).Getanswer4());
holder.ButtonGroupView.setTag(new Integer(position)); // I passed the current position as a tag
return convertView;
}
//
class ViewHolder {
TextView t = null;
TextView QuestionView;
TextView Answer1View;
TextView Answer2View;
TextView Answer3View;
TextView Answer4View;
RadioButton Answer1Button, Answer2Button, Answer3Button, Answer4Button;
RadioGroup ButtonGroupView;
}}
My question Item:
public class QuestionItem {
private String Question;
private String Answer1;
private String Answer2;
private String Answer3;
private String Answer4;
private String Answer;
public String getQuestion() {
return Question;
}
public void setQuestion(String Question) {
this.Question = Question;
}
public String Getanswer1() {
return Answer1;
}
public void setAnswer1(String Answer1) {
this.Answer1 = Answer1;
}
public String Getanswer2() {
return Answer2;
}
public void setAnswer2(String Answer2) {
this.Answer2 = Answer2;
}
public String Getanswer3() {
return Answer3;
}
public void setAnswer3(String Answer3) {
this.Answer3 = Answer3;
}
public String Getanswer4() {
return Answer4;
}
public void setAnswer4(String Answer4) {
this.Answer4 = Answer4;
}
public String GetAnswer() {
return Answer;
}
public void setAnswer(String Answer) {
this.Answer = Answer;
}
}
I want to save all the answers and compare them to the right answers and save the score to be used as a variable in another fragment.
UPDATE: added "Log.d(listData.get(pos).toString(), element.GetAnswer());" And i can see it is storing the choses answers so how do i request them in the testfragment and check them
Ive tried several things but cannot find the solution. pls help:)?
I'm trying to implement the Show More and Show less functionality on my text view, which should expand and collapse on clicking Show More or Show Less text vice versa.
Problem is if i expand the text view on clicking Show More & scroll down the list ,it again collapses.
Thanks.
private void bindPostContent(ViewHolder holder, Cursor c) {
//Main text in HTML format
String postContent = c.getString(thoughtC);
holder.tvPostHtml.setVisibility(View.VISIBLE);
holder.tvPostHtml.setText(Html.fromHtml(c.getString(thoughtC)));
Linkify.addLinks(holder.tvPostHtml, Linkify.WEB_URLS);
if (!TextUtils.isEmpty(postContent)) {
if (postContent.length() > 240) {
resizePostContent(holder.tvPostHtml, "...Show More", true);
}
} else {
holder.tvPostHtml.setVisibility(View.GONE);
}
//Display attachments
AttachmentContainer container = new AttachmentContainer(tid, holder.llAttachmentContainer,
ThoughtContainerType.THOUGHT,
context, c.getString(attachmentC),
attachJsonMapper, pollState);
value = container.value;
container.renderAttachmentViews();
}
public void resizePostContent(final TextView tvPostContent, final String showMoreLessLabel, final boolean showMore) {
if (tvPostContent.getTag() == null) {
tvPostContent.setTag(tvPostContent.getText());
}
ViewTreeObserver vto = tvPostContent.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
ViewTreeObserver obs = tvPostContent.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if (showMore) {
String text = tvPostContent.getText().subSequence(0, 240) + " " + showMoreLessLabel;
tvPostContent.setText(text);
tvPostContent.setMovementMethod(LinkMovementMethod.getInstance());
tvPostContent.setText(showMoreLess(Html.fromHtml(tvPostContent.getText().toString()), tvPostContent, showMoreLessLabel, showMore));
} else {
String text = tvPostContent.getText().subSequence(0, tvPostContent.getText().length()) + " " + showMoreLessLabel;
tvPostContent.setText(text);
tvPostContent.setMovementMethod(LinkMovementMethod.getInstance());
tvPostContent.setText(showMoreLess(Html.fromHtml(tvPostContent.getText().toString()), tvPostContent, showMoreLessLabel, showMore));
} tvPostContent.setOnClickListener(clickListener);
}
});
}
private SpannableStringBuilder showMoreLess(final Spanned postContentSpanned, final TextView tvPostContent, final String showMoreLessLabel, final boolean viewMore) {
String str = postContentSpanned.toString();
SpannableStringBuilder ssb = new SpannableStringBuilder(postContentSpanned);
if (str.contains(showMoreLessLabel)) {
ssb.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
tvPostContent.setOnClickListener(null);
if (viewMore) {
tvPostContent.setLayoutParams(tvPostContent.getLayoutParams());
tvPostContent.setText(tvPostContent.getTag().toString());
resizePostContent(tvPostContent, "Show Less", false);
} else {
tvPostContent.setLayoutParams(tvPostContent.getLayoutParams());
tvPostContent.setText(tvPostContent.getTag().toString());
resizePostContent(tvPostContent, "...Show More", true);
}
}
#Override public void updateDrawState(TextPaint ds){
ds.setUnderlineText(false);
ds.setColor(context.getResources().getColor(R.color.newsfeed_item_action_performed));
}
}, str.indexOf(showMoreLessLabel), str.indexOf(showMoreLessLabel) + showMoreLessLabel.length(), 0);
}
return ssb;
}
I'm trying to make a simple Drag and Drop FlexTable using native GWT drag events to allow the user to move rows around.
//This works fine
galleryList.getElement().setDraggable(Element.DRAGGABLE_TRUE);
galleryList.addDragStartHandler(new DragStartHandler() {
#Override
public void onDragStart(DragStartEvent event) {
event.setData("text", "Hello World");
groupList.getElement().getStyle().setBackgroundColor("#aff");
}
});
However, I'd like to:
1. Give a visual indicator where the item will be dropped.
2. Work out where i should drop the row, when the drop event fires.
galleryList.addDragOverHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
//TODO: How do i get the current location one would drop an item into a flextable here
}
});
galleryList.addDragEndHandler(new DragEndHandler() {
#Override
public void onDragEnd(DragEndEvent event) {
//TODO: How do i know where i am in the flextable
}
});
I see these FlexTable methods are useful in getting a cell/row:
public Cell getCellForEvent(ClickEvent event)
protected Element getEventTargetCell(Event event)
But the problem is the Drag events do not inherit of Event
Thanks in advance
Caveat: I cant gaurentee this will work out the box. This is the code I ended up making (And works for me) - it's a Drag n Drop Widget which inherits from FlexTable. I detect any drag events on the table, and then try compute where the mouse is over that FlexTable.
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
public class DragFlexTable extends FlexTable implements DraggableWidget {
public String dragStyle = "drag-table-row";
public String dragReplaceStyle;
protected DragVerticalHandler dragFlexTableHandler;
private int currentRow;
private int[] yLocations;
private int rowBeingDragged;
DragFlexTable() {
sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
getElement().setDraggable(Element.DRAGGABLE_TRUE);
final DragUtil dragUtil = new DragUtil();
addDragStartHandler(new DragStartHandler() {
#Override
public void onDragStart(DragStartEvent event) {
dragUtil.startVerticalDrag(event, DragFlexTable.this);
}
});
addDragEndHandler(new DragEndHandler() {
#Override
public void onDragEnd(DragEndEvent event) {
dragUtil.endVerticalDrag(event, DragFlexTable.this);
}
});
addDragOverHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
dragUtil.handleVerticalDragOver(event, DragFlexTable.this, 3);
}
});
}
#Override
public void onBrowserEvent(Event event) {
super.onBrowserEvent(event);
Element td = getEventTargetCell(event);
if (td == null) return;
Element tr = DOM.getParent(td);
currentRow = TableRowElement.as(td.getParentElement()).getSectionRowIndex();
switch (DOM.eventGetType(event)) {
case Event.ONMOUSEOVER: {
//tr.addClassName(ROW_STYLE_NAME + "-mouseover");
tr.addClassName(dragStyle);
break;
}
case Event.ONMOUSEOUT: {
tr.removeClassName(dragStyle);
break;
}
}
}
public void setDragStyle(String dragStyle) {
this.dragStyle = dragStyle;
}
#Override
public void resetDragState() {
yLocations = null;
}
#Override
public void setRowBeingDragged(int currentRow) {
this.rowBeingDragged = currentRow;
}
#Override
public int getDragRow(DragDropEventBase event) {
if (yLocations == null) {
yLocations = new int[getRowCount()];
for (int i = 0; i < getRowCount(); i++) {
Widget widget = getWidget(i, 0);
if (widget.isVisible()) {
com.google.gwt.dom.client.Element imgTd = widget.getElement().getParentElement();
int absoluteBottom = imgTd.getAbsoluteBottom();
yLocations[i] = absoluteBottom;
} else {
yLocations[i] = -1;
}
}
}
int lastY = 0;
for (int i = 0; i < yLocations.length; i++) {
int absoluteBottom = yLocations[i];
//invisible
if (absoluteBottom != -1) {
int absoluteTop = lastY;
int clientY = event.getNativeEvent().getClientY();
if (absoluteBottom > clientY && absoluteTop < clientY) {
//com.google.gwt.dom.client.Element tr = imgTd.getParentElement();
return i;
}
lastY = absoluteBottom;
}
}
return currentRow;
}
#Override
public com.google.gwt.dom.client.Element getTrElement(int row) {
return getWidget(row, 0).getElement().getParentElement().getParentElement();
}
#Override
public DragVerticalHandler getDragFlexTableHandler() {
return dragFlexTableHandler;
}
public void setDragReplaceStyle(String dragReplaceStyle) {
this.dragReplaceStyle = dragReplaceStyle;
}
#Override
public int getRowBeingDragged() {
return rowBeingDragged;
}
#Override
public String getDragReplaceStyle() {
return dragReplaceStyle;
}
public void setDragFlexTableHandler(DragVerticalHandler dragFlexTableHandler) {
this.dragFlexTableHandler = dragFlexTableHandler;
}
}
This is util class which it uses
import com.google.gwt.event.dom.client.DragEndEvent;
import com.google.gwt.event.dom.client.DragOverEvent;
import com.google.gwt.event.dom.client.DragStartEvent;
import com.google.gwt.user.client.ui.Image;
public class DragUtil {
private int alternateIgnoreEvent = 0;
private int lastDragRow = -1;
public void startVerticalDrag(DragStartEvent event, DraggableWidget widget) {
widget.resetDragState();
//Required
event.setData("text", "dragging");
int currentRow = widget.getDragRow(event);
widget.setRowBeingDragged(currentRow);
if (widget.getDragFlexTableHandler()!=null) {
Image thumbnailImg = widget.getDragFlexTableHandler().getImage(currentRow);
if (thumbnailImg!=null) {
event.getDataTransfer().setDragImage(thumbnailImg.getElement(), -10, -10);
}
}
}
public void handleVerticalDragOver(DragOverEvent event, DraggableWidget widgets, int sensitivity) {
if (alternateIgnoreEvent++ % sensitivity != 0) {
//many events fire, for every pixel move, which slow the browser, so ill ignore some
return;
}
int dragRow = widgets.getDragRow(event);
if (dragRow != lastDragRow) {
com.google.gwt.dom.client.Element dragOverTr = widgets.getTrElement(dragRow);
if (lastDragRow != -1) {
com.google.gwt.dom.client.Element lastTr = widgets.getTrElement(lastDragRow);
lastTr.removeClassName(widgets.getDragReplaceStyle());
}
lastDragRow = dragRow;
dragOverTr.addClassName(widgets.getDragReplaceStyle());
}
}
public void endVerticalDrag(DragEndEvent event, DraggableWidget widgets) {
int newRowPosition = widgets.getDragRow(event);
//cleanup last position
if (newRowPosition != lastDragRow) {
com.google.gwt.dom.client.Element lastTr = widgets.getTrElement(lastDragRow);
lastTr.removeClassName(widgets.getDragReplaceStyle());
}
if (newRowPosition != widgets.getRowBeingDragged()) {
com.google.gwt.dom.client.Element dragOverTr = widgets.getTrElement(newRowPosition);
dragOverTr.removeClassName(widgets.getDragReplaceStyle());
widgets.getDragFlexTableHandler().moveRow(widgets.getRowBeingDragged(), newRowPosition);
}
}
}
In the uibinder I then use it like this:
<adminDnd:DragFlexTable ui:field='itemFlexTable' styleName='{style.table}' cellSpacing='0'
cellPadding='0'
dragStyle='{style.drag-table-row-mouseover}'
dragReplaceStyle='{style.drag-replace-table-row}'
/>
Even i had this problem and after a little prototyping this is what I ended up with
.I created a class which extended the flex table.Here is the code
public class DragFlexTable extends FlexTable implements
MouseDownHandler,MouseUpHandler,MouseMoveHandler,
MouseOutHandler
{
private int row,column,draggedrow,draggedcolumn;
private Element td;
private Widget w;
private boolean emptycellclicked;
DragFlexTable()
{
sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONMOUSEDOWN | Event.ONMOUSEMOVE);
this.addMouseDownHandler(this);
this.addMouseMoveHandler(this);
this.addMouseUpHandler(this);
this.addMouseOutHandler(this);
}
#Override
public void onBrowserEvent(Event event)
{
super.onBrowserEvent(event);
td = getEventTargetCell(event);
if (td == null)
{
return;
}
Element tr = DOM.getParent((com.google.gwt.user.client.Element) td);
Element body = DOM.getParent((com.google.gwt.user.client.Element) tr);
row = DOM.getChildIndex((com.google.gwt.user.client.Element) body, (com.google.gwt.user.client.Element) tr);//(body, tr);
column = DOM.getChildIndex((com.google.gwt.user.client.Element) tr, (com.google.gwt.user.client.Element) td);
}
boolean mousedown;
#Override
public void onMouseDown(MouseDownEvent event)
{
if (event.getNativeButton() == NativeEvent.BUTTON_LEFT)
{
//to ensure empty cell is not clciked
if (!td.hasChildNodes())
{
emptycellclicked = true;
}
event.preventDefault();
start(event);
mousedown = true;
}
}
#Override
public void onMouseMove(MouseMoveEvent event)
{
if (mousedown)
{
drag(event);
}
}
#Override
public void onMouseUp(MouseUpEvent event)
{
if (event.getNativeButton() == NativeEvent.BUTTON_LEFT)
{
if (!emptycellclicked)
{
end(event);
}
emptycellclicked = false;
mousedown = false;
}
}
#Override
public void onMouseOut(MouseOutEvent event)
{
this.getCellFormatter().getElement(row, column).getStyle().clearBorderStyle();
this.getCellFormatter().getElement(row, column).getStyle().clearBorderColor();
this.getCellFormatter().getElement(row, column).getStyle().clearBorderWidth();
w.getElement().getStyle().setOpacity(1);
mousedown = false;
}
private void start(MouseDownEvent event)
{
w = this.getWidget(row, column);
w.getElement().getStyle().setOpacity(0.5);
}
private void drag(MouseMoveEvent event)
{
if (draggedrow != row || draggedcolumn != column)
{
this.getCellFormatter().getElement(draggedrow, draggedcolumn).getStyle().clearBorderStyle();
this.getCellFormatter().getElement(draggedrow, draggedcolumn).getStyle().clearBorderColor();
this.getCellFormatter().getElement(draggedrow, draggedcolumn).getStyle().clearBorderWidth();
this.draggedrow = row;
this.draggedcolumn = column;
this.getCellFormatter().getElement(row, column).getStyle().setBorderStyle(BorderStyle.DASHED);
this.getCellFormatter().getElement(row, column).getStyle().setBorderColor("black");
this.getCellFormatter().getElement(row, column).getStyle().setBorderWidth(2, Unit.PX);
}
}
private void end(MouseUpEvent event)
{
insertDraggedWidget(row, column);
}
private void insertDraggedWidget(int r,int c)
{
this.getCellFormatter().getElement(r, c).getStyle().clearBorderStyle();
this.getCellFormatter().getElement(r, c).getStyle().clearBorderColor();
this.getCellFormatter().getElement(r, c).getStyle().clearBorderWidth();
w.getElement().getStyle().setOpacity(1);
if (this.getWidget(r, c) != null)
{
//pushing down the widgets already in the column
// int widgetheight = (this.getWidget(r, c).getOffsetHeight() / 2) + this.getWidget(r, c).getAbsoluteTop();
// int rw;
//placing the widget above the dropped widget
for (int i = this.getRowCount() - 1; i >= r; i--)
{
if (this.isCellPresent(i, c))
{
this.setWidget(i + 1, c, this.getWidget(i, c));
}
}
}
this.setWidget(r, c, w);
//removes unneccesary blank rows
cleanupRows();
//pushing up the column in the stack
// for (int i = oldrow;i<this.getRowCount()-1 ;i++)
// {
//
// this.setWidget(i ,oldcolumn, this.getWidget(i+1, oldcolumn));
//
// }
}
private void cleanupRows()
{
ArrayList<Integer> rowsfilled = new ArrayList<Integer>();
for (int i = 0; i <= this.getRowCount() - 1; i++)
{
for (int j = 0; j <= this.getCellCount(i) - 1; j++)
{
if (this.getWidget(i, j) != null)
{
rowsfilled.add(i);
break;
}
}
}
//replace the empty rows
for (int i = 0; i < rowsfilled.size(); i++)
{
int currentFilledRow = rowsfilled.get(i);
if (i != currentFilledRow)
{
for (int j = 0; j < this.getCellCount(currentFilledRow); j++)
{
this.setWidget(i, j, this.getWidget(currentFilledRow, j));
}
}
}
for (int i = rowsfilled.size(); i < this.getRowCount(); i++)
{
this.removeRow(i);
}
}
public HandlerRegistration addMouseUpHandler(MouseUpHandler handler)
{
return addDomHandler(handler, MouseUpEvent.getType());
}
public HandlerRegistration addMouseDownHandler(MouseDownHandler handler)
{
return addDomHandler(handler, MouseDownEvent.getType());
}
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler)
{
return addDomHandler(handler, MouseMoveEvent.getType());
}
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler)
{
return addDomHandler(handler, MouseOutEvent.getType());
}
}
and the on module load for the above custom widget is
public void onModuleLoad()
{
Label a = new Label("asad");
Label b = new Label("ad");
Label c = new Label("qwad");
Label w = new Label("zxd");
a.setPixelSize(200, 200);
a.getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
a.getElement().getStyle().setBorderWidth(1, Unit.PX);
b.setPixelSize(200, 200);
c.setPixelSize(200, 200);
w.setPixelSize(200, 200);
a.getElement().getStyle().setBackgroundColor("red");
b.getElement().getStyle().setBackgroundColor("yellowgreen");
c.getElement().getStyle().setBackgroundColor("lightblue");
w.getElement().getStyle().setBackgroundColor("grey");
DragFlexTable d = new DragFlexTable();
d.setWidget(0, 0, a);
d.setWidget(0, 1, b);
d.setWidget(1, 0, c);
d.setWidget(1, 1, w);
d.setCellPadding(20);
RootPanel.get().add(d);
}
I need a text field very similar in behavior to Gxt's NumberField. Unfortunately I am not using Gxt in my application and GWT 2.0 does not have a Numeric text field implementation as yet.
So that currently leaves me with an option to simulate a NumberField by filtering out non-numeric keystrokes using a keyboardHandler.
Is this the the best way to approach the problem? Does anyone here have a better solution/approach in mind?
Thanks in advance :)
Here you can find the code that I use in one of my classes. The features are much more limited that those of GXT, but should put you in the proper track.
It's a really basic widget, but does what I need to.
public class ValueTextBox extends TextBox {
private int min = 0;
private int max = 100;
private boolean minConstrained = true;
private boolean maxConstrained = true;
private int minDigits = 1;
private int step = 1;
private KeyUpHandler keyUpHandler = new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent event) {
if (isReadOnly() || !isEnabled()) {
return;
}
int keyCode = event.getNativeEvent().getKeyCode();
boolean processed = false;
switch (keyCode) {
case KeyCodes.KEY_LEFT:
case KeyCodes.KEY_RIGHT:
case KeyCodes.KEY_BACKSPACE:
case KeyCodes.KEY_DELETE:
case KeyCodes.KEY_TAB:
if (getText().isEmpty()) {
setValue(formatValue(min));
}
return;
case KeyCodes.KEY_UP:
if (step != 0) {
increaseValue();
processed = true;
}
break;
case KeyCodes.KEY_DOWN:
if (step != 0) {
decreaseValue();
processed = true;
}
break;
}
if (processed) {
cancelKey();
}
}
};
private KeyPressHandler keyPressHandler = new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
if (isReadOnly() || !isEnabled()) {
return;
}
int keyCode = event.getNativeEvent().getKeyCode();
switch (keyCode) {
case KeyCodes.KEY_LEFT:
case KeyCodes.KEY_RIGHT:
case KeyCodes.KEY_BACKSPACE:
case KeyCodes.KEY_DELETE:
case KeyCodes.KEY_TAB:
case KeyCodes.KEY_UP:
case KeyCodes.KEY_DOWN:
return;
}
int index = getCursorPos();
String previousText = getText();
String newText;
if (getSelectionLength() > 0) {
newText = previousText.substring(0, getCursorPos())
+ event.getCharCode()
+ previousText.substring(getCursorPos()
+ getSelectionLength(), previousText.length());
} else {
newText = previousText.substring(0, index)
+ event.getCharCode()
+ previousText.substring(index, previousText.length());
}
cancelKey();
setValue(newText, true);
}
};
public ValueTextBox(int value) {
this(value, 0, 100);
}
public ValueTextBox(int value, int min, int max) {
this(value, min, max, true);
}
public ValueTextBox(int value, int min, int max, boolean constrained) {
this(value, min, max, constrained, constrained);
}
public ValueTextBox(int value, int min, int max, boolean minConstrained,
boolean maxConstrained) {
super();
addKeyPressHandler(keyPressHandler);
addKeyUpHandler(keyUpHandler);
this.min = min;
this.max = max;
this.minConstrained = minConstrained;
this.maxConstrained = maxConstrained;
setValue(formatValue(value), false);
setTextAlignment(TextBoxBase.ALIGN_CENTER);
setStyleName(Resources.INSTANCE.css().fwFormEntry());
}
public void setMinDigits(int minDigits) {
if (minDigits > 0) {
this.minDigits = minDigits;
String value = getText();
long newValue = parseValue(value);
setText(formatValue(newValue));
}
}
public void setSteps(int step) {
this.step = step;
}
protected void increaseValue() {
if (step != 0) {
String value = getText();
long newValue = parseValue(value);
newValue += step;
if (maxConstrained && (newValue > max)) {
return;
}
setValue(formatValue(newValue));
}
}
protected void decreaseValue() {
if (step != 0) {
String value = getText();
long newValue = parseValue(value);
newValue -= step;
if (minConstrained && (newValue < min)) {
return;
}
setValue(formatValue(newValue));
}
}
/**
* #param value
* the value to format
* #return the formatted value
*/
protected String formatValue(long value) {
String newValue = String.valueOf(value);
if (minDigits > newValue.length()) {
String leading = StringUtils.repeat("0", (minDigits - newValue
.length()));
newValue = leading + newValue;
}
return newValue;
}
#Override
public void setValue(String value) {
setValue(value, false);
}
#Override
public void setValue(String value, boolean fireEvents) {
try {
long newValue = parseValue(value);
if ((maxConstrained && (newValue > max))
|| (minConstrained && (newValue < min))) {
return;
}
String prevText = getValue();
super.setText(formatValue(newValue));
if (fireEvents) {
ValueChangeEvent.fireIfNotEqual(this, getValue(), prevText);
}
} catch (Exception ex) {
// Do Nothing
System.out.println(ex.getMessage());
}
}
/**
* #param value
* the value to parse
* #return the parsed value
*/
protected long parseValue(String value) {
return Long.valueOf(value);
}
}
Update: The code is available in https://github.com/ctasada/GWT-Eureka
Here is a simple KeyPressHandler to allow the user to input decimal numbers;
public void onKeyPress(KeyPressEvent event){
TextBox sender = (TextBox)event.getSource();
if (sender.isReadOnly() || !sender.isEnabled()) {
return;
}
Character charCode = event.getCharCode();
int unicodeCharCode = event.getUnicodeCharCode();
// allow digits, '.' and non-characters
if (!(Character.isDigit(charCode) || charCode == '.' || unicodeCharCode == 0)){
sender.cancelKey();
}
}
Don't know when these classes were added to GWT, but they work fine for me without any extra code:
com.google.gwt.user.client.ui.DoubleBox
com.google.gwt.user.client.ui.IntegerBox
com.google.gwt.user.client.ui.LongBox
For more advanced validation you may want to overwrite their base class ValueBox with some custom Parser...
Here is my implementation of NumberField. Very similar in functionality to Carlos's version, but with additional support for decimal input and non-numeric key filtering.
public class NumberBox extends TextBox
{
private boolean isDecimal = false;
public NumberBox( )
{
}
public boolean isDecimal( )
{
return isDecimal;
}
public void setDecimal( boolean isDecimal )
{
this.isDecimal = isDecimal;
}
public Integer getIntegerValue( )
{
return ( StringUtil.isEmpty( getSanitizedValue( ) ) ) ? null : Integer.parseInt( getSanitizedValue( ) );
}
#Override
protected void initialize( )
{
super.initialize( );
addStyleName( "number" );
this.addKeyPressHandler( new KeyPressHandler( )
{
public void onKeyPress( KeyPressEvent event )
{
if ( !isEnabled( ) || isReadOnly( ) )
return;
int keyCode = event.getNativeEvent( ).getKeyCode( );
// allow special keys
if ( ( keyCode == KeyCodes.KEY_BACKSPACE )
|| ( keyCode == KeyCodes.KEY_DELETE )
|| ( keyCode == KeyCodes.KEY_ENTER ) || ( keyCode == KeyCodes.KEY_ESCAPE ) || ( keyCode == KeyCodes.KEY_RIGHT )
|| ( keyCode == KeyCodes.KEY_LEFT ) || ( keyCode == KeyCodes.KEY_TAB ) )
return;
// check for decimal '.'
if ( isDecimal( ) && '.' == (char)keyCode && !getValue( ).contains( "." ) )
return;
// filter out non-digits
if ( Character.isDigit( charCode ) )
return;
cancelKey( );
}
} );
}
}
PS: Superclass TextBox is a custom class extending GWT TextBox with some additional application specific features. The method initialize() is basically invoked inside the TextBox constructor, and getSanitizedValue does some basic sanity checks with trimming.
Carlos Tasada answer works, but contains a bug: you should add event.isShiftKeyDown() check in onKeyPress handler before switch/case block. It will pass some symbols like '(' otherwise.
Based on Julian Downes answer you can do this:
text.addKeyPressHandler(new KeyPressHandler() {
#Override
public void onKeyPress(KeyPressEvent event) {
TextBox sender = (TextBox) event.getSource();
if (sender.isReadOnly() || !sender.isEnabled()) {
return;
}
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER){
return;
}
Character charCode = event.getCharCode();
try{
Double.parseDouble(sender.getText().concat(charCode.toString()));
}
catch(Exception e){
sender.cancelKey();
}
}
});