/**
* 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.
*
* HIDB2 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 hidb2.gui.action.GeneralOpen;
import hidb2.gui.node.DefaultNode;
import hidb2.gui.node.FolderListNode;
import hidb2.kern.Folder;
import hidb2.kern.FolderDescription;
import java.util.logging.Logger;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.DrillDownAdapter;
import org.eclipse.ui.part.ViewPart;
/**
* This View displays the search results.
*
*/
public class SearchView extends ViewPart implements DataUpdatable
{
public static final String ID = "HIDB2.SearchView";
final static Logger log = Logger.getLogger("hidb2.gui");
private Composite _parent;
private TreeViewer _viewer;
private DrillDownAdapter drillDownAdapter;
// private OpenFile openAction;
// private OpenIcon openIconAction;
// private ViewIcon viewIconAction;
private GeneralOpen doubleClickAction;
class NameSorter extends ViewerSorter
{
}
@Override
public void createPartControl(Composite parent)
{
_parent = parent;
// IPreferenceStore pref = Activator.getDefault().getPreferenceStore();
// parent.setLayout(new FillLayout());
fillPullDownMenu();
createDirTree();
}
/**
* Create the file tree viewer.
*/
private void createDirTree()
{
_viewer = new TreeViewer(_parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
drillDownAdapter = new DrillDownAdapter(_viewer);
_viewer.setContentProvider(new SearchViewContentProvider(this));
_viewer.setLabelProvider(new DBDataViewLabelProvider());
_viewer.setSorter(new NameSorter());
_viewer.setInput(getViewSite());
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
_viewer.addSelectionChangedListener(new ISelectionChangedListener()
{
@Override
public void selectionChanged(SelectionChangedEvent event)
{
ISelection selection = _viewer.getSelection();
if (selection.isEmpty())
{
// Clear drawing area
}
else
{
// FileNode fn = (FileNode) ((IStructuredSelection) selection).getFirstElement();
}
}
});
// Add Custom Drawing callbacks
Tree tr = _viewer.getTree();
tr.addListener(SWT.MeasureItem, new Listener()
{
public void handleEvent(Event event)
{
((DefaultNode) ((TreeItem) event.item).getData()).mesureItem(event);
}
});
tr.addListener(SWT.EraseItem, new Listener()
{
public void handleEvent(Event event)
{
((DefaultNode) ((TreeItem) event.item).getData()).eraseItem(event);
}
});
tr.addListener(SWT.PaintItem, new Listener()
{
public void handleEvent(Event event)
{
((DefaultNode) ((TreeItem) event.item).getData()).paintItem(event);
}
});
//par defaut on ouvre au niveau 1
_viewer.expandToLevel(1);
}
private void hookContextMenu()
{
// File Menu
MenuManager menuMgr = new MenuManager("#PopupMenu", "HIDB2.FolderListNode.popup");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener()
{
public void menuAboutToShow(IMenuManager manager)
{
fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(_viewer.getControl());
_viewer.getControl().setMenu(menu);
getSite().registerContextMenu(menuMgr.getId(), menuMgr, _viewer);
}
private void contributeToActionBars()
{
IActionBars bars = getViewSite().getActionBars();
fillLocalPullDown(bars.getMenuManager());
fillLocalToolBar(bars.getToolBarManager());
}
/* (non-Javadoc)
* @see org.eclipse.ui.part.WorkbenchPart#dispose()
*/
@Override
public void dispose()
{
super.dispose();
// Dispose used Fonts
DefaultNode.dispose();
}
private void fillLocalPullDown(IMenuManager manager)
{
// manager.add(openAction);
}
private void fillContextMenu(IMenuManager manager)
{
// Other plug-ins can contribute there actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
manager.add(new Separator());
drillDownAdapter.addNavigationActions(manager);
}
private void fillLocalToolBar(IToolBarManager manager)
{
// manager.add(openAction);
manager.add(new Separator());
drillDownAdapter.addNavigationActions(manager);
}
private void makeActions()
{
// openAction = new OpenFile(_viewer);
doubleClickAction = new GeneralOpen();
// openIconAction = new OpenIcon(this);
// viewIconAction = new ViewIcon(this);
}
private void hookDoubleClickAction()
{
_viewer.addSelectionChangedListener(new ISelectionChangedListener()
{
@Override
public void selectionChanged(SelectionChangedEvent event)
{
doubleClickAction.selectionChanged(null, event.getSelection());
}
});
_viewer.addDoubleClickListener(new IDoubleClickListener()
{
public void doubleClick(DoubleClickEvent event)
{
doubleClickAction.run(null);
}
});
}
public TreeViewer getTree()
{
return _viewer;
}
@Override
public void setFocus()
{
}
private void fillPullDownMenu()
{
// Fill Pulldown menu
// IActionBars bars = getViewSite().getActionBars();
// IMenuManager manager = bars.getMenuManager();
// manager.add(new SwitchListIconAction());
}
/**
*
*/
public void addFolder(Folder f)
{
SearchViewContentProvider cntp = (SearchViewContentProvider) _viewer.getContentProvider();
final FolderListNode fdtn = cntp.update(f);
_parent.getDisplay().asyncExec(new Runnable()
{
public void run()
{
_viewer.refresh(fdtn, true);
}
});
}
public void addFolderDescription(FolderDescription fd)
{
SearchViewContentProvider cntp = (SearchViewContentProvider) _viewer.getContentProvider();
/* FolderListNode fdtn = */ cntp.update(fd);
_parent.getDisplay().asyncExec(new Runnable()
{
public void run()
{
_viewer.refresh();
}
});
}
@Override
public void refresh(int subject, Object clientData)
{
// SearchViewContentProvider cntp = (SearchViewContentProvider) _viewer.getContentProvider();
// switch (subject)
// {
// case FOLDERLIST:
// _viewer.refresh(cntp.updateFolderDescr((FolderDescription) clientData));
// break;
//
// // case LISTDESCRLIST:
// // viewer.refresh(cntp.updateListDescr((ListDescription) clientData));
// // break;
//
// default:
// break;
// }
}
@Override
public void restart()
{
_viewer.setContentProvider(new SearchViewContentProvider(this));
_viewer.refresh();
}
}