Filter file system file selection dialog by ContentTypeId - eclipse

In the Eclipse plugins we're developing, we've defined a custom content type, using the org.eclipse.core.contenttype.contentTypes extension point. We're successfully using the content type to enable or disable UI components, based on, e.g., whether the user is editing a file of that type.
I'd like to take this ones step further and also use it to filter files in a file selection dialog, such that it only shows files that match the content type.
I have found that filtering a JFace Viewer this way is possible, so for files in the workspace, we could use an ElementTreeSelectionDialog and add a ViewerFilter.
Is there a way to do the same for a file selection dialog of the entire file system (instead of filtering by file extension)? Or is this impossible because it's restricted to the OS's filtering?

The standard SWT FileDialog can only be filtered by file extension and cannot be extended.
You could write your own file selection dialog using the normal Java File or Path APIs with a tree viewer and a viewer filter.
Because the files are outside of the workspace you can't use any of the IResource, IFile, IFolder APIs. However you can still use the IContentTypeManager interface which gives you access to your content types.
IContentTypeManager manager = Platform.getContentTypeManager();
If the file extension is enough to distinguish the files you can just use:
IContentType contentType = manager.findContentTypeFor("the file name");
If you need to use the content describers use:
InputStream stream = ... new FileInputStream(....
IContentType contentType = manager.findContentTypeFor(stream, "the file name");
stream.close();

Related

Can OpenXML be used to launch a new Word instance?

I'm able to generate Word documents without issue. I save the resulting *.docx file to a temporary location and then need to launch the file in Word.
The requirement is to not "open" the file in Word (easily done with a Process.Start) but to have load into Word as a new unsaved file. This is because certain propriety integrations for Word need to take over when a user saves the file and don't kick in if the file is ready saved but to a location on disk.
I've achieved this by using Interop calls to the Word application, adding the new document to Word's workspace. My problem is with Interop which tends to break on various client machines, particularly when Office upgrades take place (say a client had 32-bit office but upgraded with a 64-bit version).
I'm somewhat new to OpenXML, but can it be used to automate Word or is Interop my only real option?
object oFilename = tmpFileName;
object oNewTemplate = false;
object oDocumentType = 0;
object oVisible = true;
Document document = _application.Documents.Add(ref oFilename, ref oNewTemplate, ref oDocumentType, ref oVisible);
No, the Open XML technology has no way of interacting with the Office (Word) application - it's for file creation/manipulation, only. The interop is required in order to do anything with the Word application.
There is sort of a way around this - and it's only possible with Word, no other Office application has this - is to convert the Open XML content to the OPC flat-file format. This "concatenates" the various packages that make up the zip file to a pure text string, essetially a single XML file.
XML content in the OPC flat-file format can then be written to an already opened (even newly created) Word document using the Range.InsertXML method via "the interop". In a way, this "streams" the Open XML content into the opened Word document.
The problem with this approach is that certain document-level properties are not written to the target document, so not all aspects of the opened document can be changed. For example: page size, orientation, headers, footers... So if this kind of thing also needs to be affected the interop is required for such settings.

How to add a new file association from a plugin

In a project I have some internal files that are just JSON files but with specific well known filenames. I would like to add file associations for these files from my visual studio code plugin so they automatically get recognized as JSON and highlighted accordingly but cannot see a way to do this from the plugin. Does anyone know a way to do this?
It turns out you can just add the necessary associations to the workspace settings
// add file associations for our configurations files
let conf = vscode.workspace.getConfiguration('files');
let assocs = conf.get<{}>('associations');
assocs['special.info'] = 'json';
conf.update('associations', assocs, ConfigurationTarget.Workspace);

How to start Microsoft Word from Java FX with parameters to run a macro

I have a document management system which stores files in a MS Word format. In my application, I would like to be able to open that document in Word.
I would like Word to handle all of the file system access out of the content management system. What I need to do is the following:
1) Create a new document based off a template, and then provide information that can be parsed and placed into specific fields.
I see I can do this as follows:
Runtime.getRuntime().exec("C:/Program Files (x86)/Microsoft Office/Office15/winword.exe /ttemplate_name");
My assumption here is that the template is installed on the local drive. However I would like to provide some data so that fields could be prepopulated and I am not sure how to do that?
2) I would like to be able to run a macro to open the document directly from the content management system. I think I can run a macro as follows:
Runtime.getRuntime().exec("C:/Program Files (x86)/Microsoft Office/Office15/winword.exe /mmacro_name");
However, in this case, I would need to provide the document id from the content management system so that it can retrieve it and open it.
I am unsure what switch or parameter I can use to provide the additional data for word?
Thanks!
Word provides no command-line facility to pass arguments or data when opening or creating a document.
As long as macro code is available, the macro can read data that's stored somewhere, such as in an XML file. But the file path would need to be hard-coded or derivable from a known location (path).
You don't necessarily need to call a macro in a document (or template attached to the document). If the macro is named AutoNew or AutoOpen it will execute automatically when a document is created from the template or, respectively, when a document is opened.

How to specify and read properties in an Eclipse plugin

I have an Eclipse product which uses my own plugins. I want to read some properties based on user inputs. I want to persist these properties on some user action, and read those properties back when required. Can this be achieved using some Eclipse API?
A more elaborate description of the above problem:
Say I have a property abc=xyz in a config file myconfig.ini. This property is read by the perspective during the bootstrapping process. During use of the perspective, some action sets this property to a new value xyz=def. Now, I should be able to save the new value in myconfig.ini. So next time the bootstrapping happens, the value of xyz is read as def instead of abc. However, I can also choose to manually set it to abc by editing the myconfig.ini file.
How would I manage myconfig.ini? Where should it exist within my eclipse product project?
What is the best API to manage reading, writing and updating properties in myconfig.ini?
You can use resource markers mechanism:
IMarker marker = file.createMarker(IMarker.MARKER);
marker.setAttribute(IMarker.MESSAGE, "blabla");
marker.setAttribute("attr", 5);
You can search for markers by using the findMarkers methods on IResource.
See FAQ also
You should consider using the apache configurations API http://commons.apache.org/proper/commons-configuration/
It can read and write INI files and if you want to change the configuration file type or add more configuration options you can simply configure it.
I would add a hidden directory to the workspace root e.g. ${WORKSPACE}/.productName/product.ini
and add an ISaveParticipant that ensures the ini file gets updated on shutdown.
You can get the Workspace using the ResourcesPlugin
IWorkspace workspace = ResourcesPlugin.getWorkspace();
and resolve it to an absolute path
IWorkspaceRoot wsRoot = workspace.getRoot();
IPath wsPath = wsRoot.getRawLocation();
IPath absoluteWsPath = wsPath.makeAbsolute();

Open a file from crystal report export to stream function

How can i open a file directly from stream which is exported through crystal report export to stream function? I am using vs2010 and sap crystal report.
MemoryStream m = (MemoryStream)(PReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
FileStream f = new FileStream(purchaseCombo.SelectedItem.ToString(),FileMode.Create,System.IO.FileAccess.Write);
byte[] bytes = new byte[m.Length];
m.Read(bytes, 0, (int)m.Length);
f.Write(bytes, 0, bytes.Length);
f.Close();
m.Close();
It depends on the file that you are exporting. Since Crystal Reports can export a number of different file types and each of them has a specific program that can handle it, you will need to search for the exact file type that you're interested. For example with a quick search on the internet, I found that a stream of an html file can be directly previewed inside a WebBrowser component (in a WPF app) by using the NavigateToStream method. Maybe there are some 3rd party components that expose methods for directly opening files from memory.
Though, the easiest way would be to export a temporary file to disk instead of memory by using the ExportToDisk method and delete it after its usage, since many components read from paths rather than memory. Before exporting the file, you can use the GetTempFileName method which creates and names a temporary file.