📄 idrawpanel.java
字号:
/*
* This file is part of Caliph & Emir.
*
* Caliph & Emir is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Caliph & Emir is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Caliph & Emir; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright statement:
* --------------------
* (c) 2005 by Werner Klieber (werner@klieber.info)
* http://caliph-emir.sourceforge.net
*/
package at.wklieber.gui;
import at.wklieber.Settings;
import at.wklieber.tools.Console;
import at.wklieber.tools.IAccessFile;
import at.wklieber.tools.Java2dTools;
import at.wklieber.tools.MessageBox;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class IDrawPanel
//extends JScrollPane
extends JPanel
implements MouseListener, MouseMotionListener, DropTargetListener {
private static Category cat = Logger.getLogger(IDrawPanel.class.getName());
private static Console console = Console.getReference();
private static Settings cfg = Settings.getReference();
private static Java2dTools java2dTools = Java2dTools.getReference();
private static IAccessFile config = cfg.getConfigAccess();
private static final int DEFAULT_SHAPE_SIZE = 150;
public static final int DROP_ACCECPT_ALL = 0;
public static final int DROP_ACCECPT_IMAGES = 1; // use in the draw by sketch area
public static final int DROP_ACCECPT_REPRESENTANTS = 2; // use in the mainframe
private DropTarget dropTarget = null;
private int acceptDropTargets = DROP_ACCECPT_ALL;
// if true, the panel is used to draw Mulmiedia objects and operator
// this means, when deleting objects, the previous operator (ILine) is removed too
private boolean isMathMode = false;
// contains a method that is called when a new compoent is dropped
// to this drawPanel
private IComponentReceivedInterface parentFrame = null;
private BufferedImage backgroundImage = null;
private Rectangle backgroundImageSize = null;
private boolean acceptDnd = true; // if false, all dnd sources are not allowed
public IDrawPanel() {
init(null);
}
public IDrawPanel(IComponentReceivedInterface parentFrame1) {
init(parentFrame1);
}
private void init(IComponentReceivedInterface parentFrame1) {
parentFrame = parentFrame1;
isMathMode = false;
//--------- dnd trop stuff
dropTarget = new DropTarget((Component) this, (int) DnDConstants.ACTION_COPY,
(DropTargetListener) this, true);
dropTarget.setActive(true);
this.setDropTarget(dropTarget);
validate();
}
public boolean isAcceptDnd() {
return acceptDnd;
}
public void setAcceptDnd(boolean acceptDnd) {
this.acceptDnd = acceptDnd;
}
public int getAcceptDropTargets() {
return acceptDropTargets;
}
public void setAcceptDropTargets(int acceptDropTargets) {
this.acceptDropTargets = acceptDropTargets;
}
/**
* returns the number of ILines in the drawpanel.
* Note: all ILines must be at the and of the component-list (so they are painted first);
*/
public int getILineComponentsCount() {
int returnValue = 0;
try {
Component[] comps = this.getComponents();
int counter = comps.length - 1;
while ((counter > -1) && (ILine.class.isInstance(comps[counter]))) {
returnValue++;
counter--;
}
} catch (Exception e) {
cat.error(e);
}
return returnValue;
}
/*public void setObjectPalette(ObjectPalette objectPalette) {
this.objectPalette = objectPalette;
}*/
// not prepared to add lines
public void addNewComponent(IComponent component1) {
//this.add(component1, 0);
this.add(component1);
//this.add(component1, this.getComponents().length);
component1.setDrawPanel((JPanel) this);
component1.addMouseListener(component1);
component1.addMouseMotionListener(component1);
}
/**
* dnd drop target stuff. Supported types are Images and Java-Icomponents (internul stuff).
* Supported is Windows with Internet Explorer and Mozilla
*
* @param dtde
*/
public void drop(DropTargetDropEvent dtde) {
try {
if (!acceptDnd) {
dtde.rejectDrop();
return;
}
//cat.debug("dnd dropped, included Flavors:");
Transferable t = dtde.getTransferable();
DataFlavor[] flavorList = dtde.getCurrentDataFlavors();
/*
for (int i = 0; i < flavorList.length; i++) {
cat.debug("--> Dnd Transfer Mime-Type: <" + flavorList[i].getMimeType() + ">, Info <" + flavorList[i].toString() + ">");
}
*/
/*
cat.debug("TransferList");
DataFlavor[] flavorListD = t.getTransferDataFlavors();
for (int i = 0; i < flavorListD.length; i++) {
cat.debug("--> Dnd Transfer Mime-Type: <" + flavorListD[i].getMimeType() + ">, Info <" + flavorListD[i].toString() + ">");
}
*/
IComponent component = null; //this object will receive the dropped data
String urlString = ""; // set this to the image name to request metadat for search results
URL imageUrl = null; // for downloading images in ie
//------------- ACCEPT own IComponent Data ----------------------------
if (t.isDataFlavorSupported(IComponentTransferable.localIComponentFlavor)) {
cat.info("Got IComponent from Objectpalette");
dtde.acceptDrop(DnDConstants.ACTION_COPY);
component = (IComponent) t.getTransferData(IComponentTransferable.localIComponentFlavor);
dtde.getDropTargetContext().dropComplete(true);
boolean accept = true;
String errorMessage = "";
if (acceptDropTargets == DROP_ACCECPT_REPRESENTANTS) {
// it is the panel of the mainframe - we do only accept images and IRepresentants
String name = component.getClass().getName();
//System.out.println(name);
if (!(IImageComponent.class.getName().equals(name))) {
accept = false;
errorMessage = "This kind of object cannot be dropped into his screen. \n " +
"Please use an image or a Representant";
}
}
// TODO: add here restrictions for the "draw by sketch" dialog
if (!accept) {
MessageBox.displayMessage("Error: invalid object", errorMessage);
dtde.getDropTargetContext().dropComplete(true);
return; // ----------------> EXIT POINT <--------------------------------------
}
// prepare for default output in parent
if (IImageComponent.class.isInstance(component)) {
IImageComponent c = (IImageComponent) component;
if (c.getImage() == null) cat.error("Image is null");
}
if (IColorRectangle.class.isInstance(component)) {
IColorRectangle c = (IColorRectangle) component;
c.setDoDisplayPercentage(true);
} else if (IShape.class.isInstance(component)) {
IShape c = (IShape) component;
//c.setMoveable(true);
//c.setResizeable(true);
///c.setDoDnd(false);
c.setDrawDots(true);
c.setComponentBounds(0, 0, DEFAULT_SHAPE_SIZE, DEFAULT_SHAPE_SIZE);
}
//--------------------- get as much data as possible from standard flavors ------
} else {
// tested for internet explorer (not working in all cases) and mozilla (working)
boolean accept = false;
BufferedImage bImage = null;
String dropString = "";
if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
cat.info("get IMAGE from imageFlavor directly");
dtde.acceptDrop(DnDConstants.ACTION_COPY);
Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
if (image != null) {
bImage = java2dTools.imageToBufferedImage(image);
}
cat.debug("DnD Target: Image received received");
accept = true;
// get the image url for further
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
cat.info("get URL from stringFlavor directly");
String dummy = (String) t.getTransferData(DataFlavor.stringFlavor);
if (dummy != null && dummy.length() > 0) {
urlString = dummy;
cat.debug("Image URL: <" + urlString + ">");
}
} else { // try to get the url from something else: does not work
/*
for (int i = 0; i < flavorList.length; i++) {
DataFlavor x = flavorList[i];
if (x.isFlavorTextType()) {
String data = "";
Object q = t.getTransferData(x);
data = q.getClass().getName();
System.out.println("DATA: <" + data + ">");
if (String.class.isInstance(q)) {
String res = (String) q;
data = "String: <" + res + ">";
System.out.println(data);
}
if (java.io.InputStreamReader.class.isInstance(q)) {
InputStreamReader res = (InputStreamReader) q;
data = MiscTools.inputStreamToString(res, "");
data = "InputStreamReader: <" + data + ">";
System.out.println(data);
}
}
} // end for
*/
}
} // end if is imageflavor
// try to get the image url
for (int i = 0; i < flavorList.length; i++) {
DataFlavor flavor = flavorList[i];
if (flavor.getRepresentationClass().equals(java.net.URL.class)) {
try {
if (dtde.isDataFlavorSupported(flavor)) {
if (!accept) {
dtde.acceptDrop(DnDConstants.ACTION_COPY);
accept = true;
}
URL myUrl = (URL) dtde.getTransferable().getTransferData(flavor);
cat.debug("DnD Target: URL received, null: " + (myUrl == null));
if (myUrl != null) {
imageUrl = myUrl;
}
//outputArea.append("\tURL: " + myUrl.toString() + "\n");
break;
}
} catch (Exception e) {
cat.debug("unable to get The URL of the image. (Mozill causes this exception)");
//e.printStackTrace();
}
}
} // end for
//} // end if imageFlavor
// ie just sends the string, so we have load it
if ((bImage == null) && (imageUrl != null)) {
/*
URL url = null;
try {
url = new URL(imageUrl);
} catch (MalformedURLException e) {
//e.printStackTrace();
cat.error(e);
}
*/
// Get the image
if (imageUrl != null) {
cat.info("load image from url: " + imageUrl.toExternalForm());
Image image = Toolkit.getDefaultToolkit().createImage(imageUrl);
if (image != null) {
bImage = java2dTools.imageToBufferedImage(image);
}
}
}
component = new IImageComponent(this, bImage);
if (!accept) {
cat.debug("Flavor rejected");
dtde.rejectDrop();
}
} // end if what flavor
dtde.getDropTargetContext().dropComplete(true);
//----- Now we have all the data
if (component != null) {
Point point = dtde.getLocation();
component.setDrawPanel((JPanel) this);
component.setComponentLocation(point);
component.setDoDnd(false);
component.setResizeable(true);
component.setMoveable(true);
if (parentFrame == null) { // draw it if no listener. Otherwise the listener gets the data and can do what it wants
addNewComponent(component);
repaint();
//addImage(image.getImage(), (int) point.getX(), (int) point.getY());
} else {
parentFrame.getIcomponentFromDnd(component);
}
repaint();
if (imageUrl != null) {
urlString = imageUrl.toExternalForm();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -