/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PoJamas is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui;
import static hidb2.kern.AttrType.T_Object;
import hidb2.gui.editor.HIDB2Toolkit;
import hidb2.gui.util.FormLayoutFactory;
import hidb2.gui.util.PojoCellModifier;
import hidb2.gui.util.PojoColumnDescr;
import hidb2.gui.util.PoserTableViewer;
import hidb2.kern.AttrType;
import hidb2.kern.Attribut;
import hidb2.kern.AttributedDescription;
import hidb2.kern.CardDescription;
import hidb2.kern.DataStore;
import hidb2.kern.Folder;
import hidb2.kern.FolderDescription;
import hidb2.kern.FolderSearchQuery;
import hidb2.kern.FolderSearchResult;
import hidb2.kern.HIDBConst;
import hidb2.kern.QueryAttr;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IViewPart;
import org.h2.fulltext.FullTextLucene;
/**
* Search dialog.
* Manage search options and SearchView filling.
*
*/
public class DlgSearch extends Dialog implements HIDBConst
{
final static Logger log = Logger.getLogger("hidb2.gui");
private int _res = C_FAIL;
private Shell _dlg;
private SearchView _searchView;
private Text _txtSearched;
private Button _chkAllWord;
private Button _chkCaseSensitive;
private Button _chkAnyFolder;
private Combo _cmbDescr;
private PoserTableViewer _tbl;
private ComboBoxCellEditor _cmbCellEditor = null;
private FolderSearchQuery _query = new FolderSearchQuery();
/**
* Search dialog creation.
* @param parent
* @param searchView
*/
public DlgSearch(Shell parent, IViewPart searchView)
{
super(parent, SWT.DIALOG_TRIM);
_searchView = (SearchView) searchView;
_dlg = new Shell(parent.getShell(), SWT.DIALOG_TRIM);
_dlg.setText("Search Options");
_dlg.setLayout(new GridLayout(1, false));
// Create Simple Text Panel
createTextPanel();
// Create FolderDescriptionList Panel
createFolderPanel();
// Validation Button
Composite panButton = new Group(_dlg, SWT.NONE);
GridData gdPan = new GridData();
gdPan.grabExcessHorizontalSpace = true;
gdPan.horizontalAlignment = SWT.FILL;
panButton.setLayoutData(gdPan);
RowLayout rowLayout2 = new RowLayout(SWT.HORIZONTAL);
rowLayout2.fill = true;
rowLayout2.wrap = false;
rowLayout2.justify = true;
rowLayout2.pack = false;
panButton.setLayout(rowLayout2);
Button btnValid = new Button(panButton, SWT.PUSH);
btnValid.setText("OK");
Button btnReindex = new Button(panButton, SWT.PUSH);
btnReindex.setText("reIndex");
// Cancel Button
Button btnCancel = new Button(panButton, SWT.PUSH);
btnCancel.setText("Cancel");
btnValid.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
// Read search options
_query.allWord = _chkAllWord.getSelection();
_query.useAnyFolder = _chkAnyFolder.getSelection();
_query.caseSensitive = _chkCaseSensitive.getSelection();
if (_query.useAnyFolder)
{
_query.searchPattern = _txtSearched.getText();
}
else
{
_query.searchPattern = "";
}
_res = C_OK;
_dlg.close();
}
});
btnCancel.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
_res = C_FAIL;
_dlg.close();
}
});
btnReindex.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
try
{
IRunnableWithProgress op = new IRunnableWithProgress()
{
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
try
{
FullTextLucene.reindex(Application.getDataStore().getCnx());
}
catch (SQLException e1)
{
log.warning("ReIndexation failed");
e1.printStackTrace();
}
}
};
new ProgressMonitorDialog(getParent()).run(true, false, op);
}
catch (InvocationTargetException eit)
{
// handle exception
eit.printStackTrace();
}
catch (InterruptedException eie)
{
// handle cancelation
eie.printStackTrace();
}
}
});
_dlg.setDefaultButton(btnValid);
_dlg.pack();
}
/**
* Create Simple Text Panel
*/
private void createTextPanel()
{
Composite panel = new Group(_dlg, SWT.NONE);
GridData gdPan = new GridData();
gdPan.grabExcessHorizontalSpace = true;
gdPan.horizontalAlignment = SWT.FILL;
panel.setLayoutData(gdPan);
panel.setLayout(new GridLayout(3, false));
Label labS = new Label(panel, SWT.NONE);
labS.setText("Text:");
_txtSearched = new Text(panel, SWT.NONE);
GridData gdCmb = new GridData();
gdCmb.grabExcessHorizontalSpace = true;
gdCmb.horizontalAlignment = SWT.FILL;
gdCmb.minimumWidth = 200;
_txtSearched.setLayoutData(gdCmb);
GridLayout gl = new GridLayout();
Group comp53 = new Group(panel, SWT.VERTICAL);
comp53.setLayout(gl);
comp53.setText("Text Options:");
_chkAllWord = new Button(comp53, SWT.CHECK);
_chkAllWord.setText("All Word");
_chkCaseSensitive = new Button(comp53, SWT.CHECK);
_chkCaseSensitive.setText("Case Sensitive");
_chkAnyFolder = new Button(comp53, SWT.CHECK);
_chkAnyFolder.setText("Any Folder");
_chkAnyFolder.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
_query.useAnyFolder = _chkAnyFolder.getSelection();
_cmbDescr.setEnabled(!_query.useAnyFolder);
}
});
}
/**
* Create FolderDescriptionList Panel
*/
private void createFolderPanel()
{
Composite panel = new Group(_dlg, SWT.NONE);
GridData gdPan = new GridData();
gdPan.grabExcessHorizontalSpace = true;
gdPan.horizontalAlignment = SWT.FILL;
panel.setLayoutData(gdPan);
panel.setLayout(new GridLayout(2, false));
_cmbDescr = new Combo(panel, SWT.READ_ONLY | SWT.DROP_DOWN);
GridData gdCmb = new GridData();
gdCmb.grabExcessHorizontalSpace = true;
gdCmb.horizontalAlignment = SWT.FILL;
gdCmb.minimumWidth = 200;
_cmbDescr.setLayoutData(gdCmb);
_cmbDescr.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
int idx = _cmbDescr.getSelectionIndex();
FolderDescription nfd = Application.getDataStore().getFolderDescriptionList().get(idx);
if (_query.fd != nfd)
{
_query.fd = nfd;
String[] tblAttrName = new String[nfd.getAttributList().size()];
int i = 0;
for (Attribut a : nfd.getAttributList())
{
tblAttrName[i++] = a.getName();
}
_cmbCellEditor.setItems(tblAttrName);
_query.lstQAttr.clear();
_tbl.refresh();
}
}
});
Label labA = new Label(panel, SWT.NONE);
labA.setText("Attributes");
_tbl = new PoserTableViewer(panel);
GridData gdTbl = new GridData();
gdTbl.grabExcessHorizontalSpace = true;
gdTbl.horizontalAlignment = SWT.FILL;
gdTbl.minimumHeight = 60;
gdTbl.heightHint = 100;
_tbl.setLayoutData(gdTbl);
_tbl.addAttribut(new PojoColumnDescr("Attr", T_Object, "Attr", 80)
{
public CellEditor createCellEditor(final Table table)
{
if ((_cellEdt == null) && editable)
{
String[] tblAttrName = new String[0];
_cellEdt = new ComboBoxCellEditor(table, tblAttrName);
_cmbCellEditor = (ComboBoxCellEditor) _cellEdt;
}
return _cellEdt;
}
});
_tbl.addROAttribut("Type", AttrType.T_String, "Type", 80);
_tbl.addAttribut(new PojoColumnDescr("Test", AttrType.T_Object, "Test", 50)
{
public CellEditor createCellEditor(final Table table)
{
if ((_cellEdt == null) && editable)
{
int i = 0;
QueryAttr.T_Test[] te = QueryAttr.T_Test.values();
String[] tblAttrName = new String[te.length];
for (QueryAttr.T_Test e : te)
{
tblAttrName[i++] = e.text;
}
_cellEdt = new ComboBoxCellEditor(table, tblAttrName);
}
return _cellEdt;
}
});
_tbl.addAttribut("Min", AttrType.T_String, "Min", 60);
_tbl.addAttribut("Max", AttrType.T_String, "Max", 60);
// _tbl.addAttribut("Min", AttrType.T_String, "Min", 60);
HIDB2Toolkit toolkit = new HIDB2Toolkit(_dlg.getDisplay());
Composite containerBtn = toolkit.createComposite(panel, SWT.NONE);
containerBtn.setLayout(FormLayoutFactory.createClearGridLayout(true, 1));
/*Button btnAddAfter =*/toolkit.createGridButton(containerBtn, "Add", new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent e)
{
QueryAttr qa = new QueryAttr();
_query.lstQAttr.add(qa);
_tbl.refresh();
}
});
/*Button btnRem =*/toolkit.createGridButton(containerBtn, "Delete", new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent e)
{
ISelection selection = _tbl.getSelection();
if (!selection.isEmpty())
{
Object obj = ((IStructuredSelection) selection).getFirstElement();
QueryAttr qattr = (QueryAttr) obj;
// Execute the deletion
_query.lstQAttr.remove(qattr);
_tbl.refresh();
}
}
});
_tbl.createMMI();
// Add a specific CellModifier to manage Combo indexes <-> Real Value
_tbl.setCellModifier(new PojoCellModifier(_tbl)
{
@Override
public Object getValue(Object element, String property)
{
Object o = null;
if (property.equals("Attr"))
{
int idxName = _query.fd.getAttributList().indexOf(element);
o = new Integer(idxName);
}
else
{
o = super.getValue(element, property);
if (property.equals("Test"))
{
o = new Integer(((Enum<?>) o).ordinal());
}
}
return o;
}
@Override
public void modify(Object element, String property, Object value)
{
if (property.equals("Attr"))
{
int index = (Integer) value;
value = _query.fd.getAttributList().get(index);
}
else
{
if (property.equals("Test"))
{
int index = (Integer) value;
value = QueryAttr.T_Test.values()[index];
}
}
super.modify(element, property, value);
}
});
}
/**
* Open the search specification dialog.
*
* @return
*/
public int open()
{
// Fill fields
_chkAllWord.setSelection(_query.allWord);
_chkAnyFolder.setSelection(_query.useAnyFolder);
_chkCaseSensitive.setSelection(_query.caseSensitive);
_txtSearched.setText(_query.searchPattern);
List<FolderDescription> lstDescr = Application.getDataStore().getFolderDescriptionList();
String[] tabItems = new String[lstDescr.size()];
int i = 0;
for (FolderDescription f : lstDescr)
{
tabItems[i++] = f.getName() + "[" + f.getNbInstance() + "]";
}
_cmbDescr.setItems(tabItems);
_cmbDescr.select(0);
_cmbDescr.setEnabled(!_query.useAnyFolder);
_tbl.setInput(_query.lstQAttr);
_dlg.open();
while (!_dlg.isDisposed())
{
if (!_dlg.getDisplay().readAndDispatch())
_dlg.getDisplay().sleep();
}
_dlg.dispose();
if (_res == C_OK)
{
try
{
IRunnableWithProgress op = new IRunnableWithProgress()
{
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
int rc = C_OK;
DataStore das = Application.getDataStore();
List<FolderSearchResult> lstSR = das.search(_query);
if (rc == C_OK)
{
// Display search results
for (FolderSearchResult fsr : lstSR)
{
FolderDescription fd = null;
CardDescription cd = null;
boolean isCard = false;
// Retrieve FolderDescription
AttributedDescription ad = das.find(fsr.getTableName());
if (ad instanceof CardDescription)
{
cd = (CardDescription) ad;
fd = cd.getParent();
isCard = true;
}
else
{
if (ad instanceof FolderDescription)
{
fd = (FolderDescription) ad;
}
}
if (fd != null) // Ignore ListDescription
{
_searchView.addFolderDescription(fd);
for (int id : fsr.getKeys())
{
Folder f = isCard ? das.find(cd, id) : das.find(fd, id);
_searchView.addFolder(f);
}
}
}
}
}
};
new ProgressMonitorDialog(getParent()).run(true, false, op);
}
catch (InvocationTargetException e)
{
// handle exception
e.printStackTrace();
}
catch (InterruptedException e)
{
// handle cancelation
e.printStackTrace();
}
}
return _res;
}
}