Search in sources :

Example 6 with SWTError

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

the class DNDExample method createDragSource.

private void createDragSource() {
    if (dragSource != null)
        dragSource.dispose();
    dragSource = new DragSource(dragControl, dragOperation);
    dragSource.setTransfer(dragTypes);
    dragSource.addDragListener(new DragSourceListener() {

        @Override
        public void dragFinished(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragFinished\n");
            printEvent(event);
            dragDataText = dragDataRTF = dragDataHTML = dragDataURL = null;
            dragDataFiles = null;
            if (event.detail == DND.DROP_MOVE) {
                switch(dragControlType) {
                    case BUTTON_CHECK:
                    case BUTTON_TOGGLE:
                    case BUTTON_RADIO:
                        {
                            Button b = (Button) dragControl;
                            b.setText("");
                            break;
                        }
                    case STYLED_TEXT:
                        {
                            StyledText text = (StyledText) dragControl;
                            text.insert("");
                            break;
                        }
                    case TABLE:
                        {
                            Table table = (Table) dragControl;
                            TableItem[] items = table.getSelection();
                            for (TableItem item : items) {
                                item.dispose();
                            }
                            break;
                        }
                    case TEXT:
                        {
                            Text text = (Text) dragControl;
                            text.insert("");
                            break;
                        }
                    case TREE:
                        {
                            Tree tree = (Tree) dragControl;
                            TreeItem[] items = tree.getSelection();
                            for (TreeItem item : items) {
                                item.dispose();
                            }
                            break;
                        }
                    case CANVAS:
                        {
                            dragControl.setData("STRINGS", null);
                            dragControl.redraw();
                            break;
                        }
                    case LABEL:
                        {
                            Label label = (Label) dragControl;
                            label.setText("");
                            break;
                        }
                    case LIST:
                        {
                            List list = (List) dragControl;
                            int[] indices = list.getSelectionIndices();
                            list.remove(indices);
                            break;
                        }
                    case COMBO:
                        {
                            Combo combo = (Combo) dragControl;
                            combo.setText("");
                            break;
                        }
                }
            }
        }

        @Override
        public void dragSetData(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragSetData\n");
            printEvent(event);
            if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataText;
            }
            if (RTFTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataRTF;
            }
            if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataHTML;
            }
            if (URLTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataURL;
            }
            if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = dragDataFiles;
            }
        }

        @Override
        public void dragStart(org.eclipse.swt.dnd.DragSourceEvent event) {
            dragConsole.append(">>dragStart\n");
            printEvent(event);
            dragDataFiles = fileList.getItems();
            switch(dragControlType) {
                case BUTTON_CHECK:
                case BUTTON_TOGGLE:
                case BUTTON_RADIO:
                    {
                        Button b = (Button) dragControl;
                        dragDataText = b.getSelection() ? "true" : "false";
                        break;
                    }
                case STYLED_TEXT:
                    {
                        StyledText text = (StyledText) dragControl;
                        String s = text.getSelectionText();
                        if (s.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = s;
                        }
                        break;
                    }
                case TABLE:
                    {
                        Table table = (Table) dragControl;
                        TableItem[] items = table.getSelection();
                        if (items.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < items.length; i++) {
                                buffer.append(items[i].getText());
                                if (items.length > 1 && i < items.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case TEXT:
                    {
                        Text text = (Text) dragControl;
                        String s = text.getSelectionText();
                        if (s.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = s;
                        }
                        break;
                    }
                case TREE:
                    {
                        Tree tree = (Tree) dragControl;
                        TreeItem[] items = tree.getSelection();
                        if (items.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < items.length; i++) {
                                buffer.append(items[i].getText());
                                if (items.length > 1 && i < items.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case CANVAS:
                    {
                        String[] strings = (String[]) dragControl.getData("STRINGS");
                        if (strings == null || strings.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < strings.length; i++) {
                                buffer.append(strings[i]);
                                if (strings.length > 1 && i < strings.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case LABEL:
                    {
                        Label label = (Label) dragControl;
                        String string = label.getText();
                        if (string.length() == 0) {
                            event.doit = false;
                        } else {
                            dragDataText = string;
                        }
                        break;
                    }
                case LIST:
                    {
                        List list = (List) dragControl;
                        String[] selection = list.getSelection();
                        if (selection.length == 0) {
                            event.doit = false;
                        } else {
                            StringBuilder buffer = new StringBuilder();
                            for (int i = 0; i < selection.length; i++) {
                                buffer.append(selection[i]);
                                if (selection.length > 1 && i < selection.length - 1) {
                                    buffer.append("\n");
                                }
                            }
                            dragDataText = buffer.toString();
                        }
                        break;
                    }
                case COMBO:
                    {
                        Combo combo = (Combo) dragControl;
                        String string = combo.getText();
                        Point selection = combo.getSelection();
                        if (selection.x == selection.y) {
                            event.doit = false;
                        } else {
                            dragDataText = string.substring(selection.x, selection.y);
                        }
                        break;
                    }
                default:
                    throw new SWTError(SWT.ERROR_NOT_IMPLEMENTED);
            }
            if (dragDataText != null) {
                dragDataRTF = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\cf1\\b " + dragDataText + "}";
                dragDataHTML = "<b>" + dragDataText + "</b>";
                dragDataURL = "http://" + dragDataText.replace(' ', '.');
                try {
                    new URL(dragDataURL);
                } catch (MalformedURLException e) {
                    dragDataURL = null;
                }
            }
            for (Transfer dragType : dragTypes) {
                if (dragType instanceof TextTransfer && dragDataText == null) {
                    event.doit = false;
                }
                if (dragType instanceof RTFTransfer && dragDataRTF == null) {
                    event.doit = false;
                }
                if (dragType instanceof HTMLTransfer && dragDataHTML == null) {
                    event.doit = false;
                }
                if (dragType instanceof URLTransfer && dragDataURL == null) {
                    event.doit = false;
                }
                if (dragType instanceof FileTransfer && (dragDataFiles == null || dragDataFiles.length == 0)) {
                    event.doit = false;
                }
            }
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) URL(java.net.URL) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) List(org.eclipse.swt.widgets.List) SWTError(org.eclipse.swt.SWTError) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) URLTransfer(org.eclipse.swt.dnd.URLTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) URLTransfer(org.eclipse.swt.dnd.URLTransfer) Transfer(org.eclipse.swt.dnd.Transfer) HTMLTransfer(org.eclipse.swt.dnd.HTMLTransfer) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) FileTransfer(org.eclipse.swt.dnd.FileTransfer) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 7 with SWTError

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

the class Test_org_eclipse_swt_SWTError method test_printStackTrace.

@Test
public void test_printStackTrace() {
    try {
        Class.forName("java.io.PrintStream");
    } catch (ClassNotFoundException e) {
        // ignore test if running on CLDC
        return;
    }
    // test default SWTError
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    System.setErr(new PrintStream(out));
    SWTError error = new SWTError();
    error.printStackTrace();
    assertTrue(out.size() > 0);
    assertTrue(new String(out.toByteArray()).indexOf("test_printStackTrace") != -1);
    // test SWTError with code
    out = new ByteArrayOutputStream();
    System.setErr(new PrintStream(out));
    error = new SWTError(SWT.ERROR_INVALID_ARGUMENT);
    error.printStackTrace();
    assertTrue(out.size() > 0);
    assertTrue(new String(out.toByteArray()).indexOf("test_printStackTrace") != -1);
}
Also used : SWTError(org.eclipse.swt.SWTError) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 8 with SWTError

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

the class BrowserDemoView method createPartControl.

@Override
public void createPartControl(Composite parent) {
    this.parent = parent;
    parent.setLayout(new FillLayout());
    try {
        Browser browser = new Browser(parent, SWT.NONE);
        browser.dispose();
    } catch (SWTError e) {
        Text text = new Text(parent, SWT.MULTI | SWT.READ_ONLY);
        text.setText("Browser widget cannot be instantiated. The exact error is:\r\n" + e);
        text.requestLayout();
        return;
    }
    TabFolder folder = new TabFolder(parent, SWT.NONE);
    TabItem item = new TabItem(folder, SWT.NONE);
    new PawnTab(item);
    item = new TabItem(folder, SWT.NONE);
    new EditorTab(item);
}
Also used : SWTError(org.eclipse.swt.SWTError) TabItem(org.eclipse.swt.widgets.TabItem) TabFolder(org.eclipse.swt.widgets.TabFolder) Text(org.eclipse.swt.widgets.Text) FillLayout(org.eclipse.swt.layout.FillLayout) Browser(org.eclipse.swt.browser.Browser)

Example 9 with SWTError

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

the class BrowserTab method createExampleWidgets.

/**
 * Creates the "Example" widgets.
 */
@Override
void createExampleWidgets() {
    /* Compute the widget style */
    int style = getDefaultStyle();
    if (borderButton.getSelection())
        style |= SWT.BORDER;
    if (webKitButton.getSelection())
        style |= SWT.WEBKIT;
    /* Create the example widgets */
    try {
        browser = new Browser(browserGroup, style);
    } catch (SWTError e) {
        // Probably missing browser
        try {
            browser = new Browser(browserGroup, style & ~(SWT.WEBKIT));
        } catch (SWTError e2) {
            // Unsupported platform
            errorMessage = e.getMessage();
            return;
        }
        MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
        String resourceString = "WebKitNotFound";
        dialog.setMessage(ControlExample.getResourceString(resourceString, e.getMessage()));
        dialog.open();
    }
    if (lastUrl != null) {
        browser.setUrl(lastUrl);
    } else if (lastText != null) {
        browser.setText(lastText);
    } else {
        StringBuilder sb = new StringBuilder(300);
        try (InputStream htmlStream = ControlExample.class.getResourceAsStream("browser-content.html");
            BufferedReader br = new BufferedReader(new InputStreamReader(htmlStream))) {
            int read = 0;
            while ((read = br.read()) != -1) sb.append((char) read);
        } catch (IOException e) {
            log(e.getMessage());
        }
        String text = sb.toString();
        browser.setText(text);
    }
    lastText = lastUrl = null;
}
Also used : SWTError(org.eclipse.swt.SWTError) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Browser(org.eclipse.swt.browser.Browser) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 10 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_Constructor.

@Test
public void test_Constructor() {
    boolean exceptionThrown = false;
    String detail = "";
    if (Printer.getDefaultPrinterData() == null) {
        /* There aren't any printers, so verify that the
		 * constructor throws an ERROR_NO_HANDLES SWTError.
		 */
        try {
            Printer printer = new Printer();
            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();
            printer.dispose();
        } catch (Throwable ex) {
            exceptionThrown = true;
            detail = ex.getMessage();
        }
        assertFalse("Exception thrown: " + detail, exceptionThrown);
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) Printer(org.eclipse.swt.printing.Printer) Test(org.junit.Test)

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