How to Create a customized Directory using java - java

Problem:
I wanted to create a directory such that when I click on a button for one time the directory must be created with the name "Node_1". when I click on the same button once again the directory must be created with the name "Node_2".
Please do not bother about the button. I need the code where the count must be incremented as stated above.
The below sample code will create a directory with the name "Node_1" When I click once.
But when I click button Once again, it will create a directory with the same name "Node_1" again. But I wanted to create with the name "Node_2".
String src = "C:\\Users\\DELL\\Documents\\NetBeansProjects\\src\\Nodes\\Node_1\\n1_sample.txt";

Hope, this will help:
int clickCounter = 0;
void onClick() {
++clickCounter;
.
.
String src = "...\\Node_" + clickCounter + "\\...txt"
}
Note: Learn to ask question in a better way. Share what you want to achieve, what you've tried already, and what problem you are facing, and also keep it simple.

Related

Problem clicking elements using class and index

I'm trying to write code to automatically DM people on Instagram. Im stuck on getting the code to click on the DM button.
In UIautomatorviewer, there is no text or a resource-id, so I tried using the class.
When I run the code it doesn't click on the right thing. It clicks on the button NEXT to it.
Here is the code -
By path2 = By.xpath("//android.widget.ImageView[#index='3']");
driver.findElement(path2).click();
Thread.sleep(5000);
Can anyone help? I'm new to this so i'm not very experienced.
Because I can not see the entire XML I can not really tell what you did wrong but probably your xpath is not correct.
With UIAutomatorViewer you can save the XML and then you can test your xpath on it. Either with XMLSpy or an online tool like https://www.freeformatter.com/xpath-tester.html.
Probably there are more android.widget.ImageView with index = 3 and I think appium selects the first one? So you could change your xpath to a more unique one like:
//android.widget.FrameLayout//android.widget.LinearLayout//android.widget.ImageView[#index='3']"
Based on UIAutomator Viewer screen you can also use content-desc as shown below
By path2 = By.xpath("//android.widget.ImageView[contains(#content-desc,'Message')]");
driver.findElement(path2).click();

How can I disable a dialog in ImageJ?

I'm using ImageJ a lot to look at image stacks composed of a number of single images sitting in one folder. I can just drag and drop the respective folder into the ImageJ GUI and it creates a scrollable visualization, which is very convenient. It could be even more convenient though since each time I do it, a dialog appears asking whether I want to open all images in the folder as a stack. Is it possible to make it default to "Yes"? Would I need to change the source code and compile it myself..? If that is the case, where could I start looking?
A suggestion would be to make a feature request to the author of Imagej Wayne rasband, e.g., at the Github repository:
https://github.com/imagej/imagej1
Or you can write a small macro (use the macro recorder with the menu actions!) which can be also be installed in ImageJ. Something like:
run("Image Sequence...", "open=C:\\images\\ sort");
Here the macro docs:
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/docs/guide/146-14.html
To disable the dialog in the source code: Find the source file ij>plugin>DragAndDrop.java. From its openDirectory method, delete the dialog-related lines and assign boolean values to convertToRGB and virtualStack, both of which are normally defined by check boxes in the now defunct dialog window. The code should now look like this:
private void openDirectory(File f, String path) {
if (path==null) return;
if (!(path.endsWith(File.separator)||path.endsWith("/")))
path += File.separator;
String[] names = f.list();
names = (new FolderOpener()).trimFileList(names);
if (names==null)
return;
convertToRGB = false;
virtualStack = false;
String options = " sort";
if (convertToRGB) options += " convert_to_rgb";
if (virtualStack) options += " use";
IJ.run("Image Sequence...", "open=[" + path + "]"+options);
DirectoryChooser.setDefaultDirectory(path);
IJ.register(DragAndDrop.class);
}
I did this with ImageJ 1.51p. The source code can be downloaded here. After making these changes, just run the build.xml ant script.
Note that writing a macro might provide a cleaner and more portable way to achieve this--refer to Marcel's answer for further reading.

What is the best way to make a checkbox remember it's state?

I have a javafx application that uses many checkboxes. When a checkbox is selected, a button will appear in the main window. Checkbox set to "false" makes the button disappear. Easy enough.
Problem is that the checkbox does not remember that it was checked and the application always starts with the checkboxes selected, as defined in the fxml file:
<CheckBox fx:id="googleHider" onAction="#hideGoogle" selected="true" text="Google">
Since I couldn't find anything satisfying on Google, I came up with a homegrown solution, setting up a function that sets the selected state of the checkbox to "true" or "false" directly in the fxml document, which works perfectly.
I just wonder if this is good practice, simple and effective, or if somebody has found or can imagine a more elegant or minimalistic solution. (My previous programming experience is more in Python, I don't know if it shows. Is there a more "Javaian" way...?)
boolean isSelected = googleHider.isSelected();
if (isSelected == false) {
System.out.println("not selected");
Path path = Paths.get("src/javafxapplication2/PopupFXML.fxml");
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path));
//following line changes the initial state of the checkbox from "true" to "false"
content = content.replaceAll("\"#hideGoogle\" selected=\"true\"", "\"#hideGoogle\" selected=\"false\"");
Files.write(path, content.getBytes(charset));
}
else {
System.out.println("selected");
Path path = Paths.get("src/javafxapplication2/PopupFXML.fxml");
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path));
// next line changes the state back from "false" to "true"
content = content.replaceAll("\"#hideGoogle\" selected=\"false\"", "\"#hideGoogle\" selected=\"true\"");
Files.write(path, content.getBytes(charset));
}
I would definnitely use a settings file. Such a settings file should not be located in your application as you cannot always modify your application while it is beeing executed. Good locations are the AppData folder (on windows) or the user directory (on linux and mac) or the folder where the application is saved in.
I wrote a small library that creates *.properties-files and saves them in the AppData folder (or the user directory if not on windows), you can use it too: Common
You can create SharedPrefrences and add value using .putBoolean().
if(ch.isSelected()){
SharedPreferences settings = getSharedPreferences(PREFRENCES_NAME, 0);
settings.edit().putBoolean("check",true).commit();
}
You can do that in many ways. In the link provided below, you will understand the basic concept of user session management and sharing data among activites.
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

Eclipse RCP 4.x - Defining workspace location

I know that this question was asked many times, but I didn't find an exact answer which would fulfill my desires :)
Long story short:
I've got simple E4 application, product project, feature and main plugin with simple trim window.
Works, after exporting works too.
Now. I add lifeCycleURI property, create bundleclass for it and create simple dialog with Text area and a Button. Run it\export it and it works, before running main Trim Window dialog is shown. Fine.. Cool etc.
But I want to enter location eg. C:\TEST and after clicking button I want it to be my workspace area for the application (with .metedata and so on). HOW ???
Of course I've tried with :
Location instanceLocation = Platform.getInstanceLocation();
instanceLocation.set(new URL("file", null, "C:\TEST"), false);
But... It says that I can't change location cause it is already set... Tried to use above in Activator. The same. Tried to add
-data #noDefault in products Launching Arguments ... The same...
I always try to accomplish my tasks by myself but this.... this... ehh... Help ?
You should be able to do this in the #PostContextCreate method of the life cycle class. Don't specify the '-data' argument
#PostContextCreate
public void postContextCreate()
{
Location instanceLoc = Platform.getInstanceLocation();
// Stop if location is set
if (instanceLoc.isSet())
return;
File file = new File("C:\\TEST");
instanceLocation.set(file.toURL(), false);
}
Note: You need '\\' in your file path.
This is adapted from code which I use in my e4 RCP.
If you are currently testing the application from within Eclipse you will need to clear the workspace location in the 'Run Configuration' for the application. Open 'Run > Run Configurations', find your application and clear the 'Location' field on the 'Main' tab.

Eclipse: Ctrl+Shift+Right is incorrect

Consider this simple class:
package net.zomis.test;
public class Test {
public Test() {
}
public void registerSomething(String key) {
}
}
When I have the cursor placed right before registerSomething and pressing CtrlShift→, I'm used to Eclipse only selecting register at first. But now, it selects the entire registerSomething text, when I press it again it selects the entire method (from public void to }), when I press it again it selects the entire class, and the last time the entire file is selected. The exactly same thing happens if I press CtrlShift←.
Also, when I simply click the End key, I get an option to choose: Line End or Text End.
What has gone wrong in my settings for this to happen? Where can I find these keybindings?
If it matters, I have this workspace within my Dropbox-folder to synchronize it between my PC and Mac.
Turns our that the fact that I had the workspace in my Dropbox folder matters a lot. Note to everybody else: Do not share your workspace between computers. How I solved this is the following:
Create a new workspace on each computer
In the computer-specific workspace, import projects from your shared Dropbox/other folder
I noticed that when having a shared workspace on the computers, the Mac somehow changed the key-bindings to the Mac-version, which made them not work on the PC. The correct Ctrl+Shift+→ key-binding on Mac is Alt+Shift+→.
So by using a separate workspace on each computer, I can use that computer's specific key-bindings, without mixing them up.
Open the preferences dialog from the Main Menu: Window->Preferences.
In the dialog, open General->Keys. You'll see a table of keybindings, with a field with type filter text. In that field, type: Ctrl+Shift+Right, and you'll narrow the table down to this key.
The behavior that you expect - only register is selected with the first keypress - is the behavior that I see when using Ctrl+Shift+Right, and the keybinding for me is: Select Next Word.
There is a Restore Command button that should reset this binding if it has been changed.

Categories

Resources