- Timestamp:
- 09/26/11 20:24:03 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/swt/CommandHistoryDialog.java
r192 r195 1 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; 2 11 3 12 import org.eclipse.swt.widgets.Dialog; 4 13 import org.eclipse.swt.widgets.Display; 14 import org.eclipse.swt.widgets.FileDialog; 5 15 import org.eclipse.swt.widgets.Shell; 6 16 import org.eclipse.swt.widgets.List; … … 11 21 import org.eclipse.swt.layout.GridData; 12 22 13 public class CommandHistoryDialog extends Dialog { 14 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.ConsoleObserver; 27 import org.eclipse.swt.events.DisposeListener; 28 import org.eclipse.swt.events.DisposeEvent; 29 import org.eclipse.swt.events.SelectionAdapter; 30 import org.eclipse.swt.events.SelectionEvent; 31 32 public class CommandHistoryDialog extends Dialog implements ConsoleObserver { 33 34 protected java.util.List<String> history = new LinkedList<String>(); 35 36 protected List commandHistoryList; 15 37 protected Object result; 16 38 protected Shell shell; 39 40 boolean isOpen; 17 41 18 42 /** … … 23 47 public CommandHistoryDialog(Shell parent, int style) { 24 48 super(parent, style); 25 setText("SWT Dialog"); 49 setText("Command History"); 50 isOpen = false; 51 Console.getInstance().registerObserver(this); 26 52 } 27 53 … … 34 60 shell.open(); 35 61 shell.layout(); 62 isOpen = true; 36 63 Display display = getParent().getDisplay(); 37 64 while (!shell.isDisposed()) { … … 47 74 */ 48 75 private void createContents() { 49 shell = new Shell(getParent(), SWT.DIALOG_TRIM); 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 }); 50 82 shell.setSize(450, 300); 51 83 shell.setText(getText()); … … 57 89 new Label(shell, SWT.NONE); 58 90 59 List list = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI); 60 list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1)); 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 } 61 96 62 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 }); 63 106 btnExec.setText("Execute"); 64 107 65 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 }); 66 117 btnCopyToClipboard.setText("Copy to Clipboard"); 67 118 68 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 }); 69 157 btnCreateBatchfile.setText("Create Batchfile"); 70 158 71 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 } 72 201 }
Note: See TracChangeset
for help on using the changeset viewer.