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

Last change on this file since 2183 was 2046, checked in by pharms, 9 years ago
  • corrected auto completion
  • Property svn:mime-type set to text/plain
File size: 7.3 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 int commandHistoryIndex = 0;
57
58    protected Text textCommand;
59
60    protected StyledText textConsoleOutput;
61
62    /**
63     * Create the composite.
64     *
65     * @param parent
66     * @param style
67     */
68    public ConsoleTabComposite(Composite parent, int style) {
69        super(parent, style);
70        createContents();
71        Console.getInstance().registerCommandListener(this);
72    }
73
74    @Override
75    public void commandNotification(String command) {
76        commandHistory.add(command);
77        commandHistoryIndex = commandHistory.size();
78    }
79
80    private void createContents() {
81        setLayout(new GridLayout(3, false));
82
83        Label lblCommand = new Label(this, SWT.NONE);
84        lblCommand.setText("Command:");
85
86        textCommand = new Text(this, SWT.BORDER);
87        textCommand.addKeyListener(new KeyAdapter() {
88            @Override
89            public void keyReleased(KeyEvent e) {
90                if (e.keyCode == SWT.CR) {
91                    executeCommand();
92                }
93                else if (e.keyCode == SWT.TAB) {
94                    autoCompleteCommand();
95                }
96                else if (e.keyCode == SWT.ARROW_UP) {
97                    setToPreviousCommand();
98                }
99                else if (e.keyCode == SWT.ARROW_DOWN) {
100                    setToNextCommand();
101                }
102            }
103        });
104        textCommand.addTraverseListener(new TraverseListener() {
105            @Override
106            public void keyTraversed(TraverseEvent e) {
107                if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS))
108                {
109                    e.doit = false;
110                }
111            }
112           
113        });
114       
115        GridData gd_textCommand = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
116        gd_textCommand.widthHint = 304;
117        textCommand.setLayoutData(gd_textCommand);
118        textCommand.setFocus();
119
120        Button btnEnter = new Button(this, SWT.NONE);
121        btnEnter.addSelectionListener(new SelectionAdapter() {
122            @Override
123            public void widgetSelected(SelectionEvent e) {
124                executeCommand();
125            }
126        });
127        btnEnter.setText("Enter");
128
129        textConsoleOutput =
130            new StyledText(this, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL |
131                SWT.CANCEL);
132        GridData gd_textConsoleOutput = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
133        gd_textConsoleOutput.heightHint = 102;
134        gd_textConsoleOutput.widthHint = 456;
135        textConsoleOutput.setLayoutData(gd_textConsoleOutput);
136        textConsoleOutput.addListener(SWT.Modify, new Listener() {
137            public void handleEvent(Event e) {
138                textConsoleOutput.setTopIndex(textConsoleOutput.getLineCount() - 1);
139            }
140        });
141
142    }
143
144    private void executeCommand() {
145        String command = textCommand.getText().trim();
146        CommandExecuter.getInstance().exec(command);
147        textCommand.setText("");
148    }
149
150    private void autoCompleteCommand() {
151        String prefix = textCommand.getText();
152        int end = textCommand.getCaretPosition() - 1;
153        int start = 0;
154       
155        for (int i = end; i >= 0; i--) {
156            if (Character.isWhitespace(prefix.charAt(i))) {
157                start = i + 1;
158                break;
159            }
160        }
161       
162        String textPrefix = prefix.substring(0, start);
163        String textSuffix = prefix.substring(end + 1);
164        prefix = prefix.substring(start, end + 1);
165       
166        String completedPrefix = null;
167        if ("".equals(textCommand.getText().substring(0, start).trim())) {
168            // search for a command completion
169            completedPrefix = CommandExecuter.getInstance().autoCompleteCommand(prefix);
170        }
171        else {
172            // complete an object in the data store or a file name
173            completedPrefix = FileTools.autoCompletePath(prefix);
174           
175            if (prefix.equals(completedPrefix)) {
176                // file completion wasn't possible, so try to complete using data objects
177                String[] completions =
178                    GlobalDataContainer.getInstance().getAllKeys().toArray(new String[0]);
179                completedPrefix = StringTools.autocomplete(prefix, completions);
180            }
181        }
182       
183        textCommand.setText(textPrefix + completedPrefix + textSuffix);
184        textCommand.setSelection(textPrefix.length() + completedPrefix.length());
185    }
186   
187    private void setToPreviousCommand() {
188        if (commandHistoryIndex > 0) {
189            commandHistoryIndex--;
190           
191            textCommand.setText(commandHistory.get(commandHistoryIndex));
192            textCommand.setSelection(commandHistory.get(commandHistoryIndex).length());
193        }
194    }
195
196    private void setToNextCommand() {
197        if (commandHistoryIndex < (commandHistory.size() - 1)) {
198            commandHistoryIndex++;
199           
200            textCommand.setText(commandHistory.get(commandHistoryIndex));
201            textCommand.setSelection(commandHistory.get(commandHistoryIndex).length());
202        }
203    }
204   
205    @Override
206    protected void checkSubclass() {
207        // Disable the check that prevents subclassing of SWT components
208    }
209
210}
Note: See TracBrowser for help on using the repository browser.