Search in sources :

Example 1 with SWTError

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

the class TextViewerDeleteLineTarget method deleteLine.

/**
 * Deletes the lines that intersect with the given <code>selection</code>.
 *
 * @param document the document
 * @param selection the selection to use to determine the document range to delete
 * @param type the line deletion type, must be one of
 * 	<code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
 * @param copyToClipboard <code>true</code> if the deleted line should be copied to the clipboard
 * @throws BadLocationException if position is not valid in the given document
 * @since 3.5
 */
public void deleteLine(IDocument document, ITextSelection selection, int type, boolean copyToClipboard) throws BadLocationException {
    IRegion deleteRegion = getDeleteRegion(document, selection, type);
    int deleteOffset = deleteRegion.getOffset();
    int deleteLength = deleteRegion.getLength();
    if (deleteLength == 0)
        return;
    if (copyToClipboard) {
        fClipboard.checkState();
        try {
            fClipboard.append(document.get(deleteOffset, deleteLength));
        } catch (SWTError e) {
            if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
                throw e;
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59459
            // don't delete if copy to clipboard fails, rather log & abort
            // log
            Status status = new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, e.code, EditorMessages.Editor_error_clipboard_copy_failed_message, e);
            TextEditorPlugin.getDefault().getLog().log(status);
            fClipboard.uninstall();
            // don't delete
            return;
        }
        fClipboard.setDeleting(true);
        // $NON-NLS-1$
        document.replace(deleteOffset, deleteLength, "");
        fClipboard.setDeleting(false);
        fClipboard.saveState();
    } else {
        // $NON-NLS-1$
        document.replace(deleteOffset, deleteLength, "");
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IRegion(org.eclipse.jface.text.IRegion)

Example 2 with SWTError

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

the class ImageAnalyzer method menuSaveMaskAs.

void menuSaveMaskAs() {
    if (image == null || !showMask)
        return;
    if (imageData.getTransparencyType() == SWT.TRANSPARENCY_NONE)
        return;
    // stop any animation in progress
    animate = false;
    // Get the user to choose a file name and type to save.
    FileDialog fileChooser = new FileDialog(shell, SWT.SAVE);
    fileChooser.setFilterPath(lastPath);
    if (fileName != null)
        fileChooser.setFileName(fileName);
    fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS);
    fileChooser.setFilterNames(SAVE_FILTER_NAMES);
    String filename = fileChooser.open();
    lastPath = fileChooser.getFilterPath();
    if (filename == null)
        return;
    // Figure out what file type the user wants saved.
    int filetype = fileChooser.getFilterIndex();
    if (filetype == -1) {
        /* The platform file dialog does not support user-selectable file filters.
			 * Determine the desired type by looking at the file extension.
			 */
        filetype = determineFileType(filename);
        if (filetype == SWT.IMAGE_UNDEFINED) {
            MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
            box.setMessage(createMsg(bundle.getString("Unknown_extension"), filename.substring(filename.lastIndexOf('.') + 1)));
            box.open();
            return;
        }
    }
    if (new java.io.File(filename).exists()) {
        MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
        box.setMessage(createMsg(bundle.getString("Overwrite"), filename));
        if (box.open() == SWT.CANCEL)
            return;
    }
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        // Save the mask of the current image to the specified file.
        ImageData maskImageData = imageData.getTransparencyMask();
        loader.data = new ImageData[] { maskImageData };
        loader.save(filename, filetype);
    } 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) ImageData(org.eclipse.swt.graphics.ImageData) Cursor(org.eclipse.swt.graphics.Cursor) FileDialog(org.eclipse.swt.widgets.FileDialog) Point(org.eclipse.swt.graphics.Point) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 3 with SWTError

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

the class ImageAnalyzer method menuOpenFile.

void menuOpenFile() {
    // stop any animation in progress
    animate = false;
    // Get the user to choose an image file.
    FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
    if (lastPath != null)
        fileChooser.setFilterPath(lastPath);
    fileChooser.setFilterExtensions(OPEN_FILTER_EXTENSIONS);
    fileChooser.setFilterNames(OPEN_FILTER_NAMES);
    String filename = fileChooser.open();
    lastPath = fileChooser.getFilterPath();
    if (filename == null)
        return;
    showFileType(filename);
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    ImageLoader oldLoader = loader;
    try {
        loader = new ImageLoader();
        if (incremental) {
            // Prepare to handle incremental events.
            loader.addImageLoaderListener(event -> incrementalDataLoaded(event));
            incrementalThreadStart();
        }
        // Read the new image(s) from the chosen file.
        long startTime = System.currentTimeMillis();
        imageDataArray = loader.load(filename);
        loadTime = System.currentTimeMillis() - startTime;
        if (imageDataArray.length > 0) {
            // Cache the filename.
            currentName = filename;
            fileName = filename;
            // If there are multiple images in the file (typically GIF)
            // then enable the Previous, Next and Animate buttons.
            previousButton.setEnabled(imageDataArray.length > 1);
            nextButton.setEnabled(imageDataArray.length > 1);
            animateButton.setEnabled(imageDataArray.length > 1 && loader.logicalScreenWidth > 0 && loader.logicalScreenHeight > 0);
            // Display the first image in the file.
            imageDataIndex = 0;
            displayImage(imageDataArray[imageDataIndex]);
        }
    } catch (SWTException e) {
        showErrorDialog(bundle.getString("Loading_lc"), filename, e);
        loader = oldLoader;
    } catch (SWTError e) {
        showErrorDialog(bundle.getString("Loading_lc"), filename, e);
        loader = oldLoader;
    } catch (OutOfMemoryError e) {
        showErrorDialog(bundle.getString("Loading_lc"), filename, e);
        loader = oldLoader;
    } 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) ImageLoader(org.eclipse.swt.graphics.ImageLoader) FileDialog(org.eclipse.swt.widgets.FileDialog)

Example 4 with SWTError

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

the class ImageAnalyzer method menuSaveAs.

void menuSaveAs() {
    if (image == null)
        return;
    // stop any animation in progress
    animate = false;
    // Get the user to choose a file name and type to save.
    FileDialog fileChooser = new FileDialog(shell, SWT.SAVE);
    fileChooser.setFilterPath(lastPath);
    if (fileName != null) {
        String name = fileName;
        int nameStart = name.lastIndexOf(java.io.File.separatorChar);
        if (nameStart > -1) {
            name = name.substring(nameStart + 1);
        }
        fileChooser.setFileName(name.substring(0, name.indexOf(".")) + "." + imageTypeCombo.getText().toLowerCase());
    }
    fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS);
    fileChooser.setFilterNames(SAVE_FILTER_NAMES);
    switch(imageTypeCombo.getSelectionIndex()) {
        case 0:
            fileChooser.setFilterIndex(4);
            break;
        case 1:
            fileChooser.setFilterIndex(5);
            break;
        case 2:
            fileChooser.setFilterIndex(2);
            break;
        case 3:
            fileChooser.setFilterIndex(3);
            break;
        case 4:
            fileChooser.setFilterIndex(6);
            break;
        case 5:
            fileChooser.setFilterIndex(0);
            break;
    }
    String filename = fileChooser.open();
    lastPath = fileChooser.getFilterPath();
    if (filename == null)
        return;
    // Figure out what file type the user wants saved.
    int filetype = fileChooser.getFilterIndex();
    if (filetype == -1) {
        /* The platform file dialog does not support user-selectable file filters.
			 * Determine the desired type by looking at the file extension.
			 */
        filetype = determineFileType(filename);
        if (filetype == SWT.IMAGE_UNDEFINED) {
            MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
            box.setMessage(createMsg(bundle.getString("Unknown_extension"), filename.substring(filename.lastIndexOf('.') + 1)));
            box.open();
            return;
        }
    }
    if (new java.io.File(filename).exists()) {
        MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
        box.setMessage(createMsg(bundle.getString("Overwrite"), filename));
        if (box.open() == SWT.CANCEL)
            return;
    }
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        // Save the current image to the specified file.
        boolean multi = false;
        if (loader.data.length > 1) {
            MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
            box.setMessage(createMsg(bundle.getString("Save_all"), Integer.valueOf(loader.data.length)));
            int result = box.open();
            if (result == SWT.CANCEL)
                return;
            if (result == SWT.YES)
                multi = true;
        }
        /* If the image has transparency but the user has transparency turned off,
			 * turn it off in the saved image. */
        int transparentPixel = imageData.transparentPixel;
        if (!multi && transparentPixel != -1 && !transparent) {
            imageData.transparentPixel = -1;
        }
        if (!multi)
            loader.data = new ImageData[] { imageData };
        loader.compression = compressionCombo.indexOf(compressionCombo.getText());
        loader.save(filename, filetype);
        /* Restore the previous transparency setting. */
        if (!multi && transparentPixel != -1 && !transparent) {
            imageData.transparentPixel = transparentPixel;
        }
        // Update the shell title and file type label,
        // and use the new file.
        fileName = filename;
        shell.setText(createMsg(bundle.getString("Analyzer_on"), filename));
        typeLabel.setText(createMsg(bundle.getString("Type_string"), fileTypeString(filetype)));
    } 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) ImageData(org.eclipse.swt.graphics.ImageData) Cursor(org.eclipse.swt.graphics.Cursor) FileDialog(org.eclipse.swt.widgets.FileDialog) Point(org.eclipse.swt.graphics.Point) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 5 with SWTError

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

the class ImageAnalyzer method menuLoad.

/* Just use Image(device, filename) to load an image file. */
void menuLoad() {
    // stop any animation in progress
    animate = false;
    // Get the user to choose an image file.
    FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
    if (lastPath != null)
        fileChooser.setFilterPath(lastPath);
    fileChooser.setFilterExtensions(OPEN_FILTER_EXTENSIONS);
    fileChooser.setFilterNames(OPEN_FILTER_NAMES);
    String filename = fileChooser.open();
    lastPath = fileChooser.getFilterPath();
    if (filename == null)
        return;
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        // Read the new image from the chosen file.
        long startTime = System.currentTimeMillis();
        Image newImage = new Image(display, filename);
        // don't include getImageData in load time
        loadTime = System.currentTimeMillis() - startTime;
        imageData = newImage.getImageData();
        // Cache the filename.
        currentName = filename;
        fileName = filename;
        // Fill in array and loader data.
        loader = new ImageLoader();
        imageDataArray = new ImageData[] { imageData };
        loader.data = imageDataArray;
        // Display the image.
        imageDataIndex = 0;
        displayImage(imageData);
    } catch (SWTException e) {
        showErrorDialog(bundle.getString("Loading_lc"), filename, e);
    } catch (SWTError e) {
        showErrorDialog(bundle.getString("Loading_lc"), filename, e);
    } catch (OutOfMemoryError e) {
        showErrorDialog(bundle.getString("Loading_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) Image(org.eclipse.swt.graphics.Image) ImageLoader(org.eclipse.swt.graphics.ImageLoader) FileDialog(org.eclipse.swt.widgets.FileDialog)

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