com.kopson.cite.actions.CopyRowClipboard.java Source code

Java tutorial

Introduction

Here is the source code for com.kopson.cite.actions.CopyRowClipboard.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Piotr Kopka
 * 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.kopson.cite.com/legal/epl-v10.html
 *
 * Contributors:
 *     Piotr Kopka
 *******************************************************************************/

package com.kopson.cite.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import com.kopson.cite.smartlogmodel.SmartLogEntry;
import com.kopson.cite.views.SmartLogConsole;

/**
 * Copy log row to clipboard
 * 
 * @author kopson
 *
 */
public class CopyRowClipboard extends AbstractHandler {

    @SuppressWarnings("unchecked")
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IWorkbenchPage page = window.getActivePage();
        IViewPart view = page.findView(SmartLogConsole.ID);
        Clipboard clipboard = new Clipboard(Display.getDefault());
        ISelection selection = view.getSite().getSelectionProvider().getSelection();
        List<SmartLogEntry> logList = new ArrayList<SmartLogEntry>();
        if (selection != null && selection instanceof IStructuredSelection) {
            IStructuredSelection currentSelection = (IStructuredSelection) selection;
            for (Iterator<SmartLogEntry> iterator = currentSelection.iterator(); iterator.hasNext();) {
                SmartLogEntry logRow = iterator.next();
                logList.add(logRow);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (SmartLogEntry logRow : logList) {
            sb.append(rowToString(logRow));
        }
        TextTransfer textTransfer = TextTransfer.getInstance();
        clipboard.setContents(new Object[] { sb.toString() }, new Transfer[] { textTransfer });

        return null;
    }

    /**
     * Convert row to string.
     * 
     * @param row
     * @return log row
     */
    private String rowToString(SmartLogEntry row) {
        return row.toString() + System.getProperty("line.separator");
    }

}