Issue #27444: permit users to rename file extensions. diff --git a/openide.loaders/src/org/openide/loaders/DataNode.java b/openide.loaders/src/org/openide/loaders/DataNode.java --- a/openide.loaders/src/org/openide/loaders/DataNode.java +++ b/openide.loaders/src/org/openide/loaders/DataNode.java @@ -83,8 +83,8 @@ /** Create a data node for a given data object. * The provided children object will be used to hold all child nodes. - * The name is always set to the base name of the primary file; - * the display name may instead be set to the base name with extension. + * The name is always the {@linkplain FileObject#getNameExt full name} of the primary file; + * the default display name is the same (unless {@link #setShowFileExtensions} has been turned off). * @param obj object to work with * @param ch children container for the node * @param lookup the lookup to provide content of {@link #getLookup} @@ -105,7 +105,7 @@ obj.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange (propL, obj)); - super.setName (obj.getName ()); + super.setName(obj.getPrimaryFile().getNameExt()); updateDisplayName (); } @@ -114,30 +114,7 @@ String newDisplayName; if (prim.isRoot()) { - // Special case - getName{,Ext} will just return "". - // Used to be handled by org.netbeans.core.RootFolderNode - // but might as well do it here. - // XXX replace with #37549 - File f = FileUtil.toFile(prim); - if (f == null) { - // Check for a JAR root explicitly. - FileObject archiveFile = FileUtil.getArchiveFile(prim); - if (archiveFile != null) { - f = FileUtil.toFile(archiveFile); - } - } - if (f != null) { - // E.g. /tmp/foo or /tmp/foo.jar - newDisplayName = f.getAbsolutePath(); - } else { - try { - // E.g. http://webdavhost.nowhere.net/mystuff/ - newDisplayName = prim.getURL().toExternalForm(); - } catch (FileStateInvalidException e) { - // Should not happen in practice. - newDisplayName = "???"; // NOI18N - } - } + newDisplayName = FileUtil.getFileDisplayName(prim); } else if (showFileExtensions || obj instanceof DataFolder || obj instanceof DefaultDataObject) { newDisplayName = prim.getNameExt(); } else { @@ -158,17 +135,37 @@ } /** Changes the name of the node and may also rename the data object. - * If the object is renamed and file extensions are to be shown, - * the display name is also updated accordingly. + * The display name is also updated accordingly. * - * @param name new name for the object + * @param name new name for the primary file (should include any extension) * @param rename rename the data object? * @exception IllegalArgumentException if the rename failed */ public void setName (String name, boolean rename) { try { if (rename) { - obj.rename (name); + FileObject prim = obj.getPrimaryFile(); + String ext = prim.getExt(); + if (ext.length() == 0) { + obj.rename(name); + } else if (name.endsWith("." + ext)) { + obj.rename(name.substring(0, name.length() - 1 - ext.length())); + } else { + if (obj.isModified()) { + // Cannot preserve any modified document in such a case. + // XXX add a localized message + throw new IllegalArgumentException(); + } + int dot = name.lastIndexOf('.'); + String newBase = dot == -1 ? name : name.substring(0, dot); + String newExt = dot == -1 ? "" : name.substring(dot + 1); + FileLock lock = prim.lock(); + try { + obj.getPrimaryFile().rename(lock, newBase, newExt); + } finally { + lock.releaseLock(); + } + } } super.setName (name); @@ -705,7 +702,7 @@ } if (DataObject.PROP_NAME.equals(ev.getPropertyName())) { - DataNode.super.setName(obj.getName()); + DataNode.super.setName(obj.getPrimaryFile().getNameExt()); updateDisplayName(); } if (DataObject.PROP_COOKIE.equals(ev.getPropertyName())) {