Package de.innovationgate.eclipse.editors.all

Source Code of de.innovationgate.eclipse.editors.all.ProjectHeaderPropertyPage

/*******************************************************************************
* 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.all;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.swing.text.StyledEditorKit.BoldAction;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;
import org.osgi.service.prefs.BackingStoreException;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.helpers.HeaderHandler;
import de.innovationgate.eclipse.utils.ui.WidgetFactory;

public class ProjectHeaderPropertyPage extends PropertyPage implements IWorkbenchPropertyPage {

    private Map<String, Text> _headerTabTextMap = new HashMap<String, Text>();

    private Map<String, HeaderHandler> _headerFileMap;

    private Button _specificHeaderCheckbox;

    private IProject _selectedProject;

    private TabFolder _headerTabFolder;

    private Button _importButton;

    private Button _exportButton;

    @Override
    protected Control createContents(Composite parent) {
        if (getElement() instanceof IJavaProject) {
            IJavaProject javaProject = (IJavaProject) getElement();
            _selectedProject = javaProject.getProject();
        }
        else if (getElement() instanceof IProject) {
            _selectedProject = (IProject) getElement();
        }

        Composite container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        container.setLayout(layout);

        _specificHeaderCheckbox = new Button(container, SWT.CHECK);
        GridData specificHeaderCheckboxLayout = new GridData();
        specificHeaderCheckboxLayout.horizontalSpan = 3;
        _specificHeaderCheckbox.setLayoutData(specificHeaderCheckboxLayout);
        _specificHeaderCheckbox.setText("Use specific Header for this project");
        _specificHeaderCheckbox.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent e) {
                if (_specificHeaderCheckbox.getSelection()) {
                    readAllForProject(_selectedProject);
                    for (String current : _headerFileMap.keySet()) {
                        _headerTabTextMap.get(current).setText(_headerFileMap.get(current).getSpecificHeader(_selectedProject));
                        _headerTabTextMap.get(current).setEnabled(true);
                        _importButton.setEnabled(true);
                        _exportButton.setEnabled(true);
                    }
                }
                else {
                    for (String current : _headerFileMap.keySet()) {
                        _headerTabTextMap.get(current).setText(_headerFileMap.get(current).getDefaultHeader());
                        _headerTabTextMap.get(current).setEnabled(false);
                        _importButton.setEnabled(false);
                        _exportButton.setEnabled(false);
                    }
                }
            }

            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
        _specificHeaderCheckbox.setSelection(Plugin.getDefault().getProjectPreferences(_selectedProject).getBoolean("useSpecificHeader", false));

       
        _headerTabFolder = new TabFolder(container, SWT.NONE);
       

       
        Composite buttonContainer = new Composite(container, SWT.NONE);
        GridLayout buttonLayout = new GridLayout();
        buttonLayout.numColumns = 1;
        buttonLayout.marginHeight = 0;
        buttonLayout.marginWidth = 0;            
  
        buttonContainer.setLayout(buttonLayout);
        buttonContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
       
       
       
       
       
       
        _importButton = new Button(buttonContainer, SWT.NONE);
        _importButton.setText("Import");
        _importButton.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent e) {
                handleImport();
            }

            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

        _exportButton = new Button(buttonContainer, SWT.NONE);
        _exportButton.setText("Export");
        _exportButton.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent e) {
                handleExport();
            }

            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

       

        GridData tabFolderLayout = new GridData(GridData.FILL_BOTH);
        tabFolderLayout.horizontalSpan = 1;
        _headerTabFolder.setLayoutData(tabFolderLayout);
        for (String current : _headerFileMap.keySet()) {
            String titel = _headerFileMap.get(current).getTitle();
            TabItem tabItem = new TabItem(_headerTabFolder, SWT.FILL);
            Composite tabItemContainer = new Composite(_headerTabFolder, SWT.NONE);
            layout = new GridLayout();
            layout.numColumns = 1;
            layout.marginWidth = 0;
            layout.marginHeight = 0;
            layout.horizontalSpacing = 0;
            layout.verticalSpacing = 0;
            tabItemContainer.setLayout(layout);
            tabItem.setText(titel);

            Text headerTabText = new Text(tabItemContainer, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
            headerTabText.setData(current);

            if (Plugin.getDefault().getProjectPreferences(_selectedProject).getBoolean("useSpecificHeader", false)) {
                headerTabText.setText(_headerFileMap.get(current).getSpecificHeader(_selectedProject));
                headerTabText.setEnabled(true);
                _importButton.setEnabled(true);
                _exportButton.setEnabled(true);
            }
            else {
                headerTabText.setText(_headerFileMap.get(current).getDefaultHeader());
                headerTabText.setEnabled(false);
                _importButton.setEnabled(false);
                _exportButton.setEnabled(false);
            }

            headerTabText.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    if (e.getSource() instanceof Text) {
                        Text source = (Text) e.getSource();
                        if (_specificHeaderCheckbox.getSelection()) {
                            _headerFileMap.get((String) source.getData()).setSpecificHeader(_selectedProject, source.getText());
                        }

                    }
                }
            });

            GridData codeLayout = new GridData(GridData.FILL_BOTH);
            codeLayout.minimumHeight = WidgetFactory.computeFontHeight(headerTabText) * 20;
            headerTabText.setLayoutData(codeLayout);
            _headerTabTextMap.put(current, headerTabText);
            tabItem.setControl(tabItemContainer);
        }

       
       
       
       
       
       
        return container;

    }

    @Override
    protected void performApply() {
        writeChanges();
        try {
            Plugin.getDefault().getProjectPreferences(_selectedProject).putBoolean("useSpecificHeader", _specificHeaderCheckbox.getSelection());
            Plugin.getDefault().getProjectPreferences(_selectedProject).flush();
        }
        catch (BackingStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.performApply();
    }

    public ProjectHeaderPropertyPage() {
        super();
        _headerFileMap = Plugin.getDefault().getHeaderFileMap();
    }

    @Override
    protected void performDefaults() {
        for (String current : _headerFileMap.keySet()) {
            _headerFileMap.get(current).setSpecificHeader(_selectedProject, "");
        }
        _specificHeaderCheckbox.setSelection(false);
        for (String current : _headerFileMap.keySet()) {
            _headerTabTextMap.get(current).setText(_headerFileMap.get(current).getDefaultHeader());
            _headerTabTextMap.get(current).setEnabled(false);
            _importButton.setEnabled(false);
            _exportButton.setEnabled(false);
        }
        super.performDefaults();
    }

    @Override
    public boolean performOk() {
        writeChanges();
        try {
            Plugin.getDefault().getProjectPreferences(_selectedProject).putBoolean("useSpecificHeader", _specificHeaderCheckbox.getSelection());
            Plugin.getDefault().getProjectPreferences(_selectedProject).flush();
        }
        catch (BackingStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return super.performOk();
    }

    private void writeChanges() {
        for (String current : _headerFileMap.keySet()) {
            _headerFileMap.get(current).writeSpecificHeader(_selectedProject);
        }
    }

    private void readAllForProject(IProject project) {
        for (String current : _headerFileMap.keySet()) {
            _headerFileMap.get(current).readSpecificHeader(project);
        }
    }

    private void handleImport() {
        FileDialog fileDialog = new FileDialog(getShell());
        fileDialog.setText("Import project headers");
        Properties properties = new Properties();
        String file = fileDialog.open();

        if (file != null) {
            try {
                properties.load(new FileInputStream(file));
                for (String key : _headerFileMap.keySet()) {
                    HeaderHandler handler = _headerFileMap.get(key);
                    String headerText = properties.getProperty(handler.getName());
                    handler.setSpecificHeader(_selectedProject, headerText);
                    _headerTabTextMap.get(key).setText(headerText);
                }

            }
            catch (FileNotFoundException e) {
                MessageDialog.openError(getShell(), "Error", "WDS was unable to read file");
                Plugin.getDefault().logError("WDS was unable to read file", e);
            }
            catch (IOException e) {
                MessageDialog.openError(getShell(), "Error", "WDS was unable to read file");
                Plugin.getDefault().logError("WDS was unable to read file", e);
            }
        }
    }

    private void handleExport() {
        FileDialog fileDialog = new FileDialog(getShell());
        fileDialog.setText("Export project headers");
        Properties properties = new Properties();
        String fileString = fileDialog.open();
       
        if(!fileString.endsWith(".pref")){
            fileString+=".pref";
        }
       
        if (fileString != null) {
            boolean overwrite = true;
            File file =new File(fileString);
            if (file.exists()) {
                overwrite = MessageDialog.openQuestion(getShell(), "File already exists", "The choosen file already exists. Overwrite?");
            }
            boolean empty = true;
            if (overwrite) {
                for (String key : _headerFileMap.keySet()) {
                    HeaderHandler handler = _headerFileMap.get(key);
                    String headerText = handler.getSpecificHeader(_selectedProject);
                    properties.put(key, headerText);        
                }               
                    
                {
                try {                                   
                    properties.store(new FileOutputStream(fileString), "Exported headers of project : " + _selectedProject.getName());
                }
                catch (FileNotFoundException e) {
                    MessageDialog.openError(getShell(), "Error", "WDS was unable to write file");
                    Plugin.getDefault().logError("WDS was unable to write file", e);
                }
                catch (IOException e) {
                    MessageDialog.openError(getShell(), "Error", "WDS was unable to write file");
                    Plugin.getDefault().logError("WDS was unable to write file", e);
                }      
                }
            }
        }
    }
}
TOP

Related Classes of de.innovationgate.eclipse.editors.all.ProjectHeaderPropertyPage

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.