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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

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.Shell;
25
 
import org.eclipse.swt.widgets.ToolBar;
26
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
27
 
import org.herac.tuxguitar.gui.items.menu.CompositionMenuItem;
28
 
import org.herac.tuxguitar.gui.items.menu.EditMenuItem;
29
 
import org.herac.tuxguitar.gui.items.menu.FileMenuItem;
30
 
import org.herac.tuxguitar.gui.items.menu.HelpMenuItem;
31
 
import org.herac.tuxguitar.gui.items.menu.LayoutMenuItem;
32
 
import org.herac.tuxguitar.gui.items.menu.MarkerMenuItem;
33
 
import org.herac.tuxguitar.gui.items.menu.MeasureMenuItem;
34
 
import org.herac.tuxguitar.gui.items.menu.NoteMenuItem;
35
 
import org.herac.tuxguitar.gui.items.menu.PlayMenuItem;
36
 
import org.herac.tuxguitar.gui.items.menu.PluginMenuItem;
37
 
import org.herac.tuxguitar.gui.items.menu.SettingsMenuItem;
38
 
import org.herac.tuxguitar.gui.items.menu.TrackMenuItem;
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.NoteToolItems;
48
 
import org.herac.tuxguitar.gui.items.tool.PlayerToolItems;
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.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.util.TuxGuitarFileUtils;
55
 
 
56
 
/**
57
 
 * @author julian
58
 
 *
59
 
 * TODO To change the template for this generated type comment go to
60
 
 * Window - Preferences - Java - Code Style - Code Templates
61
 
 */
62
 
public class ItemManager {
63
 
        private static final int HEIGHT = 32;
64
 
        
65
 
    private static ToolItems[] AVAILABLE_TOOL_BARS = new ToolItems[]{
66
 
        new FileToolItems(),
67
 
        new EditToolItems(),
68
 
        new PropertiesToolItems(),
69
 
        new TrackToolItems(),
70
 
        new DurationToolItems(),
71
 
        new NoteToolItems(),    
72
 
        new CompositionToolItems(),        
73
 
        new PlayerToolItems(),        
74
 
        new MarkerToolItems(),        
75
 
        new LayoutToolItems(),
76
 
        new ViewToolItems(),
77
 
        new NoteEffectToolItems(),
78
 
        new DynamicToolItems()
79
 
    };
80
 
        
81
 
    private TablatureEditor tablatureEditor;    
82
 
    private Menu menu;
83
 
    private Menu popupMenu;
84
 
    private CoolBar coolBar;
85
 
    private List toolItems;
86
 
    private List menuItems;
87
 
    private List popupMenuItems;    
88
 
    private boolean layout_locked;
89
 
    
90
 
    public ItemManager(TablatureEditor tablatureEditor){
91
 
        this.tablatureEditor = tablatureEditor;        
92
 
        this.toolItems = new ArrayList();
93
 
        this.menuItems = new ArrayList();
94
 
        this.popupMenuItems = new ArrayList();
95
 
        this.layout_locked = false;
96
 
    }
97
 
 
98
 
    public void createItems(final Shell shell){
99
 
                this.createMenu(shell);
100
 
                this.createPopupMenu(shell);
101
 
                this.createCoolbar(shell);
102
 
    }
103
 
    
104
 
    public void createCoolbar(final Shell shell) {                      
105
 
        this.coolBar = new CoolBar(shell,SWT.FLAT);
106
 
        this.makeCoolItems();
107
 
        
108
 
        FormData coolData = new FormData();
109
 
        coolData.left = new FormAttachment(0);
110
 
        coolData.right = new FormAttachment(100);
111
 
        coolData.top = new FormAttachment(0,0);        
112
 
        coolBar.setLayoutData(coolData);
113
 
        coolBar.addListener(SWT.Resize, new Listener() {
114
 
          public void handleEvent(Event event) {
115
 
                  layoutCoolBar();
116
 
          }
117
 
        });
118
 
    }    
119
 
    
120
 
    private void clearCoolBar(){
121
 
        if(this.coolBar != null && !this.coolBar.isDisposed()){
122
 
                this.toolItems.clear();
123
 
                
124
 
                CoolItem[] items = this.coolBar.getItems();
125
 
            for(int i = 0;i < items.length; i ++){
126
 
                items[i].dispose();
127
 
            }
128
 
                Control[] controls = this.coolBar.getChildren();
129
 
            for(int i = 0;i < controls.length; i ++){
130
 
                controls[i].dispose();
131
 
            }            
132
 
        }
133
 
    }    
134
 
    
135
 
    private void layoutCoolBar(){
136
 
        if(!this.layout_locked){
137
 
                this.layout_locked = true;
138
 
                
139
 
                this.coolBar.getParent().layout();
140
 
                this.coolBar.layout();
141
 
                
142
 
                this.layout_locked = false;
143
 
        }
144
 
    }
145
 
 
146
 
    public void makeCoolItems(){        
147
 
        clearCoolBar();         
148
 
        List enableToolBars = getEnabledToolBars();
149
 
        for(int i = 0; i < enableToolBars.size(); i ++){
150
 
                String name = (String)enableToolBars.get(i);
151
 
                for(int j = 0; j < AVAILABLE_TOOL_BARS.length; j ++){
152
 
                        if(AVAILABLE_TOOL_BARS[j].getName().trim().toLowerCase().equals(name.trim().toLowerCase())){
153
 
                                makeToolBar(AVAILABLE_TOOL_BARS[j]);
154
 
                                break;
155
 
                        }
156
 
                }
157
 
        }    
158
 
        layoutCoolBar();
159
 
    }
160
 
    
161
 
    private void makeToolBar(ToolItems item){
162
 
        ToolBar toolBar = new ToolBar(this.coolBar,SWT.FLAT | SWT.WRAP);        
163
 
        item.showItems(toolBar);
164
 
        makeCoolItem(toolBar); 
165
 
        this.toolItems.add(item);
166
 
    }
167
 
    
168
 
    private void makeCoolItem(ToolBar toolBar){
169
 
        CoolItem coolItem = new CoolItem(coolBar,SWT.NONE);
170
 
        coolItem.setControl(toolBar);
171
 
        Point size = toolBar.computeSize( SWT.DEFAULT,SWT.DEFAULT);        
172
 
        Point coolSize = coolItem.computeSize(size.x, HEIGHT);
173
 
            coolItem.setMinimumSize(coolSize);
174
 
            coolItem.setSize(coolSize);     
175
 
    }
176
 
    
177
 
    public List getEnabledToolBars(){           
178
 
        return loadEnableToolBars(getAvailableToolBarNames());
179
 
    }
180
 
 
181
 
    public List getAvailableToolBarNames(){
182
 
        List names = new ArrayList();           
183
 
                for(int i = 0; i < AVAILABLE_TOOL_BARS.length; i ++){
184
 
                        names.add(AVAILABLE_TOOL_BARS[i].getName());
185
 
        }               
186
 
                return names;
187
 
    }
188
 
    
189
 
    public void createMenu(Shell shell) {
190
 
        this.menu = new Menu(shell, SWT.BAR);        
191
 
        this.menuItems.add(new FileMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));
192
 
        this.menuItems.add(new EditMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));
193
 
        this.menuItems.add(new LayoutMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));
194
 
        this.menuItems.add(new CompositionMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));
195
 
        this.menuItems.add(new TrackMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));
196
 
        this.menuItems.add(new MeasureMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));
197
 
        this.menuItems.add(new NoteMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor)); 
198
 
        this.menuItems.add(new MarkerMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor)); 
199
 
        this.menuItems.add(new PlayMenuItem(shell,this.menu, SWT.CASCADE,tablatureEditor));  
200
 
        this.menuItems.add(new SettingsMenuItem(shell,this.menu, SWT.CASCADE));
201
 
        this.menuItems.add(new PluginMenuItem(shell,this.menu, SWT.CASCADE)); 
202
 
        this.menuItems.add(new HelpMenuItem(shell,this.menu, SWT.CASCADE));
203
 
        this.showMenuItems(this.menuItems);
204
 
        shell.setMenuBar(this.menu);
205
 
    }    
206
 
    
207
 
    public void createPopupMenu(Shell shell) {
208
 
        this.popupMenu = new Menu(shell, SWT.POP_UP);
209
 
        this.popupMenuItems.add(new EditMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor));
210
 
        this.popupMenuItems.add(new CompositionMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor));
211
 
        this.popupMenuItems.add(new TrackMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor));
212
 
        this.popupMenuItems.add(new MeasureMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor));
213
 
        this.popupMenuItems.add(new NoteMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor)); 
214
 
        this.popupMenuItems.add(new MarkerMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor));
215
 
        this.popupMenuItems.add(new PlayMenuItem(shell,this.popupMenu, SWT.CASCADE,tablatureEditor)); 
216
 
        this.showMenuItems(this.popupMenuItems);
217
 
    }        
218
 
    
219
 
    private void showMenuItems(List items){
220
 
        Iterator it = items.iterator();
221
 
        while(it.hasNext()){
222
 
                MenuItems item = (MenuItems)it.next();
223
 
            item.showItems();          
224
 
        }
225
 
    }
226
 
    
227
 
    public void updateItems(){
228
 
        if(!isDisposed()){
229
 
                updateItems(toolItems);
230
 
                updateItems(menuItems);
231
 
                updateItems(popupMenuItems);
232
 
        }       
233
 
    }
234
 
    
235
 
    public void updateItems(List items){
236
 
        Iterator it = items.iterator();
237
 
        while(it.hasNext()){
238
 
            ItemBase item = (ItemBase)it.next();
239
 
            item.update();            
240
 
        }       
241
 
    }
242
 
        
243
 
    public void loadProperties(){
244
 
        if(!isDisposed()){
245
 
                loadProperties(toolItems);
246
 
                loadProperties(menuItems);
247
 
                loadProperties(popupMenuItems);
248
 
        }
249
 
    }
250
 
 
251
 
    public void loadProperties(List items){
252
 
        Iterator it = items.iterator();
253
 
        while(it.hasNext()){
254
 
            ItemBase item = (ItemBase)it.next();
255
 
            item.loadProperties();            
256
 
        }       
257
 
    }    
258
 
    
259
 
    public CoolBar getCoolbar(){
260
 
        return this.coolBar;
261
 
    }
262
 
    
263
 
    public Menu getPopupMenu(){
264
 
        return this.popupMenu;
265
 
    }
266
 
        
267
 
    public List loadEnableToolBars(List availableItems) {
268
 
        File file = new File(getCoolItemsFileName());
269
 
        if(!file.exists()){
270
 
                saveToolBars(availableItems);
271
 
        }
272
 
        return new ToolBarsReader().loadToolBars(file);
273
 
        }
274
 
    
275
 
    public void saveToolBars(List toolItems){
276
 
        new ToolBarsWriter().saveToolBars(toolItems, getCoolItemsFileName());           
277
 
    }    
278
 
    
279
 
    public void saveDefaultToolBars(){
280
 
        saveToolBars(getAvailableToolBarNames());
281
 
    }
282
 
    
283
 
    private boolean isDisposed(){
284
 
        return (this.coolBar.isDisposed() || this.menu.isDisposed() || this.popupMenu.isDisposed());
285
 
    }
286
 
    
287
 
    private String getCoolItemsFileName(){
288
 
        return TuxGuitarFileUtils.USER_CONFIG_PREFIX + File.separator + "toolbars.xml";
289
 
    }
290
 
}
291