at.rc.tacos.client.controller.VehicleTableEditAction.java Source code

Java tutorial

Introduction

Here is the source code for at.rc.tacos.client.controller.VehicleTableEditAction.java

Source

/*******************************************************************************
 * Copyright (c) 2008, 2009 Internettechnik, FH JOANNEUM
 * http://www.fh-joanneum.at/itm
 * 
 *    Licenced under the GNU GENERAL PUBLIC LICENSE Version 2;
 *    You may obtain a copy of the License at
 *    http://www.gnu.org/licenses/gpl-2.0.txt
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *******************************************************************************/
package at.rc.tacos.client.controller;

import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import at.rc.tacos.client.Activator;
import at.rc.tacos.client.modelManager.LockManager;
import at.rc.tacos.client.modelManager.SessionManager;
import at.rc.tacos.client.view.VehicleForm;
import at.rc.tacos.factory.ImageFactory;
import at.rc.tacos.model.VehicleDetail;

/**
 * Opens a form to edit the vehicle
 * 
 * @author b.thek
 */
public class VehicleTableEditAction extends Action {

    // properties
    private TableViewer viewer;

    /**
     * Default class constructor for editing a vehicle
     */
    public VehicleTableEditAction(TableViewer viewer) {
        this.viewer = viewer;
    }

    /**
     * Returns the tooltip text for the action
     * 
     * @return the tooltip text
     */
    @Override
    public String getToolTipText() {
        return "Editiert ein vorhandenes Fahrzeug";
    }

    /**
     * Retruns the text to show in the toolbar
     * 
     * @return the text to render
     */
    @Override
    public String getText() {
        return "Fahrzeug bearbeiten";
    }

    /**
     * Returns the image to use for this action.
     * 
     * @return the image to use
     */
    @Override
    public ImageDescriptor getImageDescriptor() {
        return ImageFactory.getInstance().getRegisteredImageDescriptor("resource.vehicle");
    }

    /**
     * Shows the view to edit a vehicle
     */
    @Override
    public void run() {
        // the selection
        ISelection selection = viewer.getSelection();
        // get the selected entry
        VehicleDetail vehicle = (VehicleDetail) ((IStructuredSelection) selection).getFirstElement();

        // check if the object is currenlty locked
        String resultLockMessage = LockManager.sendLock(VehicleDetail.ID, vehicle.getVehicleName());

        // check the result of the lock
        if (resultLockMessage != null) {
            boolean forceEdit = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),
                    "Information: Eintrag wird bearbeitet",
                    "Das Fahrzeug das Sie bearbeiten mchten wird bereits von " + resultLockMessage
                            + " bearbeitet\n"
                            + "Ein gleichzeitiges Bearbeiten kann zu unerwarteten Fehlern fhren!\n\n"
                            + "Mchten Sie den Eintrag trotzdem bearbeiten?");
            if (!forceEdit)
                return;
            // log the override of the lock
            String username = SessionManager.getInstance().getLoginInformation().getUsername();
            Activator.getDefault().log("Der Eintrag " + vehicle + " wird trotz Sperrung durch " + resultLockMessage
                    + " von " + username + " bearbeitet", Status.WARNING);
        }

        // get the active shell
        Shell parent = PlatformUI.getWorkbench().getDisplay().getActiveShell();

        // create the window
        VehicleForm window = new VehicleForm(parent, vehicle);
        window.create();

        // get the shell and resize
        Shell myShell = window.getShell();
        myShell.setSize(500, 620);

        // calculate and draw centered
        Rectangle workbenchSize = parent.getBounds();
        Rectangle mySize = myShell.getBounds();
        int locationX, locationY;
        locationX = (workbenchSize.width - mySize.width) / 2 + workbenchSize.x;
        locationY = (workbenchSize.height - mySize.height) / 2 + workbenchSize.y;
        myShell.setLocation(locationX, locationY);

        // now open the window
        myShell.open();
    }
}