/** * Starts a drag. It creates a bitmap of the view being dragged. That bitmap is what you see * moving. The actual view can be repositioned if that is what the onDrop handle chooses to do. * * @param v The view that is being dragged * @param source An object representing where the drag originated * @param dragInfo The data associated with the object that is being dragged * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or {@link * #DRAG_ACTION_COPY} */ public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) { // Start dragging, but only if the source has something to drag. boolean doDrag = source.allowDrag(); if (!doDrag) return; mOriginator = v; Bitmap b = getViewBitmap(v); if (b == null) { // out of memory? return; } int[] loc = mCoordinatesTemp; v.getLocationOnScreen(loc); int screenX = loc[0]; int screenY = loc[1]; startDrag(b, screenX, screenY, 0, 0, b.getWidth(), b.getHeight(), source, dragInfo, dragAction); b.recycle(); if (dragAction == DRAG_ACTION_MOVE) { v.setVisibility(View.GONE); } }
private boolean drop(float x, float y) { invalidate(); final int[] coordinates = mDropCoordinates; DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); if (mTagPopup != null) { // don't perform dismiss action when the popup closes ((QuickActionWindow) mTagPopup).setOnDismissListener(null); } if (dropTarget != null) { dropTarget.onDragExit( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); if (dropTarget.acceptDrop( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo)) { boolean success = false; if (mTagPopup == null || !(dropTarget instanceof Workspace)) { mTagPopup = null; dropTarget.onDrop( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); success = true; } mDragSource.onDropCompleted((View) dropTarget, success); return true; } else { mDragSource.onDropCompleted((View) dropTarget, false); return true; } } return false; }
public AndItem(boolean preview, Window w) { super(preview); this.w = w; FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 1, 1); this.setLayout(fl); lefthand = new ParameterSlot(Parameter.Condition, "lefthand", preview, w); this.add(lefthand); JLabel la = new JLabel(" AND "); this.add(la); righthand = new ParameterSlot(Parameter.Condition, "righthand", preview, w); this.add(righthand); ToolTipItem tti = new ToolTipItem(); tti.setToolTipText( "<html>This is an if item, if the specified condition <br> is fulfilled, the following block will be executed</html>"); this.add(tti); this.setBackground(new Color(108, 45, 199)); this.setBorder(BorderFactory.createLineBorder(Color.black)); if (preview) { DragSource ds = new DragSource(); ParameterDragGestureListener pdgl = new ParameterDragGestureListener(); ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, pdgl); ds.addDragSourceMotionListener(pdgl); ds.addDragSourceListener( new DragSourceListener() { @Override public void dropActionChanged(DragSourceDragEvent dsde) {} @Override public void dragOver(DragSourceDragEvent dsde) {} @Override public void dragExit(DragSourceEvent dse) {} @Override public void dragEnter(DragSourceDragEvent dsde) {} @Override public void dragDropEnd(DragSourceDropEvent dsde) { ImageMover.stop(); } }); } }
public DragLabel(String s) { this.setText(s); this.setOpaque(true); this.dragSource = DragSource.getDefaultDragSource(); this.dgListener = new DGListener(); this.dsListener = new DSListener(); // component, action, listener this.dragSource.createDefaultDragGestureRecognizer(this, this.dragAction, this.dgListener); }
public void init() { DragSource dragSource = DragSource.getDefaultDragSource(); // 将srcLabel转换成拖放源,它能接受复制、移动两种操作 dragSource.createDefaultDragGestureRecognizer( srcLabel, DnDConstants.ACTION_COPY_OR_MOVE, new DragGestureListener() { public void dragGestureRecognized(DragGestureEvent event) { // 将JLabel里的文本信息包装成Transferable对象 String txt = srcLabel.getText(); Transferable transferable = new StringSelection(txt); // 继续拖放操作,拖放过程中使用手状光标 event.startDrag(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR), transferable); } }); jf.add(new JScrollPane(srcLabel)); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.pack(); jf.setVisible(true); }
private void drawTileRack(GC gc, Rectangle bounds) { gc.setClipping(bounds); int x = bounds.x; int y = bounds.y; int w = bounds.width; int h = bounds.height; gc.setBackground(tileRackBorder); gc.fillRoundRectangle(x, y, w, h, border + 1, border + 1); gc.setBackground(tileRackFill); gc.fillRectangle(x + border, y + border, w - border * 2, h - border * 2); // TileRack tileRack = getLocalTileRack(); if (tileRack == null) return; Tile[] tiles = tileRack.getTiles(); x += border; y += border; for (int i = 0; i < tiles.length; i++) { Tile tile = tiles[i]; Rectangle rectangle = new Rectangle(x, y, tileSize, tileSize); DragSource source = new DragSource(); source.rectangle = rectangle; source.x = i; source.y = -1; source.tile = tile; dragSources.put(rectangle, source); DragTarget target = new DragTarget(); target.rectangle = rectangle; target.x = i; target.y = -1; dragTargets.add(target); drawTile(gc, rectangle, tile); x += tileSize; } }
public void dragGestureRecognized(DragGestureEvent event) { if (folderPanel.getMainFrame().getNoEventsMode()) return; FileTable fileTable = folderPanel.getFileTable(); FileTableModel tableModel = fileTable.getFileTableModel(); // Return (do not initiate drag) if mouse button2 or button3 was used if ((event.getTriggerEvent().getModifiers() & (InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) != 0) return; // Do not use that to retrieve the current selected file as it is inaccurate: the selection // could have changed since the // the mouse was clicked. // AbstractFile selectedFile = fileTable.getSelectedFile(false); // // Return if selected file is null (could happen if '..' is selected) // if(selectedFile==null) // return; // Find out which row was clicked int clickedRow = fileTable.rowAtPoint(event.getDragOrigin()); // Return (do not initiate drag) if the selected file is the parent folder '..' if (clickedRow == -1 || fileTable.isParentFolder(clickedRow)) return; // Retrieve the file corresponding to the clicked row AbstractFile selectedFile = tableModel.getFileAtRow(clickedRow); // Find out which files are to be dragged, based on the selected file and currenlty marked // files. // If there are some files marked, drag marked files only if the selected file is one of the // marked files. // In any other case, only drag the selected file. FileSet markedFiles; FileSet draggedFiles; if (tableModel.getNbMarkedFiles() > 0 && (markedFiles = fileTable.getSelectedFiles()).contains(selectedFile)) { draggedFiles = markedFiles; } else { draggedFiles = new FileSet(fileTable.getCurrentFolder(), selectedFile); } // Set initial DnDContext information DnDContext.setDragInitiatedByMucommander(true); DnDContext.setDragInitiator(folderPanel); DnDContext.setDragGestureModifiersEx(event.getTriggerEvent().getModifiersEx()); // Start dragging DragSource.getDefaultDragSource() .startDrag(event, null, new TransferableFileSet(draggedFiles), this); // DragSource.getDefaultDragSource().startDrag(createCustomDragGestureEvent(event, // DnDConstants.ACTION_MOVE), null, new TransferableFileSet(draggedFiles), this); }
private boolean drop(float x, float y) { final int[] coordinates = mCoordinatesTemp; DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); if (dropTarget != null) { dropTarget.onDragExit( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); if (dropTarget.acceptDrop( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) { dropTarget.onDrop( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); mDragSource.onDropCompleted((View) dropTarget, true); return true; } else { mDragSource.onDropCompleted((View) dropTarget, false); return true; } } return false; }
public VariablePanel(String variableName, boolean preview, Window w) { super(preview); this.w = w; if (preview) { w.spellItems.put(variableName, this); } this.s = variableName; JLabel jl = new JLabel(variableName); this.setLayout(new BorderLayout()); this.add(jl, BorderLayout.CENTER); this.setBorder(BorderFactory.createLineBorder(Color.black)); this.setBackground(Color.lightGray); DragSource ds = new DragSource(); VariableDragGestureListener pdgl = new VariableDragGestureListener(); ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, pdgl); ds.addDragSourceMotionListener(pdgl); ds.addDragSourceListener( new DragSourceListener() { @Override public void dropActionChanged(DragSourceDragEvent dsde) {} @Override public void dragOver(DragSourceDragEvent dsde) {} @Override public void dragExit(DragSourceEvent dse) {} @Override public void dragEnter(DragSourceDragEvent dsde) {} @Override public void dragDropEnd(DragSourceDropEvent dsde) { ImageMover.stop(); } }); }
private void endDrag() { if (mDragging) { mDragging = false; if (mOriginator != null) { mOriginator.setVisibility(View.VISIBLE); } if (mListener != null) { mListener.onDragEnd(); } if (mDragView != null) { mDragView.remove(); mDragView = null; } mView.onDragFinished(); } }
public DragLabel(String s, int a) { if (a != DnDConstants.ACTION_NONE && a != DnDConstants.ACTION_COPY && a != DnDConstants.ACTION_MOVE && a != DnDConstants.ACTION_COPY_OR_MOVE && a != DnDConstants.ACTION_LINK) throw new IllegalArgumentException("action" + a); this.dragAction = a; this.setText(s); this.setOpaque(true); this.dragSource = DragSource.getDefaultDragSource(); this.dgListener = new DGListener(); this.dsListener = new DSListener(); // component, action, listener this.dragSource.createDefaultDragGestureRecognizer(this, this.dragAction, this.dgListener); }
/** Stop dragging without dropping. */ public void cancelDrag() { if (mDragging) { final int[] coordinates = mCoordinatesTemp; if (mLastDropTarget != null) { mLastDropTarget.onDragExit( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); } mDragSource.onDropCompleted((View) mLastDropTarget, false); } endDrag(); }
/** * Determines whether the user flung the current item to delete it. * * @return the vector at which the item was flung, or null if no fling was detected. */ private PointF isFlingingToDelete(DragSource source) { if (mFlingToDeleteDropTarget == null) return null; if (!source.supportsFlingToDelete()) return null; ViewConfiguration config = ViewConfiguration.get(mLauncher); mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { // Do a quick dot product test to ensure that we are flinging // upwards PointF vel = new PointF(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); PointF upVec = new PointF(0f, -1f); float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / (vel.length() * upVec.length())); if (theta <= Math.toRadians(MAX_FLING_DEGREES)) { return vel; } } return null; }
/** * Enables drag operations on the specified component. This class will be notified wheneven drag * operations are performed on the component. * * @param c the component for which to add 'drag' support */ public void enableDrag(Component c) { DragSource dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer( c, DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE | DnDConstants.ACTION_LINK, this); }
private void drawTile(IBoard b, GC gc, int x, int y, int xo, int yo, int size) { gc.setClipping(xo, yo, size + 1, size + 1); Color c = background; if (b.getLetterMultiplier(y, x) == 2) c = doubleLetter; else if (b.getLetterMultiplier(y, x) == 3) c = tripleLetter; else if (b.getLetterMultiplier(y, x) == 4) c = quadLetter; else if (b.getWordMultiplier(y, x) == 2) c = doubleWord; else if (b.getWordMultiplier(y, x) == 3) c = tripleWord; else if (b.getWordMultiplier(y, x) == 4) c = quadWord; gc.setBackground(c); gc.fillRectangle(xo, yo, size, size); c = light; if (b.getLetterMultiplier(y, x) == 2) c = doubleLetterLight; else if (b.getLetterMultiplier(y, x) == 3) c = tripleLetterLight; else if (b.getLetterMultiplier(y, x) == 4) c = quadLetterLight; else if (b.getWordMultiplier(y, x) == 2) c = doubleWordLight; else if (b.getWordMultiplier(y, x) == 3) c = tripleWordLight; else if (b.getWordMultiplier(y, x) == 4) c = quadWordLight; gc.setForeground(c); gc.drawLine(xo, yo, xo + size, yo); gc.drawLine(xo, yo, xo, yo + size); gc.setForeground(dark); gc.drawLine(xo + size, yo + size, xo + size, yo); gc.drawLine(xo + size, yo + size, xo, yo + size); Tile tile = b.getTile(y, x); if (tile.equals(Tile.NONE)) { for (Iterator i = placedTiles.entrySet().iterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry) i.next(); Point point = (Point) entry.getKey(); if (point.x == x && point.y == y) { tile = (Tile) entry.getValue(); DragSource source = new DragSource(); source.rectangle = new Rectangle(xo, yo, size + 1, size + 1); source.x = x; source.y = y; source.tile = tile; dragSources.put(source.rectangle, source); } } } boolean highlight = false; IGameAction action = game.getLastAction(); if (action != null && action instanceof MoveAction) { MoveAction moveAction = (MoveAction) action; MoveResult result = moveAction.getMoveResult(); Tile[] tiles = result.getPreviousBoardState(); Move move = result.getMove(); Orientation orientation = move.getOrientation(); int dx = orientation.getDx(); int dy = orientation.getDy(); int mx = move.getColumn(); int my = move.getRow(); for (int i = 0; i < tiles.length; i++) { Tile t = tiles[i]; if (mx == x && my == y && t.equals(Tile.NONE)) { highlight = true; } mx += dx; my += dy; } } drawTile(gc, new Rectangle(xo, yo, size + 1, size + 1), tile, highlight); if (arrow != null && arrow.x == x && arrow.y == y) { drawArrow(gc, xo, yo, size); } }
public TestXEmbedServer(boolean manual) { // Enable testing extensions in XEmbed server System.setProperty("sun.awt.xembed.testing", "true"); f = new Frame("Main frame"); f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { synchronized (TestXEmbedServer.this) { TestXEmbedServer.this.notifyAll(); } dummy.dispose(); f.dispose(); } }); f.setLayout(new BorderLayout()); Container bcont = new Container(); toFocus = new Button("Click to focus server"); final TextField tf = new TextField(20); tf.setName("0"); DragSource ds = new DragSource(); final DragSourceListener dsl = new DragSourceAdapter() { public void dragDropEnd(DragSourceDropEvent dsde) {} }; final DragGestureListener dgl = new DragGestureListener() { public void dragGestureRecognized(DragGestureEvent dge) { dge.startDrag(null, new StringSelection(tf.getText()), dsl); } }; ds.createDefaultDragGestureRecognizer(tf, DnDConstants.ACTION_COPY, dgl); final DropTargetListener dtl = new DropTargetAdapter() { public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_COPY); try { tf.setText( tf.getText() + (String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor)); } catch (Exception e) { } } }; final DropTarget dt = new DropTarget(tf, dtl); Button b_add = new Button("Add client"); b_add.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { addClient(); } }); Button b_remove = new Button("Remove client"); b_remove.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (clientCont.getComponentCount() != 0) { clientCont.remove(clientCont.getComponentCount() - 1); } } }); b_close = new JButton("Close modal dialog"); b_close.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { modal_d.dispose(); } }); b_modal = new Button("Show modal dialog"); b_modal.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { modal_d = new JDialog(f, "Modal dialog", true); modal_d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); modal_d.setBounds(0, 100, 200, 50); modal_d.getContentPane().add(b_close); modal_d.validate(); modal_d.show(); } }); bcont.add(tf); bcont.add(toFocus); bcont.add(b_add); bcont.add(b_remove); bcont.add(b_modal); if (manual) { Button pass = new Button("Pass"); pass.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { passed = true; synchronized (TestXEmbedServer.this) { TestXEmbedServer.this.notifyAll(); } } }); bcont.add(pass); Button fail = new Button("Fail"); fail.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { passed = false; synchronized (TestXEmbedServer.this) { TestXEmbedServer.this.notifyAll(); } } }); bcont.add(fail); } b_modal.setName("2"); bcont.setLayout(new FlowLayout()); f.add(bcont, BorderLayout.NORTH); clientCont = Box.createVerticalBox(); f.add(clientCont, BorderLayout.CENTER); dummy = new JFrame("Dummy"); dummy.getContentPane().add(new JButton("Button")); dummy.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); dummy.setBounds(0, VERTICAL_POSITION, 100, 100); dummy.setVisible(true); f.setBounds(300, VERTICAL_POSITION, 800, 300); f.setVisible(true); }
private boolean drop(float x, float y) { final int[] coordinates = mCoordinatesTemp; DropTarget dropTarget2 = findDropTarget((int) x, (int) y, coordinates); DropTarget dropTarget; if (usingFixedDropTarget) { dropTarget = fixedDropTarget; } else { dropTarget = dropTarget2; } if (dropTarget != null && dropTarget2 != null) { dropTarget.onDragExit( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); dropTarget2.onDragExit( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); // Condicion para no caer en la barra de arriba en Create if (createMode && y - mTouchOffsetY < 50) return false; if (dropTarget.acceptDrop( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo)) { if (powerPointMode) { // este es el dropPanel ((DropPanel) dropTarget) .updateObject( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); // este es el dragLayer dropTarget2.onDrop( mDragSource, coordinates[0], coordinates[1], (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); } else { dropTarget.onDrop( mDragSource, (int) x, (int) y, (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); if (!(dropTarget2 instanceof Mochila)) { // No queremos meter 2 veces los objetos en la mochila dropTarget2.onDrop( mDragSource, (int) x, (int) y, (int) mTouchOffsetX, (int) mTouchOffsetY, mDragView, mDragInfo); } } mDragSource.onDropCompleted((View) dropTarget, true); return true; } else { mDragSource.onDropCompleted((View) dropTarget, false); return true; } } return false; }