Changeset 1236


Ignore:
Timestamp:
06/27/13 17:14:16 (11 years ago)
Author:
pharms
Message:
  • added autocompletion and history of commands
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/ConsoleTabComposite.java

    r927 r1236  
    1515package de.ugoe.cs.autoquest.ui.swt; 
    1616 
     17import java.util.LinkedList; 
     18 
    1719import org.eclipse.swt.SWT; 
    1820import org.eclipse.swt.widgets.Event; 
     
    2729import org.eclipse.swt.events.SelectionAdapter; 
    2830import org.eclipse.swt.events.SelectionEvent; 
     31import org.eclipse.swt.events.TraverseEvent; 
     32import org.eclipse.swt.events.TraverseListener; 
    2933 
    3034import org.eclipse.swt.events.KeyAdapter; 
     
    3236 
    3337import de.ugoe.cs.util.console.CommandExecuter; 
     38import de.ugoe.cs.util.console.Console; 
     39import de.ugoe.cs.util.console.listener.ICommandListener; 
    3440 
    3541/** 
     
    4147 * @version 1.0 
    4248 */ 
    43 public class ConsoleTabComposite extends Composite { 
     49public class ConsoleTabComposite extends Composite implements ICommandListener { 
     50 
     51    protected java.util.List<String> commandHistory = new LinkedList<String>(); 
    4452 
    4553    protected Text textCommand; 
     
    5664        super(parent, style); 
    5765        createContents(); 
     66        Console.getInstance().registerCommandListener(this); 
     67    } 
     68 
     69    @Override 
     70    public void commandNotification(String command) { 
     71        commandHistory.add(command); 
    5872    } 
    5973 
     
    7185                    executeCommand(); 
    7286                } 
     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                } 
    7396            } 
    7497        }); 
     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         
    75109        GridData gd_textCommand = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1); 
    76110        gd_textCommand.widthHint = 304; 
     
    108142    } 
    109143 
     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     
    110184    @Override 
    111185    protected void checkSubclass() { 
Note: See TracChangeset for help on using the changeset viewer.