package pt.treps.autocompletion;
import org.fife.ui.autocomplete.AutoCompletion;
import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.CompletionProvider;
import org.fife.ui.autocomplete.ParameterizedCompletion;
import pt.treps.TReps;
import pt.treps.parser.MatchableShorthandCompletion;
import pt.treps.parser.Project;
import pt.treps.parser.ProjectTask;
import pt.treps.parser.Task;
import javax.swing.text.Caret;
import javax.swing.text.JTextComponent;
public class TimeAutoCompletion extends AutoCompletion {
public TimeAutoCompletion(CompletionProvider provider) {
super(provider);
}
protected void insertCompletion(Completion c) {
System.out.println("ENTRE: " + c.getReplacementText());
JTextComponent textComp = getTextComponent();
String alreadyEntered = c.getAlreadyEntered(textComp);
hidePopupWindow();
Caret caret = textComp.getCaret();
int dot = caret.getDot();
int len = alreadyEntered.length();
int start = dot-len;
Object itemObj = ((MatchableShorthandCompletion) c).getItem().getObject();
String token = null;
if (itemObj instanceof Project) {
try {
start = textComp.getDocument().getText(0, dot).lastIndexOf("$") + 1;
} catch (Exception exc) {
exc.printStackTrace();
}
}
if (itemObj instanceof Task) {
try {
start = textComp.getDocument().getText(0, dot).lastIndexOf("#") + 1;
} catch (Exception exc) {
exc.printStackTrace();
}
}
String replacement = getReplacementText(c, textComp.getDocument(), start, len);
caret.setDot(start);
caret.moveDot(dot);
textComp.replaceSelection(replacement);
if (isParameterAssistanceEnabled() &&
(c instanceof ParameterizedCompletion)) {
ParameterizedCompletion pc = (ParameterizedCompletion)c;
displayDescriptionToolTip(pc, true);
}
if (itemObj instanceof Project) {
((TimeCompletionProvider) this.provider).mainFrame.getComboBoxProjects().setSelectedItem(itemObj);
TReps.setSelectedProject((Project) itemObj);
}
if (itemObj instanceof Task) {
((TimeCompletionProvider) this.provider).mainFrame.getComboBoxTasks().setSelectedItem(itemObj);
}
if (itemObj instanceof ProjectTask) {
ProjectTask projectTask = (ProjectTask) itemObj;
((TimeCompletionProvider) this.provider).mainFrame.getComboBoxProjects().setSelectedItem(projectTask.getProject());
((TimeCompletionProvider) this.provider).mainFrame.getComboBoxTasks().setSelectedItem(projectTask.getTask());
}
}
}