Package de.innovationgate.eclipse.editors.helpers

Source Code of de.innovationgate.eclipse.editors.helpers.HeaderHandler

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/

package de.innovationgate.eclipse.editors.helpers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.osgi.service.prefs.BackingStoreException;

import de.innovationgate.eclipse.editors.Plugin;

public class HeaderHandler {
    private File _defaultHeaderFile;
    private String _defaultHeader = "";
    private String _title;
    private String _name;
    private Map<String, String> _specificHeaders = new HashMap<String, String>();   
   
    public String getHeaderForProject(IProject project){

        if(Plugin.getDefault().getProjectPreferences(project).getBoolean("useSpecificHeader", false)){
            readSpecificHeader(project);
            return getSpecificHeader(project);
        }else{
            readDefaultHeaderFile();
            return getDefaultHeader();
        }
    }
   
   
    public String getSpecificHeader(IProject project){
       String projectName = project.getName();
       return _specificHeaders.get(projectName)!=null ? _specificHeaders.get(projectName) : "";
    }
   
    public void setSpecificHeader(IProject project, String header){
        String projectName = project.getName();
        _specificHeaders.put(projectName, header);
   
   
    public void writeSpecificHeader(IProject project) {
        IEclipsePreferences prefs = Plugin.getDefault().getProjectPreferences(project);
        prefs.put(getName(), getSpecificHeader(project));
        try {
            prefs.flush();
        }
        catch (BackingStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
    public void readSpecificHeader(IProject project){
        IEclipsePreferences prefs = Plugin.getDefault().getProjectPreferences(project);
        _specificHeaders.put(project.getName(),prefs.get(getName(), ""));
    }   
   
    public String getName() {
        return _name;
    }

    public String getTitle(){
        return _title;
    }

    public void setDefaultHeaderFile(File headerFile) {
        _defaultHeaderFile = headerFile;
    }

    public void setDefaultHeader(String header) {
       _defaultHeader = header;
    }
   
    public String getDefaultHeader() {
        return _defaultHeader;
    }

    public void writeDefaultHeaderFile() {
        if(!_defaultHeaderFile.exists()){
            try {
                _defaultHeaderFile.createNewFile();
            }
            catch (IOException e) {
                Plugin.getDefault().logError("Can not create file at : " + _defaultHeaderFile.getAbsolutePath(), e);
            }
        }
            PrintWriter writer;
            try {
                writer = new PrintWriter(_defaultHeaderFile);
                writer.println(_defaultHeader);
                writer.close();
            }
            catch (FileNotFoundException e) {
                Plugin.getDefault().logError("File not found : " + _defaultHeaderFile.getAbsolutePath(), e);
            }       
    }

    public void readDefaultHeaderFile() {
        if(_defaultHeaderFile.exists()){
            BufferedReader breader = null;
            try {
                breader = new BufferedReader(new FileReader(_defaultHeaderFile));
                StringBuffer sb = new StringBuffer();
   
                String tmp;
                do {
                    tmp = breader.readLine();
                    if (tmp != null) {
                        sb.append(tmp);
                    }
                } while (tmp != null);
   
                _defaultHeader = sb.toString();
            }
            catch (FileNotFoundException e) {
                Plugin.getDefault().logError("File not found : " + _defaultHeaderFile.getAbsolutePath(), e);
            }
            catch (IOException e) {
                Plugin.getDefault().logError("Unable to read file : " + _defaultHeaderFile.getAbsolutePath(), e);
            }finally{
                try {
                    breader.close();
                }
                catch (IOException e) {
                    Plugin.getDefault().logError("Unable to close reader for file : " + _defaultHeaderFile.getAbsolutePath(), e);
                }
            }
        }else{
            _defaultHeader = "";
        }
    }

    public HeaderHandler(String name, File headerFile, String title) {
        _name = name;
        _title = title;       
        _defaultHeaderFile = headerFile;
        if (!_defaultHeaderFile.exists()) {
            try {
                _defaultHeaderFile.createNewFile();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
TOP

Related Classes of de.innovationgate.eclipse.editors.helpers.HeaderHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.