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.

Bug 141886

Summary: Let user select alternate file extensions in simple target chooser
Product: projects Reporter: Tomas Mysik <tmysik>
Component: Generic InfrastructureAssignee: Jesse Glick <jglick>
Status: RESOLVED FIXED    
Severity: blocker CC: alexvsimon, apireviews, fzamboj, vv159170
Priority: P2 Keywords: API, API_REVIEW_FAST
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Bug Depends on: 132080, 184314    
Bug Blocks: 184295, 182958, 183045    
Attachments: Possible API to tell the simple target chooser to permit free file extensions
Revised patch which lets scripting handler know what you want to do

Description Tomas Mysik 2008-07-28 14:53:10 UTC
It would be great to have possibility to listen on particular field changes in "standard" new file wizard 
(Templates.createSimpleTargetChooser()). Because now, if one wants e.g. just add a warning if the name of the file is 
somehow incorrect, it is not possible. One would have to copy the whole panel, moreover not only copy the whole panel, 
but then fix and update as well, of course (to follow the functionality and look of the "standard" panel).
Thanks for evaluating.

BTW this issue comes from issue #127088.
Comment 1 Milan Kubec 2008-09-01 10:51:49 UTC
Changing issue type and increasing priority.
Comment 2 Antonin Nebuzelsky 2010-03-29 14:10:39 UTC
Change of default owner.
Comment 3 Tomas Mysik 2010-03-31 15:44:54 UTC
Unfortunately, this issue seems to be a stopper for issue #182958 (P2). I can't find a way how to get a file name before one clicks the Finish button.
Meanwhile, I will try to work around it.
Comment 4 Jesse Glick 2010-03-31 23:48:06 UTC
I don't see a clear API here that would expose the panel for customization without exposing a lot of its implementation. Would need to be a clearly defined list of use cases. Would also prefer for an API to request high-level changes in behavior, rather than adding low-level field listeners.

Anyway wouldn't #182958 be better solved in the standard implementation to begin with? I don't see what about this is specific to PHP.

(#127088 is perhaps specific to PHP, but I am unconvinced that the proposed solution is a good idea anyway. Maybe better to display a one-time-only warning dialog that certain filenames, on certain platforms, with certain browsers, may not be usable, and link to a FAQ.)
Comment 5 Tomas Mysik 2010-04-01 08:44:18 UTC
(In reply to comment #4)
> Anyway wouldn't #182958 be better solved in the standard implementation to
> begin with? I don't see what about this is specific to PHP.

Unfortunately, PHP is specific because we needed to have a possibility to specify a file extension as well (the usual extensions for PHP files are .php and .phtml) but this is not possible in the standard New File wizard (one specify only file name itself, the extension is added automatically). And having two same templates for in fact one file seemed to be a bad solution.

So the problem in issue #182958 is that user types "MyFile" and only "MyFile" is checked for existence but then (in instantiate() method), if we find out that it's not a valid PHP file, we add ".php" to the file name. The best way to me seems to have a possibility to listen on the Component and have access to its fields/values so I can create my own WizardPanel which would delegate to the standard one and would have an "improved" isValid() method. Maybe there's some simpler solution but I can't see it.

BTW I have work arounded the problem in issue #182958, P4 now.
Comment 6 Quality Engineering 2010-04-02 04:48:47 UTC
Integrated into 'main-golden', will be available in build *201004020200* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/3a6bcf959de3
User: Tomas Mysik <tmysik@netbeans.org>
Log: #182958: New File dialog overwrites existing file if...

For the correct fix, bug #141886 needs to be fixed.
Comment 7 Jesse Glick 2010-04-02 15:20:34 UTC
(In reply to comment #5)
> we needed to have a possibility to
> specify a file extension as well
> but this is not possible in the standard New File wizard (one
> specify only file name itself, the extension is added automatically).

Probably this is a common enough requirement, so there could simply be an option for the standard panel to either not append any extension, or to append the extension appropriate to the template unless you type in your own extension (detected simply by presence of '.' in the filename). Then the r/o field showing the generated file would be correct, and validation would be correct, and the PHP modules would need do nothing but set a flag.
Comment 8 Tomas Mysik 2010-04-05 15:36:21 UTC
(In reply to comment #7)
> Probably this is a common enough requirement, so there could simply be an
> option for the standard panel to either not append any extension, or to append
> the extension appropriate to the template unless you type in your own extension
> (detected simply by presence of '.' in the filename). Then the r/o field
> showing the generated file would be correct, and validation would be correct,
> and the PHP modules would need do nothing but set a flag.

That would be perfect.
Comment 9 Jesse Glick 2010-04-05 23:31:47 UTC
(In reply to comment #7)
> append the extension appropriate to the template
> unless you type in your own extension
> (detected simply by presence of '.' in the filename)

Making this the default behavior of the filechooser appears to be impossible. It's a long story, but the problem is with TemplateWizard.DefaultIterator.instantiate, since common impls of DataObject.createFromTemplate(DataFolder,String) always append the extension from the template - see ScriptingCreateFromTemplateHandler, FileEntry.createFromTemplate, and so on - and this probably cannot be compatibly changed.

TW.DI.instantiate could just create the target file itself in case it encountered a filename that looked like it included an extension, but then its behavior might be rather different from the normal case - for example, ScriptEngine processing would not be run. It is possible to use a fake template file with the same file attributes incl. that for ScriptEngine, but then FreeMarker substitution does not work correctly (due to the fake template's location), and it is likely that indentation and charset handling would not work correctly either.

So the alternative is to make this lax extension behavior be enabled upon request, assuming a custom wizard iterator which knows how to handle the special filename. I know how to do that, but it still won't work very well with the PHP wizard as currently written, since that suddenly changes the generated filename when you click Finish.

These hacks in the wizard iterator can be removed and the template file made to be a *.php, but then ".php" is appended to the generated filename even if it already had an extension. (Making the template be a DefaultDataObject does not help with this.) Of course NewFileWizardIterator.instantiate could bypass DataObject.createFromTemplate entirely and create the target file directly with the right name, but then it would need to duplicate functionality in ScriptingCreateFromTemplateHandler which you probably don't want.

The best I can come up with is to have the wizard not append any extension for you in the case of PHP files. This works fine, you just need to type in ".php" or ".phtml" or whatever when you make the file; otherwise you get a file not recognized as PHP. Probably it would be possible to display a warning to the user in the last panel in case the target file would not be recognized as a PHP file.
Comment 10 Jesse Glick 2010-04-05 23:35:30 UTC
Created attachment 96723 [details]
Possible API to tell the simple target chooser to permit free file extensions

Works but is not necessarily an improvement in the case of the PHP wizard.
Comment 11 Jesse Glick 2010-04-06 01:39:02 UTC
Created attachment 96725 [details]
Revised patch which lets scripting handler know what you want to do
Comment 12 Jesse Glick 2010-04-06 01:45:25 UTC
(In reply to comment #9)
> These hacks in the wizard iterator can be removed and the template file made to
> be a *.php, but then ".php" is appended to the generated filename even if it
> already had an extension. (Making the template be a DefaultDataObject does not
> help with this.) Of course NewFileWizardIterator.instantiate could bypass
> DataObject.createFromTemplate entirely and create the target file directly with
> the right name, but then it would need to duplicate functionality in
> ScriptingCreateFromTemplateHandler which you probably don't want.

Rectified using a new API to tell ScriptingCreateFromTemplateHandler to match the semantics of the wizard: fixes the file extension, and also the ${name} and ${nameAndExt} template substitutions.

If this approach is to your liking, I can change the summary of this issue and submit it for API review. That would count as a full solution of bug #182958 and bug #183045, and php.project need do nothing but call the new API.

(I still think bug #127088 should be addressed differently - say you got a multibyte filename from svn up?)
Comment 13 Tomas Mysik 2010-04-06 09:29:45 UTC
(In reply to comment #9)
> you just need to type in ".php"
> or ".phtml" or whatever when you make the file; otherwise you get a file not
> recognized as PHP. Probably it would be possible to display a warning to the
> user in the last panel in case the target file would not be recognized as a PHP
> file.

IMHO the warning would be needed because right now, users can type just "index" and "index.php" is created.

(In reply to comment #12)
> If this approach is to your liking, I can change the summary of this issue and
> submit it for API review. That would count as a full solution of bug #182958
> and bug #183045, and php.project need do nothing but call the new API.

Yes, I mean it's perfect for our needs so please, do an API review. Thanks.

> (I still think bug #127088 should be addressed differently - say you got a
> multibyte filename from svn up?)

Agreed, I will do that.
Comment 14 Jesse Glick 2010-04-06 14:23:54 UTC
(In reply to comment #13)
> right now, users can type just "index" and "index.php" is created.

Comment #9 referred to the first patch. The above behavior is still the case with the revised patch:

What You Type | What Created File Shows | What ${name} Expands To | ${nameAndExt}
--------------+-------------------------+-------------------------+--------------
index         | index.php               | index                   | index.php
index.php     | index.php               | index                   | index.php
index.phtml   | index.phtml             | index                   | index.phtml

(There is nothing stopping you from typing, say, "index.html", in which case a file will be created with PHP tags in it that the IDE and perhaps the web server do not recognize, but I guess that is the user's choice.)
Comment 15 Jesse Glick 2010-04-06 14:25:50 UTC
Please review the second attached patch, with API changes in Templates and CreateFromTemplateHandler making it easier to write a New File wizard which permits the user to enter a different file extension than the default for the template.
Comment 16 Jesse Glick 2010-04-13 16:21:01 UTC
core-main #fd412d15d125
Comment 17 Quality Engineering 2010-04-14 04:39:33 UTC
Integrated into 'main-golden', will be available in build *201004140201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/fd412d15d125
User: Jesse Glick <jglick@netbeans.org>
Log: Issue #141886: let user select alternate file extensions in simple target chooser.