I am usign GWT2.3.
We developed CustomPager by overriding SimplePager.
We override createText() method such a way that we are showing string like "Page 1 of 4" using following code
public String createText() {
if(searchRecordCount%pageSizeForText == 0){
totalPages = searchRecordCount/pageSizeForText;
}else{
totalPages = (searchRecordCount/pageSizeForText) + 1;
}
NumberFormat formatter = NumberFormat.getFormat("#,###");
return "Page "+formatter.format(this.getPage()+1) + " of " + formatter.format(totalPages);
}
Now I want to use TextBox for CurrentPage so that user can enter page Number in textBox. (Functionality GoTo entered pageNumber)
createText() returns string so I cant user textBox ;) + Can't provide css
How can I do this ? Is there any way to solve this problem? Workaround if any or Sample code
There are two ways how to achieve this:
1.) Use HTML code to create a TextBox:
In the createText() function you can create the textbox manually by using HTML code (better use SafeHTML templates for avoiding XSS):
String textbox_str = "<input type='textbox' name='goto'>";
However you have to write code for handling the actual event (i.e. ChangeEvent) and call setPage() of your SimplePager using JSNI.
2.) Add TextBox widget to SimplePager and override constructor:
SimplePager is basically a Composite which adds ImageButtons in its constructor for the forward and backward links.
You can extend SimplePager add a TextBox and override the constructor to add the TextBox between the forward and backward ImageButtons.
Related
I want to search every matched keyword in a pdf file and get their position in the page which they located.
I just found some code in iText5 which looks like match what I need
for (i = 1; i <= pageNum; i++)
{
pdfReaderContentParser.processContent(i, new RenderListener()
{
#Override
public void renderText(TextRenderInfo textRenderInfo)
{
String text = textRenderInfo.getText();
if (null != text && text.contains(KEY_WORD))
{
Float boundingRectange = textRenderInfo
.getBaseline().getBoundingRectange();
resu = new float[3];
System.out.println("======="+text);
System.out.println("h:"+boundingRectange.getHeight());
System.out.println("w:"+boundingRectange.width);
System.out.println("centerX:"+boundingRectange.getCenterX());
System.out.println("centerY:"+boundingRectange.getCenterY());
System.out.println("x:"+boundingRectange.getX());
System.out.println("y:"+boundingRectange.getY());
System.out.println("maxX:"+boundingRectange.getMaxX());
System.out.println("maxY:"+boundingRectange.getMaxY());
System.out.println("minX:"+boundingRectange.getMinX());
System.out.println("minY:"+boundingRectange.getMinY());
resu[0] = boundingRectange.x;
resu[1] = boundingRectange.y;
resu[2] = i;
}
}
#Override
public void renderImage(ImageRenderInfo arg0)
{
}
#Override
public void endTextBlock()
{
}
#Override
public void beginTextBlock()
{
}
});
But I don't know how to deal with it in iText7 .
iText7 has pdf2Data add-on which can easily help you achieving your goal (and help with other data extraction cases).
Let's say you want to extract positions of word Header. We go to https://pdf2data.online demo application, upload our template (any file containing the words you want to extract), and go to data field editor which looks like this:
Now, you can add a data field with a selector that would select the data you are interested in. In this case you can use Regular expression selector which is very flexible generally, but in our case the settings are pretty straightforward:
You can see that the editor application highlights all occurrences of the word we are searching for. Now, let's get back to the first step (there is an icon at the top right of the editor to go back to demo), and download our template (link to the bottom of the icon corresponding to the uploaded file).
Now you can look over the information on how to include pdf2Data in your project at this page: https://pdf2data.online/gettingStarted, roughly the code you need is the following:
LicenseKey.loadLicenseFile("license.xml");
Template template = Pdf2DataExtractor.parseTemplateFromPDF("Template.pdf");
Pdf2DataExtractor extractor = new Pdf2DataExtractor(template);
ParsingResult result = extractor.recognize("toParse.pdf");
for (ResultElement element : result.getResults("Headers")) {
Rectangle bbox = element.getBbox();
int page = element.getPage();
System.out.println(MessageFormat.format("Coordinates on page {0}: [{1}, {2}, {3}, {4}]",
page, bbox.getX(), bbox.getY(), bbox.getX() + bbox.getWidth(), bbox.getY() + bbox.getHeight()));
}
Example output:
Coordinates on page 1: [38.5, 788.346, 77.848, 799.446]
Coordinates on page 1: [123.05, 788.346, 162.398, 799.446]
Coordinates on page 1: [207.6, 788.346, 246.948, 799.446]
Coordinates on page 2: [38.5, 788.346, 77.848, 799.446]
Coordinates on page 2: [123.05, 788.346, 162.398, 799.446]
Coordinates on page 2: [207.6, 788.346, 246.948, 799.446]
pdf2Data add-on is closed source and available only at a commercial license option at the moment. Of course it is possible to port your code directly to iText7 and this would be another solution to the task you have, but I must warn you that your code is not universal for all scenarios, e.g. text in a PDF can be written letter by letter, instead of writing a whole word at once (the visual appearance of the two PDFs can easily stay the same), and in this case the code you attached would not work. pdf2Data handles those cases out of the box, taking the burden out of your shoulders.
I can switch between two tabs/windows but my requirement is to know or get active window between them.
In my project, on a click of a webElement of a page a random pop(tab/window) gets opened and I would like to know whether that(new) window has focus or my original page.
I tried to use JNA Api to get the active window and its title but my web page is
remotely located.
Perfect solution is greatly appreciated.
Thanks
driver.getTitle() will give you the title of the page that you can use to determine which page you are on or if you are on the page where you want to be and then use the logic to switch window if required. getTitle() returns a String and you can use one of the string methods to compare the title, for example:
String title = getDriver().getTitle();
if(!title.equals("Expected Title")) {
//may be you would like to switch window here
}
String title = driver.getTitle()
This will give you the title of the page which you can refer to using Selenium to figure out which page the driver is currently on.
I wrote my own method to switch to a window if the window title is known, maybe some of this would be helpful. I used Selenide (Java) methods for this, but if you've got Vanilla WebDriver, you can achieve the same thing
/** Switches user to window of user's choice */
public static void switchToWindow(String windowTitle) {
WebDriver driver = getWebDriver();
// Get list of all open tabs - note behaviour may be different between FireFox and Chrome.
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
// iterate through open tabs. If the title of the page is contained in the tab, switch to it.
for (String windowHandle : driver.getWindowHandles()) {
String title = getWebDriver().getTitle();
driver.switchTo().window(windowHandle);
if (title.equalsIgnoreCase(windowTitle)) {
break;
}
}
}
This method might not be lightening fast, but it will iterate through current open windows and check the title matches the one you've specified.
If you want to assert the title, you could use the xpath selector:
String pageTitle = driver.findElement(By.xpath("//title[text() = 'Title you looking for']"));
This is a dumb example with you can surround with try/catch, implement assertions or other technique to have the result you need.
In JavaScript, for me this worked. After clicking on the first link of bing search results in edge, my link opened in a new tab. I explicitly mentioned to stay in the same tab.
async function switchTab() {
await driver.getAllWindowHandles().then(async function (handles) {
await driver.switchTo().window(handles[1]);
});
}
//get initial window handles
Set<String> prevWindowHandles = driver.getWindowHandles();
while(true){
//get current window handles
Set<String> currWindowHandles = driver.getWindowHandles();
//if one of the current window handles not equals to
//any of the previous window handles,switch to this window
//and prevWindowHandles = currWindowHandles
for(String prevHandle : prevWindowHandles){
int noEqualNum = 0;
for(String currHandle : currWindowHandles){
if(!currHandle.equals(prevHandle))
noEqualNum++
}
if(noEqualNum == currWindowHandles.size()){
driver.switchTo().window(currWindow);
prevWindowHandles = currWindowHandles;
break;
}
}
}
I have two textareas in jsp page the values entered in first textarea value should be displayed in next text area frequently using jQuery.
Here is the code I've tried so far
function getMsg()
{
document.getElementById("chatBox").value =
document.getElementById("messageBox").value;
document.getElementById("messageBox").value=""; }'
}
$(function() {
$("#test1").keyup(function() {
$('#test2').val(this.value);
});
});
Is this you were expecting
$(document).ready(function () {
$('#firsttext_area').keyup(function () {
$("#sectext_area").val( $('#firsttext_area').val());
});
});
Demo in fiddle
In jquery you can try the following
$("#textAreaOne").keyup(function() {
$("#textAreaTwo").val($("#textAreaOne").val());
});
Update:
It sounds from your comment that you want to append text to the second text area, you would have to create a button that the user will click to fire your event
Create a button, for example
<button id="chatButton">Chat</button>
Then wire the buttons onclick event to perform the append functionality, for example
$("#chatButton").click(function() {
$("#textAreaTwo").val($("#textAreaTwo").val() + "\n\n" + $("textAreaOne").val());
});
This is what the exact answer for my question. I tried with this and it is working.
<script>
var tempMsg;
function getValue()
{
var userMsg="Me: "+document.getElementById("messageBox").value;
var chatMsg=document.getElementById("chatBox");
tempMsg=chatMsg.innerHTML+=userMsg+"\n\n";
}
</script>
You can implement this by event onChange.
I want to change the text of selected text in JTextArea.
For example when i press button i want the selected text to be change (Original Text Selected - I want to replace like this when i press the button : Replace:Original Text Selected) this is what i am trying to do in my code,
String replacement = "Replace:" + messageBodyText.getSelectedText() ";
but i have no idea how to change only selected text i am trying to do something but i am changing entire text of JTextArea Hope you understood my question ?
Thanks to Hovercraft Full Of Eels he solved my problem this is my code for other people who are facing the same problem :
int start = messageBodyText.getSelectionStart();
int end = messageBodyText.getSelectionEnd();
StringBuilder strBuilder = new StringBuilder(messageBodyText.getText());
strBuilder.replace(start, end, "Replace:" + messageBodyText.getSelectedText() + ".");
messageBodyText.setText(strBuilder.toString());
textComponent.replaceSelection(newText);
JTextComponent (and thus JTextArea) has getSelectionStart() and getSelectionEnd() methods that will help you. Get your text from the JTextArea or its Document, and using these int values you can change your text and replace it into the text component.
For example,
int start = myTextField.getSelectionStart();
int end = myTextField.getSelectionEnd();
StringBuilder strBuilder = new StringBuilder(myTextField.getText());
strBuilder.replace(start, end, newText);
myTextField.setText(strBuilder.toString());
My JEditorPane automatically wraps words; I don't want that. All I want is a horizontal bar to appear that allows the user to write as much as desired. How can I do that? I have tried several methods. I have overridden the getScrollableTracksViewportWidth(), but that didn't help. Does any one know how I can turn off the word wrap?
A quick google search lead me to this page, which implements it by subclassing the text pane and overriding the getScrollableTracksViewportWidth() method:
// Override getScrollableTracksViewportWidth
// to preserve the full width of the text
public boolean getScrollableTracksViewportWidth() {
Component parent = getParent();
ComponentUI ui = getUI();
return parent != null ? (ui.getPreferredSize(this).width <= parent
.getSize().width) : true;
}
Try this
http://java-sl.com/wrap.html
If you can control the text which is going into, and you are using features of JEditorPane, you can mark the code via html, and use white-space:nowrap; stile property.
jEditorPane1.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("<div style='");
if (!wordWrap.isSelected()) { //some checkbox
sb.append("white-space:nowrap;");
}
sb.append("font-family:\"Monospaced\">'");
sb.append("your very interesting long and full of spaces text");
/*be aware, more then one space in row will be replaced by single space
to avoid it you need to substitute by .
Also rememberer that \n have to be repalced by <br>
so filering like:
line = line.replaceAll("\n", "<br>\n"); //be aware, <br/> do not work
line = line.replaceAll(" ", " ");
line = line.replaceAll("\t", " ");
may be usefull.*/
sb.append("</div>");
jEditorPane1.settext(sb.toString()); //jeditor pane do not support addition/insertion of text in html mode
why don't you use a jTextField?
it's only one line.