3e538ca0633c84a3c1277e409fb784e29b0420df
[RRRRHHHH_Code] / ruralHouses / src / gui / StartWindow.java
1 package gui;
2
3 /**
4  * @author willCodeForFood
5  */
6 import exceptions.DB4oManagerCreationException;
7 import gui.BookRuralHouseGUI;
8
9 import javax.swing.*;
10
11 import configuration.ConfigXML;
12 //import businessLogic.FacadeImplementation;
13 import businessLogic.ApplicationFacadeInterface;
14 import businessLogic.FacadeImplementation;
15
16 import java.rmi.*;
17 import java.awt.Color;
18 import java.awt.Font;
19 import java.awt.Rectangle;
20 import java.awt.Dimension;
21 import java.awt.BorderLayout;
22 import java.awt.FlowLayout;
23 import java.awt.GridLayout;
24 import java.awt.GridBagLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.event.WindowAdapter;
27 import java.awt.event.WindowEvent;
28
29
30 public class StartWindow extends JFrame {
31         
32         private static final long serialVersionUID = 1L;
33
34         private JPanel jContentPane = null;
35         private JButton boton1 = null;
36         private JButton boton2 = null;
37         private JButton boton3 = null;
38         private static configuration.ConfigXML c;
39
40         public static ApplicationFacadeInterface facadeInterface;
41         private JLabel lblNewLabel;
42         
43         public static void main(String[] args) {
44
45                 StartWindow a = new StartWindow();
46                 a.setVisible(true);
47                 
48                 try {
49
50                         c=ConfigXML.getInstance();
51
52                         System.setProperty("java.security.policy", c.getJavaPolicyPath());
53                                                 
54                         System.setSecurityManager(new RMISecurityManager());
55
56                         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
57
58                         c=configuration.ConfigXML.getInstance();
59                         if (c.isBusinessLogicLocal())
60                                 facadeInterface=new FacadeImplementation();
61                         else {
62                                 
63                                 final String businessLogicNode = c.getBusinessLogicNode();
64                                 // Remote service name
65                                 String serviceName = "/"+c.getServiceRMI();
66                                 // RMI server port number
67                                 int portNumber = Integer.parseInt(c.getPortRMI());
68                                 // RMI server host IP IP 
69                                 facadeInterface = (ApplicationFacadeInterface) Naming.lookup("rmi://"
70                                         + businessLogicNode + ":" + portNumber + serviceName);
71                         } 
72
73                 } catch (java.rmi.ConnectException e) {
74                         a.lblNewLabel.setText("No business logic: Run BusinessLogicServer first!!");
75                         a.lblNewLabel.setForeground(Color.RED);
76                         System.out.println("Error in StartWindow: "+e.toString());
77                 } catch (java.rmi.NotBoundException e) {
78                         a.lblNewLabel.setText("No business logic: Maybe problems running BusinessLogicServer");
79                         a.lblNewLabel.setForeground(Color.RED);
80                         System.out.println("Error in StartWindow: "+e.toString());
81                 } catch (com.db4o.ext.DatabaseFileLockedException e) {
82                         a.lblNewLabel.setText("Database locked: Do not run BusinessLogicServer or BusinessLogicServer!!");
83                         a.lblNewLabel.setForeground(Color.RED);         
84                         System.out.println("Error in StartWindow: "+e.toString());
85                 } catch (Exception e) {
86                         a.lblNewLabel.setText("Error: "+e.toString());
87                         a.lblNewLabel.setForeground(Color.RED);         
88                         System.out.println("Error in StartWindow: "+e.toString());
89                 }
90                 //a.pack();
91
92                 
93
94         }
95         /**
96          * This is the default constructor
97          */
98
99         public StartWindow() {
100                 super();
101
102                 addWindowListener(new WindowAdapter() {
103                         @Override
104                         public void windowClosing(WindowEvent e) {
105                                 ApplicationFacadeInterface facade=StartWindow.facadeInterface;
106                                 try {
107                                         if (c.isBusinessLogicLocal()) facade.close();
108                                 } catch (Exception e1) {
109                                         // TODO Auto-generated catch block
110                                         System.out.println("Error: "+e1.toString()+" , probably problems with Business Logic or Database");
111                                 }
112                                 System.exit(1);
113                         }
114                 });
115                 initialize();
116                 //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
117         }
118         public static ApplicationFacadeInterface getBusinessLogic(){
119                 return facadeInterface;
120         }
121         
122
123         /**
124          * This method initializes this
125          * 
126          * @return void
127          */
128         private void initialize() {
129                 // this.setSize(271, 295);
130                 this.setSize(495, 290);
131                 this.setContentPane(getJContentPane());
132                 this.setTitle("Rural Houses");
133         }
134
135         /**
136          * This method initializes jContentPane
137          * 
138          * @return javax.swing.JPanel
139          */
140         private JPanel getJContentPane() {
141                 if (jContentPane == null) {
142                         GridLayout gridLayout = new GridLayout();
143                         gridLayout.setRows(4);
144                         gridLayout.setColumns(1);
145                         jContentPane = new JPanel();
146                         jContentPane.setLayout(gridLayout);
147                         jContentPane.add(getLblNewLabel());
148                         jContentPane.add(getBoton2(), null);
149                         jContentPane.add(getBoton3(), null);
150                         jContentPane.add(getBoton1(), null);
151                 }
152                 return jContentPane;
153         }
154
155         /**
156          * This method initializes boton1
157          * 
158          * @return javax.swing.JButton
159          */
160         private JButton getBoton1() {
161                 if (boton1 == null) {
162                         boton1 = new JButton();
163                         boton1.setText("Book rural house");
164                         boton1.addActionListener(new java.awt.event.ActionListener() {
165                                 public void actionPerformed(java.awt.event.ActionEvent e) {
166                                         // C?digo cedido por la univerdad
167                                         JFrame a = new BookRuralHouseGUI();
168                                         a.setVisible(true);
169                                 }
170                         });
171                 }
172                 return boton1;
173         }
174
175         /**
176          * This method initializes boton2
177          * 
178          * @return javax.swing.JButton
179          */
180         private JButton getBoton2() {
181                 if (boton2 == null) {
182                         boton2 = new JButton();
183                         boton2.setText("Login");
184                         boton2.addActionListener(new java.awt.event.ActionListener() {
185                                 public void actionPerformed(java.awt.event.ActionEvent e) {
186                                         // C?digo cedido por la universidad
187                                         JFrame a = new LoginGUI();
188                                         a.setVisible(true);
189                                 }
190                         });
191                 }
192                 return boton2;
193         }
194         
195         /**
196          * This method initializes boton3
197          * 
198          * @return javax.swing.JButton
199          */
200         private JButton getBoton3() {
201                 if (boton3 == null) {
202                         boton3 = new JButton();
203                         boton3.setText("Query availability");
204                         boton3.addActionListener(new java.awt.event.ActionListener() {
205                                 public void actionPerformed(java.awt.event.ActionEvent e) {
206                                         // C?digo cedido por la universidad
207                                         //JFrame a = new QueryAvailabilityWindow();
208                                         JFrame a = new QueryAvailabilityGUI();
209
210                                         a.setVisible(true);
211                                 }
212                         });
213                 }
214                 return boton3;
215         }
216         
217
218         private JLabel getLblNewLabel() {
219                 if (lblNewLabel == null) {
220                         lblNewLabel = new JLabel("Select option:");
221                         lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
222                         lblNewLabel.setForeground(Color.BLACK);
223                         lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
224                 }
225                 return lblNewLabel;
226         }
227
228 } // @jve:decl-index=0:visual-constraint="0,0"
229