package org.jampa.controllers;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.PlatformUI;
public class LogController {
private List<String> _log = null;
private static LogController _instance = null;
protected transient PropertyChangeSupport logChangeListeners;
private LogController() {
_log = new ArrayList<String>();
logChangeListeners = new PropertyChangeSupport(this);
}
public static LogController getInstance() {
if (_instance == null) {
_instance = new LogController();
}
return _instance;
}
public void add(String message) {
_log.add(message);
fireLogChange(null, null, null);
}
public int getNbLines() {
return _log.size();
}
public String getMessages() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < _log.size(); i++) {
sb.append(_log.get(i));
}
return sb.toString();
}
public void copyLogToClipboard() {
Clipboard cb = new Clipboard(PlatformUI.getWorkbench().getDisplay());
String textData = getMessages();
if (textData.length() > 0) {
TextTransfer textTransfer = TextTransfer.getInstance();
cb.setContents(new Object[]{textData}, new Transfer[]{textTransfer});
}
}
public void addLogChangeListener(PropertyChangeListener listener){
if (listener == null) {
throw new IllegalArgumentException();
}
this.logChangeListeners.addPropertyChangeListener(listener);
}
public void removeLogChangeListener(PropertyChangeListener listener){
this.logChangeListeners.removePropertyChangeListener(listener);
}
protected void fireLogChange(String prop, Object old, Object newValue){
if (this.logChangeListeners.hasListeners(prop)) {
this.logChangeListeners.firePropertyChange(prop, old, newValue);
}
}
}