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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/system/config/items/ToolBarsOption.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.system.config.items;
 
2
 
 
3
import java.util.Iterator;
 
4
import java.util.List;
 
5
 
 
6
import org.eclipse.swt.SWT;
 
7
import org.eclipse.swt.events.SelectionAdapter;
 
8
import org.eclipse.swt.events.SelectionEvent;
 
9
import org.eclipse.swt.layout.GridData;
 
10
import org.eclipse.swt.layout.GridLayout;
 
11
import org.eclipse.swt.widgets.Button;
 
12
import org.eclipse.swt.widgets.Composite;
 
13
import org.eclipse.swt.widgets.ToolBar;
 
14
import org.herac.tuxguitar.gui.SystemImages;
 
15
import org.herac.tuxguitar.gui.TuxGuitar;
 
16
import org.herac.tuxguitar.gui.system.config.ConfigEditor;
 
17
 
 
18
public class ToolBarsOption extends Option{     
 
19
        private boolean changes;
 
20
        private List availableToolBars;
 
21
        private List customToolBars;
 
22
        private org.eclipse.swt.widgets.List availableList;
 
23
    private org.eclipse.swt.widgets.List customList;
 
24
    private Button addButton;
 
25
    private Button removeButton;
 
26
    private Button moveUpButton;
 
27
    private Button moveDownButton;
 
28
    
 
29
    public ToolBarsOption(ConfigEditor configEditor,ToolBar toolBar,final Composite parent){
 
30
        super(configEditor,toolBar,parent,TuxGuitar.getProperty("settings.config.toolbars"),SWT.FILL,SWT.FILL); 
 
31
        this.changes = false;
 
32
        this.loadToolBars();
 
33
    }
 
34
 
 
35
        public void createOption() {
 
36
                getToolItem().setText(TuxGuitar.getProperty("settings.config.toolbars"));       
 
37
                getToolItem().setImage(SystemImages.OPTION_TOOLBARS);
 
38
                getToolItem().addSelectionListener(this);
 
39
                
 
40
                Composite composite = new Composite(getComposite(),SWT.NONE);
 
41
                composite.setLayout(new GridLayout(2,false));
 
42
                composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
43
                
 
44
                //left list
 
45
                Composite leftList = new Composite(composite, SWT.NONE);
 
46
                leftList.setLayout(new GridLayout());   
 
47
                leftList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
48
                
 
49
                availableList = new org.eclipse.swt.widgets.List(leftList,SWT.BORDER | SWT.V_SCROLL);
 
50
                availableList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));                                 
 
51
                for(int i = 0;i < this.availableToolBars.size(); i ++){
 
52
                        String name = (String)availableToolBars.get(i);
 
53
                        availableList.add(TuxGuitar.getProperty(name));
 
54
                }               
 
55
                availableList.addSelectionListener(new SelectionAdapter() {
 
56
                        public void widgetSelected(SelectionEvent e) {
 
57
                                checkButtons();
 
58
                        }               
 
59
                });
 
60
                
 
61
                //right list
 
62
                Composite rightList = new Composite(composite, SWT.NONE);
 
63
                rightList.setLayout(new GridLayout());
 
64
                rightList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
65
                
 
66
                customList = new org.eclipse.swt.widgets.List(rightList,SWT.BORDER | SWT.V_SCROLL);
 
67
                customList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));            
 
68
                customList.addSelectionListener(new SelectionAdapter() {
 
69
                        public void widgetSelected(SelectionEvent e) {
 
70
                                checkButtons();
 
71
                        }               
 
72
                });             
 
73
                
 
74
                //left buttons
 
75
                Composite leftButtons = new Composite(composite, SWT.NONE);
 
76
                leftButtons.setLayout(new GridLayout());        
 
77
                leftButtons.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
 
78
                
 
79
                addButton = new Button(leftButtons, SWT.PUSH);
 
80
                addButton.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
81
                addButton.setText("Add");               
 
82
                addButton.addSelectionListener(new SelectionAdapter() {
 
83
                        public void widgetSelected(SelectionEvent e) {
 
84
                                addToolBar(availableList.getSelectionIndex());
 
85
                        }               
 
86
                });             
 
87
                
 
88
                //right buttons         
 
89
                Composite rightButtons = new Composite(composite, SWT.NONE);
 
90
                rightButtons.setLayout(new GridLayout(3,false));
 
91
                rightButtons.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
 
92
                
 
93
                removeButton = new Button(rightButtons, SWT.PUSH);
 
94
                removeButton.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
95
                removeButton.setText("Remove");
 
96
                removeButton.addSelectionListener(new SelectionAdapter() {
 
97
                        public void widgetSelected(SelectionEvent e) {
 
98
                                removeToolBar(customList.getSelectionIndex());
 
99
                        }               
 
100
                });
 
101
                
 
102
                moveUpButton = new Button(rightButtons,SWT.ARROW | SWT.UP);     
 
103
                moveUpButton.addSelectionListener(new SelectionAdapter() {
 
104
                        public void widgetSelected(SelectionEvent e) {
 
105
                                moveUp(customList.getSelectionIndex());
 
106
                        }               
 
107
                });
 
108
                
 
109
                moveDownButton = new Button(rightButtons,SWT.ARROW | SWT.DOWN);
 
110
                moveDownButton.addSelectionListener(new SelectionAdapter() {
 
111
                        public void widgetSelected(SelectionEvent e) {
 
112
                                moveDown(customList.getSelectionIndex());
 
113
                        }               
 
114
                });             
 
115
                
 
116
                reloadList(-1);
 
117
        }               
 
118
 
 
119
        
 
120
        public  GridData makeButtonData(int horizontalAlignment,int minWith,int minHeight){   
 
121
        GridData data = new GridData();         
 
122
        data.minimumWidth = minWith;
 
123
        data.minimumHeight = minHeight;
 
124
 
 
125
        data.horizontalAlignment = horizontalAlignment;
 
126
        data.grabExcessHorizontalSpace = true;
 
127
                
 
128
        return data;
 
129
        }
 
130
 
 
131
        private void loadToolBars(){
 
132
                this.availableToolBars = TuxGuitar.instance().getItemManager().getAvailableToolBarNames();
 
133
                this.customToolBars = TuxGuitar.instance().getItemManager().getEnabledToolBars();
 
134
        }
 
135
        
 
136
        private void reloadList(int selection){
 
137
                this.customList.removeAll();
 
138
                
 
139
                Iterator it = this.customToolBars.iterator();
 
140
                while(it.hasNext()){
 
141
                        String name = (String)it.next();
 
142
                        this.customList.add(TuxGuitar.getProperty(name));
 
143
                }
 
144
                this.customList.select(selection);
 
145
                this.checkButtons();
 
146
        }
 
147
        
 
148
        private void checkButtons(){
 
149
                if(addButton != null && !addButton.isDisposed()){
 
150
                        boolean enabled = false;
 
151
                        int index = availableList.getSelectionIndex();
 
152
                        if(index >= 0 && index < this.availableToolBars.size()){
 
153
                                enabled = (!exists((String)this.availableToolBars.get(index)));
 
154
                        }
 
155
                        addButton.setEnabled(enabled);
 
156
                }
 
157
                if(removeButton != null && !removeButton.isDisposed()){
 
158
                        int index = customList.getSelectionIndex();
 
159
                        removeButton.setEnabled((index >= 0 && index < this.customToolBars.size()));
 
160
                }       
 
161
                if(moveUpButton != null && !moveUpButton.isDisposed()){
 
162
                        int index = customList.getSelectionIndex();
 
163
                        moveUpButton.setEnabled(index > 0 && index < this.customToolBars.size());
 
164
                }
 
165
                if(moveDownButton != null && !moveDownButton.isDisposed()){
 
166
                        int index = customList.getSelectionIndex();
 
167
                        moveDownButton.setEnabled(index >= 0 && index < (this.customToolBars.size() - 1));
 
168
                }               
 
169
        }
 
170
        
 
171
        private void addToolBar(int index){
 
172
                if(index >= 0 && index < this.availableToolBars.size()){
 
173
                        String name = (String)this.availableToolBars.get(index);
 
174
                        if(!exists(name)){
 
175
                                this.customToolBars.add(name);
 
176
                                this.reloadList((this.customToolBars.size() - 1));
 
177
                                this.changes = true;
 
178
                        }
 
179
                }
 
180
        }
 
181
 
 
182
        private void removeToolBar(int index){
 
183
                if(index >= 0 && index < this.customToolBars.size()){
 
184
                        this.customToolBars.remove(index);
 
185
                        this.reloadList((this.customToolBars.size() > index)?index:this.customToolBars.size() - 1);
 
186
                        this.changes = true;
 
187
                }
 
188
        }       
 
189
        
 
190
        private void moveUp(int index){
 
191
                if(index > 0 && index < this.customToolBars.size()){
 
192
                        String name = (String)this.customToolBars.get(index);
 
193
                        this.customToolBars.remove(index);
 
194
                        this.customToolBars.add((index - 1),name);
 
195
                        this.reloadList((index - 1));
 
196
                        this.changes = true;
 
197
                }
 
198
        }               
 
199
 
 
200
        private void moveDown(int index){
 
201
                if(index >= 0 && index < (this.customToolBars.size() - 1)){
 
202
                        String name = (String)this.customToolBars.get(index);
 
203
                        this.customToolBars.remove(index);
 
204
                        this.customToolBars.add((index + 1),name);
 
205
                        this.reloadList((index + 1));
 
206
                        this.changes = true;
 
207
                }               
 
208
        }               
 
209
 
 
210
        private boolean exists(String name){
 
211
                Iterator it = this.customToolBars.iterator();
 
212
                while(it.hasNext()){
 
213
                        String currentName = (String)it.next();
 
214
                        if(currentName.equals(name)){
 
215
                                return true;
 
216
                        }
 
217
                }
 
218
                return false;
 
219
        }
 
220
        
 
221
        public void updateConfig() {
 
222
                TuxGuitar.instance().getItemManager().saveToolBars(this.customToolBars);
 
223
        }
 
224
        
 
225
    public void applyConfig(boolean force){
 
226
        if(this.changes || force){
 
227
                TuxGuitar.instance().loadToolBars();
 
228
        }
 
229
    }
 
230
}