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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/items/tool/NoteToolItems.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 02-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.tool;
8
 
 
9
 
import org.eclipse.swt.SWT;
10
 
import org.eclipse.swt.events.SelectionAdapter;
11
 
import org.eclipse.swt.events.SelectionEvent;
12
 
import org.eclipse.swt.graphics.Point;
13
 
import org.eclipse.swt.graphics.Rectangle;
14
 
import org.eclipse.swt.widgets.Menu;
15
 
import org.eclipse.swt.widgets.MenuItem;
16
 
import org.eclipse.swt.widgets.ToolBar;
17
 
import org.eclipse.swt.widgets.ToolItem;
18
 
import org.herac.tuxguitar.gui.SystemImages;
19
 
import org.herac.tuxguitar.gui.TuxGuitar;
20
 
import org.herac.tuxguitar.gui.actions.insert.InsertChordAction;
21
 
import org.herac.tuxguitar.gui.actions.note.ChangeTiedNoteAction;
22
 
import org.herac.tuxguitar.gui.items.ToolItems;
23
 
import org.herac.tuxguitar.song.models.Chord;
24
 
 
25
 
/**
26
 
 * @author julian
27
 
 *
28
 
 * TODO To change the template for this generated type comment go to
29
 
 * Window - Preferences - Java - Code Style - Code Templates
30
 
 */
31
 
public class NoteToolItems  extends ToolItems{
32
 
    public static final String NAME = "note.items";
33
 
    
34
 
    private ToolBar toolBar;   
35
 
    private ToolItem tiedNote;
36
 
    private ChordMenuItem chordItems;
37
 
    
38
 
    public NoteToolItems(){
39
 
        super(NAME);
40
 
    }
41
 
   
42
 
    public void showItems(ToolBar toolBar){
43
 
        this.toolBar = toolBar;     
44
 
        
45
 
        this.tiedNote = new ToolItem(toolBar, SWT.PUSH);
46
 
        this.tiedNote.setImage(SystemImages.TIED_NOTE_IMAGE);
47
 
        this.tiedNote.addSelectionListener(TuxGuitar.instance().getAction(ChangeTiedNoteAction.NAME));
48
 
        
49
 
        this.chordItems = new ChordMenuItem();
50
 
        this.chordItems.addItems();        
51
 
       
52
 
        loadProperties();
53
 
    }
54
 
 
55
 
    
56
 
    
57
 
    public void update(){
58
 
        this.chordItems.update();
59
 
    }
60
 
    
61
 
    public void loadProperties(){
62
 
        this.tiedNote.setToolTipText(TuxGuitar.getProperty("note.tiednote"));
63
 
        this.chordItems.setToolTipText(TuxGuitar.getProperty("insert.chord"));  
64
 
    }           
65
 
    
66
 
    
67
 
    private class ChordMenuItem extends SelectionAdapter {
68
 
        private long lastEdit;
69
 
        private ToolItem item;
70
 
        private Menu subMenu;
71
 
        private MenuItem[] subMenuItems;
72
 
        
73
 
        public ChordMenuItem() {
74
 
            this.item = new ToolItem(toolBar, SWT.DROP_DOWN);            
75
 
            this.item.setImage(SystemImages.CHORD_IMAGE);                         
76
 
            this.item.addSelectionListener(this);                    
77
 
            this.subMenu = new Menu(this.item.getParent().getShell());
78
 
        }
79
 
 
80
 
        public void setToolTipText(String text){
81
 
            this.item.setToolTipText(text);
82
 
        }
83
 
        
84
 
        public void addItems() {    
85
 
                this.disposeItems();
86
 
                this.subMenuItems = new MenuItem[TuxGuitar.instance().getCustomChordManager().countChords()];            
87
 
                for(int i = 0;i < this.subMenuItems.length; i++){               
88
 
                        Chord chord = TuxGuitar.instance().getCustomChordManager().getChord(i);                 
89
 
                        this.subMenuItems[i] = new MenuItem(this.subMenu, SWT.PUSH);
90
 
                        this.subMenuItems[i].setData(chord);
91
 
                        this.subMenuItems[i].setText(chord.getName());       
92
 
                        this.subMenuItems[i].addSelectionListener(TuxGuitar.instance().getAction(InsertChordAction.NAME));
93
 
                }
94
 
        }
95
 
 
96
 
        public void disposeItems() {            
97
 
                if(this.subMenuItems != null){
98
 
                        for(int i = 0;i < this.subMenuItems.length; i++){                                       
99
 
                                this.subMenuItems[i].dispose();          
100
 
                        }
101
 
                }
102
 
        }
103
 
        
104
 
        public void widgetSelected(SelectionEvent event) {
105
 
                if (event.detail == SWT.ARROW && this.subMenuItems != null && this.subMenuItems.length > 0) {            
106
 
                ToolItem item = (ToolItem) event.widget;
107
 
                Rectangle rect = item.getBounds();
108
 
                Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
109
 
                this.subMenu.setLocation(pt.x, pt.y + rect.height);
110
 
                this.subMenu.setVisible(true);
111
 
            }else{
112
 
                TuxGuitar.instance().getAction(InsertChordAction.NAME).process(event); 
113
 
            }
114
 
        }
115
 
        
116
 
        public void update(){
117
 
                if(lastEdit != TuxGuitar.instance().getCustomChordManager().getLastEdit()){
118
 
                        this.addItems();
119
 
                        this.lastEdit = TuxGuitar.instance().getCustomChordManager().getLastEdit();
120
 
                }
121
 
        }          
122
 
    }       
123
 
}
124