autobind.java

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

JAVA
254
字号
/*
 * $Header: /home/cvs/WEBPUMP2.0/WebPumpIDE_Src/WebPumpIDE/src/com/webpump/ui/bind/action/AutoBind.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.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.ui.texteditor.ITextEditor;
import org.eclipse.ui.IEditorInput;
import com.webpump.ui.bind.editors.XMLEditor;
import com.webpump.ui.perspective.MacroResource;

/**
 * Class for the action of BindHtmlBody which is contributed to the BindEditor.
 * 
 * @author zhang_tx
 * @version 2.0.0 2004-2-13
 */
public class AutoBind 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;
	}
    
    /**
     * 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);	
			boolean header = false;
			boolean style = false;
			boolean script = false;
			boolean body = false;
			boolean tail = false;
			try {
				header = searchForTag(MacroResource.BINDEDITOR_ACTION_HEADERHEAD,MacroResource.BINDEDITOR_ACTION_HEADERTAIL,document);
				style = searchForTag(MacroResource.BINDEDITOR_ACTION_STYLEHEAD,MacroResource.BINDEDITOR_ACTION_STYLETAIL,document);
				script = searchForTag(MacroResource.BINDEDITOR_ACTION_SCRIPTHEAD,MacroResource.BINDEDITOR_ACTION_SCRIPTTAIL,document);;
				body = searchForTag(MacroResource.BINDEDITOR_ACTION_BODYHEAD,MacroResource.BINDEDITOR_ACTION_BODYTAIL,document);;
				tail = searchForTag(MacroResource.BINDEDITOR_ACTION_TAILHEAD,MacroResource.BINDEDITOR_ACTION_TAILTAIL,document);;
			} catch (BadLocationException e) {

				e.printStackTrace();
			}
			replaceTag(document,header,style,script,body,tail);
		}
	}
    
	public void selectionChanged(IAction action, ISelection selection) {
	}
	
	public boolean searchForTag( String head,String tail,IDocument document) throws BadLocationException{
		if(document.search(0,head,true,false,true)!=-1&&
			document.search(0,tail,true,false,true)!=-1){
			return true;
		}else{
			return false;
		}
	}
	
	public void replaceTag(IDocument document,boolean headflag,boolean styleflag,boolean scriptflag,boolean bodyflag,boolean tailflag){
		try {
			if(headflag==false){
				int headoffset = -1;
				int tailoffset = -1;
				int len = 0;
				headoffset = document.search(0,"<head",true,false,true);
				if(headoffset!=-1){
					String editorText = document.get();
					for(int i=headoffset;i<editorText.length();i++){
						if(editorText.substring(i,i+1).equals(">")){
							len = i - headoffset + 1;
							break;
						}
					}
				}
				if(headoffset!=-1){
					document.replace(headoffset,len,MacroResource.BINDEDITOR_ACTION_HEADERHEAD);
				}
				tailoffset = document.search(0,"</head>",true,false,true);
				if(tailoffset != -1){
					document.replace(tailoffset,7,MacroResource.BINDEDITOR_ACTION_HEADERTAIL);
				}
			}
			if(styleflag==false){
				int headoffset = -1;
				int tailoffset = -1;
				int len = 0;
				headoffset = document.search(0,"<style",true,false,true);
				if(headoffset!=-1){
					String editorText = document.get();
					for(int i=headoffset;i<editorText.length();i++){
						if(editorText.substring(i,i+1).equals(">")){
							len = i - headoffset + 1;
							break;
						}
					}
				}
				if(headoffset!=-1){
					document.replace(headoffset,len,MacroResource.BINDEDITOR_ACTION_STYLEHEAD);
				}
				tailoffset = document.search(0,"</style>",true,false,true);
				if(tailoffset != -1){
					document.replace(tailoffset,8,MacroResource.BINDEDITOR_ACTION_STYLETAIL);
				}
			}
			if(scriptflag == false){
				int headoffset = -1;
				int headoffsetb = -1;
				int tailoffset = -1;
				int len = 0;
				int bodyheader = document.search(0,"<body",true,false,true);
				if(bodyheader !=-1){
					headoffset = document.search(bodyheader,"<script",false,false,true);
					String editorText = document.get();
					if(headoffset!=-1){
						for(int i=headoffset;i<editorText.length();i++){
							if(editorText.substring(i,i+1).equals(">")){
								len = i - headoffset + 1;
								break;
							}
						}
						document.replace(headoffset,len,MacroResource.BINDEDITOR_ACTION_SCRIPTHEAD);
					}
					bodyheader = document.search(0,"<body",true,false,true);
					tailoffset = document.search(bodyheader,"</script>",false,false,true);
				}else{
					headoffset = document.search(bodyheader,"<script",false,false,true);
					String editorText = document.get();
					if(headoffset!=-1){
						for(int i=headoffset;i<editorText.length();i++){
							if(editorText.substring(i,i+1).equals(">")){
								len = i - headoffset + 1;
								break;
							}
						}
						document.replace(headoffset,len,MacroResource.BINDEDITOR_ACTION_SCRIPTHEAD);
					}
					tailoffset = document.search(0,"</script>",true,false,true);
				}
				if(tailoffset != -1){
					document.replace(tailoffset,9,MacroResource.BINDEDITOR_ACTION_SCRIPTTAIL);
				}
			}
			if(bodyflag==false){
				int headoffset = -1;
				int tailoffset = -1;
				int len = 0;
				headoffset = document.search(0,"<body",true,false,true);
				String editorText = document.get();
				if(headoffset!=-1){
					for(int i=headoffset;i<editorText.length();i++){
						if(editorText.substring(i,i+1).equals(">")){
							len = i - headoffset + 1;
							break;
						}
					}
				}
				if(headoffset!=-1){
					String separator = System.getProperty("line.separator");
					String replace = separator + MacroResource.BINDEDITOR_ACTION_BODYHEAD+separator;
					document.replace(headoffset+len,0,replace);
				}
				tailoffset = document.search(0,"</body>",true,false,true);
				if(tailoffset != -1){
					document.replace(tailoffset,7,MacroResource.BINDEDITOR_ACTION_BODYTAIL);
				}
			}
			if(tailflag == false){
				int headoffset = -1;
				int headoffsetb = -1;
				int tailoffset = -1;
				int len = 0;
				int bodytailer = document.search(0,MacroResource.BINDEDITOR_ACTION_BODYTAIL,true,false,true);
				if(bodytailer !=-1){
					headoffset = document.search(bodytailer,"<script",true,false,true);
					String editorText = document.get();
					if(headoffset!=-1){
						for(int i=headoffset;i<editorText.length();i++){
							if(editorText.substring(i,i+1).equals(">")){
								len = i - headoffset + 1;
								break;
							}
						}
						document.replace(headoffset,len,MacroResource.BINDEDITOR_ACTION_TAILHEAD);
					}
					bodytailer = document.search(0,MacroResource.BINDEDITOR_ACTION_BODYTAIL,true,false,true);
					tailoffset = document.search(bodytailer,"</script>",true,false,true);
				}
				if(tailoffset != -1){
					document.replace(tailoffset,9,MacroResource.BINDEDITOR_ACTION_TAILTAIL);
				}
			}
			
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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