View Javadoc

1   /*
2    * Copyright (c) 1998-2004 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.experiment.gui;
20  
21  import java.awt.Dimension;
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Toolkit;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.io.File;
28  import java.util.LinkedList;
29  
30  import javax.swing.BorderFactory;
31  import javax.swing.Box;
32  import javax.swing.BoxLayout;
33  import javax.swing.JButton;
34  import javax.swing.JFrame;
35  import javax.swing.JPanel;
36  import javax.swing.SwingUtilities;
37  import javax.swing.UIManager;
38  import javax.swing.border.BevelBorder;
39  import javax.swing.border.EtchedBorder;
40  
41  import jgroup.experiment.gui.views.helpers.FileList;
42  
43  /**
44   * @author Bjarte Svaeren
45   */
46  public class GUIapp {
47    ////////////////////////////////////////////////////////////////////////////////////////////
48    // Static data fields
49    ////////////////////////////////////////////////////////////////////////////////////////////
50  
51    // Array containing the views and list containing the view buttons.
52    // These two are always in sync; that is, the first button in buttonList
53    // belongs to the object at GUIview[0]. The second button belongs to
54    // the object at GUIview[1] etc.
55    static GUIview[] views;
56    static LinkedList buttonList;
57  
58    // Get the size of the screen  
59    static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
60  
61  
62    
63    ////////////////////////////////////////////////////////////////////////////////////////////
64    // GUI fields
65    ////////////////////////////////////////////////////////////////////////////////////////////
66  
67    // The top application frame:
68    JFrame topFrame;
69  
70    // The components in the left panel:
71    Box leftBox;
72  
73    // The component in the (right) main panel
74    JPanel mainPanel;
75  
76    // Handle to the current view.
77    GUIview currentView = null;
78  
79    ////////////////////////////////////////////////////////////////////////////////////////////
80    // Constructor
81    ////////////////////////////////////////////////////////////////////////////////////////////
82  
83    GUIapp() throws Exception {
84      // Set Look and Feel
85  //    JFrame.setDefaultLookAndFeelDecorated(true);
86  
87      // Create the initial contents of the GUI
88      leftBox = new Box(BoxLayout.Y_AXIS);
89      leftBox.setBorder(BorderFactory.createCompoundBorder(
90                        BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
91                        BorderFactory.createCompoundBorder(
92                        BorderFactory.createTitledBorder("Views:"),
93                        BorderFactory.createEmptyBorder(5, 5, 5, 5))));
94  
95      mainPanel = new JPanel(new GridBagLayout()); // Empty panel
96  
97      initViews();
98      initTopFrame();
99  
100     // Show the first view in the list by default
101     JButton button = views[0].getView();    
102     button.doClick();
103   }
104 
105   ////////////////////////////////////////////////////////////////////////////////////////////
106   // main()
107   ////////////////////////////////////////////////////////////////////////////////////////////
108 
109   public static void main(String[] args) throws Exception {
110     findViews();
111 
112     // Make sure the GUI executes on the event-dispatching thread
113     javax.swing.SwingUtilities.invokeLater(new Runnable() {
114       public void run() {
115         try {
116           new GUIapp();
117         } catch (Exception e) {
118           e.printStackTrace();
119         }
120       }
121     });    
122   }
123 
124 
125 
126   ////////////////////////////////////////////////////////////////////////////////////////////
127   // Static utility methods
128   ////////////////////////////////////////////////////////////////////////////////////////////
129 
130   public static Dimension getScreenSize()
131   {
132     return screenSize;
133   }
134   
135 
136   ////////////////////////////////////////////////////////////////////////////////////////////
137   // Private methods
138   ////////////////////////////////////////////////////////////////////////////////////////////
139 
140   private static void findViews() throws Exception {
141 //FIXME this should not be dependent on having the source code available
142     File viewDir = new File("jgroup-experiment/src/main/java/jgroup/experiment/gui/views");
143     if (!viewDir.exists())
144       throw new Exception("The GUI views directory could not be found.");
145 
146     String[] viewFiles = new FileList(viewDir, ".java").getFileList();
147     views = new GUIview[viewFiles.length];
148 
149     for (int i = 0; i < viewFiles.length; i++) {
150       // Get the fileName, add the package name and strip the '.java' ending
151       String fileName = viewFiles[i];
152       String className =
153         "jgroup.experiment.gui.views."
154           + fileName.substring(0, fileName.length() - 5);
155 
156       // Create an instance of the view and place it in viewClasses[]
157       views[i] = (GUIview) Class.forName(className).newInstance();
158     }
159   }
160 
161   private void initViews() {
162     JButton button;
163     buttonList = new LinkedList();
164 
165     // Get the view buttons from the GUIviews
166     for (int i = 0; i < views.length; i++) {
167       button = views[i].getView();
168       button.setBorder(BorderFactory.createCompoundBorder(
169                        BorderFactory.createBevelBorder(BevelBorder.RAISED),
170                        BorderFactory.createEmptyBorder(5, 5, 5, 5)));
171       buttonList.add(button);
172 
173       // Add action listeners to the buttons
174       button.addActionListener(new ActionListener() {
175         public void actionPerformed(ActionEvent e) {
176 
177 //          // Notify the user that the view is loading
178 //          JFrame    topFrame = (JFrame) mainPanel.getTopLevelAncestor();
179 //          JWindow   window   = new JWindow(topFrame);
180 //          Container pane     = window.getContentPane();
181 //          JLabel    label    = new JLabel("Loading view...");
182 //          label.setBorder(BorderFactory.createCompoundBorder(
183 //                          BorderFactory.createLineBorder(Color.BLACK, 4),
184 //                          BorderFactory.createEmptyBorder(40, 40, 40, 40)));  
185 //          pane.setLayout(new BorderLayout());
186 //          pane.add(label, BorderLayout.CENTER);
187 //          window.pack();
188 //          int xpos = (topFrame.getX() + topFrame.getX() + topFrame.getWidth())  / 2 - 50;
189 //          int ypos = (topFrame.getY() + topFrame.getY() + topFrame.getHeight()) / 2 - 50;
190 //          window.setLocation(xpos, ypos);
191 //          window.setVisible(true);
192 //          topFrame.setEnabled(false);
193 
194           int index = buttonList.indexOf(e.getSource());
195           mainPanel.removeAll();
196           GridBagConstraints gbc = new GridBagConstraints();
197           gbc.fill = GridBagConstraints.BOTH;
198           gbc.anchor = GridBagConstraints.FIRST_LINE_START;
199           gbc.weightx = 1.0;
200           gbc.weighty = 1.0;
201           gbc.gridwidth  = GridBagConstraints.REMAINDER;
202           gbc.gridheight  = GridBagConstraints.REMAINDER;
203           gbc.gridx      = 0;
204           gbc.gridy      = 0;
205           mainPanel.add(views[index].makeMainPanel(), gbc);
206           mainPanel.revalidate();
207 
208 //          window.setVisible(false);
209 //          topFrame.setEnabled(true);
210         }
211       });
212 
213       leftBox.add(button);
214     }
215   }
216 
217   private void initTopFrame() {
218     topFrame = new JFrame("Jgroup GUI for experiments");
219     topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
220 
221     topFrame.getContentPane().setLayout(new GridBagLayout());
222     GridBagConstraints gbc = new GridBagConstraints();
223     
224     gbc.fill       = GridBagConstraints.VERTICAL;
225     gbc.anchor     = GridBagConstraints.LINE_START;
226     gbc.gridwidth  = GridBagConstraints.RELATIVE;
227     gbc.gridheight = GridBagConstraints.REMAINDER;
228     gbc.gridx      = 0;
229     gbc.gridy      = 0;
230     gbc.weightx    = 0.1;
231     gbc.weighty    = 0.1;
232     topFrame.getContentPane().add(leftBox, gbc);
233     
234     gbc.fill       = GridBagConstraints.BOTH;
235     gbc.anchor     = GridBagConstraints.FIRST_LINE_START;
236     gbc.gridwidth  = GridBagConstraints.REMAINDER;
237     gbc.gridx      = GridBagConstraints.RELATIVE;
238     gbc.weightx    = 1.0;
239     gbc.weighty    = 0.1;
240     topFrame.getContentPane().add(mainPanel, gbc);
241 
242     topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
243 
244     try {
245       UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
246     } catch (Exception e) {
247       SwingUtilities.updateComponentTreeUI(topFrame);
248     }
249 
250     topFrame.pack();        
251     topFrame.setSize( (int) (screenSize.width/1.3), (int) (screenSize.height/1.4) );
252     topFrame.setVisible(true);
253   }
254 }