Package tool.action.cdf

Source Code of tool.action.cdf.OpenClassComponentAction

/*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others.
* 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:
*     IBM Corporation - initial API and implementation
*******************************************************************************/
package tool.action.cdf;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

import tool.ToolPlugin;
import tool.editors.cdf.AttributeDialog;
import tool.editors.cdf.ConstantDialog;
import tool.editors.cdf.EventDialog;
import tool.editors.cdf.VirtualAttributeDialog;
import tool.editors.method.MethodEditor;
import tool.editors.method.MethodEditorInput;
import tool.model.IClassComponent;
import tool.model.ToolAttribute;
import tool.model.ToolClass;
import tool.model.ToolConstant;
import tool.model.ToolEvent;
import tool.model.ToolEventHandler;
import tool.model.ToolMethod;
import tool.model.ToolVirtualAttribute;
import tool.model.ToolWindowAttribute;

public class OpenClassComponentAction extends Action {
 
  private IWorkbenchPage page;
  private IClassComponent data;
  private ISelectionProvider provider;


  /**
   * Construct the OpenPropertyAction with the given page.
   * @param page The page to use as context to open the editor.
   * @param selectionProvider The selection provider
   */
  public OpenClassComponentAction(IWorkbenchPage page, ISelectionProvider selectionProvider) {
    setText("Open");
    this.page = page;
    provider = selectionProvider;
  }
  public OpenClassComponentAction(IWorkbenchPage page, IClassComponent data) {
    setText("Open");
    this.page = page;
    this.data = data;
  }
 
  /* (non-Javadoc)
   * @see org.eclipse.jface.action.Action#isEnabled()
   */
  public boolean isEnabled() {
    if (provider != null){
      ISelection selection = provider.getSelection();
      if(!selection.isEmpty()) {
        IStructuredSelection sSelection = (IStructuredSelection) selection;
        if(sSelection.size() == 1) {
          if (sSelection.getFirstElement() instanceof IClassComponent) {
            data = ((IClassComponent)sSelection.getFirstElement());        
            return true;
          }
        }
      }
    } else if (data != null){
      return true;
    }
    return false;
  }
 
  /* (non-Javadoc)
   * @see org.eclipse.jface.action.Action#run()
   */
  public void run() {
    /* In production code, you should always externalize strings,
     *   but this is an example. */
    try {
      if(isEnabled()) {
        if (data instanceof ToolMethod || data instanceof ToolEventHandler){
          String searchText = "";
          IFile cexFile = null;
          if (data instanceof ToolMethod){
            showMethodEditor((ToolMethod)data);
            return;
         
          } else {
            ToolEventHandler eh = ((ToolEventHandler)data);
            cexFile = eh.getFile()
            searchText = eh.getSignature();
            searchText = "event handler\\s+.+\\." + searchText.trim();
          }
         
          if (cexFile == null)
            return;
          IEditorPart editor = IDE.openEditor(page, cexFile);

          if (editor instanceof ITextEditor) {
            ITextEditor textEditor = (ITextEditor) editor;

            IDocumentProvider documentProvider =
              textEditor.getDocumentProvider();
            IDocument document =
              documentProvider.getDocument(editor.getEditorInput());

            FindReplaceDocumentAdapter searchAdapter =
              new FindReplaceDocumentAdapter(document);

            try {
              IRegion region = searchAdapter.find(0,
                  searchText,
                  true /* forwardSearch */,
                  false /* caseSensitive */,
                  false /* wholeWord */,
                  true /* regExSearch */);
              if (region != null)
                ((ITextEditor)editor).selectAndReveal(region.getOffset(), region.getLength());

            } catch (BadLocationException e) {
              ToolPlugin.showError("Error Opening class component", e);
            }
            return;
          }
        } else if (data instanceof ToolWindowAttribute){
          showWindowEditor((ToolWindowAttribute)data);
          return;
        } else if (data instanceof ToolAttribute){
          AttributeDialog.show((ToolAttribute)data);
        }else if (data instanceof ToolConstant){
          ConstantDialog.show((ToolConstant)data);
        }else if (data instanceof ToolEvent){
          EventDialog.show((ToolEvent)data);
        }else if (data instanceof ToolVirtualAttribute){
          VirtualAttributeDialog.show((ToolVirtualAttribute)data);
        }
      }
    } catch (PartInitException e) {
      ToolPlugin.showError("Error Opening class component", e);
    }
  }
 
  protected void showMethodEditor(ToolMethod method) throws PartInitException{
   
    //IEditorPart editor = IDE.openEditor(page, method, MethodEditor.EDITOR_ID);
   
   
    IEditorInput ip = new MethodEditorInput(method);
    IEditorPart editor = IDE.openEditor(page, ip, MethodEditor.EDITOR_ID);
   
   
//    IEditorInput ip = new MethodEditorInput(method);
//    IEditorPart editor = IDE.openEditor(page, ip, ToolEditor.EDITOR_ID);
//   
//    ((ToolEditor)editor).setVisibleRegion(method.getMethodSourceRegion(true));
   
  }
  protected void showWindowEditor(ToolWindowAttribute attribute) throws PartInitException{
    IFile windowFile = ((ToolClass)attribute.getParent()).getWindowFile();
    IEditorPart editor = IDE.openEditor(page, windowFile, "tool.editors.windowEditor");
  }
}
TOP

Related Classes of tool.action.cdf.OpenClassComponentAction

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.