]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.team.ui/src/org/simantics/team/ui/handlers/StageEditHandler.java
Removed contact application support prints
[simantics/platform.git] / bundles / org.simantics.team.ui / src / org / simantics / team / ui / handlers / StageEditHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.team.ui.handlers;
13
14 import java.io.File;
15 import java.nio.charset.Charset;
16
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.ui.PlatformUI;
24 import org.simantics.db.Resource;
25 import org.simantics.db.ServerReference;
26 import org.simantics.db.Session;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.service.LifecycleSupport;
29 import org.simantics.team.Activator;
30 import org.simantics.team.internal.StagingLauncher;
31 import org.simantics.team.internal.StagingLauncher.Config;
32 import org.simantics.team.internal.StagingLauncher.StagingResult;
33 import org.simantics.ui.SimanticsUI;
34 import org.simantics.ui.workbench.handler.AbstractPreferenceHandler;
35 import org.simantics.utils.FileUtils;
36 import org.simantics.utils.ui.ErrorLogger;
37 import org.simantics.utils.ui.dialogs.ShowError;
38
39 public class StageEditHandler extends AbstractPreferenceHandler {
40
41     public StageEditHandler() {
42         super(Activator.getDefault());
43     }
44     private void showError(String msg, Throwable t) {
45         String title = "Staging failed!";
46         ShowError.showError(title, msg, t);
47     }
48     @Override
49     public Object execute(ExecutionEvent event) throws ExecutionException {
50         System.out.println("DEBUG: EditStageHandler");
51         try {
52             Session session = SimanticsUI.getSession();
53             File wsFolder = new File(Platform.getInstanceLocation().getURL().getFile());
54             File stFolder = new File(wsFolder, "staging");
55             if (!stFolder.isDirectory())
56                 throw new DatabaseException("Folder must exist. path=" + stFolder.getAbsolutePath());
57             show(session, stFolder);
58         } catch (Throwable t) {
59             showError("Staging failed:", t);
60         } finally {
61         }
62         return null;
63     }
64     private void show(Session session, File stFolder) {
65         final IProgressMonitor monitor = new NullProgressMonitor();
66         try {
67             monitor.beginTask("begin", 1300);
68             LifecycleSupport lfs = session.getService(LifecycleSupport.class);
69             ServerReference sref = lfs.getSessionReference().getServerReference();
70 //            SerialisationSupport ss = session.getService(SerialisationSupport.class);
71             String randomAccessId = "target"; // String.valueOf(ss.getRandomAccessId(target));
72             Resource targetLibrary = null;
73             final Config config = new Config(session, targetLibrary, stFolder, null);
74             StagingResult result = StagingLauncher.launch(config, sref.toString(), randomAccessId);
75             if (result.getExitValue() != 0) {
76                 String msg = "";
77                 if (result.getLogFile().exists()) {
78                     byte[] data = FileUtils.readFile(result.getLogFile());
79                     msg = new String(data, Charset.defaultCharset());
80                 }
81                 if (msg.isEmpty() || msg.matches(" *"))
82                     msg = "Staging crashed.";
83                 ShowError.showError("Staging failed, errorcode="+result.getExitValue(), msg, new Exception());
84             }
85             if (result.getMessageLog() != null) {
86                 MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
87                         "Staging failed",
88                         result.getMessageLog());
89             }
90             } catch (Exception e) {
91                 ErrorLogger.defaultLogError(e);
92             } finally {
93                 monitor.done();
94             }
95     }
96 //    private String openInitDialog()
97 //    throws DatabaseException {
98 //        final Display display = PlatformUI.getWorkbench().getDisplay();
99 //        final StageInitDialog.Data data = new StageInitDialog.Data();
100 //        display.syncExec(new Runnable() {
101 //            @Override
102 //            public void run() {
103 //                data.comment = "Help!";
104 //                StageInitDialog d =  new StageInitDialog(display.getActiveShell(), data);
105 //                if (Dialog.OK != d.open())
106 //                    data.comment = null;
107 //            }});
108 //        return data.comment;
109 //    }
110 }