source: trunk/quest-ui-core/src/de/ugoe/cs/quest/ui/swt/CommandHistoryDialog.java @ 434

Last change on this file since 434 was 434, checked in by sherbold, 12 years ago
  • renamed packages to fit QUEST project structure
  • Property svn:mime-type set to text/plain
File size: 5.2 KB
Line 
1package de.ugoe.cs.quest.ui.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.listener.ICommandListener;
27
28import org.eclipse.swt.events.DisposeListener;
29import org.eclipse.swt.events.DisposeEvent;
30import org.eclipse.swt.events.SelectionAdapter;
31import org.eclipse.swt.events.SelectionEvent;
32
33public class CommandHistoryDialog extends Dialog implements ICommandListener {
34       
35        protected java.util.List<String> history = new LinkedList<String>();
36       
37        protected List commandHistoryList;
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().registerCommandListener(this);
52        }
53
54        /**
55         * Open the dialog.
56         */
57        public void open() {
58                createContents();
59                shell.open();
60                shell.layout();
61                isOpen = true;
62                Display display = getParent().getDisplay();
63                while (!shell.isDisposed()) {
64                        if (!display.readAndDispatch()) {
65                                display.sleep();
66                        }
67                }
68        }
69
70        /**
71         * Create contents of the dialog.
72         */
73        private void createContents() {
74                shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
75                shell.addDisposeListener(new DisposeListener() {
76                        public void widgetDisposed(DisposeEvent arg0) {
77                                isOpen = false;
78                        }
79                });
80                shell.setSize(450, 300);
81                shell.setText(getText());
82                shell.setLayout(new GridLayout(3, false));
83               
84                Label lblRecentCommands = new Label(shell, SWT.NONE);
85                lblRecentCommands.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
86                lblRecentCommands.setText("Recent Commands:");
87                new Label(shell, SWT.NONE);
88               
89                commandHistoryList = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
90                commandHistoryList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
91                for( String command : history ) {
92                        commandHistoryList.add(command);
93                }
94               
95                Button btnExec = new Button(shell, SWT.NONE);
96                btnExec.addSelectionListener(new SelectionAdapter() {
97                        @Override
98                        public void widgetSelected(SelectionEvent e) {
99                                for( String command : commandHistoryList.getSelection() ) {
100                                        CommandExecuter.getInstance().exec(command);
101                                }
102                        }
103                });
104                btnExec.setText("Execute");
105               
106                Button btnCopyToClipboard = new Button(shell, SWT.NONE);
107                btnCopyToClipboard.addSelectionListener(new SelectionAdapter() {
108                        @Override
109                        public void widgetSelected(SelectionEvent e) {
110                                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
111                                StringSelection content = new StringSelection(getSelectedCommands());
112                                clipboard.setContents(content, null);
113                        }
114                });
115                btnCopyToClipboard.setText("Copy to Clipboard");
116               
117                Button btnCreateBatchfile = new Button(shell, SWT.NONE);
118                btnCreateBatchfile.addSelectionListener(new SelectionAdapter() {
119                        @Override
120                        public void widgetSelected(SelectionEvent event) {
121                                FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
122                                String filename = fileDialog.open();
123                                if( filename!=null ) {
124                                        File file = new File(filename);
125                                        boolean fileCreated;
126                                        try {
127                                                fileCreated = file.createNewFile();
128                                                if (!fileCreated) {
129                                                        Console.traceln("Created batchfile " + filename);
130                                                } else {
131                                                        Console.traceln("Overwrote file " + filename);
132                                                }
133                                        } catch (IOException e) {
134                                                Console.printerrln("Unable to create file " + filename);
135                                                Console.logException(e);
136                                        }
137                                        OutputStreamWriter writer = null;
138                                        try {
139                                                writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
140                                        } catch (IOException e) {
141                                                Console.printerrln("Unable to open file for writing (read-only file):"
142                                                                + filename);
143                                                Console.logException(e);
144                                                return;
145                                        }
146                                        try {
147                                                writer.write(getSelectedCommands());
148                                                writer.close();
149                                        } catch (IOException e) {
150                                                Console.printerrln("Unable to write to file.");
151                                                Console.logException(e);
152                                        }
153                                }
154                        }
155                });
156                btnCreateBatchfile.setText("Create Batchfile");
157
158        }
159
160        @Override
161        public void commandNotification(String command) {
162                history.add(command);
163                if( isOpen ) {
164                        commandHistoryList.add(command);
165                }
166        }
167       
168        public boolean isOpen() {
169                return isOpen;
170        }
171       
172        private String getSelectedCommands() {
173                StringBuilder commands = new StringBuilder();
174                for( String command : commandHistoryList.getSelection()) {
175                        commands.append(command + StringTools.ENDLINE);
176                }
177                return commands.toString();
178        }
179}
Note: See TracBrowser for help on using the repository browser.