1 package net.sourceforge.tracelog.ui; 2 3 import net.sourceforge.tracelog.utils.Util; 4 5 import org.eclipse.swt.SWT; 6 import org.eclipse.swt.graphics.Image; 7 import org.eclipse.swt.graphics.Rectangle; 8 import org.eclipse.swt.layout.GridData; 9 import org.eclipse.swt.layout.GridLayout; 10 import org.eclipse.swt.widgets.Event; 11 import org.eclipse.swt.widgets.Label; 12 import org.eclipse.swt.widgets.Listener; 13 import org.eclipse.swt.widgets.MessageBox; 14 import org.eclipse.swt.widgets.ProgressBar; 15 import org.eclipse.swt.widgets.Shell; 16 17 public class ShellMain extends AbstractWidget { 18 private Shell mainShell; 19 20 23 private int splashCountdown = 3; 25 ShellMain() { 26 super(); 27 } 28 29 32 private void displaySplash() { 33 final Image image = new Image(display, getClass().getResourceAsStream("/images/splash.bmp")); 34 35 final Shell splash = new Shell(SWT.ON_TOP); 36 GridLayout gridLayout = new GridLayout(); 37 gridLayout.marginHeight = 0; 38 gridLayout.marginWidth = 0; 39 gridLayout.horizontalSpacing = 0; 40 gridLayout.verticalSpacing = 0; 41 splash.setLayout(gridLayout); 42 43 Label label = new Label(splash, SWT.NONE); 44 label.setImage(image); 45 46 final ProgressBar bar = new ProgressBar(splash, SWT.NONE); 47 bar.setMaximum(splashCountdown); 48 bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 49 50 splash.pack(); 51 centerPositionShell(splash); 52 splash.open(); 53 54 display.asyncExec(new Runnable () { 55 public void run() { 56 57 int wait = splashCountdown; 58 for (int i = 0; i < wait; ++i) { 59 bar.setSelection(i + 1); 60 try { 61 Thread.sleep(1000); 62 --splashCountdown; 63 } 64 catch (Exception ignored) { 65 } 66 } 67 68 splash.close(); 69 image.dispose(); 70 splash.dispose(); 71 72 displayMainShell(); 74 } 75 }); 76 77 while (!display.isDisposed()) { 78 if (!display.readAndDispatch()) 79 display.sleep(); 80 } 81 82 display.dispose(); 83 } 84 85 91 private void centerPositionShell(Shell shell) { 92 Rectangle shellRect = shell.getBounds(); 93 Rectangle displayRect = display.getPrimaryMonitor().getBounds(); 94 int x = (displayRect.width - shellRect.width) / 2; 95 int y = (displayRect.height - shellRect.height) / 2; 96 shell.setLocation(x, y); 97 } 98 99 102 private void displayMainShell() { 103 if (mainShell == null || mainShell.isDisposed()) { 106 mainShell = new Shell(display); 107 mainShell.setLayout(new GridLayout()); 108 mainShell.setText(appTitle + " " + appVersion); 109 mainShell.setImage(new Image(display, Util.getOwnResource(projectProperties.getShellIconPath()))); 110 mainShell.setSize(800, 600); 111 mainShell.setMinimumSize(800, 600); 112 centerPositionShell(mainShell); 113 114 mainShell.addListener(SWT.Close, new Listener() { 121 public void handleEvent(Event event) { 122 event.doit = false; 123 124 MessageBox messageBox = new MessageBox(mainShell, SWT.YES | SWT.NO); 125 messageBox.setText("Exit Comfirmation"); 126 messageBox.setMessage("Would you like " + appTitle + " to run silently in the system tray?"); 127 128 int response = messageBox.open(); 129 130 if (response == SWT.YES) { 131 mainShell.setVisible(false); 132 } 133 else { 134 display.dispose(); 135 } 136 } 137 }); 138 139 IMediator actionMediator = new ActionMediator(); 140 super.setMediator(actionMediator); 141 142 widgetFactory.createOptionShell(mainShell, actionMediator); 143 145 widgetFactory.createAboutShell(mainShell, actionMediator); 146 widgetFactory.createHistoryShell(mainShell, actionMediator); 147 148 widgetFactory.createMainShellMenuBar(mainShell, actionMediator).run(); 149 widgetFactory.createMainShellButtonBar(mainShell, actionMediator).run(); 150 widgetFactory.createMainShellLogViewer(mainShell, actionMediator).run(); 151 152 mainShell.open(); 153 mainShell.forceFocus(); 154 155 widgetFactory.createTrayShell(actionMediator).run(); 156 } 157 else { 159 mainShell.setMinimized(false); 160 mainShell.setFocus(); 161 mainShell.setVisible(true); 162 } 163 } 164 165 public void run() { 166 if (splashCountdown == 0) { 167 displayMainShell(); 168 } 169 else { 170 displaySplash(); 171 } 172 } 173 } 174 | Popular Tags |