
it.tidalwave.netbeans.filesystem.node.impl.RootNode Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://openbluesky.java.net
* SCM: https://bitbucket.org/tidalwave/openbluesky-src
*
**********************************************************************************************************************/
package it.tidalwave.netbeans.filesystem.node.impl;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.Node.Handle;
import org.openide.util.Utilities;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.indexedfilesystem.IndexedFileSystem;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class RootNode extends AbstractNode
{
private static final String CLASS = RootNode.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
/** The default name for the computer icon. */
private static final String DEFAULT_HOST_NAME = "My Computer";
/** The global filesystem which also binds an unique id to files. */
private static final IndexedFileSystem indexedFileSystem = new IndexedFileSystem();
private static RootNode instance;
/***************************************************************************
*
*
**************************************************************************/
static class RootNodeHandle implements Node.Handle
{
private static final long serialVersionUID = 3242335239823432432L;
@Nonnull
public Node getNode()
{
return RootNode.getInstance();
}
}
/***************************************************************************
*
*
**************************************************************************/
@Nonnull
public synchronized static Node getInstance()
{
if (instance == null)
{
instance = new RootNode();
}
return instance;
}
/***************************************************************************
*
*
**************************************************************************/
private RootNode()
{
super(createChildren());
setName(getComputerName());
}
/***************************************************************************
*
*
**************************************************************************/
@Override
@Nonnull
public Handle getHandle()
{
return new RootNodeHandle();
}
/***************************************************************************
*
*
**************************************************************************/
@Nonnull
private static Children createChildren()
{
final List roots = new ArrayList(indexedFileSystem.findRoots());
logger.info(">>>> roots: " + roots);
for (final FileObject mountPoint : findFileSystemMountPoints())
{
final FileObject indexedRoot = indexedFileSystem.findResource(mountPoint.getPath());
if (indexedRoot != null)
{
roots.add(indexedRoot);
}
else
{
logger.warning("Null indexedRoot for " + mountPoint.getPath() + " - skipped");
}
}
logger.info(">>>> roots with mount points: " + roots);
final Children children = new Children.Array();
for (final FileObject root : roots)
{
try
{
final DataObject dataObject = DataObject.find(root);
final Node node = dataObject.getNodeDelegate();
children.add(new Node[]{node.cloneNode()}); // clone 'cause the original has another parent
}
// Defensive: catch everything, or a single error would break all
catch (Exception e)
{
logger.warning("For " + root + ": " + e.toString());
logger.throwing(CLASS, "", e);
}
}
return children;
}
/***************************************************************************
*
* Retrieve system-specific mount points.
*
* @return a list of system-specific mount points
*
**************************************************************************/
@Nonnull
private static List findFileSystemMountPoints()
{
final List result = new ArrayList();
final List files = new ArrayList();
//
// Mac OS X mounts all disks under /Volumes
//
if (Utilities.getOperatingSystem() == Utilities.OS_MAC)
{
files.addAll(Arrays.asList(new File("/Volumes").listFiles()));
}
//
// Linux can mount files under /media and/or /mnt
//
else if (Utilities.isUnix())
{
final File media = new File("/media");
final File mnt = new File("/mnt");
if (media.exists())
{
files.addAll(Arrays.asList(media.listFiles()));
}
if (mnt.exists())
{
files.addAll(Arrays.asList(mnt.listFiles()));
}
}
for (final File file : files)
{
try
{
if (!file.getCanonicalPath().equals("/"))
{
final FileObject fileObject = FileUtil.toFileObject(file);
if (fileObject != null)
{
result.add(fileObject);
}
else
{
logger.warning("Null fileObject for " + file + " - skipped");
}
}
}
catch (IOException e)
{
logger.warning("findFileSystemMountPoints(): " + e);
}
}
return result;
}
/***************************************************************************
*
* Retrieve the computer name
*
* @return the computer name
*
**************************************************************************/
@Nonnull
private static String getComputerName()
{
try
{
String name = InetAddress.getLocalHost().getHostName();
final int i = name.indexOf('.');
if (i > 0)
{
name = name.substring(0, i);
}
return name.substring(0, 1).toUpperCase() + ((name.length() > 1) ? name.substring(1) : "");
}
catch (Exception e)
{
return DEFAULT_HOST_NAME;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy