JTextArea中实现批量添加、删除注释功能

用JTextArea写了简单的sql编辑器,实现了自动批量增加、删除注释功能,代码如下:

private void addDeleteZhuShi(){
		
		//1. 获得选中文本
		String selection = jTextArea1.getSelectedText();

		//2. 没有选中文本的场景
		if (selection == null) {
			
			//获得当前光标位置
			int cur = jTextArea1.getCaretPosition();
			
			//2.1 如果光标在文本域的起始位置
			if(cur == 0){
				//判断当前光标所在行是否以“--”或“//”开头,如果以注释符开头,则删除注释符,如果不以注释符开头,则插入注释符。
				String text = jTextArea1.getText();
				if(text.startsWith("--") || text.startsWith("//")){
					jTextArea1.replaceRange("", 0, 2);
				}else{								
					jTextArea1.insert("--", 0);
				}
			//2.2  如果光标不在起始位置
			}else{
				// 获得光标之前所有文本信息
				String preText = null;
				try {
					preText = jTextArea1.getText(0, cur);
				} catch (BadLocationException e) {
					if("true".equals(ConfigUtil.getConfInfo().get(Const.IS_LOG)+"")){				
						log.error(null, e);
					}
					return;
				}
				//2.2.1  如果光标之前包含有换行符
				if (preText.contains("\n")) {
					
					//获得光标所在行的前2个起始字符
					String pre2char = null;
					try {
						pre2char = jTextArea1.getText(preText.lastIndexOf("\n") + 1, 2);
					} catch (BadLocationException e) {
						if("true".equals(ConfigUtil.getConfInfo().get(Const.IS_LOG)+"")){				
							log.error(null, e);
						}
						return;
					}
					//如果光标所在行以注释符开头
					if("--".equals(pre2char) || "//".equals(pre2char)){
						jTextArea1.replaceRange("", preText.lastIndexOf("\n") + 1, preText.lastIndexOf("\n") + 1 + 2);
					
						//否则,在该位置之后的第一行之前插入注释符。
					}else{	
						jTextArea1.insert("--", preText.lastIndexOf("\n") + 1);
					}
				//2.2.2  如果选中文本之前不存在换行符的场景,即当前光标在第一行的场景
				} else {
					String text = jTextArea1.getText();
					
					//删除文本域起始注释符
					if(text.startsWith("--") || text.startsWith("//")){
						jTextArea1.replaceRange("", 0, 2);
					}else{					
						//直接在文本域起始位置插入注释符
						jTextArea1.insert("--", 0);
					}
				}
			}
		}	
		//3. 有选中文本的场景
		else {
			// 对选中文本按行“\n”分割
			String[] lines = selection.split("\n");
			
			//3.1  选中文本不包含换行符的场景,即只选择一行的场景
			if(lines.length == 1){
				
				int selectionStart = jTextArea1.getSelectionStart();

				// 获得光标之前最后一个 \n 的位置,在该位置之后的第一行之前插入注释符。
				String preText = null;
				if(selectionStart == 0){//文本域首字符被选中的场景
					preText = "";
				}else{			
					try {
						preText = jTextArea1.getText(0, selectionStart);//获得选中文本之前的数据
					} catch (BadLocationException e) {
						if("true".equals(ConfigUtil.getConfInfo().get(Const.IS_LOG)+"")){				
							log.error(null, e);
						}
						return;
					}
				}
				//3.1.1 选中文本前的文本包含换行符的场景,即所选文本不是第一行的场景。
				if (preText.contains("\n")) {
					
					//获得当前行的前2个起始字符
					String pre2char = null;
					try {
						pre2char = jTextArea1.getText(preText.lastIndexOf("\n") + 1, 2);
					} catch (BadLocationException e) {
						if("true".equals(ConfigUtil.getConfInfo().get(Const.IS_LOG)+"")){				
							log.error(null, e);
						}
					}
					//如果光标所在行以注释符开头
					if("--".equals(pre2char) || "//".equals(pre2char)){
						jTextArea1.replaceRange("", preText.lastIndexOf("\n") + 1, preText.lastIndexOf("\n") + 1 + 2);
					}else{					
						jTextArea1.insert("--", preText.lastIndexOf("\n") + 1);
					}
				//3.1.2  如果选中文本之前不存在换行符的场景,即当前是第一行的场景
				} else {
					String text = jTextArea1.getText();
					
					//删除文本域起始注释符
					if(text.startsWith("--") || text.startsWith("//")){
						jTextArea1.replaceRange("", 0, 2);
					}else{	
						//文本域起始位置插入注释符
						jTextArea1.insert("--", 0);
					}
				}
				
			//3.2   选中文本包含换行符的场景
			}else{
				//1、首先处理选中文本的第一行,如果第一行以注释符开头,则都执行删除注释操作,否则,都执行添加注释操作。
				boolean isStartsWithZhuShi = false;
				int selectionStart = jTextArea1.getSelectionStart();
				int selectionEnd = jTextArea1.getSelectionEnd();
				
				// 获得光标之前最后一个 \n 的位置,在该位置之后的第一行之前插入注释符。
				String preText = null;
				if(selectionStart == 0){//文本域首字符被选中的场景
					preText = "";
				}else{			
					try {
						preText = jTextArea1.getText(0, selectionStart);
					} catch (BadLocationException e) {
						if("true".equals(ConfigUtil.getConfInfo().get(Const.IS_LOG)+"")){				
							log.error(null, e);
						}
						return;
					}
				}
				if (preText.contains("\n")) {
					
					//获得选中文本第一行的前2个起始字符
					String pre2char = null;
					try {
						pre2char = jTextArea1.getText(preText.lastIndexOf("\n") + 1, 2);
					} catch (BadLocationException e) {
						if("true".equals(ConfigUtil.getConfInfo().get(Const.IS_LOG)+"")){				
							log.error(null, e);
						}
						return;
					}
					//如果光标所在行以注释符开头
					if("--".equals(pre2char) || "//".equals(pre2char)){
						isStartsWithZhuShi = true;
					}
					if(isStartsWithZhuShi){//处理第一行,删除注释操作
						
						jTextArea1.replaceRange("", preText.lastIndexOf("\n") + 1, preText.lastIndexOf("\n") + 1 + 2);
						
					}else{//处理第一行,添加注释操作
						jTextArea1.insert("--", preText.lastIndexOf("\n") + 1);
					}
					
					// 如果选中文本之前不存在换行符的场景,即当前是第一行的场景
				}else{
					String text = jTextArea1.getText();
					
					//删除文本域起始注释符
					if(text.startsWith("--") || text.startsWith("//")){
						isStartsWithZhuShi = true;
					}
					if(isStartsWithZhuShi){//处理选中文本第一行,删除注释操作
						
						jTextArea1.replaceRange("", 0, 2);
						
					}else{//处理选中文本第一行,添加注释操作
						jTextArea1.insert("--", 0);
					}
				}					
				
				//2、然后处理其他行
				StringBuilder builder = new StringBuilder();
				if(isStartsWithZhuShi){
					if(lines[0].startsWith("--")||lines[0].startsWith("//")){
						lines[0] = lines[0].substring(2);
					}
					builder.append(lines[0]);
					for (int i = 1; i < lines.length; i++){
						builder.append("\n");
						if(lines[i].startsWith("--") || lines[i].startsWith("//")){
							lines[i] = lines[i].substring(2);
						}
						builder.append(lines[i]);
					}
					selectionStart = jTextArea1.getSelectionStart();
					selectionEnd = jTextArea1.getSelectionEnd();
					jTextArea1.replaceRange(builder.toString(), selectionStart, selectionEnd);
					
					//重新选中选择的文本
					selectionEnd = selectionStart + builder.length();
					jTextArea1.setSelectionStart(selectionStart);
					jTextArea1.setSelectionEnd(selectionEnd);			
				
				}else{				
					builder.append(lines[0]);
					for (int i = 1; i < lines.length; i++){
						builder.append("\n");
						builder.append("--").append(lines[i]);
					}
					selectionStart = jTextArea1.getSelectionStart();
					selectionEnd = jTextArea1.getSelectionEnd();
					jTextArea1.replaceRange(builder.toString(), selectionStart, selectionEnd);
					
					//重新选中选择的文本
					selectionEnd = selectionStart + builder.length();
					jTextArea1.setSelectionStart(selectionStart);
					jTextArea1.setSelectionEnd(selectionEnd);			
				}
			}
			}
	}

  效果图:

 

http://huangqiqing123.iteye.com/blog/1391409
 

猜你喜欢

转载自huangqiqing123.iteye.com/blog/1391409