Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb7ad8fa0dc6fac91f7e822d3532e19d8adad70f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.common.preferences;

import org.eclipse.etrice.ui.common.Activator;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class ETricePreferencePage
	extends FieldEditorPreferencePage
	implements IWorkbenchPreferencePage {

	private BooleanFieldEditor snap;
	private StringFieldEditor hor;
	private StringFieldEditor ver;
	private Button useGridCheck;

	public ETricePreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("eTrice Preferences");
	}
	
	public void createFieldEditors() {
		addLabel("");
		addLabel("Textual Model Editor");
		BooleanFieldEditor autoSaveText = new BooleanFieldEditor(
				PreferenceConstants.SAVE_TEXT_ON_FOCUS_LOST,
				"Auto-save textual model when focus changes",
				getFieldEditorParent());
		addField(autoSaveText);
		
		addLabel("");
		addLabel("Diagram Editors");
		addField(
				new BooleanFieldEditor(
					PreferenceConstants.CONFIRM_DELETE,
					"&Confirm diagram element deletion",
					getFieldEditorParent()));
		
		BooleanFieldEditor autoSave = new BooleanFieldEditor(
				PreferenceConstants.SAVE_DIAG_ON_FOCUS_LOST,
				"Auto-save diagram when focus changes",
				getFieldEditorParent());
		addField(autoSave);

		StringFieldEditor exportPath = new StringFieldEditor(PreferenceConstants.EXPORT_DIAGRAM_PATH, "&Export Diagram Path:", getFieldEditorParent());
		addField(exportPath);

		RadioGroupFieldEditor exportRelTo = new RadioGroupFieldEditor(
				PreferenceConstants.EXPORT_DIAGRAM_PATH_RELATIVE_TO,
				"Export Path Relative to",
				2,
				new String[][] {
					{"Project", PreferenceConstants.PATH_REL_TO_PROJECT},
					{"Model", PreferenceConstants.PATH_REL_TO_MODEL}
				},
				getFieldEditorParent(),
				true);
		addField(exportRelTo);

		RadioGroupFieldEditor imgFormat = new RadioGroupFieldEditor(
				PreferenceConstants.EXPORT_DIAGRAM_FORMAT,
				"Image Format for Export:",
				4,
				new String[][] {
					{"BMP", PreferenceConstants.FORMAT_BMP},
					{"GIF", PreferenceConstants.FORMAT_GIF},
					{"JPG", PreferenceConstants.FORMAT_JPG},
					{"PNG", PreferenceConstants.FORMAT_PNG}
				},
				getFieldEditorParent(),
				true);
		addField(imgFormat);
		
		BooleanFieldEditor useGrid = new BooleanFieldEditor(
			PreferenceConstants.USE_GRID,
			"Use &Grid (grid settings apply to new diagrams only)",
			getFieldEditorParent());
		addField(useGrid);
		
		snap = new BooleanFieldEditor(
			PreferenceConstants.SNAP_TO_GRID,
			"&Snap to Grid",
			getFieldEditorParent());
		addField(snap);
		
		hor = new StringFieldEditor(PreferenceConstants.HOR_GRID_UNIT, "&Horizontal Grid Unit [px]:", getFieldEditorParent());
		addField(hor);
		
		ver = new StringFieldEditor(PreferenceConstants.VER_GRID_UNIT, "&Vertical Grid Unit [px]:", getFieldEditorParent());
		addField(ver);

		useGridCheck = (Button) useGrid.getDescriptionControl(getFieldEditorParent());
		useGridCheck.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateGridFields();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				updateGridFields();
			}
		});
	}

	@Override
	protected void initialize() {
		super.initialize();
		updateGridFields();
	}
	
	private void updateGridFields() {
		boolean enabled = useGridCheck.getSelection();
		snap.setEnabled(enabled, getFieldEditorParent());
		hor.setEnabled(enabled, getFieldEditorParent());
		ver.setEnabled(enabled, getFieldEditorParent());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}

	private void addLabel(String message) {
		Label label = new Label(getFieldEditorParent(), SWT.None);
		GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false, 2, 1);
		label.setLayoutData(data);
		//label.setForeground(ColorConstants.red);
		if (message != null)
			label.setText(message);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk()
	 */
	@Override
	public boolean performOk() {
		boolean ok = super.performOk();
		
		if (ok) {
			// TODO: apply to open editors
//			IPreferenceStore store = Activator.getDefault().getPreferenceStore();
//			boolean snap = store.getBoolean(PreferenceConstants.SNAP_TO_GRID);
//			boolean useGrid = store.getBoolean(PreferenceConstants.USE_GRID);
//			int horUnit = useGrid? store.getInt(PreferenceConstants.HOR_GRID_UNIT) : 0;
//			int verUnit = useGrid? store.getInt(PreferenceConstants.VER_GRID_UNIT) : 0;
//
//			IWorkbench wb = PlatformUI.getWorkbench();
//			IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
//			IWorkbenchPage page = win.getActivePage();
//			IEditorReference[] editorReferences = page.getEditorReferences();
//			for (IEditorReference ref : editorReferences) {
//				IEditorPart editor = ref.getEditor(true);
//				if (editor instanceof RoomDiagramEditor) {
//					Diagram diagram = ((DiagramEditorInput)editor.getEditorInput()).getDiagram();
//					diagram.setSnapToGrid(snap);
//					diagram.setGridUnit(horUnit);
//					diagram.setVerticalGridUnit(verUnit);
//				}
//			}
		}
		
		return ok;
	}
}

Back to the top