Create file in Eclipse plugin without actually saving the file? - java

I would like to know if there is a way to create a file and not saving the file or going through any wizard screen. For instance, I would like to click a button, open a file with a default name, modify the file and optionally save the file into the hard drive.

In order to open an editor programmatically, you can use one of the static openEditor() methods provided by the IDE class, for example openEditor( IWorkbenchPage, IEditorInput, String ).
You are free to implement a custom IEditorInput or more useful in this case probably, the specialized IStorageEditorInput interface. Your implementation of getStorage() may return a storage that provides content that is independent of an (existing) file.
However, existing editors may or may not understand certain editor input types. Unfortunately, there is no way to determine upfront whether a certain editor can handle a certain input type.
If you are implementing your own editor (see IEditorPart and EditorPart) however, you can, of course, tailor the editor for that input type.
The answers to this question have pointers to building editors in Eclipse in general: Tutorial regarding the development of a custom Eclipse editor

Related

how to allow the user to manually edit a properties file in an android app?

I have an android app that uses a properties file that is currently stored on the sdcard.
I want to allow the user to manually edit this properties file.
Is there a preferred editor that people use or do you roll your own for your app?
Is there a preferred editor that people use
I am not aware of any Android device that ships with a text editor, let alone a dedicated properties file editor. I would be fairly surprised if there are any.
The Android SDK does not ship with a properties file editor, unless you count EditText. I am not aware of any libraries that implement a properties file editor.
do you roll your own for your app?
Most Android developers do not use properties files on-device. Instead, they use preferences, which come with their own UI (PreferenceScreen and kin) and backing store (SharedPreferences, plus PreferenceDataStore on Android O).
You would need to create your own UI for editing your properties. Or, depending upon your use case, you might implement a more traditional preference-based system, using the properties file for import/export.

Is writing into class resources a good way to save files?

When we want to load a static file e.g. a picture, a sound file, a file containing information about a game map,... we can store them as resources in jar file and use getClass.getResource("images/splash.png") (also getResourceAsStream) to load and use them. But when we want to read and write into a file like settings file, I don't think using resources is a good way, because i think resources are designed to store read/only files that are not supposed to change, like splash screen image or a game's background music; These are my reasons to think this way:
That is why return value of getResourceAsStream is an instance of InputStream and we don't have a similar function which gives us an OutputStream, because we're not supposed to alter resource files.
Writing into resources changes program .jar file and i guess it's not a good thing at all; Because if we do so: we can't use check-sums to verify file, if we are a limited user and system administrator doesn't give us write permission we can't make changes into main .jar file, user-specific preferences are hard or impossible to implement,...
So, my questions are:
Which parts of my thoughts and assumptions are right or wrong?
If they're right what is the best(I mean short and portable between OSs and Computers) way to store files like that? (Application setting/preferences, A game save file, ...)
(#Some user who may wants to mark this as duplicate: I don't think my question is a duplicate, i searched in the site, I admit it has some common parts with some questions but it's not duplicate!)
Your three observations in #2 above are valid reasons not to store settings in a resource file, regardless of the APIs provided.
There are a variety of ways to save settings in Java, including:
The Java system property "user.home" provides the user's home directory, to which the user should have write access. You can create an application-specific subdirectory underneath it.
Java provides a Preferences API. This may store settings in a directory or (on Windows) in the registry.
OSGI provides a preferences API.
If you're using the Eclipse RCP, you can write to the configuration directory using a ConfigurationScope. See the Eclipse FAQ "What is a preference scope").

How to read a file that is currently opened using Eclipse plugins?

I'm doing a project using Eclipse plugin to create an IDE. I have to read the file that is currently active for a particular string? How can I do this?
I came to know there is something called as InputStream plugin, but don't know what parameter to pass for it. Can anyone help me in doing this?
If you just want to read the file that's open in the active editor from a command handler, for example, you can do something like:
IEditorPart editor = HandlerUtil.getActiveEditor(event);
IFile file = editor.getAdapter(IFile.class);
You can then use the resources API to read the content of the file. If you are trying to get ahold of the text that is actually in the open editor, then it matters what kind of editor you are dealing with. Many things are based on Text Editors which have API to access the current file buffer.

How to write an editor that shows the content of a folder in eclipse?

Motivation
I have written an eclipse plugin that shows me a list of all files and folders with unreviewed content. When selecting a folder, I want an editor to open showing all files and subfolders that this folder contains. It has to work for versioned items, too. So I have to create the content of the editor within my plugin (no backing IResource).
What I currently have
Right now I'm opening a RemoteFileEditorInput for a versioned file (subclipse) or I'm using IWorkbenchPage.openEditor() for a FileEditorInput.
Question
What's an easy way to visualize dynamic content (directory listing) inside of an text editor?
EDIT 2010-04-13:
More Context
I want to be able to create some code review comments on a directory listing. For this I want to be able to open a diff viewer to see which elements were removed or added between two revisions of the directory.
I'd like to enter a comment for a directory the same way I do for a text file. That's why I try to display the directory content as a text file - I've already implemented the mechanisms for commenting a text file.
I call the content of the directory editor "dynamic", because it's not really backed by an existing directory in the filesystem. For older versions of a directory I will create the editor content within my program code.
I'm afraid I don't totally understand your question (and I lack the rep to comment on it).
Is this right?: You want to be able to open one of three editors in support of your goal:
An editor for unversioned files (you say you have this)
An editor for versioned files (you say you have this)
An editor for directories (this is what you need?)
What does the user do here? What does it look like?
I'm confused by "What's an easy way to visualize dynamic content (directory listing) inside of an text editor?"
Knowing that I probably completely miss your point, I would note that your editor can contain whatever controls you want it to (cf. some of the "forms" editors in PDE, like for editing a plug-in manifest) - you'd probably extend EditorPart, maybe create a ManagedForm in your createPartControl(), create a ListViewer, TableViewer, or TreeViewer in there, give it an appropriate content provider, and give it an appropriate label provider.
I'm also confused by "What's an easy way to visualize dynamic content (directory listing) inside of an text editor?"
I can only assume that you want to somehow poll or listen to some events and tell your viewer to update() or refresh().
Finally I would question whether the directory listing really belongs in an editor . . . most navigation happens in views. Will the user actually be performing edits to the directory somehow, and possibly "saving" them at some later time?
I hope this helps us all narrow down into whatever will help you.

Changing pop-ups dynamically in eclipse plugin

I have a plugin where for a particular type of file i will enable some options to perform. so now i have requirement where i need to decide the type of a file dynamically and apply a particular options for that type of file.
Is it possible to do that? if yes, need some help about the same.
Thanks.
So I guess you want to show the context menu for something that represents a file in a view. Determine the file type based on custom rules (file extension is the easiest rule, looking inside the file the most complex) and create the popup or distribute content to an existing one.
If it's your own view, maybe a list or tree that shows a folder structure, then you have to register the Viewer as a SelectionProvider, listen to selection events, evaluate the selection (maybe a String or a File object) and create the popup menu.
Contributing actions to existing popup menus (like the navigator view context menus) is possible as well but a bit more challenging.

Categories