org.apache.airavata.xbaya.ui.dialogs.monitor.MonitorWindow.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.airavata.xbaya.ui.dialogs.monitor.MonitorWindow.java

Source

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */

package org.apache.airavata.xbaya.ui.dialogs.monitor;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.Date;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;

import org.apache.airavata.common.utils.BrowserLauncher;
import org.apache.airavata.common.utils.StringUtil;
import org.apache.airavata.common.utils.XMLUtil;
import org.apache.airavata.ws.monitor.EventDataRepository;
import org.apache.airavata.ws.monitor.MonitorUtil;
import org.apache.airavata.xbaya.ui.XBayaGUI;
import org.apache.airavata.xbaya.ui.dialogs.XBayaDialog;
import org.apache.airavata.xbaya.ui.widgets.GridPanel;
import org.apache.airavata.xbaya.ui.widgets.XBayaLabel;
import org.apache.airavata.xbaya.ui.widgets.XBayaTextField;
import org.apache.commons.lang.StringEscapeUtils;
import org.xmlpull.infoset.XmlElement;

import xsul5.XmlConstants;

public class MonitorWindow {

    private XBayaGUI xbayaGUI;

    private XBayaDialog dialog;

    private XBayaTextField timeTextField;

    private XBayaTextField idTextField;

    private XBayaTextField statusTextField;

    private JEditorPane messageTextArea;
    private String messageText;

    /**
     * Constructs a MonitorWindow.
     * 
     * @param engine
     *            The XBayaEngine
     */
    public MonitorWindow(XBayaGUI xbayaGUI) {
        this.xbayaGUI = xbayaGUI;
        init();
    }

    /**
     * Shows the notification.
     * 
     * @param event
     *            The notification to show
     */
    public void show(XmlElement event) {
        Date timestamp = MonitorUtil.getTimestamp(event);
        if (timestamp != null) {
            this.timeTextField.setText(timestamp.toString());
        } else {
            this.timeTextField.setText("");
        }
        this.idTextField.setText(MonitorUtil.getNodeID(event));
        this.statusTextField.setText(MonitorUtil.getStatus(event));

        // Show the raw XML for now.
        messageText = XMLUtil.BUILDER.serializeToStringPretty(event);
        this.messageTextArea.setText(StringUtil
                .createHTMLUrlTaggedString2(StringEscapeUtils.escapeHtml(messageText),
                        StringUtil.getURLS(messageText))
                .replaceAll(Pattern.quote(" "), "&nbsp;").replaceAll(Pattern.quote("\n"), "<br />\n"));

        this.dialog.show();
        this.dialog.getDialog().setSize(600, 800);
    }

    private void hide() {
        this.dialog.hide();
    }

    private void init() {
        this.timeTextField = new XBayaTextField();
        this.timeTextField.setEditable(false);
        XBayaLabel timeLabel = new XBayaLabel(EventDataRepository.Column.TIME.getName(), this.timeTextField);

        this.idTextField = new XBayaTextField();
        this.idTextField.setEditable(false);
        XBayaLabel idLabel = new XBayaLabel(EventDataRepository.Column.ID.getName(), this.idTextField);

        this.statusTextField = new XBayaTextField();
        this.statusTextField.setEditable(false);
        XBayaLabel statusLabel = new XBayaLabel(EventDataRepository.Column.STATUS.getName(), this.statusTextField);

        this.messageTextArea = new JEditorPane(XmlConstants.CONTENT_TYPE_HTML, "");
        this.messageTextArea.setSize(500, 500);
        this.messageTextArea.setEditable(false);
        messageTextArea.setBackground(Color.WHITE);
        messageTextArea.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent event) {
                if (event.getEventType() == EventType.ACTIVATED) {
                    URL url = event.getURL();
                    try {
                        BrowserLauncher.openURL(url.toString());
                    } catch (Exception e) {
                        MonitorWindow.this.xbayaGUI.getErrorWindow().error(MonitorWindow.this.dialog.getDialog(),
                                e.getMessage(), e);
                    }
                }
            }
        });
        JScrollPane pane = new JScrollPane(messageTextArea);
        pane.setSize(500, 500);
        XBayaLabel messageLabel = new XBayaLabel(EventDataRepository.Column.MESSAGE.getName(), pane);

        GridPanel infoPanel = new GridPanel();
        infoPanel.add(timeLabel);
        infoPanel.add(this.timeTextField);
        infoPanel.add(idLabel);
        infoPanel.add(this.idTextField);
        infoPanel.add(statusLabel);
        infoPanel.add(this.statusTextField);
        infoPanel.add(messageLabel);
        infoPanel.add(pane);
        infoPanel.layout(4, 2, 3, 1);

        JButton okButton = new JButton("OK");
        okButton.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                hide();
            }
        });
        JButton copyButton = new JButton("Copy to Clipboard");
        copyButton.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(messageText),
                        null);
            }
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        buttonPanel.add(copyButton);

        this.dialog = new XBayaDialog(this.xbayaGUI, "Notification", infoPanel, buttonPanel);
        this.dialog.setDefaultButton(okButton);
    }
}