View Javadoc

1   package org.telscenter.pas.authortool.main;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Container;
5   import java.awt.EventQueue;
6   import java.awt.GridBagConstraints;
7   import java.awt.GridBagLayout;
8   import java.awt.Toolkit;
9   import java.io.File;
10  import java.io.IOException;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.JFileChooser;
14  import javax.swing.JFrame;
15  import javax.swing.JPanel;
16  import javax.swing.JToolBar;
17  import javax.swing.UIManager;
18  import javax.swing.border.MatteBorder;
19  import javax.swing.plaf.metal.MetalLookAndFeel;
20  import javax.swing.plaf.metal.OceanTheme;
21  
22  import net.sf.sail.core.beans.SessionContext;
23  import net.sf.sail.core.util.FileUtils;
24  
25  import org.telscenter.pas.authortool.buttonbar.ButtonBarProvider;
26  import org.telscenter.pas.authortool.context.CurnitAuthoringContext;
27  import org.telscenter.pas.authortool.context.IAuthoringSessionManager;
28  import org.telscenter.pas.authortool.statusbar.StatusBarProvider;
29  import org.telscenter.pas.authortool.toolbar.ToolBarProvider;
30  import org.telscenter.pas.ui.browser.BrowserFactoryRegistry;
31  import org.telscenter.pas.ui.browser.WebRendererFactory;
32  
33  import com.l2fprod.common.swing.StatusBar;
34  
35  public class AuthoringShellMain {
36  
37  	private JFrame frame;
38  
39  	private CurnitAuthoringContext curnitAuthoringContext;
40  
41  	private ToolBarProvider tbp;
42  
43  	public JFrame getMainFrame() {
44  		return frame;
45  	}
46  
47  	public AuthoringShellMain(
48  			final CurnitAuthoringContext curnitAuthoringContext) {
49  		// register webrender as the browser, this should be refactored, but
50  		// this seems the best place for it right now
51  
52  		curnitAuthoringContext.setMainAuthoringFrame(this.getMainFrame());
53  		WebRendererFactory factory = new WebRendererFactory();
54  		BrowserFactoryRegistry.registerFactory(factory, 3);
55  
56  		frame = new JFrame("Pas Authoring Shell");
57  		this.curnitAuthoringContext = curnitAuthoringContext;
58  
59  		// add menu
60  		frame.setJMenuBar(curnitAuthoringContext.getMenuProvider().getMenuBar());
61  		final Container contentPane = frame.getContentPane();
62  
63  		final JPanel mainPanel = new JPanel();
64  		final MatteBorder matteBorder = BorderFactory.createMatteBorder(4, 4,
65  				4, 4, mainPanel.getBackground());
66  		mainPanel.setBorder(matteBorder);
67  
68  		final BorderLayout borderLayout = new BorderLayout();
69  		borderLayout.setHgap(4);
70  		borderLayout.setVgap(4);
71  
72  		mainPanel.setLayout(borderLayout);
73  		contentPane.add(mainPanel);
74  		// create the button bar
75  		final JPanel buttonPanel = createButtonBar();
76  		buttonPanel.setOpaque(false);
77  		mainPanel.setName("mainpanel");
78  		mainPanel.add(createToolBar(), BorderLayout.NORTH);
79  		// mainPanel.add(buttonPanel, BorderLayout.WEST);
80  
81  		// create the content pane
82  		mainPanel.add(curnitAuthoringContext.getCardProvider().getCardPanel(),
83  				BorderLayout.CENTER);
84  
85  		// create the status bar
86  		final StatusBarProvider statusBarProvider = StatusBarProvider
87  				.getInstance();
88  		curnitAuthoringContext.setStatusBarProvider(statusBarProvider);
89  		final StatusBar statusBar = statusBarProvider.getStatusBar();
90  		mainPanel.add(statusBar, BorderLayout.SOUTH);
91  
92  		// finish up configure the frame
93  		configureJFrame(frame);
94  	}
95  
96  	protected JToolBar createToolBar() {
97  
98  		tbp = new ToolBarProvider(curnitAuthoringContext);
99  		return tbp.getToolbar();
100 	}
101 
102 	private JPanel createButtonBar() {
103 		final JPanel pane = new JPanel();
104 		pane.setOpaque(false);
105 		final GridBagLayout gridbag = new GridBagLayout();
106 		final GridBagConstraints c = new GridBagConstraints();
107 		c.weighty = 1; // reset to the default
108 		c.weightx = 1;
109 		c.fill = GridBagConstraints.BOTH;
110 		c.gridwidth = GridBagConstraints.REMAINDER; // end row
111 		c.gridheight = 1;
112 
113 		pane.setLayout(gridbag);
114 
115 		// create the buttonbar
116 		final ButtonBarProvider bbProvider = new ButtonBarProvider(
117 				curnitAuthoringContext);
118 
119 		pane.add(bbProvider.getButtonBar(), c);
120 
121 		return pane;
122 	}
123 
124 	private void configureJFrame(final JFrame frame) {
125 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
126 		frame.pack();
127 		frame.setSize(800, 600);
128 		frame.setLocation(100, 100);
129 		frame.setVisible(true);
130 	}
131 
132 	public static void main(final String[] args) throws IOException {
133 
134 		Toolkit.getDefaultToolkit().setDynamicLayout(true);
135 		Runnable runner = new Runnable() {
136 
137 			public void run() {
138 				try {
139 					//set the look and feel for all platforms
140 					UIManager.setLookAndFeel(new MetalLookAndFeel());
141 				
142 					//sets the current theme
143 					MetalLookAndFeel.setCurrentTheme(new OceanTheme());
144 					
145 					//antiliasing the fonts globally
146 					System.setProperty( "swing.aatext", "true" );
147 				} catch (Exception e) {
148 				}
149 
150 				File toOpen = null;
151 				if (args.length == 0) {
152 					JFileChooser fileChooser = new JFileChooser();
153 					fileChooser.setFileFilter(FileUtils.getCurnitFileFilter());
154 					int i = fileChooser.showOpenDialog(null);
155 					if (i == JFileChooser.APPROVE_OPTION) {
156 						toOpen = fileChooser.getSelectedFile();
157 					}
158 				} else {
159 					toOpen = new File(args[0]);
160 				}
161 				if (toOpen == null)
162 					System.exit(1);
163 				
164 				SessionContext.exitOnClose = false;
165 				
166 				IAuthoringSessionManager sessionManager;
167 				try {
168 					sessionManager = new SimpleFileSessionManager(
169 							toOpen);
170 					final CurnitAuthoringContext curnitAuthoringContext = new CurnitAuthoringContext(
171 							sessionManager);
172 					new AuthoringShellMain(curnitAuthoringContext);
173 				} catch (IOException e) {
174 					// TODO Auto-generated catch block
175 					e.printStackTrace();
176 				}//try
177 			}// run
178 		};
179 		EventQueue.invokeLater(runner);
180 //		System.setProperty("apple.laf.useScreenMenuBar", "true");
181 //		System.setProperty("apple.laf.mrj.application.apple.menu.about.name", "SAIL Authoring Tool");
182 	}
183 
184 }