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