Skip to content

Commit

Permalink
refactoring improvements, added LayoutDataHelper for placing widgets,…
Browse files Browse the repository at this point in the history
… added label showing total number of items in the table
  • Loading branch information
ceki committed Jul 20, 2012
1 parent e6ae674 commit afcdb6a
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 136 deletions.
@@ -0,0 +1,93 @@
/**
* Logback-beagle: The logback Console Plugin for Eclipse
* Copyright (C) 2006-2012, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation.
*/
package ch.qos.logback.beagle.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Control;

public class LayoutDataHelper {

FormData formData;
final Control control;

public static LayoutDataHelper make(Control control) {
return new LayoutDataHelper(control);
}

public static LayoutDataHelper make(Control control, int width, int height) {
return new LayoutDataHelper(control, width, height);
}


public LayoutDataHelper(Control control) {
this.control = control;
formData = new FormData();
}

public LayoutDataHelper(Control control, int width, int height) {
this.control = control;
formData = new FormData(width, height);
}

// relative to the control's parent -------------------------------
public LayoutDataHelper bottom(int offset) {
formData.bottom = new FormAttachment(100, offset);
return this;
}

public LayoutDataHelper left(int offset) {
formData.left = new FormAttachment(0, offset);
return this;
}

public LayoutDataHelper top(int offset) {
formData.top = new FormAttachment(0, offset);
return this;
}


public LayoutDataHelper right(int offset) {
formData.right = new FormAttachment(100, offset);
return this;
}

// relative to aControl, a control passed as parameter ------------------------------

public LayoutDataHelper rightOf(Control aControl, int offset) {
formData.left = new FormAttachment(aControl, offset, SWT.RIGHT);
return this;
}

public LayoutDataHelper leftOf(Control aControl, int offset) {
formData.right = new FormAttachment(aControl, offset, SWT.LEFT);
return this;
}

public LayoutDataHelper above(Control aControl, int offset) {
formData.bottom = new FormAttachment(aControl, offset, SWT.TOP);
return this;
}

public LayoutDataHelper below(Control aControl, int offset) {
formData.top = new FormAttachment(aControl, offset, SWT.BOTTOM);
return this;
}


public FormData getFormData() {
return formData;
}

public void set() {
control.setLayoutData(formData);
}

}
Expand Up @@ -13,8 +13,6 @@
import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
Expand All @@ -26,6 +24,7 @@
import ch.qos.logback.beagle.menu.MenuBuilder;
import ch.qos.logback.beagle.preferences.BeaglePreferencesChangeListenter;
import ch.qos.logback.beagle.preferences.BeaglePreferencesPage;
import ch.qos.logback.beagle.util.LayoutDataHelper;
import ch.qos.logback.beagle.util.MetricsUtil;
import ch.qos.logback.beagle.util.ResourceUtil;
import ch.qos.logback.beagle.view.listener.ColumnControlListener;
Expand All @@ -42,77 +41,73 @@

public class TableMediator {

static final int OFFSET_FROM_BUTTOM = -5;
static final int OFFSET_FROM_BUTTOM = -10;


public Grid grid;
final Grid grid;
public ClassicTISBuffer classicTISBuffer;
public BeaglePreferencesChangeListenter preferencesChangeListenter;

final Composite parent;
public ConverterFacade converterFacade = new ConverterFacade();
ConverterFacade converterFacade = new ConverterFacade();
private LoggerContext loggerContext = new LoggerContext();
Label timeDifferenceLabel;
Label totalEventsLabel;
ToolItem unfreezeToolItem;

public TableMediator(Composite parent) {
this.parent = parent;
this.timeDifferenceLabel = new Label(parent, SWT.LEFT);
this.totalEventsLabel = new Label(parent, SWT.LEFT);
this.grid = new Grid(parent, SWT.VIRTUAL | SWT.H_SCROLL | SWT.V_SCROLL
| SWT.MULTI | SWT.BORDER);

init();
}

private void init() {
loggerContext.setName("beagle");

grid = new Grid(parent, SWT.VIRTUAL | SWT.H_SCROLL | SWT.V_SCROLL
| SWT.MULTI | SWT.BORDER);
grid.setFont(ResourceUtil.FONT);

int charHeight = MetricsUtil.computeCharHeight(grid);
int charWidth = MetricsUtil.computeCharWidth(grid);

FormData formData;
formData = new FormData(Constants.ICON_SIZE, Constants.ICON_SIZE);
formData.left = new FormAttachment(0, charWidth);
formData.bottom = new FormAttachment(100, OFFSET_FROM_BUTTOM);

LayoutDataHelper.make(this.totalEventsLabel, 12 * charWidth, charHeight)
.left(charWidth).bottom(OFFSET_FROM_BUTTOM).set();
this.totalEventsLabel.setText("0 events");

Label separator0Label = new Label(parent, SWT.SEPARATOR | SWT.VERTICAL);
LayoutDataHelper.make(separator0Label, charWidth, charHeight)
.bottom(OFFSET_FROM_BUTTOM).rightOf(this.totalEventsLabel, 10).set();

formData = new FormData(30 * charWidth, charHeight);
Label diffCueLabel = new Label(parent, SWT.LEFT);
formData.bottom = new FormAttachment(100, OFFSET_FROM_BUTTOM);
diffCueLabel.setLayoutData(formData);
diffCueLabel.setText("");
LayoutDataHelper.make(timeDifferenceLabel, 12 * charWidth, charHeight)
.bottom(OFFSET_FROM_BUTTOM).rightOf(separator0Label, 10).set();

ToolBar toolbar = new ToolBar(parent, SWT.HORIZONTAL);
formData = new FormData();
formData.right = new FormAttachment(100, -5);
formData.bottom = new FormAttachment(100, OFFSET_FROM_BUTTOM);

// formData.right = new FormAttachment(100, -5);
// formData.top = new FormAttachment(0, 5);
toolbar.setLayoutData(formData);
ToolItem unfreezeToolItem = new ToolItem(toolbar, SWT.PUSH);
unfreezeToolItem.setEnabled(false);
unfreezeToolItem.setImage(ResourceUtil
.getImage(ResourceUtil.RELEASE_SCROLL_LOCK_IMG_KEY));
unfreezeToolItem.setToolTipText("release scroll lock");
LayoutDataHelper.make(toolbar).right(-5).bottom(-5).set();

formData = new FormData();
formData.top = new FormAttachment(0, 5);
formData.left = new FormAttachment(0, 5);
formData.right = new FormAttachment(100, -5);
formData.bottom = new FormAttachment(toolbar, -5);
this.unfreezeToolItem = new ToolItem(toolbar, SWT.PUSH);
this.unfreezeToolItem.setEnabled(false);
this.unfreezeToolItem.setImage(ResourceUtil
.getImage(ResourceUtil.RELEASE_SCROLL_LOCK_IMG_KEY));
this.unfreezeToolItem.setToolTipText("release scroll lock");

grid.setLayoutData(formData);
LayoutDataHelper.make(grid).top(5).left(5).right(-5).above(toolbar, -1)
.set();
grid.setHeaderVisible(true);
grid.setLinesVisible(false);
grid.setAutoHeight(true);

grid.addControlListener(new TableControlListener(charWidth));

initConverterFacade();

int bufSize = getPreferredBufferSize();
classicTISBuffer = new ClassicTISBuffer(converterFacade, grid, bufSize);
classicTISBuffer = new ClassicTISBuffer(this, bufSize);
createColumns();
classicTISBuffer.diffCue = diffCueLabel;
grid.pack();

preferencesChangeListenter = new BeaglePreferencesChangeListenter(this);

// when the table is cleared classicTISBuffer's handleEvent method will
Expand All @@ -121,40 +116,43 @@ private void init() {
grid.addDisposeListener(classicTISBuffer);

UnfreezeToolItemListener unfreezeButtonListener = new UnfreezeToolItemListener(
classicTISBuffer);
this);
unfreezeToolItem.addSelectionListener(unfreezeButtonListener);

TableItemSelectionListener tableItemSelectionListener = new TableItemSelectionListener(
grid, classicTISBuffer, unfreezeToolItem, unfreezeButtonListener);
this, unfreezeToolItem);
grid.addSelectionListener(tableItemSelectionListener);

TableSelectionViaMouseMovements myMouseListener = new TableSelectionViaMouseMovements(
classicTISBuffer);
this);
grid.addMouseMoveListener(myMouseListener);
grid.addMouseListener(myMouseListener);
grid.addMouseTrackListener(myMouseListener);

grid.addMouseMoveListener(new TimeDifferenceMouseListener(classicTISBuffer));
grid.addMouseMoveListener(new TimeDifferenceMouseListener(this));

Menu menu = MenuBuilder.buildMenu(classicTISBuffer);
MenuBuilder.addOnMenuSelectionAction(menu, classicTISBuffer);
grid.setMenu(menu);
grid.setItemCount(0);
}

public Grid getGrid() {
return grid;
}

public void setBufferSize(int size) {
classicTISBuffer.setBufferSize(size);
}


void disposeOfExistingColumns() {
for(GridColumn column: grid.getColumns()) {
for (GridColumn column : grid.getColumns()) {
column.dispose();
}
}

public void patternChange(String newPattern) {
if(newPattern != null) {
if (newPattern != null) {
converterFacade.setPattern(newPattern);
converterFacade.start();
grid.removeAll();
Expand All @@ -163,7 +161,7 @@ public void patternChange(String newPattern) {
classicTISBuffer.rebuildEmptyGrid();
}
}

int getPreferredBufferSize() {
int result = BeaglePreferencesPage.BUFFER_SIZE_PREFERENCE_DEFAULT_VALUE;
if (Activator.INSTANCE != null) {
Expand Down Expand Up @@ -204,7 +202,6 @@ public void createColumns() {
}
}


private void initConverterFacade() {
converterFacade.setContext(loggerContext);
String pattern = BeaglePreferencesPage.PATTERN_PREFERENCE_DEFAULT_VALUE;
Expand All @@ -216,5 +213,16 @@ private void initConverterFacade() {
converterFacade.start();
}

public void setTimeDifferenceLabelText(String str) {
timeDifferenceLabel.setText(str);
}

public void setTotalEventsLabelText(String str) {
totalEventsLabel.setText(str);
}

public ConverterFacade getConverterFacade() {
return converterFacade;
}

}
Expand Up @@ -8,27 +8,24 @@
*/
package ch.qos.logback.beagle.view.listener;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.ToolItem;

import ch.qos.logback.beagle.view.TableMediator;
import ch.qos.logback.beagle.visual.ClassicTISBuffer;

public class TableItemSelectionListener implements SelectionListener {

Grid grid;
ClassicTISBuffer classicTISBuffer;
GridItem lastSelection;
ToolItem unfreezeButton;

final TableMediator tableMediator;

public TableItemSelectionListener(Grid table,
ClassicTISBuffer visualElementBuffer, ToolItem unfreezeButton,
UnfreezeToolItemListener unfreezeButtonListener) {
this.grid = table;
this.classicTISBuffer = visualElementBuffer;
public TableItemSelectionListener(TableMediator tableMediator,
ToolItem unfreezeButton) {
this.tableMediator = tableMediator;
this.unfreezeButton = unfreezeButton;

}
Expand All @@ -39,14 +36,14 @@ public void widgetDefaultSelected(SelectionEvent e) {

@Override
public void widgetSelected(SelectionEvent e) {
GridItem currentlySelectedTI = (GridItem) e.item;
if(currentlySelectedTI == null)
GridItem currentlySelectedGridItem = (GridItem) e.item;
if(currentlySelectedGridItem == null)
return;

classicTISBuffer.setScrollingEnabled(false);
tableMediator.classicTISBuffer.setScrollingEnabled(false);
unfreezeButton.setEnabled(true);
lastSelection = currentlySelectedTI;
classicTISBuffer.clearCues();
lastSelection = currentlySelectedGridItem;
tableMediator.setTimeDifferenceLabelText("");

}
}

0 comments on commit afcdb6a

Please sign in to comment.