Package de.innovationgate.eclipse.editors.helpers

Source Code of de.innovationgate.eclipse.editors.helpers.LabelTagReplaceHandler

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

import java.text.ParseException;

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.text.ITextSelection;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;

import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.tml.TMLPartitionScanner;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
import de.innovationgate.eclipse.editors.tmlscript.TMLScriptPartitionScanner;

public class LabelTagReplaceHandler {
  private FindReplaceDocumentAdapter _findreplace;
  private IDocument _doc;
  private IRegion _currentSelection = null;
  private String _labelFileName;
  private ITextEditor _editor;
  private boolean _hasNext;
  private boolean _hasPrev;
 
 
 
  public boolean hasNext(){
    return _hasNext;
  }
 
  public boolean hasPrev(){
    return _hasPrev;
  }
 

  public LabelTagReplaceHandler(IDocument doc, String labelFileName) {
    super();
    _doc = doc;
    _labelFileName = labelFileName;
    _findreplace = new FindReplaceDocumentAdapter(doc);
    IEditorPart editorPart = Plugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (editorPart != null && editorPart instanceof ITextEditor) {
      _editor = (ITextEditor) editorPart;
    }
    ITextSelection selection = ((ITextSelection) _editor.getEditorSite().getSelectionProvider().getSelection());
    _currentSelection = new Region(selection.getOffset(), selection.getLength());
   
   
   
  }

  public String getLabelFileName() {
    return _labelFileName;
  }

  public void setLabelFileName(String labelFileName) {
    _labelFileName = labelFileName;
  }
 
 
  public void selectString(String string, boolean forward) throws BadLocationException {
    int direction ;   
    if(forward){
      direction=1;
   
    }
    else
      direction=0;
    }       

      _currentSelection = _findreplace.find(_currentSelection.getOffset()+(_currentSelection.getLength()*direction), string, forward, true, true, false);   
     
     
     
     
     
      if(_currentSelection != null){
        _editor.selectAndReveal(_currentSelection.getOffset(), _currentSelection.getLength())
                         
          try {
            if(_findreplace.find(_currentSelection.getOffset()+_currentSelection.getLength(), string, true, true, true, false) == null){
              _hasNext = false;
            }else{
              _hasNext = true;
            }
          } catch (BadLocationException e) {
            _hasNext = false;
         
         
          try {
            if(_findreplace.find(_currentSelection.getOffset(), string, false, true, true, false) == null){
              _hasPrev = false;
            }else{
              _hasPrev = true;
            }
          } catch (BadLocationException e) {
            _hasPrev = false;
          }         
 
     
      }
     
     
     
     
   

  }

 


  public void replace(String key) throws BadLocationException {
    ITextSelection selection = ((ITextSelection) _editor.getEditorSite().getSelectionProvider().getSelection());
    String tmlLabelTag = getRefactorString(key);
    _doc.replace(selection.getOffset(), selection.getLength(), tmlLabelTag);
    _editor.selectAndReveal(_currentSelection.getOffset(), tmlLabelTag.length())
   
  }


  public String getRefactorString(String key) throws BadLocationException {
    ITextSelection textSelection = ((ITextSelection) _editor.getEditorSite().getSelectionProvider().getSelection());
    ITypedRegion region = _doc.getPartition(textSelection.getOffset());

    String tmlLabelTag = "";

    if (region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE)) {
      // generats tmltag
      tmlLabelTag = "<tml:label";
      if (!_labelFileName.equals("general")) {
        tmlLabelTag += " file=\"" + _labelFileName + "\"";
      }
      tmlLabelTag += " key=\"" + key + "\"/>";
    }

    if (region.getType().equals(TMLPartitionScanner.TML_TAG_START)) {

      try {
        TMLRegion tmlRegion = TMLRegion.parse(region, _doc, textSelection.getOffset());

        if (tmlRegion.isCursorInAttributeValue()) {
          tmlLabelTag = "{label(\'" + key + "\')}";
        }

        if (tmlRegion.isCursorInTagName()) {

          selectString(key,true);
          return null;
        }

      } catch (ParseException e) {
        Plugin.getDefault().logInfo("Cant parse region " + region.getType(), e);
      }
    }

    if (region.getType().equals(TMLScriptPartitionScanner.TMLSCRIPT)) {
      tmlLabelTag = "label(\"" + key + "\");";

    }
    return tmlLabelTag;
  }

}
TOP

Related Classes of de.innovationgate.eclipse.editors.helpers.LabelTagReplaceHandler

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.