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

The following examples show how to use docking.widgets.filechooser.GhidraFileChooser#setFileFilter() . 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: KeyBindingUtils.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static File getFileFromUser(File startingDir) {
	KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
	Component activeComponent = kfm.getActiveWindow();
	GhidraFileChooser fileChooser = new GhidraFileChooser(activeComponent);
	fileChooser.setTitle("Please Select A File");
	fileChooser.setFileFilter(FILE_FILTER);
	fileChooser.setApproveButtonText("OK");
	fileChooser.setCurrentDirectory(startingDir);

	File selectedFile = fileChooser.getSelectedFile();

	// make sure the file has the correct extension
	if ((selectedFile != null) &&
		!selectedFile.getName().endsWith(PREFERENCES_FILE_EXTENSION)) {
		selectedFile = new File(selectedFile.getAbsolutePath() + PREFERENCES_FILE_EXTENSION);
	}

	// save off the last location to which the user navigated so we can
	// return them to that spot if they user the dialog again.
	Preferences.setProperty(LAST_KEY_BINDING_EXPORT_DIRECTORY,
		fileChooser.getCurrentDirectory().getAbsolutePath());

	return selectedFile;
}
 
Example 2
Source File: DebugDecompilerAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected void decompilerActionPerformed(DecompilerActionContext context) {
	JComponent parentComponent = context.getDecompilerPanel();
	GhidraFileChooser fileChooser = new GhidraFileChooser(parentComponent);
	fileChooser.setTitle("Please Choose Output File");
	fileChooser.setFileFilter(new ExtensionFileFilter(new String[] { "xml" }, "XML Files"));
	File file = fileChooser.getSelectedFile();
	if (file == null) {
		return;
	}
	if (file.exists()) {
		if (OptionDialog.showYesNoDialog(parentComponent, "Overwrite Existing File?",
			"Do you want to overwrite the existing file?") == OptionDialog.OPTION_TWO) {
			return;
		}
	}
	controller.setStatusMessage("Dumping debug info to " + file.getAbsolutePath());
	controller.refreshDisplay(controller.getProgram(), controller.getLocation(), file);
}
 
Example 3
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 4
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 5
Source File: ToolServicesImpl.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private GhidraFileChooser getFileChooser() {
	GhidraFileChooser newFileChooser = new GhidraFileChooser(null);
	newFileChooser.setFileFilter(new GhidraFileFilter() {
		@Override
		public boolean accept(File file, GhidraFileChooserModel model) {
			if (file == null) {
				return false;
			}

			if (file.isDirectory()) {
				return true;
			}

			return file.getAbsolutePath().toLowerCase().endsWith("tool");
		}

		@Override
		public String getDescription() {
			return "Tools";
		}
	});

	String exportDir = Preferences.getProperty(Preferences.LAST_TOOL_EXPORT_DIRECTORY);
	if (exportDir != null) {
		newFileChooser.setCurrentDirectory(new File(exportDir));
	}

	newFileChooser.setTitle("Export Tool");
	newFileChooser.setApproveButtonText("Export");

	return newFileChooser;
}
 
Example 6
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 7
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 8
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 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: 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();
	}
}
 
Example 11
Source File: ExportToHeaderAction.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Writes the selection out to a file of the user's choosing.  One file will be written for
 * each DataTypeManager in the selection.
 * @param gTree The tree that contains the selected nodes.
 */
private void exportToC(GTree gTree, DataTypeManager programDataTypeMgr) {

	List<Class<? extends AnnotationHandler>> classes =
		ClassSearcher.getClasses(AnnotationHandler.class);

	List<AnnotationHandler> list = new ArrayList<>();
	Class<?>[] constructorArgumentTypes = {};
	for (Class<? extends AnnotationHandler> clazz : classes) {

		if (clazz == DefaultAnnotationHandler.class) {
			continue;
		}

		try {
			Constructor<?> constructor = clazz.getConstructor(constructorArgumentTypes);
			Object obj = constructor.newInstance();
			list.add(AnnotationHandler.class.cast(obj));
		}
		catch (Exception e) {
			Msg.showError(this, plugin.getTool().getToolFrame(), "Export Data Types",
				"Error creating " + clazz.getName() + "\n" + e.toString(), e);
		}
	}

	AnnotationHandler handler = null;
	if (!list.isEmpty()) {
		list.add(0, new DefaultAnnotationHandler());
		AnnotationHandlerDialog dlg = new AnnotationHandlerDialog(list);
		plugin.getTool().showDialog(dlg, (Component) null);
		if (!dlg.wasSuccessful()) {
			return;
		}
		handler = dlg.getHandler();
	}
	else {
		handler = new DefaultAnnotationHandler();
	}

	TreePath[] paths = gTree.getSelectionPaths();
	Map<DataTypeManager, List<DataType>> managersToDataTypesMap = new HashMap<>();

	for (TreePath path : paths) {
		addToManager(path, managersToDataTypesMap);
	}

	GhidraFileChooser fileChooser = new GhidraFileChooser(gTree);

	// filter the files if we can
	String[] fileExtensions = handler.getFileExtensions();
	if (fileExtensions.length > 0) {
		fileChooser.setFileFilter(
			new ExtensionFileFilter(fileExtensions, handler.getLanguageName() + " Files"));
	}

	Set<Entry<DataTypeManager, List<DataType>>> entrySet = managersToDataTypesMap.entrySet();
	for (Entry<DataTypeManager, List<DataType>> entry : entrySet) {
		DataTypeManager dataTypeManager = entry.getKey();
		File file = getFile(gTree, fileChooser, dataTypeManager, handler);
		if (file == null) {
			return; // user cancelled
		}

		List<DataType> dataTypeList = entry.getValue();
		new TaskLauncher(
			new DataTypeWriterTask(gTree, programDataTypeMgr, dataTypeList, handler, file),
			gTree);
	}
}