I am developing an Eclipse plug-in that programmatically modifies C++ files from the workspace. I am now trying to save the changes made. I have taken a look at this solution : How can I call save method in eclipse plugin development programmatically but this includes having the files I want to save open in the editor. Since I have
5k+ files to modify and save, I cannot afford to open them all in an editor before saving.
While looking for a solution I found this topic on the official Eclipse forum : https://www.eclipse.org/forums/index.php/t/1070377/ . Unfortunately, no answer has been provided.
I have tried using :
PlatformUI.getWorkbench().getService(IHandlerService.class).executeCommand("org.eclipse.ui.file.saveAll", event)
or
IWorkspace.save(boolean, IProgressMonitor)
or
ITranslationUnit.save(IProgressMonitor, boolean)
or
ICProject.save(IProgressMonitor, boolean)
but none of these solution worked.
My question is therefore :
How to save files programmatically in Eclipse without using an editor ?
Thanks a lot in advance
EDIT :
example of what I'm trying to achieve
deleteAndSave(IFunction functionToDelete) {
boolean forceDeletion = true;
IProgressMonitor progressMonitor = new NullProgressMonitor();
//delete the portion of code
functionToDelete.delete(forceDeletion, progressMonitor);
//we get the file containing the function
IFile file = (IFile) functionToDelete.getUnderlyingResource();
//save the file to the disk (ideally 'file.save()' ?)
file.getWorkspace().save(true, progressMonitor);
}
At the end of this execution, I would expect the new version of the file to be saved on the disk. The function is correctly deleted and the modifications appear in the IDE if I open the file in the editor, but the file is marked as unsaved by Eclipse (showing a star before the file name).
EDIT :
While this doesn't answer the question (without using an editor), I got the correct behaviour with :
deleteAndSave(IFunction functionToDelete) {
IProgressMonitor progressMonitor = new NullProgressMonitor();
IWorkbenchPage activePage = PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage();
functionToDelete.delete(true, progressMonitor);
IFile file = (IFile) functionToDelete.getUnderlyingResource();
IEditorPart editorPart = IDE.openEditor(activePage, file);
editorPart.doSave(progressMonitor);
activePage.closeEditor(editorPart, false);
}
However this opens an editor page for each file and closes it immediately, so the performance are not satisfying for a large volume of files.
It sounds like the problem might be that your operation is modifying the working copy of a file, and not the underlying file itself.
Changes to the working copy can be synced to the underlying file by calling IWorkingCopy.commit().
Does the following help?
...
functionToDelete.delete(forceDeletion, progressMonitor);
ITranslationUnit tu = functionToDelete.getTranslationUnit();
if (tu.isWorkingCopy()) {
boolean forceCommit = true;
((IWorkingCopy) tu).commit(forceCommit, progressMonitor);
}
Related
I'm doing an internal Project, in Java, where i have to read an Excel and Parse it into a mpp respectively a MS Project compatible .xml file. I'm creating the file and everythings working fine.
But i want to show the columns "Work" and "ID" by default.
I can show them in MS Projects and the values are as I expect them to be, but i have to select and show them. Is there a possibility to show them by default when you open the .xml file?
I tried a lot and searched:
https://www.mpxj.org/
https://www.mpxj.org/apidocs/overview-summary.html
https://github.com/tmyroadctfig/mpxj/tree/master/net/sf/mpxj
adding columns to header in mpxj using ProjectWriter
as well as StackOverflow. But i didn't found any Information helping me out or it wasn't helpful cause methods changed and i didn't found the equivalent new ones.
this is where i fill the tasks and resources of my mpp.
if (!extractedRow.getElement().isEmpty())
{
element = contract.addTask();
element.setName(extractedRow.getElement());
element.setStart(startingDate);
element.setOutlineLevel(LookUp.Mpp_Conversion_Element_OutlineLevel());
element.setID(id++);
}
else if (!extractedRow.getWorkpackage().isEmpty())
{
workpackage = Objects.requireNonNull(element).addTask();
workpackage.setName(extractedRow.getWorkpackage());
workpackage.setOutlineLevel(LookUp.Mpp_Conversion_Workpackage_OutlineLevel());
workpackage.setID(id++);
}
else if (!extractedRow.getTask().isEmpty())
{
task = Objects.requireNonNull(workpackage).addTask();
task.setName(extractedRow.getTask());
task.setType(TaskType.FIXED_WORK);
task.setOutlineLevel(LookUp.Mpp_Conversion_Task_OutlineLevel());
task.setWork(Duration.getInstance(extractedRow.getEstimatedTime(), TimeUnit.HOURS));
task.setDuration(Duration.getInstance(extractedRow.getEstimatedTime() / 8, TimeUnit.DAYS));
task.setRemainingWork(Duration.getInstance(extractedRow.getEstimatedTime(), TimeUnit.HOURS));
task.setID(id++);
if (!extractedRow.getRole().isEmpty())
{
for (Resource resource : _project.getResources())
{
if (resource.getName().equals(_filereader.get_mapper().getMapping(extractedRow.getRole())))
{
assn = Objects.requireNonNull(task).addResourceAssignment(resource);
assn.setStart(task.getStart());
assn.setWork(Duration.getInstance(extractedRow.getEstimatedTime(), TimeUnit.HOURS));
}
}
}
}
Kind Regards
Unfortunately using an MPP file is the only way for you to specify the visual appearance of a schedule automatically when it is opened by Microsoft Project.
You have a few options:
I believe Aspose.Tasks can generate MPP files, so it may be that as part of that you can configure the layout you want
As you are already working with Excel you may want to consider using VBA which would allow you to both extract the data you need from Excel, populate the schedule in MS Project and configure the layout of the schedule.
Finally, if you want to use MPXJ, when you generate the MSPDI file and import it into Microsoft Project you are given some options about how the file is opened (as a new schedule, append to an existing schedule, update an existing schedule). If you create an empty "template" MPP file with the visual layout you require, using the append or update options when you open the XML file and selecting the template file as the target should give you a finished file using the data you've generated with the visual layout you need.
I'm developing an Eclipse plugin in which I'm trying to get the text from the current open editor, I can do that using this code:
IFile file = ((FileEditorInput) editorInput).getFile();
InputStream inputStream = file.getContents();
String result = IOUtils.toString(inputStream);
But looks like it only returns the state of the file when it is saved, if I have that file open in the editor and make some changes I want to get this latest state, not the one when it was saved, is there any API I can use for that?
I have developed an ExcelReader/Writer program that is intended to be ran on other computers. In this program, it takes in an excel.xls of hard data, reads it and writes it in a formatted way to an excel.xlsx file, and then saves it in a directory "CoC-Forms".
To reduce the steps to run my program, I have included the empty .xlsx file that it writes to in the project. However, when exporting it to an executable jar (to run on other computers), I seem to be having trouble accessing this empty form. I know this is probably a very simple answer, but I have been stuck for over a full work day and it has completely halted my progress.
Here is a snippet of my environment. On the left is my Project directory in Intellij (CoC.xlsx is the empty form) and the highlights on the right are where I am attempting to access the file and where the error is happening respectively.
JOptionPane.showMessageDialog(null,"About to look for CoC");
//fileFrom = new File(s + "/out/production/XML Reader/CoC.xlsx");
//fileFrom = new File("/XML Reader/out/production/XML Reader/CoC.xlsx");
//fileFrom = new File("CoC.xlsx");
//fileFrom = new File(ExcelWriter.class.getResource("CoC.xlsx").getPath());
CodeSource src = ExcelWriter.class.getProtectionDomain().getCodeSource();
/*if (src != null) {
URL url = new URL(src.getLocation(), "CoC.xlsx");
fileFrom = new File()
System.out.println(url);
} else {
JOptionPane.showMessageDialog(null,"Failed");
} */
fileFrom = new File(new File("."), "CoC.xlsx");
I solved my problem by having people download the whole project folder, file referencing with a ../../ from point of execution, and then going deeper into the file path to find the location at which I placed the file. Choppy, but it works and solved the problem.
I'm working on a program in which I want to add the possibility of copy-pasting (or cut-pasting) files. I could create it so it only works within the program, but it would be nicer if I could use the system-wide clipboard. That has one huge problem though: when pasting I don't know if files are copied or cut from the system explorer, I only get the file locations.
I am using Java and the javafx clipboard. Some sample code:
Clipboard clipboard = Clipboard.getSystemClipboard();
List<File> files = clipboard.getFiles();
// destDir is a File, the target directory.
for (File oldFile : files) {
if (oldFile.isDirectory()) {
FileUtils.copyDirectoryToDirectory(oldFile, destDir);
} else {
FileUtils.copyFileToDirectory(oldFile, destDir);
}
}
Here I simply copy the files, but how do I for example know when to use FileUtils.copyDirectoryToDirectory and when to use FileUtils.moveDirectoryToDirectory (aka copy or cut)?
Thanks,
Luca
Turns out, as pointed out by Fildor , that this is only possible when using drag and drop with the Dragboard. The Clipboard does not have such functionality.
I am supposed to read the currently open DSL File to create an Auto Populate Feature. For that I am supposed to find the location of the file programmatically and then read it. However there seems to be no alternatives for this purpose. If I use Eclipse Plugin Methods, I get the following error java.lang.ClassCastException: org.eclipse.xtext.xbase.ui.editor.XbaseEditor cannot be cast to org.eclipse.core.resources.IFile
Please Help
The standard way to determine the file that any editor is editing is:
IEditorPart editor = get the editor
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput)editorInput).getFile();
// TODO handle file
}