📄 beepanel.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) 2002-2004 by Mathias Lux (mathias@juggle.at) and the Know-Center Graz
* Inffeldgasse 21a, 8010 Graz, Austria
* http://www.know-center.at
*/
package at.know.center.wv_wr.imb.objectcatalog.semanticscreator;
import static java.lang.Thread.sleep;
import at.know.center.wv_wr.imb.objectcatalog.OCToolkit;
import at.know.center.wv_wr.imb.objectcatalog.mpeg7tools.Mpeg7FileFilter;
import at.know.center.wv_wr.imb.objectcatalog.mpeg7tools.XMLFileFilter;
import at.lux.fotoannotation.utils.TextChangesListener;
import at.lux.grapviz.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import java.util.List;
public class BeePanel extends JPanel implements ActionListener {
private LinkedList<EmbedderThread> running = new LinkedList<EmbedderThread>();
private Vector objects, relations;
private SemanticObjectRepresentation draggingObject, highlightedObject;
private SemanticRelationRepresentation highlightedRelation;
private Point pressedAt;
private Point offset, clickedAt, startRel, endRel;
// Logger logger = Logger.getLogger(BeePanel.class);
private int state;
private JPopupMenu menu;
private JMenuItem addObject, remObject, remRelation, distributeElements, cascadeElements, importFile, about, embedElements;
private BeeDataExchange parent;
private BufferedImage logo, background;
private DropTarget dt;
public BeePanel(BeeDataExchange parent) {
super();
this.parent = parent;
dt = new DropTarget(this, new DropTargetAdapter() {
public void drop(DropTargetDropEvent dtde) {
Point p = dtde.getLocation();
Transferable t = dtde.getTransferable();
try {
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
addObjectFromDND(p, s.trim());
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// this.addKeyListener(new KeyAdapter() {
// /**
// * Invoked when a key has been typed.
// * This event occurs when a key press is followed by a key release.
// */
// public void keyTyped(KeyEvent e) {
// // Wird ENTFERNEN gedr點kt, so werden alle markierten
// // Elemente gel鰏cht :)
// System.out.println("pressed: " + e.getKeyCode());
// if (e.getKeyCode() == KeyEvent.VK_DELETE) {
// }
// }
// });
this.setDropTarget(dt);
try {
logo = ImageIO.read(BeePanel.class.getResource("logo.png"));
} catch (Exception ioex) {
ioex.printStackTrace();
}
this.setDoubleBuffered(true);
objects = new Vector();
relations = new Vector();
draggingObject = null;
highlightedObject = null;
highlightedRelation = null;
pressedAt = null;
menu = new JPopupMenu();
addObject = new JMenuItem("add object");
addObject.setActionCommand("addObject");
addObject.addActionListener(this);
remObject = new JMenuItem("remove object");
remObject.addActionListener(this);
remObject.setActionCommand("remObject");
JMenuItem remMarkedObject = new JMenuItem("remove marked objects");
remMarkedObject.addActionListener(this);
remMarkedObject.setActionCommand("remMarked");
remRelation = new JMenuItem("remove relation");
remRelation.addActionListener(this);
remRelation.setActionCommand("remRelation");
cascadeElements = new JMenuItem("cascade elements");
cascadeElements.addActionListener(this);
cascadeElements.setActionCommand("cascade");
distributeElements = new JMenuItem("distribute elements");
distributeElements.addActionListener(this);
distributeElements.setActionCommand("distribute");
embedElements = new JMenuItem("embed elements");
embedElements.addActionListener(this);
embedElements.setActionCommand("embed");
importFile = new JMenuItem("import description ...");
importFile.addActionListener(this);
importFile.setActionCommand("import");
about = new JMenuItem("About ...");
about.addActionListener(this);
about.setActionCommand("about");
// menu.add(addObject);
menu.add(remObject);
menu.add(remMarkedObject);
// menu.addSeparator();
menu.add(remRelation);
menu.addSeparator();
menu.add(cascadeElements);
menu.add(distributeElements);
menu.add(embedElements);
menu.add(importFile);
menu.addSeparator();
menu.add(about);
this.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
background = null;
super.componentResized(e);
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
checkIfIsInObject(e.getPoint());
repaint();
}
public void mouseDragged(MouseEvent e) {
if (pressedAt != null && draggingObject != null) {
Point p = draggingObject.getP();
p.setLocation(e.getPoint().x + offset.x, e.getPoint().y + offset.y);
if (e.getPoint().x > 0 && e.getPoint().y > 0 && e.getPoint().x < getWidth() && e.getPoint().y < getHeight())
draggingObject.setP(p);
} else if (startRel != null) {
endRel = e.getPoint();
}
checkIfIsInObject(e.getPoint());
repaint();
}
});
this.addMouseListener(new BeePanelMouseAdapter());
background = null;
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (!(background != null))
background = generateBackground();
g2.clearRect(0, 0, this.getWidth(), this.getHeight());
g2.drawImage(background, 0, 0, this);
g2.setFont(Font.decode("Verdana-10"));
for (Iterator it = objects.iterator(); it.hasNext();) {
SemanticObjectRepresentation sem = (SemanticObjectRepresentation) it.next();
sem.drawObject(g2);
}
for (Iterator iterator = relations.iterator(); iterator.hasNext();) {
SemanticRelationRepresentation relation = (SemanticRelationRepresentation) iterator.next();
relation.drawRelation(g2);
}
if (startRel != null && endRel != null) {
g2.drawLine(startRel.x, startRel.y, endRel.x, endRel.y);
}
}
public void addObject(Point p) {
String label = JOptionPane.showInputDialog("Please give a name for the new object:");
objects.add(new SemanticObjectRepresentation(p, label));
TextChangesListener.getInstance().fireDataChanged();
repaint();
embedElements();
}
public void addObject(Point p, String label) {
objects.add(new SemanticObjectRepresentation(p, label));
TextChangesListener.getInstance().fireDataChanged();
repaint();
embedElements();
}
public void addObject(Point p, Element node) {
objects.add(new SemanticObjectRepresentation(p, node));
TextChangesListener.getInstance().fireDataChanged();
repaint();
embedElements();
}
private void checkIfIsInObject(Point p) {
highlightedObject = null;
highlightedRelation = null;
for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
SemanticObjectRepresentation sem = (SemanticObjectRepresentation) iterator.next();
if (sem.checkIfInside(p)) {
highlightedObject = sem;
}
}
for (Iterator iterator = relations.iterator(); iterator.hasNext();) {
SemanticRelationRepresentation rel = (SemanticRelationRepresentation) iterator.next();
if (rel.contains(p)) {
highlightedRelation = rel;
}
}
}
private void checkIfPressedInObject(Point p) {
for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
SemanticObjectRepresentation sem = (SemanticObjectRepresentation) iterator.next();
if (sem.checkIfInside(p)) {
draggingObject = sem;
offset = new Point(sem.getP().x - pressedAt.x, sem.getP().y - pressedAt.y);
}
}
}
private void showMenu(int x, int y) {
if (highlightedObject != null)
remObject.setEnabled(true);
else
remObject.setEnabled(false);
if (highlightedRelation != null)
remRelation.setEnabled(true);
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -