/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package org.jboss.as.cli.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;

/**
 *
 * @author Stan Silvert ssilvert@redhat.com (C) 2012 Red Hat Inc.
 */
public class SelectPreviousOpMouseAdapter extends MouseAdapter {
    private JTextPane output;
    private JTextField cmdText;
    private DoOperationActionListener opListener;

    public SelectPreviousOpMouseAdapter(JTextPane output, JTextField cmdText, DoOperationActionListener opListener) {
        this.output = output;
        this.cmdText = cmdText;
        this.opListener = opListener;
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        if (me.getClickCount() < 2) return;

        int pos = output.viewToModel(me.getPoint());

        try {
            int rowStart = Utilities.getRowStart(output, pos);
            int rowEnd = Utilities.getRowEnd(output, pos);
            String line = output.getDocument().getText(rowStart, rowEnd - rowStart);
            if (opListener.getCmdHistory().contains(line)) {
                output.select(rowStart, rowEnd);
                cmdText.setText(line);
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

    }

}

@@ -989,16 +989,215 @@
 i.gui;%0A%0A
+import java.awt.Toolkit;%0Aimport java.awt.datatransfer.Clipboard;%0Aimport java.awt.datatransfer.ClipboardOwner;%0Aimport java.awt.datatransfer.StringSelection;%0Aimport java.awt.datatransfer.Transferable;%0A
 import j
@@ -1220,24 +1220,24 @@
 useAdapter;%0A
-
 import java.
@@ -1541,16 +1541,42 @@
 Adapter 
+implements ClipboardOwner 
 %7B%0A    pr
@@ -1680,16 +1680,106 @@
 istener;
+%0A    private Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
 %0A%0A    pu
@@ -2526,16 +2526,94 @@
 (line);%0A
+                systemClipboard.setContents(new StringSelection(line), this);%0A
         
@@ -2712,10 +2712,105 @@
 %0A    %7D%0A%0A
+    public void lostOwnership(Clipboard clpbrd, Transferable t) %7B%0A        // do nothing%0A    %7D%0A%0A
 %7D%0A

When selecting a previous command with double-click, copy to system clipboard.