bindhtmlscript.java

来自「一个eclipse插件源代码。用于web开发」· Java 代码 · 共 227 行

JAVA
227
字号
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/bind/action/BindHtmlScript.java,v 1.1.1.1 2004/07/01 09:07:40 wang_j Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2004/07/01 09:07:40 $
 *
 * ====================================================================
 *
 * The NanJing HopeRun(IT-FOREST) Software License, Version 2.0.0
 *
 * Copyright 2003-2004 by NanJing HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and
 *                        IT Forest Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with
 * HopeRun(IT-FOREST) Information System Co., Ltd, CHINA and IT Forest Corporation.
 */
package com.webpump.ui.bind.action;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import com.webpump.ui.bind.editors.BindEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.IEditorInput;
import com.webpump.ui.bind.editors.XMLEditor;
import com.webpump.ui.perspective.MacroResource;
import com.webpump.ui.perspective.WebpumpIDEPlugin;

/**
 * Class for the action of BindHtmlBody which is contributed to the BindEditor.
 * 
 * @author zhang_tx
 * @version 2.0.0 2004-2-13
 */
public class BindHtmlScript implements IEditorActionDelegate {
	
	/** the editor to which is this action contributed */
	private BindEditor m_objeditor;
	/** the action object itself */
	public static IAction m_objaction;

	/**
	 * method which will be called when the UI button of this action is being created
	 *
	 * @function:1.set the active BindEditor to which the action  is now contributed
	 *           2.to set the original state of the UI button of this action according
	 *             to the text of the editor
	 * 
	 * @param IAction aciton   the action object itself
	 * @param IEditorPart targetEditor   the editor to which is this action contributed
	 */
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		
		/** to set the active BindEditor to which the action is now contributed */
		m_objeditor = (targetEditor instanceof BindEditor) ? (BindEditor) targetEditor : null;
		/** the member editor of the editor which is editted by the user */
		XMLEditor xmleditor = null;
		/** to get the xmleditor object */
		if (m_objeditor!=null)
			xmleditor = m_objeditor.getXMLEditor(); 
		
		if(xmleditor!=null){
			m_objaction = action;
			/** to get the document of the xmleditor */
			IDocumentProvider provider= ((ITextEditor) xmleditor).getDocumentProvider();
			IEditorInput input = xmleditor.getEditorInput();
			IDocument document = provider.getDocument(input);
			
			/** to search for the bind tag header of the action 
			 *  if finded, then search for the bind tag tail of the action
			 *  if not, then activate the UI button of this action 
			 */
			int searchtag = -1;
			try {
				searchtag = document.search(0,"<!--WebPumpTools[Script] -->",true,false,true);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
			if(searchtag != -1){
				/** to search for the bind tag header of the action 
				 *  if finded, then set the UI button of this action cannot be triggered
				 *  if not, then activate the UI button
				 */
				int tailsearchtag = -1;
				try {
					tailsearchtag = document.search(0,"<!--End of WebPumpTools[Script] -->",true,false,true);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
				if(tailsearchtag != -1){
					action.setEnabled(false);
				}else{
					action.setEnabled(true);
				}
			}else{
				action.setEnabled(true);
			}
		}
	}
    
	/**
	 * method which will be called when the action's UI button is clicked by the user
	 *
	 * @param IAction aciton   the action object itself
	 */
	public void run(IAction action) {
		/** the member editor of the editor which is editted by the user */
		XMLEditor xmleditor = null;
		/** to get the xmleditor object */
		if (m_objeditor!=null)
			xmleditor = m_objeditor.getXMLEditor();
		 
		if(xmleditor!=null){
			/** the text which is now editted in the xmleditor */
			String editorText =
				xmleditor.getDocumentProvider().getDocument(m_objeditor.getEditorInput()).get();
			/** to get the document of the xmleditor */
			IDocumentProvider provider= ((ITextEditor) xmleditor).getDocumentProvider();
			IEditorInput input= xmleditor.getEditorInput();
			IDocument document= provider.getDocument(input);
			
			/** to get the cursorposition in the xmleditor */
			TextSelection selectedText = (TextSelection)xmleditor.getEditorSite().getSelectionProvider().getSelection();
			String strSelectedText = selectedText.getText();
			//cursorposition
			int cursorpos = selectedText.getOffset();
			int newcursorpos = cursorpos + 29;
			int sellen = strSelectedText.length();
			/** if the place is fit for adding bind tag, then add it
			 *  if not, then use a messagedialog to tell the user cannot add the tag
			 */        
			int endsearchtag = -1;
			String tagetkey = "";
			for(int i = 0;cursorpos + sellen + i < editorText.length();i++){
				if(cursorpos + sellen +i+4 < editorText.length()){
					/** to search for the first bind tag begin with "<!--" after the cursorposition */
					String taget = editorText.substring(cursorpos + sellen +i,cursorpos + sellen +i+4);
					if(taget.compareTo("<!--") == 0){
						endsearchtag = cursorpos + sellen +i+4;
						if(endsearchtag != -1){
							
							/** to search for the tagkey that can make us sure 
							 *  of the position of the cursor, then with it, 
							 *  we can decide if it is fit for add the bind tag
							 */
							String tagkey =editorText.substring(endsearchtag,endsearchtag+4);
							/** the cursorposition is just fit for add, so add the bind tag */
							if(tagkey.compareTo("WebP") == 0){
								String c = "<!--WebPumpTools[Script] -->\n" + strSelectedText +  "\n<!--End of WebPumpTools[Script] -->\n";
								if(cursorpos > 0&&!editorText.substring(cursorpos-1,cursorpos).equals("\n")){
									c = "\n" + c;
								}
								try {
									action.setEnabled(false);
									document.replace(cursorpos,sellen,c);
									TextSelection newpos = new TextSelection(document,newcursorpos,0);
									xmleditor.getEditorSite().getSelectionProvider().setSelection(newpos);
								} catch (BadLocationException e) {
								}
								break;
							}
							/** the cursor is inside of the DrawScene bind area, cannot add the bind tag */
							if(tagkey.compareTo("WEBP") == 0){
								MessageDialog.openInformation(
									m_objeditor.getEditorSite().getPage().getWorkbenchWindow().getShell(),
									(WebpumpIDEPlugin.getResourceString(MacroResource.BINDEDITOR_ACTION_WARNING)),
									(WebpumpIDEPlugin.getResourceString(MacroResource.BINDEDITOR_ACTION_WARNING_TEXT)));
								break;
							}
							/** the cursor is inside of the DrawScene bind area, cannot add the bind tag */
							if(tagkey.compareTo("/WEB") == 0){
								MessageDialog.openInformation(
									m_objeditor.getEditorSite().getPage().getWorkbenchWindow().getShell(),
									(WebpumpIDEPlugin.getResourceString(MacroResource.BINDEDITOR_ACTION_WARNING)),
									(WebpumpIDEPlugin.getResourceString(MacroResource.BINDEDITOR_ACTION_WARNING_TEXT)));
								break;
							}
							/** the cursor is inside of the other bind areas, cannot add the bind tag */
							if(tagkey.compareTo("End ") == 0){
								MessageDialog.openInformation(
									m_objeditor.getEditorSite().getPage().getWorkbenchWindow().getShell(),
									(WebpumpIDEPlugin.getResourceString(MacroResource.BINDEDITOR_ACTION_WARNING)),
									(WebpumpIDEPlugin.getResourceString(MacroResource.BINDEDITOR_ACTION_WARNING_TEXT)));
								break;
							}
							if(!tagkey.equals("WEBP")&&!tagkey.equals("/WEB")&&!tagkey.equals("WebP")&&!tagkey.equals("End ")){
								endsearchtag = -1;
							}
						}
						break;
					}
				}else{
					break;
				}

			}
			/** the cursorposition is just fit for add, so add the bind tag */
			if(endsearchtag == -1){
				String c = "<!--WebPumpTools[Script] -->\n"+ strSelectedText +"\n<!--End of WebPumpTools[Script] -->\n";
				if(cursorpos > 0&&!editorText.substring(cursorpos-1,cursorpos).equals("\n")){
					c = "\n" + c;
				}
				try {
					action.setEnabled(false);
					document.replace(cursorpos,sellen,c);
					TextSelection newpos = new TextSelection(document,newcursorpos,0);
					xmleditor.getEditorSite().getSelectionProvider().setSelection(newpos);
				} catch (BadLocationException e) {
				}
			}
		}
	}
    
	public void selectionChanged(IAction action, ISelection selection) {
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?