View Javadoc
1   package org.openflexo.gina.swing.editor;
2   
3   import java.awt.Window;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.io.File;
7   import java.util.List;
8   import java.util.Vector;
9   import java.util.prefs.PreferenceChangeEvent;
10  import java.util.prefs.PreferenceChangeListener;
11  
12  import javax.swing.JCheckBoxMenuItem;
13  import javax.swing.JFrame;
14  import javax.swing.JMenu;
15  import javax.swing.JMenuBar;
16  import javax.swing.JMenuItem;
17  import javax.swing.SwingUtilities;
18  import javax.swing.UIManager;
19  import javax.swing.UIManager.LookAndFeelInfo;
20  import javax.swing.UnsupportedLookAndFeelException;
21  
22  import org.openflexo.gina.swing.utils.JFIBPreferences;
23  import org.openflexo.gina.swing.utils.localization.LocalizedEditor;
24  import org.openflexo.gina.swing.utils.logging.FlexoLoggingViewer;
25  import org.openflexo.localization.Language;
26  import org.openflexo.logging.FlexoLoggingManager;
27  
28  public class FIBEditorMenuBar extends JMenuBar implements PreferenceChangeListener {
29  
30  	private final FIBEditor fibEditor;
31  
32  	private final JMenu fileMenu;
33  	private final JMenu editMenu;
34  	private final JMenu toolsMenu;
35  	private final JMenu helpMenu;
36  
37  	private final JMenuItem newItem;
38  	private final JMenuItem loadItem;
39  	private final JMenuItem saveItem;
40  	private final JMenuItem saveAsItem;
41  	private final JMenuItem closeItem;
42  	private final JMenuItem quitItem;
43  
44  	private final JMenu languagesItem;
45  	// private final JMenuItem inspectItem;
46  	private final JMenuItem logsItem;
47  	private final JMenuItem localizedItem;
48  	private final JMenuItem displayFileItem;
49  	private final JMenuItem testInterfaceItem;
50  	private final JMenuItem componentLocalizationItem;
51  	private final JMenuItem componentValidationItem;
52  
53  	private final JMenu openRecent;
54  	// private final JMenuItem showPaletteItem;
55  
56  	private JFrame frame;
57  
58  	public FIBEditorMenuBar(FIBEditor aFIBEditor, JFrame aFrame) {
59  		this.fibEditor = aFIBEditor;
60  		this.frame = aFrame;
61  		fileMenu = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("file"));
62  		editMenu = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("edit"));
63  		toolsMenu = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("tools"));
64  		helpMenu = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("help"));
65  
66  		newItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("new_component"));
67  		newItem.addActionListener(new ActionListener() {
68  			@Override
69  			public void actionPerformed(ActionEvent e) {
70  				FIBEditorMenuBar.this.fibEditor.newFIB(frame);
71  			}
72  		});
73  
74  		loadItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("open_component"));
75  		loadItem.addActionListener(new ActionListener() {
76  			@Override
77  			public void actionPerformed(ActionEvent e) {
78  				FIBEditorMenuBar.this.fibEditor.loadFIB(frame);
79  			}
80  		});
81  
82  		openRecent = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("open_recent"));
83  		JFIBPreferences.addPreferenceChangeListener(this);
84  		updateOpenRecent();
85  		saveItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("save_component"));
86  		saveItem.addActionListener(new ActionListener() {
87  			@Override
88  			public void actionPerformed(ActionEvent e) {
89  				if (fibEditor.getActiveEditedComponent() != null) {
90  					fibEditor.saveFIB(fibEditor.getActiveEditedComponent(), frame);
91  				}
92  			}
93  		});
94  
95  		saveAsItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("save_component_as"));
96  		saveAsItem.addActionListener(new ActionListener() {
97  			@Override
98  			public void actionPerformed(ActionEvent e) {
99  				if (fibEditor.getActiveEditedComponent() != null) {
100 					fibEditor.saveFIBAs(fibEditor.getActiveEditedComponent(), frame);
101 				}
102 			}
103 		});
104 
105 		closeItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("close"));
106 		closeItem.addActionListener(new ActionListener() {
107 			@Override
108 			public void actionPerformed(ActionEvent e) {
109 				if (fibEditor.getActiveEditedComponent() != null) {
110 					fibEditor.closeFIB(fibEditor.getActiveEditedComponent(), frame);
111 				}
112 			}
113 		});
114 
115 		quitItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("quit"));
116 		quitItem.addActionListener(new ActionListener() {
117 			@Override
118 			public void actionPerformed(ActionEvent e) {
119 				fibEditor.quit();
120 			}
121 		});
122 		testInterfaceItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("test_component"));
123 		testInterfaceItem.addActionListener(new ActionListener() {
124 			@Override
125 			public void actionPerformed(ActionEvent e) {
126 				if (fibEditor.getActiveEditedComponent() != null) {
127 					fibEditor.testFIB(fibEditor.getActiveEditedComponent(), frame);
128 				}
129 			}
130 		});
131 
132 		componentLocalizationItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("component_localization"));
133 		componentLocalizationItem.addActionListener(new ActionListener() {
134 			@Override
135 			public void actionPerformed(ActionEvent e) {
136 				if (fibEditor.getActiveEditedComponent() != null) {
137 					fibEditor.localizeFIB(fibEditor.getActiveEditedComponent(), frame);
138 				}
139 			}
140 		});
141 
142 		final JMenu lafsItem = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("look_and_feel"));
143 		final Vector<LAFMenuItem> lafsItems = new Vector<>();
144 		for (final LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
145 			LAFMenuItem lafItem = new LAFMenuItem(laf);
146 			lafsItems.add(lafItem);
147 			lafItem.addActionListener(new ActionListener() {
148 				@Override
149 				public void actionPerformed(ActionEvent e) {
150 					try {
151 						UIManager.setLookAndFeel(laf.getClassName());
152 					} catch (ClassNotFoundException e1) {
153 						e1.printStackTrace();
154 					} catch (InstantiationException e1) {
155 						e1.printStackTrace();
156 					} catch (IllegalAccessException e1) {
157 						e1.printStackTrace();
158 					} catch (UnsupportedLookAndFeelException e1) {
159 						e1.printStackTrace();
160 					}
161 					Window windows[] = frame.getOwnedWindows();
162 					for (int j = 0; j < windows.length; j++) {
163 						SwingUtilities.updateComponentTreeUI(windows[j]);
164 					}
165 					SwingUtilities.updateComponentTreeUI(frame);
166 					for (LAFMenuItem me : lafsItems) {
167 						me.updateState();
168 					}
169 				}
170 			});
171 			lafsItem.add(lafItem);
172 		}
173 
174 		fileMenu.add(newItem);
175 		fileMenu.add(loadItem);
176 		fileMenu.add(openRecent);
177 		fileMenu.add(saveItem);
178 		fileMenu.add(saveAsItem);
179 		fileMenu.add(closeItem);
180 		fileMenu.addSeparator();
181 		fileMenu.add(testInterfaceItem);
182 		fileMenu.add(componentLocalizationItem);
183 
184 		fileMenu.add(lafsItem);
185 		languagesItem = new JMenu(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("switch_to_language"));
186 		for (Language lang : Language.availableValues()) {
187 			JMenuItem languageItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey(lang.getName()));
188 			final Language language = lang;
189 			languageItem.addActionListener(new ActionListener() {
190 				@Override
191 				public void actionPerformed(ActionEvent e) {
192 					fibEditor.switchToLanguage(language);
193 				}
194 			});
195 			languagesItem.add(languageItem);
196 		}
197 		fileMenu.add(languagesItem);
198 		fileMenu.addSeparator();
199 		fileMenu.add(quitItem);
200 
201 		/*inspectItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("inspect"));
202 		inspectItem.addActionListener(new ActionListener() {
203 			@Override
204 			public void actionPerformed(ActionEvent e) {
205 				FIBEditorMenuBar.this.fibEditor.getInspector().setVisible(true);
206 			}
207 		});
208 		inspectItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
209 				ToolBox.getPLATFORM() != ToolBox.MACOS ? InputEvent.CTRL_MASK : InputEvent.META_MASK));*/
210 
211 		/*showPaletteItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("show_palette"));
212 		showPaletteItem.addActionListener(new ActionListener() {
213 		
214 			@Override
215 			public void actionPerformed(ActionEvent e) {
216 				FIBEditorMenuBar.this.fibEditor.getPalettes().setVisible(true);
217 			}
218 		});*/
219 		logsItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("logs"));
220 		logsItem.addActionListener(new ActionListener() {
221 			@Override
222 			public void actionPerformed(ActionEvent e) {
223 				FlexoLoggingViewer.showLoggingViewer(FlexoLoggingManager.instance(), FIBEditor.APP_FIB_LIBRARY, frame);
224 			}
225 		});
226 
227 		localizedItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("localized_editor"));
228 		localizedItem.addActionListener(new ActionListener() {
229 			@Override
230 			public void actionPerformed(ActionEvent e) {
231 				if (FIBEditorMenuBar.this.fibEditor.localizedEditor == null) {
232 					FIBEditorMenuBar.this.fibEditor.localizedEditor = new LocalizedEditor(frame, "localized_editor",
233 							FIBEditor.EDITOR_LOCALIZATION, FIBEditor.EDITOR_LOCALIZATION, true, false);
234 				}
235 				FIBEditorMenuBar.this.fibEditor.localizedEditor.setVisible(true);
236 			}
237 		});
238 
239 		displayFileItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("display_file"));
240 		displayFileItem.addActionListener(new ActionListener() {
241 			@Override
242 			public void actionPerformed(ActionEvent e) {
243 				if (fibEditor.getActiveEditedComponent() != null) {
244 					FIBEditor.logger.info("Getting this " + fibEditor.getActiveEditedComponent().getFactory()
245 							.stringRepresentation(fibEditor.getActiveEditedComponent().getFIBComponent()));
246 				}
247 			}
248 		});
249 
250 		componentValidationItem = new JMenuItem(FIBEditor.EDITOR_LOCALIZATION.localizedForKey("validate_component"));
251 		componentValidationItem.addActionListener(new ActionListener() {
252 			@Override
253 			public void actionPerformed(ActionEvent e) {
254 				if (fibEditor.getActiveEditedComponent() != null) {
255 					fibEditor.validateFIB(fibEditor.getActiveEditedComponent(), frame);
256 				}
257 			}
258 		});
259 
260 		// toolsMenu.add(inspectItem);
261 		// toolsMenu.add(showPaletteItem);
262 		toolsMenu.add(logsItem);
263 		toolsMenu.add(localizedItem);
264 		toolsMenu.add(displayFileItem);
265 		toolsMenu.addSeparator();
266 		toolsMenu.add(componentValidationItem);
267 
268 		add(fileMenu);
269 		add(editMenu);
270 		add(toolsMenu);
271 		add(helpMenu);
272 
273 		frame.setJMenuBar(this);
274 	}
275 
276 	private boolean willUpdate = false;
277 
278 	@Override
279 	public void preferenceChange(PreferenceChangeEvent evt) {
280 		if (evt.getKey().startsWith(JFIBPreferences.LAST_FILE)) {
281 			if (willUpdate) {
282 				return;
283 			}
284 			else {
285 				willUpdate = true;
286 			}
287 			SwingUtilities.invokeLater(new Runnable() {
288 				@Override
289 				public void run() {
290 					willUpdate = false;
291 					updateOpenRecent();
292 				}
293 			});
294 		}
295 	}
296 
297 	private void updateOpenRecent() {
298 		openRecent.removeAll();
299 		List<File> files = JFIBPreferences.getLastFiles();
300 		openRecent.setEnabled(files.size() != 0);
301 		for (final File file : files) {
302 			JMenuItem item = new JMenuItem(file.getName());
303 			item.setToolTipText(file.getAbsolutePath());
304 			item.addActionListener(new ActionListener() {
305 				@Override
306 				public void actionPerformed(ActionEvent e) {
307 					FIBEditorMenuBar.this.fibEditor.loadFIB(file, frame);
308 				}
309 			});
310 			openRecent.add(item);
311 		}
312 	}
313 
314 	class LAFMenuItem extends JCheckBoxMenuItem {
315 		private final LookAndFeelInfo laf;
316 
317 		public LAFMenuItem(LookAndFeelInfo laf) {
318 			super(laf.getName(), UIManager.getLookAndFeel().getClass().getName().equals(laf.getClassName()));
319 			this.laf = laf;
320 		}
321 
322 		public void updateState() {
323 			setState(UIManager.getLookAndFeel().getClass().getName().equals(laf.getClassName()));
324 		}
325 	}
326 
327 }