blob: 3b2bcd568226ff4b828d830e7448f8e09b7f0778 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2015 ALL4TEC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Jonathan Dumont (ALL4TEC) - initial API and implementation
******************************************************************************/
package org.polarsys.esf.core.common.ui.widget;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE.SharedImages;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
import org.polarsys.esf.core.common.ui.CommonUIActivator;
/**
* Control contribution used to display the current active project
* according to the active editor.
*
* This is made to ease user work, by displaying directly on which project he is working.
*
* @author $Author: jdumont $
* @version $Revision: 83 $
*/
public class ActiveProjectControlContribution
extends WorkbenchWindowControlContribution
implements IPartListener {
/** The default label for the active project name. */
private static final String DEFAULT_PROJECT_NAME = CommonUIActivator.getMessages().getString(
"ActiveProjectControlContribution.default.projectname"); //$NON-NLS-1$
/** The composite containing all the widgets used to display the active project name. */
private ActiveProjectComposite mActiveProjectComposite = null;
/** The last editor activated and used to get the active project name. */
private IEditorPart mActiveEditorPart = null;
/**
* Default constructor.
*/
public ActiveProjectControlContribution() {
// Register this new instance as part listener
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener(this);
}
/**
* {@inheritDoc}
*
* When the control contribution is disposed, removed it from the part listeners.
*/
@Override
public void dispose() {
// Unregister the instance from the part listeners
IWorkbenchWindow vWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (vWorkbenchWindow != null) {
vWorkbenchWindow.getPartService().removePartListener(this);
}
// Call the parent method
super.dispose();
}
/**
* {@inheritDoc}
*/
@Override
protected Control createControl(final Composite pParent) {
// Create a new composite containing all the widgets to display for the active project
mActiveProjectComposite = new ActiveProjectComposite(pParent);
return mActiveProjectComposite;
}
/**
* {@inheritDoc}
*/
@Override
public void partActivated(final IWorkbenchPart pPart) {
// Ensure that the activated part is an editor
if (pPart instanceof IEditorPart) {
// Remember of the active editor
mActiveEditorPart = (IEditorPart) pPart;
// Get the editor input
IEditorInput vEditorInput = mActiveEditorPart.getEditorInput();
// Check the type of the editor input to find the file used as input
IFile vFileInput = null;
if (vEditorInput instanceof IFileEditorInput) {
// For a file editor input, get directly the file object
vFileInput = ((IFileEditorInput) vEditorInput).getFile();
} else if (vEditorInput instanceof URIEditorInput) {
// For an URI editor input, get the file object by looking for in
// the workspace content, the file which corresponds to the given URI
// ... Build the file path from the URI
IPath vFilePath = new Path(((URIEditorInput) vEditorInput).getURI().toPlatformString(true));
// ... Try to find the file from the build path, in the workspace content
vFileInput = ResourcesPlugin.getWorkspace().getRoot().getFile(vFilePath);
}
// Finally, if the input file is found, try to get its parent project name
// to update the controls value
if (vFileInput != null && vFileInput.getProject() != null) {
mActiveProjectComposite.updateActiveProjectControls(vFileInput.getProject().getName());
}
}
}
/**
* {@inheritDoc}
*
* Treat this event as a part activation.
* Otherwise, nothing is done by default.
*/
@Override
public void partBroughtToTop(final IWorkbenchPart pPart) {
partActivated(pPart);
}
/**
* {@inheritDoc}
*/
@Override
public void partClosed(final IWorkbenchPart pPart) {
// If the last active editor used is the one which is closed,
// then reset the controls content to the default value
if (pPart.equals(mActiveEditorPart)) {
mActiveProjectComposite.updateActiveProjectControls(null);
}
}
/**
* {@inheritDoc}
*/
@Override
public void partDeactivated(final IWorkbenchPart pPart) {
// Nothing to do
}
/**
* {@inheritDoc}
*/
@Override
public void partOpened(final IWorkbenchPart pPart) {
// Nothing to do
}
/**
* Composite which contains all the widgets used to display the current project.
*/
private class ActiveProjectComposite
extends Composite {
/** The preferred width for the composite. */
private static final int CONTROL_WIDTH = 250;
/** The preferred height for the composite. */
private static final int CONTROL_HEIGHT = 17;
/** The text used to display the active project name. */
private Text mActiveProjectNameText = null;
/** The introduction label. */
private Label mActiveProjectNameLabel = null;
/** The opened project image. */
private final Image mOpenedProjectImage = PlatformUI.getWorkbench().getSharedImages()
.getImage(SharedImages.IMG_OBJ_PROJECT);
/** The closed project image. */
private final Image mClosedProjectImage = PlatformUI.getWorkbench().getSharedImages()
.getImage(SharedImages.IMG_OBJ_PROJECT_CLOSED);
/**
* Default constructor.
*
* @param pParent The parent composite
*/
ActiveProjectComposite(final Composite pParent) {
// Call the parent method
super(pParent, SWT.FLAT);
// Create all the content of the composite
createContent(pParent);
}
/**
* Create all the widget to include in this composite instance.
*
* @param pParent The parent composite
*/
private void createContent(final Composite pParent) {
// Prepare and set this instance layout and layout data
final GridLayout vLayout = new GridLayout();
vLayout.marginHeight = 0;
vLayout.marginWidth = 1;
setLayout(vLayout);
setFont(pParent.getFont());
// Create a child composite and set its layout and display properties
final Composite vChildComposite = new Composite(this, SWT.BORDER);
vChildComposite.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
final GridLayout vChildLayout = new GridLayout(3, false);
vChildLayout.marginHeight = 0;
vChildLayout.marginWidth = 0;
vChildComposite.setLayout(vChildLayout);
GridData vGridData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
vGridData.widthHint = CONTROL_WIDTH;
vChildComposite.setLayoutData(vGridData);
vChildComposite.setFont(pParent.getFont());
// Create the widgets needed
createActiveProjectLabel(vChildComposite);
createActiveProjectText(vChildComposite);
// Update the text and label values with the default text and image
updateActiveProjectControls(null);
}
/**
* Create the active project text.
*
* @param pParent The parent composite
*/
private void createActiveProjectText(final Composite pParent) {
// Create the text and set its layout
mActiveProjectNameText = new Text(pParent, SWT.SINGLE | SWT.READ_ONLY);
GridData vGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
vGridData.heightHint = CONTROL_HEIGHT;
mActiveProjectNameText.setLayoutData(vGridData);
// Set the text background
mActiveProjectNameText.setBackground(pParent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
}
/**
* Create the active project label.
*
* @param pParent The parent composite
*/
private void createActiveProjectLabel(final Composite pParent) {
// Create the label and set its layout
mActiveProjectNameLabel = new Label(pParent, SWT.NONE);
mActiveProjectNameLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 0, 0));
// Set the label background
mActiveProjectNameLabel.setBackground(pParent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
}
/**
* Update the active project text message, and the displayed image,
* according to the given project name.
*
* If the given value is <code>null</code> or empty, set the default text
* and image.
*
* @param pProjectName The new project name to display
*/
private void updateActiveProjectControls(final String pProjectName) {
// Check if the given project name is valid
if (StringUtils.isNotEmpty(pProjectName)) {
// Update the text value and the label image
if (mActiveProjectNameText != null) {
mActiveProjectNameText.setText(pProjectName);
}
if (mActiveProjectNameLabel != null) {
mActiveProjectNameLabel.setImage(mOpenedProjectImage);
}
} else {
// The given project name is not valid, set the default values
// for the text value and the label image
if (mActiveProjectNameText != null) {
mActiveProjectNameText.setText(DEFAULT_PROJECT_NAME);
}
if (mActiveProjectNameLabel != null) {
mActiveProjectNameLabel.setImage(mClosedProjectImage);
}
}
}
}
}