List of usage examples for org.eclipse.swt.widgets Canvas Canvas
public Canvas(Composite parent, int style)
From source file:org.eclipse.swt.examples.imageanalyzer.ImageAnalyzer.java
void createWidgets() { // Add the widgets to the shell in a grid layout. GridLayout layout = new GridLayout(); layout.marginHeight = 0;//www. j a v a 2 s.c om layout.numColumns = 2; shell.setLayout(layout); // Add a composite to contain some control widgets across the top. Composite controls = new Composite(shell, SWT.NONE); RowLayout rowLayout = new RowLayout(); rowLayout.marginTop = 5; rowLayout.marginBottom = 5; rowLayout.spacing = 8; controls.setLayout(rowLayout); GridData gridData = new GridData(); gridData.horizontalSpan = 2; controls.setLayoutData(gridData); // Combo to change the background. Group group = new Group(controls, SWT.NONE); group.setLayout(new RowLayout()); group.setText(bundle.getString("Background")); backgroundCombo = new Combo(group, SWT.DROP_DOWN | SWT.READ_ONLY); backgroundCombo.setItems(bundle.getString("None"), bundle.getString("White"), bundle.getString("Black"), bundle.getString("Red"), bundle.getString("Green"), bundle.getString("Blue")); backgroundCombo.select(backgroundCombo.indexOf(bundle.getString("White"))); backgroundCombo.addSelectionListener(widgetSelectedAdapter(event -> changeBackground())); // Combo to change the compression ratio. group = new Group(controls, SWT.NONE); group.setLayout(new GridLayout(3, true)); group.setText(bundle.getString("Save_group")); imageTypeCombo = new Combo(group, SWT.DROP_DOWN | SWT.READ_ONLY); String[] types = { "JPEG", "PNG", "GIF", "ICO", "TIFF", "BMP" }; for (String type : types) { imageTypeCombo.add(type); } imageTypeCombo.select(imageTypeCombo.indexOf("JPEG")); imageTypeCombo.addSelectionListener(widgetSelectedAdapter(event -> { int index = imageTypeCombo.getSelectionIndex(); switch (index) { case 0: compressionCombo.setEnabled(true); compressionRatioLabel.setEnabled(true); if (compressionCombo.getItemCount() == 100) break; compressionCombo.removeAll(); for (int i = 0; i < 100; i++) { compressionCombo.add(String.valueOf(i + 1)); } compressionCombo.select(compressionCombo.indexOf("75")); break; case 1: compressionCombo.setEnabled(true); compressionRatioLabel.setEnabled(true); if (compressionCombo.getItemCount() == 10) break; compressionCombo.removeAll(); for (int i = 0; i < 4; i++) { compressionCombo.add(String.valueOf(i)); } compressionCombo.select(0); break; case 2: case 3: case 4: case 5: compressionCombo.setEnabled(false); compressionRatioLabel.setEnabled(false); break; } })); imageTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); compressionRatioLabel = new Label(group, SWT.NONE); compressionRatioLabel.setText(bundle.getString("Compression")); compressionRatioLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); compressionCombo = new Combo(group, SWT.DROP_DOWN | SWT.READ_ONLY); for (int i = 0; i < 100; i++) { compressionCombo.add(String.valueOf(i + 1)); } compressionCombo.select(compressionCombo.indexOf("75")); compressionCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); // Combo to change the x scale. String[] values = { "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2", "3", "4", "5", "6", "7", "8", "9", "10", }; group = new Group(controls, SWT.NONE); group.setLayout(new RowLayout()); group.setText(bundle.getString("X_scale")); scaleXCombo = new Combo(group, SWT.DROP_DOWN); for (String value : values) { scaleXCombo.add(value); } scaleXCombo.select(scaleXCombo.indexOf("1")); scaleXCombo.addSelectionListener(widgetSelectedAdapter(event -> scaleX())); // Combo to change the y scale. group = new Group(controls, SWT.NONE); group.setLayout(new RowLayout()); group.setText(bundle.getString("Y_scale")); scaleYCombo = new Combo(group, SWT.DROP_DOWN); for (String value : values) { scaleYCombo.add(value); } scaleYCombo.select(scaleYCombo.indexOf("1")); scaleYCombo.addSelectionListener(widgetSelectedAdapter(event -> scaleY())); // Combo to change the alpha value. group = new Group(controls, SWT.NONE); group.setLayout(new RowLayout()); group.setText(bundle.getString("Alpha_K")); alphaCombo = new Combo(group, SWT.DROP_DOWN | SWT.READ_ONLY); for (int i = 0; i <= 255; i += 5) { alphaCombo.add(String.valueOf(i)); } alphaCombo.select(alphaCombo.indexOf("255")); alphaCombo.addSelectionListener(widgetSelectedAdapter(event -> alpha())); // Check box to request incremental display. group = new Group(controls, SWT.NONE); group.setLayout(new RowLayout()); group.setText(bundle.getString("Display")); incrementalCheck = new Button(group, SWT.CHECK); incrementalCheck.setText(bundle.getString("Incremental")); incrementalCheck.setSelection(incremental); incrementalCheck.addSelectionListener( widgetSelectedAdapter(event -> incremental = ((Button) event.widget).getSelection())); // Check box to request transparent display. transparentCheck = new Button(group, SWT.CHECK); transparentCheck.setText(bundle.getString("Transparent")); transparentCheck.setSelection(transparent); transparentCheck.addSelectionListener(widgetSelectedAdapter(event -> { transparent = ((Button) event.widget).getSelection(); if (image != null) { imageCanvas.redraw(); } })); // Check box to request mask display. maskCheck = new Button(group, SWT.CHECK); maskCheck.setText(bundle.getString("Mask")); maskCheck.setSelection(showMask); maskCheck.addSelectionListener(widgetSelectedAdapter(event -> { showMask = ((Button) event.widget).getSelection(); if (image != null) { imageCanvas.redraw(); } })); // Check box to request background display. backgroundCheck = new Button(group, SWT.CHECK); backgroundCheck.setText(bundle.getString("Background")); backgroundCheck.setSelection(showBackground); backgroundCheck.addSelectionListener( widgetSelectedAdapter(event -> showBackground = ((Button) event.widget).getSelection())); // Group the animation buttons. group = new Group(controls, SWT.NONE); group.setLayout(new RowLayout()); group.setText(bundle.getString("Animation")); // Push button to display the previous image in a multi-image file. previousButton = new Button(group, SWT.PUSH); previousButton.setText(bundle.getString("Previous")); previousButton.setEnabled(false); previousButton.addSelectionListener(widgetSelectedAdapter(event -> previous())); // Push button to display the next image in a multi-image file. nextButton = new Button(group, SWT.PUSH); nextButton.setText(bundle.getString("Next")); nextButton.setEnabled(false); nextButton.addSelectionListener(widgetSelectedAdapter(event -> next())); // Push button to toggle animation of a multi-image file. animateButton = new Button(group, SWT.PUSH); animateButton.setText(bundle.getString("Animate")); animateButton.setEnabled(false); animateButton.addSelectionListener(widgetSelectedAdapter(event -> animate())); // Label to show the image file type. typeLabel = new Label(shell, SWT.NONE); typeLabel.setText(bundle.getString("Type_initial")); typeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Canvas to show the image. imageCanvas = new Canvas(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND); imageCanvas.setBackground(whiteColor); imageCanvas.setCursor(crossCursor); gridData = new GridData(); gridData.verticalSpan = 15; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; imageCanvas.setLayoutData(gridData); imageCanvas.addPaintListener(event -> { if (image == null) { Rectangle bounds = imageCanvas.getBounds(); event.gc.fillRectangle(0, 0, bounds.width, bounds.height); } else { paintImage(event); } }); imageCanvas.addMouseMoveListener(event -> { if (image != null) { showColorAt(event.x, event.y); } }); // Set up the image canvas scroll bars. ScrollBar horizontal = imageCanvas.getHorizontalBar(); horizontal.setVisible(true); horizontal.setMinimum(0); horizontal.setEnabled(false); horizontal .addSelectionListener(widgetSelectedAdapter(event -> scrollHorizontally((ScrollBar) event.widget))); ScrollBar vertical = imageCanvas.getVerticalBar(); vertical.setVisible(true); vertical.setMinimum(0); vertical.setEnabled(false); vertical.addSelectionListener(widgetSelectedAdapter(event -> scrollVertically((ScrollBar) event.widget))); // Label to show the image size. sizeLabel = new Label(shell, SWT.NONE); sizeLabel.setText(bundle.getString("Size_initial")); sizeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the image depth. depthLabel = new Label(shell, SWT.NONE); depthLabel.setText(bundle.getString("Depth_initial")); depthLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the transparent pixel. transparentPixelLabel = new Label(shell, SWT.NONE); transparentPixelLabel.setText(bundle.getString("Transparent_pixel_initial")); transparentPixelLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the time to load. timeToLoadLabel = new Label(shell, SWT.NONE); timeToLoadLabel.setText(bundle.getString("Time_to_load_initial")); timeToLoadLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Separate the animation fields from the rest of the fields. Label separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); separator.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the logical screen size for animation. screenSizeLabel = new Label(shell, SWT.NONE); screenSizeLabel.setText(bundle.getString("Animation_size_initial")); screenSizeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the background pixel. backgroundPixelLabel = new Label(shell, SWT.NONE); backgroundPixelLabel.setText(bundle.getString("Background_pixel_initial")); backgroundPixelLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the image location (x, y). locationLabel = new Label(shell, SWT.NONE); locationLabel.setText(bundle.getString("Image_location_initial")); locationLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the image disposal method. disposalMethodLabel = new Label(shell, SWT.NONE); disposalMethodLabel.setText(bundle.getString("Disposal_initial")); disposalMethodLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the image delay time. delayTimeLabel = new Label(shell, SWT.NONE); delayTimeLabel.setText(bundle.getString("Delay_initial")); delayTimeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show the background pixel. repeatCountLabel = new Label(shell, SWT.NONE); repeatCountLabel.setText(bundle.getString("Repeats_initial")); repeatCountLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Separate the animation fields from the palette. separator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); separator.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Label to show if the image has a direct or indexed palette. paletteLabel = new Label(shell, SWT.NONE); paletteLabel.setText(bundle.getString("Palette_initial")); paletteLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); // Canvas to show the image's palette. paletteCanvas = new Canvas(shell, SWT.BORDER | SWT.V_SCROLL | SWT.NO_REDRAW_RESIZE); paletteCanvas.setFont(fixedWidthFont); paletteCanvas.getVerticalBar().setVisible(true); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; GC gc = new GC(paletteLabel); paletteWidth = gc.stringExtent(bundle.getString("Max_length_string")).x; gc.dispose(); gridData.widthHint = paletteWidth; gridData.heightHint = 16 * 11; // show at least 16 colors paletteCanvas.setLayoutData(gridData); paletteCanvas.addPaintListener(event -> { if (image != null) paintPalette(event); }); // Set up the palette canvas scroll bar. vertical = paletteCanvas.getVerticalBar(); vertical.setVisible(true); vertical.setMinimum(0); vertical.setIncrement(10); vertical.setEnabled(false); vertical.addSelectionListener(widgetSelectedAdapter(event -> scrollPalette((ScrollBar) event.widget))); // Sash to see more of image or image data. sash = new Sash(shell, SWT.HORIZONTAL); gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; sash.setLayoutData(gridData); sash.addSelectionListener(widgetSelectedAdapter(event -> { if (event.detail != SWT.DRAG) { ((GridData) paletteCanvas.getLayoutData()).heightHint = SWT.DEFAULT; Rectangle paletteCanvasBounds = paletteCanvas.getBounds(); int minY = paletteCanvasBounds.y + 20; Rectangle dataLabelBounds = dataLabel.getBounds(); int maxY = statusLabel.getBounds().y - dataLabelBounds.height - 20; if (event.y > minY && event.y < maxY) { Rectangle oldSash = sash.getBounds(); sash.setBounds(event.x, event.y, event.width, event.height); int diff = event.y - oldSash.y; Rectangle bounds = imageCanvas.getBounds(); imageCanvas.setBounds(bounds.x, bounds.y, bounds.width, bounds.height + diff); bounds = paletteCanvasBounds; paletteCanvas.setBounds(bounds.x, bounds.y, bounds.width, bounds.height + diff); bounds = dataLabelBounds; dataLabel.setBounds(bounds.x, bounds.y + diff, bounds.width, bounds.height); bounds = dataText.getBounds(); dataText.setBounds(bounds.x, bounds.y + diff, bounds.width, bounds.height - diff); //shell.layout(true); } } })); // Label to show data-specific fields. dataLabel = new Label(shell, SWT.NONE); dataLabel.setText(bundle.getString("Pixel_data_initial")); gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; dataLabel.setLayoutData(gridData); // Text to show a dump of the data. dataText = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL); dataText.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); dataText.setFont(fixedWidthFont); gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.heightHint = 128; gridData.grabExcessVerticalSpace = true; dataText.setLayoutData(gridData); dataText.addMouseListener(MouseListener.mouseDownAdapter(event -> { if (image != null && event.button == 1) { showColorForData(); } })); dataText.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent event) { if (image != null) { showColorForData(); } } }); // Label to show status and cursor location in image. statusLabel = new Label(shell, SWT.NONE); statusLabel.setText(""); gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; statusLabel.setLayoutData(gridData); }
From source file:edu.pitt.dbmi.odie.ui.jfreechart.EnhancedChartComposite.java
/** * Constructs a JFreeChart panel.//w ww . java 2 s. c o m * * @param comp * The parent. * @param style * The style of the composite. * @param jfreechart * the chart. * @param width * the preferred width of the panel. * @param height * the preferred height of the panel. * @param minimumDrawW * the minimum drawing width. * @param minimumDrawH * the minimum drawing height. * @param maximumDrawW * the maximum drawing width. * @param maximumDrawH * the maximum drawing height. * @param usingBuffer * a flag that indicates whether to use the off-screen buffer to * improve performance (at the expense of memory). * @param properties * a flag indicating whether or not the chart property editor * should be available via the popup menu. * @param save * a flag indicating whether or not save options should be * available via the popup menu. * @param print * a flag indicating whether or not the print option should be * available via the popup menu. * @param zoom * a flag indicating whether or not zoom options should be added * to the popup menu. * @param tooltips * a flag indicating whether or not tooltips should be enabled * for the chart. */ public EnhancedChartComposite(Composite comp, int style, JFreeChart jfreechart, int width, int height, int minimumDrawW, int minimumDrawH, int maximumDrawW, int maximumDrawH, boolean usingBuffer, boolean properties, boolean save, boolean print, boolean zoom, boolean tooltips) { super(comp, style); setChart(jfreechart); this.chartMouseListeners = new EventListenerList(); setLayout(new FillLayout()); this.info = new ChartRenderingInfo(); this.useBuffer = usingBuffer; this.refreshBuffer = false; this.minimumDrawWidth = minimumDrawW; this.minimumDrawHeight = minimumDrawH; this.maximumDrawWidth = maximumDrawW; this.maximumDrawHeight = maximumDrawH; this.zoomTriggerDistance = DEFAULT_ZOOM_TRIGGER_DISTANCE; setDisplayToolTips(tooltips); // create the canvas and add the required listeners this.canvas = new Canvas(this, SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND); this.canvas.addPaintListener(this); this.canvas.addMouseListener(this); this.canvas.addMouseMoveListener(this); // set up popup menu... this.popup = null; if (properties || save || print || zoom) this.popup = createPopupMenu(properties, save, print, zoom); this.enforceFileExtensions = true; }
From source file:org.eclipse.swt.examples.layoutexample.Tab.java
/** * Refreshes the composite and draws all controls * in the layout example./* w w w . ja v a 2s. c o m*/ */ void refreshLayoutComposite() { /* Remove children that are already laid out */ children = layoutComposite.getChildren(); for (Control child : children) { child.dispose(); } /* Add all children listed in the table */ TableItem[] items = table.getItems(); children = new Control[items.length]; String[] itemValues = new String[] { LayoutExample.getResourceString("Item", new String[] { "1" }), LayoutExample.getResourceString("Item", new String[] { "2" }), LayoutExample.getResourceString("Item", new String[] { "3" }) }; for (int i = 0; i < items.length; i++) { String control = items[i].getText(1); String controlName = items[i].getText(0); if (control.equals("Button")) { Button button = new Button(layoutComposite, SWT.PUSH); button.setText(controlName); children[i] = button; } else if (control.equals("Canvas")) { Canvas canvas = new Canvas(layoutComposite, SWT.BORDER); children[i] = canvas; } else if (control.equals("Combo")) { Combo combo = new Combo(layoutComposite, SWT.NONE); combo.setItems(itemValues); combo.setText(controlName); children[i] = combo; } else if (control.equals("Composite")) { Composite composite = new Composite(layoutComposite, SWT.BORDER); children[i] = composite; } else if (control.equals("CoolBar")) { CoolBar coolBar = new CoolBar(layoutComposite, SWT.NONE); ToolBar toolBar = new ToolBar(coolBar, SWT.BORDER); ToolItem item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); CoolItem coolItem1 = new CoolItem(coolBar, 0); coolItem1.setControl(toolBar); toolBar = new ToolBar(coolBar, SWT.BORDER); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "3" })); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "4" })); CoolItem coolItem2 = new CoolItem(coolBar, 0); coolItem2.setControl(toolBar); Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT); coolItem1.setSize(coolItem1.computeSize(size.x, size.y)); coolItem2.setSize(coolItem2.computeSize(size.x, size.y)); coolBar.setSize(coolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT)); children[i] = coolBar; } else if (control.equals("Group")) { Group group = new Group(layoutComposite, SWT.NONE); group.setText(controlName); children[i] = group; } else if (control.equals("Label")) { Label label = new Label(layoutComposite, SWT.NONE); label.setText(controlName); children[i] = label; } else if (control.equals("Link")) { Link link = new Link(layoutComposite, SWT.NONE); link.setText(controlName); children[i] = link; } else if (control.equals("List")) { org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(layoutComposite, SWT.BORDER); list.setItems(itemValues); children[i] = list; } else if (control.equals("ProgressBar")) { ProgressBar progress = new ProgressBar(layoutComposite, SWT.NONE); progress.setSelection(50); children[i] = progress; } else if (control.equals("Scale")) { Scale scale = new Scale(layoutComposite, SWT.NONE); children[i] = scale; } else if (control.equals("Slider")) { Slider slider = new Slider(layoutComposite, SWT.NONE); children[i] = slider; } else if (control.equals("StyledText")) { StyledText styledText = new StyledText(layoutComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); styledText.setText(controlName); children[i] = styledText; } else if (control.equals("Table")) { Table table = new Table(layoutComposite, SWT.BORDER); table.setLinesVisible(true); TableItem item1 = new TableItem(table, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); TableItem item2 = new TableItem(table, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = table; } else if (control.equals("Text")) { Text text = new Text(layoutComposite, SWT.BORDER); text.setText(controlName); children[i] = text; } else if (control.equals("ToolBar")) { ToolBar toolBar = new ToolBar(layoutComposite, SWT.BORDER); ToolItem item1 = new ToolItem(toolBar, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); ToolItem item2 = new ToolItem(toolBar, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = toolBar; } else { Tree tree = new Tree(layoutComposite, SWT.BORDER); TreeItem item1 = new TreeItem(tree, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); TreeItem item2 = new TreeItem(tree, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = tree; } } }
From source file:LayoutExample.java
/** * Refreshes the composite and draws all controls in the layout example. *//*from w ww . j a v a 2s . com*/ void refreshLayoutComposite() { /* Remove children that are already laid out */ children = layoutComposite.getChildren(); for (int i = 0; i < children.length; i++) { children[i].dispose(); } /* Add all children listed in the table */ TableItem[] items = table.getItems(); children = new Control[items.length]; String[] itemValues = new String[] { LayoutExample.getResourceString("Item", new String[] { "1" }), LayoutExample.getResourceString("Item", new String[] { "2" }), LayoutExample.getResourceString("Item", new String[] { "3" }) }; for (int i = 0; i < items.length; i++) { String control = items[i].getText(1); if (control.equals("Button")) { Button button = new Button(layoutComposite, SWT.PUSH); button.setText(LayoutExample.getResourceString("Button_Index", new String[] { new Integer(i).toString() })); children[i] = button; } else if (control.equals("Canvas")) { Canvas canvas = new Canvas(layoutComposite, SWT.BORDER); children[i] = canvas; } else if (control.equals("Combo")) { Combo combo = new Combo(layoutComposite, SWT.NONE); combo.setItems(itemValues); combo.setText( LayoutExample.getResourceString("Combo_Index", new String[] { new Integer(i).toString() })); children[i] = combo; } else if (control.equals("Composite")) { Composite composite = new Composite(layoutComposite, SWT.BORDER); children[i] = composite; } else if (control.equals("CoolBar")) { CoolBar coolBar = new CoolBar(layoutComposite, SWT.NONE); ToolBar toolBar = new ToolBar(coolBar, SWT.BORDER); ToolItem item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); CoolItem coolItem1 = new CoolItem(coolBar, 0); coolItem1.setControl(toolBar); toolBar = new ToolBar(coolBar, SWT.BORDER); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "3" })); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "4" })); CoolItem coolItem2 = new CoolItem(coolBar, 0); coolItem2.setControl(toolBar); Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT); coolItem1.setSize(coolItem1.computeSize(size.x, size.y)); coolItem2.setSize(coolItem2.computeSize(size.x, size.y)); coolBar.setSize(coolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT)); children[i] = coolBar; } else if (control.equals("Group")) { Group group = new Group(layoutComposite, SWT.NONE); group.setText( LayoutExample.getResourceString("Group_Index", new String[] { new Integer(i).toString() })); children[i] = group; } else if (control.equals("Label")) { Label label = new Label(layoutComposite, SWT.NONE); label.setText( LayoutExample.getResourceString("Label_Index", new String[] { new Integer(i).toString() })); children[i] = label; } else if (control.equals("List")) { List list = new List(layoutComposite, SWT.BORDER); list.setItems(itemValues); children[i] = list; } else if (control.equals("ProgressBar")) { ProgressBar progress = new ProgressBar(layoutComposite, SWT.NONE); progress.setSelection(50); children[i] = progress; } else if (control.equals("Scale")) { Scale scale = new Scale(layoutComposite, SWT.NONE); children[i] = scale; } else if (control.equals("Slider")) { Slider slider = new Slider(layoutComposite, SWT.NONE); children[i] = slider; } else if (control.equals("StyledText")) { StyledText styledText = new StyledText(layoutComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); styledText.setText(LayoutExample.getResourceString("StyledText_Index", new String[] { new Integer(i).toString() })); children[i] = styledText; } else if (control.equals("Table")) { Table table = new Table(layoutComposite, SWT.BORDER); table.setLinesVisible(true); TableItem item1 = new TableItem(table, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); TableItem item2 = new TableItem(table, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = table; } else if (control.equals("Text")) { Text text = new Text(layoutComposite, SWT.BORDER); text.setText( LayoutExample.getResourceString("Text_Index", new String[] { new Integer(i).toString() })); children[i] = text; } else if (control.equals("ToolBar")) { ToolBar toolBar = new ToolBar(layoutComposite, SWT.BORDER); ToolItem item1 = new ToolItem(toolBar, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); ToolItem item2 = new ToolItem(toolBar, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = toolBar; } else { Tree tree = new Tree(layoutComposite, SWT.BORDER); TreeItem item1 = new TreeItem(tree, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); TreeItem item2 = new TreeItem(tree, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = tree; } } }
From source file:DNDExample.java
private Control createWidget(int type, Composite parent, String prefix) { switch (type) { case BUTTON_CHECK: { Button button = new Button(parent, SWT.CHECK); button.setText(prefix + " Check box"); return button; }/*from w w w .j a v a 2 s . co m*/ case BUTTON_TOGGLE: { Button button = new Button(parent, SWT.TOGGLE); button.setText(prefix + " Toggle button"); return button; } case BUTTON_RADIO: { Button button = new Button(parent, SWT.RADIO); button.setText(prefix + " Radio button"); return button; } case TABLE: { Table table = new Table(parent, SWT.BORDER | SWT.MULTI); TableColumn column1 = new TableColumn(table, SWT.NONE); TableColumn column2 = new TableColumn(table, SWT.NONE); for (int i = 0; i < 10; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, prefix + " name " + i); item.setText(1, prefix + " value " + i); } column1.pack(); column2.pack(); return table; } case TEXT: { Text text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); text.setText(prefix + " Text"); return text; } case TREE: { Tree tree = new Tree(parent, SWT.BORDER); for (int i = 0; i < 3; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText(prefix + " item " + i); for (int j = 0; j < 3; j++) { TreeItem subItem = new TreeItem(item, SWT.NONE); subItem.setText(prefix + " item " + j); for (int k = 0; k < 3; k++) { TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); subsubItem.setText(prefix + " item " + k); } } } return tree; } case CANVAS: { Canvas canvas = new Canvas(parent, SWT.BORDER); canvas.setData("STRINGS", new String[] { prefix + " Canvas widget" }); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Canvas c = (Canvas) e.widget; Image image = (Image) c.getData("IMAGE"); if (image != null) { e.gc.drawImage(image, 5, 5); } else { String[] strings = (String[]) c.getData("STRINGS"); if (strings != null) { FontMetrics metrics = e.gc.getFontMetrics(); int height = metrics.getHeight(); int y = 5; for (int i = 0; i < strings.length; i++) { e.gc.drawString(strings[i], 5, y); y += height + 5; } } } } }); return canvas; } case LABEL: { Label label = new Label(parent, SWT.BORDER); label.setText(prefix + " Label"); return label; } case LIST: { List list = new List(parent, SWT.BORDER); list.setItems(new String[] { prefix + " Item a", prefix + " Item b", prefix + " Item c", prefix + " Item d" }); return list; } default: throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED); } }
From source file:org.eclipse.swt.examples.dnd.DNDExample.java
private Control createWidget(int type, Composite parent, String prefix) { switch (type) { case BUTTON_CHECK: { Button button = new Button(parent, SWT.CHECK); button.setText(prefix + " Check box"); return button; }// w ww . jav a 2 s .c o m case BUTTON_TOGGLE: { Button button = new Button(parent, SWT.TOGGLE); button.setText(prefix + " Toggle button"); return button; } case BUTTON_RADIO: { Button button = new Button(parent, SWT.RADIO); button.setText(prefix + " Radio button"); return button; } case STYLED_TEXT: { StyledText text = new StyledText(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); text.setText(prefix + " Styled Text"); return text; } case TABLE: { Table table = new Table(parent, SWT.BORDER | SWT.MULTI); table.setHeaderVisible(true); TableColumn column0 = new TableColumn(table, SWT.LEFT); column0.setText("Name"); TableColumn column1 = new TableColumn(table, SWT.RIGHT); column1.setText("Value"); TableColumn column2 = new TableColumn(table, SWT.CENTER); column2.setText("Description"); for (int i = 0; i < 10; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, prefix + " name " + i); item.setText(1, prefix + " value " + i); item.setText(2, prefix + " description " + i); item.setImage(itemImage); } column0.pack(); column1.pack(); column2.pack(); return table; } case TEXT: { Text text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); text.setText(prefix + " Text"); return text; } case TREE: { Tree tree = new Tree(parent, SWT.BORDER | SWT.MULTI); tree.setHeaderVisible(true); TreeColumn column0 = new TreeColumn(tree, SWT.LEFT); column0.setText("Name"); TreeColumn column1 = new TreeColumn(tree, SWT.RIGHT); column1.setText("Value"); TreeColumn column2 = new TreeColumn(tree, SWT.CENTER); column2.setText("Description"); for (int i = 0; i < 3; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText(0, prefix + " name " + i); item.setText(1, prefix + " value " + i); item.setText(2, prefix + " description " + i); item.setImage(itemImage); for (int j = 0; j < 3; j++) { TreeItem subItem = new TreeItem(item, SWT.NONE); subItem.setText(0, prefix + " name " + i + " " + j); subItem.setText(1, prefix + " value " + i + " " + j); subItem.setText(2, prefix + " description " + i + " " + j); subItem.setImage(itemImage); for (int k = 0; k < 3; k++) { TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); subsubItem.setText(0, prefix + " name " + i + " " + j + " " + k); subsubItem.setText(1, prefix + " value " + i + " " + j + " " + k); subsubItem.setText(2, prefix + " description " + i + " " + j + " " + k); subsubItem.setImage(itemImage); } } } column0.pack(); column1.pack(); column2.pack(); return tree; } case CANVAS: { Canvas canvas = new Canvas(parent, SWT.BORDER); canvas.setData("STRINGS", new String[] { prefix + " Canvas widget" }); canvas.addPaintListener(e -> { Canvas c = (Canvas) e.widget; Image image = (Image) c.getData("IMAGE"); if (image != null) { e.gc.drawImage(image, 5, 5); } else { String[] strings = (String[]) c.getData("STRINGS"); if (strings != null) { FontMetrics metrics = e.gc.getFontMetrics(); int height = metrics.getHeight(); int y = 5; for (String string : strings) { e.gc.drawString(string, 5, y); y += height + 5; } } } }); return canvas; } case LABEL: { Label label = new Label(parent, SWT.BORDER); label.setText(prefix + " Label"); return label; } case LIST: { List list = new List(parent, SWT.BORDER | SWT.MULTI); list.setItems(prefix + " Item a", prefix + " Item b", prefix + " Item c", prefix + " Item d"); return list; } case COMBO: { Combo combo = new Combo(parent, SWT.BORDER); combo.setItems("Item a", "Item b", "Item c", "Item d"); return combo; } default: throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED); } }
From source file:CustomControlExample.java
/** * Creates the "Example" widgets./* w w w . java2s.co m*/ */ void createExampleWidgets() { /* Compute the widget style */ int style = getDefaultStyle(); if (horizontalButton.getSelection()) style |= SWT.H_SCROLL; if (verticalButton.getSelection()) style |= SWT.V_SCROLL; if (borderButton.getSelection()) style |= SWT.BORDER; if (noBackgroundButton.getSelection()) style |= SWT.NO_BACKGROUND; if (noFocusButton.getSelection()) style |= SWT.NO_FOCUS; if (noMergePaintsButton.getSelection()) style |= SWT.NO_MERGE_PAINTS; if (noRedrawResizeButton.getSelection()) style |= SWT.NO_REDRAW_RESIZE; /* Create the example widgets */ paintCount = 0; cx = 0; cy = 0; canvas = new Canvas(canvasGroup, style); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { paintCount++; GC gc = e.gc; if (fillDamageButton.getSelection()) { Color color = e.display.getSystemColor(colors[paintCount % colors.length]); gc.setBackground(color); gc.fillRectangle(e.x, e.y, e.width, e.height); } Point size = canvas.getSize(); gc.drawArc(cx + 1, cy + 1, size.x - 2, size.y - 2, 0, 360); gc.drawRectangle(cx + (size.x - 10) / 2, cy + (size.y - 10) / 2, 10, 10); } }); canvas.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent event) { Point size = canvas.getSize(); maxX = size.x * 3 / 2; maxY = size.y * 3 / 2; resizeScrollBars(); } }); ScrollBar bar = canvas.getHorizontalBar(); if (bar != null) { bar.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollHorizontal((ScrollBar) event.widget); } }); } bar = canvas.getVerticalBar(); if (bar != null) { bar.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { scrollVertical((ScrollBar) event.widget); } }); } }