/*
* PropertiesView.java
*
* (c) Copyright 2004-2009, Andreas Beckers.
* All Rights Reserved.
*
* created 17.06.2004 15:36:19 by Andreas Beckers
* last changed $Date$ by $Author$
*/
package net.sf.myway.ui;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;
/**
* @version $Revision$
* @author Andreas Beckers
*/
public class PropertiesView extends ViewPart {
private static Logger _log = Logger.getLogger(PropertiesView.class);
public static final String ID = "net.sf.myway.ui.view.properties";
private TreeViewer _viewer;
private Tree _tree;
private PropertyContentProvider _content;
private ObjectListener _objectListener;
@Override
public void createPartControl(Composite parent) {
_objectListener = new ObjectAdapter() {
@Override
public void changed(ObjectEvent event) {
if (event.getSource() == PropertiesView.this)
return;
if (event.getObject() == _content.getObject()) {
_viewer.refresh();
return;
}
_content.setObject(event.getObject());
_viewer.setInput(_viewer.getInput());
_viewer.refresh();
}
/**
* @see net.sf.myway.ui.ObjectAdapter#selected(net.sf.myway.ui.ObjectEvent)
*/
@Override
public void selected(ObjectEvent event) {
if (event.getSource() == PropertiesView.this)
return;
if (event.getObject() == _content.getObject()) {
_viewer.refresh();
return;
}
_content.setObject(event.getObject());
_viewer.setInput(_viewer.getInput());
_viewer.refresh();
}
};
UIActivator.getDefault().addObjectListener(_objectListener);
_content = new PropertyContentProvider();
_viewer = new TreeViewer(parent, SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
_viewer.setContentProvider(_content);
_viewer.setInput(this);
_tree = _viewer.getTree();
_tree.setHeaderVisible(true);
final TreeEditor editor = new TreeEditor(_tree);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
_tree.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IStructuredSelection sel = (IStructuredSelection) _viewer.getSelection();
UIActivator.getDefault().setSelection(sel);
if (!sel.isEmpty()) {
Object element = sel.getFirstElement();
if (element instanceof PropertyBinding) {
PropertyBinding prop = (PropertyBinding) element;
if (!prop.getDescriptor().isEditable())
return;
createEditor(editor, (TreeItem) e.item);
}
}
}
});
TreeColumn c1 = new TreeColumn(_tree, SWT.DEFAULT);
new TreeViewerColumn(_viewer, c1).setLabelProvider(new ColumnLabelProvider() {
/**
* @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
*/
@Override
public String getText(Object element) {
if (element instanceof PropertyBinding)
return ((PropertyBinding) element).getDescriptor().getName();
return super.getText(element);
}
});
c1.setText("Name");
c1.setWidth(200);
TreeColumn c2 = new TreeColumn(_tree, SWT.DEFAULT);
new TreeViewerColumn(_viewer, c2).setLabelProvider(new ColumnLabelProvider() {
/**
* @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
*/
@Override
public String getText(Object element) {
if (element instanceof PropertyBinding) {
PropertyBinding prop = (PropertyBinding) element;
Object val = prop.getValue();
if (val == null)
return null;
if (val instanceof Calendar)
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(((Calendar) val).getTime());
return val.toString();
}
return super.getText(element);
}
});
c2.setText("Value");
c2.setWidth(250);
Object o = UIActivator.getDefault().getCurrentObject();
_content.setObject(o);
}
/**
* @see org.eclipse.ui.part.WorkbenchPart#dispose()
*/
@Override
public void dispose() {
UIActivator.getDefault().removeObjectListener(_objectListener);
}
@Override
public void setFocus() {
_viewer.getControl().setFocus();
}
/**
* @return Returns the tree.
*/
protected Tree getTree() {
return _tree;
}
/**
* @return Returns the viewer.
*/
protected TreeViewer getViewer() {
return _viewer;
}
/**
* @param editor
* @param e
*/
protected void createEditor(final TreeEditor editor, TreeItem item) {
Class<? extends AbstractEditor> edClass = ((PropertyBinding) item.getData()).getDescriptor().getEditorClass();
if (edClass == null) {
Class<?> type = ((PropertyBinding) item.getData()).getType();
if (type.equals(String.class))
edClass = StringEditor.class;
else if (type.equals(Integer.class) || type.equals(int.class))
edClass = IntegerEditor.class;
}
if (edClass != null)
try {
edClass.newInstance().createPart(getTree(), editor, item, _content);
}
catch (InstantiationException ex) {
// TODO Auto-generated catch block
_log.error("", ex);
}
catch (IllegalAccessException ex) {
// TODO Auto-generated catch block
_log.error("", ex);
}
}
}