Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 968d39720a3e5626eec177e1d229fa0e5e89e79e (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
/**********************************************************************
 * Copyright (c) 2004 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 ***********************************************************************/ 
package org.eclipse.cdt.debug.internal.ui.preferences; 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.internal.ui.ICDebugHelpContextIds;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.AddContainerAction;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.DownAction;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.EditContainerAction;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.RemoveAction;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.SourceContainerAction;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.SourceContainerViewer;
import org.eclipse.cdt.debug.internal.ui.sourcelookup.UpAction;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * The "Source Lookup Path" preference page.
 */
public class SourcePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	private SourceContainerViewer fPathViewer;
	private List fActions = new ArrayList(6);
	private IWorkbench fWorkbench;
	private AddContainerAction fAddAction; 
	private EditContainerAction fEditAction;

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createContents( Composite parent ) {
		Font font = parent.getFont();
		Composite comp = new Composite( parent, SWT.NONE );
		GridLayout topLayout = new GridLayout();
		topLayout.numColumns = 2;
		comp.setLayout( topLayout );
		GridData gd = new GridData( GridData.FILL_BOTH );
		comp.setLayoutData( gd );
		Label viewerLabel = new Label( comp, SWT.LEFT );
		viewerLabel.setText( PreferenceMessages.getString( "SourcePreferencePage.0" ) ); //$NON-NLS-1$
		gd = new GridData( GridData.HORIZONTAL_ALIGN_FILL );
		gd.horizontalSpan = 2;
		viewerLabel.setLayoutData( gd );
		viewerLabel.setFont( font );
		fPathViewer = new SourceContainerViewer( comp );
		gd = new GridData( GridData.FILL_BOTH );
		fPathViewer.getControl().setLayoutData( gd );
		fPathViewer.getControl().setFont( font );
		Composite pathButtonComp = new Composite( comp, SWT.NONE );
		GridLayout pathButtonLayout = new GridLayout();
		pathButtonLayout.marginHeight = 0;
		pathButtonLayout.marginWidth = 0;
		pathButtonComp.setLayout( pathButtonLayout );
		gd = new GridData( GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL );
		pathButtonComp.setLayoutData( gd );
		pathButtonComp.setFont( font );
		createVerticalSpacer( comp, 2 );
		GC gc = new GC( parent );
		gc.setFont( parent.getFont() );
		FontMetrics fontMetrics = gc.getFontMetrics();
		gc.dispose();
		fAddAction = new AddContainerAction();
		Button button = createPushButton( pathButtonComp, fAddAction.getText(), fontMetrics );
		fAddAction.setButton( button );
		addAction( fAddAction );
		fEditAction = new EditContainerAction();
		button = createPushButton( pathButtonComp, fEditAction.getText(), fontMetrics );
		fEditAction.setButton( button );
		addAction( fEditAction );
		SourceContainerAction action = new RemoveAction();
		button = createPushButton( pathButtonComp, action.getText(), fontMetrics );
		action.setButton( button );
		addAction( action );
		action = new UpAction();
		button = createPushButton( pathButtonComp, action.getText(), fontMetrics );
		action.setButton( button );
		addAction( action );
		action = new DownAction();
		button = createPushButton( pathButtonComp, action.getText(), fontMetrics );
		action.setButton( button );
		addAction( action );
		retargetActions( fPathViewer );
		Dialog.applyDialogFont( comp );
		getWorkbench().getHelpSystem().setHelp( comp, ICDebugHelpContextIds.SOURCE_PREFERENCE_PAGE );
		initialize();
		return comp;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init( IWorkbench workbench ) {
		fWorkbench = workbench;
	}

	private Button createPushButton( Composite parent, String label, FontMetrics fontMetrics ) {
		Button button = new Button( parent, SWT.PUSH );
		button.setFont( parent.getFont() );
		button.setText( label );
		GridData gd = getButtonGridData( button, fontMetrics );
		button.setLayoutData( gd );
		return button;
	}

	private GridData getButtonGridData( Button button, FontMetrics fontMetrics ) {
		GridData gd = new GridData( GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING );
		int widthHint = Dialog.convertHorizontalDLUsToPixels( fontMetrics, IDialogConstants.BUTTON_WIDTH );
		gd.widthHint = Math.max( widthHint, button.computeSize( SWT.DEFAULT, SWT.DEFAULT, true ).x );
		return gd;
	}

	private IWorkbench getWorkbench() {
		return fWorkbench;
	}

	private void addAction( SourceContainerAction action ) {
		fActions.add( action );
	}

	private void retargetActions( SourceContainerViewer viewer ) {
		Iterator actions = fActions.iterator();
		while( actions.hasNext() ) {
			SourceContainerAction action = (SourceContainerAction)actions.next();
			action.setViewer( viewer );
		}
	}

	private void createVerticalSpacer( Composite comp, int colSpan ) {
		Label label = new Label( comp, SWT.NONE );
		GridData gd = new GridData();
		gd.horizontalSpan = colSpan;
		label.setLayoutData( gd );
	}

	private void initialize() {
		ISourceLookupDirector director = CDebugCorePlugin.getDefault().getCommonSourceLookupDirector();
		fPathViewer.setEntries( director.getSourceContainers() );
		fAddAction.setSourceLookupDirector( director );
		fEditAction.setSourceLookupDirector( director );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
	 */
	protected void performDefaults() {
		fPathViewer.setEntries( new ISourceContainer[0] );
		super.performDefaults();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performOk()
	 */
	public boolean performOk() {
		CDebugCorePlugin.getDefault().getCommonSourceLookupDirector().setSourceContainers( fPathViewer.getEntries() );
		CDebugCorePlugin.getDefault().savePluginPreferences();
		return true;
	}
}

Back to the top