All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.examples.nodes.example7.model.FileModel4IOCapabilitiesProvider 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.examples.nodes.example7.model;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.io.File;
import org.openide.util.lookup.ServiceProvider;
import it.tidalwave.role.spi.FileBinaryReadable;
import it.tidalwave.role.spi.FileBinaryWritable;
import it.tidalwave.role.spi.FileTextReadable;
import it.tidalwave.role.spi.FileTextWritable;
import it.tidalwave.netbeans.capabilitiesprovider.CapabilitiesProvider;
import it.tidalwave.netbeans.capabilitiesprovider.CapabilitiesProviderSupport;
import it.tidalwave.netbeans.examples.nodes.example6.model.FileModel4;

/***********************************************************************************************************************
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@ServiceProvider(service=CapabilitiesProvider.class)
public class FileModel4IOCapabilitiesProvider extends CapabilitiesProviderSupport 
  {
    private final static List TEXT_EXTENSIONS = Arrays.asList("txt", "java", "xml");
    
    @Override @Nonnull
    public Collection createCapabilities (final @Nonnull FileModel4 fileModel) 
      {
        final List roles = new ArrayList();
        final File file = new File(fileModel.getPath());
        final String extension = file.getName().replaceAll("^.*\\.", "");
        final boolean textFile = TEXT_EXTENSIONS.contains(extension);
        
        if (file.isFile() && file.canRead())
          {
            roles.add(new FileBinaryReadable(file)); 
            
            if (textFile)
              {
                roles.add(new FileTextReadable(file)); 
              }
          }
        
        if (file.isFile() && file.canWrite())
          {
            roles.add(new FileBinaryWritable(file));
            
            if (textFile)
              {
                roles.add(new FileTextWritable(file)); 
              }
          }
        
        return roles;
      }
  }