Skip to content

Commit 093acc5

Browse files
committed
Introduce new layout utility
Replace LayoutUtil with a more universal layout utility that allows to configure SWT layouts and layout data in a fluent style. This saves a lot of code and hopefully makes layout code easier to read and to write. Example using static imports: gridLayout( composite ).spacing( 5 ); gridData( control ).fillBoth().span( 2, 1 ); This approach seems like a good alternative to writing dedicated helper methods like createGridDataWithHorizontalSpan( int ) etc.
1 parent 84e453d commit 093acc5

File tree

15 files changed

+934
-157
lines changed

15 files changed

+934
-157
lines changed

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/preferences/ui/ConfigPreferencePage.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import com.eclipsesource.jshint.ui.internal.builder.JSHintBuilder;
3131
import com.eclipsesource.jshint.ui.internal.preferences.OptionsPreferences;
3232
import com.eclipsesource.jshint.ui.internal.preferences.PreferencesFactory;
33-
import com.eclipsesource.jshint.ui.internal.util.LayoutUtil;
3433

3534
import static com.eclipsesource.jshint.ui.internal.util.JsonUtil.jsonEquals;
36-
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.createGridDataFillWithMinSize;
35+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.gridData;
36+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.gridLayout;
3737

3838

3939
public class ConfigPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
@@ -50,24 +50,33 @@ public void init( IWorkbench workbench ) {
5050

5151
@Override
5252
protected Control createContents( Composite parent ) {
53-
Composite composite = LayoutUtil.createMainComposite( parent );
54-
createConfigText( composite );
53+
Composite composite = new Composite( parent, SWT.NONE );
54+
Control labelPart = createLabelPart( composite );
55+
Control configTextPart = createConfigTextPart( composite );
56+
gridData( composite ).fillBoth();
57+
gridLayout( composite ).spacing( 3 );
58+
gridData( labelPart ).fillHorizontal().widthHint( 360 );
59+
gridData( configTextPart ).fillBoth().sizeHint( 360, 180 );
5560
loadPreferences();
5661
return composite;
5762
}
5863

59-
private void createConfigText( Composite composite ) {
60-
Link link = new Link( composite, SWT.WRAP );
64+
private Control createLabelPart( Composite parent ) {
65+
Link link = new Link( parent, SWT.WRAP );
6166
link.setText( "For syntax, see <a>http://www.jshint.com/docs/</a>." );
6267
BrowserSupport.INSTANCE.enableHyperlinks( link );
63-
configEditor = new ConfigEditor( composite ) {
68+
return link;
69+
}
70+
71+
private Control createConfigTextPart( Composite parent ) {
72+
configEditor = new ConfigEditor( parent ) {
6473
@Override
6574
public void handleError( String message ) {
6675
setErrorMessage( message );
6776
setValid( message == null );
6877
}
6978
};
70-
configEditor.getControl().setLayoutData( createGridDataFillWithMinSize( 360, 180 ) );
79+
return configEditor.getControl();
7180
}
7281

7382
private void loadPreferences() {

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/preferences/ui/ConfigPropertyPage.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
import static com.eclipsesource.jshint.ui.internal.util.IOUtil.readFileUtf8;
3434
import static com.eclipsesource.jshint.ui.internal.util.IOUtil.writeFileUtf8;
35-
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.*;
35+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.gridData;
36+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.gridLayout;
3637

3738

3839
public class ConfigPropertyPage extends AbstractPropertyPage {
@@ -43,30 +44,40 @@ public class ConfigPropertyPage extends AbstractPropertyPage {
4344

4445
@Override
4546
protected Control createContents( Composite parent ) {
46-
Composite composite = createMainComposite( parent );
47-
createProjectSpecificSection( composite );
48-
createConfigText( composite );
47+
Composite composite = new Composite( parent, SWT.NONE );
48+
Control projectSpecificPart = createProjectSpecificPart( composite );
49+
Control labelPart = createLabelPart( composite );
50+
Control configTextPart = createConfigTextPart( composite );
51+
gridData( composite ).fillBoth();
52+
gridLayout( composite ).spacing( 3 );
53+
gridData( projectSpecificPart );
54+
gridData( labelPart ).fillHorizontal().widthHint( 360 );
55+
gridData( configTextPart ).fillBoth().sizeHint( 360, 180 );
4956
loadPreferences();
5057
return composite;
5158
}
5259

53-
private void createProjectSpecificSection( Composite parent ) {
54-
projectSpecificCheckbox = new Button( parent, SWT.CHECK );
55-
projectSpecificCheckbox.setText( "Enable project specific configuration" );
56-
projectSpecificCheckbox.addListener( SWT.Selection, new Listener() {
57-
public void handleEvent( Event event ) {
58-
prefsChanged();
59-
}
60-
} );
61-
Link link = new Link( parent, SWT.WRAP );
62-
link.setText( "The project specific configuration will be read from a file named .jshintrc"
63-
+ " in the project root."
64-
+ " For the syntax of this file, see <a>http://www.jshint.com/docs/</a>." );
65-
BrowserSupport.INSTANCE.enableHyperlinks( link );
66-
link.setLayoutData( createGridDataHFillWithMinWidth( 360 ) );
67-
}
60+
private Control createProjectSpecificPart( Composite parent ) {
61+
projectSpecificCheckbox = new Button( parent, SWT.CHECK );
62+
projectSpecificCheckbox.setText( "Enable project specific configuration" );
63+
projectSpecificCheckbox.addListener( SWT.Selection, new Listener() {
64+
public void handleEvent( Event event ) {
65+
prefsChanged();
66+
}
67+
});
68+
return projectSpecificCheckbox;
69+
}
70+
71+
private Control createLabelPart( Composite parent ) {
72+
Link link = new Link( parent, SWT.WRAP );
73+
link.setText( "The project specific configuration will be read from a file named .jshintrc"
74+
+ " in the project root."
75+
+ " For the syntax of this file, see <a>http://www.jshint.com/docs/</a>." );
76+
BrowserSupport.INSTANCE.enableHyperlinks( link );
77+
return link;
78+
}
6879

69-
private void createConfigText( Composite parent ) {
80+
private Control createConfigTextPart( Composite parent ) {
7081
configEditor = new ConfigEditor( parent ) {
7182
@Override
7283
public void handleError( String message ) {
@@ -76,7 +87,7 @@ public void handleError( String message ) {
7687
}
7788
}
7889
};
79-
configEditor.getControl().setLayoutData( createGridDataFillWithMinSize( 360, 180 ) );
90+
return configEditor.getControl();
8091
}
8192

8293
@Override

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/preferences/ui/IncludesView.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@
2020
import org.eclipse.swt.events.SelectionAdapter;
2121
import org.eclipse.swt.events.SelectionEvent;
2222
import org.eclipse.swt.graphics.Image;
23-
import org.eclipse.swt.layout.GridData;
24-
import org.eclipse.swt.layout.GridLayout;
25-
import org.eclipse.swt.layout.RowLayout;
2623
import org.eclipse.swt.widgets.Button;
2724
import org.eclipse.swt.widgets.Composite;
2825
import org.eclipse.swt.widgets.Display;
2926
import org.eclipse.swt.widgets.Event;
3027
import org.eclipse.swt.widgets.Label;
31-
import org.eclipse.swt.widgets.Layout;
3228
import org.eclipse.swt.widgets.Listener;
3329
import org.eclipse.swt.widgets.Table;
3430
import org.eclipse.swt.widgets.TableItem;
@@ -37,6 +33,8 @@
3733

3834
import com.eclipsesource.jshint.ui.internal.preferences.EnablementPreferences;
3935

36+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.*;
37+
4038

4139
public class IncludesView extends Composite {
4240

@@ -46,7 +44,7 @@ public class IncludesView extends Composite {
4644

4745
public IncludesView( Composite parent, int style, IProject project ) {
4846
super( parent, style );
49-
super.setLayout( createGridLayout( 2, false ) );
47+
gridLayout( this ).columns( 2 ).spacing( 5, 3 );
5048
createImages();
5149
createIncludeControls();
5250
createExcludeControls();
@@ -71,11 +69,6 @@ public void storePreferences( EnablementPreferences preferences ) {
7169
preferences.setExcludePatterns( excludePatterns );
7270
}
7371

74-
@Override
75-
public void setLayout( Layout layout ) {
76-
// prevent changing the default layout
77-
}
78-
7972
private void createImages() {
8073
Display display = getDisplay();
8174
ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
@@ -85,19 +78,19 @@ private void createImages() {
8578
private void createIncludeControls() {
8679
Label label = new Label( this, SWT.NONE );
8780
label.setText( "Enable JSHint for these files and folders:" );
88-
label.setLayoutData( createHorSpanData() );
81+
gridData( label ).span( 2, 1 );
8982
includeTable = new Table( this, SWT.BORDER );
90-
includeTable.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
83+
gridData( includeTable ).fillBoth();
9184
createButtonsBar( includeTable );
9285
addListeners( includeTable );
9386
}
9487

9588
private void createExcludeControls() {
9689
Label label = new Label( this, SWT.NONE );
9790
label.setText( "But exclude these files and folders from validation:" );
98-
label.setLayoutData( createHorSpanData() );
91+
gridData( label ).span( 2, 1 );
9992
excludeTable = new Table( this, SWT.BORDER );
100-
excludeTable.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
93+
gridData( excludeTable ).fillBoth();
10194
createButtonsBar( excludeTable );
10295
addListeners( excludeTable );
10396
}
@@ -119,7 +112,7 @@ public void handleEvent( Event event ) {
119112

120113
private void createButtonsBar( final Table table ) {
121114
Composite buttons = new Composite( this, SWT.NONE );
122-
buttons.setLayout( createButtonsLayout() );
115+
rowLayout( buttons ).vertical().fill( true ).spacing( 3 );
123116
createAddButton( table, buttons );
124117
createEditButton( table, buttons );
125118
createRemoveButton( table, buttons );
@@ -266,23 +259,4 @@ private static ArrayList<String> getPatterns( Table table ) {
266259
return result;
267260
}
268261

269-
private static RowLayout createButtonsLayout() {
270-
RowLayout layout = new RowLayout( SWT.VERTICAL );
271-
layout.fill = true;
272-
return layout;
273-
}
274-
275-
private static GridLayout createGridLayout( int numColumns, boolean makeColumnsEqualWidth ) {
276-
GridLayout layout = new GridLayout( numColumns, makeColumnsEqualWidth );
277-
layout.marginHeight = 0;
278-
layout.marginWidth = 0;
279-
return layout;
280-
}
281-
282-
private static GridData createHorSpanData() {
283-
GridData labelData = new GridData();
284-
labelData.horizontalSpan = 2;
285-
return labelData;
286-
}
287-
288262
}

bundles/com.eclipsesource.jshint.ui/src/com/eclipsesource/jshint/ui/internal/preferences/ui/JSHintPreferencePage.java

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012, 2013 EclipseSource.
2+
* Copyright (c) 2012, 2013 EclipseSource and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -27,8 +27,6 @@
2727
import org.eclipse.swt.events.ModifyListener;
2828
import org.eclipse.swt.events.SelectionAdapter;
2929
import org.eclipse.swt.events.SelectionEvent;
30-
import org.eclipse.swt.layout.GridData;
31-
import org.eclipse.swt.layout.GridLayout;
3230
import org.eclipse.swt.widgets.Button;
3331
import org.eclipse.swt.widgets.Composite;
3432
import org.eclipse.swt.widgets.Control;
@@ -44,6 +42,9 @@
4442
import com.eclipsesource.jshint.ui.internal.builder.JSHintBuilder;
4543
import com.eclipsesource.jshint.ui.internal.preferences.JSHintPreferences;
4644

45+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.gridData;
46+
import static com.eclipsesource.jshint.ui.internal.util.LayoutUtil.gridLayout;
47+
4748

4849
public class JSHintPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
4950

@@ -70,9 +71,7 @@ protected IPreferenceStore doGetPreferenceStore() {
7071
@Override
7172
protected Control createContents( Composite parent ) {
7273
Composite composite = new Composite( parent, SWT.NONE );
73-
GridLayout mainLayout = createMainLayout();
74-
mainLayout.marginTop = 10;
75-
composite.setLayout( mainLayout );
74+
gridLayout( composite ).columns( 3 ).marginTop( 10 );
7675
createCustomJSHintArea( composite );
7776
updateControls();
7877
return composite;
@@ -104,10 +103,10 @@ private void createCustomJSHintArea( Composite parent ) {
104103
defaultLibButton.setText( "Use the &built-in JSHint library (version "
105104
+ JSHint.getDefaultLibraryVersion()
106105
+ ")" );
107-
defaultLibButton.setLayoutData( createFillData( 3 ) );
106+
gridData( defaultLibButton ).fillBoth().span( 3, 1 );
108107
customLibButton = new Button( parent, SWT.RADIO );
109108
customLibButton.setText( "Provide a &custom JSHint library file" );
110-
customLibButton.setLayoutData( createFillData( 3 ) );
109+
gridData( customLibButton ).fillBoth().span( 3, 1 );
111110
customLibButton.addSelectionListener( new SelectionAdapter() {
112111
@Override
113112
public void widgetSelected( SelectionEvent e ) {
@@ -116,9 +115,7 @@ public void widgetSelected( SelectionEvent e ) {
116115
}
117116
} );
118117
customLibPathText = new Text( parent, SWT.BORDER );
119-
GridData textData = createFillData( 2 );
120-
textData.horizontalIndent = 25;
121-
customLibPathText.setLayoutData( textData );
118+
gridData( customLibPathText ).fillBoth().span( 2, 1 ).indent( 25, 0 );
122119
customLibPathText.addModifyListener( new ModifyListener() {
123120
public void modifyText( ModifyEvent e ) {
124121
updateValuesFromControls();
@@ -135,9 +132,7 @@ public void widgetSelected( SelectionEvent e ) {
135132
Text customLibPathLabelText = new Text( parent, SWT.READ_ONLY | SWT.WRAP );
136133
customLibPathLabelText.setText( "This file is usually named 'jshint.js'." );
137134
customLibPathLabelText.setBackground( parent.getBackground() );
138-
GridData labelTextData = createFillData( 2 );
139-
labelTextData.horizontalIndent = 25;
140-
customLibPathLabelText.setLayoutData( labelTextData );
135+
gridData( customLibPathLabelText ).fillBoth().span( 2, 1 ).indent( 25, 1 );
141136
}
142137

143138
private void selectFile() {
@@ -237,17 +232,4 @@ private void triggerRebuild() throws CoreException {
237232
}
238233
}
239234

240-
private static GridLayout createMainLayout() {
241-
GridLayout layout = new GridLayout( 3, false );
242-
layout.marginWidth = 0;
243-
layout.marginHeight = 0;
244-
return layout;
245-
}
246-
247-
private static GridData createFillData( int span ) {
248-
GridData data = new GridData( SWT.FILL, SWT.CENTER, true, false );
249-
data.horizontalSpan = span;
250-
return data;
251-
}
252-
253235
}

0 commit comments

Comments
 (0)