Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b0054c7e8883f5225843db77987cacdb7ae5780 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * 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:
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/
package org.eclipse.scout.rt.ui.rap.busy;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.scout.commons.logger.IScoutLogger;
import org.eclipse.scout.commons.logger.ScoutLogManager;
import org.eclipse.scout.rt.client.IClientSession;
import org.eclipse.scout.rt.client.busy.BusyJob;
import org.eclipse.scout.rt.ui.rap.window.IRwtScoutPart;
import org.eclipse.scout.rt.ui.rap.window.dialog.RwtScoutDialog;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

/**
 * Default RWT busy handler for a {@link IClientSession}
 * 
 * @author imo
 * @since 3.8
 */
public class WaitForBlockingJob extends BusyJob {
  private static final IScoutLogger LOG = ScoutLogManager.getLogger(WaitForBlockingJob.class);

  private List<IRwtScoutPart> m_parts;

  public WaitForBlockingJob(String name, RwtBusyHandler handler) {
    super(name, handler);
  }

  @Override
  protected RwtBusyHandler getBusyHandler() {
    return (RwtBusyHandler) super.getBusyHandler();
  }

  /**
   * Show a wait cursor until long operation timeout
   */
  @Override
  protected void runBusy(final IProgressMonitor monitor) {
    Display display = getBusyHandler().getDisplay();
    final Control busyControl = (Control) getBusyHandler().getUiEnvironment().getClientSession().getData(RwtBusyHandler.BUSY_CONTROL_CLIENT_SESSION_KEY);
    try {
      if (display != null && !display.isDisposed()) {
        display.syncExec(new Runnable() {
          @Override
          public void run() {
            m_parts = findAffectedParts();
            if (busyControl != null && !busyControl.isDisposed()) {
              busyControl.setVisible(true);
            }
          }
        });
      }
      //
      super.runBusy(monitor);
    }
    finally {
      if (display != null && !display.isDisposed()) {
        display.asyncExec(new Runnable() {
          @Override
          public void run() {
            if (busyControl != null && !busyControl.isDisposed()) {
              busyControl.setVisible(false);
            }
          }
        });
      }
    }
  }

  @Override
  protected void runBlocking(IProgressMonitor monitor) {
    //schedule blocking job
    new BlockPartsJob(getName(), getBusyHandler(), m_parts).schedule();
  }

  /**
   * @return all affected parts in the same swt / scout environment (user session). The first part is the active part
   *         and may be null.
   */
  protected List<IRwtScoutPart> findAffectedParts() {
    ArrayList<IRwtScoutPart> candidateParts = new ArrayList<IRwtScoutPart>();
    for (IRwtScoutPart part : getBusyHandler().getUiEnvironment().getOpenFormParts()) {
      if (isDialogPart(part) || isViewOrEditorPart(part)) {
        candidateParts.add(part);
      }
    }
    ArrayList<IRwtScoutPart> affectedParts = new ArrayList<IRwtScoutPart>();
    //find an active dialog, it would be the only affected item
    for (IRwtScoutPart part : candidateParts) {
      if (part.isActive() && isDialogPart(part)) {
        affectedParts.add(part);
        return affectedParts;
      }
    }
    //find a visible dialog, it would be the only affected item
    for (IRwtScoutPart part : candidateParts) {
      if (part.isActive() && isDialogPart(part)) {
        affectedParts.add(part);
        return affectedParts;
      }
    }
    //find an active view, also all other views are affected
    for (IRwtScoutPart part : candidateParts) {
      if (part.isActive() && isViewOrEditorPart(part)) {
        affectedParts.addAll(candidateParts);
        affectedParts.remove(part);
        affectedParts.add(0, part);
        return affectedParts;
      }
    }
    //all views are affected, none shows a cancel button
    affectedParts.add(null);
    affectedParts.addAll(candidateParts);
    return affectedParts;
  }

  public static boolean isDialogPart(IRwtScoutPart part) {
    return (part instanceof RwtScoutDialog);
  }

  public static boolean isViewOrEditorPart(IRwtScoutPart part) {
    return part != null && !part.getClass().getSimpleName().contains("Popup");
  }

  protected List<IRwtScoutPart> getParts() {
    return m_parts;
  }

}

Back to the top