Package de.innovationgate.eclipse.wgadesigner.views

Source Code of de.innovationgate.eclipse.wgadesigner.views.RuntimeView

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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
import org.eclipse.ui.internal.actions.SelectWorkingSetAction;
import org.eclipse.ui.part.ViewPart;

import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.actions.RestartWGARuntime;
import de.innovationgate.eclipse.wgadesigner.actions.StartWGARuntime;
import de.innovationgate.eclipse.wgadesigner.actions.StopWGARuntime;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatServerStateListener;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;


@SuppressWarnings("restriction")
public class RuntimeView extends ViewPart{
    private TableViewer _table;
    private IWorkspace _workspace;    
    private WGARuntime _currentRunningRuntime;
    private WGARuntime _selectedRuntime;
    private TomcatServerStateListener _tomcatServerStateListener;
    private IResourceChangeListener _resourceChangeListener;
    private ISelectionChangedListener _selectionChangedListener;
    private Action _startWGARuntime;
    private Action _refreshAction;
    private Action _stopWGARuntime;
    private Action _doubleClickAction;
    private Action _workingSetSelectionAction;
    private ArrayList<IProject> _projectsInWorkingSet;
    private WorkingSetFilterActionGroup _actionGroup;
   
   
    public RuntimeView() {
        super();
        _workspace = ResourcesPlugin.getWorkspace();
        _tomcatServerStateListener = new TomcatServerStateListener() {     

            public void stateChanged(int state) {
                _currentRunningRuntime = TomcatUtils.getInstance().getCurrentRuntime();
                Display.getDefault().asyncExec(new Runnable() {
                    public void run() {
                        validateActions();
                        _table.refresh();
                    }
                });
            }
        };
       
        _resourceChangeListener = new IResourceChangeListener() {           
            public void resourceChanged(IResourceChangeEvent event) {
                Display.getDefault().asyncExec(new Runnable() {
                    public void run() {
                        validateActions();
                        updateWorkingSetList();
                        if (_actionGroup != null) {
                            _table.setInput(WGADesignerPlugin.getAllRuntimes(_projectsInWorkingSet));
                        }
                        else {
                            _table.setInput(WGADesignerPlugin.getAllRuntimes());
                        }                       
                        _table.refresh();
                    }
                });

            }
        };
       
        _selectionChangedListener = new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {               
                if (!_table.getSelection().isEmpty()) {
                   
                    ISelection selection = _table.getSelection();
                    if (selection instanceof StructuredSelection) {
                        _selectedRuntime = (WGARuntime) ((StructuredSelection)selection).getFirstElement();                       
                    }
                   
                    Display.getDefault().asyncExec(new Runnable() {
                        public void run() {
                            validateActions();
                        }
                    });
                }
               
            }

        };     
       
        _workspace.addResourceChangeListener(_resourceChangeListener, IResourceChangeEvent.POST_BUILD);
        TomcatUtils.getInstance().addListener(_tomcatServerStateListener);
    }



    @Override
    public void dispose() {
        _workspace.removeResourceChangeListener(_resourceChangeListener);
        TomcatUtils.getInstance().removeListener(_tomcatServerStateListener);
        _table.removeSelectionChangedListener(_selectionChangedListener);
        super.dispose();
    }







    @Override
    public void init(IViewSite site) throws PartInitException {       
        super.init(site);     
    }


    class RuntimeViewContentProvider extends ArrayContentProvider {

        @Override
        public Object[] getElements(Object inputElement) {
           List<WGARuntime> runtimes = new ArrayList<WGARuntime>();
           
            if(inputElement instanceof  Map){
                Map<String,WGARuntime> runtimeMap = ( Map<String,WGARuntime>) inputElement;
               
                for (String key : runtimeMap.keySet()) {
                    runtimes.add(runtimeMap.get(key));
                }               
            } 
           
            return runtimes.toArray();
        }

        public void dispose() {  
           
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            //System.out.println(oldInput+"\n\n" + newInput);           
        }
       
    }

   
    class RuntimeViewLabelProvider extends LabelProvider{

        @Override
        public Image getImage(Object element) {
            if (element instanceof WGARuntime) {               
                WGARuntime runtime = (WGARuntime)element;
                if (TomcatUtils.getInstance().isRunning(runtime)) {
                    return WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_SERVER_START);
                }
                else {
                    return WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_RUNTIME);
                }
            }
           
            return null;
           
        }

        @Override
        public String getText(Object element) {
            if(element instanceof WGARuntime){
                WGARuntime runtime = (WGARuntime)element;
               
                if (TomcatUtils.getInstance().isRunning(runtime)) {
                    String distri = TomcatUtils.getInstance().getCurrentRuntime().getConfiguration().getWgaDistribution();
                    return runtime.getName() + " " + distri + " (running) ";                 
                }
                return runtime.getName();
            }           
            return null;
        }
       
    }
   
   
    class NameSorter extends ViewerSorter {
        public int compare(Viewer viewer, Object e1, Object e2) {
            if (e1 instanceof WGARuntime || e2 instanceof WGARuntime) {
                WGARuntime rt1 = (WGARuntime) e1;
                WGARuntime rt2 = (WGARuntime) e2;
                return rt1.getName().compareTo(rt2.getName());
            }

            return 0;

        }
    }
   


    @Override
    public void createPartControl(Composite parent) {       
        _table = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);     
        _table.setContentProvider(new RuntimeViewContentProvider());      
        _table.setLabelProvider(new RuntimeViewLabelProvider());
        _table.setSorter(new NameSorter());
        _table.setInput(WGADesignerPlugin.getAllRuntimes());
       
        if (_actionGroup != null) {
            _table.setInput(WGADesignerPlugin.getAllRuntimes(_projectsInWorkingSet));
        }
        else {
            _table.setInput(WGADesignerPlugin.getAllRuntimes());
        }      
       
        _table.addSelectionChangedListener(_selectionChangedListener);      

        makeActions();
        hookContextMenu();
        hookDoubleClickAction();
        contributeToActionBars();
       
    }   
   
    private void makeActions (){

    _startWGARuntime = new Action() {
        public void run() {
            if (_table.getTable().getItemCount() > 0) {
                if (TomcatUtils.getInstance().isRunning(_selectedRuntime)) {
                    RestartWGARuntime.call();
                }
                else {
                    StartWGARuntime.call(_selectedRuntime);
                }               
            }
        }
    };

    _startWGARuntime.setText("Start/ restart WGA Runtime");
    _startWGARuntime.setImageDescriptor(ImageDescriptor.createFromImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_SERVER_START)));
    _startWGARuntime.setDisabledImageDescriptor(ImageDescriptor.createFromImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_INAKTIV_WGA_SERVER_START)));

    _refreshAction = new Action() {
        public void run() {
            updateWorkingSetList();
            _table.refresh()
         }
    };
    _refreshAction.setText("Refresh view");
    _refreshAction.setImageDescriptor(ImageDescriptor.createFromImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_REFRESH)));

    // _refreshAction.setEnabled(enabled);
    _stopWGARuntime = new Action() {
        public void run() {
            StopWGARuntime.call();
        }
    };

    _stopWGARuntime.setText("Stop WGA Runtime");
    // stopWGARuntime.setToolTipText("Action 2 tooltip");
    _stopWGARuntime.setImageDescriptor(ImageDescriptor.createFromImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_SERVER_STOP)));
    _stopWGARuntime.setDisabledImageDescriptor(ImageDescriptor.createFromImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_INAKTIV_WGA_SERVER_STOP)));
    _stopWGARuntime.setEnabled(false);
   
    _doubleClickAction = new Action() {
        public void run() {
            ISelection selection = _table.getSelection();
            Object obj = ((IStructuredSelection) selection).getFirstElement();
            if (obj instanceof WGARuntime) {
                WGARuntime runtime  = (WGARuntime)obj;
                try {
                    runtime.openEditor();
                }
                catch (PartInitException e) {
                    WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), getViewSite().getShell(), "Open runtime configuration failed", "Unable to open runtime configuration.", e);
                }
            }
        }
    };
   
   
   
   
   
    _actionGroup = new WorkingSetFilterActionGroup(getSite().getShell(), new IPropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            updateWorkingSetList()
            _table.refresh();
        }
    });
   
    XMLMemento memento = null;
    // TODO rebuild memento for whole plugin?
    try {
        File mementoFile = getWorkingSetMemento();
        if (mementoFile.exists()) {
            FileReader reader = new FileReader(mementoFile);
            memento = XMLMemento.createReadRoot(reader);
            if (memento != null) {
                _actionGroup.restoreState(memento);
            }
        }
    }
    catch (WorkbenchException e) {
    }
    catch (FileNotFoundException e) {
    }

    _workingSetSelectionAction = new SelectWorkingSetAction(_actionGroup, getSite().getShell());
    _workingSetSelectionAction.setText("Edit Working Sets");
   
   
    //_workingSetSelectionAction.setImageDescriptor(ImageDescriptor.createFromImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_RUNTIME)));

    }
   
    private void hookContextMenu() {
        MenuManager menuMgr = new MenuManager("#PopupMenu");
        menuMgr.setRemoveAllWhenShown(true);
        menuMgr.addMenuListener(new IMenuListener() {
            public void menuAboutToShow(IMenuManager manager) {
                manager.add(_startWGARuntime);
                manager.add(_stopWGARuntime);
            }
        });
        Menu menu = menuMgr.createContextMenu(_table.getControl());
        _table.getControl().setMenu(menu);
        getSite().registerContextMenu(menuMgr, _table);
    }
   
    private void hookDoubleClickAction() {
        _table.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                _doubleClickAction.run();
            }
        });
    }
   
    private void contributeToActionBars() {
        IActionBars bars = getViewSite().getActionBars();
        fillLocalPullDown(bars.getMenuManager());
        fillLocalToolBar(bars.getToolBarManager());
    }
   
    private void fillLocalPullDown(IMenuManager manager) {
        manager.add(_startWGARuntime);
        manager.add(new Separator());
        manager.add(_stopWGARuntime);
        manager.add(new Separator());
        manager.add(_workingSetSelectionAction);

    }
   
    private void fillLocalToolBar(IToolBarManager manager) {
        manager.add(_startWGARuntime);
        manager.add(_stopWGARuntime);
        //manager.add(_workingSetSelectionAction);
        manager.add(_refreshAction);
    }
   
   
    private void validateActions() {
    if (_table.getTable().getSelection().length > 0) {
            if (TomcatUtils.getInstance().isRunning()) {
                if (_currentRunningRuntime.equals(_selectedRuntime)) {
                    _startWGARuntime.setEnabled(true);
                    _stopWGARuntime.setEnabled(true);
                    return;
                }
                else {
                    _startWGARuntime.setEnabled(true);
                    _stopWGARuntime.setEnabled(false);
                    return;
                }
            }
            else {
                _startWGARuntime.setEnabled(true);
                _stopWGARuntime.setEnabled(false);
                return;
            }       
    }

    _startWGARuntime.setEnabled(false);
    _stopWGARuntime.setEnabled(false);
    }
   
   
   
    private void saveMementoToFile(XMLMemento memento) {
        File stateFile = getWorkingSetMemento();
        if (stateFile != null) {
            try {
                FileOutputStream stream = new FileOutputStream(stateFile);
                OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
                memento.save(writer);
                writer.close();
            }
            catch (IOException e) {
                stateFile.delete();
                WGADesignerPlugin.getDefault().logError("unable to write config file : " + stateFile.getAbsolutePath(), e);
            }
        }
    }

    private File getWorkingSetMemento() {
        IPath path = WGADesignerPlugin.getDefault().getStateLocation();
        if (path == null) {
            return null;
        }
        path = path.append("runtimeViewSelectedWorkingsets.xml");
        return path.toFile();
    }
   
    private void updateWorkingSetList() {

        _projectsInWorkingSet = new ArrayList<IProject>();
        _projectsInWorkingSet.clear();

        if (_actionGroup.getWorkingSet() == null) {
            Map<String, WGARuntime> allRuntimes = WGADesignerPlugin.getAllRuntimes();
            for (String currentKey : allRuntimes.keySet()) {
                _projectsInWorkingSet.add(allRuntimes.get(currentKey).getProject());
            }
        }
        else {
            if (!_actionGroup.getWorkingSet().isEmpty()) {
                for (IAdaptable current : _actionGroup.getWorkingSet().getElements()) {
                    if (current instanceof IProject) {
                        IProject currentProject = (IProject) current;
                        _projectsInWorkingSet.add(currentProject);
                    }
                }
            }
            else if(_actionGroup.getWorkingSet().isEmpty() && _actionGroup.getWorkingSet().isAggregateWorkingSet()){
                Map<String, WGARuntime> allRuntimes = WGADesignerPlugin.getAllRuntimes();
                for (String currentKey : allRuntimes.keySet()) {
                    _projectsInWorkingSet.add(allRuntimes.get(currentKey).getProject());
                }

            }
        }

        XMLMemento memento = XMLMemento.createWriteRoot("runTimeViewSelectedWorkingSet");
        _actionGroup.saveState(memento);
        saveMementoToFile(memento);
       
        if (_actionGroup != null) {
            _table.setInput(WGADesignerPlugin.getAllRuntimes(_projectsInWorkingSet));
        }
        else {
            _table.setInput(WGADesignerPlugin.getAllRuntimes());
        }      
       
    }
   
   
   

    @Override
    public void setFocus() {
        _table.getControl().setFocus();       
    }



  

   

}
TOP

Related Classes of de.innovationgate.eclipse.wgadesigner.views.RuntimeView

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.