001: package net.sourceforge.tracelog.ui;
002:
003: import net.sourceforge.tracelog.utils.Util;
004:
005: import org.eclipse.swt.SWT;
006: import org.eclipse.swt.graphics.Image;
007: import org.eclipse.swt.graphics.Rectangle;
008: import org.eclipse.swt.layout.GridData;
009: import org.eclipse.swt.layout.GridLayout;
010: import org.eclipse.swt.widgets.Event;
011: import org.eclipse.swt.widgets.Label;
012: import org.eclipse.swt.widgets.Listener;
013: import org.eclipse.swt.widgets.MessageBox;
014: import org.eclipse.swt.widgets.ProgressBar;
015: import org.eclipse.swt.widgets.Shell;
016:
017: public class ShellMain extends AbstractWidget {
018: private IMediator actionMediator;
019: private Shell mainShell;
020: private int splashCountdown = 3; // 3 seconds splash
021:
022: // TODO remove this before deploying
023: // private int splashCountdown = 0; // for testing only
024:
025: ShellMain() {
026: super ();
027: }
028:
029: public void createLogViewer() {
030: widgetFactory.createMainShellLogViewer(mainShell,
031: actionMediator).run();
032: mainShell.layout();
033: }
034:
035: public void run() {
036: if (splashCountdown == 0) {
037: displayMainShell();
038: } else {
039: displaySplash();
040: }
041: }
042:
043: /**
044: * Positions the shell in the middle of the primary monitor screen.
045: *
046: * @param shell
047: * Shell to be positioned.
048: */
049: private void centerPositionShell(Shell shell) {
050: Rectangle shellRect = shell.getBounds();
051: Rectangle displayRect = display.getPrimaryMonitor().getBounds();
052: int x = (displayRect.width - shellRect.width) / 2;
053: int y = (displayRect.height - shellRect.height) / 2;
054: shell.setLocation(x, y);
055: }
056:
057: /**
058: * Creates the main shell if required and displays it.
059: */
060: private void displayMainShell() {
061: // only create the main shell if it is not created or has already
062: // disposed
063: if (mainShell == null || mainShell.isDisposed()) {
064: mainShell = new Shell(display);
065: mainShell.setLayout(new GridLayout());
066: mainShell.setText(appTitle + " " + appVersion);
067: mainShell.setImage(new Image(display, Util
068: .getOwnResource(projectProperties
069: .getShellIconPath())));
070: mainShell.setSize(800, 600);
071: mainShell.setMinimumSize(800, 600);
072: centerPositionShell(mainShell);
073:
074: // prevents main shell from closing when user clicks on the X at the
075: // top right of the shell. Rather, make it missing so that the log
076: // viewer would run silently at back end. The beauty with this is
077: // that when user clicks on the system tray, user can view
078: // historical log. Another advantage of doing this is to enable the
079: // system tray icon to blink when log viewer is updated.
080: mainShell.addListener(SWT.Close, new Listener() {
081: public void handleEvent(Event event) {
082: event.doit = false;
083:
084: MessageBox messageBox = new MessageBox(mainShell,
085: SWT.YES | SWT.NO);
086: messageBox.setText("Exit Comfirmation");
087: messageBox.setMessage("Would you like " + appTitle
088: + " to run silently in the system tray?");
089:
090: int response = messageBox.open();
091:
092: if (response == SWT.YES) {
093: mainShell.setVisible(false);
094: } else {
095: display.dispose();
096: }
097: }
098: });
099:
100: actionMediator = new ActionMediator();
101: super .setMediator(actionMediator);
102:
103: widgetFactory.createOptionShell(mainShell, actionMediator);
104: widgetFactory.createAboutShell(mainShell, actionMediator);
105: widgetFactory.createHistoryShell(mainShell, actionMediator);
106: widgetFactory.createMainShellMenuBar(mainShell,
107: actionMediator).run();
108: widgetFactory.createMainShellButtonBar(mainShell,
109: actionMediator).run();
110:
111: createLogViewer();
112:
113: mainShell.open();
114: mainShell.forceFocus();
115:
116: widgetFactory.createTrayShell(actionMediator).run();
117: }
118: // if main shell already initialized, display in on the screen.
119: else {
120: mainShell.setMinimized(false);
121: mainShell.setFocus();
122: mainShell.setVisible(true);
123: }
124: }
125:
126: /**
127: * Displays splash screen.
128: */
129: private void displaySplash() {
130: final Image image = new Image(display, getClass()
131: .getResourceAsStream("/images/splash.bmp"));
132:
133: final Shell splash = new Shell(SWT.ON_TOP);
134: GridLayout gridLayout = new GridLayout();
135: gridLayout.marginHeight = 0;
136: gridLayout.marginWidth = 0;
137: gridLayout.horizontalSpacing = 0;
138: gridLayout.verticalSpacing = 0;
139: splash.setLayout(gridLayout);
140:
141: Label label = new Label(splash, SWT.NONE);
142: label.setImage(image);
143:
144: final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
145: bar.setMaximum(splashCountdown);
146: bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
147:
148: splash.pack();
149: centerPositionShell(splash);
150: splash.open();
151:
152: display.asyncExec(new Runnable() {
153: public void run() {
154:
155: int wait = splashCountdown;
156: for (int i = 0; i < wait; ++i) {
157: bar.setSelection(i + 1);
158: try {
159: Thread.sleep(1000);
160: --splashCountdown;
161: } catch (Exception ignored) {
162: }
163: }
164:
165: splash.close();
166: image.dispose();
167: splash.dispose();
168:
169: // display main shell
170: displayMainShell();
171: }
172: });
173:
174: while (!display.isDisposed()) {
175: if (!display.readAndDispatch())
176: display.sleep();
177: }
178:
179: display.dispose();
180: }
181: }
|