/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;
/**
* Displays the messages logged by the application.
*
*/
public class LogView extends ViewPart
{
public static final String ID = "HIDB2.logView";
public final static String[] columnNames =
{
"Level", "Date", "Class", "Method", "Message"
};
public final static int[] columnWidth =
{
40, 100, 100, 100, 250
};
private TableViewer tabv;
private Composite _parent;
static long _t0 = System.currentTimeMillis();
static LogManager _lm = LogManager.getLogManager();
private static int NB_TMP_LOG = 100;
private List<LogRecord> _logBuffer = new LinkedList<LogRecord>();
private List<LogRecord> _tmpLogBuffer = new ArrayList<LogRecord>(NB_TMP_LOG);
private Object _logSem = new Object();
private boolean _endMonitor = false;
Handler _mh = new Handler()
{
/* (non-Javadoc)
* @see java.util.logging.MemoryHandler#publish(java.util.logging.LogRecord)
*/
@Override
public synchronized void publish(LogRecord record)
{
synchronized (_tmpLogBuffer)
{
_tmpLogBuffer.add(record);
}
synchronized (_logSem)
{
_logSem.notify();
}
}
@Override
public void close() throws SecurityException
{
_endMonitor = true;
}
@Override
public void flush()
{
}
};
public void createPartControl(Composite parent)
{
Logger log = Logger.getLogger("");
log.addHandler(_mh);
// MMI Creation
_parent = parent;
Composite top = new Composite(parent, SWT.NONE);
FillLayout layout = new FillLayout();
top.setLayout(layout);
tabv = new TableViewer(top, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
tabv.setContentProvider(new IStructuredContentProvider()
{
@SuppressWarnings("unchecked")
public Object[] getElements(Object inputElement)
{
Object t[] = null;
synchronized (_logBuffer)
{
t = ((List<LogRecord>) inputElement).toArray();
}
return t;
}
public void dispose()
{
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
{
}
});
tabv.setLabelProvider(new ITableLabelProvider()
{
public Image getColumnImage(Object element, int columnIndex)
{
return null;
}
public String getColumnText(Object element, int columnIndex)
{
LogRecord lr = (LogRecord) element;
switch (columnIndex)
{
case 0:
return lr.getLevel().toString();
case 1:
return Long.toString(lr.getMillis() - _t0);
case 2:
return lr.getSourceClassName();
case 3:
return lr.getSourceMethodName();
case 4:
return lr.getMessage();
default:
return null;
}
}
public void addListener(ILabelProviderListener listener)
{
}
public void dispose()
{
}
public boolean isLabelProperty(Object element, String property)
{
return false;
}
public void removeListener(ILabelProviderListener listener)
{
}
});
Table tabExistingChannels = tabv.getTable();
tabExistingChannels.setHeaderVisible(true);
tabExistingChannels.setLinesVisible(true);
// The Editor Input
tabExistingChannels.setHeaderVisible(true);
// Column titles
TableColumn colonne;
// TableSortSelectionListener listener;
for (int indice = 0; indice < columnNames.length; indice++)
{
colonne = new TableColumn(tabExistingChannels, (indice == 4) ? SWT.LEFT : SWT.CENTER);
colonne.setText(columnNames[indice]);
colonne.setWidth(columnWidth[indice]);
}
// Set Table Color
TableItem item;
for (int i = 0, n = tabExistingChannels.getItemCount(); i < n; i++)
{
item = tabExistingChannels.getItem(i);
item.setBackground(new Color(parent.getDisplay(), 239, 239, 231));
}
tabv.setInput(_logBuffer);
top.pack();
// Create a little thread that monitors the log buffer to
// avoid to keep too old messages.
Thread thr = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
while (!_endMonitor)
{
synchronized (_logSem)
{
_logSem.wait(2000L);
}
synchronized (_tmpLogBuffer)
{
int s = _tmpLogBuffer.size();
if ((s >= NB_TMP_LOG)
|| ((s > 0) && (System.currentTimeMillis() - _tmpLogBuffer.get(0).getMillis() > 2000L)))
{
// Have to flush
synchronized (_logBuffer)
{
_logBuffer.addAll(_tmpLogBuffer);
_tmpLogBuffer.clear();
}
_parent.getDisplay().asyncExec(new Runnable()
{
public void run()
{
tabv.refresh();
}
});
}
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
thr.start();
}
public void setFocus()
{
}
}