bug 3051 + cleaning
[scilab.git] / scilab / modules / console / src / java / org / scilab / modules / console / CompletionAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007-2008 - INRIA - Vincent COUVERT
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.console;
14
15 import java.awt.Point;
16 import java.awt.event.ActionEvent;
17 import java.util.List;
18 import java.util.Iterator;
19
20 import com.artenum.rosetta.core.action.AbstractConsoleAction;
21 import com.artenum.rosetta.interfaces.core.CompletionItem;
22
23 import org.scilab.modules.completion.Completion;
24
25 /**
26  * Class used when Scilab user asks for completion on the current edited line
27  * @author Vincent COUVERT
28  * @author Allan CORNET 
29  */
30 public class CompletionAction extends AbstractConsoleAction {
31         private static final long serialVersionUID = 1L;
32
33         /**
34          * Constructor
35          */
36         public CompletionAction() {
37                 super();
38         }
39
40         /**
41          * Threats the event
42          * @param e the action event that occured
43          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
44          */
45         public void actionPerformed(ActionEvent e) {
46                 Point location = configuration.getInputParsingManager().getWindowCompletionLocation();
47                 List<CompletionItem> completionItems = configuration.getCompletionManager().getCompletionItems();
48
49                 if (completionItems != null && completionItems.size() == 1) {
50                         /* Only one item returned, autoselected and appended to command line */
51                         configuration.getCompletionWindow().show(completionItems, location);
52                         configuration.getInputParsingManager().writeCompletionPart(configuration.getCompletionWindow().getCompletionResult());
53                         ((SciCompletionWindow) configuration.getCompletionWindow()).setVisible(false);
54                 } else if (completionItems != null && completionItems.size() != 0) {
55                         String [] completionArray = new String [completionItems.size()];
56                         
57                         int i = 0;
58                         Iterator < CompletionItem >  it = completionItems.iterator(); 
59                         while  (it.hasNext()) {  
60                                 CompletionItem currentItem = it.next();  
61                                 completionArray[i] = currentItem.getReturnValue();
62                                 i++;
63                         }
64             
65                         java.util.Arrays.sort(completionArray);
66                         String commonPartOfWord = Completion.getCommonPart(completionArray, completionItems.size() );
67
68                         if (commonPartOfWord.length()!= 0) {
69                                 if (configuration.getInputParsingManager().getPartLevel(0).length() != 0) {
70                                         configuration.getInputParsingManager().append(commonPartOfWord.substring(configuration.getInputParsingManager().getPartLevel(0).length()) );
71                                 }
72                         }
73                         configuration.getCompletionWindow().show(completionItems, location);
74                         
75                 }
76
77         }
78         
79 }