Skip to content

Commit

Permalink
Fix issue #63: Infinite panel can be attached to any composite
Browse files Browse the repository at this point in the history
  • Loading branch information
lcaron committed May 31, 2017
1 parent 3009b76 commit d5331b3
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
Expand All @@ -37,7 +38,7 @@ public class InfiniteProgressPanel {
private static final String INFINITE_PANEL_KEY = "org.mihalis.opal.InfinitePanel.InfiniteProgressPanel";
private static final int NUMBER_OF_STEPS = 10;

private final Shell parent;
private final Composite parent;
private Shell shellHover;
private String text;
private Font textFont;
Expand All @@ -58,7 +59,7 @@ public class InfiniteProgressPanel {
/**
* Constructs a new instance of this class given its parent.
*
* @param shell a shell that will be the parent of the new instance (cannot
* @param composite a composite that will be the parent of the new instance (cannot
* be null)
* @exception IllegalArgumentException
* <ul>
Expand All @@ -72,20 +73,21 @@ public class InfiniteProgressPanel {
* thread that created the parent</li>
* </ul>
*/
private InfiniteProgressPanel(final Shell shell) {
if (shell == null) {
private InfiniteProgressPanel(final Composite composite) {
if (composite == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}

if (shell.isDisposed()) {
if (composite.isDisposed()) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}

parent = shell;
if (shell.getData(INFINITE_PANEL_KEY) != null) {

if (composite.getData(INFINITE_PANEL_KEY) != null) {
throw new IllegalArgumentException("This shell has already an infinite panel attached on it !");
}

parent = composite;

text = null;
textFont = null;
barsCount = 14;
Expand All @@ -95,7 +97,7 @@ private InfiniteProgressPanel(final Shell shell) {
fadeIn = false;
fadeOut = false;
fadeOutCounter = 0;
shell.setData(INFINITE_PANEL_KEY, this);
composite.setData(INFINITE_PANEL_KEY, this);

parent.addListener(SWT.Activate, new Listener() {

Expand Down Expand Up @@ -136,7 +138,7 @@ public void start() {
}

private void createShell() {
shellHover = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM | SWT.ON_TOP);
shellHover = new Shell(parent.getShell(), SWT.APPLICATION_MODAL | SWT.NO_TRIM | SWT.ON_TOP);
shellHover.setLayout(new FillLayout());
shellHover.setAlpha(0);

Expand Down Expand Up @@ -349,41 +351,41 @@ public void stop() {
}

/**
* Returns the infinite progress panel for the shell. If no infinite panel
* Returns the infinite progress panel for the composite. If no infinite panel
* has been declared, returns null.
*
* @param shell the shell for which we are trying to get the associated
* progess panel
* @return the progress panel associated to shell, or null if there is no
* progress panel
*/
public static InfiniteProgressPanel getInfiniteProgressPanelFor(final Shell shell) {
if (shell == null) {
public static InfiniteProgressPanel getInfiniteProgressPanelFor(final Composite composite) {
if (composite == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}

if (shell.isDisposed()) {
if (composite.isDisposed()) {
SWT.error(SWT.ERROR_WIDGET_DISPOSED);
}

if (shell.getDisplay().isDisposed()) {
if (composite.getDisplay().isDisposed()) {
SWT.error(SWT.ERROR_DEVICE_DISPOSED);
}

final InfiniteProgressPanel[] temp = new InfiniteProgressPanel[1];
shell.getDisplay().syncExec(new Runnable() {
composite.getDisplay().syncExec(new Runnable() {

@Override
public void run() {
final Object data = shell.getData(INFINITE_PANEL_KEY);
final Object data = composite.getData(INFINITE_PANEL_KEY);
if (data != null && data instanceof InfiniteProgressPanel) {
temp[0] = (InfiniteProgressPanel) data;
}
}
});

if (temp[0] == null) {
return new InfiniteProgressPanel(shell);
return new InfiniteProgressPanel(composite);
} else {
return temp[0];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*******************************************************************************
* Copyright (c) 2011 Laurent CARON
* 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:
* Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation
*******************************************************************************/
package org.mihalis.opal.infinitePanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
* This snippet demonstrates the infinite progress panel
*
*/
public class SnippetInfiniteProgressPanelComposite {
private static Shell shell;
private static Group group;

public static void main(final String[] args) {
final Display display = new Display();
shell = new Shell();
shell.setLayout(new FillLayout());

group = new Group(shell, SWT.NONE);
group.setLayout(new GridLayout(2, false));
group.setText("Sample group");

createRow("First Name");
createRow("Last Name");
createRow("E-mail");
createRow("Phone number");

createButtons();

shell.setSize(shell.computeSize(400, 400));

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

private static void createRow(final String label) {
final Label lbl = new Label(group, SWT.NONE);
lbl.setText(label);
lbl.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));

final Text text = new Text(group, SWT.SINGLE | SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
}

private static void createButtons() {
final Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
composite.setLayout(new GridLayout(2, false));

final Button ok = new Button(composite, SWT.PUSH);
ok.setText("Ok");
ok.setLayoutData(new GridData(SWT.END, SWT.END, true, true));
ok.addSelectionListener(new SelectionListener() {

@Override
public void widgetSelected(final SelectionEvent e) {
// Retrieve an infinite progress panel
final InfiniteProgressPanel panel = InfiniteProgressPanel.getInfiniteProgressPanelFor(group);

// Set up a text (optional)
panel.setText("Please wait...");
panel.setTextColor(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_RED));
panel.setTextFont(new Font(shell.getDisplay(), "Lucida Sans", 18, SWT.BOLD));

panel.start();
final Thread performer = new Thread(new Runnable() {
@Override
public void run() {
performLongTask(panel);
}
}, "Performer");
performer.start();
}

private void performLongTask(final InfiniteProgressPanel panel) {
try {
Thread.sleep(4000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
// Stop the progress panel
panel.stop();
}

@Override
public void widgetDefaultSelected(final SelectionEvent e) {
}
});

final Button cancel = new Button(composite, SWT.PUSH);
cancel.setText("Cancel");
cancel.setLayoutData(new GridData(SWT.CENTER, SWT.END, false, true));
cancel.addSelectionListener(new SelectionListener() {

@Override
public void widgetSelected(final SelectionEvent e) {
shell.dispose();
}

@Override
public void widgetDefaultSelected(final SelectionEvent e) {
shell.dispose();
}
});
}

}

0 comments on commit d5331b3

Please sign in to comment.