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.util.LinkedList;
|
---|
18 |
|
---|
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;
|
---|
31 | import org.eclipse.swt.events.TraverseEvent;
|
---|
32 | import org.eclipse.swt.events.TraverseListener;
|
---|
33 |
|
---|
34 | import org.eclipse.swt.events.KeyAdapter;
|
---|
35 | import org.eclipse.swt.events.KeyEvent;
|
---|
36 |
|
---|
37 | import de.ugoe.cs.util.console.CommandExecuter;
|
---|
38 | import de.ugoe.cs.util.console.Console;
|
---|
39 | import de.ugoe.cs.util.console.listener.ICommandListener;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * <p>
|
---|
43 | * Implements the composite for the console tab in the applications main window.
|
---|
44 | * </p>
|
---|
45 | *
|
---|
46 | * @author Steffen Herbold
|
---|
47 | * @version 1.0
|
---|
48 | */
|
---|
49 | public class ConsoleTabComposite extends Composite implements ICommandListener {
|
---|
50 |
|
---|
51 | protected java.util.List<String> commandHistory = new LinkedList<String>();
|
---|
52 |
|
---|
53 | protected Text textCommand;
|
---|
54 |
|
---|
55 | protected StyledText textConsoleOutput;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Create the composite.
|
---|
59 | *
|
---|
60 | * @param parent
|
---|
61 | * @param style
|
---|
62 | */
|
---|
63 | public ConsoleTabComposite(Composite parent, int style) {
|
---|
64 | super(parent, style);
|
---|
65 | createContents();
|
---|
66 | Console.getInstance().registerCommandListener(this);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void commandNotification(String command) {
|
---|
71 | commandHistory.add(command);
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void createContents() {
|
---|
75 | setLayout(new GridLayout(3, false));
|
---|
76 |
|
---|
77 | Label lblCommand = new Label(this, SWT.NONE);
|
---|
78 | lblCommand.setText("Command:");
|
---|
79 |
|
---|
80 | textCommand = new Text(this, SWT.BORDER);
|
---|
81 | textCommand.addKeyListener(new KeyAdapter() {
|
---|
82 | @Override
|
---|
83 | public void keyReleased(KeyEvent e) {
|
---|
84 | if (e.keyCode == SWT.CR) {
|
---|
85 | executeCommand();
|
---|
86 | }
|
---|
87 | else if (e.keyCode == SWT.TAB) {
|
---|
88 | autoCompleteCommand();
|
---|
89 | }
|
---|
90 | else if (e.keyCode == SWT.ARROW_UP) {
|
---|
91 | setToPreviousCommand();
|
---|
92 | }
|
---|
93 | else if (e.keyCode == SWT.ARROW_DOWN) {
|
---|
94 | setToNextCommand();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | });
|
---|
98 | textCommand.addTraverseListener(new TraverseListener() {
|
---|
99 | @Override
|
---|
100 | public void keyTraversed(TraverseEvent e) {
|
---|
101 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS))
|
---|
102 | {
|
---|
103 | e.doit = false;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | });
|
---|
108 |
|
---|
109 | GridData gd_textCommand = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
|
---|
110 | gd_textCommand.widthHint = 304;
|
---|
111 | textCommand.setLayoutData(gd_textCommand);
|
---|
112 | textCommand.setFocus();
|
---|
113 |
|
---|
114 | Button btnEnter = new Button(this, SWT.NONE);
|
---|
115 | btnEnter.addSelectionListener(new SelectionAdapter() {
|
---|
116 | @Override
|
---|
117 | public void widgetSelected(SelectionEvent e) {
|
---|
118 | executeCommand();
|
---|
119 | }
|
---|
120 | });
|
---|
121 | btnEnter.setText("Enter");
|
---|
122 |
|
---|
123 | textConsoleOutput =
|
---|
124 | new StyledText(this, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL |
|
---|
125 | SWT.CANCEL);
|
---|
126 | GridData gd_textConsoleOutput = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
|
---|
127 | gd_textConsoleOutput.heightHint = 102;
|
---|
128 | gd_textConsoleOutput.widthHint = 456;
|
---|
129 | textConsoleOutput.setLayoutData(gd_textConsoleOutput);
|
---|
130 | textConsoleOutput.addListener(SWT.Modify, new Listener() {
|
---|
131 | public void handleEvent(Event e) {
|
---|
132 | textConsoleOutput.setTopIndex(textConsoleOutput.getLineCount() - 1);
|
---|
133 | }
|
---|
134 | });
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void executeCommand() {
|
---|
139 | String command = textCommand.getText().trim();
|
---|
140 | CommandExecuter.getInstance().exec(command);
|
---|
141 | textCommand.setText("");
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void autoCompleteCommand() {
|
---|
145 | String commandPrefix = textCommand.getText().trim();
|
---|
146 | String completedPrefix = CommandExecuter.getInstance().autoCompleteCommand(commandPrefix);
|
---|
147 |
|
---|
148 | textCommand.setText(completedPrefix.toString());
|
---|
149 | textCommand.setSelection(completedPrefix.length());
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void setToPreviousCommand() {
|
---|
153 | String currentCommand = textCommand.getText().trim();
|
---|
154 |
|
---|
155 | int index = 0;
|
---|
156 | for (index = 0; index < commandHistory.size(); index++) {
|
---|
157 | if (currentCommand.equals(commandHistory.get(index))) {
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (index > 0) {
|
---|
163 | textCommand.setText(commandHistory.get(index - 1));
|
---|
164 | textCommand.setSelection(commandHistory.get(index - 1).length());
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void setToNextCommand() {
|
---|
169 | String currentCommand = textCommand.getText().trim();
|
---|
170 |
|
---|
171 | int index = 0;
|
---|
172 | for (index = 0; index < commandHistory.size(); index++) {
|
---|
173 | if (currentCommand.equals(commandHistory.get(index))) {
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (index < (commandHistory.size() - 1)) {
|
---|
179 | textCommand.setText(commandHistory.get(index + 1));
|
---|
180 | textCommand.setSelection(commandHistory.get(index + 1).length());
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | protected void checkSubclass() {
|
---|
186 | // Disable the check that prevents subclassing of SWT components
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|