[927] | 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 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
[526] | 16 |
|
---|
[1236] | 17 | import java.util.LinkedList;
|
---|
| 18 |
|
---|
[526] | 19 | import org.eclipse.swt.SWT;
|
---|
| 20 | import org.eclipse.swt.widgets.Event;
|
---|
| 21 | import org.eclipse.swt.widgets.Listener;
|
---|
| 22 | import org.eclipse.swt.widgets.Text;
|
---|
| 23 | import org.eclipse.swt.widgets.Label;
|
---|
| 24 | import org.eclipse.swt.widgets.Composite;
|
---|
| 25 | import org.eclipse.swt.widgets.Button;
|
---|
| 26 | import org.eclipse.swt.layout.GridLayout;
|
---|
| 27 | import org.eclipse.swt.layout.GridData;
|
---|
| 28 | import org.eclipse.swt.custom.StyledText;
|
---|
| 29 | import org.eclipse.swt.events.SelectionAdapter;
|
---|
| 30 | import org.eclipse.swt.events.SelectionEvent;
|
---|
[1236] | 31 | import org.eclipse.swt.events.TraverseEvent;
|
---|
| 32 | import org.eclipse.swt.events.TraverseListener;
|
---|
[526] | 33 |
|
---|
| 34 | import org.eclipse.swt.events.KeyAdapter;
|
---|
| 35 | import org.eclipse.swt.events.KeyEvent;
|
---|
| 36 |
|
---|
[1243] | 37 | import de.ugoe.cs.util.FileTools;
|
---|
| 38 | import de.ugoe.cs.util.StringTools;
|
---|
[526] | 39 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
[1236] | 40 | import de.ugoe.cs.util.console.Console;
|
---|
[1243] | 41 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
[1236] | 42 | import de.ugoe.cs.util.console.listener.ICommandListener;
|
---|
[526] | 43 |
|
---|
| 44 | /**
|
---|
| 45 | * <p>
|
---|
[570] | 46 | * Implements the composite for the console tab in the applications main window.
|
---|
[526] | 47 | * </p>
|
---|
| 48 | *
|
---|
| 49 | * @author Steffen Herbold
|
---|
| 50 | * @version 1.0
|
---|
| 51 | */
|
---|
[1236] | 52 | public class ConsoleTabComposite extends Composite implements ICommandListener {
|
---|
[526] | 53 |
|
---|
[1236] | 54 | protected java.util.List<String> commandHistory = new LinkedList<String>();
|
---|
| 55 |
|
---|
[570] | 56 | protected Text textCommand;
|
---|
[526] | 57 |
|
---|
[570] | 58 | protected StyledText textConsoleOutput;
|
---|
[526] | 59 |
|
---|
[570] | 60 | /**
|
---|
| 61 | * Create the composite.
|
---|
| 62 | *
|
---|
| 63 | * @param parent
|
---|
| 64 | * @param style
|
---|
| 65 | */
|
---|
| 66 | public ConsoleTabComposite(Composite parent, int style) {
|
---|
| 67 | super(parent, style);
|
---|
| 68 | createContents();
|
---|
[1236] | 69 | Console.getInstance().registerCommandListener(this);
|
---|
[570] | 70 | }
|
---|
[526] | 71 |
|
---|
[1236] | 72 | @Override
|
---|
| 73 | public void commandNotification(String command) {
|
---|
| 74 | commandHistory.add(command);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[570] | 77 | private void createContents() {
|
---|
| 78 | setLayout(new GridLayout(3, false));
|
---|
| 79 |
|
---|
| 80 | Label lblCommand = new Label(this, SWT.NONE);
|
---|
| 81 | lblCommand.setText("Command:");
|
---|
| 82 |
|
---|
| 83 | textCommand = new Text(this, SWT.BORDER);
|
---|
| 84 | textCommand.addKeyListener(new KeyAdapter() {
|
---|
| 85 | @Override
|
---|
| 86 | public void keyReleased(KeyEvent e) {
|
---|
| 87 | if (e.keyCode == SWT.CR) {
|
---|
| 88 | executeCommand();
|
---|
| 89 | }
|
---|
[1236] | 90 | else if (e.keyCode == SWT.TAB) {
|
---|
| 91 | autoCompleteCommand();
|
---|
| 92 | }
|
---|
| 93 | else if (e.keyCode == SWT.ARROW_UP) {
|
---|
| 94 | setToPreviousCommand();
|
---|
| 95 | }
|
---|
| 96 | else if (e.keyCode == SWT.ARROW_DOWN) {
|
---|
| 97 | setToNextCommand();
|
---|
| 98 | }
|
---|
[570] | 99 | }
|
---|
| 100 | });
|
---|
[1236] | 101 | textCommand.addTraverseListener(new TraverseListener() {
|
---|
| 102 | @Override
|
---|
| 103 | public void keyTraversed(TraverseEvent e) {
|
---|
| 104 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS))
|
---|
| 105 | {
|
---|
| 106 | e.doit = false;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | });
|
---|
| 111 |
|
---|
[570] | 112 | GridData gd_textCommand = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
|
---|
| 113 | gd_textCommand.widthHint = 304;
|
---|
| 114 | textCommand.setLayoutData(gd_textCommand);
|
---|
| 115 | textCommand.setFocus();
|
---|
| 116 |
|
---|
| 117 | Button btnEnter = new Button(this, SWT.NONE);
|
---|
| 118 | btnEnter.addSelectionListener(new SelectionAdapter() {
|
---|
| 119 | @Override
|
---|
| 120 | public void widgetSelected(SelectionEvent e) {
|
---|
| 121 | executeCommand();
|
---|
| 122 | }
|
---|
| 123 | });
|
---|
| 124 | btnEnter.setText("Enter");
|
---|
| 125 |
|
---|
| 126 | textConsoleOutput =
|
---|
| 127 | new StyledText(this, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL |
|
---|
| 128 | SWT.CANCEL);
|
---|
| 129 | GridData gd_textConsoleOutput = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
|
---|
| 130 | gd_textConsoleOutput.heightHint = 102;
|
---|
| 131 | gd_textConsoleOutput.widthHint = 456;
|
---|
| 132 | textConsoleOutput.setLayoutData(gd_textConsoleOutput);
|
---|
| 133 | textConsoleOutput.addListener(SWT.Modify, new Listener() {
|
---|
| 134 | public void handleEvent(Event e) {
|
---|
| 135 | textConsoleOutput.setTopIndex(textConsoleOutput.getLineCount() - 1);
|
---|
| 136 | }
|
---|
| 137 | });
|
---|
| 138 |
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private void executeCommand() {
|
---|
| 142 | String command = textCommand.getText().trim();
|
---|
| 143 | CommandExecuter.getInstance().exec(command);
|
---|
| 144 | textCommand.setText("");
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[1236] | 147 | private void autoCompleteCommand() {
|
---|
[1243] | 148 | String prefix = textCommand.getText();
|
---|
| 149 | int end = textCommand.getCaretPosition() - 1;
|
---|
| 150 | int start = 0;
|
---|
[1236] | 151 |
|
---|
[1243] | 152 | for (int i = end; i >= 0; i--) {
|
---|
| 153 | if (Character.isWhitespace(prefix.charAt(i))) {
|
---|
| 154 | start = i + 1;
|
---|
| 155 | break;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | String textPrefix = prefix.substring(0, start);
|
---|
| 160 | String textSuffix = prefix.substring(end + 1);
|
---|
| 161 | prefix = prefix.substring(start, end + 1);
|
---|
| 162 |
|
---|
| 163 | String completedPrefix = null;
|
---|
| 164 | if ("".equals(textCommand.getText().substring(0, start).trim())) {
|
---|
| 165 | // search for a command completion
|
---|
| 166 | completedPrefix = CommandExecuter.getInstance().autoCompleteCommand(prefix);
|
---|
| 167 | }
|
---|
| 168 | else {
|
---|
| 169 | // complete an object in the data store or a file name
|
---|
| 170 | completedPrefix = FileTools.autoCompletePath(prefix);
|
---|
| 171 |
|
---|
| 172 | if (prefix.equals(completedPrefix)) {
|
---|
| 173 | // file completion wasn't possible, so try to complete using data objects
|
---|
| 174 | String[] completions =
|
---|
| 175 | GlobalDataContainer.getInstance().getAllKeys().toArray(new String[0]);
|
---|
| 176 | completedPrefix = StringTools.autocomplete(prefix, completions);
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | textCommand.setText(textPrefix + completedPrefix + textSuffix);
|
---|
| 181 | textCommand.setSelection(textPrefix.length() + completedPrefix.length());
|
---|
[1236] | 182 | }
|
---|
| 183 |
|
---|
| 184 | private void setToPreviousCommand() {
|
---|
| 185 | String currentCommand = textCommand.getText().trim();
|
---|
| 186 |
|
---|
| 187 | int index = 0;
|
---|
| 188 | for (index = 0; index < commandHistory.size(); index++) {
|
---|
| 189 | if (currentCommand.equals(commandHistory.get(index))) {
|
---|
| 190 | break;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if (index > 0) {
|
---|
| 195 | textCommand.setText(commandHistory.get(index - 1));
|
---|
| 196 | textCommand.setSelection(commandHistory.get(index - 1).length());
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | private void setToNextCommand() {
|
---|
| 201 | String currentCommand = textCommand.getText().trim();
|
---|
| 202 |
|
---|
| 203 | int index = 0;
|
---|
| 204 | for (index = 0; index < commandHistory.size(); index++) {
|
---|
| 205 | if (currentCommand.equals(commandHistory.get(index))) {
|
---|
| 206 | break;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | if (index < (commandHistory.size() - 1)) {
|
---|
| 211 | textCommand.setText(commandHistory.get(index + 1));
|
---|
| 212 | textCommand.setSelection(commandHistory.get(index + 1).length());
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[570] | 216 | @Override
|
---|
| 217 | protected void checkSubclass() {
|
---|
| 218 | // Disable the check that prevents subclassing of SWT components
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[526] | 221 | }
|
---|