org.eclipse.wst.ide.remote.ui.internal.viewers.HeaderViewer.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.wst.ide.remote.ui.internal.viewers.HeaderViewer.java

Source

/*******************************************************************************
 * Copyright (c) 2003, 2013 IBM Corporation and others.
 * 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:
 *     IBM Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.ide.remote.ui.internal.viewers;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.ide.remote.core.internal.provisional.Request;
//import org.eclipse.wst.ide.remote.core.internal.provisional.Response;
import org.eclipse.wst.ide.remote.ui.internal.ContextIds;
import org.eclipse.wst.ide.remote.ui.internal.Messages;
import org.eclipse.wst.ide.remote.ui.internal.MonitorUIPlugin;
import org.eclipse.wst.ide.remote.ui.internal.custom.MonitorStackLayout;

/**
 * An transport (header) viewer.
 */
public class HeaderViewer {
    protected boolean displayHeader;

    protected Composite headerComp;
    protected MonitorStackLayout layout;

    protected Text headerLabel;
    protected Text headerText;
    protected Request rr;
    protected byte msg;

    protected boolean hidden;

    /**
     * Request header constant.
     */
    public static byte REQUEST_HEADER = 0;

    /**
     * Response header constant.
     */
    public static byte RESPONSE_HEADER = 1;

    /**
     * Create a new header viewer.
     * 
     * @param parent
     * @param message
     */
    public HeaderViewer(Composite parent, byte message) {
        displayHeader = true;
        hidden = false;

        headerComp = new Composite(parent, SWT.NONE);
        layout = new MonitorStackLayout();
        headerComp.setLayout(layout);

        headerText = new Text(headerComp, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL);
        Display display = headerComp.getDisplay();
        headerText.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
        headerText.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        headerText.setFont(JFaceResources.getTextFont());
        PlatformUI.getWorkbench().getHelpSystem().setHelp(headerText, ContextIds.VIEW_RESPONSE);

        headerLabel = new Text(headerComp, SWT.READ_ONLY);

        layout.topControl = headerText;

        rr = null;
        msg = message;

        //if (msg==REQUEST_HEADER)
        //headerLabel.setText(NLS.bind(Messages.headerLabel1,""));
        //else if(msg==RESPONSE_HEADER)
        headerText.setText(NLS.bind(Messages.headerLabel2, ""));

        setDisplayHeader(false);
    }

    /**
     * 
     * @param request
     */
    public void setRequestResponse(Request request) {
        rr = request;
        if (!hidden)
            getView();
    }

    /**
     * 
     * @param b
     */
    public void setDisplayHeader(boolean b) {
        if (displayHeader != b) {
            displayHeader = b;

            if (displayHeader)
                layout.topControl = headerText;
            else
                layout.topControl = headerLabel;

            headerComp.layout(true);
            getView();
        }
    }

    private void getView() {
        String out = "";
        boolean req = true;

        if (rr != null) {
            if (msg == REQUEST_HEADER) {
                req = true;
                byte[] b = rr.getRequest(Request.CONTENT);
                //System.out.println(rr.getName());
                if (b != null)
                    out = MonitorUIPlugin.parse(b);
            } else if (msg == RESPONSE_HEADER) {
                req = false;
                byte[] b = rr.getRequest(Request.CONTENT);
                //System.out.println(rr.getName());
                if (b != null)
                    out = MonitorUIPlugin.parse(b);
                String command = "";
                if (out.length() != 0)
                    command = out.split(" ")[1];
                if (command != null)
                    out = command;
            }
        }

        if (displayHeader)
            headerText.setText("kdsb");
        else {
            if (req)
                headerLabel.setText(NLS.bind(Messages.headerLabel1, out));
            else
                headerLabel.setText(NLS.bind(Messages.headerLabel2, out));
        }
    }

    /**
     * Dispose the header.
     */
    public void dispose() {
        headerComp.dispose();
    }

    /**
     * Set whether the header can be edited.
     * 
     * @param editable If true the header can be edited, otherwise the header cannot be edited.
     */
    public void setEditable(boolean editable) {
        headerText.setEditable(editable);
    }

    /**
     * Get the content from the header.
     * 
     * @return The content from the header.
     */
    public byte[] getContent() {
        if (headerText == null || headerText.isDisposed())
            return null;

        String header = headerText.getText().trim();
        // Need to ensure that the following 4 bytes end the header. The getBytes()
        // method removes spaces at the end of the string.
        byte[] twoNewlines = new byte[] { '\015', '\012', '\015', '\012' };
        byte[] headerBytes = header.getBytes();
        byte[] retBytes = new byte[headerBytes.length + 4];
        System.arraycopy(headerBytes, 0, retBytes, 0, headerBytes.length);
        System.arraycopy(twoNewlines, 0, retBytes, headerBytes.length, 4);
        return retBytes;
    }
}