/******************************************************************************* * Licensed Materials - Property of IBM * (c) Copyright IBM Corporation 2009. All Rights Reserved. * * Note to U.S. Government Users Restricted Rights: Use, * duplication or disclosure restricted by GSA ADP Schedule * Contract with IBM Corp. *******************************************************************************/ package com.ibm.team.workitem.example.ide.ui; import java.util.ArrayList; import java.util.List; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.LocalResourceManager; import org.eclipse.jface.resource.ResourceManager; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import com.ibm.team.jface.JazzResources; import com.ibm.team.repository.common.util.NLS; import com.ibm.team.ui.editor.ITeamFormConfiguration; import com.ibm.team.ui.editor.ITeamFormData; import com.ibm.team.ui.editor.ITeamFormLayout; import com.ibm.team.ui.editor.TeamFormLayouts; import com.ibm.team.workitem.client.WorkItemChangeEvent; import com.ibm.team.workitem.client.WorkItemWorkingCopy; import com.ibm.team.workitem.client.internal.util.ResolvedWorkItem; import com.ibm.team.workitem.common.model.IAttribute; import com.ibm.team.workitem.ide.ui.internal.editor.Util; import com.ibm.team.workitem.ide.ui.internal.editor.WorkItemEditorInput; import com.ibm.team.workitem.ide.ui.internal.editor.WorkItemEditorInputFuture; import com.ibm.team.workitem.ide.ui.internal.editor.WorkItemEditorToolkit; import com.ibm.team.workitem.ide.ui.internal.editor.WorkItemUIWorkingCopy; import com.ibm.team.workitem.ide.ui.internal.editor.presentations.*; import com.ibm.team.workitem.ide.ui.internal.editor.presentations.presentationhandler.AbstractPresentationUpdater; import com.ibm.team.workitem.ide.ui.internal.editor.presentations.presentationhandler.IPresentationUpdater; import com.ibm.team.workitem.ide.ui.internal.editor.presentations.presentationhandler.PresentationHandlerManager; import com.ibm.team.workitem.rcp.ui.IWorkItemUIWorkingCopy; public class StarAttributePart extends AttributePart { private WorkItemWorkingCopy fWorkingCopy; private RequiredPropertyLabel fAttributeName; private Button fStar; private ResourceManager fResourceManager; private IPresentationUpdater fPresentationUpdater= new AbstractPresentationUpdater() { @Override public void setVisible(boolean visible) { List changedControls= new ArrayList(); if (fAttributeName != null && !fAttributeName.isDisposed()) { TeamFormUtil.setVisible(fAttributeName.getLayoutControl(), visible); changedControls.add(fAttributeName.getLayoutControl()); } if (fStar != null && !fStar.isDisposed()) { TeamFormUtil.setVisible(fStar, visible); changedControls.add(fStar); } Util.updateFormLayout(changedControls.toArray(new Control[changedControls.size()])); }; @Override public void setRequired(boolean required) { if (fAttributeName != null && !fAttributeName.isDisposed()) { fAttributeName.setRequired(required); } } @Override public void attributeChanged(WorkItemChangeEvent event) { handleValueChanged(); }; }; @Override public void createContent(ITeamFormLayout formLayout) { final WorkItemEditorToolkit toolkit= (WorkItemEditorToolkit) getSite().getToolkit(); Composite parent= formLayout.getContainer(); // Attribute Name Label if (isLabelVisible()) { fAttributeName= new RequiredPropertyLabel(parent, toolkit, getBackgroundStyle()); formLayout.add(fAttributeName.getLayoutControl(), ITeamFormConfiguration.LABEL_GUIDE); } fResourceManager= new LocalResourceManager(JFaceResources.getResources(), parent); // Star Button fStar= toolkit.createButton(parent, "", SWT.TOGGLE | SWT.FLAT, getBackgroundStyle()); //$NON-NLS-1$ TeamFormLayouts.setLayoutData(fStar, ITeamFormData.BUTTON); formLayout.add(fStar, ITeamFormConfiguration.CONTENT_GUIDE); if (isReadOnly()) { fStar.setEnabled(false); } else { fStar.addSelectionListener(new SelectionListener() { @Override public void widgetDefaultSelected(SelectionEvent e) { } @Override public void widgetSelected(SelectionEvent e) { if (fWorkingCopy != null) { // Change the value on the work item fWorkingCopy.getWorkItem().setValue(getAttribute(), fStar.getSelection()); } } }); } } // Get the value from the resolved work item private boolean getNonNullValue() { Boolean value= null; if (fWorkingCopy != null && getAttribute() != null && fWorkingCopy.getWorkItem().hasAttribute(getAttribute())) { WorkItemUIWorkingCopy uiCopy= (WorkItemUIWorkingCopy) fWorkingCopy.getAdapter(IWorkItemUIWorkingCopy.class); ResolvedWorkItem workItem= uiCopy.getResolvedWorkItem(); value= (Boolean)workItem.getValue(getAttribute()); } if (value == null) { value= Boolean.FALSE; } return value.booleanValue(); } // The value has changed, update the UI private void handleValueChanged() { if (!fStar.isDisposed()) { boolean value= getNonNullValue(); if (fStar.getSelection() != value) { fStar.setSelection(value); } if (value) { fStar.setImage(JazzResources.getImageWithDefault(fResourceManager, ImagePool.STAR_ICON_ENABLED)); } else { fStar.setImage(JazzResources.getImageWithDefault(fResourceManager, ImagePool.STAR_ICON_DISABLED)); } } } @Override public void setFocus() { if (fStar != null && !fStar.isDisposed()) { fStar.setFocus(); } } @Override public void setInput(Object input) { removeListeners(); if (input instanceof WorkItemEditorInput && ((WorkItemEditorInput) input).isResolved() && getAttribute() != null) { WorkItemEditorInput workItemEditorInput= (WorkItemEditorInput) input; fWorkingCopy= workItemEditorInput.getWorkingCopy(); addListeners(); // Update Attribute Name if (fAttributeName != null && fStar != null && !fAttributeName.isDisposed()) { fAttributeName.setText(NLS.bind(ATTRNAME_COLON, getAttribute().getDisplayName())); } // Update Button Image if (fStar != null && !fStar.isDisposed()) { handleValueChanged(); } } else if (input instanceof WorkItemEditorInputFuture) { fWorkingCopy= null; } } private void addListeners() { PresentationHandlerManager mgr= (PresentationHandlerManager) getSite().getAdapter(PresentationHandlerManager.class); if (mgr != null) { mgr.addPresentationUpdater(fPresentationUpdater, getDescriptor()); } } private void removeListeners() { if (getSite() != null) { PresentationHandlerManager mgr= (PresentationHandlerManager) getSite().getAdapter(PresentationHandlerManager.class); if (mgr != null) { mgr.removePresentationUpdater(fPresentationUpdater); } } } @Override public void dispose() { removeListeners(); fWorkingCopy= null; super.dispose(); } @Override public IAttribute getAttribute() { return getAttribute(fWorkingCopy); } }