/*
* 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.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.gjt.sp.jedit.View;
import org.gjt.sp.jedit.textarea.Selection;
import functionality.LogSplit;
import functionality.SentenceContext;
/**
* Class to create and manage the split panel
*
* @author Marta Rodr�guez
* @author Teresa de Salas
* @author Ana Vargas
*
*/
public class Split {
/**
* actually view from jEdit text area
*/
private View v;
/**
* original sentence
*/
private String originalS;
/**
* new sentence
*/
private String splitS;
private JFrame splitFrame;
/**
* number of lines for original sentence
*/
private int numLinesOriginal;
/**
* number of lines for split sentence
*/
private int numLinesSplit;
/**
* number of lines when sentence is separated into lines
*/
private int numLines;
/**
* where the original sentence starts
*/
private int start;
/**
* offset where the original sentence ends
*/
private int end;
/**
* file log
*/
private LogSplit log;
/**
* arrayList to store the sentences
*/
private ArrayList<SentenceContext> array;
/**
* to register the order of the change made
*/
private int counter;
/**
* Constructor
* @param v: active view
* @param o: original sentence
* @param s: split sentence
* @param start: original sentence start offset
* @param end: original sentence end offset
*/
public Split(View v, String o, String s, int start, int end){
this.v = v;
originalS = o;
splitS = s;
this.start = start;
this.end = end;
numLinesOriginal = 0;
numLinesSplit = 0;
String fileAux = this.v.getBuffer().getDirectory(); /* returns where is the file located */
String filePath = fileAux.concat(this.v.getBuffer().getName());
filePath = filePath.concat(".xml");
log = new LogSplit(filePath);
array = log.getLogSplit();
counter = log.getCounter();
}
/**
*
* @return the visual component for the plugin
*/
private JPanel getSplit(){
JPanel showPanelSplit = new JPanel();
showPanelSplit.setLayout (new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
JTextArea originalSentenceLabel = new JTextArea(getNewDefinition(originalS));
numLinesOriginal = numLines; /* update original sentence number lines */
originalSentenceLabel.setEditable(false);
originalSentenceLabel.setBackground(null);
originalSentenceLabel.setBorder(BorderFactory.createTitledBorder(null, "Original sentence", 2, 2,
new Font("Monospaced", Font.PLAIN, 14), new Color(255,165,0)));
constraints.gridx = 0; //starts at column 0
constraints.gridy = 0; //starts at row 0
constraints.gridwidth = 4;
constraints.gridheight = 2;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
showPanelSplit.add(originalSentenceLabel, constraints);
JPanel splitSentencePanel = new JPanel();
splitSentencePanel.setBorder(BorderFactory.createTitledBorder(null, "Split sentence", 2, 2,
new Font("Monospaced", Font.PLAIN, 14), new Color(255,165,0)));
final JTextArea splitSentenceArea = new JTextArea(getNewDefinition(splitS));
if (numLines < 4) {
numLinesSplit = 4;
for (int i = numLines; i < 4 ; i++)
splitSentenceArea.append("\n");
}
else{
numLinesSplit = numLines; /* update original sentence number lines */
}
splitSentenceArea.setEditable(true);
JScrollPane splitSentenceScroll = new JScrollPane();
splitSentenceScroll.setViewportView(splitSentenceArea);
constraints.gridx = 0; //starts at column 0
constraints.gridy = 2; //starts at row 2
constraints.gridwidth = 4;
constraints.gridheight = 2;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
splitSentencePanel.add(splitSentenceScroll);
showPanelSplit.add(splitSentencePanel,constraints);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2));
JButton applyButton = new JButton("Apply ");
applyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String newSentence = rebuiltDefinition(splitSentenceArea.getText().toString());
v.getTextArea().setSelectedText(new Selection.Range(start,end),newSentence);
counter++;
String edited;
if (newSentence.equals(splitS)){
edited = "No";
}
else{
edited = "Yes";
}
SentenceContext sentence = new SentenceContext(originalS,newSentence,counter,edited, splitS);
if (!v.getBuffer().isUntitled()){
array.add(sentence);
log.setCounter(counter);
log.setLogSplit(array);
log.writeSplitXML();
}
splitFrame.dispose();
}
});
buttonPanel.add(applyButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
splitFrame.dispose();
}
});
buttonPanel.add(cancelButton);
constraints.gridx = 0; //starts at column 0
constraints.gridy = 4; //starts at row 4
constraints.gridwidth = 4;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.CENTER;
showPanelSplit.add(buttonPanel,constraints);
return showPanelSplit;
}
/**
* Create the Split' frame
*/
public void getFrameSplit(){
splitFrame = new JFrame("Split");
splitFrame.add(getSplit());
splitFrame.setEnabled(true);
splitFrame.setVisible(true);
splitFrame.setSize(275,150+20*(numLinesOriginal + numLinesSplit));
splitFrame.setLocation(700, 100);
URL urlImage = this.getClass().getClassLoader().getResource("jedit-icon48.png"); /* get the image url which is contains in the jar */
ImageIcon icon = new ImageIcon(urlImage); /* create the imageIcon with the url image */
splitFrame.setIconImage(icon.getImage()); /* puts the jedit image in our frame */
}
/**
* This method gets the definition with breaking lines
* @param definition: original sentence
*/
private String getNewDefinition(String definition){
String[] definitionTokens = definition.split(" ");
int numTokens = definitionTokens.length;
int countTokensProcessed = 0;
String newDefinition ="";
numLines = 0;
while (countTokensProcessed < numTokens) {
String line = "";
int numCharacters = 0;
while ((numCharacters < 30) && (countTokensProcessed < numTokens)
&& (definitionTokens[countTokensProcessed].length()<= (30-numCharacters))) {
line = line.concat(" ");
line = line.concat(definitionTokens[countTokensProcessed]);
numCharacters = definitionTokens[countTokensProcessed].length() + numCharacters + 1;
countTokensProcessed++;
}
numLines++;
newDefinition = newDefinition.concat(line);
if (countTokensProcessed != numTokens){
newDefinition = newDefinition.concat("\n");
}
}
return newDefinition;
}
/**
* This method remove intros and white spaces at the beginning of a new line
*/
private String rebuiltDefinition(String oldDef){
String newDef= "";
String[] linesTokens = oldDef.split("\n");
int lines = linesTokens.length;
for( int i = 0; i < lines ; i++){
linesTokens[i] = removeFirstWhiteSpace(linesTokens[i]);
if (i != 0){
newDef = newDef.concat(" ");
}
newDef = newDef.concat(linesTokens[i]);
}
return newDef;
}
/**
* This method removes the first white space in the line
* @param line
*/
private String removeFirstWhiteSpace(String line){
String newDef = "";
while (line.charAt(0) == ' '){
line = line.substring(1);
}
String[] tokens = line.split(" ");
for(int i = 0; i < tokens.length; i ++){
if (i != 0){
newDef = newDef.concat(" ");
}
newDef = newDef.concat(tokens[i]);
}
return newDef;
}
}