Package de.innovationgate.eclipse.editors.tml

Source Code of de.innovationgate.eclipse.editors.tml.TMLEditor

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

import java.io.IOException;
import java.util.ResourceBundle;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IParameter;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.Parameterization;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.texteditor.ContentAssistAction;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.Preferences;
import de.innovationgate.eclipse.editors.actions.SurroundWithAction;
import de.innovationgate.eclipse.editors.all.AbstractWGATextEditor;
import de.innovationgate.eclipse.editors.commands.CreateLabel;
import de.innovationgate.eclipse.editors.commands.DisplayHelp;
import de.innovationgate.eclipse.editors.commands.ToggleComment;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.model.TMLFileMetadataModel;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;

public class TMLEditor extends AbstractWGATextEditor implements IResourceChangeListener, IPropertyChangeListener {

  private Composite _breadcrumbComposite;
    private Button _btn;
    private Button _chk;
    private TMLFileMetadataModel _tmlFileModel;
    private Button _btnDirectAccess;
   
   
    private boolean _dirty = false;
    private Button _btnOpenInBrowser;
    private Label _toolbarLblProject;
    private Label _toolbarLblLocation;

    public TMLEditor() {
    super();   
    setSourceViewerConfiguration(new TMLEditorConfiguration());
    setDocumentProvider(new TMLDocumentProvider());   
    Plugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
  }
 
  @Override
  protected void doSetInput(IEditorInput newInput) throws CoreException {
    super.doSetInput(newInput);
    // set title
    IFile input = getInputFile();
    if (input != null) {
      WGADesignStructureHelper helper = new WGADesignStructureHelper(input);
      String mediaKey = WGADesignStructureHelper.determineMediaKey(input);
      if (mediaKey == null) {
        mediaKey = "html";
      }
      int segmentsMatch = helper.getTmlRoot().getFolder(mediaKey).getFullPath().matchingFirstSegments(input.getFullPath());
      String tmlFileReference = input.getFullPath().removeFirstSegments(segmentsMatch).removeFileExtension().toString();
      tmlFileReference = tmlFileReference.replaceAll("/", ":");
     
      if (tmlFileReference.length() > 20) {
        String wrappedTitle = tmlFileReference.substring(tmlFileReference.length() - 16);
        if (wrappedTitle.startsWith(":")) {
          wrappedTitle = wrappedTitle.substring(1);
        }
        setPartName("... " + wrappedTitle);
      } else {
        setPartName(tmlFileReference);
      }
      _tmlFileModel = new TMLFileMetadataModel(input);
     
      ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
     
      updateToolbar();
    }
   
  }

  @Override
  protected void createActions() {
    super.createActions();
    ResourceBundle resourceBundle = Plugin.getDefault().getResourceBundle();
    IAction action= new ContentAssistAction(resourceBundle, "ContentAssistProposal.", this);
    action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
   
    String actionId ="de.innovationgate.eclipse.wgadesigner.editors.tml.actions.ContentAssistProposal";
    setAction(actionId, action);
    markAsStateDependentAction(actionId, true);

      setAction(SurroundWithAction.ID, new SurroundWithAction());
        if(Plugin.getDefault().getPreferenceStore().getBoolean(Preferences.DISPLAY_TMLEDITOR_TOOLBAR)){
            showToolbar();
        }
  }


  @Override
  protected void editorContextMenuAboutToShow(IMenuManager menu) {
    super.editorContextMenuAboutToShow(menu);
   
    menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, new CommandContributionItem(new CommandContributionItemParameter(getSite(), null, ToggleComment.ID, null, null, null, null, "Toggle comment", null, null, CommandContributionItem.STYLE_PUSH, null, true)));   
    menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, new CommandContributionItem(new CommandContributionItemParameter(getSite(), null, CreateLabel.ID, null, null, null, null, "Create label", null, null, CommandContributionItem.STYLE_PUSH, null, true)));
    menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, new SurroundWithAction());
    menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, new CommandContributionItem(new CommandContributionItemParameter(getSite(), null, DisplayHelp.ID, null, null, null, null, "Display help", null, null, CommandContributionItem.STYLE_PUSH, null, true)));
  }
 
  public void shiftRight() throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {   
    IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
    handlerService.executeCommand("org.eclipse.ui.edit.text.shiftRight", null);
  }
 
  public void shiftLeft() throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {   
    IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
    handlerService.executeCommand("org.eclipse.ui.edit.text.shiftLeft", null);
  }

  @Override
  protected String[] getKeyBindingScopes() {
    return new String[] { "de.innovationgate.eclipse.ids.contexts.TMLEditorContext" };
  }

    @Override
    public void createToolbar(Composite parent) {
        GridLayout gridLayout = new GridLayout(2, true);
        parent.setLayout(gridLayout);
       
        Composite left = new Composite(parent, SWT.NONE);
        left.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true));
        RowLayout layout = new RowLayout();
        layout.center = true;
        left.setLayout(layout);
       
        Composite right = new Composite(parent, SWT.NONE);
        right.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
        layout = new RowLayout();
        layout.center = true;
        right.setLayout(layout);

       
        _btnDirectAccess = new Button(right, SWT.CHECK);
        _btnDirectAccess.setText("direct access");
       

        boolean isDirectAccess = false;
        try {
            isDirectAccess = WGADesignStructureHelper.isDirectAccess(getInputFile());
        }
        catch (Exception e1) {           
        }  
       
        _btnDirectAccess.setSelection(isDirectAccess);
        if (hasMDHeader()) {
            _btnDirectAccess.setEnabled(false);
        } else {
            _btnDirectAccess.setEnabled(true);
        }
       
        _btnDirectAccess.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent e) {
                if (_tmlFileModel != null) {
                    _tmlFileModel.setDirectAccess(_btnDirectAccess.getSelection());
                    _dirty = true;
                    firePropertyChange(IEditorPart.PROP_DIRTY);
                }
            }
           
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
       
        _btnOpenInBrowser = new Button(right, SWT.PUSH);
        _btnOpenInBrowser.setText("open in browser");
        if (isDirectAccess) {
            _btnOpenInBrowser.setEnabled(true);
        } else {
            _btnOpenInBrowser.setEnabled(false);
        }
       
        _btnOpenInBrowser.addSelectionListener(new SelectionListener() {
           
            public void widgetSelected(SelectionEvent e) {
                try {
                    ICommandService cService = (ICommandService) getSite().getService(ICommandService.class);
                    IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
                   
                    Command command = cService.getCommand("de.innovationgate.eclipse.ids.commands.OpenModuleInBrowser");
                   
                    IParameter paramTMLFile = command.getParameter("de.innovationgate.eclipse.ids.commands.OpenModuleInBrowser.paramTMLFilePath");
 
                    Parameterization parm = new Parameterization(paramTMLFile, getInputFile().getFullPath().toString());
      
                    ParameterizedCommand parmCommand = new ParameterizedCommand(command, new Parameterization[] { parm });
                    handlerService.executeCommand(parmCommand, null);
                }
                catch (NotDefinedException e1) {
                }
                catch (ExecutionException e1) {
                    WorkbenchUtils.showErrorDialog(getSite().getShell(), "Unable to open tml module in browser", e1.getMessage());
                }
                catch (NotEnabledException e1) {
                }
                catch (NotHandledException e1) {
                }
            }
           
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });
       
        Composite leftComp = new Composite(left, SWT.NONE);
       
        GridLayout leftCompLayout = new GridLayout(1, false);
        leftCompLayout.marginTop = 0;
        leftCompLayout.marginBottom = 0;
        leftCompLayout.marginRight = 0;
        leftCompLayout.marginLeft = 0;
        leftCompLayout.marginWidth = 0;
        leftCompLayout.horizontalSpacing = 2;
        leftCompLayout.verticalSpacing = 2;
       
        leftComp.setLayout(leftCompLayout);
       
        _toolbarLblProject = new Label(leftComp, SWT.NONE);
       
        FontData[] fontData = _toolbarLblProject.getFont().getFontData();
        for(int i = 0; i < fontData.length; ++i) {
            fontData[i].setStyle(SWT.BOLD);
        }
        final Font newFont = new Font(getSite().getShell().getDisplay(), fontData);
        _toolbarLblProject.setFont(newFont);
        _toolbarLblProject.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
                newFont.dispose();
            }
        });
       
        _toolbarLblLocation = new Label(leftComp, SWT.NONE);
       
        updateToolbar();
    }
   
    private void updateToolbar() {
        if (_toolbarLblProject != null && !_toolbarLblProject.isDisposed()) {
            _toolbarLblProject.setText("Project '" + getInputFile().getProject().getName() + "'");
        }
        if (_toolbarLblLocation != null && !_toolbarLblLocation.isDisposed()) {
            _toolbarLblLocation.setText(getInputFile().getProjectRelativePath().toString());
        }
    }
 
    @Override
    public void dispose() {
        ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
        Plugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
        super.dispose();
    }

    private boolean hasMDHeader() {
        try {
            return getDocumentProvider().getDocument(getEditorInput()).getContentType(0).equals(TMLPartitionScanner.TML_METAHEADER);
        }
        catch (Exception e) {
        }
        return false;
    }


    public void resourceChanged(IResourceChangeEvent event) {
        getSite().getShell().getDisplay().asyncExec(new Runnable() {
           
            public void run() {
                boolean directAccess = false;
                try {
                    directAccess = WGADesignStructureHelper.isDirectAccess(getInputFile());
                } catch (Exception e) {           
                }
                if (_btnDirectAccess != null) {
                    if (hasMDHeader()) {
                        _btnDirectAccess.setEnabled(false);
                    } else {
                        _btnDirectAccess.setEnabled(true);
                    }
                   _btnDirectAccess.setSelection(directAccess);           
                }
               
                if (directAccess) {
                    _btnOpenInBrowser.setEnabled(true);
                } else {
                    _btnOpenInBrowser.setEnabled(false);
                }
                try {
                    _tmlFileModel.reload();
                }
                catch (IOException e) {
                }
            }
        });
      
    }

    @Override
    public void doSave(IProgressMonitor progressMonitor) {       
        if (_tmlFileModel != null && !hasMDHeader()) {
            try {
                _tmlFileModel.saveChanges();
                _dirty = false;
            }
            catch (IOException e) {
                progressMonitor.setCanceled(true);
            }
        }
        super.doSave(progressMonitor);       
    }

    @Override
    public boolean isDirty() {
        if (_dirty) {
            return true;
        } else {
            return super.isDirty();
        }
    }

    public void propertyChange(PropertyChangeEvent event) {       
        if(event.getProperty().equals(Preferences.DISPLAY_TMLEDITOR_TOOLBAR)){
            if(Plugin.getDefault().getPreferenceStore().getBoolean(Preferences.DISPLAY_TMLEDITOR_TOOLBAR)){
                showToolbar();                      
            }else{
                hideToolbar();
            }
        }       
    }
 
   
}
TOP

Related Classes of de.innovationgate.eclipse.editors.tml.TMLEditor

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.