4,10 → 4,10 |
* Coded by: Group 5, software practice summer 2003 |
* University of Bielefeld, Germany |
* |
* @version $Revision: 1.18 $ |
* @version $Revision: 1.19 $ |
* |
* Last modification: $Date: 2003/07/21 12:51:32 $ |
* $Id: RegionEditorPlugin.java,v 1.18 2003/07/21 12:51:32 smcsporr Exp $ |
* Last modification: $Date: 2003/07/21 19:52:33 $ |
* $Id: RegionEditorPlugin.java,v 1.19 2003/07/21 19:52:33 smcsporr Exp $ |
*/ |
|
package src.plugins.regionEditorPlugin; |
34,6 → 34,7 |
import javax.swing.JMenuItem; |
import javax.swing.JPopupMenu; |
|
import org.w3c.dom.NodeList; |
import org.w3c.dom.NamedNodeMap; |
import org.w3c.dom.Document; |
import org.w3c.dom.Node; |
57,8 → 58,8 |
* |
* @author Yulia Klassen |
* @author Viktoriya Zudova |
* |
* @version $Revision: 1.18 $ Last modification: $Date: 2003/07/21 12:51:32 $ |
* |
* @version $Revision: 1.19 $ Last modification: $Date: 2003/07/21 19:52:33 $ |
*/ |
public class RegionEditorPlugin |
extends JPanel |
76,6 → 77,10 |
/* A button "Confirm" to change the image name and url in the node. */ |
private JButton confirmButton = new JButton("Confirm"); |
|
private JTextField regionNameField = new JTextField(); |
|
private JButton regionConfirmButton = new JButton("Confirm"); |
|
/* An image panel to show an image in. */ |
private ImagePanel imagePanel; |
|
101,10 → 106,10 |
private Node changedNode; |
|
/* */ |
private List rects = new ArrayList(); |
private List regions = new ArrayList(); |
|
/* */ |
private Rectangle selectedRect; |
private Region selectedRegion; |
|
/* */ |
private Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR); |
125,16 → 130,16 |
private JPopupMenu popup = new JPopupMenu(); |
|
/* */ |
private JMenuItem deleteSelectedItem = new JMenuItem("Delete the rectangle"); |
private JMenuItem deleteSelectedItem = new JMenuItem("Delete the region"); |
|
/** The class constructor creates all elements for the RegionEditor plugin. */ |
public RegionEditorPlugin() { |
|
|
// Sets new layout. |
setLayout(new BorderLayout()); |
|
// Creates the image panel. |
imagePanel = new ImagePanel(rects); |
imagePanel = new ImagePanel(regions); |
imagePanel.addMouseListener(this); |
imagePanel.addMouseMotionListener(this); |
|
144,11 → 149,12 |
leftPanel.add(imageNameField); |
leftPanel.add(confirmButton, BorderLayout.EAST); |
|
// Right panel: label to show name of image file, |
// open button to change images. |
// Right panel: label to show name of region |
JPanel rightPanel = new JPanel(); |
rightPanel.setPreferredSize(new Dimension(240, 24)); |
rightPanel.setLayout(new BorderLayout()); |
rightPanel.add(regionNameField); |
rightPanel.add(regionConfirmButton, BorderLayout.EAST); |
|
// Top panel contains left and right panels. |
JPanel topPanel = new JPanel(); |
160,6 → 166,7 |
add(imagePanel); |
|
confirmButton.addActionListener(this); |
regionConfirmButton.addActionListener(this); |
|
// Init popup menu. |
deleteSelectedItem.addActionListener(this); |
184,35 → 191,119 |
* @param environment: The current document. |
* @param elementPath: The <code>TreePath</code> of the selected |
* node in the evironment. |
* |
* |
* @see src.plugins.PluginInterface#start |
*/ |
public void start(Node node, Document environment, TreePath elementPath) { |
|
|
oldNode = node; |
gui.getStatusInterface().setIcon(StatusInterface.ICON_NONE); |
gui.getStatusInterface().setMessage(null); |
gui.getStatusInterface().setPoint(null); |
|
printTree(node, 0); |
if (node != null) { |
imageName = node.getAttributes().getNamedItem("image").getNodeValue(); |
imageName = node.getAttributes().getNamedItem("image").getNodeValue(); |
} |
//imageUrl = "images/hermann.jpg"; // test only !!! |
imageUrl = "images/office1.jpg"; // test only !!! |
|
imageNameField.setText(imageName); |
loadImage(imageUrl); |
|
// load existing regions |
initRegions(node); |
|
gui.setPluginPanel(this); |
setVisible(true); |
} |
|
private void initRegions(Node node) |
{ |
regions.clear(); |
|
NodeList children = node.getChildNodes(); |
for(int i = 0; i < children.getLength(); i++) { |
Node rectNode = children.item(i); |
|
if(rectNode.getNodeName().equals("RECTANGLE")) { |
NodeList rectChildren = rectNode.getChildNodes(); |
int x = 0; |
int y = 0; |
int w = 1; |
int h = 1; |
String name = null; |
|
for(int j = 0; j < rectChildren.getLength(); j++) { |
Node subnode = rectChildren.item(j); |
|
// get coordinates |
if(subnode.getNodeName().equals("COORDS")) { |
NamedNodeMap attrs = subnode.getAttributes(); |
|
try { |
x = Integer.parseInt(attrs.getNamedItem("x").getNodeValue()); |
y = Integer.parseInt(attrs.getNamedItem("y").getNodeValue()); |
w = Integer.parseInt(attrs.getNamedItem("w").getNodeValue()); |
h = Integer.parseInt(attrs.getNamedItem("h").getNodeValue()); |
} |
catch(NumberFormatException ex) { |
// ignore any errors in coordinates |
} |
} |
else if(subnode.getNodeName().equals("#text")) { |
String value = subnode.getNodeValue().trim(); |
|
if(!value.equals("")) name = value; |
} |
} |
|
regions.add(new Region(name, new Rectangle(x, y, w, h))); |
} |
} |
} |
|
private void printTree(Node node, int indent) |
{ |
if(indent == 0) |
System.out.println("================================================="); |
|
String name = node.getNodeName(); |
NamedNodeMap attrs = node.getAttributes(); |
NodeList list = node.getChildNodes(); |
|
// indent |
for(int i = 0; i < indent*2; i++) System.out.print(" "); |
|
// name |
System.out.print(name + ":"); |
|
// attributes |
if(attrs != null) { |
for(int i = 0; i < attrs.getLength(); i++) |
System.out.print(" " + attrs.item(i).getNodeName() + "=\"" |
+ attrs.item(i).getNodeValue() + "\""); |
} |
|
// value |
if(node.getNodeValue() != null) |
System.out.print(" [" + node.getNodeValue() + "]"); |
System.out.println(); |
|
// children |
for(int i = 0; i < list.getLength(); i++) |
printTree(list.item(i), indent+1); |
|
if(indent == 0) |
System.out.println("================================================="); |
} |
|
/** |
* Stops the plugin by setting it to invisible and resetting the display of the |
* status panel. |
* |
* |
* @see src.plugins.PluginInterface#stop |
*/ |
public void stop() { |
public void stop() { |
setVisible(false); |
gui.getStatusInterface().setIcon(StatusInterface.ICON_NONE); |
gui.getStatusInterface().setMessage(null); |
227,16 → 318,9 |
} |
|
|
/** |
* Forces the text editor plugin to return the actual status as a node, 'cause |
* not well-formed parts have to be capsuled into a prosessing instruction. |
* |
* @see src.gui.PluginInterface#forceStatusReport() |
*/ |
public void forceStatusReport() { |
changedNode = oldNode.cloneNode(true); |
changedNode.getAttributes().getNamedItem("url").setNodeValue(imageUrl); |
changedNode.getAttributes().getNamedItem("name").setNodeValue(imageName); |
changedNode.getAttributes().getNamedItem("image").setNodeValue(imageName); |
firePluginChangedElement(oldNode, changedNode); |
} |
|
245,6 → 329,10 |
ourEventListener.firedOurEvent(e); |
} |
|
private void sendRegionChanged(Region region) |
{ |
} |
|
/** |
* Loads image. |
* |
298,14 → 386,19 |
public void actionPerformed(ActionEvent e) |
{ |
if(e.getSource() == confirmButton) { |
if(imageName != null) { |
imageName = imageNameField.getText(); |
forceStatusReport(); |
imageName = imageNameField.getText(); |
forceStatusReport(); |
} |
|
if(e.getSource() == regionConfirmButton) { |
if(selectedRegion != null) { |
selectedRegion.setName(regionNameField.getText()); |
sendRegionChanged(selectedRegion); |
} |
} |
|
if(e.getSource() == deleteSelectedItem) { // delete the selected rectangle |
deleteRect(selectedRect); |
if(e.getSource() == deleteSelectedItem) { // delete the selected region |
deleteRegion(selectedRegion); |
} |
} |
|
313,7 → 406,7 |
/** |
* Invoked when a mouse button is pressed on a component. |
* |
* @see |
* @see |
* |
* @param e mouse event |
*/ |
322,7 → 415,8 |
if(e.getSource() == imagePanel && image != null) { |
showMouse(e.getX(), e.getY()); |
|
if(inMove && selectedRect != null) { // Move selected rectangle. |
if(inMove && selectedRegion != null) { // Move selected region. |
Rectangle rect = selectedRegion.getRect(); |
double x = (e.getX() - imagePanel.getDx()) * imagePanel.getCoef(); |
double y = (e.getY() - imagePanel.getDy()) * imagePanel.getCoef(); |
|
329,41 → 423,42 |
if(x < 0) x = 0; |
if(y < 0) y = 0; |
|
if(x + (int)selectedRect.getWidth() >= image.getWidth(null)) |
x = image.getWidth(null) - (int)selectedRect.getWidth() - 1; |
if(y + (int)selectedRect.getHeight() >= imagePanel.getHeight()) |
y = image.getHeight(null) - (int)selectedRect.getHeight() - 1; |
if(x + (int)rect.getWidth() >= image.getWidth(null)) |
x = image.getWidth(null) - (int)rect.getWidth() - 1; |
if(y + (int)rect.getHeight() >= image.getHeight(null)) |
y = image.getHeight(null) - (int)rect.getHeight() - 1; |
|
selectedRect.setLocation((int)x, (int)y); |
rect.setLocation((int)x, (int)y); |
repaint(); |
} |
else if(inResize && selectedRect != null) { // Resize selected rectangle. |
else if(inResize && selectedRegion != null) { // Resize selected region. |
Rectangle rect = selectedRegion.getRect(); |
double x = (e.getX() - imagePanel.getDx()) * imagePanel.getCoef(); |
double y = (e.getY() - imagePanel.getDy()) * imagePanel.getCoef(); |
double w = x - selectedRect.getX(); |
double h = y - selectedRect.getY(); |
double w = x - rect.getX(); |
double h = y - rect.getY(); |
|
if(w < 1) w = 1; |
if(h < 1) h = 1; |
|
if(x >= image.getWidth(null)) |
w = image.getWidth(null) - selectedRect.getX(); |
w = image.getWidth(null) - rect.getX(); |
if(y >= image.getHeight(null)) |
h = image.getHeight(null) - selectedRect.getY(); |
h = image.getHeight(null) - rect.getY(); |
|
selectedRect.setSize((int)w, (int)h); |
rect.setSize((int)w, (int)h); |
repaint(); |
} |
else { // Creates a new rectangle. |
else { // Creates a new region. |
double x = (e.getX() - imagePanel.getDx()) * imagePanel.getCoef(); |
double y = (e.getY() - imagePanel.getDy()) * imagePanel.getCoef(); |
|
if(x >= 0 && y >= 0 && x < image.getWidth(null) && y < image.getHeight(null)) { |
inResize = true; |
setSelectedRect(new Rectangle( |
setSelectedRegion(new Region(null, new Rectangle( |
(int)((e.getX() - imagePanel.getDx()) * imagePanel.getCoef()), |
(int)((e.getY() - imagePanel.getDy()) * imagePanel.getCoef()), 1, 1)); |
addRect(selectedRect); |
(int)((e.getY() - imagePanel.getDy()) * imagePanel.getCoef()), 1, 1))); |
addRegion(selectedRegion); |
} |
} |
} |
379,15 → 474,15 |
if(e.getSource() == imagePanel && image != null) { |
showMouse(e.getX(), e.getY()); |
|
Rectangle rect1 = findRect(e.getX(), e.getY(), true); // Try move. |
Region region1 = findRegion(e.getX(), e.getY(), true); // Try move. |
|
if(rect1 != null) { // Found one. |
if(region1 != null) { // Found one. |
setCursor(moveCursor); |
} |
else { // Not found - try resize. |
Rectangle rect2 = findRect(e.getX(), e.getY(), false); |
Region region2 = findRegion(e.getX(), e.getY(), false); |
|
if(rect2 != null) { // Found one. |
if(region2 != null) { // Found one. |
setCursor(resizeCursor); |
} |
else { // Nothing found. |
428,7 → 523,7 |
*/ |
public void mouseClicked(MouseEvent e) { |
if(e.getSource() == imagePanel && image != null) { |
selectRect(e.getX(), e.getY()); |
selectRegion(e.getX(), e.getY()); |
repaint(); |
} |
} |
445,19 → 540,19 |
showPopup(e.getX(), e.getY()); |
} |
else { // Test for move/resize. |
Rectangle rect1 = findRect(e.getX(), e.getY(), true); // Try move. |
Region region1 = findRegion(e.getX(), e.getY(), true); // Try move. |
|
if(rect1 != null) { // Found one. |
if(region1 != null) { // Found one. |
setCursor(moveCursor); |
setSelectedRect(rect1); |
setSelectedRegion(region1); |
inMove = true; |
} |
else { // Not found - try resize. |
Rectangle rect2 = findRect(e.getX(), e.getY(), false); |
Region region2 = findRegion(e.getX(), e.getY(), false); |
|
if(rect2 != null) { // Found one. |
if(region2 != null) { // Found one. |
setCursor(resizeCursor); |
setSelectedRect(rect2); |
setSelectedRegion(region2); |
inResize = true; |
} |
else { // Nothing found. |
475,7 → 570,7 |
* pressed on a component. |
*/ |
public void mouseReleased(MouseEvent e) { |
|
|
/* If the event was fired by the image panel and an image is available ... */ |
if(e.getSource() == imagePanel && image != null) { |
if(e.isPopupTrigger()) { |
489,7 → 584,7 |
|
/** |
* Empty method needed to implement the <code>MouseListener</code> interface. |
* |
* |
* @see java.awt.event.MouseListener#mouseEntered |
*/ |
public void mouseEntered(MouseEvent e) { |
497,10 → 592,10 |
|
/** |
* Invoked when the mouse exits a component. Will reset the point display |
* int the status panel. |
* int the status panel. |
* |
* @param e: A mouse event indicating that the cursor has left a component. |
* |
* |
* @see java.awt.event.MouseListener#mouseExited |
*/ |
public void mouseExited(MouseEvent e) { |
512,11 → 607,11 |
/** |
* ... |
* |
* @param rect add the given rectangle |
* @param region add the given region |
*/ |
public void addRect(Rectangle rect) |
public void addRegion(Region region) |
{ |
rects.add(rect); |
regions.add(region); |
repaint(); |
} |
|
523,33 → 618,33 |
/** |
* ... |
* |
* @param rect delete the given rectangle |
* @param region delete the given region |
*/ |
public void deleteRect(Rectangle rect) |
public void deleteRegion(Region region) |
{ |
if(selectedRect == rect) setSelectedRect(null); // If it was the selected one. |
rects.remove(rect); |
if(selectedRegion == region) setSelectedRegion(null); // If it was the selected one. |
regions.remove(region); |
repaint(); |
} |
|
/** |
* ... |
* |
* @param x point of rectangle that the user will select |
* @param y point of rectangle that the user will select |
* |
* @param x point of region that the user will select |
* @param y point of region that the user will select |
*/ |
private void selectRect(int x, int y) |
private void selectRegion(int x, int y) |
{ |
int xx = (int)((x - imagePanel.getDx()) * imagePanel.getCoef()); |
int yy = (int)((y - imagePanel.getDy()) * imagePanel.getCoef()); |
|
setSelectedRect(null); // Reset old selection. |
setSelectedRegion(null); // Reset old selection. |
|
for(int i = 0; i < rects.size(); i++) { |
Rectangle rect = (Rectangle)rects.get(i); |
for(int i = 0; i < regions.size(); i++) { |
Region region = (Region)regions.get(i); |
|
if(rect.contains(xx, yy)) { // Found one. |
setSelectedRect(rect); |
if(region.getRect().contains(xx, yy)) { // Found one. |
setSelectedRegion(region); |
break; |
} |
} |
561,24 → 656,25 |
* @param x ... |
* @param y ... |
* @param nordWest ... |
* |
* @return currentRect ... |
* |
* @return |
*/ |
private Rectangle findRect(int x, int y, boolean nordWest) |
private Region findRegion(int x, int y, boolean nordWest) |
{ |
int xx = (int)((x - imagePanel.getDx()) * imagePanel.getCoef()); |
int yy = (int)((y - imagePanel.getDy()) * imagePanel.getCoef()); |
|
Rectangle currentRect = null; |
Region currentRegion = null; |
|
for(int i = 0; i < rects.size(); i++) { |
Rectangle rect = (Rectangle)rects.get(i); |
for(int i = 0; i < regions.size(); i++) { |
Region region = (Region)regions.get(i); |
Rectangle rect = region.getRect(); |
|
if(nordWest) { |
if(Math.abs((int)rect.getX() - xx) < 5 |
&& Math.abs((int)rect.getY() - yy) < 5) |
{ // Found one. |
currentRect = rect; |
currentRegion = region; |
break; |
} |
} |
586,38 → 682,43 |
if(Math.abs((int)rect.getX() + (int)rect.getWidth() - xx) < 5 |
&& Math.abs((int)rect.getY() + (int)rect.getHeight() - yy) < 5) |
{ // Found one. |
currentRect = rect; |
currentRegion = region; |
break; |
} |
} |
} |
|
return currentRect; |
return currentRegion; |
} |
|
/** |
* ... |
* |
* |
* @param x ... |
* @param y ... |
*/ |
private void showPopup(int x, int y) |
{ |
selectRect(x, y); |
selectRegion(x, y); |
repaint(); |
|
// Disable the item if no rectangle is selected. |
deleteSelectedItem.setEnabled(selectedRect != null); |
// Disable the item if no region is selected. |
deleteSelectedItem.setEnabled(selectedRegion != null); |
popup.show(this, x, y); |
} |
|
/** |
* Sets the currently selected rectangle in the image panel. |
* |
* @param rect: The rectangle selected. |
* Sets the currently selected region in the image panel. |
* |
* @param region The region selected. |
*/ |
private void setSelectedRect(Rectangle rect) { |
selectedRect = rect; |
imagePanel.setSelectedRect(rect); |
private void setSelectedRegion(Region region) { |
selectedRegion = region; |
imagePanel.setSelectedRegion(region); |
|
if(selectedRegion == null) |
regionNameField.setText(""); |
else |
regionNameField.setText(selectedRegion.getName()); |
} |
} |