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;
}
}
}
});
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations