[922] | 1 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
[526] | 2 |
|
---|
| 3 | import java.awt.Toolkit;
|
---|
| 4 | import java.awt.datatransfer.Clipboard;
|
---|
| 5 | import java.awt.datatransfer.StringSelection;
|
---|
| 6 | import java.io.File;
|
---|
| 7 | import java.io.FileOutputStream;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.OutputStreamWriter;
|
---|
| 10 | import java.util.LinkedList;
|
---|
[639] | 11 | import java.util.logging.Level;
|
---|
[526] | 12 |
|
---|
| 13 | import org.eclipse.swt.widgets.Dialog;
|
---|
| 14 | import org.eclipse.swt.widgets.Display;
|
---|
| 15 | import org.eclipse.swt.widgets.FileDialog;
|
---|
| 16 | import org.eclipse.swt.widgets.Shell;
|
---|
| 17 | import org.eclipse.swt.widgets.List;
|
---|
| 18 | import org.eclipse.swt.SWT;
|
---|
| 19 | import org.eclipse.swt.widgets.Label;
|
---|
| 20 | import org.eclipse.swt.widgets.Button;
|
---|
| 21 | import org.eclipse.swt.layout.GridLayout;
|
---|
| 22 | import org.eclipse.swt.layout.GridData;
|
---|
| 23 |
|
---|
| 24 | import de.ugoe.cs.util.StringTools;
|
---|
| 25 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
| 26 | import de.ugoe.cs.util.console.Console;
|
---|
| 27 | import de.ugoe.cs.util.console.listener.ICommandListener;
|
---|
| 28 |
|
---|
| 29 | import org.eclipse.swt.events.DisposeListener;
|
---|
| 30 | import org.eclipse.swt.events.DisposeEvent;
|
---|
| 31 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
| 32 | import org.eclipse.swt.events.SelectionEvent;
|
---|
| 33 |
|
---|
| 34 | public class CommandHistoryDialog extends Dialog implements ICommandListener {
|
---|
| 35 |
|
---|
[570] | 36 | protected java.util.List<String> history = new LinkedList<String>();
|
---|
[526] | 37 |
|
---|
[570] | 38 | protected List commandHistoryList;
|
---|
| 39 | protected Shell shell;
|
---|
[526] | 40 |
|
---|
[570] | 41 | boolean isOpen;
|
---|
[526] | 42 |
|
---|
[570] | 43 | /**
|
---|
| 44 | * Create the dialog.
|
---|
| 45 | *
|
---|
| 46 | * @param parent
|
---|
| 47 | * @param style
|
---|
| 48 | */
|
---|
| 49 | public CommandHistoryDialog(Shell parent, int style) {
|
---|
| 50 | super(parent, style);
|
---|
| 51 | setText("Command History");
|
---|
| 52 | isOpen = false;
|
---|
| 53 | Console.getInstance().registerCommandListener(this);
|
---|
| 54 | }
|
---|
[526] | 55 |
|
---|
[570] | 56 | /**
|
---|
| 57 | * Open the dialog.
|
---|
| 58 | */
|
---|
| 59 | public void open() {
|
---|
| 60 | createContents();
|
---|
| 61 | shell.open();
|
---|
| 62 | shell.layout();
|
---|
| 63 | isOpen = true;
|
---|
| 64 | Display display = getParent().getDisplay();
|
---|
| 65 | while (!shell.isDisposed()) {
|
---|
| 66 | if (!display.readAndDispatch()) {
|
---|
| 67 | display.sleep();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Create contents of the dialog.
|
---|
| 74 | */
|
---|
| 75 | private void createContents() {
|
---|
| 76 | shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
|
---|
| 77 | shell.addDisposeListener(new DisposeListener() {
|
---|
| 78 | public void widgetDisposed(DisposeEvent arg0) {
|
---|
| 79 | isOpen = false;
|
---|
| 80 | }
|
---|
| 81 | });
|
---|
| 82 | shell.setSize(450, 300);
|
---|
| 83 | shell.setText(getText());
|
---|
| 84 | shell.setLayout(new GridLayout(3, false));
|
---|
| 85 |
|
---|
| 86 | Label lblRecentCommands = new Label(shell, SWT.NONE);
|
---|
| 87 | lblRecentCommands.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
|
---|
| 88 | lblRecentCommands.setText("Recent Commands:");
|
---|
| 89 | new Label(shell, SWT.NONE);
|
---|
| 90 |
|
---|
| 91 | commandHistoryList = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
|
---|
| 92 | commandHistoryList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
|
---|
| 93 | for (String command : history) {
|
---|
| 94 | commandHistoryList.add(command);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | Button btnExec = new Button(shell, SWT.NONE);
|
---|
| 98 | btnExec.addSelectionListener(new SelectionAdapter() {
|
---|
| 99 | @Override
|
---|
| 100 | public void widgetSelected(SelectionEvent e) {
|
---|
| 101 | for (String command : commandHistoryList.getSelection()) {
|
---|
| 102 | CommandExecuter.getInstance().exec(command);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | });
|
---|
| 106 | btnExec.setText("Execute");
|
---|
| 107 |
|
---|
| 108 | Button btnCopyToClipboard = new Button(shell, SWT.NONE);
|
---|
| 109 | btnCopyToClipboard.addSelectionListener(new SelectionAdapter() {
|
---|
| 110 | @Override
|
---|
| 111 | public void widgetSelected(SelectionEvent e) {
|
---|
| 112 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
---|
| 113 | StringSelection content = new StringSelection(getSelectedCommands());
|
---|
| 114 | clipboard.setContents(content, null);
|
---|
| 115 | }
|
---|
| 116 | });
|
---|
| 117 | btnCopyToClipboard.setText("Copy to Clipboard");
|
---|
| 118 |
|
---|
| 119 | Button btnCreateBatchfile = new Button(shell, SWT.NONE);
|
---|
| 120 | btnCreateBatchfile.addSelectionListener(new SelectionAdapter() {
|
---|
| 121 | @Override
|
---|
| 122 | public void widgetSelected(SelectionEvent event) {
|
---|
| 123 | FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
|
---|
| 124 | String filename = fileDialog.open();
|
---|
| 125 | if (filename != null) {
|
---|
| 126 | File file = new File(filename);
|
---|
| 127 | boolean fileCreated;
|
---|
| 128 | try {
|
---|
| 129 | fileCreated = file.createNewFile();
|
---|
| 130 | if (!fileCreated) {
|
---|
[639] | 131 | Console.traceln(Level.INFO, "Created batchfile " + filename);
|
---|
[570] | 132 | }
|
---|
| 133 | else {
|
---|
[639] | 134 | Console.traceln(Level.INFO, "Overwrote file " + filename);
|
---|
[570] | 135 | }
|
---|
| 136 | }
|
---|
| 137 | catch (IOException e) {
|
---|
| 138 | Console.printerrln("Unable to create file " + filename);
|
---|
| 139 | Console.logException(e);
|
---|
| 140 | }
|
---|
| 141 | OutputStreamWriter writer = null;
|
---|
| 142 | try {
|
---|
| 143 | writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
|
---|
| 144 | }
|
---|
| 145 | catch (IOException e) {
|
---|
| 146 | Console.printerrln("Unable to open file for writing (read-only file):" +
|
---|
| 147 | filename);
|
---|
| 148 | Console.logException(e);
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
| 151 | try {
|
---|
| 152 | writer.write(getSelectedCommands());
|
---|
| 153 | writer.close();
|
---|
| 154 | }
|
---|
| 155 | catch (IOException e) {
|
---|
| 156 | Console.printerrln("Unable to write to file.");
|
---|
| 157 | Console.logException(e);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | });
|
---|
| 162 | btnCreateBatchfile.setText("Create Batchfile");
|
---|
| 163 |
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | @Override
|
---|
| 167 | public void commandNotification(String command) {
|
---|
| 168 | history.add(command);
|
---|
| 169 | if (isOpen) {
|
---|
| 170 | commandHistoryList.add(command);
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public boolean isOpen() {
|
---|
| 175 | return isOpen;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | private String getSelectedCommands() {
|
---|
| 179 | StringBuilder commands = new StringBuilder();
|
---|
| 180 | for (String command : commandHistoryList.getSelection()) {
|
---|
| 181 | commands.append(command + StringTools.ENDLINE);
|
---|
| 182 | }
|
---|
| 183 | return commands.toString();
|
---|
| 184 | }
|
---|
[526] | 185 | }
|
---|