1 package org.herac.tuxguitar.gui.editors.matrix;
2 
3 import java.util.Properties;
4 
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.events.DisposeEvent;
7 import org.eclipse.swt.events.DisposeListener;
8 import org.eclipse.swt.events.SelectionAdapter;
9 import org.eclipse.swt.events.SelectionEvent;
10 import org.eclipse.swt.graphics.Color;
11 import org.eclipse.swt.graphics.Font;
12 import org.eclipse.swt.graphics.FontData;
13 import org.eclipse.swt.graphics.RGB;
14 import org.eclipse.swt.graphics.Resource;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.ColorDialog;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.FontDialog;
22 import org.eclipse.swt.widgets.Group;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25 import org.herac.tuxguitar.gui.TuxGuitar;
26 import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
27 import org.herac.tuxguitar.gui.system.config.TGConfigManager;
28 import org.herac.tuxguitar.gui.util.DialogUtils;
29 
30 public class MatrixConfig {
31 
32 	private Font font;
33 	private Color[] colorLines;
34 	private Color colorForeground;
35 	private Color colorBorder;
CommentParserTest()36 	private Color colorPosition;
37 	private Color colorNote;
38 	private Color colorPlay;
39 
40 	public MatrixConfig(){
41 		super();
42 	}
43 
44 	public Font getFont() {
45 		return this.font;
46 	}
47 
48 	public Color getColorForeground() {
49 		return this.colorForeground;
50 	}
51 
52 	public Color getColorBorder() {
53 		return this.colorBorder;
54 	}
parseString(const char * Source)55 
56 	public Color getColorPosition() {
57 		return this.colorPosition;
58 	}
59 
60 	public Color getColorNote() {
61 		return this.colorNote;
62 	}
63 
64 	public Color getColorPlay() {
65 		return this.colorPlay;
66 	}
67 
68 	public Color[] getColorLines() {
69 		return this.colorLines;
70 	}
71 
72 	public Color getColorLine(int index) {
73 		return this.colorLines[index];
74 	}
75 
76 	public void load(){
77 		Display display = TuxGuitar.instance().getDisplay();
78 		TGConfigManager config = TuxGuitar.instance().getConfig();
HasChildCount(const Comment * C,size_t Count)79 		this.font = new Font(display,config.getFontDataConfigValue(TGConfigKeys.MATRIX_FONT));
80 		this.colorForeground = new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_FOREGROUND));
81 		this.colorBorder = new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_BORDER));
82 		this.colorPosition = new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_POSITION));
83 		this.colorNote = new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_NOTE));
84 		this.colorPlay = new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_PLAY_NOTE));
85 		this.colorLines = new Color[]{
86 				new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_LINE_1)),
87 				new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_LINE_2)),
88 				new Color(display,config.getRGBConfigValue(TGConfigKeys.MATRIX_COLOR_LINE_3)),
89 		};
90 	}
91 
GetChildAt(const Comment * C,size_t Idx,T * & Child)92 	public void defaults(){
93 		TGConfigManager config = TuxGuitar.instance().getConfig();
94 		Properties defaults = config.getDefaults();
95 		config.setProperty(TGConfigKeys.MATRIX_FONT,defaults.getProperty(TGConfigKeys.MATRIX_FONT));
96 		config.setProperty(TGConfigKeys.MATRIX_COLOR_FOREGROUND,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_FOREGROUND));
97 		config.setProperty(TGConfigKeys.MATRIX_COLOR_BORDER,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_BORDER));
98 		config.setProperty(TGConfigKeys.MATRIX_COLOR_POSITION,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_POSITION));
99 		config.setProperty(TGConfigKeys.MATRIX_COLOR_NOTE,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_NOTE));
100 		config.setProperty(TGConfigKeys.MATRIX_COLOR_PLAY_NOTE,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_PLAY_NOTE));
101 		config.setProperty(TGConfigKeys.MATRIX_COLOR_LINE_1,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_LINE_1));
102 		config.setProperty(TGConfigKeys.MATRIX_COLOR_LINE_2,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_LINE_2));
103 		config.setProperty(TGConfigKeys.MATRIX_COLOR_LINE_3,defaults.getProperty(TGConfigKeys.MATRIX_COLOR_LINE_3));
104 	}
105 
106 	public void save(FontData fontData,
107 					 RGB rgbForeground,
108 					 RGB rgbBorder,
109 					 RGB rgbPosition,
110 					 RGB rgbNote,
111 					 RGB rgbPlay,
112 					 RGB rgbLines[]){
113 		TGConfigManager config = TuxGuitar.instance().getConfig();
114 
115 		config.setProperty(TGConfigKeys.MATRIX_FONT,fontData);
116 		config.setProperty(TGConfigKeys.MATRIX_COLOR_FOREGROUND,rgbForeground);
HasTextAt(const Comment * C,size_t Idx,StringRef Text)117 		config.setProperty(TGConfigKeys.MATRIX_COLOR_BORDER,rgbBorder);
118 		config.setProperty(TGConfigKeys.MATRIX_COLOR_POSITION,rgbPosition);
119 		config.setProperty(TGConfigKeys.MATRIX_COLOR_NOTE,rgbNote);
120 		config.setProperty(TGConfigKeys.MATRIX_COLOR_PLAY_NOTE,rgbPlay);
121 		config.setProperty(TGConfigKeys.MATRIX_COLOR_LINE_1,rgbLines[0]);
122 		config.setProperty(TGConfigKeys.MATRIX_COLOR_LINE_2,rgbLines[1]);
123 		config.setProperty(TGConfigKeys.MATRIX_COLOR_LINE_3,rgbLines[2]);
124 	}
125 
126 	public void dispose(){
127 		dispose(this.font);
128 		dispose(this.colorForeground);
129 		dispose(this.colorBorder);
130 		dispose(this.colorPosition);
131 		dispose(this.colorNote);
132 		dispose(this.colorPlay);
133 		dispose(this.colorLines);
134 	}
135 
136 	protected void dispose(Resource[] resources){
137 		if(resources != null){
HasTextWithNewlineAt(const Comment * C,size_t Idx,StringRef Text)138 			for(int i = 0; i < resources.length; i ++){
139 				dispose(resources[i]);
140 			}
141 		}
142 	}
143 
144 	protected void dispose(Resource resource){
145 		if(resource != null){
146 			resource.dispose();
147 		}
148 	}
149 
150 	private static final int MINIMUM_CONTROL_WIDTH = 180;
151 	private static final int MINIMUM_BUTTON_WIDTH = 80;
152 	private static final int MINIMUM_BUTTON_HEIGHT = 25;
153 
154 	public void configure(Shell shell) {
155 		final Shell dialog = DialogUtils.newDialog(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
156 		dialog.setLayout(new GridLayout());
157 		dialog.setText(TuxGuitar.getProperty("matrix.settings"));
158 
HasBlockCommandAt(const Comment * C,const CommandTraits & Traits,size_t Idx,BlockCommandComment * & BCC,StringRef Name,ParagraphComment * & Paragraph)159 		// ----------------------------------------------------------------------
160 		Group group = new Group(dialog,SWT.SHADOW_ETCHED_IN);
161 		group.setLayout(new GridLayout(2, false));
162 		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
163 		group.setText(TuxGuitar.getProperty("matrix.settings"));
164 
165 		// fonts
166 		final FontData fontData = getFontChooser(group,TuxGuitar.getProperty("matrix.font"),this.font.getFontData()[0]);
167 
168 		// colors
169 		final RGB rgbForeground = getColorChooser(group,TuxGuitar.getProperty("matrix.foreground-color"), this.colorForeground.getRGB());
170 
171 		final RGB rgbLines[] = new RGB[]{
172 				getColorChooser(group,TuxGuitar.getProperty("matrix.line-color-1"), this.colorLines[0].getRGB()),
173 				getColorChooser(group,TuxGuitar.getProperty("matrix.line-color-2"), this.colorLines[1].getRGB()),
174 				getColorChooser(group,TuxGuitar.getProperty("matrix.line-color-over"), this.colorLines[2].getRGB()),
175 		};
176 
177 		final RGB rgbBorder = getColorChooser(group,TuxGuitar.getProperty("matrix.border-color"), this.colorBorder.getRGB());
178 		final RGB rgbPosition = getColorChooser(group,TuxGuitar.getProperty("matrix.position-color"), this.colorPosition.getRGB());
179 		final RGB rgbNote = getColorChooser(group,TuxGuitar.getProperty("matrix.note-color"), this.colorNote.getRGB());
HasParamCommandAt(const Comment * C,const CommandTraits & Traits,size_t Idx,ParamCommandComment * & PCC,StringRef CommandName,ParamCommandComment::PassDirection Direction,bool IsDirectionExplicit,StringRef ParamName,ParagraphComment * & Paragraph)180 		final RGB rgbPlay = getColorChooser(group,TuxGuitar.getProperty("matrix.play-note-color"), this.colorPlay.getRGB());
181 
182 		// ------------------BUTTONS--------------------------
183 		Composite buttons = new Composite(dialog, SWT.NONE);
184 		buttons.setLayout(new GridLayout(3, false));
185 		buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, true));
186 
187 		final Button buttonDefaults = new Button(buttons, SWT.PUSH);
188 		buttonDefaults.setText(TuxGuitar.getProperty("defaults"));
189 		buttonDefaults.setLayoutData(getButtonData());
190 		buttonDefaults.addSelectionListener(new SelectionAdapter() {
191 			public void widgetSelected(SelectionEvent arg0) {
192 				dialog.dispose();
193 				defaults();
194 				applyChanges();
195 			}
196 		});
197 
198 		final Button buttonOK = new Button(buttons, SWT.PUSH);
199 		buttonOK.setText(TuxGuitar.getProperty("ok"));
200 		buttonOK.setLayoutData(getButtonData());
201 		buttonOK.addSelectionListener(new SelectionAdapter() {
202 			public void widgetSelected(SelectionEvent arg0) {
203 				dialog.dispose();
204 				save(fontData, rgbForeground, rgbBorder, rgbPosition, rgbNote, rgbPlay, rgbLines);
205 				applyChanges();
206 			}
207 		});
208 
209 		Button buttonCancel = new Button(buttons, SWT.PUSH);
210 		buttonCancel.setText(TuxGuitar.getProperty("cancel"));
211 		buttonCancel.setLayoutData(getButtonData());
212 		buttonCancel.addSelectionListener(new SelectionAdapter() {
213 			public void widgetSelected(SelectionEvent arg0) {
214 				dialog.dispose();
215 			}
216 		});
217 
218 		dialog.setDefaultButton( buttonOK );
219 
220 		DialogUtils.openDialog(dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
221 	}
222 
223 	protected GridData getButtonData(){
224 		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
225 		data.minimumWidth = MINIMUM_BUTTON_WIDTH;
226 		data.minimumHeight = MINIMUM_BUTTON_HEIGHT;
227 		return data;
HasTParamCommandAt(const Comment * C,const CommandTraits & Traits,size_t Idx,TParamCommandComment * & TPCC,StringRef CommandName,StringRef ParamName,ParagraphComment * & Paragraph)228 	}
229 
230 	protected void applyChanges(){
231 		this.dispose();
232 		this.load();
233 	}
234 
235 	private RGB getColorChooser(final Composite parent,String title,RGB rgb){
236 		Label label = new Label(parent, SWT.NULL);
237 		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
238 		label.setText(title);
239 
240 		ButtonColor button = new ButtonColor(parent, SWT.PUSH, TuxGuitar.getProperty("choose"));
241 		button.setLayoutData(getAlignmentData(MINIMUM_CONTROL_WIDTH,SWT.FILL));
242 		button.loadColor(rgb);
243 
244 		return button.getValue();
245 	}
246 
247 	private FontData getFontChooser(final Composite parent,String title,FontData fontData){
248 		final FontData selection = new FontData(fontData.getName(),fontData.getHeight(),fontData.getStyle());
249 
250 		Label label = new Label(parent, SWT.NULL);
251 		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
252 		label.setText(title);
253 
254 		Button button = new Button(parent, SWT.PUSH);
255 		button.setLayoutData(getAlignmentData(MINIMUM_CONTROL_WIDTH,SWT.FILL));
256 		button.setText(TuxGuitar.getProperty("choose"));
257 		button.addSelectionListener(new SelectionAdapter() {
258 			public void widgetSelected(SelectionEvent arg0) {
259 				Font font = new Font(parent.getDisplay(),selection);
260 				FontDialog fontDialog = new FontDialog(parent.getShell());
261 				fontDialog.setFontList(font.getFontData());
HasInlineCommandAt(const Comment * C,const CommandTraits & Traits,size_t Idx,InlineCommandComment * & ICC,StringRef Name)262 				FontData fd = fontDialog.open();
263 				if(fd != null){
264 					selection.setName( fd.getName() );
265 					selection.setHeight( fd.getHeight() );
266 					selection.setStyle( fd.getStyle() );
267 				}
268 				font.dispose();
269 			}
270 		});
271 		return selection;
272 	}
273 
274 	private GridData getAlignmentData(int minimumWidth,int horizontalAlignment){
275 		GridData data = new GridData();
276 		data.minimumWidth = minimumWidth;
277 		data.horizontalAlignment = horizontalAlignment;
278 		data.verticalAlignment = SWT.DEFAULT;
279 		data.grabExcessHorizontalSpace = true;
280 		data.grabExcessVerticalSpace = true;
281 		return data;
HasInlineCommandAt(const Comment * C,const CommandTraits & Traits,size_t Idx,InlineCommandComment * & ICC,StringRef Name,NoArgs)282 	}
283 
284 	private class ButtonColor {
285 		protected Button button;
286 		protected Color color;
287 		protected RGB value;
288 
289 		public ButtonColor(Composite parent, int style, String text){
290 			this.value = new RGB(0,0,0);
291 			this.button = new Button(parent, style);
292 			this.button.setText(text);
293 			this.addListeners();
294 		}
295 
296 		protected void setLayoutData(Object layoutData){
297 			this.button.setLayoutData(layoutData);
298 		}
299 
300 		protected void loadColor(RGB rgb){
301 			this.value.red = rgb.red;
302 			this.value.green = rgb.green;
303 			this.value.blue = rgb.blue;
304 
305 			Color color = new Color(this.button.getDisplay(), this.value);
306 			this.button.setForeground(color);
307 			this.disposeColor();
308 			this.color = color;
309 		}
310 
311 		protected void disposeColor(){
312 			if(this.color != null && !this.color.isDisposed()){
313 				this.color.dispose();
314 				this.color = null;
315 			}
316 		}
317 
318 		private void addListeners(){
319 			this.button.addSelectionListener(new SelectionAdapter() {
320 				public void widgetSelected(SelectionEvent event) {
321 					ColorDialog dlg = new ColorDialog(ButtonColor.this.button.getShell());
322 					dlg.setRGB(ButtonColor.this.value);
323 					dlg.setText(TuxGuitar.getProperty("choose-color"));
324 					RGB result = dlg.open();
325 					if (result != null) {
326 						ButtonColor.this.loadColor(result);
327 					}
328 				}
329 			});
330 			this.button.addDisposeListener(new DisposeListener() {
331 				public void widgetDisposed(DisposeEvent e) {
332 					ButtonColor.this.disposeColor();
333 				}
334 			});
335 		}
336 
337 		protected RGB getValue(){
338 			return this.value;
339 		}
340 	}
341 }
342