/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils.ui;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.HyperlinkGroup;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import de.innovationgate.eclipse.utils.Activator;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.model.TaskStatus;
import de.innovationgate.wga.common.beans.csconfig.v1.PluginConfig;
public class MultiTaskStatusControl extends Composite implements IHyperlinkListener {
private List<TaskStatus> _beans;
private Map<TaskStatus,Map<String,Control>> _controls = new HashMap<TaskStatus, Map<String,Control>>();
private Button _refresh;
private static final String LABEL_CONTROL = "LABEL";
private static final String LINK_CONTROL = "LINK";
private static final String IMAGE_CONTROL = "IMAGE";
private static final String DATA_STATUS_BEAN = "STATUS_BEAN";
public MultiTaskStatusControl(Composite parent, int style) {
super(parent, style);
}
public void init(List<TaskStatus> beans) {
_beans = beans;
createControls();
}
private void createControls() {
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.marginHeight = 0;
layout.marginWidth = 0;
this.setLayout(layout);
_refresh = new Button(this, SWT.PUSH);
_refresh.setImage(Activator.getDefault().getImageRegistry().get(Activator.IMAGE_REFRESH));
_refresh.setToolTipText("refresh");
GridData gd = new GridData();
gd.horizontalSpan = 3;
gd.horizontalAlignment = SWT.END;
_refresh.setLayoutData(gd);
HyperlinkGroup group = new HyperlinkGroup(getShell().getDisplay());
for (TaskStatus status : _beans) {
Label label = new Label(this, SWT.None);
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
ImageHyperlink link = new ImageHyperlink(this, SWT.None);
link.addHyperlinkListener(this);
link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
group.add(link);
Label image = new Label(this, SWT.None);
image.setText("");
image.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Map<String,Control> controlSet = new HashMap<String,Control>();
controlSet.put(LABEL_CONTROL, label);
controlSet.put(LINK_CONTROL, link);
controlSet.put(IMAGE_CONTROL, image);
_controls.put(status, controlSet);
update(status);
}
}
public void update(TaskStatus status) {
Map<String,Control> controlSet = _controls.get(status);
if (controlSet != null) {
((Label)controlSet.get(LABEL_CONTROL)).setText(status.getTaskLabel());
ImageHyperlink link = (ImageHyperlink)controlSet.get(LINK_CONTROL);
link.setData(DATA_STATUS_BEAN, status);
if (status.getSeverity() == TaskStatus.OK || status.getSeverity() == TaskStatus.PENDING) {
//link.setEnabled(false);
link.setUnderlined(false);
} else {
//link.setEnabled(true);
link.setUnderlined(true);
}
if (status.getSeverity() == TaskStatus.PENDING) {
link.setImage(Activator.getDefault().getImageRegistry().get(Activator.IMAGE_FLAG_GRAY));
link.setText("Pending ...");
} else if (status.getSeverity() == TaskStatus.OK || status.getSeverity() == TaskStatus.INFO) {
link.setImage(Activator.getDefault().getImageRegistry().get(Activator.IMAGE_FLAG_GREEN));
link.setText("OK");
} else if (status.getSeverity() == TaskStatus.WARNING) {
link.setImage(Activator.getDefault().getImageRegistry().get(Activator.IMAGE_FLAG_YELLOW));
link.setText("Warning");
} else if (status.getSeverity() == TaskStatus.ERROR) {
link.setImage(Activator.getDefault().getImageRegistry().get(Activator.IMAGE_FLAG_RED));
link.setText("Error");
}
if (status.getSeverityLabel() != null) {
link.setText(status.getSeverityLabel());
}
}
layout(true);
}
public void linkActivated(HyperlinkEvent e) {
Hyperlink link = (Hyperlink) e.getSource();
TaskStatus status = (TaskStatus)link.getData(DATA_STATUS_BEAN);
if (status.getSeverity() == TaskStatus.WARNING || status.getSeverity() == TaskStatus.ERROR) {
String title = status.getTaskLabel();
if (status.getSeverity() == TaskStatus.WARNING) {
title = "WARNING - " + title;
} else {
title = "ERROR - " + title;
}
if (status.getSeverity() == TaskStatus.ERROR) {
WorkbenchUtils.showErrorDialog(getShell(), title, status.getMessage(), status.getException());
} else if (status.getSeverity() == TaskStatus.WARNING) {
MessageDialog.openWarning(getShell(), title, status.getMessage());
}
}
}
public void linkEntered(HyperlinkEvent e) {
// TODO Auto-generated method stub
}
public void linkExited(HyperlinkEvent e) {
// TODO Auto-generated method stub
}
public Button getRefreshButton() {
return _refresh;
}
}