org.phrasebook.transword.client.CwAdminTerminBox.java Source code

Java tutorial

Introduction

Here is the source code for org.phrasebook.transword.client.CwAdminTerminBox.java

Source

/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.phrasebook.transword.client;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.phrasebook.transword.client.model.Antonym;
import org.phrasebook.transword.client.model.Association;
import org.phrasebook.transword.client.model.Collocation;
import org.phrasebook.transword.client.model.Example;
import org.phrasebook.transword.client.model.FixedExpression;
import org.phrasebook.transword.client.model.Grammar;
import org.phrasebook.transword.client.model.LexNote;
import org.phrasebook.transword.client.model.Synonym;
import org.phrasebook.transword.shared.services.FixedExpressionServiceAsync;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.NoSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel;

/**
 * Dictionary administration subsystem.
 */
public class CwAdminTerminBox extends Content {

    // Data Grid
    //private ListDataProvider<FixedExpression> dataProvider;

    public final FilteredListDataProvider<FixedExpression> dataProvider = new FilteredListDataProvider<FixedExpression>(
            new IFilter<FixedExpression>() {

                public boolean isValid(FixedExpression value, String filter) {
                    if (filter == null || value == null)
                        return true;
                    return value.getCoreString().toLowerCase().contains(filter.toLowerCase())
                            || value.getDefinition_short().contains(filter.toLowerCase());
                }
            });

    /**
     * The main DataGrid.
     */
    private DataGrid<FixedExpression> dataGrid;

    /**
     * The pager used to change the range of data.
     */
    private SimplePager pager;

    private List<FixedExpression> dataList;

    private final FixedExpressionServiceAsync feService = FixedExpressionServiceAsync.Util.getInstance();

    private static final String SERVER_ERROR = "An error occurred while "
            + "attempting to contact the server. Please check your network "
            + "connection and try again. The error is : ";

    /**
     * The constants used in this Content Widget.
     */
    public static interface CwConstants extends Constants {
        String cwAdminTerminInfo();

        String cwAdminTerminDescription();

        String cwAdminTerminName();

        String cwFixedExpressionName();

        String cwFixedExpressionMean();

        String cwFixedExpressionEdNote();

        String cwFixedExpressionLink();

        String cwFixedUsageStyle();

        String cwFixedEnglish();

        String cwFixedIgnoreOnSearch();

        String cwFixedDoNotDisplay();

        String cwNewFE();
    }

    /**
     * An instance of the constants.
     */
    private final CwConstants constants;

    /**
     * Constructor.
     * 
     * @param constants
     *            the constants
     */
    public CwAdminTerminBox(CwConstants constants) {

        super(constants.cwAdminTerminName(), constants.cwAdminTerminDescription());
        this.constants = constants;

    }

    class NewEditFEHandler implements ClickHandler, KeyUpHandler {

        final ListBox antonyms = new ListBox(true);

        final ListBox synonyms = new ListBox(true);

        // disabled by Kaum
        // final List Box Associations = new ListBox(true);
        final ListBox lexnotes = new ListBox(true);

        final ListBox examples = new ListBox(true);

        final List<Example> exList = new ArrayList<Example>();

        final ListBox collocations = new ListBox(true);

        final List<Collocation> colList = new ArrayList<Collocation>();

        final ListBox grammars = new ListBox(true);

        final List<Grammar> grList = new ArrayList<Grammar>();

        FixedExpression fe = new FixedExpression();

        public void onKeyUp(KeyUpEvent event) {

        }

        public void onClick(ClickEvent event) {
            showEditDialogBox();
        }

        private void showEditDialogBox() {

            feService.findFixedExpression(selExpression.getId(), new AsyncCallback<FixedExpression>() {

                public void onFailure(Throwable caught) {
                    Window.alert("FOUND EXCEPTION: please email jr.pathfinder@gmail.com if you see this error <br/>"
                            + caught.toString() + selExpression.getId());
                }

                public void onSuccess(FixedExpression result) {

                    fe = result;
                }

            });

            // Timer delay to get objects in time

            Timer t = new Timer() {

                @Override
                public void run() {

                    // Filling antonyms

                    antonyms.clear();
                    for (Antonym f : fe.getAntonyms()) {
                        antonyms.addItem(f.getAntonym());
                    }

                    synonyms.clear();
                    for (Synonym f : fe.getSynonyms()) {
                        synonyms.addItem(f.getSynonym());
                    }

                    //               associations.clear();
                    //               for (Association f : fe.getAssociations()) {
                    //                  associations.addItem(f.getAssociation());
                    //               }
                    //               
                    collocations.clear();
                    colList.clear();
                    for (Collocation f : fe.getCollocations()) {
                        collocations.addItem(f.getCollocation());
                        colList.add(f);
                    }

                    lexnotes.clear();
                    for (LexNote f : fe.getLexnotes()) {
                        lexnotes.addItem(f.getLexNote());
                    }

                    examples.clear();
                    exList.clear();
                    for (Example f : fe.getExamples()) {
                        examples.addItem(f.getExample());
                        exList.add(f);
                    }

                    grammars.clear();
                    grList.clear();
                    for (Grammar f : fe.getGrammars()) {
                        grammars.addItem(f.getGrammar());
                        grList.add(f);
                    }

                    final DialogBox newPhrase = new DialogBox();
                    newPhrase.setText("Edit Fixed Expression");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Update");

                    // Add a handler to close the DialogBox
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newPhrase.hide();
                        }
                    });

                    // Create a vertical panel to manage phrases
                    DockPanel ap = new DockPanel();
                    ap.setSpacing(3);
                    final HorizontalPanel hPanel = new HorizontalPanel();
                    hPanel.setSpacing(3);
                    final VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    HTML label = new HTML(constants.cwAdminTerminInfo());
                    label.ensureDebugId("cwNewFE-label");
                    ap.add(label, DockPanel.NORTH);

                    antonyms.setWidth("100px");

                    synonyms.setWidth("100px");

                    //associations.setWidth("100px");

                    collocations.setWidth("100px");

                    examples.setWidth("100px");

                    grammars.setWidth("100px");

                    lexnotes.setWidth("100px");

                    // Fixed Expression CoreString
                    HTML name = new HTML("<b>Phrase</b>");
                    vPanel.add(name);
                    HTML labelCore = new HTML(constants.cwFixedExpressionName());
                    vPanel.add(labelCore);
                    final TextBox core = new TextBox();
                    final Hidden hid = new Hidden(fe.getId().toString());
                    hid.setValue(fe.getId().toString());
                    core.setText(fe.getCoreString());
                    vPanel.add(core);

                    // Fixed Expression's Meaning
                    HTML labelMean = new HTML(constants.cwFixedExpressionMean());
                    vPanel.add(labelMean);
                    final TextBox mean = new TextBox();
                    vPanel.add(mean);
                    mean.setText(fe.getDefinition_short());

                    // Fixed Expression's English Translation
                    HTML labelEng = new HTML(constants.cwFixedEnglish());
                    vPanel.add(labelEng);
                    final TextArea eng = new TextArea();
                    eng.setText(fe.getEngTranslation());
                    vPanel.add(eng);

                    // Fixed Expression's Usage Style
                    HTML labelUsStyle = new HTML(constants.cwFixedUsageStyle());
                    vPanel.add(labelUsStyle);
                    final TextBox style = new TextBox();
                    style.setText(fe.getUsageStyle());
                    vPanel.add(style);

                    // Fixed Expression's Editor's Note
                    HTML labelEdNote = new HTML(constants.cwFixedExpressionEdNote());
                    vPanel.add(labelEdNote);
                    final TextArea note = new TextArea();
                    note.setText(fe.getEditorsNotes());
                    vPanel.add(note);

                    // Fixed Expression's Internet Link
                    HTML labelLink = new HTML(constants.cwFixedExpressionLink());
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    link.setText(fe.getInternetadr());
                    vPanel.add(link);

                    // Fixed Expression's Ignore on Search
                    HTML labelIgnore = new HTML(constants.cwFixedIgnoreOnSearch());
                    vPanel.add(labelIgnore);
                    final CheckBox ignore = new CheckBox();

                    // ignore.setChecked(fe.getIgnore_on_search());
                    ignore.setEnabled(false);
                    vPanel.add(ignore);

                    // Fixed Expression's do not display
                    HTML labelDont = new HTML(constants.cwFixedDoNotDisplay());
                    vPanel.add(labelDont);
                    final CheckBox dont = new CheckBox();
                    dont.setEnabled(false);
                    vPanel.add(dont);
                    hPanel.add(vPanel);

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Update the fixed expression
                            if (!core.getText().trim().equals("")

                                    && !mean.getText().trim().equals("")) {

                                fe.setId(fe.getId());
                                fe.setCoreString(core.getText().trim());
                                fe.setDefinition_short(mean.getText().trim());
                                fe.setEngTranslation(eng.getText().trim());
                                fe.setEditorsNotes(note.getText().trim());
                                fe.setUsageStyle(style.getText().trim());
                                fe.setIgnore_on_search(ignore.getText().trim());
                                fe.setDo_not_display(dont.getText().trim());
                                fe.setInternetadr(link.getText().trim());

                                List<Antonym> availableAntonyms = new ArrayList<Antonym>();
                                List<Synonym> availableSynonyms = new ArrayList<Synonym>();
                                //List<Association> availableAssociations = new ArrayList<Association>();
                                List<LexNote> availableLexNotes = new ArrayList<LexNote>();

                                //List <Grammar>     availableGrammars     = new ArrayList<Grammar>();
                                //List <Example>     availableExamples     = new ArrayList<Example>();
                                //List <Collocation> availableCollocation  = new ArrayList<Collocation>();

                                if (synonyms.getItemCount() > 0) {
                                    for (int i = 0; i < synonyms.getItemCount(); i++) {
                                        availableSynonyms.add(new Synonym(synonyms.getItemText(i)));
                                    }
                                }
                                fe.setSynonyms(availableSynonyms);

                                if (antonyms.getItemCount() > 0) {
                                    for (int i = 0; i < antonyms.getItemCount(); i++) {
                                        availableAntonyms.add(new Antonym(antonyms.getItemText(i)));
                                    }
                                }
                                fe.setAntonyms(availableAntonyms);

                                if (lexnotes.getItemCount() > 0) {
                                    for (int i = 0; i < lexnotes.getItemCount(); i++) {
                                        availableLexNotes.add(new LexNote(lexnotes.getItemText(i)));
                                    }
                                }
                                fe.setLexnotes(availableLexNotes);

                                //                        if (examples.getItemCount() > 0) {
                                //                           for (int i = 0; i < examples.getItemCount(); i++) {
                                //                              availableExamples.add(new Example(
                                //                                    examples.getItemText(i)));
                                //                           }
                                //                        }
                                fe.setExamples(exList);

                                //                        if (collocations.getItemCount() > 0) {
                                //                           for (int i = 0; i < collocations
                                //                                 .getItemCount(); i++) {
                                //                              availableCollocation.add(new Collocation(
                                //                                    collocations.getItemText(i)));
                                //                           }
                                //                        }
                                fe.setCollocations(colList);

                                //                        if (grammars.getItemCount() > 0) {
                                //                           for (int i = 0; i < grammars.getItemCount(); i++) {
                                //                              availableGrammars.add(new Grammar(
                                //                                    grammars.getItemText(i)));
                                //                           }
                                //                        }
                                fe.setGrammars(grList);

                                //                        if (associations.getItemCount() > 0) {
                                //                           for (int i = 0; i < associations
                                //                                 .getItemCount(); i++) {
                                //                              availableAssociations.add(new Association(
                                //                                    associations.getItemText(i)));
                                //                           }
                                //                        }
                                //                        fe.setAssociations(availableAssociations);

                                final DialogBox dlgBox = new DialogBox();
                                final HTML serverResponseLabel = new HTML();
                                final Button clsButton = new Button("Close");

                                dlgBox.setText("Fixed Expression Result");
                                dlgBox.setAnimationEnabled(true);
                                // We can set the id of a widget by accessing
                                // its
                                // Element
                                clsButton.getElement().setId("closeButton");
                                VerticalPanel dialogVPanel = new VerticalPanel();
                                dialogVPanel.addStyleName("dialogVPanel");
                                dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
                                dialogVPanel.add(serverResponseLabel);
                                dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
                                dialogVPanel.add(clsButton);
                                dlgBox.setWidget(dialogVPanel);

                                // Add a handler to close the DialogBox
                                clsButton.addClickHandler(new ClickHandler() {
                                    public void onClick(ClickEvent event) {
                                        dlgBox.hide();
                                        newPhrase.hide();
                                    }
                                });

                                feService.updateFixedExpression(fe, new AsyncCallback<Void>() {
                                    public void onFailure(Throwable caught) {
                                        // Show the RPC error message to
                                        // the user
                                        dlgBox.setText("Remote Procedure Call<server> - Failure");
                                        serverResponseLabel.addStyleName("serverResponseLabelError");
                                        serverResponseLabel.setHTML(SERVER_ERROR + caught.toString());
                                        dlgBox.center();
                                        clsButton.setFocus(true);
                                    }

                                    public void onSuccess(Void noAnswer) {
                                        dlgBox.setText("Fixed Expression <update> result");
                                        serverResponseLabel.removeStyleName("serverResponseLabelError");
                                        serverResponseLabel.setHTML("Updated");
                                        dlgBox.center();
                                        clsButton.setFocus(true);
                                    }
                                });

                            }
                        }
                    });

                    ap.add(tool, DockPanel.SOUTH);

                    // Antonym Edit Panel
                    DockPanel dock = new DockPanel();
                    HorizontalPanel hP = new HorizontalPanel();

                    HTML antonym = new HTML("<b>Antonyms</b>");
                    dock.add(antonym, DockPanel.NORTH);
                    Button add = new Button("Add");
                    final Button edit = new Button("Edit");

                    add.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            showAntonym();
                        }

                        private void showAntonym() {
                            final DialogBox newAntonym = new DialogBox();
                            newAntonym.setText("New Antonym");
                            final Button closeButton = new Button("Cancel");
                            final Button saveButton = new Button("Add");

                            HorizontalPanel tool = new HorizontalPanel();
                            // Tool Bar
                            tool.add(closeButton);
                            tool.add(saveButton);

                            tool.setSpacing(5);

                            VerticalPanel vPanel = new VerticalPanel();
                            vPanel.setSpacing(3);

                            // Fixed Expression's Antonym
                            HTML labelLink = new HTML("Antonym");
                            vPanel.add(labelLink);
                            final TextBox link = new TextBox();
                            vPanel.add(link);
                            vPanel.add(tool);

                            // Add a handler to close the Dialog Box
                            closeButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    newAntonym.hide();
                                }
                            });

                            saveButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    // Send New Antonym to back panel ...

                                    if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                        antonyms.addItem(link.getText());

                                        edit.setEnabled(true);
                                        newAntonym.hide();

                                    }
                                }
                            });

                            newAntonym.add(vPanel);
                            newAntonym.center();

                        }
                    });
                    hP.add(add);

                    Button delete = new Button("Delete");
                    delete.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {

                            if (antonyms.getItemCount() > 0) {

                                int selected = -1;
                                selected = antonyms.getSelectedIndex();
                                while (selected != -1) {
                                    selected = antonyms.getSelectedIndex();
                                    if (selected != -1) {
                                        antonyms.removeItem(selected);
                                    }
                                }
                            }
                        }

                    });
                    hP.add(delete);

                    if (antonyms.getItemCount() > 0) {
                        edit.setEnabled(true);
                    } else {
                        edit.setEnabled(false);
                    }
                    edit.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {

                            if (antonyms.getItemCount() > 0) {

                                int selected = -1;
                                selected = antonyms.getSelectedIndex();
                                if (selected == -1) {
                                    Window.alert("Please select item to edit!");
                                } else {
                                    // Edit Panel
                                    final DialogBox editAntonym = new DialogBox();
                                    editAntonym.setText("Edit Antonym");
                                    final Button closeButton = new Button("Cancel");
                                    final Button saveButton = new Button("Update");

                                    HorizontalPanel tool = new HorizontalPanel();
                                    // Tool Bar
                                    tool.add(closeButton);
                                    tool.add(saveButton);
                                    tool.setSpacing(5);
                                    VerticalPanel vPanel = new VerticalPanel();
                                    vPanel.setSpacing(3);
                                    // Fixed Expression's Antonym
                                    HTML labelLink = new HTML("Antonym");
                                    vPanel.add(labelLink);
                                    final TextBox link = new TextBox();
                                    link.setText(antonyms.getValue(selected));
                                    vPanel.add(link);
                                    vPanel.add(tool);

                                    // Add a handler to close the Dialog Box
                                    final int sel = selected;
                                    closeButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            editAntonym.hide();
                                        }
                                    });

                                    saveButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            // Send Edit Antonym to Back
                                            // to Panel.

                                            if (link.getText() != null && !link.equals("")
                                                    && link.getText().length() > 0) {

                                                antonyms.setItemText(sel, link.getText());

                                                editAntonym.hide();

                                            }
                                        }
                                    });

                                    editAntonym.add(vPanel);
                                    editAntonym.center();

                                }

                            } else {
                                edit.setEnabled(false);
                            }
                        }

                    });

                    hP.add(edit);
                    hP.setSpacing(5);
                    dock.add(hP, DockPanel.CENTER);
                    antonyms.setVisibleItemCount(3);
                    dock.add(antonyms, DockPanel.SOUTH);

                    // Synonym Edit Panel

                    DockPanel dockSyn = new DockPanel();
                    HorizontalPanel hPsyn = new HorizontalPanel();
                    HTML synonym = new HTML("<b>Synonyms</b>");
                    dockSyn.add(synonym, DockPanel.NORTH);
                    Button addSyn = new Button("Add");
                    final Button editS = new Button("Edit");

                    addSyn.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            showSynonym();
                        }

                        private void showSynonym() {
                            final DialogBox newSynonym = new DialogBox();
                            newSynonym.setText("New Synonym");
                            final Button closeButton = new Button("Cancel");
                            final Button saveButton = new Button("Add");

                            HorizontalPanel tool = new HorizontalPanel();
                            tool.add(closeButton);
                            tool.add(saveButton);
                            tool.setSpacing(5);

                            VerticalPanel vPanel = new VerticalPanel();
                            vPanel.setSpacing(3);

                            // Fixed Expression's Synonym
                            HTML labelLink = new HTML("Synonym");
                            vPanel.add(labelLink);
                            final TextBox link = new TextBox();
                            vPanel.add(link);
                            vPanel.add(tool);

                            // Add a handler to close the Dialog Box
                            closeButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    newSynonym.hide();
                                }
                            });

                            saveButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    // Send New Synonym to back panel.

                                    if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                        synonyms.addItem(link.getText());
                                        editS.setEnabled(true);
                                        newSynonym.hide();

                                    }
                                }
                            });

                            newSynonym.add(vPanel);
                            newSynonym.center();

                        }
                    });
                    hPsyn.add(addSyn);

                    Button deleteSyn = new Button("Delete");
                    deleteSyn.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {

                            if (synonyms.getItemCount() > 0) {

                                int selected = -1;
                                selected = synonyms.getSelectedIndex();
                                while (selected != -1) {
                                    selected = synonyms.getSelectedIndex();
                                    if (selected != -1) {
                                        synonyms.removeItem(selected);
                                    }
                                }
                            }
                        }

                    });

                    if (synonyms.getItemCount() > 0) {
                        editS.setEnabled(true);
                    } else {
                        editS.setEnabled(false);
                    }
                    editS.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {

                            if (synonyms.getItemCount() > 0) {

                                int selected = -1;
                                selected = synonyms.getSelectedIndex();
                                if (selected == -1) {
                                    Window.alert("Please select item to edit!");
                                } else {
                                    // Edit Panel

                                    final DialogBox editSynonym = new DialogBox();
                                    editSynonym.setText("Edit Synonym");
                                    final Button closeButton = new Button("Cancel");
                                    final Button saveButton = new Button("Update");

                                    HorizontalPanel tool = new HorizontalPanel();
                                    // Tool Bar
                                    tool.add(closeButton);
                                    tool.add(saveButton);

                                    tool.setSpacing(5);

                                    VerticalPanel vPanel = new VerticalPanel();
                                    vPanel.setSpacing(3);

                                    // Fixed Expression's Synonym
                                    HTML labelLink = new HTML("Synonym");
                                    vPanel.add(labelLink);
                                    final TextBox link = new TextBox();
                                    link.setText(synonyms.getValue(selected));
                                    vPanel.add(link);
                                    vPanel.add(tool);

                                    // Add a handler to close the Dialog Box
                                    final int sel = selected;
                                    closeButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            editSynonym.hide();
                                        }
                                    });

                                    saveButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            // Send Edit Antonym to Back
                                            // to Panel.

                                            if (link.getText() != null && !link.equals("")
                                                    && link.getText().length() > 0) {

                                                synonyms.setItemText(sel, link.getText());

                                                editSynonym.hide();

                                            }
                                        }
                                    });

                                    editSynonym.add(vPanel);
                                    editSynonym.center();

                                }

                            } else {
                                editS.setEnabled(false);
                            }
                        }

                    });

                    hPsyn.add(deleteSyn);
                    hPsyn.add(editS);
                    hPsyn.setSpacing(5);
                    dockSyn.add(hPsyn, DockPanel.CENTER);
                    synonyms.setVisibleItemCount(3);
                    dockSyn.add(synonyms, DockPanel.SOUTH);

                    //                  Association edit panel

                    //               DockPanel dockAss = new DockPanel();
                    //               HorizontalPanel hPass = new HorizontalPanel();
                    //
                    //               HTML association = new HTML("<b>Associations</b>");
                    //               dockAss.add(association, DockPanel.NORTH);
                    //               Button addAss = new Button("Add");
                    //               final Button editA = new Button("Edit");
                    //
                    //               addAss.addClickHandler(new ClickHandler() {
                    //                  public void onClick(ClickEvent event) {
                    //                     showAssociation();
                    //                  }
                    //
                    //                  private void showAssociation() {
                    //                     final DialogBox newAss = new DialogBox();
                    //                     newAss.setText("New Association");
                    //                     final Button closeButton = new Button("Cancel");
                    //                     final Button saveButton = new Button("Add");
                    //
                    //                     HorizontalPanel tool = new HorizontalPanel();
                    //                     tool.add(closeButton);
                    //                     tool.add(saveButton);
                    //                     tool.setSpacing(5);
                    //
                    //                     VerticalPanel vPanel = new VerticalPanel();
                    //                     vPanel.setSpacing(3);
                    //
                    //                     // Fixed Expression's Association
                    //                     HTML labelLink = new HTML("Association");
                    //                     vPanel.add(labelLink);
                    //                     final TextBox link = new TextBox();
                    //                     vPanel.add(link);
                    //                     vPanel.add(tool);
                    //
                    //                     // Add a handler to close the Dialog Box
                    //                     closeButton.addClickHandler(new ClickHandler() {
                    //                        public void onClick(ClickEvent event) {
                    //                           newAss.hide();
                    //                        }
                    //                     });
                    //
                    //                     saveButton.addClickHandler(new ClickHandler() {
                    //                        public void onClick(ClickEvent event) {
                    //                           // Send New Association to back panel.
                    //
                    //                           if (link.getText() != null
                    //                                 && !link.equals("")
                    //                                 && link.getText().length() > 0) {
                    //
                    //                              associations.addItem(link.getText());
                    //                                        editA.setEnabled(true); 
                    //                              newAss.hide();
                    //
                    //                           }
                    //                        }
                    //                     });
                    //
                    //                     newAss.add(vPanel);
                    //                     newAss.center();
                    //
                    //                  }
                    //               });
                    //               hPass.add(addAss);
                    //
                    //               Button deleteAss = new Button("Delete");
                    //               deleteAss.addClickHandler(new ClickHandler() {
                    //
                    //                  public void onClick(ClickEvent event) {
                    //                     if (associations.getItemCount() > 0) {
                    //
                    //                        int selected = -1;
                    //                        selected = associations.getSelectedIndex();
                    //                        while (selected != -1) {
                    //                           selected = associations.getSelectedIndex();
                    //                           if (selected != -1) {
                    //                              associations.removeItem(selected);
                    //                           }
                    //                        }
                    //                     }
                    //                  }
                    //
                    //               });
                    //               hPass.add(deleteAss);
                    //               
                    //               if (associations.getItemCount() > 0) {
                    //                  editA.setEnabled(true);
                    //               } else {
                    //                  editA.setEnabled(false);
                    //               }
                    //               editA.addClickHandler(new ClickHandler() {
                    //                  public void onClick(ClickEvent event) {
                    //
                    //                     if (associations.getItemCount() > 0) {
                    //
                    //                        int selected = -1;
                    //                        selected = associations.getSelectedIndex();
                    //                        if (selected == -1) {
                    //                           Window.alert("Please select item to edit!");
                    //                        } else {
                    //                           // Edit Panel
                    //
                    //                           final DialogBox editAss = new DialogBox();
                    //                           editAss.setText("Edit Association");
                    //                           final Button closeButton = new Button(
                    //                                 "Cancel");
                    //                           final Button saveButton = new Button(
                    //                                 "Update");
                    //
                    //                           HorizontalPanel tool = new HorizontalPanel();
                    //                           // Tool Bar
                    //                           tool.add(closeButton);
                    //                           tool.add(saveButton);
                    //
                    //                           tool.setSpacing(5);
                    //
                    //                           VerticalPanel vPanel = new VerticalPanel();
                    //                           vPanel.setSpacing(3);
                    //
                    //                           // Fixed Expression's Synonym
                    //                           HTML labelLink = new HTML("Association");
                    //                           vPanel.add(labelLink);
                    //                           final TextBox link = new TextBox();
                    //                           link.setText(associations.getValue(selected));
                    //                           vPanel.add(link);
                    //                           vPanel.add(tool);
                    //
                    //                           // Add a handler to close the Dialog Box
                    //                           final int sel = selected;
                    //                           closeButton
                    //                                 .addClickHandler(new ClickHandler() {
                    //                                    public void onClick(
                    //                                          ClickEvent event) {
                    //                                       editAss.hide();
                    //                                    }
                    //                                 });
                    //
                    //                           saveButton
                    //                                 .addClickHandler(new ClickHandler() {
                    //                                    public void onClick(
                    //                                          ClickEvent event) {
                    //                                       // Send Edit Association to Back
                    //                                       // to Panel.
                    //
                    //                                       if (link.getText() != null
                    //                                             && !link.equals("")
                    //                                             && link.getText()
                    //                                                   .length() > 0) {
                    //
                    //                                          associations.setItemText(
                    //                                                     sel,
                    //                                                     link.getText());
                    //
                    //                                          editAss.hide();
                    //
                    //                                       }
                    //                                    }
                    //                                 });
                    //
                    //                           editAss.add(vPanel);
                    //                           editAss.center();
                    //
                    //                        }
                    //
                    //                     } else {
                    //                        editA.setEnabled(false);
                    //                     }
                    //                  }
                    //
                    //               });
                    //
                    //               
                    //               hPass.setSpacing(5);
                    //               hPass.add(editA);
                    //               dockAss.add(hPass, DockPanel.CENTER);
                    //               associations.setVisibleItemCount(3);
                    //               dockAss.add(associations, DockPanel.SOUTH);

                    // Examples edit panel

                    DockPanel dockex = new DockPanel();
                    HorizontalPanel hPex = new HorizontalPanel();

                    HTML example = new HTML("<b>Examples</b>");
                    dockex.add(example, DockPanel.NORTH);
                    Button addex = new Button("Add");
                    final Button editEx = new Button("Edit");

                    addex.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            showExample();
                        }

                        private void showExample() {
                            final DialogBox newEx = new DialogBox();
                            newEx.setText("New Example");
                            final Button closeButton = new Button("Cancel");
                            final Button saveButton = new Button("Add");

                            HorizontalPanel tool = new HorizontalPanel();
                            tool.add(closeButton);
                            tool.add(saveButton);
                            tool.setSpacing(5);

                            VerticalPanel vPanel = new VerticalPanel();
                            vPanel.setSpacing(3);

                            // Fixed Expression's Example
                            HTML labelLink = new HTML("Example");
                            vPanel.add(labelLink);
                            final TextBox link = new TextBox();
                            vPanel.add(link);
                            HTML labelEng = new HTML("English");
                            vPanel.add(labelEng);
                            final TextArea linkA = new TextArea();
                            vPanel.add(linkA);
                            vPanel.add(tool);

                            // Add a handler to close the Dialog Box
                            closeButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    newEx.hide();
                                }
                            });

                            saveButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    // Send New Example to back panel.
                                    if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                        examples.addItem(link.getText());
                                        exList.add(new Example(link.getText(), linkA.getText()));

                                        editEx.setEnabled(true);
                                        newEx.hide();

                                    }
                                }
                            });
                            newEx.add(vPanel);
                            newEx.center();
                        }
                    });
                    hPex.add(addex);

                    Button deleteEx = new Button("Delete");
                    deleteEx.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {

                            if (examples.getItemCount() > 0) {

                                int selected = -1;
                                selected = examples.getSelectedIndex();
                                while (selected != -1) {
                                    selected = examples.getSelectedIndex();
                                    if (selected != -1) {
                                        examples.removeItem(selected);
                                        exList.remove(selected);
                                    }
                                }
                            }
                        }

                    });
                    hPex.add(deleteEx);
                    if (examples.getItemCount() > 0) {
                        editEx.setEnabled(true);
                    } else {
                        editEx.setEnabled(false);
                    }

                    editEx.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {

                            if (examples.getItemCount() > 0) {

                                int selected = -1;
                                selected = examples.getSelectedIndex();
                                if (selected == -1) {
                                    Window.alert("Please select item to edit!");
                                } else {
                                    // Edit Panel

                                    final DialogBox editExx = new DialogBox();
                                    editExx.setText("Edit Example");
                                    final Button closeButton = new Button("Cancel");
                                    final Button saveButton = new Button("Update");

                                    HorizontalPanel tool = new HorizontalPanel();
                                    // Tool Bar
                                    tool.add(closeButton);
                                    tool.add(saveButton);

                                    tool.setSpacing(5);

                                    VerticalPanel vPanel = new VerticalPanel();
                                    vPanel.setSpacing(3);

                                    // Fixed Expression's Example
                                    HTML labelLink = new HTML("Example");
                                    vPanel.add(labelLink);
                                    final TextBox link = new TextBox();
                                    link.setText(examples.getValue(selected));

                                    final TextArea linkA = new TextArea();
                                    for (Example e : exList) {
                                        if (e.getExample().equals(examples.getValue(selected)))
                                            linkA.setText(e.getEngTranslation());
                                    }
                                    vPanel.add(link);
                                    HTML labelLinkT = new HTML("English");
                                    vPanel.add(labelLinkT);

                                    vPanel.add(linkA);

                                    vPanel.add(tool);

                                    // Add a handler to close the Dialog Box
                                    final int sel = selected;
                                    closeButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            editExx.hide();
                                        }
                                    });

                                    saveButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            // Send Edit Example to Back Panel.

                                            if (link.getText() != null && !link.equals("")
                                                    && link.getText().length() > 0) {

                                                examples.setItemText(sel, link.getText());

                                                // ? delete before update the new item
                                                exList.remove(sel);
                                                //exList.
                                                exList.add(sel, new Example(link.getText(), linkA.getText()));

                                                editExx.hide();

                                            }
                                        }
                                    });

                                    editExx.add(vPanel);
                                    editExx.center();

                                }

                            } else {
                                editEx.setEnabled(false);
                            }
                        }

                    });

                    hPex.add(editEx);
                    hPex.setSpacing(5);
                    dockex.add(hPex, DockPanel.CENTER);
                    examples.setVisibleItemCount(3);
                    dockex.add(examples, DockPanel.SOUTH);

                    // Grammars edit panel

                    DockPanel dockGr = new DockPanel();
                    HorizontalPanel hPGr = new HorizontalPanel();

                    HTML grammar = new HTML("<b>Grammar</b>");
                    dockGr.add(grammar, DockPanel.NORTH);
                    Button addGr = new Button("Add");
                    final Button editGr = new Button("Edit");

                    addGr.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            showGrammar();
                        }

                        private void showGrammar() {
                            final DialogBox newGr = new DialogBox();
                            newGr.setText("New Grammar");
                            final Button closeButton = new Button("Cancel");
                            final Button saveButton = new Button("Add");

                            HorizontalPanel tool = new HorizontalPanel();
                            tool.add(closeButton);
                            tool.add(saveButton);
                            tool.setSpacing(5);

                            VerticalPanel vPanel = new VerticalPanel();
                            vPanel.setSpacing(3);

                            // Fixed Expression's grammar
                            HTML labelLink = new HTML("Grammar");
                            vPanel.add(labelLink);
                            final TextBox link = new TextBox();
                            vPanel.add(link);

                            HTML labelLinkLong = new HTML("Description");
                            vPanel.add(labelLinkLong);
                            final TextArea linkLong = new TextArea();
                            vPanel.add(linkLong);

                            vPanel.add(tool);

                            // Add a handler to close the Dialog Box
                            closeButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    newGr.hide();
                                }
                            });

                            saveButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    // Send New Synonym to back panel.

                                    if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                        grammars.addItem(link.getText());
                                        grList.add(new Grammar(link.getText(), linkLong.getText()));
                                        editGr.setEnabled(true);

                                        newGr.hide();

                                    }
                                }
                            });

                            newGr.add(vPanel);
                            newGr.center();

                        }
                    });
                    hPGr.add(addGr);

                    Button deleteGr = new Button("Delete");
                    deleteGr.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            if (grammars.getItemCount() > 0) {
                                int selected = -1;
                                selected = grammars.getSelectedIndex();
                                while (selected != -1) {
                                    selected = grammars.getSelectedIndex();
                                    if (selected != -1) {
                                        grammars.removeItem(selected);
                                        grList.remove(selected);
                                    }
                                }
                            }
                        }

                    });

                    if (grammars.getItemCount() > 0) {
                        editGr.setEnabled(true);
                    } else {
                        editGr.setEnabled(false);
                    }
                    editGr.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {

                            if (grammars.getItemCount() > 0) {

                                int selected = -1;
                                selected = grammars.getSelectedIndex();
                                if (selected == -1) {
                                    Window.alert("Please select item to edit!");
                                } else {
                                    // Edit Panel

                                    final DialogBox editGrr = new DialogBox();
                                    editGrr.setText("Edit Grammar");
                                    final Button closeButton = new Button("Cancel");
                                    final Button saveButton = new Button("Update");

                                    HorizontalPanel tool = new HorizontalPanel();
                                    // Tool Bar
                                    tool.add(closeButton);
                                    tool.add(saveButton);

                                    tool.setSpacing(5);

                                    VerticalPanel vPanel = new VerticalPanel();
                                    vPanel.setSpacing(3);

                                    // Fixed Expression's Grammar
                                    HTML labelLink = new HTML("Grammar");
                                    vPanel.add(labelLink);
                                    final TextBox link = new TextBox();
                                    link.setText(grammars.getValue(selected));

                                    final TextArea linkA = new TextArea();
                                    for (Grammar e : grList) {
                                        if (e.getGrammar().equals(grammars.getValue(selected)))
                                            linkA.setText(e.getGrammar_long());
                                    }
                                    vPanel.add(link);
                                    HTML labelLinkT = new HTML("Description");
                                    vPanel.add(labelLinkT);

                                    vPanel.add(linkA);

                                    vPanel.add(tool);

                                    // Add a handler to close the Dialog Box
                                    final int sel = selected;
                                    closeButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            editGrr.hide();
                                        }
                                    });

                                    saveButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            // Send Edit Example to Back Panel.

                                            if (link.getText() != null && !link.equals("")
                                                    && link.getText().length() > 0) {

                                                grammars.setItemText(sel, link.getText());

                                                // ? delete before update the new item
                                                grList.remove(sel);
                                                //exList.
                                                grList.add(sel, new Grammar(link.getText(), linkA.getText()));

                                                editGrr.hide();

                                            }
                                        }
                                    });

                                    editGrr.add(vPanel);
                                    editGrr.center();

                                }

                            } else {
                                editGr.setEnabled(false);
                            }
                        }

                    });

                    hPGr.add(deleteGr);
                    hPGr.add(editGr);
                    hPGr.setSpacing(5);
                    dockGr.add(hPGr, DockPanel.CENTER);
                    grammars.setVisibleItemCount(3);
                    dockGr.add(grammars, DockPanel.SOUTH);

                    //               // LexNotes<Etimological Info> edit panel
                    //
                    DockPanel dockLx = new DockPanel();
                    HorizontalPanel hPLx = new HorizontalPanel();

                    HTML lexnote = new HTML("<b>Etymological Information</b>");
                    dockLx.add(lexnote, DockPanel.NORTH);
                    Button addLx = new Button("Add");
                    final Button editLx = new Button("Edit");

                    addLx.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            showLexNote();
                        }

                        private void showLexNote() {
                            final DialogBox newLx = new DialogBox();
                            newLx.setText("New Etymological Information");
                            final Button closeButton = new Button("Cancel");
                            final Button saveButton = new Button("Add");

                            HorizontalPanel tool = new HorizontalPanel();
                            tool.add(closeButton);
                            tool.add(saveButton);
                            tool.setSpacing(5);

                            VerticalPanel vPanel = new VerticalPanel();
                            vPanel.setSpacing(3);

                            // Fixed Expression's Lexical Note
                            HTML labelLink = new HTML("Etymological Information");
                            vPanel.add(labelLink);
                            final TextBox link = new TextBox();
                            vPanel.add(link);
                            vPanel.add(tool);

                            // Add a handler to close the Dialog Box
                            closeButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    newLx.hide();
                                }
                            });

                            saveButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    // Send New Lexical Notes to back panel.

                                    if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                        lexnotes.addItem(link.getText());
                                        editLx.setEnabled(true);

                                        newLx.hide();

                                    }
                                }
                            });

                            newLx.add(vPanel);
                            newLx.center();

                        }
                    });
                    hPLx.add(addLx);

                    Button deleteLx = new Button("Delete");
                    deleteLx.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {

                            if (lexnotes.getItemCount() > 0) {

                                int selected = -1;
                                selected = lexnotes.getSelectedIndex();
                                while (selected != -1) {
                                    selected = lexnotes.getSelectedIndex();
                                    if (selected != -1) {
                                        lexnotes.removeItem(selected);
                                    }
                                }
                            }
                        }

                    });
                    hPLx.add(deleteLx);
                    hPLx.add(editLx);

                    if (lexnotes.getItemCount() > 0) {
                        editLx.setEnabled(true);
                    } else {
                        editLx.setEnabled(false);
                    }
                    editLx.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {

                            if (lexnotes.getItemCount() > 0) {

                                int selected = -1;
                                selected = lexnotes.getSelectedIndex();
                                if (selected == -1) {
                                    Window.alert("Please select item to edit!");
                                } else {
                                    // Edit Panel

                                    final DialogBox editLxx = new DialogBox();
                                    editLxx.setText("Edit Etymological Information");
                                    final Button closeButton = new Button("Cancel");
                                    final Button saveButton = new Button("Update");

                                    HorizontalPanel tool = new HorizontalPanel();
                                    // Tool Bar
                                    tool.add(closeButton);
                                    tool.add(saveButton);

                                    tool.setSpacing(5);

                                    VerticalPanel vPanel = new VerticalPanel();
                                    vPanel.setSpacing(3);

                                    // Fixed Expression's Lexical Note
                                    HTML labelLink = new HTML("Etymological Information");
                                    vPanel.add(labelLink);
                                    final TextBox link = new TextBox();
                                    link.setText(lexnotes.getValue(selected));
                                    vPanel.add(link);
                                    vPanel.add(tool);

                                    // Add a handler to close the Dialog Box
                                    final int sel = selected;
                                    closeButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            editLxx.hide();
                                        }
                                    });

                                    saveButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            // Send Edit Association to Back
                                            // to Panel.

                                            if (link.getText() != null && !link.equals("")
                                                    && link.getText().length() > 0) {

                                                lexnotes.setItemText(sel, link.getText());

                                                editLxx.hide();

                                            }
                                        }
                                    });

                                    editLxx.add(vPanel);
                                    editLxx.center();

                                }

                            } else {
                                editLx.setEnabled(false);
                            }
                        }

                    });

                    hPLx.setSpacing(5);
                    dockLx.add(hPLx, DockPanel.CENTER);
                    lexnotes.setVisibleItemCount(3);
                    dockLx.add(lexnotes, DockPanel.SOUTH);

                    // Collocation edit panel

                    DockPanel dockCl = new DockPanel();
                    HorizontalPanel hPCl = new HorizontalPanel();

                    HTML collocation = new HTML("<b>Collocations</b>");
                    dockCl.add(collocation, DockPanel.NORTH);
                    Button addCl = new Button("Add");
                    addCl.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            showCollocation();
                        }

                        private void showCollocation() {
                            final DialogBox newCl = new DialogBox();
                            newCl.setText("New Collocation");
                            final Button closeButton = new Button("Cancel");
                            final Button saveButton = new Button("Add");

                            HorizontalPanel tool = new HorizontalPanel();
                            tool.add(closeButton);
                            tool.add(saveButton);
                            tool.setSpacing(5);

                            VerticalPanel vPanel = new VerticalPanel();
                            vPanel.setSpacing(3);

                            // Fixed Expression's Collocation
                            HTML labelLink = new HTML("Collocation");
                            vPanel.add(labelLink);
                            final TextBox link = new TextBox();
                            vPanel.add(link);

                            HTML labelLinkEng = new HTML("English");
                            vPanel.add(labelLinkEng);
                            final TextArea linkEng = new TextArea();

                            vPanel.add(linkEng);

                            vPanel.add(tool);

                            // Add a handler to close the Dialog Box
                            closeButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    newCl.hide();
                                }
                            });

                            saveButton.addClickHandler(new ClickHandler() {
                                public void onClick(ClickEvent event) {
                                    // Send New Synonym to back panel.

                                    if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                        collocations.addItem(link.getText());
                                        colList.add(new Collocation(link.getText(), linkEng.getText()));
                                        newCl.hide();

                                    }
                                }
                            });

                            newCl.add(vPanel);
                            newCl.center();

                        }
                    });
                    hPCl.add(addCl);

                    final Button editCl = new Button("Edit");
                    editCl.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {

                            if (collocations.getItemCount() > 0) {

                                int selected = -1;
                                selected = collocations.getSelectedIndex();
                                if (selected == -1) {
                                    Window.alert("Please select item to edit!");
                                } else {
                                    // Edit Panel

                                    final DialogBox editCll = new DialogBox();
                                    editCll.setText("Edit Collocation");
                                    final Button closeButton = new Button("Cancel");
                                    final Button saveButton = new Button("Update");

                                    HorizontalPanel tool = new HorizontalPanel();
                                    // Tool Bar
                                    tool.add(closeButton);
                                    tool.add(saveButton);

                                    tool.setSpacing(5);

                                    VerticalPanel vPanel = new VerticalPanel();
                                    vPanel.setSpacing(3);

                                    // Fixed Expression's Collocation
                                    HTML labelLink = new HTML("Collocation");
                                    vPanel.add(labelLink);
                                    final TextBox link = new TextBox();
                                    link.setText(collocations.getValue(selected));

                                    final TextArea linkA = new TextArea();
                                    for (Collocation e : colList) {
                                        if (e.getCollocation().equals(collocations.getValue(selected)))
                                            linkA.setText(e.getEngTranslation());
                                    }
                                    vPanel.add(link);
                                    HTML labelLinkT = new HTML("English");
                                    vPanel.add(labelLinkT);

                                    vPanel.add(linkA);

                                    vPanel.add(tool);

                                    // Add a handler to close the Dialog Box
                                    final int sel = selected;
                                    closeButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            editCll.hide();
                                        }
                                    });

                                    saveButton.addClickHandler(new ClickHandler() {
                                        public void onClick(ClickEvent event) {
                                            // Send Edit Example to Back Panel.

                                            if (link.getText() != null && !link.equals("")
                                                    && link.getText().length() > 0) {

                                                collocations.setItemText(sel, link.getText());

                                                // ? delete before update the new item
                                                colList.remove(sel);
                                                //exList.
                                                colList.add(sel, new Collocation(link.getText(), linkA.getText()));

                                                editCll.hide();

                                            }
                                        }
                                    });

                                    editCll.add(vPanel);
                                    editCll.center();

                                }

                            } else {
                                editCl.setEnabled(false);
                            }
                        }

                    });

                    Button deleteCl = new Button("Delete");
                    deleteCl.addClickHandler(new ClickHandler() {

                        public void onClick(ClickEvent event) {

                            if (collocations.getItemCount() > 0) {

                                int selected = -1;
                                selected = collocations.getSelectedIndex();
                                while (selected != -1) {
                                    selected = collocations.getSelectedIndex();
                                    if (selected != -1) {
                                        collocations.removeItem(selected);
                                        colList.remove(selected);
                                    }
                                }
                            }
                        }

                    });
                    hPCl.add(deleteCl);
                    if (collocations.getItemCount() > 0) {
                        editCl.setEnabled(true);
                    } else {
                        editCl.setEnabled(false);
                    }
                    hPCl.add(editCl);
                    hPCl.setSpacing(5);
                    dockCl.add(hPCl, DockPanel.CENTER);
                    collocations.setVisibleItemCount(3);
                    dockCl.add(collocations, DockPanel.SOUTH);

                    VerticalPanel vertical = new VerticalPanel();
                    vertical.add(dock);
                    vertical.add(dockSyn);
                    //   vertical.add(dockAss);
                    vertical.add(dockLx);
                    vertical.add(dockex);

                    VerticalPanel vertical2 = new VerticalPanel();

                    vertical2.add(dockex);
                    vertical2.add(dockCl);
                    vertical2.add(dockGr);

                    hPanel.add(vertical);
                    hPanel.add(vertical2);
                    ap.add(hPanel, DockPanel.CENTER);
                    newPhrase.add(ap);
                    newPhrase.center();

                }
            };
            t.schedule(3000);

        }

    }

    class NewRefFEHandler implements ClickHandler, KeyUpHandler {

        public void onKeyUp(KeyUpEvent event) {
            // TODO Auto-generated method stub

        }

        public void onClick(ClickEvent event) {
            refreshList();

        }

        public void refreshList() {

            Window.Location.reload();
        }

    }

    class NewFEHandler implements ClickHandler, KeyUpHandler {

        final ListBox antonyms = new ListBox(true);

        final ListBox synonyms = new ListBox(true);

        //final ListBox associations = new ListBox(true);

        // Etymological Info 

        final ListBox lexnotes = new ListBox(true);

        final ListBox examples = new ListBox(true);
        final List<Example> exList = new ArrayList<Example>();

        final ListBox collocations = new ListBox(true);
        final List<Collocation> colList = new ArrayList<Collocation>();

        final ListBox grammars = new ListBox(true);
        final List<Grammar> grList = new ArrayList<Grammar>();

        protected FixedExpression fe = new FixedExpression();

        public void onKeyUp(KeyUpEvent event) {
        }

        public void onClick(ClickEvent event) {
            showNewDialogBox();
        }

        private void showNewDialogBox() {

            synonyms.clear();
            antonyms.clear();
            //associations.clear();
            collocations.clear();
            lexnotes.clear();
            examples.clear();
            grammars.clear();

            final DialogBox newPhrase = new DialogBox();
            newPhrase.setText("New Fixed Expression");
            final Button closeButton = new Button("Close");
            final Button saveButton = new Button("Save");

            // Add a handler to close the DialogBox
            closeButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    newPhrase.hide();
                }
            });

            // Create a vertical panel to manage phrases
            DockPanel ap = new DockPanel();
            ap.setSpacing(3);
            HorizontalPanel hPanel = new HorizontalPanel();
            hPanel.setSpacing(3);
            VerticalPanel vPanel = new VerticalPanel();
            vPanel.setSpacing(3);

            HTML label = new HTML(constants.cwAdminTerminInfo());
            label.ensureDebugId("cwNewFE-label");
            ap.add(label, DockPanel.NORTH);

            antonyms.setWidth("100px");
            synonyms.setWidth("100px");
            //   associations.setWidth("100px");
            collocations.setWidth("100px");
            examples.setWidth("100px");
            grammars.setWidth("100px");
            lexnotes.setWidth("100px");

            // Fixed Expression Core String
            HTML name = new HTML("<b>Phrase</b>");
            vPanel.add(name);
            HTML labelCore = new HTML(constants.cwFixedExpressionName());
            vPanel.add(labelCore);
            final TextBox core = new TextBox();
            vPanel.add(core);

            // Fixed Expression's meaning
            HTML labelMean = new HTML(constants.cwFixedExpressionMean());
            vPanel.add(labelMean);
            final TextBox mean = new TextBox();
            vPanel.add(mean);

            // Fixed Expression's English translation
            HTML labelEng = new HTML(constants.cwFixedEnglish());
            vPanel.add(labelEng);
            final TextArea eng = new TextArea();
            vPanel.add(eng);

            // Fixed Expression's Usage style
            HTML labelUsStyle = new HTML(constants.cwFixedUsageStyle());
            vPanel.add(labelUsStyle);
            final TextBox style = new TextBox();
            vPanel.add(style);

            // Fixed Expression's editor's note
            HTML labelEdNote = new HTML(constants.cwFixedExpressionEdNote());
            vPanel.add(labelEdNote);
            final TextArea note = new TextArea();
            vPanel.add(note);

            // Fixed Expression's Internet link
            HTML labelLink = new HTML(constants.cwFixedExpressionLink());
            vPanel.add(labelLink);
            final TextBox link = new TextBox();
            vPanel.add(link);

            // Fixed Expression's ignore on search
            HTML labelIgnore = new HTML(constants.cwFixedIgnoreOnSearch());
            vPanel.add(labelIgnore);
            final CheckBox ignore = new CheckBox();
            ignore.setEnabled(false);
            vPanel.add(ignore);

            // Fixed Expression's do not display
            HTML labelDont = new HTML(constants.cwFixedDoNotDisplay());
            vPanel.add(labelDont);
            final CheckBox dont = new CheckBox();
            dont.setEnabled(false);
            vPanel.add(dont);
            hPanel.add(vPanel);

            HorizontalPanel tool = new HorizontalPanel();
            tool.add(closeButton);
            tool.add(saveButton);
            tool.setSpacing(5);

            saveButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    // Save the fixed expression
                    if (!core.getText().trim().equals("") && !mean.getText().trim().equals("")) {
                        fe.setCoreString(core.getText().trim());
                        fe.setDefinition_short(mean.getText().trim());
                        fe.setEngTranslation(eng.getText().trim());
                        fe.setEditorsNotes(note.getText().trim());
                        fe.setUsageStyle(style.getText().trim());
                        fe.setIgnore_on_search(ignore.getText().trim());
                        fe.setDo_not_display(dont.getText().trim());
                        fe.setInternetadr(link.getText().trim());

                        List<Antonym> availableAntonyms = new ArrayList<Antonym>();

                        List<Synonym> availableSynonyms = new ArrayList<Synonym>();

                        List<Association> availableAssociations = new ArrayList<Association>();

                        List<Collocation> availableCollocation = new ArrayList<Collocation>();

                        List<LexNote> availableLexNotes = new ArrayList<LexNote>();

                        List<Grammar> availableGrammars = new ArrayList<Grammar>();

                        List<Example> availableExamples = new ArrayList<Example>();

                        if (synonyms.getItemCount() > 0) {
                            for (int i = 0; i < synonyms.getItemCount(); i++) {
                                availableSynonyms.add(new Synonym(synonyms.getItemText(i)));
                            }
                        }
                        fe.setSynonyms(availableSynonyms);

                        if (antonyms.getItemCount() > 0) {
                            for (int i = 0; i < antonyms.getItemCount(); i++) {
                                availableAntonyms.add(new Antonym(antonyms.getItemText(i)));
                            }
                        }
                        fe.setAntonyms(availableAntonyms);

                        if (lexnotes.getItemCount() > 0) {
                            for (int i = 0; i < lexnotes.getItemCount(); i++) {
                                availableLexNotes.add(new LexNote(lexnotes.getItemText(i)));
                            }
                        }
                        fe.setLexnotes(availableLexNotes);

                        availableExamples.addAll(exList);
                        fe.setExamples(availableExamples);

                        //                  if (collocations.getItemCount() > 0) {
                        //                     for (int i = 0; i < collocations.getItemCount(); i++) {
                        //                        availableCollocation.add(new Collocation(
                        //                              collocations.getItemText(i)));
                        //                     }
                        //                  }
                        availableCollocation.addAll(colList);
                        fe.setCollocations(availableCollocation);

                        //                  if (grammars.getItemCount() > 0) {
                        //                     for (int i = 0; i < grammars.getItemCount(); i++) {
                        //                        availableGrammars.add(new Grammar(grammars
                        //                              .getItemText(i)));
                        //                     }
                        //                  }
                        availableGrammars.addAll(grList);
                        fe.setGrammars(availableGrammars);

                        //                  if (associations.getItemCount() > 0) {
                        //                     for (int i = 0; i < associations.getItemCount(); i++) {
                        //                        availableAssociations.add(new Association(
                        //                              associations.getItemText(i)));
                        //                     }
                        //                  }
                        //                  fe.setAssociations(availableAssociations);

                        final DialogBox dlgBox = new DialogBox();
                        final HTML serverResponseLabel = new HTML();
                        final Button clsButton = new Button("Close");

                        dlgBox.setText("Fixed Expression Result");
                        dlgBox.setAnimationEnabled(true);
                        // We can set the id of a widget by accessing its
                        // Element
                        clsButton.getElement().setId("closeButton");
                        VerticalPanel dialogVPanel = new VerticalPanel();
                        dialogVPanel.addStyleName("dialogVPanel");
                        dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
                        dialogVPanel.add(serverResponseLabel);
                        dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
                        dialogVPanel.add(clsButton);
                        dlgBox.setWidget(dialogVPanel);

                        // Add a handler to close the DialogBox
                        clsButton.addClickHandler(new ClickHandler() {
                            public void onClick(ClickEvent event) {
                                dlgBox.hide();
                                newPhrase.hide();
                            }
                        });

                        feService.saveOrUpdateFixedExpression(fe, new AsyncCallback<Void>() {
                            public void onFailure(Throwable caught) {
                                // Show the RPC error message to the
                                // user
                                dlgBox.setText("Remote Procedure Call - Failure");
                                serverResponseLabel.addStyleName("serverResponseLabelError");
                                serverResponseLabel.setHTML(SERVER_ERROR + caught.toString());
                                dlgBox.center();
                                clsButton.setFocus(true);
                            }

                            public void onSuccess(Void noAnswer) {
                                dlgBox.setText("Fixed Expression add result");
                                serverResponseLabel.removeStyleName("serverResponseLabelError");
                                serverResponseLabel.setHTML("OK");
                                dlgBox.center();
                                clsButton.setFocus(true);
                            }
                        });

                    }
                }
            });

            ap.add(tool, DockPanel.SOUTH);

            // Antonym Edit Panel
            DockPanel dock = new DockPanel();
            HorizontalPanel hP = new HorizontalPanel();

            HTML antonym = new HTML("<b>Antonyms</b>");
            dock.add(antonym, DockPanel.NORTH);
            Button add = new Button("Add");
            add.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showAntonym();
                }

                private void showAntonym() {
                    final DialogBox newAntonym = new DialogBox();
                    newAntonym.setText("New Antonym");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Add");

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    // Fixed Expression's Antonym
                    HTML labelLink = new HTML("Antonym");
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    vPanel.add(link);
                    vPanel.add(tool);

                    // Add a handler to close the Dialog Box
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newAntonym.hide();
                        }
                    });

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Send New Antonym to back panel.
                            if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {
                                antonyms.addItem(link.getText());
                                newAntonym.hide();
                            }
                        }
                    });

                    newAntonym.add(vPanel);
                    newAntonym.center();

                }
            });
            hP.add(add);

            Button delete = new Button("Delete");
            delete.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {

                    if (antonyms.getItemCount() > 0) {

                        int selected = -1;
                        selected = antonyms.getSelectedIndex();
                        while (selected != -1) {
                            selected = antonyms.getSelectedIndex();
                            if (selected != -1) {
                                antonyms.removeItem(selected);
                            }
                        }
                    }
                }

            });
            hP.add(delete);
            hP.setSpacing(5);
            dock.add(hP, DockPanel.CENTER);
            antonyms.setVisibleItemCount(3);
            dock.add(antonyms, DockPanel.SOUTH);

            // Synonym Edit Panel

            DockPanel dockSyn = new DockPanel();
            HorizontalPanel hPsyn = new HorizontalPanel();
            HTML synonym = new HTML("<b>Synonyms</b>");
            dockSyn.add(synonym, DockPanel.NORTH);
            Button addSyn = new Button("Add");
            addSyn.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showSynonym();
                }

                private void showSynonym() {
                    final DialogBox newSynonym = new DialogBox();
                    newSynonym.setText("New Synonym");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Add");

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    // Fixed Expression's Synonym
                    HTML labelLink = new HTML("Synonym");
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    vPanel.add(link);
                    vPanel.add(tool);

                    // Add a handler to close the Dialog Box
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newSynonym.hide();
                        }
                    });

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Send New Synonym to back panel.

                            if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                synonyms.addItem(link.getText());

                                newSynonym.hide();

                            }
                        }
                    });

                    newSynonym.add(vPanel);
                    newSynonym.center();

                }
            });
            hPsyn.add(addSyn);

            Button deleteSyn = new Button("Delete");
            deleteSyn.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {
                    if (synonyms.getItemCount() > 0) {

                        int selected = -1;
                        selected = synonyms.getSelectedIndex();
                        while (selected != -1) {
                            selected = synonyms.getSelectedIndex();
                            if (selected != -1) {
                                synonyms.removeItem(selected);
                            }
                        }
                    }
                }

            });
            hPsyn.add(deleteSyn);
            hPsyn.setSpacing(5);
            dockSyn.add(hPsyn, DockPanel.CENTER);
            synonyms.setVisibleItemCount(3);
            dockSyn.add(synonyms, DockPanel.SOUTH);

            // Association edit panel

            //         DockPanel dockAss = new DockPanel();
            //         HorizontalPanel hPass = new HorizontalPanel();
            //
            //         HTML association = new HTML("<b>Associations</b>");
            //         dockAss.add(association, DockPanel.NORTH);
            //         Button addAss = new Button("Add");
            //         addAss.addClickHandler(new ClickHandler() {
            //            public void onClick(ClickEvent event) {
            //               showAssociation();
            //            }
            //
            //            private void showAssociation() {
            //               final DialogBox newAss = new DialogBox();
            //               newAss.setText("New Association");
            //               final Button closeButton = new Button("Cancel");
            //               final Button saveButton = new Button("Add");
            //
            //               HorizontalPanel tool = new HorizontalPanel();
            //               tool.add(closeButton);
            //               tool.add(saveButton);
            //               tool.setSpacing(5);
            //
            //               VerticalPanel vPanel = new VerticalPanel();
            //               vPanel.setSpacing(3);
            //
            //               // Fixed Expression's Association
            //               HTML labelLink = new HTML("Association");
            //               vPanel.add(labelLink);
            //               final TextBox link = new TextBox();
            //               vPanel.add(link);
            //               vPanel.add(tool);
            //
            //               // Add a handler to close the Dialog Box
            //               closeButton.addClickHandler(new ClickHandler() {
            //                  public void onClick(ClickEvent event) {
            //                     newAss.hide();
            //                  }
            //               });
            //
            //               saveButton.addClickHandler(new ClickHandler() {
            //                  public void onClick(ClickEvent event) {
            //                     // Send New Association to back panel.
            //
            //                     if (link.getText() != null && !link.equals("")
            //                           && link.getText().length() > 0) {
            //
            //                        associations.addItem(link.getText());
            //
            //                        newAss.hide();
            //
            //                     }
            //                  }
            //               });
            //
            //               newAss.add(vPanel);
            //               newAss.center();
            //
            //            }
            //         });
            //         hPass.add(addAss);
            //
            //         Button deleteAss = new Button("Delete");
            //         deleteAss.addClickHandler(new ClickHandler() {
            //
            //            public void onClick(ClickEvent event) {
            //
            //               if (associations.getItemCount() > 0) {
            //
            //                  int selected = -1;
            //                  selected = associations.getSelectedIndex();
            //                  while (selected != -1) {
            //                     selected = associations.getSelectedIndex();
            //                     if (selected != -1) {
            //                        associations.removeItem(selected);
            //                     }
            //                  }
            //               }
            //            }
            //
            //         });
            //         hPass.add(deleteAss);
            //         hPass.setSpacing(5);
            //         dockAss.add(hPass, DockPanel.CENTER);
            //         associations.setVisibleItemCount(3);
            //         dockAss.add(associations, DockPanel.SOUTH);

            // Examples edit panel

            DockPanel dockex = new DockPanel();
            HorizontalPanel hPex = new HorizontalPanel();

            HTML example = new HTML("<b>Examples</b>");
            dockex.add(example, DockPanel.NORTH);
            Button addex = new Button("Add");
            addex.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showExample();
                }

                private void showExample() {
                    final DialogBox newEx = new DialogBox();
                    newEx.setText("New Example");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Add");

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    // Fixed Expression's Example
                    HTML labelLink = new HTML("Example");
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    vPanel.add(link);

                    HTML labelEng = new HTML("English");
                    vPanel.add(labelEng);
                    final TextArea linkA = new TextArea();
                    vPanel.add(linkA);

                    vPanel.add(tool);

                    // Add a handler to close the Dialog Box
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newEx.hide();
                        }
                    });

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Send New Example to back panel.

                            if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                examples.addItem(link.getText());
                                exList.add(new Example(link.getText(), linkA.getText()));

                                newEx.hide();

                            }
                        }
                    });

                    newEx.add(vPanel);
                    newEx.center();

                }
            });
            hPex.add(addex);

            Button deleteEx = new Button("Delete");
            deleteEx.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {
                    if (examples.getItemCount() > 0) {

                        int selected = -1;
                        selected = examples.getSelectedIndex();
                        while (selected != -1) {
                            selected = examples.getSelectedIndex();
                            if (selected != -1) {
                                examples.removeItem(selected);
                                exList.remove(selected);
                            }
                        }
                    }
                }

            });
            hPex.add(deleteEx);
            hPex.setSpacing(5);
            dockex.add(hPex, DockPanel.CENTER);
            examples.setVisibleItemCount(3);
            dockex.add(examples, DockPanel.SOUTH);

            // Grammars edit panel

            DockPanel dockGr = new DockPanel();
            HorizontalPanel hPGr = new HorizontalPanel();

            HTML grammar = new HTML("<b>Grammar</b>");
            dockGr.add(grammar, DockPanel.NORTH);
            Button addGr = new Button("Add");
            addGr.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showGrammar();
                }

                private void showGrammar() {
                    final DialogBox newGr = new DialogBox();
                    newGr.setText("New Grammar");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Add");

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    // Fixed Expression's grammar
                    HTML labelLink = new HTML("Grammar");
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    vPanel.add(link);

                    HTML labelEng = new HTML("Description");
                    vPanel.add(labelEng);
                    final TextArea linkA = new TextArea();
                    vPanel.add(linkA);

                    vPanel.add(tool);

                    // Add a handler to close the Dialog Box
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newGr.hide();
                        }
                    });

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Send New Synonym to back panel.

                            if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                grammars.addItem(link.getText());
                                grList.add(new Grammar(link.getText(), linkA.getText()));
                                newGr.hide();

                            }
                        }
                    });

                    newGr.add(vPanel);
                    newGr.center();

                }
            });
            hPGr.add(addGr);

            Button deleteGr = new Button("Delete");
            deleteGr.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {
                    if (grammars.getItemCount() > 0) {

                        int selected = -1;
                        selected = grammars.getSelectedIndex();
                        while (selected != -1) {
                            selected = grammars.getSelectedIndex();
                            if (selected != -1) {
                                grammars.removeItem(selected);
                                grList.remove(selected);
                            }
                        }
                    }
                }

            });
            hPGr.add(deleteGr);
            hPGr.setSpacing(5);
            dockGr.add(hPGr, DockPanel.CENTER);
            grammars.setVisibleItemCount(3);
            dockGr.add(grammars, DockPanel.SOUTH);

            // LexNotes edit panel

            DockPanel dockLx = new DockPanel();
            HorizontalPanel hPLx = new HorizontalPanel();

            HTML lexnote = new HTML("<b>Etymological Information</b>");
            dockLx.add(lexnote, DockPanel.NORTH);
            Button addLx = new Button("Add");
            addLx.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showLexNote();
                }

                private void showLexNote() {
                    final DialogBox newLx = new DialogBox();
                    newLx.setText("New Etymological Information");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Add");

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    // Fixed Expression's Lexical Note
                    HTML labelLink = new HTML("Etymological Information");
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    vPanel.add(link);
                    vPanel.add(tool);

                    // Add a handler to close the Dialog Box
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newLx.hide();
                        }
                    });

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Send New Lexical Notes to back panel.

                            if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                lexnotes.addItem(link.getText());

                                newLx.hide();

                            }
                        }
                    });

                    newLx.add(vPanel);
                    newLx.center();

                }
            });
            hPLx.add(addLx);

            Button deleteLx = new Button("Delete");
            deleteLx.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {
                    if (lexnotes.getItemCount() > 0) {

                        int selected = -1;
                        selected = lexnotes.getSelectedIndex();
                        while (selected != -1) {
                            selected = lexnotes.getSelectedIndex();
                            if (selected != -1) {
                                lexnotes.removeItem(selected);
                            }
                        }
                    }
                }

            });
            hPLx.add(deleteLx);
            hPLx.setSpacing(5);
            dockLx.add(hPLx, DockPanel.CENTER);
            lexnotes.setVisibleItemCount(3);
            dockLx.add(lexnotes, DockPanel.SOUTH);

            // Collocation edit panel

            DockPanel dockCl = new DockPanel();
            HorizontalPanel hPCl = new HorizontalPanel();

            HTML collocation = new HTML("<b>Collocations</b>");
            dockCl.add(collocation, DockPanel.NORTH);
            Button addCl = new Button("Add");
            addCl.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    showCollocation();
                }

                private void showCollocation() {
                    final DialogBox newCl = new DialogBox();
                    newCl.setText("New Collocation");
                    final Button closeButton = new Button("Cancel");
                    final Button saveButton = new Button("Add");

                    HorizontalPanel tool = new HorizontalPanel();
                    tool.add(closeButton);
                    tool.add(saveButton);
                    tool.setSpacing(5);

                    VerticalPanel vPanel = new VerticalPanel();
                    vPanel.setSpacing(3);

                    // Fixed Expression's Collocation
                    HTML labelLink = new HTML("Collocation");
                    vPanel.add(labelLink);
                    final TextBox link = new TextBox();
                    vPanel.add(link);

                    HTML labelEng = new HTML("English");
                    vPanel.add(labelEng);
                    final TextArea linkA = new TextArea();
                    vPanel.add(linkA);
                    vPanel.add(tool);

                    // Add a handler to close the Dialog Box
                    closeButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            newCl.hide();
                        }
                    });

                    saveButton.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            // Send New Synonym to back panel.

                            if (link.getText() != null && !link.equals("") && link.getText().length() > 0) {

                                collocations.addItem(link.getText());
                                colList.add(new Collocation(link.getText(), linkA.getText()));
                                newCl.hide();

                            }
                        }
                    });

                    newCl.add(vPanel);
                    newCl.center();

                }
            });
            hPCl.add(addCl);

            Button deleteCl = new Button("Delete");
            deleteCl.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {

                    if (collocations.getItemCount() > 0) {

                        int selected = -1;
                        selected = collocations.getSelectedIndex();
                        while (selected != -1) {
                            selected = collocations.getSelectedIndex();
                            if (selected != -1) {
                                collocations.removeItem(selected);
                                colList.remove(selected);
                            }
                        }
                    }
                }

            });
            hPCl.add(deleteCl);
            hPCl.setSpacing(5);
            dockCl.add(hPCl, DockPanel.CENTER);
            collocations.setVisibleItemCount(3);
            dockCl.add(collocations, DockPanel.SOUTH);

            VerticalPanel vertical = new VerticalPanel();
            vertical.add(dock);
            vertical.add(dockSyn);
            //vertical.add(dockAss);
            vertical.add(dockLx);
            vertical.add(dockex);

            VerticalPanel vertical2 = new VerticalPanel();

            vertical2.add(dockLx);
            vertical2.add(dockCl);
            vertical2.add(dockGr);

            hPanel.add(vertical);
            hPanel.add(vertical2);
            ap.add(hPanel, DockPanel.CENTER);
            newPhrase.add(ap);
            newPhrase.center();
        }
    }

    final FixedExpression selExpression = new FixedExpression();

    /**
     * Initialize this example.
     */
    @Override
    public Widget onInitialize() {

        // Top ToolBar panel
        final Button newFE = new Button(constants.cwNewFE());
        final Button newRefresh = new Button("Refresh");
        final Button newEdit = new Button("Edit");

        final TextBoxAdvanced searchFilter = new TextBoxAdvanced();

        newEdit.setEnabled(false);
        DockPanel admin = new DockPanel();

        HorizontalPanel hPanel = new HorizontalPanel();
        hPanel.setSpacing(5);
        hPanel.add(newFE);

        HorizontalPanel sPanel = new HorizontalPanel();
        sPanel.setSpacing(5);
        sPanel.add(searchFilter);

        hPanel.add(newRefresh);
        hPanel.add(newEdit);

        VerticalPanel svPanel = new VerticalPanel();
        svPanel.add(hPanel);
        svPanel.add(sPanel);

        searchFilter.addValueChangeHandler(new IStringValueChanged() {
            public void valueChanged(String newValue) {
                dataProvider.setFilter(newValue);
                dataProvider.refresh();
            }
        });

        admin.add(svPanel, DockPanel.NORTH);

        NewFEHandler handler = new NewFEHandler();
        newFE.addClickHandler(handler);

        NewRefFEHandler refhandler = new NewRefFEHandler();

        NewEditFEHandler edithandler = new NewEditFEHandler();
        newRefresh.addClickHandler(refhandler);

        newEdit.addClickHandler(edithandler);

        // Create a DataGrid.
        /*
         * Set a key provider that provides a unique key for each contact. If
         * key is used to identify contacts when fields (such as the name and
         * address) change.
         */
        dataGrid = new DataGrid<FixedExpression>();
        dataGrid.setWidth("100%");

        /*
         * Do not refresh the headers every time the data is updated. The footer
         * depends on the current data, so we do not disable auto refresh on the
         * footer.
         */
        dataGrid.setAutoHeaderRefreshDisabled(true);
        // Set the message to display when the table is empty.

        // Create a Pager to control the table.
        SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
        pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
        pager.setDisplay(dataGrid);
        pager.setVisible(true);

        //dataProvider = new FilteredListDataProvider<FixedExpression>();

        dataProvider.setList(new ArrayList<FixedExpression>());
        dataGrid.setEmptyTableWidget(new Label("Loading, please wait. There are no fixed expressions yet!"));

        // Attach a column sort handler to the ListDataProvider to sort the
        // list.
        final DialogBox dlgBox = new DialogBox();
        final HTML serverResponseLabel = new HTML();
        final Button clsButton = new Button("Close");

        dlgBox.setText("Fixed Expression Result");
        dlgBox.setAnimationEnabled(true);
        // We can set the id of a widget by accessing its
        // Element
        clsButton.getElement().setId("closeButton");
        VerticalPanel dialogVPanel = new VerticalPanel();
        dialogVPanel.addStyleName("dialogVPanel");
        dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
        dialogVPanel.add(serverResponseLabel);
        dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
        dialogVPanel.add(clsButton);
        dlgBox.setWidget(dialogVPanel);

        // Add a handler to close the DialogBox
        clsButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                dlgBox.hide();
            }
        });

        final List<FixedExpression> lst = new ArrayList<FixedExpression>();

        feService.listFixedExpression(new AsyncCallback<List<FixedExpression>>() {
            public void onFailure(Throwable caught) {
                // Show the RPC error message to the
                // user
                dlgBox.setText("Remote Procedure Call - Failure");
                serverResponseLabel.addStyleName("serverResponseLabelError");
                serverResponseLabel.setHTML(SERVER_ERROR + caught.toString());
                dlgBox.center();
                clsButton.setFocus(true);
            }

            public void onSuccess(List<FixedExpression> list) {

                for (FixedExpression f : list) {
                    lst.add(f);
                }
                dlgBox.setText("Fixed Expression - Add Result");
                serverResponseLabel.removeStyleName("Server Response Label Error");
                serverResponseLabel.setHTML("OK" + lst.size());
                // dlgBox.center();
                clsButton.setFocus(true);
            }
        });
        ListHandler<FixedExpression> sortHandler = new ListHandler<FixedExpression>(dataProvider.getList());
        dataGrid.addColumnSortHandler(sortHandler);

        final NoSelectionModel<FixedExpression> selectionModel = new NoSelectionModel<FixedExpression>();

        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

            public void onSelectionChange(SelectionChangeEvent event) {
                @SuppressWarnings("unchecked")
                NoSelectionModel<FixedExpression> sel = (NoSelectionModel<FixedExpression>) event.getSource();
                selExpression.setCoreString(sel.getLastSelectedObject().getCoreString());
                selExpression.setId(sel.getLastSelectedObject().getId());
                if (selExpression.getId() != null) {
                    newEdit.setEnabled(true);
                } else {
                    newEdit.setEnabled(false);
                }
            }
        });

        dataGrid.setSelectionModel(selectionModel);// ,
        // DefaultSelectionEventManager.<FixedExpression>
        // createCheckboxManager());
        // dataGrid.setSele
        dataGrid.setVisible(true);
        initTableColumns(selectionModel, sortHandler);
        dataProvider.addDataDisplay(dataGrid);
        dataGrid.addColumnSortHandler(sortHandler);

        // dataGrid.add

        admin.add(dataGrid, DockPanel.CENTER);
        admin.add(pager, DockPanel.SOUTH);

        admin.setWidth("100%");
        dataGrid.setHeight("450px");
        admin.setCellWidth(dataGrid, "100%");
        admin.setCellWidth(pager, "100%");

        Timer t = new Timer() {
            public void run() {
                dataList = lst;
                List<FixedExpression> list2 = dataProvider.getList();
                list2.addAll(dataList);
                dataProvider.refresh();
            }
        };

        // Schedule the timer to run once in 40 seconds.
        t.schedule(40000);
        return admin;
    }

    private void initTableColumns(final SelectionModel<FixedExpression> selectionModel,
            ListHandler<FixedExpression> sortHandler) {

        // Fixed Expression
        Column<FixedExpression, String> fixedExpression = new Column<FixedExpression, String>(new TextCell()) {
            @Override
            public String getValue(FixedExpression object) {
                return object.getCoreString();
            }
        };
        fixedExpression.setSortable(true);
        sortHandler.setComparator(fixedExpression, new Comparator<FixedExpression>() {

            public int compare(FixedExpression o1, FixedExpression o2) {
                return o1.getCoreString().compareTo(o2.getCoreString());
            }
        });
        dataGrid.addColumn(fixedExpression, "Fixed Expression");
        fixedExpression.setFieldUpdater(new FieldUpdater<FixedExpression, String>() {

            public void update(int index, FixedExpression object, String value) {
                // Called when the user changes the value.
                object.setCoreString(value);
                dataProvider.refresh();
            }
        });
        dataGrid.setColumnWidth(fixedExpression, 50, Unit.PCT);

        // Meaning
        Column<FixedExpression, String> meaningColumn = new Column<FixedExpression, String>(new TextCell()) {
            @Override
            public String getValue(FixedExpression object) {
                return object.getDefinition_short();
            }
        };
        meaningColumn.setSortable(true);
        sortHandler.setComparator(meaningColumn, new Comparator<FixedExpression>() {
            public int compare(FixedExpression o1, FixedExpression o2) {
                return o1.getDefinition_short().compareTo(o2.getDefinition_short());
            }
        });
        dataGrid.addColumn(meaningColumn, "Definition");
        meaningColumn.setFieldUpdater(new FieldUpdater<FixedExpression, String>() {
            public void update(int index, FixedExpression object, String value) {
                // Called when the user changes the value.
                object.setDefinition_short(value);
                dataProvider.refresh();
            }
        });
        dataGrid.setColumnWidth(meaningColumn, 20, Unit.PCT);

        // English Translation.
        Column<FixedExpression, String> engColumn = new Column<FixedExpression, String>(new TextCell()) {
            @Override
            public String getValue(FixedExpression object) {
                return object.getEngTranslation();
            }
        };
        engColumn.setSortable(true);
        sortHandler.setComparator(engColumn, new Comparator<FixedExpression>() {
            public int compare(FixedExpression o1, FixedExpression o2) {
                return o1.getEngTranslation().compareTo(o2.getEngTranslation());
            }
        });
        dataGrid.addColumn(engColumn, "In English");
        engColumn.setFieldUpdater(new FieldUpdater<FixedExpression, String>() {
            public void update(int index, FixedExpression object, String value) {
                // Called when the user changes the value.
                object.setEngTranslation(value);
                dataProvider.refresh();
            }
        });
        dataGrid.setColumnWidth(engColumn, 30, Unit.PCT);

        // Usage Style.
        Column<FixedExpression, String> usColumn = new Column<FixedExpression, String>(new TextCell()) {
            @Override
            public String getValue(FixedExpression object) {
                return object.getUsageStyle();
            }
        };
        usColumn.setSortable(true);
        sortHandler.setComparator(usColumn, new Comparator<FixedExpression>() {
            public int compare(FixedExpression o1, FixedExpression o2) {
                return o1.getUsageStyle().compareTo(o2.getUsageStyle());
            }
        });
        dataGrid.addColumn(usColumn, "Usage Style");
        dataGrid.setColumnWidth(usColumn, 20, Unit.PCT);
    }

    //HERE 

    @Override
    protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
        GWT.runAsync(CwAdminTerminBox.class, new RunAsyncCallback() {

            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }

            public void onSuccess() {
                callback.onSuccess(onInitialize());
            }
        });
    }
}