Skip to content

Commit 554bd81

Browse files
xpomulvogella
authored andcommitted
Split table creation and configuration logic for QuickAccess
Fixes #3742 by splitting createTable() into the table creation logic and the table configuration logic. The former is executed as it was, but the latter is postponed until after the Dialog Font preferences have been applied. Signed-off-by: Stefan Winkler <stefan@winklerweb.net>
1 parent 7324f31 commit 554bd81

2 files changed

Lines changed: 78 additions & 65 deletions

File tree

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/quickaccess/QuickAccessContents.java

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Lars Vogel <Lars.Vogel@vogella.com> - Bug 472654, 491272, 491398
1515
* Leung Wang Hei <gemaspecial@yahoo.com.hk> - Bug 483343
1616
* Patrik Suzzi <psuzzi@gmail.com> - Bug 491291, 491529, 491293, 492434, 492452, 459989, 507322
17+
* Stefan Winkler <stefan@winklerweb.net> - Bug #3742
1718
*******************************************************************************/
1819
package org.eclipse.ui.internal.quickaccess;
1920

@@ -57,11 +58,10 @@
5758
import org.eclipse.swt.events.ControlEvent;
5859
import org.eclipse.swt.events.KeyEvent;
5960
import org.eclipse.swt.events.KeyListener;
60-
import org.eclipse.swt.events.MouseAdapter;
6161
import org.eclipse.swt.events.MouseEvent;
62+
import org.eclipse.swt.events.MouseListener;
6263
import org.eclipse.swt.events.MouseMoveListener;
63-
import org.eclipse.swt.events.SelectionAdapter;
64-
import org.eclipse.swt.events.SelectionEvent;
64+
import org.eclipse.swt.events.SelectionListener;
6565
import org.eclipse.swt.graphics.Color;
6666
import org.eclipse.swt.graphics.Font;
6767
import org.eclipse.swt.graphics.Point;
@@ -105,6 +105,7 @@ public abstract class QuickAccessContents {
105105
private Map<String, QuickAccessProvider> providerMap = new HashMap<>();
106106
private final Map<QuickAccessElement, QuickAccessProvider> elementsToProviders = new HashMap<>();
107107

108+
private Composite tableComposite;
108109
protected Table table;
109110
protected Label infoLabel;
110111

@@ -723,37 +724,18 @@ private void setHintTextToDisplay(boolean toDisplay) {
723724
}
724725

725726
/**
726-
* Creates the table providing the contents for the quick access dialog
727+
* Creates the table providing the contents for the quick access dialog. After
728+
* applying the dialog font, call {@link #installTableStyling(int)} to complete
729+
* the styling and layout for this table.
727730
*
728-
* @param composite parent composite with {@link GridLayout}
729-
* @param defaultOrientation the window orientation to use for the table
730-
* {@link SWT#RIGHT_TO_LEFT} or
731-
* {@link SWT#LEFT_TO_RIGHT}
731+
* @param composite parent composite with {@link GridLayout}
732732
* @return the created table
733733
*/
734-
public Table createTable(Composite composite, int defaultOrientation) {
734+
public Table createTable(Composite composite) {
735735
composite.addDisposeListener(e -> doDispose());
736-
Composite tableComposite = new Composite(composite, SWT.NONE);
736+
tableComposite = new Composite(composite, SWT.NONE);
737737
GridDataFactory.fillDefaults().grab(true, true).applyTo(tableComposite);
738-
TableColumnLayout tableColumnLayout = new TableColumnLayout();
739-
tableComposite.setLayout(tableColumnLayout);
740738
table = new Table(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION);
741-
textLayout = new TextLayout(table.getDisplay());
742-
textLayout.setOrientation(defaultOrientation);
743-
Font boldFont = resourceManager.create(FontDescriptor.createFrom(table.getFont()).setStyle(SWT.BOLD));
744-
textLayout.setFont(table.getFont());
745-
textLayout.setText(QuickAccessMessages.QuickAccess_AvailableCategories);
746-
int maxProviderWidth = (textLayout.getBounds().width);
747-
textLayout.setFont(boldFont);
748-
for (QuickAccessProvider provider : providers) {
749-
textLayout.setText(provider.getName());
750-
int width = (textLayout.getBounds().width);
751-
if (width > maxProviderWidth) {
752-
maxProviderWidth = width;
753-
}
754-
}
755-
tableColumnLayout.setColumnData(new TableColumn(table, SWT.NONE), new ColumnWeightData(0, maxProviderWidth));
756-
tableColumnLayout.setColumnData(new TableColumn(table, SWT.NONE), new ColumnWeightData(100, 100));
757739
table.getShell().addControlListener(new ControlAdapter() {
758740
@Override
759741
public void controlResized(ControlEvent e) {
@@ -772,42 +754,31 @@ public void controlResized(ControlEvent e) {
772754
}
773755
});
774756

775-
table.addKeyListener(new KeyListener() {
776-
@Override
777-
public void keyPressed(KeyEvent e) {
778-
if (e.keyCode == SWT.ARROW_UP && table.getSelectionIndex() == 0) {
779-
filterText.setFocus();
780-
} else if (e.character == SWT.ESC) {
781-
doClose();
782-
}
757+
table.addKeyListener(KeyListener.keyPressedAdapter(e -> {
758+
if (e.keyCode == SWT.ARROW_UP && table.getSelectionIndex() == 0) {
759+
filterText.setFocus();
760+
} else if (e.character == SWT.ESC) {
761+
doClose();
783762
}
763+
}));
784764

785-
@Override
786-
public void keyReleased(KeyEvent e) {
787-
// do nothing
765+
table.addMouseListener(MouseListener.mouseUpAdapter(e -> {
766+
if (table.getSelectionCount() < 1) {
767+
return;
788768
}
789-
});
790-
table.addMouseListener(new MouseAdapter() {
791-
@Override
792-
public void mouseUp(MouseEvent e) {
793-
794-
if (table.getSelectionCount() < 1) {
795-
return;
796-
}
797769

798-
if (e.button != 1) {
799-
return;
800-
}
770+
if (e.button != 1) {
771+
return;
772+
}
801773

802-
if (table.equals(e.getSource())) {
803-
Object o = table.getItem(new Point(e.x, e.y));
804-
TableItem selection = table.getSelection()[0];
805-
if (selection.equals(o)) {
806-
handleSelection();
807-
}
774+
if (table.equals(e.getSource())) {
775+
Object o = table.getItem(new Point(e.x, e.y));
776+
TableItem selection = table.getSelection()[0];
777+
if (selection.equals(o)) {
778+
handleSelection();
808779
}
809780
}
810-
});
781+
}));
811782

812783
table.addMouseMoveListener(new MouseMoveListener() {
813784
TableItem lastItem = null;
@@ -831,13 +802,44 @@ public void mouseMove(MouseEvent e) {
831802
}
832803
});
833804

834-
table.addSelectionListener(new SelectionAdapter() {
835-
@Override
836-
public void widgetDefaultSelected(SelectionEvent e) {
837-
handleSelection();
805+
table.addSelectionListener(SelectionListener.widgetDefaultSelectedAdapter(e -> handleSelection()));
806+
return table;
807+
}
808+
809+
/**
810+
* Complete the styling/layout for the table.
811+
*
812+
* This method must be called after {@link #createTable(Composite)} and after
813+
* the containing dialog implementation has applied the dialog fonts.
814+
*
815+
* @param defaultOrientation the window orientation to use for the table
816+
* {@link SWT#RIGHT_TO_LEFT} or
817+
* {@link SWT#LEFT_TO_RIGHT}
818+
*/
819+
public void configureTableStyling(int defaultOrientation) {
820+
// configure the text layout
821+
textLayout = new TextLayout(table.getDisplay());
822+
textLayout.setOrientation(defaultOrientation);
823+
Font boldFont = resourceManager.create(FontDescriptor.createFrom(table.getFont()).setStyle(SWT.BOLD));
824+
textLayout.setFont(table.getFont());
825+
textLayout.setText(QuickAccessMessages.QuickAccess_AvailableCategories);
826+
int maxProviderWidth = (textLayout.getBounds().width);
827+
textLayout.setFont(boldFont);
828+
for (QuickAccessProvider provider : providers) {
829+
textLayout.setText(provider.getName());
830+
int width = (textLayout.getBounds().width);
831+
if (width > maxProviderWidth) {
832+
maxProviderWidth = width;
838833
}
839-
});
834+
}
835+
836+
// configure the table layout
837+
TableColumnLayout tableColumnLayout = new TableColumnLayout();
838+
tableComposite.setLayout(tableColumnLayout);
839+
tableColumnLayout.setColumnData(new TableColumn(table, SWT.NONE), new ColumnWeightData(0, maxProviderWidth));
840+
tableColumnLayout.setColumnData(new TableColumn(table, SWT.NONE), new ColumnWeightData(100, 100));
840841

842+
// configure listeners
841843
final TextStyle boldStyle;
842844
if (PlatformUI.getPreferenceStore().getBoolean(IWorkbenchPreferenceConstants.USE_COLORED_LABELS)) {
843845
boldStyle = new TextStyle(boldFont, null, null);
@@ -865,8 +867,6 @@ public void widgetDefaultSelected(SelectionEvent e) {
865867
table.addListener(SWT.MeasureItem, listener);
866868
table.addListener(SWT.EraseItem, listener);
867869
table.addListener(SWT.PaintItem, listener);
868-
869-
return table;
870870
}
871871

872872
/**

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/quickaccess/QuickAccessDialog.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Tom Hochstein (Freescale) - Bug 393703 - NotHandledException selecting inactive command under 'Previous Choices' in Quick access
1414
* René Brandstetter - Bug 433778
1515
* Patrik Suzzi <psuzzi@gmail.com> - Bug 491410
16+
* Stefan Winkler <stefan@winklerweb.net> - Bug #3742
1617
*******************************************************************************/
1718
package org.eclipse.ui.internal.quickaccess;
1819

@@ -246,12 +247,23 @@ protected Control createDialogArea(Composite parent) {
246247
gridData.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN;
247248
hintText.setLayoutData(gridData);
248249

249-
Table table = contents.createTable(composite, getDefaultOrientation());
250+
Table table = contents.createTable(composite);
250251
table.addKeyListener(getKeyAdapter());
251252

252253
return composite;
253254
}
254255

256+
@Override
257+
protected Control createContents(Composite parent) {
258+
Control control = super.createContents(parent);
259+
260+
// after the complete contents (including dialog fonts) are configured, apply
261+
// the table layout and styling
262+
contents.configureTableStyling(getDefaultOrientation());
263+
264+
return control;
265+
}
266+
255267
private TriggerSequence[] getInvokingCommandKeySequences() {
256268
if (invokingCommandKeySequences == null) {
257269
if (invokingCommand != null) {
@@ -309,6 +321,7 @@ public boolean close() {
309321
@Override
310322
protected Point getDefaultSize() {
311323
GC gc = new GC(getContents());
324+
gc.setFont(contents.getTable().getFont());
312325
FontMetrics fontMetrics = gc.getFontMetrics();
313326
gc.dispose();
314327
int x = Dialog.convertHorizontalDLUsToPixels(fontMetrics, 300);

0 commit comments

Comments
 (0)