Java Code Examples for docking.widgets.filechooser.GhidraFileChooser#setSelectedFile()

The following examples show how to use docking.widgets.filechooser.GhidraFileChooser#setSelectedFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphExporterDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void chooseDestinationFile() {
	GhidraFileChooser chooser = new GhidraFileChooser(getComponent());
	chooser.setSelectedFile(getLastExportDirectory());
	chooser.setTitle("Select Output File");
	chooser.setApproveButtonText("Select Output File");
	chooser.setApproveButtonToolTipText("Select File");
	chooser.setFileSelectionMode(GhidraFileChooserMode.FILES_ONLY);

	chooser.setSelectedFileFilter(GhidraFileFilter.ALL);
	GraphExportFormat exporter = getSelectedExporter();
	if (exporter != null) {
		chooser.setFileFilter(
			new ExtensionFileFilter(exporter.getDefaultFileExtension(), exporter.toString()));
	}
	String filePath = filePathTextField.getText().trim();
	File currentFile = filePath.isEmpty() ? null : new File(filePath);
	if (currentFile != null) {
		chooser.setSelectedFile(currentFile);
	}
	File file = chooser.getSelectedFile();
	if (file != null) {
		setLastExportDirectory(file);
		filePathTextField.setText(file.getAbsolutePath());
	}
}
 
Example 2
Source File: ExporterDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void chooseDestinationFile() {
	GhidraFileChooser chooser = new GhidraFileChooser(getComponent());
	chooser.setSelectedFile(getLastExportDirectory());
	chooser.setTitle("Select Output File");
	chooser.setApproveButtonText("Select Output File");
	chooser.setApproveButtonToolTipText("Select File");
	chooser.setFileSelectionMode(GhidraFileChooserMode.FILES_ONLY);

	chooser.setSelectedFileFilter(GhidraFileFilter.ALL);
	Exporter exporter = getSelectedExporter();
	if (exporter != null && exporter.getDefaultFileExtension() != null) {
		chooser.setFileFilter(
			new ExtensionFileFilter(exporter.getDefaultFileExtension(), exporter.getName()));
	}
	String filePath = filePathTextField.getText().trim();
	File currentFile = filePath.isEmpty() ? null : new File(filePath);
	if (currentFile != null) {
		chooser.setSelectedFile(currentFile);
	}
	File file = chooser.getSelectedFile();
	if (file != null) {
		setLastExportDirectory(file);
		filePathTextField.setText(file.getAbsolutePath());
	}
}
 
Example 3
Source File: ToolSaving3Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportDefaultTool() throws Exception {

	//
	// Verify a 'default' tool does not contain 'config state' when written
	//

	PluginTool tool1 = launchTool(DEFAULT_TEST_TOOL_NAME);
	DockingActionIf exportAction = getAction(tool1, "Export Default Tool");
	performAction(exportAction, false);

	GhidraFileChooser chooser = waitForDialogComponent(GhidraFileChooser.class);
	File exportedFile = createTempFile("ExportedDefaultTool", ToolUtils.TOOL_EXTENSION);
	chooser.setSelectedFile(exportedFile);
	waitForUpdateOnChooser(chooser);
	pressButtonByText(chooser, "Export");

	OptionDialog overwriteDialog = waitForDialogComponent(OptionDialog.class);
	pressButtonByText(overwriteDialog, "Overwrite");
	waitForCondition(() -> exportedFile.length() > 0);

	assertExportedFileDoesNotContainsLine(exportedFile, "PLUGIN_STATE");
}
 
Example 4
Source File: ToolSaving3Test.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testExportedToolContainsConfigSettings() throws Exception {

	//
	// Regression test: ensure that an exported tool contains config settings
	//

	PluginTool tool1 = launchTool(DEFAULT_TEST_TOOL_NAME);
	DockingActionIf exportAction = getAction(tool1, "Export Tool");
	performAction(exportAction, false);

	GhidraFileChooser chooser = waitForDialogComponent(GhidraFileChooser.class);
	File exportedFile = createTempFile("ExportedTool", ToolUtils.TOOL_EXTENSION);
	chooser.setSelectedFile(exportedFile);
	waitForUpdateOnChooser(chooser);
	pressButtonByText(chooser, "Export");

	OptionDialog overwriteDialog = waitForDialogComponent(OptionDialog.class);
	pressButtonByText(overwriteDialog, "Overwrite");
	waitForCondition(() -> exportedFile.length() > 0);

	assertExportedFileContainsLine(exportedFile, "PLUGIN_STATE");
}
 
Example 5
Source File: DragonHelper.java    From dragondance with GNU General Public License v3.0 5 votes vote down vote up
public static String askFile(Component parent, String title, String okButtonText) {
	
	GhidraFileChooser gfc = new GhidraFileChooser(parent);
	
	if (!Globals.LastFileDialogPath.isEmpty()) {
		File def = new File(Globals.LastFileDialogPath);
		gfc.setSelectedFile(def);
	}
	
	gfc.setTitle(title);
	gfc.setApproveButtonText(okButtonText);
	gfc.setFileSelectionMode(GhidraFileChooserMode.FILES_ONLY);
	
	File file = gfc.getSelectedFile();
	
	if (file == null) {
		return null;
	}
	
	if (!file.exists())
		return null;
	
	Globals.LastFileDialogPath =  Util.getDirectoryOfFile(file.getAbsolutePath());
	
	if (Globals.LastFileDialogPath == null)
		Globals.LastFileDialogPath = System.getProperty("user.dir");
	
	return file.getAbsolutePath();
}
 
Example 6
Source File: ImporterPluginScreenShots.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private ImporterDialog selectFileForImport(String fileToImport) throws Exception {
	GhidraFileChooser fileChooser = waitForDialogComponent(GhidraFileChooser.class);

	File testDataFile = getTestDataFile(fileToImport);
	fileChooser.setSelectedFile(testDataFile);
	waitForUpdateOnChooser(fileChooser);

	pressButtonByName(fileChooser.getComponent(), "OK");

	ImporterDialog importerDialog = waitForDialogComponent(ImporterDialog.class);
	assertNotNull(importerDialog);
	return importerDialog;
}
 
Example 7
Source File: ImporterPluginScreenShots.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private BatchImportDialog selectDirForBatchImport(String rootDir) throws Exception {
	GhidraFileChooser fileChooser = waitForDialogComponent(GhidraFileChooser.class);

	File testDataFile = getTestDataDir("pe");
	fileChooser.setSelectedFile(testDataFile);
	waitForUpdateOnChooser(fileChooser);

	pressButtonByName(fileChooser.getComponent(), "OK");

	BatchImportDialog dialog = waitForDialogComponent(BatchImportDialog.class);
	assertNotNull(dialog);
	return dialog;
}
 
Example 8
Source File: GTable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private GhidraFileChooser createExportFileChooser() {
	GhidraFileChooser chooser = new GhidraFileChooser(GTable.this);
	chooser.setTitle(GTableToCSV.TITLE);
	chooser.setApproveButtonText("OK");

	String filepath = Preferences.getProperty(LAST_EXPORT_FILE);
	if (filepath != null) {
		chooser.setSelectedFile(new File(filepath));
	}

	return chooser;
}
 
Example 9
Source File: ExportToCAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private File getFile(DecompilerPanel decompilerPanel) {
	File lastUsedFile = readLastUsedFile();

	String[] extensions = new String[] { "h", "c", "cpp" };
	GhidraFileChooser fileChooser = new GhidraFileChooser(decompilerPanel);
	fileChooser.setFileFilter(new ExtensionFileFilter(extensions, "C/C++ Files"));
	if (lastUsedFile != null) {
		fileChooser.setSelectedFile(lastUsedFile);
	}
	File file = fileChooser.getSelectedFile();
	if (file == null) {
		return null;
	}

	saveLastUsedFileFile(file);

	boolean hasExtension = false;
	String path = file.getAbsolutePath();
	for (String element : extensions) {
		if (path.toLowerCase().endsWith("." + element)) {
			hasExtension = true;
		}
	}

	if (!hasExtension) {
		file = new File(path + ".c");
	}
	return file;
}
 
Example 10
Source File: RestoreDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a directory chooser for selecting the directory where the 
 * archive will be restored..
 * @return the file chooser.
 */
private GhidraFileChooser createDirectoryChooser() {
	GhidraFileChooser fileChooser = new GhidraFileChooser(null);
	// start the browsing in the user's preferred project directory
	File projectDirectory = new File(GenericRunInfo.getProjectsDirPath());
	fileChooser.setFileSelectionMode(GhidraFileChooser.DIRECTORIES_ONLY);
	fileChooser.setCurrentDirectory(projectDirectory);
	fileChooser.setSelectedFile(projectDirectory);

	return fileChooser;
}
 
Example 11
Source File: KeyBindingUtilsTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private File findAndTestFileChooser(File path, String filename) throws Exception {
	// get the file chooser and set the file it will use
	GhidraFileChooser fileChooser = waitForDialogComponent(GhidraFileChooser.class);
	if (fileChooser == null) {
		Msg.debug(this, "Couldn't find file chooser");
		printOpenWindows();
		Assert.fail("Did not get the expected GhidraFileChooser");
	}

	// change directories
	if (path != null) {
		fileChooser.setCurrentDirectory(path);
	}
	waitForUpdateOnChooser(fileChooser);

	File currentDirectory = fileChooser.getCurrentDirectory();
	File fileToSelect = new File(currentDirectory, filename);
	fileChooser.setSelectedFile(fileToSelect);
	waitForUpdateOnChooser(fileChooser);

	// press OK on the file chooser
	final JButton okButton = (JButton) getInstanceField("okButton", fileChooser);

	runSwing(() -> okButton.doClick());

	// wait to make sure that there is enough time to write the data
	waitForPostedSwingRunnables();

	// make sure that the file was created or already existed
	File selectedFile = fileChooser.getSelectedFile(false);
	assertTrue("The test file was not created after exporting.", selectedFile.exists());

	return selectedFile;
}
 
Example 12
Source File: FrontEndPlugin.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * general project utility that brings up a file chooser for
 * the user to specify a directory and filename that are used
 * for the Project location and name
 * 
 * @param fileChooser the chooser used to pick the project
 * @param mode read-only or not
 * @param preferenceName the preference property name used to save the last opened project
 * @return the project locator for the opened project 
 */
ProjectLocator chooseProject(GhidraFileChooser fileChooser, String mode,
		String preferenceName) {
	boolean create = (mode.equals("Create")) ? true : false;
	fileChooser.setTitle(mode + " a Ghidra Project");
	fileChooser.setApproveButtonText(mode + " Project");
	fileChooser.setApproveButtonToolTipText(mode + " a Ghidra Project");
	fileChooser.setSelectedFile(null);

	boolean validInput = false;
	while (!validInput) {
		File file = fileChooser.getSelectedFile();

		if (file != null) {
			String path = file.getAbsoluteFile().getParent();
			String filename = file.getName();

			// strip off extension since the LocalRootFolder takes care of it
			if (filename.endsWith(PROJECT_EXTENSION)) {
				filename = filename.substring(0, filename.lastIndexOf(PROJECT_EXTENSION) - 1);
			}
			// if user enters the name of the project manually and leaves off
			// the extension, try to open or create using the extension
			else if (!create && filename.lastIndexOf(".") > path.lastIndexOf(File.separator)) {
				// treat opening a file without the ghidra extension as an error
				Msg.showError(getClass(), tool.getToolFrame(), "Invalid Project File",
					"Cannot open '" + file.getName() + "' as a Ghidra Project");
				continue;
			}
			if (!NamingUtilities.isValidProjectName(filename)) {
				Msg.showError(getClass(), tool.getToolFrame(), "Invalid Project Name",
					filename + " is not a valid project name");
				continue;
			}
			Preferences.setProperty(preferenceName, path);
			try {
				Preferences.store();
			}
			catch (Exception e) {
				Msg.debug(this,
					"Unexpected exception storing preferences to" + Preferences.getFilename(),
					e);
			}
			return new ProjectLocator(path, filename);
		}
		return null;
	}

	return null;
}
 
Example 13
Source File: PdbSymbolServerPlugin.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private File askForLocalStorageLocation() throws CancelledException {

		final GhidraFileChooser fileChooser = new GhidraFileChooser(tool.getActiveWindow());

		// Need to store the variable in an array to allow the final variable to be reassigned.
		// Using an array prevents the compiler from warning about "The final local variable
		// cannot be assigned, since it is defined in an enclosing type."
		final File[] chosenDir = new File[1];

		File testDirectory = null;

		// localDir is not null if we already parsed the _NT_SYMBOL_PATH environment var
		if (localDir != null) {
			testDirectory = localDir;
		}
		else {
			String userHome = System.getProperty("user.home");

			String storedLocalDir =
				Preferences.getProperty(PdbParser.PDB_STORAGE_PROPERTY, userHome, true);

			testDirectory = new File(storedLocalDir);

			if (!testDirectory.exists()) {
				testDirectory = new File(userHome);
			}
		}

		final File storedDirectory = testDirectory;

		Runnable r = () -> {
			while (chosenDir[0] == null && !fileChooser.wasCancelled()) {
				fileChooser.setSelectedFile(storedDirectory);

				fileChooser.setTitle("Select Location to Save Retrieved File");
				fileChooser.setApproveButtonText("OK");
				fileChooser.setFileSelectionMode(GhidraFileChooserMode.DIRECTORIES_ONLY);
				chosenDir[0] = fileChooser.getSelectedFile();

				if (chosenDir[0] != null) {
					if (!chosenDir[0].exists()) {
						Msg.showInfo(getClass(), null, "Directory does not exist",
							"The directory '" + chosenDir[0].getAbsolutePath() +
								"' does not exist. Please create it or choose a valid directory.");
						chosenDir[0] = null;
					}
					else if (chosenDir[0].isFile()) {
						Msg.showInfo(getClass(), null, "Invalid Directory",
							"The location '" + chosenDir[0].getAbsolutePath() +
								"' represents a file, not a directory. Please choose a directory.");
						chosenDir[0] = null;
					}
				}
			}
		};
		SystemUtilities.runSwingNow(r);

		if (fileChooser.wasCancelled()) {
			throw new CancelledException();
		}

		Preferences.setProperty(PdbParser.PDB_STORAGE_PROPERTY, chosenDir[0].getAbsolutePath());

		return chosenDir[0];
	}