下面列出了org.eclipse.swt.widgets.Composite#addListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public GraphicalViewer(Composite parent) {
canvas = new Canvas(parent, SWT.NONE);
parent.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
canvas.setBounds(canvas.getParent().getClientArea());
requestImageRedraw();
}
});
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
redrawImageIfRequested();
GC gc = e.gc;
paintCanvas(gc);
}
});
}
@Override
protected Composite createDropDown( Composite parent )
{
Composite composite = new Composite( parent, SWT.BORDER
| SWT.DOUBLE_BUFFERED );
GridData gd = new GridData( );
gd.widthHint = CELL_WIDTH * 8 + MARGIN;
gd.heightHint = CELL_HEIGHT * 8 + MARGIN;
composite.setLayoutData( gd );
composite.addListener( SWT.MouseDown, this );
composite.addListener( SWT.FocusOut, this );
composite.addListener( SWT.FocusIn, this );
composite.addListener( SWT.KeyDown, this );
composite.addListener( SWT.Traverse, this );
composite.addPaintListener( this );
return composite;
}
public ItemFrame( Composite parent, Context context )
{
super( parent, SWT.BORDER );
this.context = context;
GridLayout gl = createGridLayout( );
gl.marginTop = MARGIN;
gl.marginBottom = MARGIN;
gl.marginLeft = MARGIN;
gl.marginRight = MARGIN;
setLayout( gl );
view = new Composite( this, SWT.NO_BACKGROUND );
view.setLayoutData( new GridData( VIEW_WIDTH, VIEW_HEIGHT ) );
view.addPaintListener( this );
view.addListener( SWT.MouseDown, this );
view.addListener( SWT.KeyDown, this );
view.addListener( SWT.Traverse, this );
view.addListener( SWT.FocusIn, this );
view.addListener( SWT.FocusOut, this );
btnDropDown = new Button( this, SWT.ARROW | SWT.DOWN );
btnDropDown.setLayoutData( new GridData( BUTTON_WIDTH,
VIEW_HEIGHT ) );
btnDropDown.addListener( SWT.Selection, this );
}
@Override
protected Control createControl() {
final Composite container = new Composite(this, SWT.NONE);
container.setLayout(GridLayoutFactory.fillDefaults().margins(1, 1).create());
container.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(labelAbove ? 2 : 1, 1).create());
container.setBackground(
readOnly ? Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)
: Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
container.addListener(SWT.Paint, e -> drawBorder(container, e));
int textStyle = 0;
if (readOnly) {
textStyle = SWT.READ_ONLY;
}
combo = new CCombo(container, textStyle);
combo.setData(SWTBOT_WIDGET_ID_KEY, id);
combo.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
combo.addListener(SWT.FocusIn, event -> redraw(container));
combo.addListener(SWT.FocusOut, event -> redraw(container));
combo.setEditable(!readOnly);
return container;
}
/**
* Creates the grid of labels displaying the days numbers. The grid is composed
* of a header row, displaying days initials, and 6 row for the days numbers.
* All labels are empty (no text displayed), the content depending of both the
* locale (for first day of week) and the current displayed month.
*/
private void createGrid() {
// Master grid panel
gridPanel = new Composite(this, SWT.NONE);
gridLayout = new DateChooserLayout();
gridPanel.setLayout(gridLayout);
gridPanel.addListener(SWT.Paint, listener);
// Weeks numbers panel
weeksPanel = new Composite(gridPanel, SWT.NONE);
weeks = new Cell[6];
for (int i = 0; i < 6; i++) {
weeks[i] = new Cell(weeksPanel, i);
}
// Grid header panel
headersPanel = new Composite(gridPanel, SWT.NONE);
headers = new Label[8];
for (int i = 0; i < 8; i++) {
headers[i] = new Label(headersPanel, SWT.CENTER);
headers[i].addListener(SWT.MouseDown, listener);
}
// Grid panel
daysPanel = new Composite(gridPanel, SWT.NONE);
days = new Cell[42];
for (int i = 0; i < 42; i++) {
days[i] = new Cell(daysPanel, i);
days[i].label.addListener(SWT.MouseUp, listener);
}
daysPanel.addListener(SWT.Paint, listener);
// Footer panel
todayLabel = new Label(gridPanel, SWT.CENTER);
todayLabel.addListener(SWT.MouseDoubleClick, listener);
todayLabel.addListener(SWT.MouseDown, listener);
}
/**
* Creates the header of the calendar. The header contains the label
* displaying the current month and year, and the two buttons for navigation :
* previous and next month.
*/
private void createHeader() {
monthPanel = new Composite(this, SWT.NONE);
GridLayoutFactory.fillDefaults().numColumns(3).spacing(HEADER_SPACING, 0).margins(HEADER_SPACING, 2).applyTo(monthPanel);
GridDataFactory.fillDefaults().applyTo(monthPanel);
monthPanel.addListener(SWT.MouseDown, listener);
prevMonth = new Button(monthPanel, SWT.ARROW | SWT.LEFT | SWT.FLAT);
prevMonth.addListener(SWT.MouseUp, listener);
prevMonth.addListener(SWT.FocusIn, listener);
currentMonth = new Label(monthPanel, SWT.CENTER);
currentMonth.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
currentMonth.addListener(SWT.MouseDown, listener);
nextMonth = new Button(monthPanel, SWT.ARROW | SWT.RIGHT | SWT.FLAT);
nextMonth.addListener(SWT.MouseUp, listener);
nextMonth.addListener(SWT.FocusIn, listener);
monthsMenu = new Menu(getShell(), SWT.POP_UP);
currentMonth.setMenu(monthsMenu);
for (int i = 0; i < 12; i++) {
final MenuItem item = new MenuItem(monthsMenu, SWT.PUSH);
item.addListener(SWT.Selection, listener);
item.setData(new Integer(i));
}
monthsMenu.addListener(SWT.Show, listener);
}
protected Composite createDropDown( Composite parent )
{
Composite composite = new Composite( parent, SWT.NONE );
composite.addListener( SWT.FocusOut, this );
composite.addListener( SWT.KeyDown, this );
return composite;
}
public GanttTester() {
final Display display = Display.getDefault(); //new Display();
final Monitor m = display.getMonitors()[0];
final Shell shell = new Shell(display);
shell.setText("GanttChart Test Application");
shell.setLayout(new FillLayout());
final SashForm sfVSplit = new SashForm(shell, SWT.VERTICAL);
final SashForm sfHSplit = new SashForm(sfVSplit, SWT.HORIZONTAL);
final ViewForm vfBottom = new ViewForm(sfVSplit, SWT.NONE);
_vfChart = new ViewForm(sfHSplit, SWT.NONE);
final ViewForm rightForm = new ViewForm(sfHSplit, SWT.NONE);
final ScrolledComposite sc = new ScrolledComposite(rightForm, SWT.V_SCROLL | SWT.H_SCROLL);
rightForm.setContent(sc);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.getVerticalBar().setPageIncrement(150);
final Composite rightComposite = new Composite(sc, SWT.NONE);
final GridLayout gl = new GridLayout();
gl.marginLeft = 0;
gl.marginTop = 0;
gl.horizontalSpacing = 0;
gl.verticalSpacing = 0;
gl.marginBottom = 0;
gl.marginHeight = 0;
gl.marginWidth = 0;
rightComposite.setLayout(gl);
sc.setContent(rightComposite);
rightComposite.addListener(SWT.Resize, new Listener() {
public void handleEvent(final Event event) {
sc.setMinSize(rightComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
sfVSplit.setWeights(new int[] { 91, 9 });
sfHSplit.setWeights(new int[] { 70, 30 });
// top left side
_ganttChart = new GanttChart(_vfChart, SWT.MULTI);
_vfChart.setContent(_ganttChart);
_ganttComposite = _ganttChart.getGanttComposite();
final TabFolder tfRight = new TabFolder(rightComposite, SWT.BORDER);
final TabItem tiGeneral = new TabItem(tfRight, SWT.NONE);
tiGeneral.setText("Creation");
final TabItem tiAdvanced = new TabItem(tfRight, SWT.NONE);
tiAdvanced.setText("Advanced");
final TabItem tiEventLog = new TabItem(tfRight, SWT.NONE);
tiEventLog.setText("Event Log");
final Composite bottom = new Composite(rightComposite, SWT.NONE);
bottom.setLayout(new GridLayout());
createCreateButtons(bottom);
vfBottom.setContent(createBottom(vfBottom));
tiGeneral.setControl(createCreationTab(tfRight)); // NOPMD
tiAdvanced.setControl(createAdvancedTab(tfRight));
tiEventLog.setControl(createEventLogTab(tfRight));
shell.setMaximized(true);
// uncomment to put on right-hand-side monitor
shell.setLocation(new Point(m.getClientArea().x, 0));
sc.setMinSize(rightComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.removeListener(SWT.KeyDown, _undoRedoListener);
shell.dispose();
}
/**
* Fill data
* @param specLoaded
*/
private void fillData(Spec specLoaded)
{
if (specLoaded == null)
{
hide();
return;
} else
{
// retrieve the markers associated with the loaded spec
IMarker[] markers = TLAMarkerHelper.getProblemMarkers(specLoaded.getProject(), null);
if (markers == null || markers.length == 0)
{
hide();
}
// sort the markers
List<IMarker> markersList = new ArrayList<IMarker>(Arrays.asList(markers));
Collections.sort(markersList, new MarkerComparator());
// Bug fix: 2 June 2010. It takes forever if
// there are a large number of markers, which
// can easily happen if you remove a definition
// that's used hundreds of times.
int iterations = Math.min(markers.length, 20);
for (int j = 0; j < iterations; j++)
{
final IMarker problem = markersList.get(j);
// listener
Listener listener = new Listener() {
// goto marker on click
public void handleEvent(Event event)
{
TLAMarkerHelper.gotoMarker(problem, ((event.stateMask & SWT.MOD1) != 0));
}
};
// contents of the item
Composite problemItem = new Composite(bar, SWT.LINE_SOLID);
problemItem.setLayout(new RowLayout(SWT.VERTICAL));
problemItem.addListener(SWT.MouseDown, listener);
String[] lines = problem.getAttribute(IMarker.MESSAGE, "").split("\n");
for (int i = 0; i < lines.length; i++)
{
StyledText styledText = new StyledText(problemItem, SWT.INHERIT_DEFAULT);
styledText.setEditable(false);
styledText.setCursor(styledText.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
styledText.setText(lines[i]);
styledText.addListener(SWT.MouseDown, listener);
if (isErrorLine(lines[i], problem))
{
StyleRange range = new StyleRange();
range.underline = true;
range.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_RED);
range.start = 0;
range.length = lines[i].length();
styledText.setStyleRange(range);
}
}
ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
item.setExpanded(true);
String markerType = TLAMarkerHelper.getType(problem);
item.setText(AdapterFactory.getMarkerTypeAsText(markerType) + " " + AdapterFactory.getSeverityAsText(problem.getAttribute(IMarker.SEVERITY,
IMarker.SEVERITY_ERROR)));
item.setHeight(problemItem.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item.setControl(problemItem);
item.addListener(SWT.MouseDown, listener);
}
}
return ;
}
public void attachToHost( Composite host )
{
this.host = host;
host.addListener( ToggleDropDown, this );
}
private void createDropDownComponent( int iXLoc, int iYLoc )
{
if ( !bEnabled )
{
return;
}
int shellWidth = this.getSize( ).x;
Shell shell = new Shell( this.getShell( ), SWT.NONE );
shell.setLayout( new FillLayout( SWT.FILL ) );
if ( ( getStyle( ) & SWT.RIGHT_TO_LEFT ) != 0 )
{
iXLoc -= shellWidth;
}
shell.setLocation( iXLoc, iYLoc );
container = new ScrolledComposite( shell, SWT.V_SCROLL );
container.setAlwaysShowScrollBars( false );
container.setExpandHorizontal( true );
cmpDropDown = new Composite( container, SWT.NONE );
GridLayout gl = new GridLayout( );
gl.horizontalSpacing = 0;
gl.verticalSpacing = 0;
gl.marginHeight = 0;
gl.marginWidth = 0;
cmpDropDown.setLayout( gl );
Listener listenerCmpDropDown = new Listener( ) {
public void handleEvent( Event event )
{
handleEventCmpDropDown( event );
}
};
cmpDropDown.addListener( SWT.KeyDown, listenerCmpDropDown );
cmpDropDown.addListener( SWT.FocusOut, listenerCmpDropDown );
popupCanvases = new ICustomChoice[this.items.length];
for ( int iC = 0; iC < items.length; iC++ )
{
ICustomChoice cnv = createChoice( cmpDropDown, items[iC] );
GridData gd = new GridData( GridData.FILL_HORIZONTAL );
cnv.setLayoutData( gd );
cnv.addListener( SWT.MouseDown, canvasListener );
cnv.addListener( SWT.MouseEnter, canvasListener );
cnv.addListener( SWT.KeyDown, canvasListener );
popupCanvases[iC] = cnv;
if ( cnvSelection.getValue( ).equals( cnv.getValue( ) ) )
{
cnv.notifyListeners( SWT.FocusIn, new Event( ) );
popupSelection = cnv;
}
}
int width = 0;
int height = 0;
int maxWidth = 0;
Control[] children = container.getChildren( );
for ( int i = 0; i < children.length; i++ )
{
Point point = children[i].computeSize( SWT.DEFAULT, SWT.DEFAULT );
maxWidth = point.x > maxWidth ? point.x : maxWidth;
height += point.y;
}
width = getSize( ).x > maxWidth ? getSize( ).x : maxWidth;
height = 18 > height ? 18 : height;
cmpDropDown.setBounds( 0, 0, width, height );
container.setContent( cmpDropDown );
if ( height >= 298 )
{
int containerWidth = maxWidth
+ container.getVerticalBar( ).getSize( ).x;
width = width > containerWidth ? getSize( ).x : containerWidth;
}
shell.setSize( width, height < 298 ? height + 2 : 300 );
shell.layout( );
shell.open( );
}