This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
I need a notification when a project file has been created. I need to process a file after it has been created (File->New) but having trouble figuring out when it happens. I need to know the full path to file and what project it was created in. The post-creation process will add the newly created file to a logical folder among other things.
http://www.netbeans.org/download/dev/javadoc/org-openide-loaders/org/openide/loaders/DataLoaderPool.html#addOperationListener(org.openide.loaders.OperationListener)
It suspect this is too general. I need to know 1) that the file was created from File -> New (that's how you create new project files), 2) the project name, 3) the full path to file. Adding a listener to the dataloader pool will give me an notification when any new file file has been created. I also need the project.
The project can easily be found using FileOwnerQuery.
Perhaps in a Java project, but not in general and that's why I need the notification (or another way to map the file to a project). A file doesn't belong to a project just because it gets created under a project or exist under a project. I need to know that the file has been created from the File->New dialog and not from any another process. The file goes into the project only if the file has been created from the File -> New dialog. And you can still create a file in let's say /tmp from File -> New and want it to belong to the project you specify in the dialog. There is no way any project can claim ownership of that file, right? How would you know what project you specified in the dialog.
"A file doesn't belong to a project just because it gets created under a project or exist under a project." - yes it does. "I need to know that the file has been created from the File->New dialog and not from any another process." - well that would require a new API. But I do not think we would plan to implement such an API, since most files *are* created via File -> New and there is little reason to believe that a realistic app would want to behave specially when presented with a new project file created by some other means (like what?). That is probably WONTFIX unless there is a compelling use case given. "And you can still create a file in let's say /tmp from File -> New and want it to belong to the project you specify in the dialog. There is no way any project can claim ownership of that file, right?" - wrong. The New File dialog only lets you create files inside the selected project.
If I create a new file from File->New, I understand how I can get notified and how I can find the project and add it to the correct logical folder in. In our type of projects a file gets compiled only if it has been *added* to the project, and not just because it exists under a project. The part I don't understand is what if a file is being created under a project by another process? Version control systems, for instance, creates additional files and some version control systems creates even files with same suffixes as original files. I will get notifications about these files too, right? And how would I know which files to add to my project and which files not to add if I only listen to data loader pool notifications? And our IDE allows the user to create files under a project that a not part of (should not be compiled) the project. How to handle these files if I only listened to the data loader pool notifications? This user model is very close to Visual Studio's model. A notification from the File->New dialog would solve all these problems for me.
I think your needs are sufficiently specialized (or more precisely, unlike the user model used by the NetBeans IDE and its direct derivatives) that you should just be using your own New File dialog and not try to use the one provided by projectui - hence WONTFIX, but will leave to Honza for further evaluation.
Rewriting the File Dialog ????? Wouldn't notifications be generally usable? And a very light-weight change too? And would give me and others the chance to post-process files created from File -> New. I'm not suggesting to fix this is 5.0 but in a future release.
The New File dialog is not all that complicated. Consists of a couple of classes. (Some weird wizard code in there which I don't like much, but easy enough to copy.) The panels after the first are all plugged in by modules. Modifying the infrastructure (first panel + Finish handler) would enable you to not only perform custom actions when making new files, like registering them somewhere, but also customize other behaviors which might not be quite right for your uses, such as the expansion and selection of newly created files.
I have a few questions/comments: 1. I suppose you want to hook into templates over which you do not have any control (like Java module's templates). Otherwise you should probably modify the templates themselves. 2. The FileOwnerQuery works even for files/folders that are not under the project folder. Please see FileOwnerQuery.markExternalOwner for details. Some features may be broken if the FileOwnerQuery does not provide correct results. It can also cause hard to find bugs. 3. Not sure if such a change would be light-weight and easily maintainable. Moreover, I am currently not aware about any usecase, except yours, so I do not think it is "generally usable". I am a bit worried about abusing such an API. I think that "post-processing templates" approach is quite dangerous, and should be avoided if possible. I agree with Jesse that creating a new New File dialog may be the most appropriate approach in your case.
Re. #1 - I doubt the original template code needs to be touched; a wizard should return a set of files it has made (this is up to the wizard to implement correctly), which the existing New File wizard infrastructure pays attention to (in order to select them). Re. #3 - I don't think Thomas is talking about post-processing templates as such; just somehow adding newly created files to some kind of list. One advantage of a custom New File wizard is that you could actually add a final panel to every template which enumerates files that are about to be created and lets you confirm (checkboxes) that you want to "add them to your project" (a concept which does not exist in the NB IDE).
Re. #3 - I don't think Thomas is talking about post-processing templates as such; just somehow adding newly created files to some kind of list. Exactly. I want to move newly created files (from New-File only) into specific logical folders. If 'rewriting' the New File dialog is not that complicated, it may be the solution for us. But I'm hesitent to duplicate code if it can be avoided and I thought notifications would be generally useful.
I can't think of any use case for New File notification in the NB IDE or its direct derivatives. We do have code which uses OperationListener to check for new files - for the code completion / refactoring database - but for this case it is correct to get the same notification when a file is created by any means, incl. a CVS update that gets new files from a team member. In NetBeans, a project is one folder (possibly with one or two external folders added) and any file beneath that is part of the project, regardless of how it got there. It seems to me that the special-cased treatment of files added via the wizard would only apply to IDEs which adopt a different concept of projects, where a project is empty by default and its contents are enumerated, as in VS or JBuilder. Since you have a pretty different model, I would not expect to be able to share every bit of code or use all of the projects/projectui APIs; there may be some sharing that makes sense but expect to implement some other things yourself where your UI would naturally diverge.
Sorry I did not make myself clear: 1. IMO, if I would like to do something special when I instantiate my own template, I should do it in the instatiate method. An API like this would be necessary only to do something special for someone else's templates. 3. Yes, I understood this. Anyway, I agree with Jesse that this is a different model that should be solved differently.
After an offline discussion and thinking about it more, I think we should accept this as a requirement. Main reason is that reimplementing New File wizard is not a good option as interoperability between projects described here and NB Java projects is desirable. However, exact requirements needs to determined yet.
Reassigning to Milos.
in offline discusson jlahoda claims that he wrote some code for c support that uses OperationListener and works for the usecases described here. Can you confirm?
We are using Jan Lahoda's OperationListener hack (he actually wrote the code for us), but it does only work for files created by wizards written in a certain way. It does work for most of the files we are interested in but not for xml file, for instance. My point is we cannot keep relying on hacks to get our modules to work. We need good solid API's that will work in future releases.
Can you be more specific about which wizards cause OperationListener to not be invoked? IMHO that would be a bug in the wizard. OperationListener is the recommended and supported way to be notified when a file is created in general.
Still missing details.
I see. If OperationListener is the recommended and supported way to get notifications, we can live with that. We are having problems with the XML Document wizard.
Perhaps some wizard uses the Filesystems API (not through Datasystems) to create the new files and you are listening only to operationCreateFromTemplate, not operationPostCreate?
the xml support is indeed using filesystem apis to create the xml document from template. why is that? reassigning for evaluation/fix.
Various complex New File wizards may of course use FS API only to create files; e.g. apisupport does this. I don't think it's a bug in the wizard; going through DS API to create files from template is rarely appropriate for custom wizards. Rather, the user of OperationListener needs to listen to both operationCreateFromTemplate and operationPostCreate in order to assure complete coverage. So I suggest this should be moved back to 'projects'. The options for reporter are: 1. Use OL and accept files which are newly created for any reason. For the NB IDE usage model this is the correct option, but other distinct applications may have different usage models. 2. Write a custom New File wizard to report on the result of InstantiatingIterator.instantiate() (and hope that this is always accurate). 3. Wait for some API from the existing New File wizard to provide the same information as #2 to an external listener.
reassigning back then
1) I believe we already do 1) and we know it doesn't work with files created with XML wizard. 2) Is unacceptable and may not work when we have all the Java modules enabled. I think we will just wait for 3). Please don't make it a p4 though.....
"I believe we already do 1) ...." - I think you do not. I wrote "for _any_ reason", i.e. also operationPostCreate. AFAIK #2 would work whether or not Java modules are enabled, but of course I have not tried it.
We don't handle operationPostCreate. I will try...
I tried to listen to operationPostCreate but it doesn't work well. I get literally hundreds of calls to operationPostCreate for just about anything I do in the IDE that involves files like opening a project or just opening the New File wizard (no files created). Yes, it also gets called when I create a new XML file from the XML wizard, but I don't think there is an simple way to really know whether a new file has been created or not so I don't think this is the solution.
Change of default owner.
is there still something to be done? the thread is long and old..
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue. Thanks for your cooperation, NetBeans IDE 8.2 Release Boss