1 /*
2  * Created on 18-dic-2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package org.herac.tuxguitar.gui.items;
8 
9 import java.io.File;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.layout.FormAttachment;
17 import org.eclipse.swt.layout.FormData;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.CoolBar;
20 import org.eclipse.swt.widgets.CoolItem;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.swt.widgets.Menu;
24 import org.eclipse.swt.widgets.MenuItem;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.ToolBar;
27 import org.herac.tuxguitar.gui.TuxGuitar;
28 import org.herac.tuxguitar.gui.editors.TGUpdateListener;
29 import org.herac.tuxguitar.gui.items.menu.BeatMenuItem;
30 import org.herac.tuxguitar.gui.items.menu.CompositionMenuItem;
31 import org.herac.tuxguitar.gui.items.menu.EditMenuItem;
32 import org.herac.tuxguitar.gui.items.menu.FileMenuItem;
33 import org.herac.tuxguitar.gui.items.menu.HelpMenuItem;
34 import org.herac.tuxguitar.gui.items.menu.MarkerMenuItem;
35 import org.herac.tuxguitar.gui.items.menu.MeasureMenuItem;
36 import org.herac.tuxguitar.gui.items.menu.ToolMenuItem;
37 import org.herac.tuxguitar.gui.items.menu.TrackMenuItem;
38 import org.herac.tuxguitar.gui.items.menu.TransportMenuItem;
39 import org.herac.tuxguitar.gui.items.menu.ViewMenuItem;
40 import org.herac.tuxguitar.gui.items.tool.BeatToolItems;
41 import org.herac.tuxguitar.gui.items.tool.CompositionToolItems;
42 import org.herac.tuxguitar.gui.items.tool.DurationToolItems;
43 import org.herac.tuxguitar.gui.items.tool.DynamicToolItems;
44 import org.herac.tuxguitar.gui.items.tool.EditToolItems;
45 import org.herac.tuxguitar.gui.items.tool.FileToolItems;
46 import org.herac.tuxguitar.gui.items.tool.LayoutToolItems;
47 import org.herac.tuxguitar.gui.items.tool.MarkerToolItems;
48 import org.herac.tuxguitar.gui.items.tool.NoteEffectToolItems;
49 import org.herac.tuxguitar.gui.items.tool.PropertiesToolItems;
50 import org.herac.tuxguitar.gui.items.tool.TrackToolItems;
51 import org.herac.tuxguitar.gui.items.tool.TransportToolItems;
52 import org.herac.tuxguitar.gui.items.tool.ViewToolItems;
53 import org.herac.tuxguitar.gui.items.xml.ToolBarsReader;
54 import org.herac.tuxguitar.gui.items.xml.ToolBarsWriter;
55 import org.herac.tuxguitar.gui.system.icons.IconLoader;
56 import org.herac.tuxguitar.gui.system.language.LanguageLoader;
57 import org.herac.tuxguitar.gui.util.TGFileUtils;
58 import org.herac.tuxguitar.util.TGSynchronizer;
59 
60 /**
61  * @author julian
62  *
63  * TODO To change the template for this generated type comment go to
64  * Window - Preferences - Java - Code Style - Code Templates
65  */
66 public class ItemManager implements TGUpdateListener,IconLoader,LanguageLoader{
67 
68 	private Menu menu;
69 	private Menu popupMenu;
70 	private CoolBar coolBar;
71 	private List loadedToolItems;
72 	private List loadedMenuItems;
73 	private List loadedPopupMenuItems;
74 	private ToolItems[] toolItems;
75 
76 	private boolean layout_locked;
77 	private boolean shouldReloadToolBars;
78 	private boolean coolbarVisible;
79 	private boolean updateCoolBarWrapIndicesEnabled;
80 
ItemManager()81 	public ItemManager(){
82 		this.loadedToolItems = new ArrayList();
83 		this.loadedMenuItems = new ArrayList();
84 		this.loadedPopupMenuItems = new ArrayList();
85 		this.layout_locked = false;
86 		this.setDefaultToolBars();
87 		this.loadItems();
88 		TuxGuitar.instance().getIconManager().addLoader(this);
89 		TuxGuitar.instance().getLanguageManager().addLoader(this);
90 		TuxGuitar.instance().getEditorManager().addUpdateListener(this);
91 	}
92 
loadItems()93 	public void loadItems(){
94 		this.createMenu();
95 		this.createPopupMenu();
96 		this.createCoolbar();
97 	}
98 
createCoolbar()99 	public void createCoolbar() {
100 		boolean initialized = (this.coolBar != null && !this.coolBar.isDisposed());
101 
102 		this.layout_locked = true;
103 		this.updateCoolBarWrapIndicesEnabled = true;
104 		if( !initialized ){
105 			FormData coolData = new FormData();
106 			coolData.left = new FormAttachment(0);
107 			coolData.right = new FormAttachment(100);
108 			coolData.top = new FormAttachment(0,0);
109 
110 			this.coolBar = new CoolBar(TuxGuitar.instance().getShell(),SWT.HORIZONTAL | SWT.FLAT);
111 			this.coolBar.setLayoutData(coolData);
112 			this.coolBar.addListener(SWT.Resize, new Listener() {
113 				public void handleEvent(Event event) {
114 					layoutCoolBar();
115 				}
116 			});
117 			this.coolBar.addListener(SWT.DragDetect, new Listener() {
118 				public void handleEvent(Event event) {
119 					disableUpdateCoolBarWrapIndices();
120 				}
121 			});
122 
123 			TuxGuitar.instance().getkeyBindingManager().appendListenersTo(this.coolBar);
124 		}
125 		this.makeCoolItems();
126 
127 		this.layout_locked = false;
128 
129 		if( initialized ){
130 			this.layoutCoolBar();
131 		}
132 	}
133 
toogleToolbarVisibility()134 	public void toogleToolbarVisibility(){
135 		if(this.coolBar != null && !this.coolBar.isDisposed()){
136 			this.layout_locked = true;
137 			this.coolBar.setVisible( !this.coolbarVisible );
138 			if( this.coolbarVisible ){
139 				this.clearCoolBar();
140 			}else{
141 				this.makeCoolItems();
142 			}
143 
144 			this.layout_locked = false;
145 
146 			this.layoutCoolBar();
147 		}
148 	}
149 
clearCoolBar()150 	private void clearCoolBar(){
151 		if(this.coolBar != null && !this.coolBar.isDisposed()){
152 			this.loadedToolItems.clear();
153 			CoolItem[] items = this.coolBar.getItems();
154 			for(int i = 0;i < items.length; i ++){
155 				items[i].dispose();
156 			}
157 			Control[] controls = this.coolBar.getChildren();
158 			for(int i = 0;i < controls.length; i ++){
159 				controls[i].dispose();
160 			}
161 		}
162 		this.coolbarVisible = false;
163 	}
164 
updateCoolBarWrapIndices()165 	protected void updateCoolBarWrapIndices(){
166 		int coolBarWidth = this.coolBar.getClientArea().width;
167 		int coolItemsWidth = 0;
168 
169 		List coolItemIndices = new ArrayList();
170 
171 		CoolItem[] items = this.coolBar.getItems();
172 		for(int i = 0;i < items.length; i ++){
173 			Point controlSise = items[i].getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT);
174 			Point itemSize = items[i].computeSize(controlSise.x, controlSise.y);
175 
176 			int nextCoolItemsWidth = ( coolItemsWidth + itemSize.x );
177 			if( nextCoolItemsWidth > coolBarWidth ){
178 				coolItemIndices.add( new Integer( i ) );
179 				nextCoolItemsWidth = itemSize.x;
180 			}
181 			coolItemsWidth = nextCoolItemsWidth;
182 		}
183 
184 		int[] coolItemIndicesArray = new int[ coolItemIndices.size() ];
185 		for(int i = 0;i < coolItemIndicesArray.length; i ++){
186 			coolItemIndicesArray[i] = ((Integer)coolItemIndices.get(i)).intValue();
187 		}
188 
189 		this.coolBar.setWrapIndices( coolItemIndicesArray );
190 	}
191 
layoutCoolBar()192 	protected void layoutCoolBar(){
193 		if(!this.layout_locked){
194 			this.layout_locked = true;
195 			if( this.updateCoolBarWrapIndicesEnabled ){
196 				this.updateCoolBarWrapIndices();
197 			}
198 			this.layoutShellLater();
199 			this.layout_locked = false;
200 		}
201 	}
202 
layoutShell()203 	protected void layoutShell(){
204 		if(!this.layout_locked){
205 			this.layout_locked = true;
206 			TuxGuitar.instance().getShell().layout(true,true);
207 			this.layout_locked = false;
208 		}
209 	}
210 
layoutShellLater()211 	protected void layoutShellLater(){
212 		try {
213 			TGSynchronizer.instance().runLater(new TGSynchronizer.TGRunnable() {
214 				public void run() throws Throwable {
215 					layoutShell();
216 				}
217 			});
218 		} catch (Throwable e) {
219 			e.printStackTrace();
220 		}
221 	}
222 
makeCoolItems()223 	public void makeCoolItems(){
224 		this.clearCoolBar();
225 		this.readToolBars();
226 		for(int i = 0; i < this.toolItems.length; i ++){
227 			if(this.toolItems[i].isEnabled()){
228 				this.makeToolBar(this.toolItems[i]);
229 			}
230 		}
231 		this.coolbarVisible = true;
232 	}
233 
makeToolBar(ToolItems item)234 	private void makeToolBar(ToolItems item){
235 		ToolBar toolBar = new ToolBar(this.coolBar,SWT.HORIZONTAL | SWT.FLAT );
236 		item.showItems(toolBar);
237 		makeCoolItem(toolBar);
238 		this.loadedToolItems.add(item);
239 	}
240 
makeCoolItem(ToolBar toolBar)241 	private void makeCoolItem(ToolBar toolBar){
242 		Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
243 		CoolItem coolItem = new CoolItem(this.coolBar, SWT.NONE);
244 		coolItem.setMinimumSize(size);
245 		coolItem.setPreferredSize(coolItem.computeSize(size.x, size.y));
246 		coolItem.setControl(toolBar);
247 	}
248 
createMenu()249 	public void createMenu() {
250 		Shell shell = TuxGuitar.instance().getShell();
251 		if(this.menu == null || this.menu.isDisposed()){
252 			this.menu = new Menu(shell, SWT.BAR);
253 		}
254 		MenuItem[] items = this.menu.getItems();
255 		for(int i = 0; i < items.length;i ++){
256 			items[i].dispose();
257 		}
258 
259 		this.loadedMenuItems.clear();
260 		this.loadedMenuItems.add(new FileMenuItem(shell,this.menu, SWT.CASCADE));
261 		this.loadedMenuItems.add(new EditMenuItem(shell,this.menu, SWT.CASCADE));
262 		this.loadedMenuItems.add(new ViewMenuItem(shell,this.menu, SWT.CASCADE));
263 		this.loadedMenuItems.add(new CompositionMenuItem(shell,this.menu, SWT.CASCADE));
264 		this.loadedMenuItems.add(new TrackMenuItem(shell,this.menu, SWT.CASCADE));
265 		this.loadedMenuItems.add(new MeasureMenuItem(shell,this.menu, SWT.CASCADE));
266 		this.loadedMenuItems.add(new BeatMenuItem(shell,this.menu, SWT.CASCADE));
267 		this.loadedMenuItems.add(new MarkerMenuItem(shell,this.menu, SWT.CASCADE));
268 		this.loadedMenuItems.add(new TransportMenuItem(shell,this.menu, SWT.CASCADE));
269 		this.loadedMenuItems.add(new ToolMenuItem(shell,this.menu, SWT.CASCADE));
270 		this.loadedMenuItems.add(new HelpMenuItem(shell,this.menu, SWT.CASCADE));
271 		this.showMenuItems(this.loadedMenuItems);
272 		shell.setMenuBar(this.menu);
273 	}
274 
createPopupMenu()275 	public void createPopupMenu() {
276 		Shell shell = TuxGuitar.instance().getShell();
277 		if(this.popupMenu == null || this.popupMenu.isDisposed()){
278 			this.popupMenu = new Menu(shell, SWT.POP_UP);
279 		}
280 		MenuItem[] items = this.popupMenu.getItems();
281 		for(int i = 0; i < items.length;i ++){
282 			items[i].dispose();
283 		}
284 		this.loadedPopupMenuItems.clear();
285 		this.loadedPopupMenuItems.add(new EditMenuItem(shell,this.popupMenu, SWT.CASCADE));
286 		this.loadedPopupMenuItems.add(new CompositionMenuItem(shell,this.popupMenu, SWT.CASCADE));
287 		this.loadedPopupMenuItems.add(new TrackMenuItem(shell,this.popupMenu, SWT.CASCADE));
288 		this.loadedPopupMenuItems.add(new MeasureMenuItem(shell,this.popupMenu, SWT.CASCADE));
289 		this.loadedPopupMenuItems.add(new BeatMenuItem(shell,this.popupMenu, SWT.CASCADE));
290 		this.loadedPopupMenuItems.add(new MarkerMenuItem(shell,this.popupMenu, SWT.CASCADE));
291 		this.loadedPopupMenuItems.add(new TransportMenuItem(shell,this.popupMenu, SWT.CASCADE));
292 		this.showMenuItems(this.loadedPopupMenuItems);
293 	}
294 
showMenuItems(List items)295 	private void showMenuItems(List items){
296 		Iterator it = items.iterator();
297 		while(it.hasNext()){
298 			MenuItems item = (MenuItems)it.next();
299 			item.showItems();
300 		}
301 	}
302 
updateItems()303 	public void updateItems(){
304 		if(!isDisposed()){
305 			updateItems(this.loadedToolItems);
306 			updateItems(this.loadedMenuItems);
307 			updateItems(this.loadedPopupMenuItems);
308 		}
309 	}
310 
updateItems(List items)311 	public void updateItems(List items){
312 		Iterator it = items.iterator();
313 		while(it.hasNext()){
314 			ItemBase item = (ItemBase)it.next();
315 			item.update();
316 		}
317 	}
318 
loadProperties()319 	public void loadProperties(){
320 		if(!isDisposed()){
321 			loadProperties(this.loadedToolItems);
322 			loadProperties(this.loadedMenuItems);
323 			loadProperties(this.loadedPopupMenuItems);
324 		}
325 	}
326 
loadProperties(List items)327 	public void loadProperties(List items){
328 		Iterator it = items.iterator();
329 		while(it.hasNext()){
330 			ItemBase item = (ItemBase)it.next();
331 			item.loadProperties();
332 		}
333 	}
334 
loadIcons()335 	public void loadIcons(){
336 		this.loadItems();
337 	}
338 
getCoolbar()339 	public CoolBar getCoolbar(){
340 		return this.coolBar;
341 	}
342 
getPopupMenu()343 	public Menu getPopupMenu(){
344 		return this.popupMenu;
345 	}
346 
readToolBars()347 	public void readToolBars() {
348 		File file = new File(getCoolItemsFileName());
349 		if(!file.exists()){
350 			writeToolBars();
351 		}
352 		this.shouldReloadToolBars = false;
353 		ToolBarsReader.loadToolBars(this,file);
354 	}
355 
writeToolBars()356 	public void writeToolBars(){
357 		File file = new File(getCoolItemsFileName());
358 		ToolBarsWriter.saveToolBars(getToolBars(), file);
359 	}
360 
setToolBarStatus(String name, boolean enabled, int index)361 	public void setToolBarStatus(String name, boolean enabled, int index){
362 		if(index >= 0 && index < this.toolItems.length){
363 			setToolBarPosition(name, index);
364 			setToolBarEnabled(index, enabled);
365 		}
366 	}
367 
setToolBarEnabled(int index, boolean enabled)368 	public void setToolBarEnabled(int index, boolean enabled){
369 		this.shouldReloadToolBars = (this.shouldReloadToolBars || (this.toolItems[ index ].isEnabled() != enabled ));
370 
371 		this.toolItems[ index ].setEnabled(enabled);
372 	}
373 
setToolBarPosition(String name, int index)374 	public void setToolBarPosition(String name, int index){
375 		if(index >= 0 && index < this.toolItems.length){
376 			ToolItems element = this.toolItems[index];
377 			if( ! element.getName().trim().toLowerCase().equals(name.trim().toLowerCase())){
378 				int oldIndex = -1;
379 				for(int i = 0; i < this.toolItems.length; i ++){
380 					if(this.toolItems[i].getName().trim().toLowerCase().equals(name.trim().toLowerCase())){
381 						oldIndex = i;
382 						break;
383 					}
384 				}
385 				if(oldIndex == -1){
386 					return;
387 				}
388 				this.toolItems[index] = this.toolItems[oldIndex];
389 				this.toolItems[oldIndex] = element;
390 
391 				this.shouldReloadToolBars = true;
392 			}
393 		}
394 	}
395 
getToolBars()396 	public ToolItems[] getToolBars(){
397 		return this.toolItems;
398 	}
399 
shouldReloadToolBars()400 	public boolean shouldReloadToolBars(){
401 		return this.shouldReloadToolBars;
402 	}
403 
setDefaultToolBars()404 	public void setDefaultToolBars(){
405 		this.toolItems = new ToolItems[]{
406 				initToolItem(new FileToolItems(), true),
407 				initToolItem(new EditToolItems(), true),
408 				initToolItem(new PropertiesToolItems(), true),
409 				initToolItem(new TrackToolItems(), true),
410 				initToolItem(new DurationToolItems(), true),
411 				initToolItem(new BeatToolItems(), true),
412 				initToolItem(new CompositionToolItems(), true),
413 				initToolItem(new TransportToolItems(), true),
414 				initToolItem(new MarkerToolItems(), true),
415 				initToolItem(new LayoutToolItems(), true),
416 				initToolItem(new ViewToolItems(), true),
417 				initToolItem(new NoteEffectToolItems(), true),
418 				initToolItem(new DynamicToolItems(), true),
419 		};
420 		this.shouldReloadToolBars = true;
421 	}
422 
initToolItem(ToolItems item, boolean enabled)423 	private ToolItems initToolItem(ToolItems item, boolean enabled){
424 		item.setEnabled(enabled);
425 		return item;
426 	}
427 
isDisposed()428 	private boolean isDisposed(){
429 		return (this.coolBar.isDisposed() || this.menu.isDisposed() || this.popupMenu.isDisposed());
430 	}
431 
getCoolItemsFileName()432 	private String getCoolItemsFileName(){
433 		return TGFileUtils.PATH_USER_CONFIG + File.separator + "toolbars.xml";
434 	}
435 
doUpdate(int type)436 	public void doUpdate(int type) {
437 		if( type == TGUpdateListener.SELECTION ){
438 			this.updateItems();
439 		}
440 	}
441 
disableUpdateCoolBarWrapIndices()442 	public void disableUpdateCoolBarWrapIndices() {
443 		if( this.updateCoolBarWrapIndicesEnabled ){
444 			this.coolBar.setWrapIndices( null );
445 		}
446 		this.updateCoolBarWrapIndicesEnabled = false;
447 	}
448 }
449