Package com.google.eclipse.protobuf.ui.commands

Source Code of com.google.eclipse.protobuf.ui.commands.SmartInsertHandler

/*
* Copyright (c) 2011 Google Inc.
*
* 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
*/
package com.google.eclipse.protobuf.ui.commands;

import static org.eclipse.xtext.ui.editor.utils.EditorUtils.getActiveXtextEditor;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;
import org.eclipse.xtext.ui.editor.XtextEditor;

/**
* Base class for command handlers that insert content in an editor.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public abstract class SmartInsertHandler extends AbstractHandler {
  /** {@inheritDoc} */
  @Override public final Object execute(ExecutionEvent event) {
    XtextEditor activeEditor = getActiveXtextEditor();
    if (activeEditor != null) {
      insertContent(activeEditor, styledTextFrom(activeEditor));
    }
    return null;
  }

  private StyledText styledTextFrom(XtextEditor editor) {
    Object adapter = editor.getAdapter(Control.class);
    return (adapter instanceof StyledText) ? (StyledText) adapter : null;
  }

  protected abstract void insertContent(XtextEditor editor, StyledText styledText);
}
TOP

Related Classes of com.google.eclipse.protobuf.ui.commands.SmartInsertHandler

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.