package projetV1;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ArmListener;
import org.eclipse.swt.events.ArmEvent;
import org.eclipse.swt.widgets.Link;
public class Analyser extends Composite {
private Text texte;
final Text prononciation;
final Text nature;
ArrayList<ArrayList> a = new ArrayList<ArrayList>();
private Text definition;
boolean s;
boolean admin;
/**
* Create the composite.
*
* @param parent
* @param style
*/
public Analyser(final Composite parent, int style) {
super(parent, SWT.NONE);
this.setSize(729, 538);
if(MainWindow.userInfo.get(5).toString().equals("1")){
admin=true;
}
else{
admin=false;
}
setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.NORMAL));
s = false;
Group group_1 = new Group(this, SWT.NONE);
group_1.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
group_1.setForeground(SWTResourceManager.getColor(128, 128, 128));
group_1.setBounds(10, 10, 339, 471);
final List listMot = new List(group_1, SWT.BORDER | SWT.V_SCROLL);
listMot.setForeground(SWTResourceManager.getColor(0, 0, 0));
listMot.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
listMot.setBounds(10, 208, 320, 237);
TabFolder tabFolder = new TabFolder(group_1, SWT.NONE);
tabFolder.setBounds(10, 31, 320, 117);
tabFolder.setForeground(SWTResourceManager.getColor(0, 0, 0));
tabFolder.setBackground(SWTResourceManager.getColor(0, 0, 0));
TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
tbtmNewItem.setText("Texte \u00E0 analyser");
final Composite composite = new Composite(tabFolder, SWT.NONE);
tbtmNewItem.setControl(composite);
texte = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
texte.setBounds(10, 23, 292, 58);
Link link = new Link(composite, SWT.NONE);
link.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
FileBrowser fb = new FileBrowser();
String path = fb.init(getParent().getShell());
if (path != null) {
String s = path.substring(path.lastIndexOf('\\') + 1);
try{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String strLine;
while ((strLine = br.readLine()) != null) {
texte.setText(texte.getText()+" "+strLine);
}
}
catch(FileNotFoundException e1){
System.out.println(e1.getMessage());
}
catch(IOException e1){
System.out.println(e1.getMessage());
}
}
}
});
link.setBounds(205, 2, 107, 15);
link.setText("<a>Importer un fichier</a>");
final Label stat = new Label(group_1, SWT.NONE);
stat.setBounds(10, 451, 320, 15);
stat.setForeground(SWTResourceManager.getColor(128, 128, 128));
stat.setAlignment(SWT.RIGHT);
final Button btnAnalyser = new Button(group_1, SWT.NONE);
btnAnalyser.setEnabled(false);
btnAnalyser.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
Txt T = new Txt();
DbConn con = new DbConn();
T.setText(texte.getText());
T.correct();
texte.setText(T.getText());
a.clear();
con.connect();
listMot.removeAll();
ArrayList b = T.mots();
String mot = "";
for (int i = 0; i < b.size(); i++) {
mot = b.get(i).toString();
ArrayList<ArrayList> c = new ArrayList<ArrayList>();
c = (con.sqlQuery("select * from fr_fr where terme=\'"
+ T.mots().get(i).toString().toLowerCase() + "\'"));
if (!c.isEmpty()) {
a.add(c.get(0));
} else {
a.add(new ArrayList<String>());
}
listMot.add(mot);
}
stat.setText(b.size() + " mots | " + texte.getText().length()
+ " lettres");
}
});
btnAnalyser.setBounds(255, 154, 75, 25);
btnAnalyser.setGrayed(true);
btnAnalyser.setFont(SWTResourceManager.getFont("Segoe UI", 9,
SWT.NORMAL));
btnAnalyser.setText("Analyser");
Label lblLesMots = new Label(group_1, SWT.NONE);
lblLesMots.setBounds(10, 187, 55, 15);
lblLesMots.setText("Les mots:");
Group group = new Group(this, SWT.SHADOW_ETCHED_IN);
group.setBounds(351, 10, 320, 471);
final Label terme = new Label(group, SWT.NONE);
terme.setBounds(40, 10, 230, 36);
terme.setFont(SWTResourceManager.getFont("Segoe UI", 15, SWT.ITALIC));
terme.setAlignment(SWT.CENTER);
terme.setForeground(SWTResourceManager.getColor(30, 144, 255));
Label lblPrononciation = new Label(group, SWT.NONE);
lblPrononciation.setBounds(13, 55, 84, 15);
lblPrononciation.setText("Prononciation:");
prononciation = new Text(group, SWT.BORDER);
prononciation.setBounds(103, 52, 167, 21);
prononciation.setEditable(false);
nature = new Text(group, SWT.BORDER);
nature.setBounds(103, 83, 193, 21);
nature.setEditable(false);
Label lblNature = new Label(group, SWT.NONE);
lblNature.setBounds(13, 86, 84, 15);
lblNature.setText("Nature:");
Label lblDfinition = new Label(group, SWT.NONE);
lblDfinition.setBounds(13, 116, 84, 15);
lblDfinition.setText("D\u00E9finition:");
Label addedBy = new Label(group, SWT.NONE);
addedBy.setForeground(SWTResourceManager.getColor(128, 128, 128));
addedBy.setAlignment(SWT.RIGHT);
addedBy.setBounds(103, 446, 193, 15);
Menu menu = new Menu(group);
group.setMenu(menu);
MenuItem mntmCopier = new MenuItem(menu, SWT.NONE);
mntmCopier.setText("Copier");
definition = new Text(group, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.CANCEL);
definition.setBounds(103, 114, 193, 323);
definition.setEditable(false);
final Button btnSpeech = new Button(group, SWT.NONE);
btnSpeech.setEnabled(false);
btnSpeech.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
Speech.speech(terme.getText().replace("\"", ""));
}
});
btnSpeech.setImage(SWTResourceManager.getImage(Analyser.class, "/projetV1/images/speaker.png"));
btnSpeech.setBounds(271, 50, 25, 25);
final Button add = new Button(this, SWT.RIGHT);
add.setEnabled(false);
add.setAlignment(SWT.CENTER);
add.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/bullet_add.png"));
add.setBounds(677, 17, 38, 38);
final Button modify = new Button(this, SWT.RIGHT);
modify.setEnabled(false);
modify.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/edit.png"));
modify.setAlignment(SWT.CENTER);
modify.setBounds(677, 59, 38, 38);
final Button export = new Button(this, SWT.RIGHT);
final Button remove = new Button(this, SWT.RIGHT);
remove.setEnabled(false);
remove.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
DialogBox dB = new DialogBox(getParent().getShell(), 4,
"Suppression",
"Etes vous sure que vous voulez supprimer ce mot?");
int i=dB.open();
if (i==1){
DbConn db = new DbConn();
if(db.connect()){
add.setEnabled(true);
remove.setEnabled(false);
modify.setEnabled(false);
export.setEnabled(false);
db.sqlUpdate("DELETE FROM fr_fr WHERE terme = \'"
+ listMot.getItem(listMot.getSelectionIndex())
.toLowerCase() + "\'");
db.disconnect();
}
}
}
});
remove.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/bullet_deny.png"));
remove.setAlignment(SWT.CENTER);
remove.setBounds(677, 101, 38, 38);
export.setEnabled(false);
export.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
ExportDialog ex = new ExportDialog(getParent().getParent()
.getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
ex.open(a, listMot.getSelectionIndex());
}
});
export.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/file_export.png"));
export.setAlignment(SWT.CENTER);
export.setBounds(677, 149, 38, 38);
Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
label.setBounds(677, 143, 36, 2);
listMot.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
terme.setText("\""
+ listMot.getItem(listMot.getSelectionIndex()) + "\"");
int index = listMot.getSelectionIndex();
if ((a.get(index)).isEmpty()) {
prononciation.setText("");
nature.setText("");
definition.setText("Ce mot n'existe. Vous pouvez l'ajouter � la base de donn�es.");
add.setEnabled(true);
modify.setEnabled(false);
remove.setEnabled(false);
export.setEnabled(false);
btnSpeech.setEnabled(false);
} else {
add.setEnabled(false);
modify.setEnabled(true);
if (admin){
remove.setEnabled(true);
}
export.setEnabled(true);
btnSpeech.setEnabled(true);
prononciation.setText(a.get(listMot.getSelectionIndex())
.get(1).toString());
nature.setText(a.get(listMot.getSelectionIndex()).get(2)
.toString());
definition.setText(a.get(listMot.getSelectionIndex())
.get(3).toString());
}
}
});
add.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
if (s) {
s = false;
DbConn db = new DbConn();
if(db.connect()){
db.sqlUpdate("INSERT INTO fr_fr VALUES (\'"
+ listMot.getItem(listMot.getSelectionIndex())
+ "\', \'" + prononciation.getText() + "\',\'"
+ nature.getText() + "\',\'" + definition.getText()
+ "\')");
db.disconnect();
nature.setEditable(false);
prononciation.setEditable(false);
definition.setEditable(false);
export.setEnabled(true);
modify.setEnabled(true);
add.setEnabled(false);
if (admin){
remove.setEnabled(true);
}
add.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/bullet_add.png"));
}
} else {
s = true;
nature.setEditable(true);
nature.setText("");
prononciation.setEditable(true);
prononciation.setText("");
definition.setEditable(true);
definition.setText("");
export.setEnabled(false);
modify.setEnabled(false);
remove.setEnabled(false);
add.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/bullet_accept.png"));
}
}
});
modify.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
if (s) {
s = false;
nature.setEditable(false);
prononciation.setEditable(false);
definition.setEditable(false);
modify.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/edit.png"));
export.setEnabled(true);
add.setEnabled(true);
if (admin){
remove.setEnabled(true);
}
} else {
s = true;
nature.setEditable(true);
prononciation.setEditable(true);
definition.setEditable(true);
modify.setImage(SWTResourceManager.getImage(Analyser.class,
"/projetV1/images/bullet_accept.png"));
export.setEnabled(false);
add.setEnabled(false);
remove.setEnabled(false);
}
}
});
texte.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
int nbr = texte.getText().length();
// charNbr.setText("" + (texte.getTextLimit() - nbr));
if ((nbr == 0)
|| (texte.getText().length() > texte.getTextLimit()))
btnAnalyser.setEnabled(false);
else
btnAnalyser.setEnabled(true);
if (texte.getText().length() > texte.getTextLimit()) {
btnAnalyser.setForeground(SWTResourceManager.getColor(56));
} else {
}
}
});
mntmCopier.addArmListener(new ArmListener() {
public void widgetArmed(ArmEvent e) {
Clipboard clipboard = new Clipboard(getDisplay());
TextTransfer textTransfer = TextTransfer.getInstance();
clipboard.setContents(
new String[] { "Terme: " + terme.getText()
+ "\nPrononciation: " + prononciation.getText()
+ "\n Nature: " + nature.getText()
+ "\nDescription: " + definition.getText() },
new Transfer[] { textTransfer });
clipboard.dispose();
}
});
}
}