/*
* Copyright (C) 2012 Marta Rodr�guez, Teresa de Salas, Ana Vargas
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* http://www.gnu.org/licenses/.
*/
import java.awt.Color;
import java.awt.Cursor;
import javax.swing.JOptionPane;
import functionality.Models;
import functionality.SentenceOnText;
import functionality.SentencesToTree;
import gatchan.highlight.HighlightPluginSplit;
import gatchan.highlight.HighlightSplit;
import opennlp.tools.parser.ParserModel;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.TokenizerModel;
import org.gjt.sp.jedit.EditPlugin;
import org.gjt.sp.jedit.View;
import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.jedit.textarea.JEditTextArea;
/**
*
* @author Marta Rodr�guez
* @author Teresa de Salas
* @author Ana Vargas
*
*/
public class SplitPlugin extends EditPlugin{
/**
* object to manage highlighted words
*/
private static HighlightPluginSplit hp;
/**
* the start offset of the sentence we want to process
*/
private static int start;
/**
* the end offset of the sentence we want to process
*/
private static int end;
/**
* boolean than indicates if word must be or not highlighted
*/
private static boolean enableHighlight;
/**
* object to manage the tokenizer's model
*/
private static TokenizerModel tModel;
/**
* model for sentences
*/
private static SentenceModel sModel;
/**
* model for parsing
*/
private static ParserModel pModel;
/**
* boolean that indicates if models are loaded
*/
private static boolean loadModels;
/**
* plugin's name
*/
public static final String NAME = "Split";
/**
* constructor
*/
public SplitPlugin(){
super();
enableHighlight = false;
loadModels = false;
hp = new HighlightPluginSplit();
hp.start();
}
/**
* stop method
*/
public void stop(){
hp.stop();
}
/**
* this method loads models for this plugin
* @param view: active view
*/
public final static void enable(View view){
if(!loadModels){
jEdit.getActiveView().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Models mod = new Models();
tModel = mod.tokModel();
sModel = mod.sentModel();
pModel = mod.parsModel();
loadModels = true;
jEdit.getActiveView().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
HighlightPluginSplit.highlightThis(view.getTextArea(),"and",Color.green);
enableHighlight = true;
}
/**
* this method remove all highlighted words
*/
public final static void disable(){
HighlightPluginSplit.removeAllHighlights();
enableHighlight = false;
}
/**
* this method shows the plugin interface if there is an "and" selected
* @param view: active view
*/
public final static void split(View view){
if (!view.getTextArea().getText().equals("")){
String selected = view.getTextArea().getSelectedText();
if (selected == null || selected.equals("")){
if(enableHighlight) JOptionPane.showMessageDialog(null, "Please select a highlighted word");
else JOptionPane.showMessageDialog(null, "Please activate Enable to Split");
}
else{
HighlightSplit highlight = new HighlightSplit(selected,Color.black);
if(enableHighlight){
if (HighlightPluginSplit.getHighlightManager().containsHighlight(highlight)){
String originalS = getOriginalSentence();
SentencesToTree st = new SentencesToTree(originalS, tModel);
String splitS = st.toTree(pModel);
if(splitS != null){
Split split = new Split(view, originalS,splitS,start,end);
split.getFrameSplit();
}
else JOptionPane.showMessageDialog(null, "There is not possible to split");
}
else{
JOptionPane.showMessageDialog(null, "Please select a highlighted word");
}
}
else{
JOptionPane.showMessageDialog(null, "Please activate Enable to Split");
}
}
}
else{
JOptionPane.showMessageDialog(null, "There is no text");
}
}
/**
* this method remove the highlight associated with the parameter word
*/
public final static void finishHighlight(String word){
HighlightSplit h = new HighlightSplit(word,Color.black);
HighlightPluginSplit.getHighlightManager().removeRow(h);
}
/**
* this method gets a sentence depending on the caret position
* @return original sentence
*/
private static String getOriginalSentence(){
JEditTextArea text = jEdit.getActiveView().getTextArea();
int caret = text.getCaretPosition();
SentenceOnText sent = new SentenceOnText(text,caret);
sent.getSentenceForWord(sModel);
start = sent.getStart();
end = sent.getEnd();
return text.getText(start, end - start);
}
}