<commit_before>/**
 * Copyright 2012 markiewb
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.markiewb.netbeans.plugin.showpathintitle;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.*;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataShadow;
import org.openide.modules.ModuleInstall;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Shows the path of the current {@link DataObject} in the title of the main
 * window. Additionally it shows <ul> <li>the project name </li> <li>the
 * NetBeans version (only supported for NB &gt;=7.1)</li> </ul>
 *
 * @author markiewb
 */
public class Installer extends ModuleInstall {

    private static final long serialVersionUID = -2422104143131463778L;
    private PropertyChangeListener propertyChangeListener;

    public Installer() {
        propertyChangeListener = new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                TopComponent activeTC = TopComponent.getRegistry().getActivated();
                if (null == activeTC) {
                    return;
                }

                DataObject dataObject = activeTC.getLookup().lookup(DataObject.class);
                Project project = activeTC.getLookup().lookup(Project.class);
                Node node = activeTC.getLookup().lookup(Node.class);

                String projectName = null;
                String projectDir = null;
                String fileName = null;
                if (null != dataObject) {
                    final FileObject primaryFile = getFileObjectWithShadowSupport(dataObject);
                    projectDir = getProjectDirectory(primaryFile);
                    projectName = getProjectName(primaryFile);

                    final File toFile = FileUtil.toFile(primaryFile);
                    fileName = toFile.getAbsolutePath();
                } else {
                    //else get the name
                    String result = activeTC.getDisplayName();
                    final String displayName = activeTC.getDisplayName();
                    if (result != null) {
                        fileName = result;
                    }
                    if (displayName != null) {
                        fileName = displayName;
                    }
                    if (null != node) {
                        fileName = node.getDisplayName();
                    }
                }

                //version only available for netbeans >=7.1
                final String version = System.getProperty("netbeans.productversion");
                final String projectNameFromProject = getProjectName(project);
                if (null != projectNameFromProject) {
                    projectName = projectNameFromProject;
                }
                Set<String> list = new LinkedHashSet<String>();
                ShowPathInTitleOptions options = ShowPathInTitleOptions.load();
                if (options.showProjectName) {
                    list.add(projectName);
                }
                if (options.showFileName) {
                    if (options.showRelativeFilename) {
                        if (null != fileName && null != projectDir && fileName.startsWith(projectDir)) {
                            String reducedFileName = fileName.substring(projectDir.length());
                            list.add(reducedFileName);
                        }
                    } else {
                        list.add(fileName);
                    }<commit_msg>[fix] fix NPE when selecting node below the library node [internal] improved fallback behaviour for non existing filename<commit_after>/**
 * Copyright 2012 markiewb
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.markiewb.netbeans.plugin.showpathintitle;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.*;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataShadow;
import org.openide.modules.ModuleInstall;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Shows the path of the current {@link DataObject} in the title of the main
 * window. Additionally it shows <ul> <li>the project name </li> <li>the
 * NetBeans version (only supported for NB &gt;=7.1)</li> </ul>
 *
 * @author markiewb
 */
public class Installer extends ModuleInstall {

    private static final long serialVersionUID = -2422104143131463778L;
    private PropertyChangeListener propertyChangeListener;

    public Installer() {
        propertyChangeListener = new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                TopComponent activeTC = TopComponent.getRegistry().getActivated();
                if (null == activeTC) {
                    return;
                }

                DataObject dataObject = activeTC.getLookup().lookup(DataObject.class);
                Project project = activeTC.getLookup().lookup(Project.class);
                Node node = activeTC.getLookup().lookup(Node.class);

                String projectName = null;
                String projectDir = null;
                String fileName = null;
                if (null != dataObject) {
                    final FileObject primaryFile = getFileObjectWithShadowSupport(dataObject);
                    projectDir = getProjectDirectory(primaryFile);
                    projectName = getProjectName(primaryFile);

                    final File toFile = FileUtil.toFile(primaryFile);

                    if (null != toFile) {
                        fileName = toFile.getAbsolutePath();
                    }
                }

                //use other filename resolution strategies, if required
                if (null == fileName) {
                    if (fileName == null && activeTC.getDisplayName() != null) {
                        fileName = activeTC.getDisplayName();
                    }
                    if (fileName == null && activeTC.getName() != null) {
                        fileName = activeTC.getName();
                    }
                    if (fileName == null && node.getDisplayName() != null) {
                        fileName = node.getDisplayName();
                    }
                    if (fileName == null && node.getName() != null) {
                        fileName = node.getName();
                    }
                }
                //version only available for netbeans >=7.1
                final String version = System.getProperty("netbeans.productversion");
                final String projectNameFromProject = getProjectName(project);
                if (null != projectNameFromProject) {
                    projectName = projectNameFromProject;
                }
                Set<String> list = new LinkedHashSet<String>();
                ShowPathInTitleOptions options = ShowPathInTitleOptions.load();
                if (options.showProjectName) {
                    list.add(projectName);
                }
                if (options.showFileName) {
                    if (options.showRelativeFilename) {
                        if (null != fileName && null != projectDir && fileName.startsWith(projectDir)) {
                            String reducedFileName = fileName.substring(projectDir.length());
                            list.add(reducedFileName);
                        }
                    } else {
                        list.add(fileName);
                    }
{'fraction_non_alphanumeric': 0.08527720516437018, 'fraction_numerical': 0.006726700661820549, 'mean_word_length': 1.9507042253521127, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '256440', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2113, 'n_words': 603}