View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.service;
16  
17  import java.awt.MenuBar;
18  import java.awt.print.PrinterException;
19  import java.awt.print.PrinterJob;
20  import java.util.logging.Logger;
21  import javax.swing.JFrame;
22  import javax.swing.JMenuBar;
23  
24  import net.curre.prefcount.PrefCountRegistry;
25  import net.curre.prefcount.gui.HelpFrame;
26  import net.curre.prefcount.gui.MainWindow;
27  import net.curre.prefcount.gui.Template;
28  import net.curre.prefcount.gui.menu.AwtMenuBar;
29  import net.curre.prefcount.gui.menu.PrefCountMenuBar;
30  import net.curre.prefcount.gui.menu.SwingMenuBar;
31  import net.curre.prefcount.gui.theme.skin.PrefSkin;
32  import net.curre.prefcount.gui.type.WindowComponent;
33  import net.curre.prefcount.util.Utilities;
34  
35  /**
36   * This service bean is responsible for
37   * handling various general tasks such as quitting, printing, etc.
38   * <p/>
39   * Created date: Jun 15, 2008
40   *
41   * @author Yevgeny Nyden
42   */
43  public class MainService {
44  
45    /** Private class logger. */
46    private static Logger log = Logger.getLogger(MainService.class.toString());
47  
48    /** Reference to the help frame. */
49    private static HelpFrame helpFrame;
50  
51    /** Private constructor to prevent instantiation. */
52    private MainService() {
53    }
54  
55    /**
56     * Displays help information in a separate window.
57     *
58     * @param itemEnum help type.
59     */
60    public static synchronized void doShowHelp(WindowComponent itemEnum) {
61  
62      if (helpFrame == null) {
63        helpFrame = new HelpFrame();
64      }
65  
66      helpFrame.refreshText(itemEnum);
67    }
68  
69    /** Sends the game results to the OS printing system. */
70    public static void doPrint() {
71      MainWindow window = PrefCountRegistry.getInstance().getMainWindow();
72      PrinterJob printJob = PrinterJob.getPrinterJob();
73      printJob.setPrintable(window);
74      if (printJob.printDialog()) {
75        try {
76          printJob.print();
77        } catch (PrinterException e) {
78          log.severe("Error printing: " + e);
79        }
80      }
81    }
82  
83    /**
84     * Sends the score board template to the OS printing system.
85     *
86     * @param numberOfPlayers number of players for the template.
87     */
88    public static void doPrintTemplate(int numberOfPlayers) {
89      Template template = new Template(numberOfPlayers);
90      PrinterJob printJob = PrinterJob.getPrinterJob();
91      printJob.setPrintable(template);
92      if (printJob.printDialog()) {
93        try {
94          printJob.print();
95        } catch (PrinterException e) {
96          log.severe("Error printing: " + e);
97        }
98      }
99    }
100 
101   /** Disposes all frames and quits the application. */
102   public static void doQuit() {
103 
104     if (helpFrame != null) {
105       helpFrame.setVisible(false);
106       helpFrame.dispose();
107     }
108 
109     MainWindow window = PrefCountRegistry.getInstance().getMainWindow();
110     window.setVisible(false);
111 
112     // when there is a pending skin set and persist it
113     PrefSkin skin = LafThemeService.getInstance().getPendingSkin();
114     if (skin != null) {
115       SettingsService.updateSkin(skin);
116       try {
117         SettingsService.persistSettings();
118       } catch (ServiceException e) {
119         log.warning("Error while saving the settings: " + e);
120       }
121     }
122 
123     if (window.playerDialogFrame != null) {
124       window.playerDialogFrame.setVisible(false);
125       window.playerDialogFrame.dispose();
126     }
127     window.dispose();
128 
129     System.exit(0);
130   }
131 
132   /**
133    * Creates a menu bar for the player dialog window
134    * and sets it on the mainWindow.playerDialogFrame object.
135    * This menu bar is created and added only when running
136    * on Mac OS platform.
137    *
138    * @param frame Reference to the frame to add this menu bar to.
139    * @return Reference to the created PrefCount menu bar or
140    *         null of running on not Mac OS platform.
141    */
142   public static PrefCountMenuBar addPlayerDialogMenuBar(JFrame frame) {
143     PrefCountMenuBar menuBar = null;
144     if (Utilities.isMacOs()) {
145       menuBar = new AwtMenuBar(PrefCountMenuBar.MenuBarType.PLAYER_DIALOG);
146       frame.setMenuBar((MenuBar) menuBar);
147     }
148     return menuBar;
149   }
150 
151   /**
152    * Creates an appropriate menu bar for the main window
153    * and sets it on the mainWindow object. Since sunstance
154    * LAF does not work with native Mac menu bar, an awt
155    * menu bar is created for the Mac platform; for other
156    * platforms, a swing menu bar is created.
157    *
158    * @param frame Frame to add menu bar to.
159    * @return Reference to the created PrefCount menu bar.
160    */
161   public static PrefCountMenuBar addMainWindowMenuBar(JFrame frame) {
162     PrefCountMenuBar menuBar;
163     if (Utilities.isMacOs()) {
164       menuBar = new AwtMenuBar(PrefCountMenuBar.MenuBarType.MAIN_WINDOW);
165       frame.setMenuBar((MenuBar) menuBar);
166     } else {
167       menuBar = new SwingMenuBar();
168       frame.setJMenuBar((JMenuBar) menuBar);
169     }
170     return menuBar;
171   }
172 
173 
174 }