Mercurial > hg > thermostat-tools-eclipse
view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/editor/BaseMasterDetailsBlock.java @ 99:7484f4e4f587
Fix dirty states in master blocks
Use a SectionPart to manage dirty states. Notify it when
dirty and let it fix up dirty flag on save automatically.
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Fri, 07 Feb 2014 13:24:10 -0500 |
parents | 1c835942f3c2 |
children |
line wrap: on
line source
package com.redhat.thermostat.tools.eclipse.plugin.editor; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TableViewer; 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.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.ui.forms.IManagedForm; import org.eclipse.ui.forms.MasterDetailsBlock; import org.eclipse.ui.forms.SectionPart; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.Section; import com.redhat.thermostat.tools.eclipse.plugin.Messages; import com.redhat.thermostat.tools.eclipse.plugin.model.Plugin; public abstract class BaseMasterDetailsBlock extends MasterDetailsBlock { protected Plugin model; protected SectionPart sectionPart; public void setModel(Plugin model) { this.model = model; } @Override protected void createToolBarActions(IManagedForm managedForm) { // no-op } @Override protected void createMasterPart(final IManagedForm managedForm, final Composite parent) { FormToolkit toolkit = managedForm.getToolkit(); final Section section = toolkit.createSection(parent, Section.TITLE_BAR | Section.DESCRIPTION); section.setText(getSectionTitle()); section.setDescription(getSectionDescription()); sectionPart = new SectionPart(section); managedForm.addPart(sectionPart); Composite sectionContents = toolkit.createComposite(section); GridLayout layout = new GridLayout(2, false); sectionContents.setLayout(layout); section.setClient(sectionContents); Table list = toolkit.createTable(sectionContents, SWT.BORDER | SWT.V_SCROLL); list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); final TableViewer viewer = new TableViewer(list); viewer.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { managedForm.fireSelectionChanged(sectionPart, event.getSelection()); } }); IStructuredContentProvider contentProvider = ArrayContentProvider.getInstance(); viewer.setContentProvider(contentProvider); viewer.setInput(getListViewModel()); model.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent arg0) { viewer.setInput(getListViewModel()); viewer.refresh(); } }); Composite buttons = toolkit.createComposite(sectionContents); GridData buttonCompositeLayoutData = new GridData(SWT.FILL, SWT.FILL, false, true); buttonCompositeLayoutData.verticalAlignment = SWT.BEGINNING; buttons.setLayoutData(buttonCompositeLayoutData); buttons.setLayout(new GridLayout(1, false)); GridData buttonLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false); Button add = toolkit.createButton(buttons, Messages.BaseMasterDetailsBlock_add, SWT.NONE); add.setLayoutData(buttonLayoutData); add.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { addButtonSelected(parent.getShell()); Object lastElement = viewer.getElementAt(viewer.getTable().getItemCount() - 1); if (lastElement != null) { viewer.setSelection(new StructuredSelection(lastElement)); } } }); Button remove = toolkit.createButton(buttons, Messages.BaseMasterDetailsBlock_remove, SWT.NONE); remove.setLayoutData(buttonLayoutData); remove.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { Object selectedItem = ((IStructuredSelection)viewer.getSelection()).getFirstElement(); removeButtonSelected(selectedItem); } }); } /** Return the name of the section containing the model list */ abstract String getSectionTitle(); /** Return the description for the section containing the model list*/ abstract String getSectionDescription(); /** Return the model object to use in the master list */ abstract Object[] getListViewModel(); /** Invoked when the 'add' button is selected */ abstract void addButtonSelected(Shell shell); /** * Invoked when the remove button is selected. The {@code selectedItem} * should be removed from the model. * * @param selectedItem * the item currently selected. */ abstract void removeButtonSelected(Object selectedItem); }