Home » Eclipse Projects » Eclipse Platform » Adding a PropertySheetViewer-like control within an editor 
| Adding a PropertySheetViewer-like control within an editor [message #191347] | 
Mon, 09 February 2004 04:31   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi all, 
 
I would like to integrate a control that is functionally equivalent to a 
PropertySheetViewer directly into an editor.  Simply using the Properties 
View is not an option, as this control must reside inside of a Composite 
within my editor. 
 
To the best of my knowledge at present, the only means of accomplishing 
this is to roll my own TableTree component that provides the same 
capabilities as Eclipse's PropertySheetViewer object.  PropertySheetViewer 
has package-protected (default) visibility, so it's obviously not an 
option to use it directly. 
 
Is there a simpler means of building a property-editing widget than 
rolling my own from scratch? 
 
Best regards 
--Tod Liebeck
 |  
 |  
  |  
| Re: Adding a PropertySheetViewer-like control within an editor [message #191550 is a reply to message #191347] | 
Mon, 09 February 2004 13:48    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
We had almost exactly the same requirement (except that we didn't have to have 
the TableTree complication), and after much work, and several costly and 
failed attempts to reuse PropertySheet-code, we simply wrote our own, from the 
TableViewer up. The biggest, and still present, pain is dealing with the 
cell-editors, since TableViewer assumes that a given column of the Table will 
only have ONE (1) type of cell-editor ever, but the essence of a 
PropertyViewer is to have different types of properties (boolean, text, 
enumeration) in the same Viewer. 
 
best of luck! 
Paul 
 
Tod Liebeck wrote: 
 
> Hi all, 
> 
> I would like to integrate a control that is functionally equivalent to a 
> PropertySheetViewer directly into an editor.  Simply using the Properties 
> View is not an option, as this control must reside inside of a Composite 
> within my editor. 
> 
> To the best of my knowledge at present, the only means of accomplishing 
> this is to roll my own TableTree component that provides the same 
> capabilities as Eclipse's PropertySheetViewer object.  PropertySheetViewer 
> has package-protected (default) visibility, so it's obviously not an 
> option to use it directly. 
> 
> Is there a simpler means of building a property-editing widget than 
> rolling my own from scratch? 
> 
> Best regards 
> --Tod Liebeck
 |  
 |  
  |  
| Re: Adding a PropertySheetViewer-like control within an editor [message #192928 is a reply to message #191550] | 
Wed, 11 February 2004 19:53   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi Paul, 
 
Thanks for confirming my fears, just wanted to be sure I wasn't 
re-inventing the wheel by writing my own property editing widget.  I've 
got a first draft up-and-running and it seems to work okay.  My version's 
based off Table rather than TableViewer though.  I didn't actually need 
the hierarchal capabilities of TableTree either.  I've attached the source 
of what I've written so far in case it might help you or anyone else here. 
 It's not production quality code yet and still has a few issues but it 
 does work for the most part.  Using it is as simple as adding it to the 
 UI and setting the property source.  Thanks again for the info. 
 
Best regards 
--Tod Liebeck 
  NextApp, Inc. 
 
-------------------------------------------- 
 
package nextapp.es.form.property; 
 
import nextapp.es.Messages; 
 
import org.eclipse.jface.viewers.CellEditor; import 
org.eclipse.jface.viewers.ColumnWeightData; import 
org.eclipse.jface.viewers.ICellEditorListener; import 
org.eclipse.jface.viewers.ILabelProvider; import 
org.eclipse.jface.viewers.TableLayout; import org.eclipse.swt.SWT; import 
org.eclipse.swt.custom.TableEditor; import 
org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; 
import org.eclipse.swt.events.MouseAdapter; import 
org.eclipse.swt.events.MouseEvent; import 
org.eclipse.swt.events.SelectionAdapter; import 
org.eclipse.swt.events.SelectionEvent; import 
org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Composite; import 
org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Table; 
import org.eclipse.swt.widgets.TableColumn; import 
org.eclipse.swt.widgets.TableItem; import 
org.eclipse.ui.views.properties.IPropertyDescriptor; import 
org.eclipse.ui.views.properties.IPropertySource; 
 
/** 
 * Displays an editable table of properties based on an * 
 <code>IPropertySource</code>. 
 */ 
public class PropertyTable extends Composite { 
     
    /** 
     * Listener for the active <code>CellEditor</code>. */ 
    private final ICellEditorListener activeEditorListener = new 
    ICellEditorListener() { 
 
        /** 
         * @see 
          org.eclipse.jface.viewers.ICellEditorListener#cancelEditor() */ 
        public void cancelEditor() { 
            deactivateCellEditor(); 
        } 
         
        /** 
         * @see 
          org.eclipse.jface.viewers.ICellEditorListener#editorValueCha nged(boolean, 
         boolean) */ 
        public void editorValueChanged(boolean oldValidState, boolean 
        newValidState) { } 
         
        /** 
         * @see 
          org.eclipse.jface.viewers.ICellEditorListener#applyEditorVal ue() 
         */ 
        public void applyEditorValue() { 
            PropertyTable.this.applyEditorValue(); deactivateCellEditor(); 
        } 
    }; 
     
    /** 
     * The <code>Table</code> used to display properties. */ 
    private Table table; 
     
    /** 
     * The <code>TableEditor</code> used to edit properties. */ 
    private TableEditor tableEditor; 
     
    /** 
     * The currently active <code>CellEditor</code>. */ 
    private CellEditor cellEditor; 
     
    /** 
     * The <code>IPropertySource</code> from which property data is 
     pulled. */ 
    private IPropertySource propertySource; 
     
    /** 
     * Creates a new <code>PropertyTable</code>. * * @param parent The 
     parent container. */ 
    public PropertyTable(Composite parent) { 
        super(parent, SWT.NONE); 
         
        setLayout(new FillLayout()); 
         
        table = new Table(this, SWT.FULL_SELECTION | SWT.SINGLE | 
        SWT.HIDE_SELECTION); table.setLinesVisible(true); 
        table.setHeaderVisible(true); 
 
        TableColumn nameColumn = new TableColumn(table, SWT.NONE); 
        nameColumn.setText(Messages.getString("ComponentProperties.NameHeader ")); 
 
        TableColumn valueColumn = new TableColumn(table, SWT.NONE); 
        valueColumn.setText(Messages.getString("ComponentProperties.ValueHeader ")); 
         
        TableLayout tableLayout = new TableLayout(); 
        tableLayout.addColumnData(new ColumnWeightData(40, false)); 
        tableLayout.addColumnData(new ColumnWeightData(60, true)); 
        table.setLayout(tableLayout); 
         
        tableEditor = new TableEditor(table); 
         
        table.addSelectionListener(new SelectionAdapter() { 
             
            /** 
             * @see 
              org.eclipse.swt.events.SelectionListener#widgetDefaultSelect ed(org.eclipse.swt.events.SelectionEvent) 
             */ 
            public void widgetDefaultSelected(SelectionEvent e) { 
                processSelection((TableItem) e.item); 
            } 
        }); 
         
        table.addMouseListener(new MouseAdapter() { 
             
            /** 
             * @see 
              org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.s wt.events.MouseEvent) 
             */ 
            public void mouseDown(MouseEvent e) { 
                Point point = new Point(e.x, e.y); 
                TableItem item = table.getItem(point); if (item != null) { 
                    processSelection(item); 
                } 
            } 
        }); 
 
        table.addKeyListener(new KeyAdapter() { 
             
            /** 
             * @see 
              org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.s wt.events.KeyEvent) 
             */ 
            public void keyReleased(KeyEvent e) { 
                if (e.character == SWT.ESC) { 
                    deactivateCellEditor(); 
                } else if (e.character == SWT.F5) { 
                    updateProperties(); 
                } 
            } 
        }); 
    } 
     
    /** 
     * Activates the cell editor for the specified item. */ 
    private void activateCellEditor(TableItem item) { 
        table.showSelection(); 
         
        IPropertyDescriptor propertyDescriptor = (IPropertyDescriptor) 
        item.getData(); cellEditor = 
        propertyDescriptor.createPropertyEditor(table); 
         
        if (cellEditor == null) { 
            // No editor provided. 
            return; 
        } 
         
         cellEditor.setValue(propertySource.getPropertyValue(property Descriptor.getId())); 
        cellEditor.activate(); 
         
        Control control = cellEditor.getControl(); if (control == null) { 
            cellEditor.deactivate(); 
            cellEditor = null; 
            return; 
        } 
 
        //BUGBUG. Temporary workaround...underlying problem is that only 
        // the text portion of the label is being overwritten during 
        editing, // so this workaround simply blots out the existing 
        image. item.setImage(1, null); 
 
        cellEditor.addListener(activeEditorListener); 
        CellEditor.LayoutData layoutData = cellEditor.getLayoutData(); 
         
        tableEditor.horizontalAlignment = layoutData.horizontalAlignment; 
        tableEditor.grabHorizontal = layoutData.grabHorizontal; 
        tableEditor.minimumWidth = layoutData.minimumWidth; 
        tableEditor.setEditor(control, item, 1); 
         
        cellEditor.setFocus(); 
    } 
     
    /** 
     * Applies the value of the currently editing item to the * 
     <code>IPropertySource</code>. 
     */ 
    private void applyEditorValue() { 
        TableItem item = tableEditor.getItem(); if (item == null || 
        item.isDisposed()) { 
            return; 
        } 
        IPropertyDescriptor propertyDescriptor = (IPropertyDescriptor) 
        item.getData(); Object oldValue = 
         propertySource.getPropertyValue(propertyDescriptor.getId()); 
        Object newValue = cellEditor.getValue(); if (oldValue != newValue 
        && (oldValue == null || !oldValue.equals(newValue))) { 
            propertySource.setPropertyValue(propertyDescriptor.getId(), 
            newValue); 
        } 
    } 
     
    /** 
     * Deactivates the active editor. 
     */ 
    private void deactivateCellEditor() { 
        TableItem item = tableEditor.getItem(); 
        tableEditor.setEditor(null, null, 1); if (cellEditor != null) { 
            cellEditor.deactivate(); 
            cellEditor.removeListener(activeEditorListener); cellEditor = 
            null; 
        } 
        if (item != null && !item.isDisposed()) { 
            updateItem(item); 
        } 
    } 
     
    /** 
     * Handles the selection of an item within the table. * * @param item 
     The newly selected item. */ 
    private void processSelection(TableItem item) { 
        if (cellEditor != null) { 
            applyEditorValue(); 
            deactivateCellEditor(); 
        } 
        activateCellEditor(item); 
    } 
     
    /** 
     * Sets the <code>IPropertySource</code> from which property data is * 
     pulled. 
     * 
     * @param propertySource The new <code>IPropertySource</code>. */ 
    public void setPropertySource(IPropertySource propertySource) { 
        this.propertySource = propertySource; updateProperties(); 
    } 
     
    /** 
     * Updates the value of a single <code>TableItem</code> to reflect the 
     * updated state of its represented property. * * @param item The 
     <code>TableItem</code> to update. */ 
    private void updateItem(TableItem item) { 
        IPropertyDescriptor propertyDescriptor = (IPropertyDescriptor) 
        item.getData(); 
         
        ILabelProvider labelProvider = 
        propertyDescriptor.getLabelProvider(); if (labelProvider == null) 
        { 
        } else { 
            Object propertyValue = 
             propertySource.getPropertyValue(propertyDescriptor.getId()); 
            item.setText(0, propertyDescriptor.getDisplayName()); if 
            (propertyValue != null) { 
                item.setText(1, labelProvider.getText(propertyValue)); 
                item.setImage(1, labelProvider.getImage(propertyValue)); 
            } 
        } 
    } 
     
    /** 
     * Updates all properties. 
     */ 
    private void updateProperties() { 
        applyEditorValue(); 
        deactivateCellEditor(); 
         
        table.removeAll(); 
        if (propertySource != null) { 
            IPropertyDescriptor[] propertyDescriptors = 
            propertySource.getPropertyDescriptors(); for (int index = 0; 
            index < propertyDescriptors.length; ++index) { 
                TableItem item = new TableItem(table, SWT.NONE); 
                item.setData(propertyDescriptors[index]); 
                updateItem(item); 
            } 
        } 
    } 
} 
 
 
-------------------------------------------- 
 
 
On Mon, 09 Feb 2004 13:48:28 -0500, Paul T. Keyser wrote: 
 
> We had almost exactly the same requirement (except that we didn't have 
> to have the TableTree complication), and after much work, and several 
> costly and failed attempts to reuse PropertySheet-code, we simply wrote 
> our own, from the TableViewer up. The biggest, and still present, pain 
> is dealing with the cell-editors, since TableViewer assumes that a given 
> column of the Table will only have ONE (1) type of cell-editor ever, but 
> the essence of a PropertyViewer is to have different types of properties 
> (boolean, text, enumeration) in the same Viewer. 
>  
> best of luck! 
> Paul 
>  
> Tod Liebeck wrote: 
>  
>> Hi all, 
>> 
>> I would like to integrate a control that is functionally equivalent to 
>> a PropertySheetViewer directly into an editor.  Simply using the 
>> Properties View is not an option, as this control must reside inside of 
>> a Composite within my editor. 
>> 
>> To the best of my knowledge at present, the only means of accomplishing 
>> this is to roll my own TableTree component that provides the same 
>> capabilities as Eclipse's PropertySheetViewer object. 
>> PropertySheetViewer has package-protected (default) visibility, so it's 
>> obviously not an option to use it directly. 
>> 
>> Is there a simpler means of building a property-editing widget than 
>> rolling my own from scratch? 
>> 
>> Best regards 
>> --Tod Liebeck
 |  
 |  
  |   
Goto Forum:
 
 Current Time: Mon Nov 03 22:06:32 EST 2025 
 Powered by  FUDForum. Page generated in 0.04052 seconds  
 |