source: trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/ConsoleTabComposite.java @ 1243

Last change on this file since 1243 was 1243, checked in by pharms, 11 years ago
  • improved autocompletion of console to also autocomplete to paths and objects in the global data container
  • Property svn:mime-type set to text/plain
File size: 7.6 KB
Line 
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
15package de.ugoe.cs.autoquest.ui.swt;
16
17import java.util.LinkedList;
18
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.widgets.Event;
21import org.eclipse.swt.widgets.Listener;
22import org.eclipse.swt.widgets.Text;
23import org.eclipse.swt.widgets.Label;
24import org.eclipse.swt.widgets.Composite;
25import org.eclipse.swt.widgets.Button;
26import org.eclipse.swt.layout.GridLayout;
27import org.eclipse.swt.layout.GridData;
28import org.eclipse.swt.custom.StyledText;
29import org.eclipse.swt.events.SelectionAdapter;
30import org.eclipse.swt.events.SelectionEvent;
31import org.eclipse.swt.events.TraverseEvent;
32import org.eclipse.swt.events.TraverseListener;
33
34import org.eclipse.swt.events.KeyAdapter;
35import org.eclipse.swt.events.KeyEvent;
36
37import de.ugoe.cs.util.FileTools;
38import de.ugoe.cs.util.StringTools;
39import de.ugoe.cs.util.console.CommandExecuter;
40import de.ugoe.cs.util.console.Console;
41import de.ugoe.cs.util.console.GlobalDataContainer;
42import de.ugoe.cs.util.console.listener.ICommandListener;
43
44/**
45 * <p>
46 * Implements the composite for the console tab in the applications main window.
47 * </p>
48 *
49 * @author Steffen Herbold
50 * @version 1.0
51 */
52public class ConsoleTabComposite extends Composite implements ICommandListener {
53
54    protected java.util.List<String> commandHistory = new LinkedList<String>();
55
56    protected Text textCommand;
57
58    protected StyledText textConsoleOutput;
59
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();
69        Console.getInstance().registerCommandListener(this);
70    }
71
72    @Override
73    public void commandNotification(String command) {
74        commandHistory.add(command);
75    }
76
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                }
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                }
99            }
100        });
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       
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
147    private void autoCompleteCommand() {
148        String prefix = textCommand.getText();
149        int end = textCommand.getCaretPosition() - 1;
150        int start = 0;
151       
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());
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   
216    @Override
217    protected void checkSubclass() {
218        // Disable the check that prevents subclassing of SWT components
219    }
220
221}
Note: See TracBrowser for help on using the repository browser.