Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] The styledText's offset issue

Hi,
   I was trying to implement the drag and drop in styledtext widget.  When I
dragged one item from view and dropped at empty line of the styled text
widget, I need to insert a string (which is relative to the dragged item) at
that location relative to the mouse cursor position. At that time I found
one behavior is that the offset of the StyledText is getting reduced by one.
It was throwing RuntimeException(IllegalArgument)

		try {
			insertText(text,startNextLineOffset -1);
		} catch (RuntimeException e1) {
			insertText(text,startNextLineOffset -2);
		}
So I need to reduce one further. Sometimes the try block works. Once it
reaches the catch block, from the next time onwards only the catch block is
getting executed. This code works fine. But it would be nice to know the
reason. 

Thanks and regards,
ArunachalaM

********************************************************************

FYI, the code snippet is below, the problematic code is italicized. 
	
      StyledText text = MyEditor.getTextWidget();
      Point location =
Display.getDefault().map(null,text,event.x,event.y);//new Point(event.x,
event.y);
	Point maxLocation = text.getLocationAtOffset(text.getCharCount());
	int maxOffset = text.getCharCount();
	if (location.y >= maxLocation.y + text.getLineHeight()) {
	  insertText(text, maxOffset);
	  return;
	}

	int startLineOffset= text.getOffsetAtLocation(new Point(0,
location.y));
	int line = text.getLineAtOffset(startLineOffset);
	if (line == text.getLineCount() - 1) {
		if (location.x > maxLocation.x + text.getLocation().x) {
			insertText(text,maxOffset);
		} else {
			int offset = text.getOffsetAtLocation(location);
			insertText(text,offset);
		}
		return;
      }
      int startNextLineOffset = text.getOffsetAtLine(line+1);
 	Point lineEnd = text.getLocationAtOffset(startNextLineOffset - 1);
	if (location.x > lineEnd.x) {
		try {
			insertText(text,startNextLineOffset -1);
		} catch (RuntimeException e1) {
			insertText(text,startNextLineOffset -2);
		}
	} else {
	   int offset = text.getOffsetAtLocation(location);
	   insertText(text,offset);
         return;
	}
    
	private void insertText(StyledText text, int offset) {
	    text.setCaretOffset(offset);
	    text.insert(fqnName);
	}        
      

<<attachment: winmail.dat>>


Back to the top