Search in sources :

Example 11 with SWTError

use of org.eclipse.swt.SWTError in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_printing_Printer method test_ConstructorLorg_eclipse_swt_printing_PrinterData.

@Test
public void test_ConstructorLorg_eclipse_swt_printing_PrinterData() {
    boolean exceptionThrown = false;
    String detail = "";
    PrinterData data = Printer.getDefaultPrinterData();
    if (data == null) {
        /* There aren't any printers, so verify that the
		 * constructor throws an ERROR_NO_HANDLES SWTError.
		 */
        try {
            Printer printer = new Printer(data);
            printer.dispose();
        } catch (SWTError ex) {
            if (ex.code == SWT.ERROR_NO_HANDLES)
                exceptionThrown = true;
        }
        assertTrue("ERROR_NO_HANDLES not thrown", exceptionThrown);
    } else {
        /* There is at least a default printer, so verify that
		 * the constructor does not throw any exceptions.
		 */
        try {
            Printer printer = new Printer(data);
            printer.dispose();
        } catch (Throwable ex) {
            exceptionThrown = true;
            detail = ex.getMessage();
        }
        assertFalse("Exception thrown: " + detail, exceptionThrown);
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) PrinterData(org.eclipse.swt.printing.PrinterData) Printer(org.eclipse.swt.printing.Printer) Test(org.junit.Test)

Example 12 with SWTError

use of org.eclipse.swt.SWTError in project eclipse.platform.swt by eclipse.

the class DNDExample method createDropTarget.

private void createDropTarget() {
    if (dropTarget != null)
        dropTarget.dispose();
    dropTarget = new DropTarget(dropControl, dropOperation);
    dropTarget.setTransfer(dropTypes);
    dropTarget.addDropListener(new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetEvent event) {
            dropConsole.append(">>dragEnter\n");
            printEvent(event);
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = dropDefaultOperation;
            }
            event.feedback = dropFeedback;
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
            dropConsole.append(">>dragLeave\n");
            printEvent(event);
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            dropConsole.append(">>dragOperationChanged\n");
            printEvent(event);
            if (event.detail == DND.DROP_DEFAULT) {
                event.detail = dropDefaultOperation;
            }
            event.feedback = dropFeedback;
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            dropConsole.append(">>dragOver\n");
            printEvent(event);
            event.feedback = dropFeedback;
        }

        @Override
        public void drop(DropTargetEvent event) {
            dropConsole.append(">>drop\n");
            printEvent(event);
            String[] strings = null;
            if (TextTransfer.getInstance().isSupportedType(event.currentDataType) || RTFTransfer.getInstance().isSupportedType(event.currentDataType) || HTMLTransfer.getInstance().isSupportedType(event.currentDataType) || URLTransfer.getInstance().isSupportedType(event.currentDataType)) {
                strings = new String[] { (String) event.data };
            }
            if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
                strings = (String[]) event.data;
            }
            if (strings == null || strings.length == 0) {
                dropConsole.append("!!Invalid data dropped");
                return;
            }
            if (strings.length == 1 && (dropControlType == TABLE || dropControlType == TREE || dropControlType == LIST)) {
                // convert string separated by "\n" into an array of strings
                String string = strings[0];
                int count = 0;
                int offset = string.indexOf("\n", 0);
                while (offset > 0) {
                    count++;
                    offset = string.indexOf("\n", offset + 1);
                }
                if (count > 0) {
                    strings = new String[count + 1];
                    int start = 0;
                    int end = string.indexOf("\n");
                    int index = 0;
                    while (start < end) {
                        strings[index++] = string.substring(start, end);
                        start = end + 1;
                        end = string.indexOf("\n", start);
                        if (end == -1)
                            end = string.length();
                    }
                }
            }
            switch(dropControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO:
                    {
                        Button b = (Button) dropControl;
                        b.setText(strings[0]);
                        break;
                    }
                case STYLED_TEXT:
                    {
                        StyledText text = (StyledText) dropControl;
                        for (String string : strings) {
                            text.insert(string);
                        }
                        break;
                    }
                case TABLE:
                    {
                        Table table = (Table) dropControl;
                        Point p = event.display.map(null, table, event.x, event.y);
                        TableItem dropItem = table.getItem(p);
                        int index = dropItem == null ? table.getItemCount() : table.indexOf(dropItem);
                        for (String string : strings) {
                            TableItem item = new TableItem(table, SWT.NONE, index);
                            item.setText(0, string);
                            item.setText(1, "dropped item");
                        }
                        TableColumn[] columns = table.getColumns();
                        for (TableColumn column : columns) {
                            column.pack();
                        }
                        break;
                    }
                case TEXT:
                    {
                        Text text = (Text) dropControl;
                        for (String string : strings) {
                            text.append(string + "\n");
                        }
                        break;
                    }
                case TREE:
                    {
                        Tree tree = (Tree) dropControl;
                        Point p = event.display.map(null, tree, event.x, event.y);
                        TreeItem parentItem = tree.getItem(p);
                        for (String string : strings) {
                            TreeItem item = parentItem != null ? new TreeItem(parentItem, SWT.NONE) : new TreeItem(tree, SWT.NONE);
                            item.setText(string);
                        }
                        break;
                    }
                case CANVAS:
                    {
                        dropControl.setData("STRINGS", strings);
                        dropControl.redraw();
                        break;
                    }
                case LABEL:
                    {
                        Label label = (Label) dropControl;
                        label.setText(strings[0]);
                        break;
                    }
                case LIST:
                    {
                        List list = (List) dropControl;
                        for (String string : strings) {
                            list.add(string);
                        }
                        break;
                    }
                case COMBO:
                    {
                        Combo combo = (Combo) dropControl;
                        combo.setText(strings[0]);
                        break;
                    }
                default:
                    throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
        }

        @Override
        public void dropAccept(DropTargetEvent event) {
            dropConsole.append(">>dropAccept\n");
            printEvent(event);
        }
    });
}
Also used : SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Point(org.eclipse.swt.graphics.Point) TableColumn(org.eclipse.swt.widgets.TableColumn) Point(org.eclipse.swt.graphics.Point) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) DropTarget(org.eclipse.swt.dnd.DropTarget)

Example 13 with SWTError

use of org.eclipse.swt.SWTError in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method menuSave.

void menuSave() {
    if (image == null)
        return;
    // stop any animation in progress
    animate = false;
    // so we have to use 'Save As...'.
    if (imageData.type == SWT.IMAGE_UNDEFINED || fileName == null) {
        menuSaveAs();
        return;
    }
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        // Save the current image to the current file.
        loader.data = new ImageData[] { imageData };
        if (imageData.type == SWT.IMAGE_JPEG)
            loader.compression = compressionCombo.indexOf(compressionCombo.getText()) + 1;
        if (imageData.type == SWT.IMAGE_PNG)
            loader.compression = compressionCombo.indexOf(compressionCombo.getText());
        loader.save(fileName, imageData.type);
    } catch (SWTException e) {
        showErrorDialog(bundle.getString("Saving_lc"), fileName, e);
    } catch (SWTError e) {
        showErrorDialog(bundle.getString("Saving_lc"), fileName, e);
    } finally {
        shell.setCursor(null);
        imageCanvas.setCursor(crossCursor);
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) SWTException(org.eclipse.swt.SWTException) Cursor(org.eclipse.swt.graphics.Cursor)

Example 14 with SWTError

use of org.eclipse.swt.SWTError in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method showErrorDialog.

/*
	 * Open an error dialog displaying the specified information.
	 */
void showErrorDialog(String operation, String filename, Throwable e) {
    MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
    String message = createMsg(bundle.getString("Error"), new String[] { operation, filename });
    String errorMessage = "";
    if (e != null) {
        if (e instanceof SWTException) {
            SWTException swte = (SWTException) e;
            errorMessage = swte.getMessage();
            if (swte.throwable != null) {
                errorMessage += ":\n" + swte.throwable.toString();
            }
        } else if (e instanceof SWTError) {
            SWTError swte = (SWTError) e;
            errorMessage = swte.getMessage();
            if (swte.throwable != null) {
                errorMessage += ":\n" + swte.throwable.toString();
            }
        } else {
            errorMessage = e.toString();
        }
    }
    box.setMessage(message + errorMessage);
    box.open();
}
Also used : SWTError(org.eclipse.swt.SWTError) SWTException(org.eclipse.swt.SWTException) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 15 with SWTError

use of org.eclipse.swt.SWTError in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method menuPrint.

void menuPrint() {
    if (image == null)
        return;
    try {
        // Ask the user to specify the printer.
        PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
        if (printerData != null)
            dialog.setPrinterData(printerData);
        printerData = dialog.open();
        if (printerData == null)
            return;
        Printer printer = new Printer(printerData);
        Point screenDPI = display.getDPI();
        Point printerDPI = printer.getDPI();
        int scaleFactor = printerDPI.x / screenDPI.x;
        Rectangle trim = printer.computeTrim(0, 0, 0, 0);
        if (printer.startJob(currentName)) {
            if (printer.startPage()) {
                GC gc = new GC(printer);
                int transparentPixel = imageData.transparentPixel;
                if (transparentPixel != -1 && !transparent) {
                    imageData.transparentPixel = -1;
                }
                Image printerImage = new Image(printer, imageData);
                gc.drawImage(printerImage, 0, 0, imageData.width, imageData.height, -trim.x, -trim.y, scaleFactor * imageData.width, scaleFactor * imageData.height);
                if (transparentPixel != -1 && !transparent) {
                    imageData.transparentPixel = transparentPixel;
                }
                printerImage.dispose();
                gc.dispose();
                printer.endPage();
            }
            printer.endJob();
        }
        printer.dispose();
    } catch (SWTError e) {
        MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
        box.setMessage(bundle.getString("Printing_error") + e.getMessage());
        box.open();
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) PrintDialog(org.eclipse.swt.printing.PrintDialog) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Printer(org.eclipse.swt.printing.Printer) GC(org.eclipse.swt.graphics.GC) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) MessageBox(org.eclipse.swt.widgets.MessageBox)

Aggregations

SWTError (org.eclipse.swt.SWTError)19 SWTException (org.eclipse.swt.SWTException)7 Point (org.eclipse.swt.graphics.Point)6 Cursor (org.eclipse.swt.graphics.Cursor)5 MessageBox (org.eclipse.swt.widgets.MessageBox)5 Browser (org.eclipse.swt.browser.Browser)4 FileDialog (org.eclipse.swt.widgets.FileDialog)4 Printer (org.eclipse.swt.printing.Printer)3 Test (org.junit.Test)3 StyledText (org.eclipse.swt.custom.StyledText)2 Image (org.eclipse.swt.graphics.Image)2 ImageData (org.eclipse.swt.graphics.ImageData)2 ImageLoader (org.eclipse.swt.graphics.ImageLoader)2 Button (org.eclipse.swt.widgets.Button)2 Combo (org.eclipse.swt.widgets.Combo)2 Label (org.eclipse.swt.widgets.Label)2 List (org.eclipse.swt.widgets.List)2 Table (org.eclipse.swt.widgets.Table)2 Text (org.eclipse.swt.widgets.Text)2 BufferedReader (java.io.BufferedReader)1