Viewpoint – Provide custom save dialog

  1. Viewpoint - Provide custom save dialog
    1. Goals
    2. Customize Viewpoint save dialogs
      1. The @ISaveDialogExtension@ interface
      2. Provide your @ISaveDialogExtension@ using the @fr.obeo.dsl.common.ui.savedialogextension@ extension point
    3. Example

Goals

Viewpoint uses the same Save Dialog for any operation that implies to the end-user whether changes made on session should be saved or discarded (when closing a session through the CloseUISessionCommand, when controlling resources...).

Developer may need to extend this save dialog to propose other choices to the end-user (for example, if the Collaborative mode is active, we have 3 choices instead of 2 : Save and commit, Save and keep changes local, Discard).

Customize Viewpoint save dialogs

The ISaveDialogExtension interface

This interface allows to :

Provide your ISaveDialogExtension using the fr.obeo.dsl.common.ui.savedialogextension extension point

Please refer to this extension point documentation for more details.

Example

package myPackage;

import java.util.Collection;

import org.eclipse.ui.ISaveablePart2;

import com.google.common.collect.Lists;

import fr.obeo.dsl.viewpoint.business.api.session.Session;

public class CustomSaveDialogExtension {

    /**
     * 
     * {@inheritDoc}
     * 
     * @see fr.obeo.dsl.common.ui.tools.api.util.ISaveDialogExtension#isSaveDialogFor(java.lang.Object)
     */
    public boolean isSaveDialogFor(Object objectToSave) {
        // This save dialog is active if
        // - the saved element is a session
        return (objectToSave instanceof Session)
        // - the session ID is "SaveSession"
                && "SaveSession".equals(((Session) objectToSave).getID());
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see fr.obeo.dsl.common.ui.tools.api.util.ISaveDialogExtension#getButtons()
     */
    public Collection<String> getButtons() {
        return Lists.newArrayList("Save", "Make something special", "Discard");
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see fr.obeo.dsl.common.ui.tools.api.util.ISaveDialogExtension#reactToValue(java.lang.Object,
     *      int)
     */
    public int reactToValue(Object objectToSave, int temporaryResult) {
        Session session = (Session) objectToSave;
        int returnedValue = ISaveablePart2.YES;

        switch (temporaryResult) {
        // Case 0 : "Save"
        case 0:
            // we will return ISaveablePart2.YES => the session will be saved
            // normally
            break;
        // Case 1 : "Make something special"
        case 1:
            makeSomethingSpecial(session);
            // we will return CANCEL => nothing else willl be done
            returnedValue = ISaveablePart2.CANCEL;
            break;
        // Case 2 : "Discard"
        case 2:
            // we return NO => changes will be discarded
            returnedValue = ISaveablePart2.NO;
            break;
        // Default : "CANCEL" or any other
        default:
            returnedValue = ISaveablePart2.CANCEL;
            break;

        }
        return returnedValue;
    }
}