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