1 package org.opensync.engine.admin.gui;
2
3 import org.opensync.engine.server.OpenSync;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import org.opensync.engine.util.I18n;
8 import java.io.*;
9
10 /***
11 *
12 * @version 1.0
13 * @author SOFTMED
14 *
15 */
16
17 public class AppFrm extends JFrame {
18 JMenuBar menuBar = new JMenuBar();
19 JMenu menuFile = new JMenu();
20 JMenuItem menuFileExit = new JMenuItem();
21 JMenuItem menuFileOpenLog = new JMenuItem();
22 JMenuItem menuFileLoadConfig = new JMenuItem();
23 JMenu menuHelp = new JMenu();
24 JMenuItem menuHelpHelp = new JMenuItem();
25 JMenuItem menuHelpAbout = new JMenuItem();
26 JToolBar toolBar = new JToolBar();
27 JButton btLogOpen = new JButton();
28 JButton btOpen = new JButton();
29 JButton btSave = new JButton();
30 JButton btHelp = new JButton();
31 ImageIcon imgOpen;
32 ImageIcon imgLogOpen;
33 ImageIcon imgSave;
34 ImageIcon imgHelp;
35 JLabel statusBar = new JLabel();
36 BorderLayout borderLayout = new BorderLayout();
37 AppPnl appPnl;
38
39 /***
40 * Construct the frame
41 *
42 * @exception Exception
43 * @param openSync
44 */
45 public AppFrm(OpenSync openSync)throws Exception{
46 appPnl = new AppPnl(openSync);
47 openSync.setAppFrame(this);
48 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
49 jbInit();
50 pack();
51 setSize(new Dimension(800, 600));
52 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
53 Dimension frameSize = getSize();
54 if (frameSize.height > screenSize.height) {
55 frameSize.height = screenSize.height;
56 }
57 if (frameSize.width > screenSize.width) {
58 frameSize.width = screenSize.width;
59 }
60 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
61 setIconImage(
62 getToolkit().createImage(AppFrm.class.getResource("/images/opensync.gif"))
63 );
64 setVisible(true);
65 }
66 /****/
67 public AppPnl getAppPnl(){
68 return appPnl;
69 }
70 /***Component initialization*/
71 private void jbInit() throws Exception {
72 I18n i18n = I18n.getInstance();
73 imgLogOpen = new ImageIcon(AppFrm.class.getResource("/images/openFile.gif"));
74 imgOpen = new ImageIcon(AppFrm.class.getResource("/images/openFile.gif"));
75 imgSave = new ImageIcon(AppFrm.class.getResource("/images/closeFile.gif"));
76 imgHelp = new ImageIcon(AppFrm.class.getResource("/images/help.gif"));
77
78 JPanel contentPane = (JPanel) getContentPane();
79 contentPane.setLayout(borderLayout);
80 setTitle(i18n.get("gui.product") + " " + i18n.get("gui.version"));
81 statusBar.setText(" ");
82 menuFile.setText(i18n.get("gui.menu.file"));
83
84 menuFileExit.setText(i18n.get("gui.menu-item.exit"));
85 menuFileExit.addActionListener(new ActionListener() {
86 public void actionPerformed(ActionEvent e) {
87 menuFileExit_actionPerformed(e);
88 }
89 });
90
91 menuFileOpenLog.setText(i18n.get("gui.menu-item.open-log"));
92 menuFileOpenLog.addActionListener(new ActionListener() {
93 public void actionPerformed(ActionEvent e) {
94 menuFileOpenLog_actionPerformed(e);
95 }
96 });
97
98 menuFileLoadConfig.setText(i18n.get("gui.menu-item.load-config"));
99 menuFileLoadConfig.addActionListener(new ActionListener() {
100 public void actionPerformed(ActionEvent e) {
101 menuFileLoadConfig_actionPerformed(e);
102 }
103 });
104
105
106 menuHelp.setText(i18n.get("gui.menu.help"));
107
108 menuHelpAbout.setText(i18n.get("gui.menu-item.about"));
109 menuHelpAbout.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 menuHelpAbout_actionPerformed(e);
112 }
113 });
114
115 menuHelpHelp.setText(i18n.get("gui.menu-item.help"));
116 menuHelpHelp.addActionListener(new ActionListener() {
117 public void actionPerformed(ActionEvent e) {
118 btHelp_actionPerformed(e);
119 }
120 });
121
122 btLogOpen.setIcon(imgOpen);
123 btLogOpen.setToolTipText(i18n.get("gui.menu-item.open-log"));
124 btLogOpen.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
126 menuFileOpenLog_actionPerformed(e);
127 }
128 });
129 btOpen.setIcon(imgOpen);
130 btOpen.setToolTipText(i18n.get("gui.menu-item.open"));
131 btLogOpen.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e) {
133 menuFileOpen_actionPerformed(e);
134 }
135 });
136 btSave.setIcon(imgSave);
137 btSave.setToolTipText(i18n.get("gui.menu-item.close"));
138 btSave.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
140 menuFileSave_actionPerformed(e);
141 }
142 });
143
144 btHelp.setIcon(imgHelp);
145 btHelp.setToolTipText(i18n.get("gui.menu.help"));
146 btHelp.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 btHelp_actionPerformed(e);
149 }
150 });
151 toolBar.add(btLogOpen);
152
153
154 toolBar.add(btHelp);
155 menuFile.add(menuFileOpenLog);
156 menuFile.add(menuFileLoadConfig);
157 menuFile.add(menuFileExit);
158 menuHelp.add(menuHelpHelp);
159 menuHelp.add(menuHelpAbout);
160 menuBar.add(menuFile);
161 menuBar.add(menuHelp);
162 setJMenuBar(menuBar);
163 contentPane.add(toolBar, BorderLayout.NORTH);
164 contentPane.add(statusBar, BorderLayout.SOUTH);
165 contentPane.add(appPnl, BorderLayout.CENTER);
166 }
167 /***
168 * File | Exit action performed
169 *
170 * @param e
171 */
172 public void menuFileExit_actionPerformed(ActionEvent e) {
173 int res = JOptionPane.showOptionDialog(AppPnl.getInstance(),
174 I18n.getInstance().format(
175 "gui.confirm.exit",null
176 ),I18n.getInstance().get("gui.product"),
177 JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,
178 new String[]{I18n.getInstance().get("common.yes"),I18n.getInstance().get("common.no")},
179 I18n.getInstance().get("common.no")
180 );
181 if(res == JOptionPane.YES_OPTION){
182 OpenSync openSync = OpenSync.getInstance();
183 if (openSync.getIncrementalMode()) {
184 try {
185 String command =
186 System.getProperty("xindice.home")+
187 System.getProperty("file.separator")+
188 "bin"+System.getProperty("file.separator")+
189 "xindiceadmin shutdown -c /db ";
190 OpenSync.executeCmd(command);
191 } catch (IOException ioex) {
192 }
193 }
194 System.exit(0);
195 }
196 }
197 /***
198 * Help | About action performed
199 *
200 * @param e
201 */
202 public void menuHelpAbout_actionPerformed(ActionEvent e) {
203 try {
204 AboutBox dlg = new AboutBox(this);
205 Dimension dlgSize = dlg.getPreferredSize();
206 Dimension frmSize = getSize();
207 Point loc = getLocation();
208 dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
209 dlg.setModal(true);
210 dlg.show();
211 }
212 catch (Exception ex) {
213 ex.printStackTrace();
214 }
215 }
216 /***
217 * @param e
218 */
219 public void menuFileOpen_actionPerformed(ActionEvent e) {
220 try {
221 OpenSync.getInstance().loadState();
222 }
223 catch (Exception ex) {
224 ex.printStackTrace();
225 }
226 }
227 /***
228 * @param e
229 */
230 public void menuFileOpenLog_actionPerformed(ActionEvent e) {
231 try {
232 JFileChooser chooser = new JFileChooser(
233 OpenSync.getInstance().getDirectoryPath("log",true)
234 );
235 chooser.showOpenDialog(AppPnl.getInstance());
236 chooser.setMultiSelectionEnabled(false);
237 File file = chooser.getSelectedFile();
238 if(file != null){
239 JFrame frm = new LogFileFrm(file.getPath());
240 frm.setVisible(true);
241 }
242 OpenSync.getInstance().loadState();
243 }
244 catch (Exception ex) {
245 ex.printStackTrace();
246 }
247 }
248 /***
249 * @param e
250 */
251 public void menuFileSave_actionPerformed(ActionEvent e) {
252 OpenSync.getInstance().saveState();
253 }
254 /***
255 * @param e
256 */
257 public void btHelp_actionPerformed(ActionEvent e) {
258 JFrame frm = new HelpFrm();
259 frm.setVisible(true);
260 }
261 /***
262 * @param e
263 */
264 public void menuFileLoadConfig_actionPerformed(ActionEvent e) {
265 int res = JOptionPane.showOptionDialog(AppPnl.getInstance(),
266 I18n.getInstance().format(
267 "gui.confirm.load-config",null
268 ),I18n.getInstance().get("gui.product"),
269 JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,
270 new String[]{I18n.getInstance().get("common.yes"),I18n.getInstance().get("common.no")},
271 I18n.getInstance().get("common.no")
272 );
273 if(res == JOptionPane.YES_OPTION){
274 OpenSync.restart();
275 };
276 }
277
278 /***
279 * Overridden so we can exit when window is closed
280 *
281 * @param e
282 */
283 protected void processWindowEvent(WindowEvent e) {
284 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
285 int res = JOptionPane.showOptionDialog(AppPnl.getInstance(),
286 I18n.getInstance().format(
287 "gui.confirm.exit",null
288 ),I18n.getInstance().get("gui.product"),
289 JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,
290 new String[]{I18n.getInstance().get("common.yes"),I18n.getInstance().get("common.no")},
291 I18n.getInstance().get("common.no")
292 );
293 if(res == JOptionPane.YES_OPTION){
294 super.processWindowEvent(e);
295 System.exit(0);
296 }
297 }
298 }
299 }