source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/swt/CommandHistoryDialog.java @ 195

Last change on this file since 195 was 195, checked in by sherbold, 13 years ago

Further work on the SWT GUI prototype.
The GUI is still not finished, however, most features are implemented and the GUI can be used. It may cause some problems, and some features are still missing.

  • Property svn:mime-type set to text/plain
File size: 5.6 KB
Line 
1package de.ugoe.cs.eventbench.swt;
2
3import java.awt.Toolkit;
4import java.awt.datatransfer.Clipboard;
5import java.awt.datatransfer.StringSelection;
6import java.io.File;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.io.OutputStreamWriter;
10import java.util.LinkedList;
11
12import org.eclipse.swt.widgets.Dialog;
13import org.eclipse.swt.widgets.Display;
14import org.eclipse.swt.widgets.FileDialog;
15import org.eclipse.swt.widgets.Shell;
16import org.eclipse.swt.widgets.List;
17import org.eclipse.swt.SWT;
18import org.eclipse.swt.widgets.Label;
19import org.eclipse.swt.widgets.Button;
20import org.eclipse.swt.layout.GridLayout;
21import org.eclipse.swt.layout.GridData;
22
23import de.ugoe.cs.util.StringTools;
24import de.ugoe.cs.util.console.CommandExecuter;
25import de.ugoe.cs.util.console.Console;
26import de.ugoe.cs.util.console.ConsoleObserver;
27import org.eclipse.swt.events.DisposeListener;
28import org.eclipse.swt.events.DisposeEvent;
29import org.eclipse.swt.events.SelectionAdapter;
30import org.eclipse.swt.events.SelectionEvent;
31
32public class CommandHistoryDialog extends Dialog implements ConsoleObserver {
33       
34        protected java.util.List<String> history = new LinkedList<String>();
35       
36        protected List commandHistoryList;
37        protected Object result;
38        protected Shell shell;
39       
40        boolean isOpen;
41
42        /**
43         * Create the dialog.
44         * @param parent
45         * @param style
46         */
47        public CommandHistoryDialog(Shell parent, int style) {
48                super(parent, style);
49                setText("Command History");
50                isOpen = false;
51                Console.getInstance().registerObserver(this);
52        }
53
54        /**
55         * Open the dialog.
56         * @return the result
57         */
58        public Object open() {
59                createContents();
60                shell.open();
61                shell.layout();
62                isOpen = true;
63                Display display = getParent().getDisplay();
64                while (!shell.isDisposed()) {
65                        if (!display.readAndDispatch()) {
66                                display.sleep();
67                        }
68                }
69                return result;
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) {
131                                                        Console.traceln("Created batchfile " + filename);
132                                                } else {
133                                                        Console.traceln("Overwrote file " + filename);
134                                                }
135                                        } catch (IOException e) {
136                                                Console.printerrln("Unable to create file " + filename);
137                                                Console.printStacktrace(e);
138                                        }
139                                        OutputStreamWriter writer = null;
140                                        try {
141                                                writer = new OutputStreamWriter(new FileOutputStream(file));
142                                        } catch (IOException e) {
143                                                Console.printerrln("Unable to open file for writing (read-only file):"
144                                                                + filename);
145                                                Console.printStacktrace(e);
146                                        }
147                                        try {
148                                                writer.write(getSelectedCommands());
149                                                writer.close();
150                                        } catch (IOException e) {
151                                                Console.printerrln("Unable to write to file.");
152                                                Console.printStacktrace(e);
153                                        }
154                                }
155                        }
156                });
157                btnCreateBatchfile.setText("Create Batchfile");
158
159        }
160
161        @Override
162        public void updateText(String newMessage) {
163                // ignore
164        }
165
166        @Override
167        public void errStream(String errMessage) {
168                // ignore
169        }
170
171        @Override
172        public void trace(String traceMessage) {
173                // ignore
174        }
175
176        @Override
177        public void printStacktrace(Exception e) {
178                // ignore
179               
180        }
181
182        @Override
183        public void commandNotification(String command) {
184                history.add(command);
185                if( isOpen ) {
186                        commandHistoryList.add(command);
187                }
188        }
189       
190        public boolean isOpen() {
191                return isOpen;
192        }
193       
194        private String getSelectedCommands() {
195                StringBuilder commands = new StringBuilder();
196                for( String command : commandHistoryList.getSelection()) {
197                        commands.append(command + StringTools.ENDLINE);
198                }
199                return commands.toString();
200        }
201}
Note: See TracBrowser for help on using the repository browser.