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

The following examples show how to use docking.widgets.filechooser.GhidraFileChooser#setFileSelectionMode() . 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: SaveImageAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static File getExportFile() {

		GhidraFileChooser chooser = new GhidraFileChooser(null);
		chooser.setTitle("Export Image As...");
		chooser.setApproveButtonText("Save As");
		chooser.setFileSelectionMode(GhidraFileChooserMode.FILES_ONLY);
		chooser.setMultiSelectionEnabled(false);

		File selected = chooser.getSelectedFile(true);

		if (chooser.wasCancelled()) {
			return null;
		}

		return selected;

	}
 
Example 2
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 3
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 4
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 5
Source File: EditActionManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private GhidraFileChooser createCertFileChooser() {

		GhidraFileChooser fileChooser = new GhidraFileChooser(tool.getToolFrame());
		fileChooser.setTitle("Select Certificate (req'd for PKI authentication only)");
		fileChooser.setApproveButtonText("Set Certificate");
		fileChooser.setFileFilter(ApplicationKeyManagerFactory.CERTIFICATE_FILE_FILTER);
		fileChooser.setFileSelectionMode(GhidraFileChooser.FILES_ONLY);
		fileChooser.setHelpLocation(new HelpLocation(plugin.getName(), "Set_PKI_Certificate"));
		return fileChooser;
	}
 
Example 6
Source File: SaveToolConfigDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Pop up a file chooser for the user to look for icon images.
 */
private void browseForIcons() {
	GhidraFileChooser iconFileChooser = new GhidraFileChooser(getComponent());
	iconFileChooser.setFileSelectionMode(GhidraFileChooser.FILES_ONLY);
	iconFileChooser.setTitle("Choose Icon");
	iconFileChooser.setApproveButtonToolTipText("Choose Icon");
	iconFileChooser.setFileFilter(
		new ExtensionFileFilter(new String[] { "gif", "jpg", "bmp", "png" }, "Image Files"));
	String iconDir = Preferences.getProperty(LAST_ICON_DIRECTORY);
	if (iconDir != null) {
		iconFileChooser.setCurrentDirectory(new File(iconDir));
	}
	File file = iconFileChooser.getSelectedFile();
	if (file == null) {
		return;
	}

	String filename = file.getAbsolutePath();
	iconField.setText(filename);

	ToolIconURL url = new ToolIconURL(filename);
	iconListModel.addElement(url);
	iconList.setSelectedValue(url, true);
	setPicture(url);

	Preferences.setProperty(LAST_ICON_DIRECTORY, file.getParent());
}
 
Example 7
Source File: FidDebugPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a raw read-only database file from a packed database.  This file is useful
 * for including in a distribution.
 */
protected void createRawFile() {
	FidFile fidFile = askChoice("Choose destination FidDb",
		"Please choose the destination FidDb for population",
		fidFileManager.getUserAddedFiles(), null);
	if (fidFile == null) {
		return;
	}
	GhidraFileChooser chooser = new GhidraFileChooser(tool.getToolFrame());
	chooser.setTitle("Where do you want to create the read-only installable database?");
	chooser.setFileSelectionMode(GhidraFileChooserMode.DIRECTORIES_ONLY);
	File selectedFile = chooser.getSelectedFile();
	if (selectedFile == null) {
		return;
	}
	File outputFile =
		new File(selectedFile, fidFile.getBaseName() + FidFile.FID_RAW_DATABASE_FILE_EXTENSION);

	try (FidDB fidDB = fidFile.getFidDB(false)) {
		fidDB.saveRawDatabaseFile(outputFile, TaskMonitor.DUMMY);
	}
	catch (VersionException | IOException | CancelledException e) {
		// BTW CancelledException can't happen because we used a DUMMY monitor.
		Msg.showError(this, null, "Error saving read-only installable database", e.getMessage(),
			e);
	}
}
 
Example 8
Source File: FidPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Method to ask for a file (copied from GhidraScript).
 * @param title popup window title
 * @param approveButtonText text for the "yes" button
 * @return the file chosen, or null
 */
private File askFile(final String title, final String approveButtonText) {
	final GhidraFileChooser chooser = new GhidraFileChooser(tool.getActiveWindow());
	chooser.setApproveButtonText(approveButtonText);
	chooser.setTitle(title);
	chooser.setFileSelectionMode(GhidraFileChooser.FILES_ONLY);
	return chooser.getSelectedFile();
}
 
Example 9
Source File: ImportPatternFileActionListener.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
	GhidraFileChooser fileChooser = new GhidraFileChooser(component);

	fileChooser.setFileSelectionMode(GhidraFileChooser.FILES_ONLY);
	fileChooser.setTitle("Select Pattern File");
	String baseDir = Preferences.getProperty(XML_IMPORT_DIR_PROPERTY);
	if (baseDir != null) {
		fileChooser.setCurrentDirectory(new File(baseDir));
	}
	ExtensionFileFilter xmlFilter = new ExtensionFileFilter("xml", "XML Files");
	fileChooser.setFileFilter(xmlFilter);
	File patternFile = fileChooser.getSelectedFile();
	if (fileChooser.wasCancelled() || patternFile == null) {
		return;
	}
	Preferences.setProperty(XML_IMPORT_DIR_PROPERTY,
		fileChooser.getCurrentDirectory().getAbsolutePath());
	Preferences.store();
	//only clear the patterns if new patterns are loaded from a file
	ResourceFile resource = new ResourceFile(patternFile);
	try {
		PatternPairSet pairSet = ClipboardPanel.parsePatternPairSet(resource);
		if (pairSet == null) {
			return;
		}
		plugin.clearPatterns();
		for (DittedBitSequence pre : pairSet.getPreSequences()) {
			PatternInfoRowObject preRow = new PatternInfoRowObject(PatternType.PRE, pre, null);
			plugin.addPattern(preRow);
		}
		//post patterns can have alignment and context register restrictions
		processPostPatterns(pairSet);
	}
	catch (IOException | SAXException e1) {
		Msg.showError(this, component, "Import Error",
			"Error Importing file " + patternFile.getAbsolutePath(), e1);
	}
	plugin.updateClipboard();
}
 
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: ExtensionTableProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an action to allow users to manually add new extensions.
 * 
 * @param panel The extensions table panel.
 */
private void createAddAction(ExtensionTablePanel panel) {
	Icon addIcon = ResourceManager.loadImage("images/Plus.png");
	DockingAction addAction = new DockingAction("ExtensionTools", "AddExtension") {

		@Override
		public void actionPerformed(ActionContext context) {

			// Don't let the user attempt to install anything if they don't have write
			// permissions on the installation dir.
			ResourceFile installDir =
				Application.getApplicationLayout().getExtensionInstallationDir();
			if (!installDir.canWrite()) {
				Msg.showError(this, null, "Permissions Error",
					"Cannot install/uninstall extensions: Invalid write permissions on installation directory.\n" +
						"See the \"Ghidra Extension Notes\" section of the Ghidra Installation Guide for more information.");
				return;
			}

			GhidraFileChooser chooser = new GhidraFileChooser(getComponent());
			chooser.setFileSelectionMode(GhidraFileChooserMode.FILES_AND_DIRECTORIES);
			chooser.setTitle("Select extension");
			chooser.addFileFilter(new ExtensionFileFilter());

			List<File> files = chooser.getSelectedFiles();
			for (File file : files) {
				try {
					if (!ExtensionUtils.isExtension(new ResourceFile(file))) {
						Msg.showError(this, null, "Installation Error", "Selected file: [" +
							file.getName() + "] is not a valid Ghidra Extension");
						continue;
					}
				}
				catch (ExtensionException e1) {
					Msg.showError(this, null, "Installation Error", "Error determining if [" +
						file.getName() + "] is a valid Ghidra Extension", e1);
					continue;
				}

				if (!hasCorrectVersion(file)) {
					Msg.showError(this, null, "Installation Error", "Extension version for [" +
						file.getName() + "] is incompatible with Ghidra.");
					continue;
				}

				try {
					if (ExtensionUtils.install(new ResourceFile(file))) {
						panel.refreshTable();
						requireRestart = true;
					}
				}
				catch (Exception e) {
					Msg.error(null, "Problem installing extension [" + file.getName() + "]", e);
				}
			}
		}
	};

	String group = "extensionTools";
	addAction.setMenuBarData(new MenuData(new String[] { "Add Extension" }, addIcon, group));
	addAction.setToolBarData(new ToolBarData(addIcon, group));
	addAction.setHelpLocation(new HelpLocation(GenericHelpTopics.FRONT_END, "ExtensionTools"));
	addAction.setDescription(
		SystemUtilities.isInDevelopmentMode() ? "Add Extension (disabled in development mode)"
				: "Add extension");
	addAction.setEnabled(
		!SystemUtilities.isInDevelopmentMode() && !Application.inSingleJarMode());
	addAction(addAction);
}
 
Example 12
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];
	}
 
Example 13
Source File: ExportPatternFileActionListener.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {

	List<PatternInfoRowObject> selected = clipboardPanel.getLastSelectedObjects();
	if (selected.isEmpty()) {
		return;
	}
	//scan through all of them: must be at least one pre-pattern and 
	//at least one post-pattern
	boolean containsPostPattern = false;
	boolean containsPrePattern = false;
	for (PatternInfoRowObject row : selected) {
		if (row.getPatternType().equals(PatternType.FIRST)) {
			containsPostPattern = true;
		}
		if (row.getPatternType().equals(PatternType.PRE)) {
			containsPrePattern = true;
		}
	}
	if (!containsPostPattern) {
		Msg.showWarn(this, component, "No Post Pattern",
			"Selected patterns must contain at least one post pattern");
		return;
	}
	if (!containsPrePattern) {
		Msg.showWarn(this, component, "No Pre Pattern",
			"Selected patterns must contain at least one pre pattern");
		return;
	}

	boolean proceed = checkConsistencyForExport(selected);
	if (!proceed) {
		return;
	}
	GhidraFileChooser gFileChooser = new GhidraFileChooser(component);
	gFileChooser.setFileSelectionMode(GhidraFileChooser.FILES_ONLY);
	ExtensionFileFilter xmlFilter = new ExtensionFileFilter("xml", "XML Files");
	gFileChooser.setFileFilter(xmlFilter);
	String baseDir = Preferences.getProperty(XML_EXPORT_DIR_PROPERTY);
	if (baseDir != null) {
		gFileChooser.setCurrentDirectory(new File(baseDir));
	}
	gFileChooser.setTitle("Select Export File");
	File outFile = gFileChooser.getSelectedFile();
	if (gFileChooser.wasCancelled() || outFile == null) {
		return;
	}
	Preferences.setProperty(XML_EXPORT_DIR_PROPERTY,
		gFileChooser.getCurrentDirectory().getAbsolutePath());
	Preferences.store();
	BitsInputDialogComponentProvider bitsProvider =
		new BitsInputDialogComponentProvider(BITS_PROVIDER_MESSAGE);
	if (bitsProvider.isCanceled()) {
		return;
	}
	int totalBits = bitsProvider.getTotalBits();
	int postBits = bitsProvider.getPostBits();
	try {
		PatternInfoRowObject.exportXMLFile(selected, outFile, postBits, totalBits);
	}
	catch (IOException e1) {
		Msg.showError(this, component, "IO Error", "IO error exporting pattern xml file", e1);
		e1.printStackTrace();
	}
}