~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/items/ItemManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080619003030-agens2gvd5m4dacu
New upstream release (Closes: #481728) also (LP: #176979, #212207)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.items.menu.CompositionMenuItem;
 
29
import org.herac.tuxguitar.gui.items.menu.EditMenuItem;
 
30
import org.herac.tuxguitar.gui.items.menu.FileMenuItem;
 
31
import org.herac.tuxguitar.gui.items.menu.HelpMenuItem;
 
32
import org.herac.tuxguitar.gui.items.menu.ViewMenuItem;
 
33
import org.herac.tuxguitar.gui.items.menu.MarkerMenuItem;
 
34
import org.herac.tuxguitar.gui.items.menu.MeasureMenuItem;
 
35
import org.herac.tuxguitar.gui.items.menu.BeatMenuItem;
 
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.tool.CompositionToolItems;
 
40
import org.herac.tuxguitar.gui.items.tool.DurationToolItems;
 
41
import org.herac.tuxguitar.gui.items.tool.DynamicToolItems;
 
42
import org.herac.tuxguitar.gui.items.tool.EditToolItems;
 
43
import org.herac.tuxguitar.gui.items.tool.FileToolItems;
 
44
import org.herac.tuxguitar.gui.items.tool.LayoutToolItems;
 
45
import org.herac.tuxguitar.gui.items.tool.MarkerToolItems;
 
46
import org.herac.tuxguitar.gui.items.tool.NoteEffectToolItems;
 
47
import org.herac.tuxguitar.gui.items.tool.BeatToolItems;
 
48
import org.herac.tuxguitar.gui.items.tool.PropertiesToolItems;
 
49
import org.herac.tuxguitar.gui.items.tool.TrackToolItems;
 
50
import org.herac.tuxguitar.gui.items.tool.TransportToolItems;
 
51
import org.herac.tuxguitar.gui.items.tool.ViewToolItems;
 
52
import org.herac.tuxguitar.gui.items.xml.ToolBarsReader;
 
53
import org.herac.tuxguitar.gui.items.xml.ToolBarsWriter;
 
54
import org.herac.tuxguitar.gui.system.icons.IconLoader;
 
55
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
 
56
import org.herac.tuxguitar.gui.util.TGFileUtils;
 
57
 
 
58
/**
 
59
 * @author julian
 
60
 *
 
61
 * TODO To change the template for this generated type comment go to
 
62
 * Window - Preferences - Java - Code Style - Code Templates
 
63
 */
 
64
public class ItemManager implements IconLoader,LanguageLoader{
 
65
        //private static final int COOL_ITEM_HEIGHT = 32;
 
66
        
 
67
        private Menu menu;
 
68
        private Menu popupMenu;
 
69
        private CoolBar coolBar;
 
70
        private List loadedToolItems;
 
71
        private List loadedMenuItems;
 
72
        private List loadedPopupMenuItems;
 
73
        private ToolItems[] toolItems;
 
74
        
 
75
        private boolean layout_locked;
 
76
        private boolean shouldReloadToolBars;
 
77
        
 
78
        public ItemManager(){
 
79
                this.loadedToolItems = new ArrayList();
 
80
                this.loadedMenuItems = new ArrayList();
 
81
                this.loadedPopupMenuItems = new ArrayList();
 
82
                this.layout_locked = false;
 
83
                this.setDefaultToolBars();
 
84
                this.loadItems();
 
85
                TuxGuitar.instance().getIconManager().addLoader(this);
 
86
                TuxGuitar.instance().getLanguageManager().addLoader(this);
 
87
        }
 
88
        
 
89
        public void loadItems(){
 
90
                this.createMenu();
 
91
                this.createPopupMenu();
 
92
                this.createCoolbar();
 
93
        }
 
94
        
 
95
        public void createCoolbar() {
 
96
                boolean initialized = (this.coolBar != null && !this.coolBar.isDisposed());
 
97
                
 
98
                this.layout_locked = true;
 
99
                if( !initialized ){
 
100
                        this.coolBar = new CoolBar(TuxGuitar.instance().getShell(),SWT.FLAT);
 
101
                        FormData coolData = new FormData();
 
102
                        coolData.left = new FormAttachment(0);
 
103
                        coolData.right = new FormAttachment(100);
 
104
                        coolData.top = new FormAttachment(0,0);
 
105
                        this.coolBar.setLayoutData(coolData);
 
106
                        this.coolBar.addListener(SWT.Resize, new Listener() {
 
107
                                public void handleEvent(Event event) {
 
108
                                        layoutCoolBar();
 
109
                                }
 
110
                        });
 
111
                        TuxGuitar.instance().getkeyBindingManager().appendListenersTo(this.coolBar);
 
112
                }
 
113
                this.makeCoolItems();
 
114
                
 
115
                this.layout_locked = false;
 
116
                
 
117
                if( initialized ){
 
118
                        this.layoutCoolBar();
 
119
                }
 
120
        }
 
121
        
 
122
        private void clearCoolBar(){
 
123
                if(this.coolBar != null && !this.coolBar.isDisposed()){
 
124
                        this.loadedToolItems.clear();
 
125
                        CoolItem[] items = this.coolBar.getItems();
 
126
                        for(int i = 0;i < items.length; i ++){
 
127
                                items[i].dispose();
 
128
                        }
 
129
                        Control[] controls = this.coolBar.getChildren();
 
130
                        for(int i = 0;i < controls.length; i ++){
 
131
                                controls[i].dispose();
 
132
                        }
 
133
                }
 
134
        }
 
135
        
 
136
        protected void layoutCoolBar(){
 
137
                if(!this.layout_locked){
 
138
                        this.layout_locked = true;
 
139
                        TuxGuitar.instance().getShell().layout();
 
140
                        this.layout_locked = false;
 
141
                }
 
142
        }
 
143
        
 
144
        public void makeCoolItems(){
 
145
                this.clearCoolBar();
 
146
                this.readToolBars();
 
147
                for(int i = 0; i < this.toolItems.length; i ++){
 
148
                        if(this.toolItems[i].isEnabled()){
 
149
                                this.makeToolBar(this.toolItems[i]);
 
150
                        }
 
151
                }
 
152
        }
 
153
        
 
154
        private void makeToolBar(ToolItems item){
 
155
                ToolBar toolBar = new ToolBar(this.coolBar,SWT.FLAT | SWT.WRAP);
 
156
                item.showItems(toolBar);
 
157
                makeCoolItem(toolBar); 
 
158
                this.loadedToolItems.add(item);
 
159
        }
 
160
        
 
161
        private void makeCoolItem(ToolBar toolBar){
 
162
                CoolItem coolItem = new CoolItem(this.coolBar,SWT.NONE);
 
163
                coolItem.setControl(toolBar);
 
164
                Point size = toolBar.computeSize( SWT.DEFAULT,SWT.DEFAULT);
 
165
                //Point coolSize = coolItem.computeSize(size.x, COOL_ITEM_HEIGHT);
 
166
                Point coolSize = coolItem.computeSize(size.x, size.y);
 
167
                coolItem.setMinimumSize(coolSize);
 
168
                coolItem.setSize(coolSize);
 
169
        }
 
170
        
 
171
        public void createMenu() {
 
172
                Shell shell = TuxGuitar.instance().getShell();
 
173
                if(this.menu == null || this.menu.isDisposed()){
 
174
                        this.menu = new Menu(shell, SWT.BAR);
 
175
                }
 
176
                MenuItem[] items = this.menu.getItems();
 
177
                for(int i = 0; i < items.length;i ++){
 
178
                        items[i].dispose();
 
179
                }
 
180
                
 
181
                this.loadedMenuItems.clear();
 
182
                this.loadedMenuItems.add(new FileMenuItem(shell,this.menu, SWT.CASCADE));
 
183
                this.loadedMenuItems.add(new EditMenuItem(shell,this.menu, SWT.CASCADE));
 
184
                this.loadedMenuItems.add(new ViewMenuItem(shell,this.menu, SWT.CASCADE));
 
185
                this.loadedMenuItems.add(new CompositionMenuItem(shell,this.menu, SWT.CASCADE));
 
186
                this.loadedMenuItems.add(new TrackMenuItem(shell,this.menu, SWT.CASCADE));
 
187
                this.loadedMenuItems.add(new MeasureMenuItem(shell,this.menu, SWT.CASCADE));
 
188
                this.loadedMenuItems.add(new BeatMenuItem(shell,this.menu, SWT.CASCADE));
 
189
                this.loadedMenuItems.add(new MarkerMenuItem(shell,this.menu, SWT.CASCADE));
 
190
                this.loadedMenuItems.add(new TransportMenuItem(shell,this.menu, SWT.CASCADE));
 
191
                this.loadedMenuItems.add(new ToolMenuItem(shell,this.menu, SWT.CASCADE));
 
192
                this.loadedMenuItems.add(new HelpMenuItem(shell,this.menu, SWT.CASCADE));
 
193
                this.showMenuItems(this.loadedMenuItems);
 
194
                shell.setMenuBar(this.menu);
 
195
        }
 
196
        
 
197
        public void createPopupMenu() {
 
198
                Shell shell = TuxGuitar.instance().getShell();
 
199
                if(this.popupMenu == null || this.popupMenu.isDisposed()){
 
200
                        this.popupMenu = new Menu(shell, SWT.POP_UP);
 
201
                }
 
202
                MenuItem[] items = this.popupMenu.getItems();
 
203
                for(int i = 0; i < items.length;i ++){
 
204
                        items[i].dispose();
 
205
                }
 
206
                this.loadedPopupMenuItems.clear();
 
207
                this.loadedPopupMenuItems.add(new EditMenuItem(shell,this.popupMenu, SWT.CASCADE));
 
208
                this.loadedPopupMenuItems.add(new CompositionMenuItem(shell,this.popupMenu, SWT.CASCADE));
 
209
                this.loadedPopupMenuItems.add(new TrackMenuItem(shell,this.popupMenu, SWT.CASCADE));
 
210
                this.loadedPopupMenuItems.add(new MeasureMenuItem(shell,this.popupMenu, SWT.CASCADE));
 
211
                this.loadedPopupMenuItems.add(new BeatMenuItem(shell,this.popupMenu, SWT.CASCADE)); 
 
212
                this.loadedPopupMenuItems.add(new MarkerMenuItem(shell,this.popupMenu, SWT.CASCADE));
 
213
                this.loadedPopupMenuItems.add(new TransportMenuItem(shell,this.popupMenu, SWT.CASCADE));
 
214
                this.showMenuItems(this.loadedPopupMenuItems);
 
215
        }
 
216
        
 
217
        private void showMenuItems(List items){
 
218
                Iterator it = items.iterator();
 
219
                while(it.hasNext()){
 
220
                        MenuItems item = (MenuItems)it.next();
 
221
                        item.showItems();
 
222
                }
 
223
        }
 
224
        
 
225
        public void updateItems(){
 
226
                if(!isDisposed()){
 
227
                        updateItems(this.loadedToolItems);
 
228
                        updateItems(this.loadedMenuItems);
 
229
                        updateItems(this.loadedPopupMenuItems);
 
230
                }
 
231
        }
 
232
        
 
233
        public void updateItems(List items){
 
234
                Iterator it = items.iterator();
 
235
                while(it.hasNext()){
 
236
                        ItemBase item = (ItemBase)it.next();
 
237
                        item.update();
 
238
                }
 
239
        }
 
240
        
 
241
        public void loadProperties(){
 
242
                if(!isDisposed()){
 
243
                        loadProperties(this.loadedToolItems);
 
244
                        loadProperties(this.loadedMenuItems);
 
245
                        loadProperties(this.loadedPopupMenuItems);
 
246
                }
 
247
        }
 
248
        
 
249
        public void loadProperties(List items){
 
250
                Iterator it = items.iterator();
 
251
                while(it.hasNext()){
 
252
                        ItemBase item = (ItemBase)it.next();
 
253
                        item.loadProperties();
 
254
                }
 
255
        }
 
256
        
 
257
        public void loadIcons(){
 
258
                this.loadItems();
 
259
        }
 
260
        
 
261
        public CoolBar getCoolbar(){
 
262
                return this.coolBar;
 
263
        }
 
264
        
 
265
        public Menu getPopupMenu(){
 
266
                return this.popupMenu;
 
267
        }
 
268
        
 
269
        public void readToolBars() {
 
270
                File file = new File(getCoolItemsFileName());
 
271
                if(!file.exists()){
 
272
                        writeToolBars();
 
273
                }
 
274
                this.shouldReloadToolBars = false;
 
275
                ToolBarsReader.loadToolBars(this,file);
 
276
        }
 
277
        
 
278
        public void writeToolBars(){
 
279
                File file = new File(getCoolItemsFileName());
 
280
                ToolBarsWriter.saveToolBars(getToolBars(), file);
 
281
        }
 
282
        
 
283
        public void setToolBarStatus(String name, boolean enabled, int index){
 
284
                if(index >= 0 && index < this.toolItems.length){
 
285
                        setToolBarPosition(name, index);
 
286
                        setToolBarEnabled(index, enabled);
 
287
                }
 
288
        }
 
289
        
 
290
        public void setToolBarEnabled(int index, boolean enabled){
 
291
                this.shouldReloadToolBars = (this.shouldReloadToolBars || (this.toolItems[ index ].isEnabled() != enabled ));
 
292
                
 
293
                this.toolItems[ index ].setEnabled(enabled);
 
294
        }
 
295
        
 
296
        public void setToolBarPosition(String name, int index){
 
297
                if(index >= 0 && index < this.toolItems.length){
 
298
                        ToolItems element = this.toolItems[index];
 
299
                        if( ! element.getName().trim().toLowerCase().equals(name.trim().toLowerCase())){
 
300
                                int oldIndex = -1;
 
301
                                for(int i = 0; i < this.toolItems.length; i ++){
 
302
                                        if(this.toolItems[i].getName().trim().toLowerCase().equals(name.trim().toLowerCase())){
 
303
                                                oldIndex = i;
 
304
                                                break;
 
305
                                        }
 
306
                                }
 
307
                                if(oldIndex == -1){
 
308
                                        return;
 
309
                                }
 
310
                                this.toolItems[index] = this.toolItems[oldIndex];
 
311
                                this.toolItems[oldIndex] = element;
 
312
                                
 
313
                                this.shouldReloadToolBars = true;
 
314
                        }
 
315
                }
 
316
        }
 
317
        
 
318
        public ToolItems[] getToolBars(){
 
319
                return this.toolItems;
 
320
        }
 
321
        
 
322
        public boolean shouldReloadToolBars(){
 
323
                return this.shouldReloadToolBars;
 
324
        }
 
325
        
 
326
        public void setDefaultToolBars(){
 
327
                this.toolItems = new ToolItems[]{
 
328
                                initToolItem(new FileToolItems(), true),
 
329
                                initToolItem(new EditToolItems(), true),
 
330
                                initToolItem(new PropertiesToolItems(), true),
 
331
                                initToolItem(new TrackToolItems(), true),
 
332
                                initToolItem(new DurationToolItems(), true),
 
333
                                initToolItem(new BeatToolItems(), true),
 
334
                                initToolItem(new CompositionToolItems(), true),
 
335
                                initToolItem(new TransportToolItems(), true),
 
336
                                initToolItem(new MarkerToolItems(), true),
 
337
                                initToolItem(new LayoutToolItems(), true),
 
338
                                initToolItem(new ViewToolItems(), true),
 
339
                                initToolItem(new NoteEffectToolItems(), true),
 
340
                                initToolItem(new DynamicToolItems(), true),
 
341
                };
 
342
                this.shouldReloadToolBars = true;
 
343
        }
 
344
        
 
345
        private ToolItems initToolItem(ToolItems item, boolean enabled){
 
346
                item.setEnabled(enabled);
 
347
                return item;
 
348
        }
 
349
        
 
350
        private boolean isDisposed(){
 
351
                return (this.coolBar.isDisposed() || this.menu.isDisposed() || this.popupMenu.isDisposed());
 
352
        }
 
353
        
 
354
        private String getCoolItemsFileName(){
 
355
                return TGFileUtils.PATH_USER_CONFIG + File.separator + "toolbars.xml";
 
356
        }
 
357
}