📄 dragdropwordjumbles.java
字号:
package com.hnjchina.example.tableviewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.PopupList;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
public class DragDropWordJumbles extends ViewPart{
String word="ECLIPSE";
String[] OPTIONS = { "Apple", "Banana", "Cherry","Doughnut", "Eggplant", "Filbert", "Greens", "Hummus", "Ice Cream", "Jam"};
Label[] labelsRowOne;
Label[] labelsRowTwo;
Font font=null;
Composite com;
/* (non-Javadoc)
* @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
*/
public void createPartControl(Composite parent) {
com=parent;
// WordJumbles(parent,word);
// PopupList(parent);
// UserPassWord(parent);
CreateSnippets(parent);
}
public void setFocus() {
}
public void setDragSource(final Label label) {
// Allows text to be moved only.
int operations = DND.DROP_MOVE;
final DragSource dragSource = new DragSource(label, operations);
// Data should be transfered in plain text format.
Transfer[] formats = new Transfer[] { TextTransfer.getInstance()};
dragSource.setTransfer(formats);
dragSource.addDragListener(new DragSourceListener() {
public void dragStart(DragSourceEvent event) {
// Disallows drags if text is not available.
if (label.getText().length() == 0)
event.doit = false;
}
public void dragSetData(DragSourceEvent event) {
// Provides the text data.
if (TextTransfer.getInstance().isSupportedType(event.dataType))
event.data = label.getText();
}
public void dragFinished(DragSourceEvent event) {
// Removes the text after the move operation.
if (event.doit == true || event.detail == DND.DROP_MOVE) {
label.setText("");
}
}
});
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
dragSource.dispose();
}
});
}
public void setDropTarget(final Label label) {
int operations = DND.DROP_MOVE;
final DropTarget dropTarget = new DropTarget(label, operations);
// Data should be transfered in plain text format.
Transfer[] formats = new Transfer[] { TextTransfer.getInstance()};
dropTarget.setTransfer(formats);
dropTarget.addDropListener(new DropTargetListener() {
public void dragEnter(DropTargetEvent event) {
// Does not accept any drop if the label has text on it.
if(label.getText().length() != 0)
event.detail = DND.DROP_NONE;
}
public void dragLeave(DropTargetEvent event) {
}
public void dragOperationChanged(DropTargetEvent event) {
}
public void dragOver(DropTargetEvent event) {
}
public void drop(DropTargetEvent event) {
if (TextTransfer
.getInstance()
.isSupportedType(event.currentDataType)) {
String text = (String) event.data;
label.setText(text);
check();
}
}
public void dropAccept(DropTargetEvent event) {
}
});
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
dropTarget.dispose();
}
});
}
private void check() {
for(int i=0; i<word.length(); i++) {
if(! labelsRowTwo[i].getText().equals(word.charAt(i) + ""))
return;
}
MessageBox messageBox = new MessageBox(com.getShell());
messageBox.setMessage("Success!");
messageBox.open();
}
public void WordJumbles(Composite parent,String word) {
GridLayout gridLayout = new GridLayout ();
gridLayout.numColumns = word.length();
gridLayout.makeColumnsEqualWidth = true;
parent.setLayout (gridLayout);
this.word = word;
labelsRowOne = new Label[word.length()];
labelsRowTwo = new Label[word.length()];
int width = 40;
int[] randomPermutation = { 5, 2, 6, 3, 1, 4, 0 };
for (int i = 0; i < word.length(); i++) {
final Label labelRowOne = new Label(parent, SWT.BORDER);
labelsRowOne[i] = labelRowOne;
labelRowOne.setBounds(10 + width * i,10 + width,width - 5,width - 5);
labelRowOne.setFont(font);
labelRowOne.setText(word.charAt(randomPermutation[i]) + "");
labelRowOne.setAlignment(SWT.CENTER);
setDragSource(labelRowOne);
//setDropTarget(labelRowOne);
}
for (int i=0;i<word.length();i++){
final Label labelRowTwo = new Label(parent, SWT.BORDER);
labelsRowTwo[i] = labelRowTwo;
labelRowTwo.setBounds(10 + width * i,20 + width,width - 5,width - 5);
labelRowTwo.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
labelRowTwo.setFont(font);
labelRowTwo.setAlignment(SWT.CENTER);
setDragSource(labelRowTwo);
}
}
public void PopupList(final Composite parent){
final Button button = new Button(parent, SWT.PUSH);
final Rectangle rect;
button.setText("Push Me");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
// Create a list
PopupList list = new PopupList(parent.getShell(),SWT.LEFT_TO_RIGHT);
// Add the items to the list
list.setItems(OPTIONS);
// Open the list and get the selected item
// String selected = list.open(parent.getBounds());
Point point,pointsize;
point=button.getLocation();
pointsize=button.getSize();
point.x=point.x+100;
point.y=point.y+50;
Rectangle rect=new Rectangle(point.x,point.y,200,350);
String selected = list.open(rect);
// Print the item to the console
System.out.println("x:"+point.x+"Y:"+point.y+"W:"+pointsize.x+"H:"+pointsize.y);
System.out.println(selected);
}
});
}
public void UserPassWord(Composite parent){
(new Label(parent, SWT.NULL)).setText("User name: ");
Text textUser = new Text(parent, SWT.SINGLE | SWT.BORDER);
textUser.setText("default_user");
textUser.setTextLimit(16);
(new Label(parent, SWT.NULL)).setText("Password: ");
Text textPassword = new Text(parent,SWT.SINGLE | SWT.BORDER);
textPassword.setEchoChar('*');
System.out.println(textPassword.getEchoChar());
textPassword.setText("simmay");
}
public void CreateSnippets(Composite parent){
Display display = parent.getDisplay();
final Clipboard cb = new Clipboard(display);
parent.setLayout(new FormLayout());
final Text text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
Button copy = new Button(parent, SWT.PUSH);
copy.setText("Copy");
copy.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
String textData = text.getSelectionText();
TextTransfer textTransfer = TextTransfer.getInstance();
cb.setContents(new Object[]{textData}, new Transfer[]{textTransfer});
}
});
Button paste = new Button(parent, SWT.PUSH);
paste.setText("Paste");
paste.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event e) {
TextTransfer transfer = TextTransfer.getInstance();
String data = (String)cb.getContents(transfer);
if (data != null) {
text.insert(data);
}
}
});
FormData data = new FormData();
data.right = new FormAttachment(100, -5);
data.top = new FormAttachment(0, 5);
copy.setLayoutData(data);
data = new FormData();
data.right = new FormAttachment(100, -5);
data.top = new FormAttachment(copy, 5);
paste.setLayoutData(data);
data = new FormData();
data.left = new FormAttachment(0, 5);
data.top = new FormAttachment(0, 5);
data.right = new FormAttachment(copy, -5);
data.bottom = new FormAttachment(100, -5);
text.setLayoutData(data);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -