All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.becauseQA.window.ui.jtextpane.JTextPaneUtils Maven / Gradle / Ivy

package com.github.becauseQA.window.ui.jtextpane;

/*-
 * #%L
 * commons-window
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2016 Alter Hu
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.basic.BasicTextAreaUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

import com.github.becauseQA.apache.commons.StringUtils;

public class JTextPaneUtils {

	/*
	 * BoxLayout
	 */
	public static void textAreaFromStream(JTextArea jTextArea, InputStream inputStream) {
		try {
			jTextArea.read(new InputStreamReader(inputStream), null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void contentFromlog4j(JTextPane JTextPane) {
		Log4jMessageAppender statusMessageAppender = new Log4jMessageAppender();
		statusMessageAppender.setJTextPane(JTextPane);
		statusMessageAppender.addFilter(new Filter() {
			@Override
			public int decide(LoggingEvent event) {
				// TODO Auto-generated method stub
				if (event.getLevel().isGreaterOrEqual(Level.DEBUG) || event.getLevel().equals(Level.ERROR)) {
					return ACCEPT;
				} else {
					return DENY;
				}
			}
		});
		LogManager.getRootLogger().addAppender(statusMessageAppender);
	}

	public static void createLog4jOutput(JTextPane jTextPane) {
		jTextPane.setEditable(true);
		// outputTextArea.setRows(100);
		jTextPane.createToolTip().setTipText("Console output");
		// outputTextArea.setLineWrap(true);
		// outputTextArea.setWrapStyleWord(true);

		//jTextPane.setUI(new BasicTextAreaUI());
		 //outputTextArea.setForeground(Color.white);
		jTextPane.setCaretColor(Color.lightGray);
		JTextPaneUtils.contentFromlog4j(jTextPane);

		JPopupMenu popupMenu = new JPopupMenu();
		JMenuItem mntmClearAll = new JMenuItem("Clear All");
		mntmClearAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jTextPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				jTextPane.setText("");
			}
		});
		popupMenu.add(mntmClearAll);

		JSeparator clearSeparator = new JSeparator();
		popupMenu.add(clearSeparator);

		JMenuItem mntmClearSelected = new JMenuItem("Clear Selected");
		mntmClearSelected.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jTextPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				Document document = jTextPane.getDocument();
				String selectedText = jTextPane.getSelectedText();
				if (!StringUtils.isEmpty(selectedText)) {
					String wholeContent = "";
					try {
						wholeContent = document.getText(0, document.getLength());
						String replaceText = wholeContent.replace(selectedText, "");
						jTextPane.setText(replaceText);
					} catch (BadLocationException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		popupMenu.add(mntmClearSelected);
		jTextPane.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popupMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});
		jTextPane.validate();

	}

	protected JScrollPane createOutputArea() {
		JScrollPane textareaScrollPane = new JScrollPane();
		textareaScrollPane.setViewportBorder(
				new TitledBorder(null, "Output", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JTextPane outputTextArea = new JTextPane();
		outputTextArea.setEditable(false);
		// outputTextArea.setRows(100);
		outputTextArea.createToolTip().setTipText("log console");
		// outputTextArea.setLineWrap(true);
		// outputTextArea.setWrapStyleWord(true);

		outputTextArea.setUI(new BasicTextAreaUI());
		// outputTextArea.setForeground(Color.white);
		outputTextArea.setCaretColor(Color.lightGray);
		JTextPaneUtils.contentFromlog4j(outputTextArea);
		textareaScrollPane.setViewportView(outputTextArea);

		JPopupMenu popupMenu = new JPopupMenu();
		outputTextArea.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popupMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});

		JMenuItem mntmClearAll = new JMenuItem("Clear All");
		mntmClearAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				outputTextArea.setText("");
			}
		});
		popupMenu.add(mntmClearAll);

		JSeparator clearSeparator = new JSeparator();
		popupMenu.add(clearSeparator);

		JMenuItem mntmClearSelected = new JMenuItem("Clear Selected");
		mntmClearSelected.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputTextArea.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				String selectedText = outputTextArea.getSelectedText();
				String replaceText = outputTextArea.getText().replace(selectedText, "");
				outputTextArea.setText(replaceText);
			}
		});
		popupMenu.add(mntmClearSelected);
		return textareaScrollPane;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy