FileDocCategorySizeDatePackage
LoggerView.javaAPI DocAzureus 3.0.3.418148Fri Jun 15 11:39:34 BST 2007org.gudy.azureus2.ui.swt.views

LoggerView

public class LoggerView extends AbstractIView implements ILogEventListener, org.gudy.azureus2.core3.config.ParameterListener
author
TuxPaper
since
2.3.0.5

Fields Summary
private static final int
COLOR_INFO
private static final int
COLOR_WARN
private static final int
COLOR_ERR
private static org.eclipse.swt.graphics.Color[]
colors
private static final int
PREFERRED_LINES
private static final int
MAX_LINES
private static final SimpleDateFormat
dateFormatter
private static final FieldPosition
formatPos
private Display
display
private Composite
panel
private org.eclipse.swt.custom.StyledText
consoleText
private Button
buttonAutoScroll
private Object[]
filter
private LinkedList
buffer
private boolean
bPaused
private boolean
bRealtime
private boolean
bEnabled
private boolean
bAutoScroll
private ArrayList[]
ignoredComponents
Constructors Summary
public LoggerView()


	 
		dateFormatter = new SimpleDateFormat("[h:mm:ss.SSS] ");
		formatPos = new FieldPosition(0);
	
		for (int i = 0; i < ignoredComponents.length; i++) {
			ignoredComponents[i] = new ArrayList();
		}
	
public LoggerView(List initialList)

		this();
		if (initialList != null)
			buffer.addAll(initialList);
		setEnabled(true);
	
Methods Summary
private voidclearConsole()

		if (display == null || display.isDisposed())
			return;

		Utils.execSWTThread(new AERunnable() {
			public void runSupport() {
				consoleText.setText("");
			}
		});
	
public voiddataSourceChanged(java.lang.Object newDataSource)

		boolean bEnable = newDataSource != null;
		if (bEnable) {
			if (newDataSource instanceof Object[])
				setFilter((Object[]) newDataSource);
			else
				setFilter(new Object[] { newDataSource });
		}

		setEnabled(bEnable);
	
public voiddelete()

		Logger.removeListener(this);
		if (panel != null && !panel.isDisposed())
			panel.dispose();
		Colors.getInstance().removeColorsChangedListener(this);
	
public CompositegetComposite()

		return panel;
	
public java.lang.StringgetData()

		return "ConsoleView.title.short";
	
public java.lang.StringgetFullTitle()

		return MessageText.getString("ConsoleView.title.full");
	
public java.lang.StringgetPluginViewName()

		return "Console";
	
private intindexToLogType(int index)

		switch (index) {
			case 0:
				return LogEvent.LT_INFORMATION;
			case 1:
				return LogEvent.LT_WARNING;
			case 2:
				return LogEvent.LT_ERROR;
		}
		return LogEvent.LT_INFORMATION;
	
public voidinitialize(Composite composite)

		display = composite.getDisplay();

		Colors.getInstance().addColorsChangedListener(this);
		parameterChanged("Color");

		panel = new Composite(composite, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.verticalSpacing = 2;
		layout.numColumns = 2;
		panel.setLayout(layout);

		GridData gd;

		consoleText = new StyledText(panel, SWT.READ_ONLY | SWT.V_SCROLL
				| SWT.H_SCROLL);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		consoleText.setLayoutData(gd);

		// XXX This doesn't work well, but it's better than nothing
		consoleText.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				GC gc = new GC(consoleText);
				int charWidth = gc.getFontMetrics().getAverageCharWidth();
				gc.dispose();

				int areaWidth = consoleText.getBounds().width;
				consoleText.setTabs(areaWidth / 6 / charWidth);
			}
		});

		ScrollBar sb = consoleText.getVerticalBar();
		sb.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				bAutoScroll = false;
				if (buttonAutoScroll != null && !buttonAutoScroll.isDisposed())
					buttonAutoScroll.setSelection(false);
			}
		});

		Composite cLeft = new Composite(panel, SWT.NULL);
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.verticalSpacing = 1;
		cLeft.setLayout(layout);
		gd = new GridData(SWT.TOP, SWT.LEAD, false, false);
		cLeft.setLayoutData(gd);

		Button buttonPause = new Button(cLeft, SWT.CHECK);
		Messages.setLanguageText(buttonPause, "LoggerView.pause");
		gd = new GridData();
		buttonPause.setLayoutData(gd);
		buttonPause.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (e.widget == null || !(e.widget instanceof Button))
					return;
				Button btn = (Button) e.widget;
				bPaused = btn.getSelection();
				if (!bPaused && buffer != null) {
					refresh();
				}
			}
		});

		Button buttonRealtime = new Button(cLeft, SWT.CHECK);
		Messages.setLanguageText(buttonRealtime, "LoggerView.realtime");
		gd = new GridData();
		buttonRealtime.setLayoutData(gd);
		buttonRealtime.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (e.widget == null || !(e.widget instanceof Button))
					return;
				Button btn = (Button) e.widget;
				bRealtime = btn.getSelection();
			}
		});

		buttonAutoScroll = new Button(cLeft, SWT.CHECK);
		Messages.setLanguageText(buttonAutoScroll, "LoggerView.autoscroll");
		gd = new GridData();
		buttonAutoScroll.setLayoutData(gd);
		buttonAutoScroll.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (e.widget == null || !(e.widget instanceof Button))
					return;
				Button btn = (Button) e.widget;
				bAutoScroll = btn.getSelection();
			}
		});
		buttonAutoScroll.setSelection(true);

		Button buttonClear = new Button(cLeft, SWT.PUSH);
		Messages.setLanguageText(buttonClear, "LoggerView.clear");
		gd = new GridData();
		buttonClear.setLayoutData(gd);
		buttonClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				consoleText.setText("");
			}
		});

		/** FileLogging filter, consisting of a List of types (info, warning, error)
		 * and a checkbox Table of component IDs.
		 */
		final String sFilterPrefix = "ConfigView.section.logging.filter";
		Group gLogIDs = new Group(panel, SWT.NULL);
		Messages.setLanguageText(gLogIDs, "LoggerView.filter");
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.numColumns = 2;
		gLogIDs.setLayout(layout);
		gd = new GridData();
		gLogIDs.setLayoutData(gd);

		Label label = new Label(gLogIDs, SWT.NONE);
		Messages.setLanguageText(label, "ConfigView.section.logging.level");
		label.setLayoutData(new GridData());

		final Label labelCatFilter = new Label(gLogIDs, SWT.NONE);
		labelCatFilter.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

		final List listLogTypes = new List(gLogIDs, SWT.BORDER | SWT.SINGLE
				| SWT.V_SCROLL);
		gd = new GridData(SWT.NULL, SWT.BEGINNING, false, false);
		listLogTypes.setLayoutData(gd);

		final int[] logTypes = { LogEvent.LT_INFORMATION, LogEvent.LT_WARNING,
				LogEvent.LT_ERROR };
		for (int i = 0; i < logTypes.length; i++)
			listLogTypes.add(MessageText.getString("ConfigView.section.logging.log"
					+ i + "type"));
		listLogTypes.select(0);

		final LogIDs[] logIDs = FileLogging.configurableLOGIDs;
		//Arrays.sort(logIDs);

		Composite cChecksAndButtons = new Composite(gLogIDs, SWT.NULL);
		layout = new GridLayout(2, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		cChecksAndButtons.setLayout(layout);
		cChecksAndButtons.setLayoutData(new GridData());

		final Composite cChecks = new Composite(cChecksAndButtons, SWT.NULL);
		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
		rowLayout.wrap = true;
		rowLayout.marginLeft = 0;
		rowLayout.marginRight = 0;
		rowLayout.marginTop = 0;
		rowLayout.marginBottom = 0;
		cChecks.setLayout(rowLayout);

		SelectionAdapter buttonClickListener = new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int index = listLogTypes.getSelectionIndex();
				if (index < 0 || index >= logTypes.length)
					return;
				Button item = (Button) e.widget;
				if (item.getSelection())
					ignoredComponents[index].remove(item.getData("LOGID"));
				else
					ignoredComponents[index].add(item.getData("LOGID"));
			}
		};
		for (int i = 0; i < logIDs.length; i++) {
			Button btn = new Button(cChecks, SWT.CHECK);
			btn.setText(MessageText.getString(sFilterPrefix + "." + logIDs[i],
					logIDs[i].toString()));

			btn.setData("LOGID", logIDs[i]);

			btn.addSelectionListener(buttonClickListener);

			if (i == 0) {
				gd = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 2);
				gd.heightHint = (btn.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + 2) * 3;
				cChecks.setLayoutData(gd);
			}
		}

		// Update table when list selection changes
		listLogTypes.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int index = listLogTypes.getSelectionIndex();
				if (index < 0 || index >= logTypes.length)
					return;

				labelCatFilter.setText(MessageText.getString(
						"ConfigView.section.logging.showLogsFor", listLogTypes
								.getSelection()));

				Control[] items = cChecks.getChildren();
				for (int i = 0; i < items.length; i++) {
					if (items[i] instanceof Button) {
						LogIDs ID = (LogIDs) items[i].getData("LOGID");
						if (ID != null) {
							boolean checked = !ignoredComponents[index].contains(ID);
							((Button) items[i]).setSelection(checked);
						}
					}
				}
			}
		});

		listLogTypes.notifyListeners(SWT.Selection, null);

		Button btn;
		btn = new Button(cChecksAndButtons, SWT.PUSH);
		gd = new GridData();
		btn.setLayoutData(gd);
		Messages.setLanguageText(btn, "LoggerView.filter.checkAll");
		btn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int index = listLogTypes.getSelectionIndex();

				Control[] items = cChecks.getChildren();
				for (int i = 0; i < items.length; i++) {
					if (items[i] instanceof Button) {
						LogIDs ID = (LogIDs) items[i].getData("LOGID");
						if (ID != null && ignoredComponents[index].contains(ID)) {
							((Button) items[i]).setSelection(true);
							ignoredComponents[index].remove(ID);
						}
					}
				}
			}
		});

		btn = new Button(cChecksAndButtons, SWT.PUSH);
		gd = new GridData();
		btn.setLayoutData(gd);
		Messages.setLanguageText(btn, "LoggerView.filter.uncheckAll");
		btn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int index = listLogTypes.getSelectionIndex();

				Control[] items = cChecks.getChildren();
				for (int i = 0; i < items.length; i++) {
					if (items[i] instanceof Button) {
						LogIDs ID = (LogIDs) items[i].getData("LOGID");
						if (ID != null && !ignoredComponents[index].contains(ID)) {
							((Button) items[i]).setSelection(false);
							ignoredComponents[index].add(ID);
						}
					}
				}
			}
		});

		if (!Logger.isEnabled()) {
			consoleText.setText(MessageText.getString("LoggerView.loggingDisabled")
					+ "\n");
		}
	
public synchronized voidlog(LogEvent event)

		if (display == null || display.isDisposed())
			return;

		if (ignoredComponents[logTypeToIndex(event.entryType)]
				.contains(event.logID))
			return;

		// Always display STDERR messages, as they may relate to the filter 
		boolean bMatch = (event.logID == LogIDs.STDERR || filter == null);

		if (!bMatch && event.relatedTo != null) {
			for (int i = 0; !bMatch && i < event.relatedTo.length; i++) {
				Object obj = event.relatedTo[i];

				if (obj == null)
					continue;

				for (int j = 0; !bMatch && j < filter.length; j++) {
					if (obj instanceof LogRelation) {
						//System.err.println(obj.getClass().getSimpleName() + " is Logrelation");

						Object newObj = ((LogRelation) obj).queryForClass(filter[j]
								.getClass());
						if (newObj != null)
							obj = newObj;
					}

					//System.err.println(obj.getClass().getName() + " matches " + filter[j].getClass().getSimpleName() + "?");

					if (obj == filter[j])
						bMatch = true;
				} // for filter
			} // for relatedTo
		}

		if (bMatch) {
			synchronized (buffer) {
				if (buffer.size() >= 200)
					buffer.removeFirst();
				buffer.add(event);
			}

			if (bRealtime && !bPaused) {
				Utils.execSWTThread(new AERunnable() {
					public void runSupport() {
						refresh();
					}
				});
			}
		}
	
private intlogTypeToIndex(int entryType)

		switch (entryType) {
			case LogEvent.LT_INFORMATION:
				return 0;
			case LogEvent.LT_WARNING:
				return 1;
			case LogEvent.LT_ERROR:
				return 2;
		}
		return 0;
	
public voidparameterChanged(java.lang.String parameterName)

		if (parameterName.startsWith("Color")) {
			Utils.execSWTThread(new AERunnable() {
				public void runSupport() {
					if (display == null || display.isDisposed())
						return;

					if (colors == null)
						colors = new Color[3];

					final Color[] newColors = { Colors.blues[Colors.BLUES_MIDLIGHT],
							Colors.colorWarning, Colors.red_ConsoleView };
					boolean bColorChanged = false;

					for (int i = 0; i < newColors.length; i++) {
						if (colors[i] == null || colors[i].isDisposed()) {
							colors[i] = newColors[i];
							bColorChanged = true;
						}
					}

					if (bColorChanged && consoleText != null) {
						// remove color
						String text = consoleText.getText();
						consoleText.setText(text);
					}
				}
			});
		}
	
public voidrefresh()

		if (bPaused)
			return;

		synchronized (buffer) {
			if (consoleText == null || consoleText.isDisposed())
				return;

			for (int i = 0; i < buffer.size(); i++) {
				try {
					LogEvent event = (LogEvent) buffer.get(i);

					int nbLinesBefore = consoleText.getLineCount();
					if (nbLinesBefore > MAX_LINES)
						consoleText.replaceTextRange(0, consoleText
								.getOffsetAtLine(PREFERRED_LINES), "");

					final StringBuffer buf = new StringBuffer();
					dateFormatter.format(event.timeStamp, buf, formatPos);
					buf.append("{").append(event.logID).append("} ");

					buf.append(event.text);
					if (event.relatedTo != null) {
						buf.append("; \t| ");
						for (int j = 0; j < event.relatedTo.length; j++) {
							Object obj = event.relatedTo[j];
							if (j > 0)
								buf.append("; ");
							if (obj instanceof LogRelation) {
								buf.append(((LogRelation) obj).getRelationText());
							} else if (obj != null) {
								buf.append(obj.getClass().getName()).append(": '").append(
										obj.toString()).append("'");
							}
						}
					}
					buf.append('\n");

					consoleText.append(buf.toString());

					int nbLinesNow = consoleText.getLineCount();
					int colorIdx = -1;
					if (event.entryType == LogEvent.LT_INFORMATION)
						colorIdx = COLOR_INFO;
					else if (event.entryType == LogEvent.LT_WARNING)
						colorIdx = COLOR_WARN;
					else if (event.entryType == LogEvent.LT_ERROR)
						colorIdx = COLOR_ERR;

					if (colors != null && colorIdx >= 0)
						consoleText.setLineBackground(nbLinesBefore - 1, nbLinesNow
								- nbLinesBefore, colors[colorIdx]);
				} catch (Exception e) {
					// don't send it to log, we might be feeding ourselves
					PrintStream ps = Logger.getOldStdErr();
					if (ps != null) {
						ps.println("Error writing event to console:");
						e.printStackTrace(ps);
					}
				}

			}
			buffer.clear();
			if (bAutoScroll)
				consoleText.setSelection(consoleText.getText().length());
		}
	
public voidsetEnabled(boolean on)

		if (bEnabled == on)
			return;
		bEnabled = on;
		if (on)
			Logger.addListener(this);
		else
			Logger.removeListener(this);
	
public voidsetFilter(java.lang.Object[] _filter)

		filter = _filter;
		clearConsole();