我有一个复合父级,包含一些按钮,我想显示属性视图上的按钮信息。我找到了几个解决方案
getSite().setSelectionProvider(viewer);
但是ListView,TableViewer,vv等查看器是ISelectProvider => parent不是ISelectProvider,所以我不知道如何实现。请帮帮我?
ButtonElementProperties.java
public class ButtonElementProperties implements IPropertySource {
final protected ButtonElement element;
protected static final String PROPERTY_FONT = "font";
protected static final String PROPERTY_SIZE = "size";
protected static final String PROPERTY_TEXT = "text";
private final Object PropertiesTable[][] = {
{ PROPERTY_FONT, new FontPropertyDescriptor(PROPERTY_FONT, "Font") },
{ PROPERTY_SIZE, new PropertyDescriptor(PROPERTY_SIZE, "Size") },
{ PROPERTY_TEXT, new TextPropertyDescriptor(PROPERTY_TEXT, "Text") },
};
String strFont = "";
Point ptSize = null;
String strText = "";
protected void firePropertyChanged(String propName, Object value) {
Button ctl = element.getControl();
if (ctl == null) {
// the GUIView is probably hidden in this case
return;
}
if (propName.equals(PROPERTY_FONT)) {
/**
Font oldfont = ctl.getFont();
if (oldfont != null) {
oldfont.dispose();
}
**/
ctl.setFont(new Font (ctl.getDisplay(),new FontData((String)value)) );
return;
}
if (propName.equals(PROPERTY_TEXT)) {
ctl.setText((String)value);
return;
}
}
protected void initProperties() {
Button ctl = element.getControl();
if (ctl == null) {
// the GUIView is probably hidden in this case
return;
}
strText = ctl.getText();
/**
Font font = ctl.getFont();
if (font != null) {
strFont = font.getFontData().toString();
}
**/
ptSize = ctl.getSize();
}
/**
* Creates a new ButtonElementProperties.
*
* @param element the element whose properties this instance represents
*/
public ButtonElementProperties(ButtonElement element) {
super();
this.element = element;
initProperties();
}
/**
* @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
*/
public Object getEditableValue() {
return this;
}
/**
* @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
*/
public IPropertyDescriptor[] getPropertyDescriptors() {
// Create the property vector.
IPropertyDescriptor[] propertyDescriptors = new IPropertyDescriptor[PropertiesTable.length];
for (int i=0;i<PropertiesTable.length;i++) {
// Add each property supported.
PropertyDescriptor descriptor;
descriptor = (PropertyDescriptor)PropertiesTable[i][1];
propertyDescriptors[i] = (IPropertyDescriptor)descriptor;
descriptor.setCategory("Basic");
}
// Return it.
return propertyDescriptors;
}
/**
* @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(Object)
*/
public Object getPropertyValue(Object name) {
if (name.equals(PROPERTY_FONT))
return strFont;
if (name.equals(PROPERTY_SIZE))
return new SizePropertySource(element,ptSize);
if (name.equals(PROPERTY_TEXT))
return strText;
return null;
}
/**
* @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(Object)
*/
public boolean isPropertySet(Object id) {
return false;
}
/**
* @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(Object)
*/
public void resetPropertyValue(Object id) {
}
/**
* @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(Object, Object)
*/
public void setPropertyValue(Object name, Object value) {
firePropertyChanged((String)name,value);
if (name.equals(PROPERTY_FONT)) {
strFont = (String)value;
return;
}
if (name.equals(PROPERTY_TEXT)) {
strText = (String)value;
return;
}
if (name.equals(PROPERTY_SIZE)) {
SizePropertySource sizeProp = (SizePropertySource)value;
ptSize = sizeProp.getValue();
}
}
/**
* Returns the mocha element.
* @return MochaElement
*/
public ButtonElement getButtonElement() {
return element;
}
}
ButtonElement.java
public class ButtonElement implements IWorkbenchAdapter,IAdaptable {
private String headingName;
private Button ctl;
/**
* Creates a new MarkElement and stores parent element and
* location in the text.
*
* @param parent the parent of this element
* @param heading text corresponding to the heading
* @param offset the offset into the Readme text
* @param length the length of the element
*/
public ButtonElement(Button initBtn, String heading) {
this.headingName = heading;
this.ctl = initBtn;
}
/* (non-Javadoc)
* Method declared on IAdaptable
*/
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
if (adapter == IWorkbenchAdapter.class)
return this;
if (adapter == IPropertySource.class)
return new ButtonElementProperties(this);
return null;
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public ImageDescriptor getImageDescriptor(Object object) {
return null;
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public String getLabel(Object o) {
return headingName;
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public Object getParent(Object o) {
return null;
}
public Button getControl() {
return ctl;
}
/**
* @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(Object)
*/
public Object[] getChildren(Object o) {
return null;
}
}
AdaptableList.java
public class AdaptableList implements IWorkbenchAdapter, IAdaptable {
@SuppressWarnings("rawtypes")
protected List children = new ArrayList();
/**
* Creates a new adaptable list with the given children.
*/
public AdaptableList() {
}
/**
* Creates a new adaptable list with the given children.
*/
@SuppressWarnings("unchecked")
public AdaptableList(IAdaptable[] newChildren) {
for (int i = 0; i < newChildren.length; i++) {
children.add(newChildren[i]);
}
}
/**
* Adds all the adaptable objects in the given enumeration to this list.
* Returns this list.
*/
@SuppressWarnings("rawtypes")
public AdaptableList add(Iterator iterator) {
while (iterator.hasNext()) {
add((IAdaptable)iterator.next());
}
return this;
}
/**
* Adds the given adaptable object to this list.
* Returns this list.
*/
@SuppressWarnings("unchecked")
public AdaptableList add(IAdaptable adaptable) {
children.add(adaptable);
return this;
}
/* (non-Javadoc)
* Method declared on IAdaptable
*/
@SuppressWarnings("rawtypes")
public Object getAdapter(Class adapter) {
if (adapter == IWorkbenchAdapter.class) return this;
return null;
}
/**
* Returns the elements in this list.
*/
public Object[] getChildren() {
return children.toArray();
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public Object[] getChildren(Object o) {
return children.toArray();
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public ImageDescriptor getImageDescriptor(Object object) {
return null;
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public String getLabel(Object object) {
return object == null ? "" : object.toString(); //$NON-NLS-1$
}
/* (non-Javadoc)
* Method declared on IWorkbenchAdapter
*/
public Object getParent(Object object) {
return null;
}
/**
* Removes the given adaptable object from this list.
*/
public void remove(IAdaptable adaptable) {
children.remove(adaptable);
}
/**
* Returns the number of items in the list
*/
public int size() {
return children.size();
}
}
===&GT;我有多页面编辑器,第1页我将此视图称为 LayoutViewManager
/**
* Creates page 1 of the multi-page editor,
* Layout
*/
void createPage1() {
final Composite composite = new Composite(getContainer(), SWT.NONE);
GridLayout layout = new GridLayout();
composite.setLayout(layout);
LayoutViewManager v = new LayoutViewManager();
v.createPartControl(composite);
int index = addPage(composite);
setPageText(index, "Layout");
}
当我将drop项目拖到此视图时,我想在属性视图中显示按钮(字体,大小,文本)的一些信息,但是这段代码不起作用,getSite()返回null,我该如何实现呢? ?
LayoutViewManager
public class LayoutViewManager extends ViewPart {
public static String LAYOUT_VIEW_ID = "customplugin.views.layoutView";
private Label label;
private Group grp1;
private AdaptableList ctlList;
private ListViewer viewer;
public LayoutViewManager() {
super();
}
@Override
public void createPartControl(final Composite parent) {
//final Composite container = new Composite(parent, SWT.NONE);
RowLayout rowLayout = new RowLayout();
parent.setLayout(rowLayout);
createDropTarget(parent);
/*
viewer = new ListViewer(parent, SWT.SINGLE );
Button btn = new Button(parent, SWT.BORDER);
ctlList = new AdaptableList();
ButtonElement btnEl = new ButtonElement(btn,"dd");
ctlList = new AdaptableList();
ctlList.add(btnEl);
viewer.setContentProvider(new BaseWorkbenchContentProvider());
viewer.setLabelProvider(new WorkbenchLabelProvider());
viewer.setInput(ctlList); */
// getSite().setSelectionProvider(viewer);
}
// private static final class SelProvider implements ISelectionProvider {
// protected IStructuredSelection projectSelection = StructuredSelection.EMPTY;
// private ISelectionProvider selProvider;
//
// public void addSelectionChangedListener(
// ISelectionChangedListener listener) {
// // do nothing
// }
//
// public ISelection getSelection() {
//// return new StructuredSelection(new Button(, 0));
// }
//
// public void removeSelectionChangedListener(
// ISelectionChangedListener listener) {
// // do nothing
// }
//
// public void setSelection(ISelection selection) {
// // do nothing
// }
// }
/**
* create drop target
* @param parent
*/
public void createDropTarget (final Composite parent) {
// // Allow data to be copied or moved to the drop target
int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT;
DropTarget target = new DropTarget(parent, operations);
// Receive data in Text or File format
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
target.setTransfer(types);
target.addDropListener(new DropTargetListener() {
@Override
public void dropAccept(DropTargetEvent event) {
}
@Override
public void drop(DropTargetEvent event) {
String data = (String) event.data;
System.out.print(data);
if (data.equalsIgnoreCase("TextView")) {
Label label = new Label(parent, SWT.BORDER);
label.setText(data);
} else if (data.equalsIgnoreCase("Button")) {
Button btn = new Button(parent, SWT.BORDER);
btn.setText(data);
// ButtonElement btnEl = new ButtonElement(btn,data);
// ctlList.add(btnEl);
}
parent.pack();
parent.layout(true);
}
@Override
public void dragOver(DropTargetEvent event) {
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
}
@Override
public void dragLeave(DropTargetEvent event) {
}
@Override
public void dragEnter(DropTargetEvent event) {
}
});
}
@Override
public Object getAdapter(Class adapter) {
return super.getAdapter(adapter);
}
@Override
public void setFocus() {
viewer.getControl().setFocus();
}
@Override
public void dispose() {
super.dispose();
}
}
答案 0 :(得分:0)
编写自己的选择提供程序,例如:
class SelProvider implements ISelectionProvider {
public ISelection getSelection() {
return new StructuredSelection(button information you want as the selection);
}
public void addSelectionChangedListener(ISelectionChangedListener listener) {
// do nothing
}
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
// do nothing
}
public void setSelection(ISelection selection) {
// do nothing
}
}
您只需要实施getSelection
方法。